Search in sources :

Example 26 with ByteArrayContent

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

the class BatchRequestTest method testExecute_checkWriteTo.

public void testExecute_checkWriteTo() throws Exception {
    String request1Method = HttpMethods.POST;
    String request1Url = "http://test/dummy/url1";
    String request1ContentType = "application/json";
    String request1Content = "{\"data\":{\"foo\":{\"v1\":{}}}}";
    String request2Method = HttpMethods.GET;
    String request2Url = "http://test/dummy/url2";
    final StringBuilder expectedOutput = new StringBuilder();
    expectedOutput.append("--__END_OF_PART__\r\n");
    expectedOutput.append("Content-Length: 118\r\n");
    expectedOutput.append("Content-Type: application/http\r\n");
    expectedOutput.append("content-id: 1\r\n");
    expectedOutput.append("content-transfer-encoding: binary\r\n");
    expectedOutput.append("\r\n");
    expectedOutput.append("POST http://test/dummy/url1 HTTP/1.1\r\n");
    expectedOutput.append("Content-Length: 26\r\n");
    expectedOutput.append("Content-Type: " + request1ContentType + "\r\n");
    expectedOutput.append("\r\n");
    expectedOutput.append(request1Content + "\r\n");
    expectedOutput.append("--__END_OF_PART__\r\n");
    expectedOutput.append("Content-Length: 39\r\n");
    expectedOutput.append("Content-Type: application/http\r\n");
    expectedOutput.append("content-id: 2\r\n");
    expectedOutput.append("content-transfer-encoding: binary\r\n");
    expectedOutput.append("\r\n");
    expectedOutput.append("GET http://test/dummy/url2 HTTP/1.1\r\n");
    expectedOutput.append("\r\n");
    expectedOutput.append("\r\n");
    expectedOutput.append("--__END_OF_PART__--\r\n");
    MockHttpTransport transport = new MockHttpTransport();
    HttpRequest request1 = transport.createRequestFactory().buildRequest(request1Method, new GenericUrl(request1Url), new ByteArrayContent(request1ContentType, request1Content.getBytes(UTF_8)));
    HttpRequest request2 = transport.createRequestFactory().buildRequest(request2Method, new GenericUrl(request2Url), null);
    subtestExecute_checkWriteTo(expectedOutput.toString(), request1, request2);
}
Also used : LowLevelHttpRequest(com.google.api.client.http.LowLevelHttpRequest) HttpRequest(com.google.api.client.http.HttpRequest) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest) MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport) GenericUrl(com.google.api.client.http.GenericUrl) ByteArrayContent(com.google.api.client.http.ByteArrayContent)

Example 27 with ByteArrayContent

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

the class MediaHttpUploaderTest method testDirectUploadServerErrorWithBackOffDisabled.

public void testDirectUploadServerErrorWithBackOffDisabled() throws Exception {
    int contentLength = MediaHttpUploader.DEFAULT_CHUNK_SIZE * 2;
    MediaTransport fakeTransport = new MediaTransport(contentLength);
    fakeTransport.testServerError = true;
    fakeTransport.directUploadEnabled = true;
    ByteArrayContent mediaContent = new ByteArrayContent(TEST_CONTENT_TYPE, new byte[contentLength]);
    MediaHttpUploader uploader = new MediaHttpUploader(mediaContent, fakeTransport, null);
    uploader.setDirectUploadEnabled(true);
    uploader.upload(new GenericUrl(TEST_DIRECT_REQUEST_URL));
    // should be 1 call made: 1 upload request with server error
    assertEquals(1, fakeTransport.lowLevelExecCalls);
}
Also used : GenericUrl(com.google.api.client.http.GenericUrl) ByteArrayContent(com.google.api.client.http.ByteArrayContent)

Example 28 with ByteArrayContent

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

the class MediaHttpUploaderTest method testDirectMediaUploadWithZeroContent.

public void testDirectMediaUploadWithZeroContent() throws Exception {
    int contentLength = 0;
    MediaTransport fakeTransport = new MediaTransport(contentLength);
    fakeTransport.directUploadEnabled = true;
    ByteArrayContent mediaContent = new ByteArrayContent(TEST_CONTENT_TYPE, new byte[contentLength]);
    MediaHttpUploader uploader = new MediaHttpUploader(mediaContent, fakeTransport, null);
    uploader.setDirectUploadEnabled(true);
    uploader.upload(new GenericUrl(TEST_DIRECT_REQUEST_URL));
    // There should be only 1 call made for direct media upload.
    assertEquals(1, fakeTransport.lowLevelExecCalls);
}
Also used : GenericUrl(com.google.api.client.http.GenericUrl) ByteArrayContent(com.google.api.client.http.ByteArrayContent)

Example 29 with ByteArrayContent

use of com.google.api.client.http.ByteArrayContent in project halyard by spinnaker.

the class GoogleWriteableProfileRegistry method writeTextObject.

private void writeTextObject(String name, String contents) {
    try {
        byte[] bytes = contents.getBytes();
        StorageObject object = new StorageObject().setBucket(spinconfigBucket).setName(name);
        ByteArrayContent content = new ByteArrayContent("application/text", bytes);
        storage.objects().insert(spinconfigBucket, object, content).execute();
    } catch (IOException e) {
        log.error("Failed to write new object " + name, e);
        throw new HalException(new ProblemBuilder(Severity.FATAL, "Failed to write to " + name + ": " + e.getMessage()).build());
    }
}
Also used : StorageObject(com.google.api.services.storage.model.StorageObject) HalException(com.netflix.spinnaker.halyard.core.error.v1.HalException) IOException(java.io.IOException) ByteArrayContent(com.google.api.client.http.ByteArrayContent) ProblemBuilder(com.netflix.spinnaker.halyard.core.problem.v1.ProblemBuilder)

Example 30 with ByteArrayContent

use of com.google.api.client.http.ByteArrayContent in project wildfly-camel by wildfly-extras.

the class GoogleDriveIntegrationTest method uploadTestFile.

private static File uploadTestFile(ProducerTemplate template, String testName) {
    File fileMetadata = new File();
    fileMetadata.setTitle(GoogleDriveIntegrationTest.class.getName() + "." + testName + "-" + UUID.randomUUID().toString());
    final String content = // 
    "Camel rocks!\n" + DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(ZonedDateTime.now()) + // 
    "\n" + "user: " + System.getProperty("user.name");
    HttpContent mediaContent = new ByteArrayContent("text/plain", content.getBytes(StandardCharsets.UTF_8));
    final Map<String, Object> headers = new HashMap<>();
    // parameter type is com.google.api.services.drive.model.File
    headers.put("CamelGoogleDrive.content", fileMetadata);
    // parameter type is com.google.api.client.http.AbstractInputStreamContent
    headers.put("CamelGoogleDrive.mediaContent", mediaContent);
    return template.requestBodyAndHeaders("google-drive://drive-files/insert", null, headers, File.class);
}
Also used : HashMap(java.util.HashMap) ByteArrayContent(com.google.api.client.http.ByteArrayContent) File(com.google.api.services.drive.model.File) HttpContent(com.google.api.client.http.HttpContent)

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