Search in sources :

Example 11 with ByteArrayContent

use of com.google.api.client.http.ByteArrayContent in project google-api-java-client by google.

the class MediaHttpUploaderTest method testResumableMediaUploadWithZeroContent.

public void testResumableMediaUploadWithZeroContent() throws Exception {
    int contentLength = 0;
    MediaTransport fakeTransport = new MediaTransport(contentLength);
    ByteArrayContent mediaContent = new ByteArrayContent(TEST_CONTENT_TYPE, new byte[contentLength]);
    MediaHttpUploader uploader = new MediaHttpUploader(mediaContent, fakeTransport, null);
    uploader.upload(new GenericUrl(TEST_RESUMABLE_REQUEST_URL));
    // There should be 2 calls made. 1 initiation request and 1 upload request.
    assertEquals(2, fakeTransport.lowLevelExecCalls);
}
Also used : GenericUrl(com.google.api.client.http.GenericUrl) ByteArrayContent(com.google.api.client.http.ByteArrayContent)

Example 12 with ByteArrayContent

use of com.google.api.client.http.ByteArrayContent in project google-api-java-client by google.

the class MediaHttpUploader method setContentAndHeadersOnCurrentRequest.

/**
 * Sets the HTTP media content chunk and the required headers that should be used in the upload
 * request.
 */
private void setContentAndHeadersOnCurrentRequest() throws IOException {
    int blockSize;
    if (isMediaLengthKnown()) {
        // We know exactly what the blockSize will be because we know the media content length.
        blockSize = (int) Math.min(chunkSize, getMediaContentLength() - totalBytesServerReceived);
    } else {
        // Use the chunkSize as the blockSize because we do know what what it is yet.
        blockSize = chunkSize;
    }
    AbstractInputStreamContent contentChunk;
    int actualBlockSize = blockSize;
    if (isMediaLengthKnown()) {
        // Mark the current position in case we need to retry the request.
        contentInputStream.mark(blockSize);
        InputStream limitInputStream = ByteStreams.limit(contentInputStream, blockSize);
        contentChunk = new InputStreamContent(mediaContent.getType(), limitInputStream).setRetrySupported(true).setLength(blockSize).setCloseInputStream(false);
        mediaContentLengthStr = String.valueOf(getMediaContentLength());
    } else {
        // If the media content length is not known we implement a custom buffered input stream that
        // enables us to detect the length of the media content when the last chunk is sent. We
        // accomplish this by always trying to read an extra byte further than the end of the current
        // chunk.
        int actualBytesRead;
        int bytesAllowedToRead;
        // amount of bytes which need to be copied from last chunk buffer
        int copyBytes = 0;
        if (currentRequestContentBuffer == null) {
            bytesAllowedToRead = cachedByte == null ? blockSize + 1 : blockSize;
            currentRequestContentBuffer = new byte[blockSize + 1];
            if (cachedByte != null) {
                currentRequestContentBuffer[0] = cachedByte;
            }
        } else {
            // currentRequestContentBuffer is not null that means one of the following:
            // 1. This is a request to recover from a server error (e.g. 503)
            // or
            // 2. The server received less bytes than the amount of bytes the client had sent. For
            // example, the client sends bytes 100-199, but the server returns back status code 308,
            // and its "Range" header is "bytes=0-150".
            // In that case, the new request will be constructed from the previous request's byte buffer
            // plus new bytes from the stream.
            copyBytes = (int) (totalBytesClientSent - totalBytesServerReceived);
            // shift copyBytes bytes to the beginning - those are the bytes which weren't received by
            // the server in the last chunk.
            System.arraycopy(currentRequestContentBuffer, currentChunkLength - copyBytes, currentRequestContentBuffer, 0, copyBytes);
            if (cachedByte != null) {
                // add the last cached byte to the buffer
                currentRequestContentBuffer[copyBytes] = cachedByte;
            }
            bytesAllowedToRead = blockSize - copyBytes;
        }
        actualBytesRead = ByteStreams.read(contentInputStream, currentRequestContentBuffer, blockSize + 1 - bytesAllowedToRead, bytesAllowedToRead);
        if (actualBytesRead < bytesAllowedToRead) {
            actualBlockSize = copyBytes + Math.max(0, actualBytesRead);
            if (cachedByte != null) {
                actualBlockSize++;
                cachedByte = null;
            }
            if (mediaContentLengthStr.equals("*")) {
                // At this point we know we reached the media content length because we either read less
                // than the specified chunk size or there is no more data left to be read.
                mediaContentLengthStr = String.valueOf(totalBytesServerReceived + actualBlockSize);
            }
        } else {
            cachedByte = currentRequestContentBuffer[blockSize];
        }
        contentChunk = new ByteArrayContent(mediaContent.getType(), currentRequestContentBuffer, 0, actualBlockSize);
        totalBytesClientSent = totalBytesServerReceived + actualBlockSize;
    }
    currentChunkLength = actualBlockSize;
    currentRequest.setContent(contentChunk);
    if (actualBlockSize == 0) {
        // No bytes to upload. Either zero content media being uploaded, or a server failure on the
        // last write, even though the write actually succeeded. Either way,
        // mediaContentLengthStr will contain the actual media length.
        currentRequest.getHeaders().setContentRange("bytes */" + mediaContentLengthStr);
    } else {
        currentRequest.getHeaders().setContentRange("bytes " + totalBytesServerReceived + "-" + (totalBytesServerReceived + actualBlockSize - 1) + "/" + mediaContentLengthStr);
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) AbstractInputStreamContent(com.google.api.client.http.AbstractInputStreamContent) InputStreamContent(com.google.api.client.http.InputStreamContent) ByteArrayContent(com.google.api.client.http.ByteArrayContent) AbstractInputStreamContent(com.google.api.client.http.AbstractInputStreamContent)

Example 13 with ByteArrayContent

use of com.google.api.client.http.ByteArrayContent in project druid by druid-io.

the class GoogleStorageTest method testInsert.

@Test
public void testInsert() throws IOException {
    String content = "abcdefghij";
    MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
    response.addHeader("Location", "http://random-path");
    response.setContent("{}");
    MockHttpTransport transport = new MockHttpTransport.Builder().setLowLevelHttpResponse(response).build();
    GoogleStorage googleStorage = makeGoogleStorage(transport);
    googleStorage.insert("bucket", "path", new ByteArrayContent("text/html", StringUtils.toUtf8(content)));
    MockLowLevelHttpRequest request = transport.getLowLevelHttpRequest();
    String actual = request.getContentAsString();
    Assert.assertEquals(content, actual);
}
Also used : MockLowLevelHttpResponse(com.google.api.client.testing.http.MockLowLevelHttpResponse) MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport) ByteArrayContent(com.google.api.client.http.ByteArrayContent) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest) Test(org.junit.Test)

