Search in sources :

Example 6 with JsonFactory

use of com.google.api.client.json.JsonFactory in project gatk by broadinstitute.

the class ReferenceAPISource method readObject.

private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
    JsonFactory jsonFactory = com.google.api.client.googleapis.util.Utils.getDefaultJsonFactory();
    final Map<String, Reference> refs = new LinkedHashMap<>();
    int size = stream.readInt();
    for (int i = 0; i < size; i++) {
        refs.put(stream.readUTF(), jsonFactory.fromString(stream.readUTF(), Reference.class));
    }
    @SuppressWarnings("unchecked") final Map<String, String> refTable = (Map<String, String>) stream.readObject();
    final String apiKey = (String) stream.readObject();
    this.referenceMap = refs;
    this.referenceNameToIdTable = refTable;
    this.apiKey = apiKey;
}
Also used : JsonFactory(com.google.api.client.json.JsonFactory)

Example 7 with JsonFactory

use of com.google.api.client.json.JsonFactory in project druid by druid-io.

the class GoogleStorageDruidModule method getGoogleStorage.

@Provides
@LazySingleton
public GoogleStorage getGoogleStorage(final GoogleAccountConfig config) throws IOException, GeneralSecurityException {
    LOG.info("Building Cloud Storage Client...");
    HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
    JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
    GoogleCredential credential = GoogleCredential.getApplicationDefault(httpTransport, jsonFactory);
    if (credential.createScopedRequired()) {
        credential = credential.createScoped(StorageScopes.all());
    }
    Storage storage = new Storage.Builder(httpTransport, jsonFactory, credential).setApplicationName(APPLICATION_NAME).build();
    return new GoogleStorage(storage);
}
Also used : HttpTransport(com.google.api.client.http.HttpTransport) GoogleNetHttpTransport(com.google.api.client.googleapis.javanet.GoogleNetHttpTransport) Storage(com.google.api.services.storage.Storage) JsonFactory(com.google.api.client.json.JsonFactory) GoogleCredential(com.google.api.client.googleapis.auth.oauth2.GoogleCredential) LazySingleton(io.druid.guice.LazySingleton) Provides(com.google.inject.Provides)

Example 8 with JsonFactory

use of com.google.api.client.json.JsonFactory in project elasticsearch by elastic.

the class RetryHttpInitializerWrapperTests method testRetryWaitTooLong.

public void testRetryWaitTooLong() throws Exception {
    TimeValue maxWaitTime = TimeValue.timeValueMillis(10);
    int maxRetryTimes = 50;
    FailThenSuccessBackoffTransport fakeTransport = new FailThenSuccessBackoffTransport(HttpStatusCodes.STATUS_CODE_SERVER_ERROR, maxRetryTimes);
    JsonFactory jsonFactory = new JacksonFactory();
    MockGoogleCredential credential = RetryHttpInitializerWrapper.newMockCredentialBuilder().build();
    MockSleeper oneTimeSleeper = new MockSleeper() {

        @Override
        public void sleep(long millis) throws InterruptedException {
            Thread.sleep(maxWaitTime.getMillis());
            // important number, use this to get count
            super.sleep(0);
        }
    };
    RetryHttpInitializerWrapper retryHttpInitializerWrapper = new RetryHttpInitializerWrapper(credential, oneTimeSleeper, maxWaitTime);
    Compute client = new Compute.Builder(fakeTransport, jsonFactory, null).setHttpRequestInitializer(retryHttpInitializerWrapper).setApplicationName("test").build();
    HttpRequest request1 = client.getRequestFactory().buildRequest("Get", new GenericUrl("http://elasticsearch.com"), null);
    try {
        request1.execute();
        fail("Request should fail if wait too long");
    } catch (HttpResponseException e) {
        assertThat(e.getStatusCode(), equalTo(HttpStatusCodes.STATUS_CODE_SERVER_ERROR));
        // should only retry once.
        assertThat(oneTimeSleeper.getCount(), lessThan(maxRetryTimes));
    }
}
Also used : LowLevelHttpRequest(com.google.api.client.http.LowLevelHttpRequest) HttpRequest(com.google.api.client.http.HttpRequest) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest) JsonFactory(com.google.api.client.json.JsonFactory) MockGoogleCredential(com.google.api.client.googleapis.testing.auth.oauth2.MockGoogleCredential) HttpResponseException(com.google.api.client.http.HttpResponseException) GenericUrl(com.google.api.client.http.GenericUrl) JacksonFactory(com.google.api.client.json.jackson2.JacksonFactory) Compute(com.google.api.services.compute.Compute) TimeValue(org.elasticsearch.common.unit.TimeValue) MockSleeper(com.google.api.client.testing.util.MockSleeper)

Example 9 with JsonFactory

use of com.google.api.client.json.JsonFactory in project google-cloud-java by GoogleCloudPlatform.

the class HttpBigQueryRpc method open.

