Java Code Examples for com.google.api.client.http.HttpRequest#getUnsuccessfulResponseHandler()

The following examples show how to use com.google.api.client.http.HttpRequest#getUnsuccessfulResponseHandler() . 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: TestApiClientUtils.java    From firebase-admin-java with Apache License 2.0 6 votes vote down vote up
/**
 * Checks whther the given HttpRequestFactory has been configured for authorization and
 * automatic retries.
 *
 * @param requestFactory The HttpRequestFactory to check.
 */
public static void assertAuthAndRetrySupport(HttpRequestFactory requestFactory) {
  assertTrue(requestFactory.getInitializer() instanceof FirebaseRequestInitializer);
  HttpRequest request;
  try {
    request = requestFactory.buildGetRequest(TEST_URL);
  } catch (IOException e) {
    throw new RuntimeException("Failed to initialize request", e);
  }

  // Verify authorization
  assertTrue(request.getHeaders().getAuthorization().startsWith("Bearer "));

  // Verify retry support
  HttpUnsuccessfulResponseHandler retryHandler = request.getUnsuccessfulResponseHandler();
  assertTrue(retryHandler instanceof RetryHandlerDecorator);
  RetryConfig retryConfig = ((RetryHandlerDecorator) retryHandler).getRetryHandler()
      .getRetryConfig();
  assertEquals(DEFAULT_RETRY_CONFIG.getMaxRetries(), retryConfig.getMaxRetries());
  assertEquals(DEFAULT_RETRY_CONFIG.getMaxIntervalMillis(), retryConfig.getMaxIntervalMillis());
  assertFalse(retryConfig.isRetryOnIOExceptions());
  assertEquals(DEFAULT_RETRY_CONFIG.getRetryStatusCodes(), retryConfig.getRetryStatusCodes());
}
 
Example 2
Source File: RetryInitializerTest.java    From firebase-admin-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testRetryOnIOException() throws IOException {
  MockSleeper sleeper = new MockSleeper();
  RetryInitializer initializer = new RetryInitializer(
      retryOnIOAndServiceUnavailableErrors(sleeper));

  CountingLowLevelHttpRequest failingRequest = CountingLowLevelHttpRequest.fromException(
      new IOException("test error"));
  HttpRequest request = TestUtils.createRequest(failingRequest);
  initializer.initialize(request);
  final HttpUnsuccessfulResponseHandler retryHandler = request.getUnsuccessfulResponseHandler();

  try {
    request.execute();
    fail("No exception thrown for HTTP error");
  } catch (IOException e) {
    assertEquals("test error", e.getMessage());
  }

  assertEquals(MAX_RETRIES, sleeper.getCount());
  assertEquals(MAX_RETRIES + 1, failingRequest.getCount());
  assertSame(retryHandler, request.getUnsuccessfulResponseHandler());
}
 
Example 3
Source File: RetryInitializerTest.java    From firebase-admin-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testRetryOnHttpError() throws IOException {
  MockSleeper sleeper = new MockSleeper();
  RetryInitializer initializer = new RetryInitializer(
      retryOnIOAndServiceUnavailableErrors(sleeper));
  CountingLowLevelHttpRequest failingRequest = CountingLowLevelHttpRequest.fromStatus(503);
  HttpRequest request = TestUtils.createRequest(failingRequest);
  initializer.initialize(request);
  final HttpUnsuccessfulResponseHandler retryHandler = request.getUnsuccessfulResponseHandler();

  try {
    request.execute();
    fail("No exception thrown for HTTP error");
  } catch (HttpResponseException e) {
    assertEquals(503, e.getStatusCode());
  }

  assertEquals(MAX_RETRIES, sleeper.getCount());
  assertEquals(MAX_RETRIES + 1, failingRequest.getCount());
  assertSame(retryHandler, request.getUnsuccessfulResponseHandler());
}
 
