/** * Constructs the content range header value for the specified arguments. * @param requestLength the length of the request that's about to be uploaded * @param isLastRequest if the request is the last request for the job * @param batchJobUploadStatus the status of the job <em>before</em> this upload * @return the content range header value, e.g., {@code bytes 0-99/100}, * {@code 100-199/*}, etc. */ @VisibleForTesting String constructContentRangeHeaderValue( long requestLength, boolean isLastRequest, BatchJobUploadStatus batchJobUploadStatus) { Preconditions.checkArgument(requestLength > 0, "Request length %s is <= 0", requestLength); long previousTotalLength = batchJobUploadStatus.getTotalContentLength(); long contentLowerBound = previousTotalLength; long contentUpperBound = previousTotalLength + requestLength - 1; String totalBytesString; if (isLastRequest) { totalBytesString = String.valueOf(contentUpperBound + 1); } else { totalBytesString = "*"; } return String.format("bytes %d-%d/%s", contentLowerBound, contentUpperBound, totalBytesString); } }
/** * Constructs the content range header value for the specified arguments. * @param requestLength the length of the request that's about to be uploaded * @param isLastRequest if the request is the last request for the job * @param batchJobUploadStatus the status of the job <em>before</em> this upload * @return the content range header value, e.g., {@code bytes 0-99/100}, * {@code 100-199/*}, etc. */ @VisibleForTesting String constructContentRangeHeaderValue( long requestLength, boolean isLastRequest, BatchJobUploadStatus batchJobUploadStatus) { Preconditions.checkArgument(requestLength > 0, "Request length %s is <= 0", requestLength); long previousTotalLength = batchJobUploadStatus.getTotalContentLength(); long contentLowerBound = previousTotalLength; long contentUpperBound = previousTotalLength + requestLength - 1; String totalBytesString; if (isLastRequest) { totalBytesString = String.valueOf(contentUpperBound + 1); } else { totalBytesString = "*"; } return String.format("bytes %d-%d/%s", contentLowerBound, contentUpperBound, totalBytesString); } }
/** * Test that verifies that {@link BatchJobUploadStatus} instances are serializable. */ @Test public void testSerializable() throws IOException, ClassNotFoundException { BatchJobUploadStatus status = new BatchJobUploadStatus(500L, URI.create("http://www.example.com/upload")); ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(); ObjectOutputStream outputStream = new ObjectOutputStream(byteOutputStream); outputStream.writeObject(status); outputStream.close(); ObjectInputStream inputStream = new ObjectInputStream(new ByteArrayInputStream(byteOutputStream.toByteArray())); Object deserializedObject = inputStream.readObject(); assertTrue( "Deserialized status should be a BatchJobUploadStatus", deserializedObject instanceof BatchJobUploadStatus); BatchJobUploadStatus deserializedStatus = (BatchJobUploadStatus) deserializedObject; assertEquals("Deserialized status upload URI does not match original", status.getResumableUploadUri(), deserializedStatus.getResumableUploadUri()); assertEquals("Deserialized status total content length does not match original", status.getTotalContentLength(), deserializedStatus.getTotalContentLength()); }
if (batchJobUploadStatus.getTotalContentLength() == 0) { 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); batchJobUploadResponse = new BatchJobUploadResponse( response, effectiveStatus.getTotalContentLength() + httpRequest.getContent().getLength(), effectiveStatus.getResumableUploadUri()); return batchJobUploadResponse; e.getStatusMessage(), effectiveStatus.getTotalContentLength() + contentLength, effectiveStatus.getResumableUploadUri()); return batchJobUploadResponse;
if (batchJobUploadStatus.getTotalContentLength() == 0) { 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); batchJobUploadResponse = new BatchJobUploadResponse( response, effectiveStatus.getTotalContentLength() + httpRequest.getContent().getLength(), effectiveStatus.getResumableUploadUri()); return batchJobUploadResponse; e.getStatusMessage(), effectiveStatus.getTotalContentLength() + contentLength, effectiveStatus.getResumableUploadUri()); return batchJobUploadResponse;
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",
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",
assertEquals( "Status total content length is incorrect", expectedStatus.getTotalContentLength(), actualStatus.getTotalContentLength()); assertEquals( "Status resumable upload URI is incorrect",