Example 14 with ByteArrayContent

use of com.google.api.client.http.ByteArrayContent in project ddf by codice.

the class PaosInInterceptor method getHttpResponse.

@VisibleForTesting
HttpResponseWrapper getHttpResponse(String responseConsumerURL, String soapResponse, Message message) throws IOException {
    // This used to use the ApacheHttpTransport which appeared to not work with 2 way TLS auth but
    // this one does
    HttpTransport httpTransport = new NetHttpTransport();
    HttpContent httpContent = new ByteArrayContent(TEXT_XML, soapResponse.getBytes("UTF-8"));
    HttpRequest httpRequest = httpTransport.createRequestFactory().buildPostRequest(new GenericUrl(responseConsumerURL), httpContent);
    HttpUnsuccessfulResponseHandler httpUnsuccessfulResponseHandler = getHttpUnsuccessfulResponseHandler(message);
    httpRequest.setUnsuccessfulResponseHandler(httpUnsuccessfulResponseHandler);
    httpRequest.getHeaders().put(SOAP_ACTION, HTTP_WWW_OASIS_OPEN_ORG_COMMITTEES_SECURITY);
    // has 20 second timeout by default
    HttpResponse httpResponse = httpRequest.execute();
    HttpResponseWrapper httpResponseWrapper = new HttpResponseWrapper();
    httpResponseWrapper.statusCode = httpResponse.getStatusCode();
    httpResponseWrapper.content = httpResponse.getContent();
    httpResponseWrapper.headers = httpResponse.getHeaders().entrySet();
    return httpResponseWrapper;
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) HttpTransport(com.google.api.client.http.HttpTransport) NetHttpTransport(com.google.api.client.http.javanet.NetHttpTransport) NetHttpTransport(com.google.api.client.http.javanet.NetHttpTransport) HttpUnsuccessfulResponseHandler(com.google.api.client.http.HttpUnsuccessfulResponseHandler) HttpResponse(com.google.api.client.http.HttpResponse) GenericUrl(com.google.api.client.http.GenericUrl) ByteArrayContent(com.google.api.client.http.ByteArrayContent) HttpContent(com.google.api.client.http.HttpContent) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 15 with ByteArrayContent

use of com.google.api.client.http.ByteArrayContent in project pentaho-kettle by pentaho.

the class GoogleDriveFileObject method doGetOutputStream.

protected OutputStream doGetOutputStream(boolean append) throws Exception {
    final File parent = getName().getParent() != null ? searchFile(getName().getParent().getBaseName(), null) : null;
    ByteArrayOutputStream out = new ByteArrayOutputStream() {

        public void close() throws IOException {
            File file = new File();
            file.setName(getName().getBaseName());
            if (parent != null) {
                file.setParents(Collections.singletonList(parent.getId()));
            }
            ByteArrayContent fileContent = new ByteArrayContent("application/octet-stream", toByteArray());
            if (count > 0) {
                driveService.files().create(file, fileContent).execute();
                ((GoogleDriveFileSystem) getFileSystem()).clearFileFromCache(getName());
            }
        }
    };
    return out;
}
Also used : ByteArrayContent(com.google.api.client.http.ByteArrayContent) File(com.google.api.services.drive.model.File)

Aggregations

ByteArrayContent (com.google.api.client.http.ByteArrayContent)43 GenericUrl (com.google.api.client.http.GenericUrl)24 HttpRequest (com.google.api.client.http.HttpRequest)17 IOException (java.io.IOException)12 Test (org.junit.Test)12 HttpResponse (com.google.api.client.http.HttpResponse)10 HttpRequestFactory (com.google.api.client.http.HttpRequestFactory)7 HttpContent (com.google.api.client.http.HttpContent)6 HttpHeaders (com.google.api.client.http.HttpHeaders)6 Gson (com.google.gson.Gson)6 MockResponse (com.google.api.ads.common.lib.testing.MockResponse)4 MockHttpTransport (com.google.api.client.testing.http.MockHttpTransport)4 MockLowLevelHttpRequest (com.google.api.client.testing.http.MockLowLevelHttpRequest)4 File (com.google.api.services.drive.model.File)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 Credential (com.google.api.client.auth.oauth2.Credential)3 GoogleCredential (com.google.api.client.googleapis.auth.oauth2.GoogleCredential)3 HttpResponseException (com.google.api.client.http.HttpResponseException)3 InputStreamContent (com.google.api.client.http.InputStreamContent)3 AbstractInputStreamContent (com.google.api.client.http.AbstractInputStreamContent)2