@Override
public String open(JobConfiguration configuration) {
    try {
        Job loadJob = new Job().setConfiguration(configuration);
        StringBuilder builder = new StringBuilder().append(BASE_RESUMABLE_URI).append(options.getProjectId()).append("/jobs");
        GenericUrl url = new GenericUrl(builder.toString());
        url.set("uploadType", "resumable");
        JsonFactory jsonFactory = bigquery.getJsonFactory();
        HttpRequestFactory requestFactory = bigquery.getRequestFactory();
        HttpRequest httpRequest = requestFactory.buildPostRequest(url, new JsonHttpContent(jsonFactory, loadJob));
        httpRequest.getHeaders().set("X-Upload-Content-Value", "application/octet-stream");
        HttpResponse response = httpRequest.execute();
        return response.getHeaders().getLocation();
    } catch (IOException ex) {
        throw translate(ex);
    }
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) HttpRequestFactory(com.google.api.client.http.HttpRequestFactory) JsonFactory(com.google.api.client.json.JsonFactory) HttpResponse(com.google.api.client.http.HttpResponse) GenericUrl(com.google.api.client.http.GenericUrl) JsonHttpContent(com.google.api.client.http.json.JsonHttpContent) IOException(java.io.IOException) Job(com.google.api.services.bigquery.model.Job)

Example 10 with JsonFactory

use of com.google.api.client.json.JsonFactory in project beam by apache.

the class GcsUtilTest method testGetSizeBytesWhenFileNotFoundBatchRetry.

@Test
public void testGetSizeBytesWhenFileNotFoundBatchRetry() throws Exception {
    JsonFactory jsonFactory = new JacksonFactory();
    String contentBoundary = "batch_foobarbaz";
    String contentBoundaryLine = "--" + contentBoundary;
    String endOfContentBoundaryLine = "--" + contentBoundary + "--";
    GenericJson error = new GenericJson().set("error", new GenericJson().set("code", 404));
    error.setFactory(jsonFactory);
    String content = contentBoundaryLine + "\n" + "Content-Type: application/http\n" + "\n" + "HTTP/1.1 404 Not Found\n" + "Content-Length: -1\n" + "\n" + error.toString() + "\n" + "\n" + endOfContentBoundaryLine + "\n";
    thrown.expect(FileNotFoundException.class);
    final LowLevelHttpResponse mockResponse = Mockito.mock(LowLevelHttpResponse.class);
    when(mockResponse.getContentType()).thenReturn("multipart/mixed; boundary=" + contentBoundary);
    // 429: Too many requests, then 200: OK.
    when(mockResponse.getStatusCode()).thenReturn(429, 200);
    when(mockResponse.getContent()).thenReturn(toStream("error"), toStream(content));
    // A mock transport that lets us mock the API responses.
    MockHttpTransport mockTransport = new MockHttpTransport.Builder().setLowLevelHttpRequest(new MockLowLevelHttpRequest() {

        @Override
        public LowLevelHttpResponse execute() throws IOException {
            return mockResponse;
        }
    }).build();
    GcsUtil gcsUtil = gcsOptionsWithTestCredential().getGcsUtil();
    gcsUtil.setStorageClient(new Storage(mockTransport, Transport.getJsonFactory(), new RetryHttpRequestInitializer()));
    gcsUtil.fileSizes(ImmutableList.of(GcsPath.fromComponents("testbucket", "testobject")));
}
Also used : GenericJson(com.google.api.client.json.GenericJson) MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport) Storage(com.google.api.services.storage.Storage) JsonFactory(com.google.api.client.json.JsonFactory) MockLowLevelHttpResponse(com.google.api.client.testing.http.MockLowLevelHttpResponse) LowLevelHttpResponse(com.google.api.client.http.LowLevelHttpResponse) JacksonFactory(com.google.api.client.json.jackson2.JacksonFactory) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest) Test(org.junit.Test)

Aggregations

JsonFactory (com.google.api.client.json.JsonFactory)13 JacksonFactory (com.google.api.client.json.jackson2.JacksonFactory)6 HttpRequest (com.google.api.client.http.HttpRequest)5 HttpTransport (com.google.api.client.http.HttpTransport)5 HttpResponse (com.google.api.client.http.HttpResponse)4 GenericJson (com.google.api.client.json.GenericJson)4 MockHttpTransport (com.google.api.client.testing.http.MockHttpTransport)4 MockLowLevelHttpRequest (com.google.api.client.testing.http.MockLowLevelHttpRequest)4 MockLowLevelHttpResponse (com.google.api.client.testing.http.MockLowLevelHttpResponse)4 GenericUrl (com.google.api.client.http.GenericUrl)3 LowLevelHttpRequest (com.google.api.client.http.LowLevelHttpRequest)3 LowLevelHttpResponse (com.google.api.client.http.LowLevelHttpResponse)3 Storage (com.google.api.services.storage.Storage)3 GoogleCredential (com.google.api.client.googleapis.auth.oauth2.GoogleCredential)2 ErrorInfo (com.google.api.client.googleapis.json.GoogleJsonError.ErrorInfo)2 HttpRequestFactory (com.google.api.client.http.HttpRequestFactory)2 NetHttpTransport (com.google.api.client.http.javanet.NetHttpTransport)2 JsonHttpContent (com.google.api.client.http.json.JsonHttpContent)2 IOException (java.io.IOException)2 Test (org.junit.Test)2