@Test public void testUploadIncrementalBatchJobOperations_notFirst_notLast() throws Exception { BatchJobUploadStatus status = new BatchJobUploadStatus(10, URI.create(mockHttpServer.getServerUrl())); String uploadRequestBody = "<mutate>testUpload</mutate>"; when(uploadBodyProvider.getHttpContent(request, false, false)) .thenReturn(new ByteArrayContent(null, uploadRequestBody.getBytes(UTF_8))); mockHttpServer.setMockResponse(new MockResponse("testUploadResponse")); BatchJobUploadResponse response = uploader.uploadIncrementalBatchJobOperations(request, false, status); assertEquals("Should have made one request", 1, mockHttpServer.getAllResponses().size()); String firstRequest = mockHttpServer.getLastResponse().getRequestBody(); String expectedBody = "testUpload"; expectedBody = Strings.padEnd(expectedBody, BatchJobUploader.REQUIRED_CONTENT_LENGTH_INCREMENT, ' '); assertEquals("Request body is incorrect", expectedBody, firstRequest); assertEquals("Request should have succeeded", 200, response.getHttpStatus()); BatchJobUploadStatus expectedStatus = new BatchJobUploadStatus( status.getTotalContentLength() + expectedBody.getBytes(UTF_8).length, URI.create(mockHttpServer.getServerUrl())); BatchJobUploadStatus actualStatus = response.getBatchJobUploadStatus(); assertEquals( "Status total content length is incorrect", expectedStatus.getTotalContentLength(), actualStatus.getTotalContentLength()); assertEquals( "Status resumable upload URI is incorrect", expectedStatus.getResumableUploadUri(), actualStatus.getResumableUploadUri()); }
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"); final BatchJobUploadStatus effectiveStatus; if (batchJobUploadStatus.getTotalContentLength() == 0) { URI uploadUri = initiateResumableUpload(batchJobUploadStatus.getResumableUploadUri()); effectiveStatus = new BatchJobUploadStatus(0, uploadUri); } else { effectiveStatus = batchJobUploadStatus; } 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); }); 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) { 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); } }
BatchJobUploader { 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"); final BatchJobUploadStatus effectiveStatus; if (batchJobUploadStatus.getTotalContentLength() == 0) { URI uploadUri = initiateResumableUpload(batchJobUploadStatus.getResumableUploadUri()); effectiveStatus = new BatchJobUploadStatus(0, uploadUri); } else { effectiveStatus = batchJobUploadStatus; } 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); }); 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) { 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); } } }
BatchJobUploader { 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"); final BatchJobUploadStatus effectiveStatus; if (batchJobUploadStatus.getTotalContentLength() == 0) { URI uploadUri = initiateResumableUpload(batchJobUploadStatus.getResumableUploadUri()); effectiveStatus = new BatchJobUploadStatus(0, uploadUri); } else { effectiveStatus = batchJobUploadStatus; } 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); }); 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) { 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); } } @Inject  BatchJobUploader(
      AdWordsSession session, HttpTransport httpTransport, BatchJobLogger batchJobLogger); }
BatchJobUploader { 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"); final BatchJobUploadStatus effectiveStatus; if (batchJobUploadStatus.getTotalContentLength() == 0) { URI uploadUri = initiateResumableUpload(batchJobUploadStatus.getResumableUploadUri()); effectiveStatus = new BatchJobUploadStatus(0, uploadUri); } else { effectiveStatus = batchJobUploadStatus; } 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); }); 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) { 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); } } @Inject  BatchJobUploader(
      AdWordsSession session, HttpTransport httpTransport, BatchJobLogger batchJobLogger); BatchJobUploadResponse uploadIncrementalBatchJobOperations(
      final BatchJobMutateRequestInterface request, final boolean isLastRequest,
      BatchJobUploadStatus batchJobUploadStatus); }
BatchJobUploader { 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"); final BatchJobUploadStatus effectiveStatus; if (batchJobUploadStatus.getTotalContentLength() == 0) { URI uploadUri = initiateResumableUpload(batchJobUploadStatus.getResumableUploadUri()); effectiveStatus = new BatchJobUploadStatus(0, uploadUri); } else { effectiveStatus = batchJobUploadStatus; } 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); }); 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) { 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); } } @Inject  BatchJobUploader(
      AdWordsSession session, HttpTransport httpTransport, BatchJobLogger batchJobLogger); BatchJobUploadResponse uploadIncrementalBatchJobOperations(
      final BatchJobMutateRequestInterface request, final boolean isLastRequest,
      BatchJobUploadStatus batchJobUploadStatus);  }