Java Code Examples for com.google.api.client.http.HttpRequestInitializer#initialize()

The following examples show how to use com.google.api.client.http.HttpRequestInitializer#initialize() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RequestFactoryModuleTest.java    From nomulus with Apache License 2.0 6 votes vote down vote up
@Test
public void test_provideHttpRequestFactory_localhost() throws Exception {
  // Make sure that localhost creates a request factory with an initializer.
  boolean origIsLocal = RegistryConfig.CONFIG_SETTINGS.get().appEngine.isLocal;
  RegistryConfig.CONFIG_SETTINGS.get().appEngine.isLocal = true;
  try {
    HttpRequestFactory factory =
        RequestFactoryModule.provideHttpRequestFactory(credentialsBundle);
    HttpRequestInitializer initializer = factory.getInitializer();
    assertThat(initializer).isNotNull();
    HttpRequest request = factory.buildGetRequest(new GenericUrl("http://localhost"));
    initializer.initialize(request);
    verifyZeroInteractions(httpRequestInitializer);
  } finally {
    RegistryConfig.CONFIG_SETTINGS.get().appEngine.isLocal = origIsLocal;
  }
}
 
Example 2
Source File: BaseApiService.java    From connector-sdk with Apache License 2.0 5 votes vote down vote up
private static HttpRequestInitializer chainedHttpRequestInitializer(
    HttpRequestInitializer... initializer) {
  return request -> {
    for (HttpRequestInitializer i : initializer) {
      if (i != null) {
        i.initialize(request);
      }
    }
  };
}
 
Example 3
Source File: CloudSearchService.java    From connector-sdk with Apache License 2.0 5 votes vote down vote up
private static HttpRequestInitializer createChainedHttpRequestInitializer(
    HttpRequestInitializer... initializers) {
  return request -> {
    for (HttpRequestInitializer initializer : initializers) {
      if (initializer != null) {
        initializer.initialize(request);
      }
    }
  };
}
 
Example 4
Source File: SearchHelper.java    From connector-sdk with Apache License 2.0 5 votes vote down vote up
private static HttpRequestInitializer createChainedHttpRequestInitializer(
    HttpRequestInitializer... initializers) {
  return request -> {
    for (HttpRequestInitializer initializer : initializers) {
      if (initializer != null) {
        initializer.initialize(request);
      }
    }
  };
}
 
Example 5
Source File: FirebaseMessagingClientImpl.java    From firebase-admin-java with Apache License 2.0 5 votes vote down vote up
private HttpRequestInitializer getBatchRequestInitializer() {
  return new HttpRequestInitializer() {
    @Override
    public void initialize(HttpRequest request) throws IOException {
      HttpRequestInitializer initializer = requestFactory.getInitializer();
      if (initializer != null) {
        initializer.initialize(request);
      }
      request.setResponseInterceptor(responseInterceptor);
    }
  };
}
 
Example 6
Source File: MainActivity.java    From apps-script-mobile-addons with Apache License 2.0 5 votes vote down vote up
/**
 * Extend the given HttpRequestInitializer (usually a Credentials object)
 * with additional initialize() instructions.
 *
 * @param requestInitializer the initializer to copy and adjust; typically
 *         a Credential object
 * @return an initializer with an extended read timeout
 */
private static HttpRequestInitializer setHttpTimeout(
        final HttpRequestInitializer requestInitializer) {
    return new HttpRequestInitializer() {
        @Override
        public void initialize(HttpRequest httpRequest)
                throws java.io.IOException {
            requestInitializer.initialize(httpRequest);
            // This allows the API to call (and avoid timing out on)
            // functions that take up to 30 seconds to complete. Note that
            // the maximum allowed script run time is 6 minutes.
            httpRequest.setReadTimeout(30000);
        }
    };
}
 
Example 7
Source File: JobServiceQuickstart.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
private static HttpRequestInitializer setHttpTimeout(
    final HttpRequestInitializer requestInitializer) {
  return request -> {
    requestInitializer.initialize(request);
    request.setHeaders(new HttpHeaders().set("X-GFE-SSL", "yes"));
    request.setConnectTimeout(1 * 60000); // 1 minute connect timeout
    request.setReadTimeout(1 * 60000); // 1 minute read timeout
  };
}
 