Example 4
Source File: ApiClientUtilsTest.java    From firebase-admin-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testAuthorizedHttpClient() throws IOException {
  FirebaseApp app = FirebaseApp.initializeApp(TEST_OPTIONS);

  HttpRequestFactory requestFactory = ApiClientUtils.newAuthorizedRequestFactory(app);

  assertTrue(requestFactory.getInitializer() instanceof FirebaseRequestInitializer);
  HttpRequest request = requestFactory.buildGetRequest(TEST_URL);
  assertEquals("Bearer test-token", request.getHeaders().getAuthorization());
  HttpUnsuccessfulResponseHandler retryHandler = request.getUnsuccessfulResponseHandler();
  assertTrue(retryHandler instanceof RetryHandlerDecorator);
  RetryConfig retryConfig = ((RetryHandlerDecorator) retryHandler).getRetryHandler()
      .getRetryConfig();
  assertEquals(4, retryConfig.getMaxRetries());
  assertEquals(60 * 1000, retryConfig.getMaxIntervalMillis());
  assertFalse(retryConfig.isRetryOnIOExceptions());
  assertEquals(retryConfig.getRetryStatusCodes(), ImmutableList.of(500, 503));
}
 
Example 5
Source File: RetryInitializerTest.java    From firebase-admin-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testMaxRetriesCountIsCumulative() throws IOException {
  MockSleeper sleeper = new MockSleeper();
  RetryInitializer initializer = new RetryInitializer(
      retryOnIOAndServiceUnavailableErrors(sleeper));

  final AtomicInteger counter = new AtomicInteger(0);
  MockLowLevelHttpRequest failingRequest = new MockLowLevelHttpRequest(){
    @Override
    public LowLevelHttpResponse execute() throws IOException {
      if (counter.getAndIncrement() < 2) {
        throw new IOException("test error");
      } else {
        return new MockLowLevelHttpResponse().setStatusCode(503).setZeroContent();
      }
    }
  };
  HttpRequest request = TestUtils.createRequest(failingRequest);
  initializer.initialize(request);
  final HttpUnsuccessfulResponseHandler retryHandler = request.getUnsuccessfulResponseHandler();

  try {
    request.execute();
    fail("No exception thrown for HTTP error");
  } catch (HttpResponseException e) {
    assertEquals(503, e.getStatusCode());
  }

  assertEquals(MAX_RETRIES, sleeper.getCount());
  assertEquals(MAX_RETRIES + 1, counter.get());
  assertSame(retryHandler, request.getUnsuccessfulResponseHandler());
}
 
Example 6
Source File: ApiClientUtilsTest.java    From firebase-admin-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testAuthorizedHttpClientWithoutRetry() throws IOException {
  FirebaseApp app = FirebaseApp.initializeApp(TEST_OPTIONS);

  HttpRequestFactory requestFactory = ApiClientUtils.newAuthorizedRequestFactory(app, null);

  assertTrue(requestFactory.getInitializer() instanceof FirebaseRequestInitializer);
  HttpRequest request = requestFactory.buildGetRequest(TEST_URL);
  assertEquals("Bearer test-token", request.getHeaders().getAuthorization());
  HttpUnsuccessfulResponseHandler retryHandler = request.getUnsuccessfulResponseHandler();
  assertFalse(retryHandler instanceof RetryHandlerDecorator);
}
 
Example 7
Source File: ApiClientUtilsTest.java    From firebase-admin-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testUnauthorizedHttpClient() throws IOException {
  FirebaseApp app = FirebaseApp.initializeApp(TEST_OPTIONS);

  HttpRequestFactory requestFactory = ApiClientUtils.newUnauthorizedRequestFactory(app);

  assertNull(requestFactory.getInitializer());
  HttpRequest request = requestFactory.buildGetRequest(TEST_URL);
  assertNull(request.getHeaders().getAuthorization());
  HttpUnsuccessfulResponseHandler retryHandler = request.getUnsuccessfulResponseHandler();
  assertNull(retryHandler);
}
 
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: MediaUploadErrorHandler.java    From google-api-java-client with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs a new instance from {@link MediaHttpUploader} and {@link HttpRequest}.
 */
public MediaUploadErrorHandler(MediaHttpUploader uploader, HttpRequest request) {
  this.uploader = Preconditions.checkNotNull(uploader);
  originalIOExceptionHandler = request.getIOExceptionHandler();
  originalUnsuccessfulHandler = request.getUnsuccessfulResponseHandler();

  request.setIOExceptionHandler(this);
  request.setUnsuccessfulResponseHandler(this);
}