public BatchJobUploadResponse uploadIncrementalBatchJobOperations ( final BatchJobMutateRequestInterface request , final boolean isLastRequest , BatchJobUploadStatus batchJobUploadStatus ) throws BatchJobException { Preconditions . checkNotNull ( batchJobUploadStatus , "Null batch job upload status" ) ; Preconditions . checkNotNull ( batchJobUploadStatus . getResumableUploadUri ( ) , "No resumable session URI" ) ; // This reference is final because it is referenced below within an anonymous class. final BatchJobUploadStatus effectiveStatus ; if ( batchJobUploadStatus . getTotalContentLength ( ) == 0 ) { // If this is the first upload, then issue a request to get the resumable session URI from // Google Cloud Storage. URI uploadUri = initiateResumableUpload ( batchJobUploadStatus . getResumableUploadUri ( ) ) ; effectiveStatus = new BatchJobUploadStatus ( 0 , uploadUri ) ; } else { effectiveStatus = batchJobUploadStatus ; } // The process below follows the Google Cloud Storage guidelines for resumable // uploads of unknown size: // https://cloud.google.com/storage/docs/concepts-techniques#unknownresumables ByteArrayContent content = request . createBatchJobUploadBodyProvider ( ) . getHttpContent ( request , effectiveStatus . getTotalContentLength ( ) == 0 , isLastRequest ) ; try { content = postProcessContent ( content , effectiveStatus . getTotalContentLength ( ) == 0L , isLastRequest ) ; } catch ( IOException e ) { throw new BatchJobException ( "Failed to post-process the request content" , e ) ; } String requestXml = null ; Throwable exception = null ; BatchJobUploadResponse batchJobUploadResponse = null ; final long contentLength = content . getLength ( ) ; try { HttpRequestFactory requestFactory = httpTransport . createRequestFactory ( req -> { HttpHeaders headers = createHttpHeaders ( ) ; headers . setContentLength ( contentLength ) ; headers . setContentRange ( constructContentRangeHeaderValue ( contentLength , isLastRequest , effectiveStatus ) ) ; req . setHeaders ( headers ) ; req . setLoggingEnabled ( true ) ; } ) ; // Incremental uploads require a PUT request. HttpRequest httpRequest = requestFactory . buildPutRequest ( new GenericUrl ( effectiveStatus . getResumableUploadUri ( ) ) , content ) ; requestXml = Streams . readAll ( content . getInputStream ( ) , UTF_8 ) ; content . getInputStream ( ) . reset ( ) ; HttpResponse response = httpRequest . execute ( ) ; batchJobUploadResponse = new BatchJobUploadResponse ( response , effectiveStatus . getTotalContentLength ( ) + httpRequest . getContent ( ) . getLength ( ) , effectiveStatus . getResumableUploadUri ( ) ) ; return batchJobUploadResponse ; } catch ( HttpResponseException e ) { if ( e . getStatusCode ( ) == 308 ) { // 308 indicates that the upload succeeded. batchJobUploadResponse = new BatchJobUploadResponse ( new ByteArrayInputStream ( new byte [ 0 ] ) , e . getStatusCode ( ) , e . getStatusMessage ( ) , effectiveStatus . getTotalContentLength ( ) + contentLength , effectiveStatus . getResumableUploadUri ( ) ) ; return batchJobUploadResponse ; } exception = e ; throw new BatchJobException ( "Failed response status from batch upload URL." , e ) ; } catch ( IOException e ) { exception = e ; throw new BatchJobException ( "Problem sending data to batch upload URL." , e ) ; } finally { batchJobLogger . logUpload ( requestXml , effectiveStatus . getResumableUploadUri ( ) , batchJobUploadResponse , exception ) ; } }
Incrementally uploads a batch job s operations and returns the response .