Ejemplo n.º 1
0
        /// <summary>
        /// Gets the upload progress.
        /// </summary>
        /// <param name="url">The resumable upload URL.</param>
        /// <param name="totalLength">The total length of upload.</param>
        /// <returns>The number of bytes uploaded so far.</returns>
        private int GetUploadProgress(string url, int totalLength)
        {
            int retval = 0;
            BulkJobErrorHandler errorHandler = new BulkJobErrorHandler(user);

            while (true)
            {
                WebResponse response = null;
                WebRequest  request  = HttpUtilities.BuildRangeRequest(url, 0,
                                                                       string.Format("bytes */{0}", totalLength), user.Config);

                LogEntry logEntry = new LogEntry(User.Config, new DefaultDateTimeProvider());
                logEntry.LogRequest(request, "Truncated", HEADERS_TO_MASK);

                try {
                    response = request.GetResponse();
                } catch (WebException e) {
                    if (IsPartialUploadSuccessResponse(e))
                    {
                        retval = ExtractUpperRange(e.Response.Headers["Range"], retval);

                        logEntry.LogResponse(e.Response, true, "");
                        logEntry.Flush();
                        break;
                    }
                    else
                    {
                        HandleCloudException(errorHandler, logEntry, e);
                    }
                }
            }
            return(retval);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Uploads the operations to a specified URL.
        /// </summary>
        /// <param name="url">The temporary URL returned by a batch job.</param>
        /// <param name="operations">The list of operations.</param>
        public void Upload(string url, Operation[] operations)
        {
            BulkJobErrorHandler errorHandler = new BulkJobErrorHandler(user);
            string postBody = GetPostBody(operations);

            while (true)
            {
                WebResponse    response = null;
                HttpWebRequest request  = BuildRequest(url, postBody);

                LogEntry logEntry = new LogEntry(User.Config, new DefaultDateTimeProvider());
                logEntry.LogRequest(request, postBody, HEADERS_TO_MASK);

                try {
                    response = request.GetResponse();
                    string contents = MediaUtilities.GetStreamContentsAsString(
                        response.GetResponseStream());
                    logEntry.LogResponse(response, false, contents);
                    logEntry.Flush();
                    break;
                } catch (WebException e) {
                    HandleCloudException(errorHandler, logEntry, e);
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Downloads the batch job results from a specified URL.
        /// </summary>
        /// <param name="url">The download URL from a batch job.</param>
        /// <returns>The results from the batch job.</returns>
        public BatchJobMutateResponse Download(string url)
        {
            BulkJobErrorHandler errorHandler = new BulkJobErrorHandler(user);

            while (true)
            {
                WebRequest request = HttpUtilities.BuildRequest(url, "GET", user.Config);

                WebResponse response = null;

                LogEntry logEntry = new LogEntry(User.Config, new DefaultDateTimeProvider());
                logEntry.LogRequest(request, "", HEADERS_TO_MASK);

                try {
                    response = request.GetResponse();
                    string contents = MediaUtilities.GetStreamContentsAsString(
                        response.GetResponseStream());
                    logEntry.LogResponse(response, false, contents);
                    logEntry.Flush();

                    return(ParseResponse(contents));
                } catch (WebException e) {
                    HandleCloudException(errorHandler, logEntry, e);
                }
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Handles the exception from Google Cloud Storage servers when uploading
        /// operations.
        /// </summary>
        /// <param name="errorHandler">The error handler.</param>
        /// <param name="logEntry">The log entry.</param>
        /// <param name="e">The web exception that was thrown by the server.</param>
        private void HandleCloudException(BulkJobErrorHandler errorHandler, LogEntry logEntry,
                                          WebException e)
        {
            string    contents          = "";
            Exception downloadException = null;

            using (WebResponse response = e.Response) {
                try {
                    contents = MediaUtilities.GetStreamContentsAsString(
                        response.GetResponseStream());
                } catch {
                    contents = e.Message;
                }

                logEntry.LogResponse(response, true, contents);
                logEntry.Flush();

                downloadException = ParseException(e, contents);
            }
            if (errorHandler.ShouldRetry(downloadException))
            {
                errorHandler.PrepareForRetry(downloadException);
            }
            else
            {
                throw downloadException;
            }
            return;
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Uploads a chunk of data for the batch job.
        /// </summary>
        /// <param name="url">The resumable upload URL.</param>
        /// <param name="postBody">The post body.</param>
        /// <param name="start">The start of range of bytes to be uploaded.</param>
        /// <param name="end">The end of range of bytes to be uploaded.</param>
        private void UploadChunk(string url, byte[] postBody, int start, int end)
        {
            BulkJobErrorHandler errorHandler = new BulkJobErrorHandler(user);

            while (true)
            {
                WebResponse response = null;
                LogEntry    logEntry = new LogEntry(User.Config, new DefaultDateTimeProvider());

                int            bytesToWrite = end - start + 1;
                HttpWebRequest request      = (HttpWebRequest)HttpUtilities.BuildRangeRequest(
                    url, bytesToWrite,
                    string.Format("bytes {0}-{1}/{2}", start, end, postBody.Length), user.Config);

                request.ContentType = "application/xml";

                try {
                    using (Stream requestStream = request.GetRequestStream()) {
                        requestStream.Write(postBody, start, bytesToWrite);
                    }

                    logEntry.LogRequest(request, "Truncated", HEADERS_TO_MASK);

                    response = request.GetResponse();

                    logEntry.LogResponse(response, true, "");
                    logEntry.Flush();
                    return;
                } catch (WebException e) {
                    response = e.Response;
                    if (IsPartialUploadSuccessResponse(e))
                    {
                        logEntry.LogResponse(e.Response, true, "");
                        logEntry.Flush();
                        return;
                    }
                    else
                    {
                        HandleCloudException(errorHandler, logEntry, e);
                    }
                }
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Handles the exception from Google Cloud Storage servers when uploading
        /// operations.
        /// </summary>
        /// <param name="errorHandler">The error handler.</param>
        /// <param name="logEntry">The log entry.</param>
        /// <param name="e">The web exception that was thrown by the server.</param>
        /// <returns>True if this is a success, false if this was a server error.
        /// </returns>
        private void HandleCloudException(BulkJobErrorHandler errorHandler, LogEntry logEntry,
                                          WebException e)
        {
            Exception downloadException = null;

            using (WebResponse response = e.Response) {
                string contents = HttpUtilities.GetErrorResponseBody(e);

                logEntry.LogResponse(response, false, contents);
                logEntry.Flush();

                downloadException = ParseException(e, contents);

                if (errorHandler.ShouldRetry(downloadException))
                {
                    errorHandler.PrepareForRetry(downloadException);
                }
                else
                {
                    throw downloadException;
                }
            }
        }
        /// <summary>
        /// Uploads a chunk of data for the batch job.
        /// </summary>
        /// <param name="url">The resumable upload URL.</param>
        /// <param name="postBody">The post body.</param>
        /// <param name="start">The start of range of bytes to be uploaded.</param>
        /// <param name="end">The end of range of bytes to be uploaded.</param>
        private void UploadChunk(string url, byte[] postBody, int start, int end)
        {
            BulkJobErrorHandler errorHandler = new BulkJobErrorHandler(user);

              while (true) {
            WebResponse response = null;
            LogEntry logEntry = new LogEntry(User.Config, new DefaultDateTimeProvider());

            int bytesToWrite = end - start + 1;
            HttpWebRequest request = (HttpWebRequest) HttpUtilities.BuildRangeRequest(
            url, bytesToWrite,
            string.Format("bytes {0}-{1}/{2}", start, end, postBody.Length), user.Config);

            request.ContentType = "application/xml";

            try {
              using (Stream requestStream = request.GetRequestStream()) {
            requestStream.Write(postBody, start, bytesToWrite);
              }

              logEntry.LogRequest(request, "Truncated", HEADERS_TO_MASK);

              response = request.GetResponse();

              logEntry.LogResponse(response, true, "");
              logEntry.Flush();
              return;
            } catch (WebException e) {
              response = e.Response;
              if (IsPartialUploadSuccessResponse(e)) {
            logEntry.LogResponse(e.Response, true, "");
            logEntry.Flush();
            return;
              } else {
            HandleCloudException(errorHandler, logEntry, e);
              }
            }
              }
        }
        /// <summary>
        /// Handles the exception from Google Cloud Storage servers when uploading
        /// operations.
        /// </summary>
        /// <param name="errorHandler">The error handler.</param>
        /// <param name="logEntry">The log entry.</param>
        /// <param name="e">The web exception that was thrown by the server.</param>
        /// <returns>True if this is a success, false if this was a server error.
        /// </returns>
        private void HandleCloudException(BulkJobErrorHandler errorHandler, LogEntry logEntry,
        WebException e)
        {
            Exception downloadException = null;

              using (WebResponse response = e.Response) {
            string contents = HttpUtilities.GetErrorResponseBody(e);

            logEntry.LogResponse(response, false, contents);
            logEntry.Flush();

            downloadException = ParseException(e, contents);

            if (errorHandler.ShouldRetry(downloadException)) {
              errorHandler.PrepareForRetry(downloadException);
            } else {
              throw downloadException;
            }
              }
        }
        /// <summary>
        /// Gets the upload progress.
        /// </summary>
        /// <param name="url">The resumable upload URL.</param>
        /// <param name="totalLength">The total length of upload.</param>
        /// <returns>The number of bytes uploaded so far.</returns>
        private int GetUploadProgress(string url, int totalLength)
        {
            int retval = 0;
              BulkJobErrorHandler errorHandler = new BulkJobErrorHandler(user);
              while (true) {
            WebResponse response = null;
            WebRequest request = HttpUtilities.BuildRangeRequest(url, 0,
            string.Format("bytes */{0}", totalLength), user.Config);

            LogEntry logEntry = new LogEntry(User.Config, new DefaultDateTimeProvider());
            logEntry.LogRequest(request, "Truncated", HEADERS_TO_MASK);

            try {
              response = request.GetResponse();
            } catch (WebException e) {
              if (IsPartialUploadSuccessResponse(e)) {
            retval = ExtractUpperRange(e.Response.Headers["Range"], retval);

            logEntry.LogResponse(e.Response, true, "");
            logEntry.Flush();
            break;
              } else {
            HandleCloudException(errorHandler, logEntry, e);
              }
            }
              }
              return retval;
        }
        /// <summary>
        /// Downloads the batch job results from a specified URL.
        /// </summary>
        /// <param name="url">The download URL from a batch job.</param>
        /// <returns>The results from the batch job.</returns>
        public BatchJobMutateResponse Download(string url)
        {
            BulkJobErrorHandler errorHandler = new BulkJobErrorHandler(user);

              while (true) {
            WebRequest request = HttpUtilities.BuildRequest(url, "GET", user.Config);

            WebResponse response = null;

            LogEntry logEntry = new LogEntry(User.Config, new DefaultDateTimeProvider());
            logEntry.LogRequest(request, "", HEADERS_TO_MASK);

            try {
              response = request.GetResponse();
              string contents = MediaUtilities.GetStreamContentsAsString(
              response.GetResponseStream());
              logEntry.LogResponse(response, false, contents);
              logEntry.Flush();

              return ParseResponse(contents);
            } catch (WebException e) {
              HandleCloudException(errorHandler, logEntry, e);
            }
              }
        }