Example 8
Source File: ChainingHttpRequestInitializer.java    From hadoop-connectors with Apache License 2.0 5 votes vote down vote up
@Override
public void initialize(HttpRequest request) throws IOException {
  List<HttpIOExceptionHandler> ioExceptionHandlers = new ArrayList<>();
  List<HttpUnsuccessfulResponseHandler> unsuccessfulResponseHandlers = new ArrayList<>();
  List<HttpExecuteInterceptor> interceptors = new ArrayList<>();
  List<HttpResponseInterceptor> responseInterceptors = new ArrayList<>();
  for (HttpRequestInitializer initializer : initializers) {
    initializer.initialize(request);
    if (request.getIOExceptionHandler() != null) {
      ioExceptionHandlers.add(request.getIOExceptionHandler());
      request.setIOExceptionHandler(null);
    }
    if (request.getUnsuccessfulResponseHandler() != null) {
      unsuccessfulResponseHandlers.add(request.getUnsuccessfulResponseHandler());
      request.setUnsuccessfulResponseHandler(null);
    }
    if (request.getInterceptor() != null) {
      interceptors.add(request.getInterceptor());
      request.setInterceptor(null);
    }
    if (request.getResponseInterceptor() != null) {
      responseInterceptors.add(request.getResponseInterceptor());
      request.setResponseInterceptor(null);
    }
  }
  request.setIOExceptionHandler(
      makeIoExceptionHandler(ioExceptionHandlers));
  request.setUnsuccessfulResponseHandler(
      makeUnsuccessfulResponseHandler(unsuccessfulResponseHandlers));
  request.setInterceptor(
      makeInterceptor(interceptors));
  request.setResponseInterceptor(
      makeResponseInterceptor(responseInterceptors));
}
 
Example 9
Source File: BatchRequestServiceTest.java    From connector-sdk with Apache License 2.0 4 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testFlushSetsHttpInterceptorOnBatchRequest() throws Exception {
  BatchRequestService batchService = setupService();
  batchService.startAsync().awaitRunning();
  assertTrue(batchService.isRunning());
  BatchRequest batchRequest = getMockBatchRequest();
  ArgumentCaptor<HttpRequestInitializer> requestInitializerCaptor =
      ArgumentCaptor.forClass(HttpRequestInitializer.class);

  AsyncRequest<GenericJson> requestToBatch = Mockito.mock(AsyncRequest.class);
  AsyncRequest<GenericJson> requestToBatch2 = Mockito.mock(AsyncRequest.class);
  SettableFutureCallback<GenericJson> callback = Mockito.mock(SettableFutureCallback.class);
  SettableFutureCallback<GenericJson> callback2 = Mockito.mock(SettableFutureCallback.class);
  when(requestToBatch.getCallback()).thenReturn(callback);
  when(requestToBatch2.getCallback()).thenReturn(callback2);

  when(batchRequestHelper.createBatch(requestInitializerCaptor.capture()))
      .thenReturn(batchRequest);

  when(requestToBatch.getStatus()).thenReturn(Status.COMPLETED);
  when(requestToBatch2.getStatus()).thenReturn(Status.COMPLETED);

  batchService.add(requestToBatch);
  batchService.add(requestToBatch2);
  batchService.flush();
  batchService.stopAsync().awaitTerminated();

  // verify the used requestInitializer
  HttpRequestInitializer requestInitializer = requestInitializerCaptor.getValue();
  assertThat(requestInitializer,
      instanceOf(BatchRequestService.EventLoggingRequestInitializer.class));
  HttpRequest testRequest =
      new MockHttpTransport().createRequestFactory().buildDeleteRequest(null);
  requestInitializer.initialize(testRequest);
  testRequest.getInterceptor().intercept(testRequest);
  verify(callback).onStart();
  verify(callback2).onStart();
  BatchPolicy defaultBatchPolicy = new BatchPolicy.Builder().build();
  assertEquals(
      defaultBatchPolicy.getBatchConnectTimeoutSeconds() * 1000,
      testRequest.getConnectTimeout());
  assertEquals(
      defaultBatchPolicy.getBatchReadTimeoutSeconds() * 1000,
      testRequest.getReadTimeout());
}
 
Example 10
Source File: FirebaseRequestInitializer.java    From firebase-admin-java with Apache License 2.0 4 votes vote down vote up
@Override
public void initialize(HttpRequest request) throws IOException {
  for (HttpRequestInitializer initializer : initializers) {
    initializer.initialize(request);
  }
}