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

The following examples show how to use com.google.api.client.http.HttpRequest#setUnsuccessfulResponseHandler() . 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: RetryHttpInitializerWrapper.java    From deployment-examples with MIT License 6 votes vote down vote up
/** Initializes the given request. */
@Override
public final void initialize(final HttpRequest request) {
  request.setReadTimeout(2 * ONEMINITUES); // 2 minutes read timeout
  final HttpUnsuccessfulResponseHandler backoffHandler =
      new HttpBackOffUnsuccessfulResponseHandler(new ExponentialBackOff()).setSleeper(sleeper);
  request.setInterceptor(wrappedCredential);
  request.setUnsuccessfulResponseHandler(
      (request1, response, supportsRetry) -> {
        if (wrappedCredential.handleResponse(request1, response, supportsRetry)) {
          // If credential decides it can handle it, the return code or message indicated
          // something specific to authentication, and no backoff is desired.
          return true;
        } else if (backoffHandler.handleResponse(request1, response, supportsRetry)) {
          // Otherwise, we defer to the judgement of our internal backoff handler.
          LOG.info("Retrying " + request1.getUrl().toString());
          return true;
        } else {
          return false;
        }
      });
  request.setIOExceptionHandler(
      new HttpBackOffIOExceptionHandler(new ExponentialBackOff()).setSleeper(sleeper));
}
 
Example 2
Source File: RetryHttpInitializerWrapper.java    From beam with Apache License 2.0 6 votes vote down vote up
/** Initializes the given request. */
@Override
public final void initialize(final HttpRequest request) {
  request.setReadTimeout(2 * ONEMINITUES); // 2 minutes read timeout
  final HttpUnsuccessfulResponseHandler backoffHandler =
      new HttpBackOffUnsuccessfulResponseHandler(new ExponentialBackOff()).setSleeper(sleeper);
  request.setInterceptor(wrappedCredential);
  request.setUnsuccessfulResponseHandler(
      (request1, response, supportsRetry) -> {
        if (wrappedCredential.handleResponse(request1, response, supportsRetry)) {
          // If credential decides it can handle it, the return code or message indicated
          // something specific to authentication, and no backoff is desired.
          return true;
        } else if (backoffHandler.handleResponse(request1, response, supportsRetry)) {
          // Otherwise, we defer to the judgement of our internal backoff handler.
          LOG.info("Retrying " + request1.getUrl().toString());
          return true;
        } else {
          return false;
        }
      });
  request.setIOExceptionHandler(
      new HttpBackOffIOExceptionHandler(new ExponentialBackOff()).setSleeper(sleeper));
}
 
Example 3
Source File: RetryInitializer.java    From firebase-admin-java with Apache License 2.0 6 votes vote down vote up
@Override
public boolean handleResponse(
    HttpRequest request,
    HttpResponse response,
    boolean supportsRetry) throws IOException {
  try {
    boolean retry = preRetryHandler.handleResponse(request, response, supportsRetry);
    if (!retry) {
      retry = retryHandler.handleResponse(request, response, supportsRetry);
    }
    return retry;
  } finally {
    // Pre-retry handler may have reset the unsuccessful response handler on the
    // request. This changes it back.
    request.setUnsuccessfulResponseHandler(this);
  }
}
 
Example 4
Source File: RetryUnsuccessfulResponseHandlerTest.java    From firebase-admin-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testDoesNotRetryOnUnspecifiedHttpStatus() throws IOException {
  MultipleCallSleeper sleeper = new MultipleCallSleeper();
  RetryUnsuccessfulResponseHandler handler = new RetryUnsuccessfulResponseHandler(
      testRetryConfig(sleeper));
  CountingLowLevelHttpRequest failingRequest = CountingLowLevelHttpRequest.fromStatus(404);
  HttpRequest request = TestUtils.createRequest(failingRequest);
  request.setUnsuccessfulResponseHandler(handler);
  request.setNumberOfRetries(MAX_RETRIES);

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

  assertEquals(0, sleeper.getCount());
  assertEquals(1, failingRequest.getCount());
}
 
Example 5
Source File: RetryUnsuccessfulResponseHandlerTest.java    From firebase-admin-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testRetryOnHttpClientErrorWhenSpecified() throws IOException {
  MultipleCallSleeper sleeper = new MultipleCallSleeper();
  RetryUnsuccessfulResponseHandler handler = new RetryUnsuccessfulResponseHandler(
      testRetryConfig(sleeper));
  CountingLowLevelHttpRequest failingRequest = CountingLowLevelHttpRequest.fromStatus(429);
  HttpRequest request = TestUtils.createRequest(failingRequest);
  request.setUnsuccessfulResponseHandler(handler);
  request.setNumberOfRetries(MAX_RETRIES);

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

  assertEquals(MAX_RETRIES, sleeper.getCount());
  assertArrayEquals(new long[]{500, 1000, 2000, 4000}, sleeper.getDelays());
  assertEquals(MAX_RETRIES + 1, failingRequest.getCount());
}
 
Example 6
Source File: RetryUnsuccessfulResponseHandlerTest.java    From firebase-admin-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testExponentialBackOffDoesNotExceedMaxInterval() throws IOException {
  MultipleCallSleeper sleeper = new MultipleCallSleeper();
  RetryUnsuccessfulResponseHandler handler = new RetryUnsuccessfulResponseHandler(
      testRetryConfig(sleeper));
  CountingLowLevelHttpRequest failingRequest = CountingLowLevelHttpRequest.fromStatus(503);
  HttpRequest request = TestUtils.createRequest(failingRequest);
  request.setUnsuccessfulResponseHandler(handler);
  request.setNumberOfRetries(10);

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

  assertEquals(10, sleeper.getCount());
  assertArrayEquals(
      new long[]{500, 1000, 2000, 4000, 8000, 16000, 32000, 64000, 120000, 120000},
      sleeper.getDelays());
  assertEquals(11, failingRequest.getCount());
}
 
Example 7
Source File: RetryUnsuccessfulResponseHandlerTest.java    From firebase-admin-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testDoesNotRetryAfterInterruption() throws IOException {
  MockSleeper sleeper = new MockSleeper() {
    @Override
    public void sleep(long millis) throws InterruptedException {
      super.sleep(millis);
      throw new InterruptedException();
    }
  };
  RetryUnsuccessfulResponseHandler handler = new RetryUnsuccessfulResponseHandler(
      testRetryConfig(sleeper));
  CountingLowLevelHttpRequest failingRequest = CountingLowLevelHttpRequest.fromStatus(503);
  HttpRequest request = TestUtils.createRequest(failingRequest);
  request.setUnsuccessfulResponseHandler(handler);
  request.setNumberOfRetries(MAX_RETRIES);

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

  assertEquals(1, sleeper.getCount());
  assertEquals(1, failingRequest.getCount());
}
 
Example 8
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);
}
 
Example 9
Source File: RetryHttpRequestInitializer.java    From beam with Apache License 2.0 5 votes vote down vote up
@Override
public void initialize(HttpRequest request) throws IOException {
  // Set a timeout for hanging-gets.
  // TODO: Do this exclusively for work requests.
  request.setReadTimeout(HANGING_GET_TIMEOUT_SEC * 1000);
  request.setWriteTimeout(this.writeTimeout);

  LoggingHttpBackOffHandler loggingHttpBackOffHandler =
      new LoggingHttpBackOffHandler(
          sleeper,
          // Back off on retryable http errors and IOExceptions.
          // A back-off multiplier of 2 raises the maximum request retrying time
          // to approximately 5 minutes (keeping other back-off parameters to
          // their default values).
          new ExponentialBackOff.Builder().setNanoClock(nanoClock).setMultiplier(2).build(),
          new ExponentialBackOff.Builder().setNanoClock(nanoClock).setMultiplier(2).build(),
          ignoredResponseCodes,
          this.customHttpErrors);

  request.setUnsuccessfulResponseHandler(loggingHttpBackOffHandler);
  request.setIOExceptionHandler(loggingHttpBackOffHandler);

  // Set response initializer
  if (responseInterceptor != null) {
    request.setResponseInterceptor(responseInterceptor);
  }
}
 
Example 10
Source File: GoogleUtils.java    From kork with Apache License 2.0 5 votes vote down vote up
static HttpRequestInitializer setTimeoutsAndRetryBehavior(final GoogleCredentials credentials) {
  return new HttpCredentialsAdapter(credentials) {
    public void initialize(HttpRequest request) throws IOException {
      super.initialize(request);
      request.setConnectTimeout(CONNECT_TIMEOUT);
      request.setReadTimeout(READ_TIMEOUT);
      HttpBackOffUnsuccessfulResponseHandler unsuccessfulResponseHandler =
          new HttpBackOffUnsuccessfulResponseHandler(new ExponentialBackOff());
      unsuccessfulResponseHandler.setBackOffRequired(
          HttpBackOffUnsuccessfulResponseHandler.BackOffRequired.ON_SERVER_ERROR);
      request.setUnsuccessfulResponseHandler(unsuccessfulResponseHandler);
    }
  };
}
 
Example 11
Source File: BaseApiService.java    From connector-sdk with Apache License 2.0 5 votes vote down vote up
/** Initialize {@link HttpRequest} to setup exponential back off and automatic retries. */
@Override
public void initialize(HttpRequest request) throws IOException {
  BackOff backOff = new ExponentialBackOff();
  request.setUnsuccessfulResponseHandler(new LoggingResponseHandler(retryPolicy, backOff));
  request.setIOExceptionHandler(new LoggingIOExceptionHandler(backOff));
}
 
Example 12
Source File: RetryInitializer.java    From firebase-admin-java with Apache License 2.0 5 votes vote down vote up
@Override
public void initialize(HttpRequest request) {
  request.setNumberOfRetries(retryConfig.getMaxRetries());
  request.setUnsuccessfulResponseHandler(newUnsuccessfulResponseHandler(request));
  if (retryConfig.isRetryOnIOExceptions()) {
    request.setIOExceptionHandler(newIOExceptionHandler());
  }
}
 
Example 13
Source File: GoogleAccountCredential.java    From google-api-java-client with Apache License 2.0 4 votes vote down vote up
@Override
public void initialize(HttpRequest request) {
  RequestHandler handler = new RequestHandler();
  request.setInterceptor(handler);
  request.setUnsuccessfulResponseHandler(handler);
}
 
Example 14
Source File: RetryHttpInitializer.java    From hadoop-connectors with Apache License 2.0 4 votes vote down vote up
@Override
public void initialize(HttpRequest request) {
  // Credential must be the interceptor to fill in accessToken fields.
  request.setInterceptor(credential);

  // Request will be retried if server errors (5XX) or I/O errors are encountered.
  request.setNumberOfRetries(options.getMaxRequestRetries());

  // Set the timeout configurations.
  request.setConnectTimeout(Math.toIntExact(options.getConnectTimeout().toMillis()));
  request.setReadTimeout(Math.toIntExact(options.getReadTimeout().toMillis()));

  // IOExceptions such as "socket timed out" of "insufficient bytes written" will follow a
  // straightforward backoff.
  HttpBackOffIOExceptionHandler exceptionHandler =
      new HttpBackOffIOExceptionHandler(new ExponentialBackOff());
  if (sleeperOverride != null) {
    exceptionHandler.setSleeper(sleeperOverride);
  }

  // Supply a new composite handler for unsuccessful return codes. 401 Unauthorized will be
  // handled by the Credential, 410 Gone will be logged, and 5XX will be handled by a backoff
  // handler.
  LoggingResponseHandler loggingResponseHandler =
      new LoggingResponseHandler(
          new CredentialOrBackoffResponseHandler(),
          exceptionHandler,
          ImmutableSet.of(HttpStatus.SC_GONE, HttpStatus.SC_SERVICE_UNAVAILABLE),
          ImmutableSet.of(HTTP_SC_TOO_MANY_REQUESTS));
  request.setUnsuccessfulResponseHandler(loggingResponseHandler);
  request.setIOExceptionHandler(loggingResponseHandler);

  if (isNullOrEmpty(request.getHeaders().getUserAgent())
      && !isNullOrEmpty(options.getDefaultUserAgent())) {
    logger.atFiner().log(
        "Request is missing a user-agent, adding default value of '%s'",
        options.getDefaultUserAgent());
    request.getHeaders().setUserAgent(options.getDefaultUserAgent());
  }

  request.getHeaders().putAll(options.getHttpHeaders());
}
 
Example 15
Source File: DriveSession.java    From cyberduck with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void initialize(final HttpRequest request) {
    super.initialize(request);
    request.setUnsuccessfulResponseHandler(new HttpBackOffUnsuccessfulResponseHandler(new ExponentialBackOff())
        .setBackOffRequired(HttpBackOffUnsuccessfulResponseHandler.BackOffRequired.ALWAYS));
}
 
Example 16
Source File: OAuthHmacCredential.java    From google-oauth-java-client with Apache License 2.0 4 votes vote down vote up
public void initialize(HttpRequest request) throws IOException {
  authorizer.initialize(request);
  request.setUnsuccessfulResponseHandler(this);
}
 
Example 17
Source File: HttpEventPublisher.java    From DataflowTemplates with Apache License 2.0 4 votes vote down vote up
/**
 * Executes a POST for the list of {@link SplunkEvent} objects into Splunk's Http Event Collector
 * endpoint.
 *
 * @param events List of {@link SplunkEvent}s
 * @return {@link HttpResponse} for the POST.
 */
public HttpResponse execute(List<SplunkEvent> events) throws IOException {

  HttpContent content = getContent(events);
  HttpRequest request = requestFactory().buildPostRequest(genericUrl(), content);

  HttpBackOffUnsuccessfulResponseHandler responseHandler =
      new HttpBackOffUnsuccessfulResponseHandler(getConfiguredBackOff());

  responseHandler.setBackOffRequired(BackOffRequired.ON_SERVER_ERROR);

  request.setUnsuccessfulResponseHandler(responseHandler);
  setHeaders(request, token());

  return request.execute();
}
 
Example 18
Source File: HttpEventPublisher.java    From beam with Apache License 2.0 4 votes vote down vote up
/**
 * Executes a POST for the list of {@link SplunkEvent} objects into Splunk's Http Event Collector
 * endpoint.
 *
 * @param events list of {@link SplunkEvent}s
 * @return {@link HttpResponse} for the POST
 */
HttpResponse execute(List<SplunkEvent> events) throws IOException {

  HttpContent content = getContent(events);
  HttpRequest request = requestFactory().buildPostRequest(genericUrl(), content);

  HttpBackOffUnsuccessfulResponseHandler responseHandler =
      new HttpBackOffUnsuccessfulResponseHandler(getConfiguredBackOff());

  responseHandler.setBackOffRequired(BackOffRequired.ON_SERVER_ERROR);

  request.setUnsuccessfulResponseHandler(responseHandler);
  setHeaders(request, token());

  return request.execute();
}
 
Example 19
Source File: HttpExample.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
/** Publish an event or state message using Cloud IoT Core via the HTTP API. */
protected static void publishMessage(
    String payload,
    String urlPath,
    String messageType,
    String token,
    String projectId,
    String cloudRegion,
    String registryId,
    String deviceId)
    throws IOException, JSONException {
  // Build the resource path of the device that is going to be authenticated.
  String devicePath =
      String.format(
          "projects/%s/locations/%s/registries/%s/devices/%s",
          projectId, cloudRegion, registryId, deviceId);
  String urlSuffix = "event".equals(messageType) ? "publishEvent" : "setState";

  // Data sent through the wire has to be base64 encoded.
  Base64.Encoder encoder = Base64.getEncoder();

  String encPayload = encoder.encodeToString(payload.getBytes(StandardCharsets.UTF_8.name()));

  urlPath = urlPath + devicePath + ":" + urlSuffix;

  final HttpRequestFactory requestFactory =
      HTTP_TRANSPORT.createRequestFactory(
          new HttpRequestInitializer() {
            @Override
            public void initialize(HttpRequest request) {
              request.setParser(new JsonObjectParser(JSON_FACTORY));
            }
          });

  HttpHeaders heads = new HttpHeaders();
  heads.setAuthorization(String.format("Bearer %s", token));
  heads.setContentType("application/json; charset=UTF-8");
  heads.setCacheControl("no-cache");

  // Add post data. The data sent depends on whether we're updating state or publishing events.
  JSONObject data = new JSONObject();
  if ("event".equals(messageType)) {
    data.put("binary_data", encPayload);
  } else {
    JSONObject state = new JSONObject();
    state.put("binary_data", encPayload);
    data.put("state", state);
  }

  ByteArrayContent content =
      new ByteArrayContent(
          "application/json", data.toString().getBytes(StandardCharsets.UTF_8.name()));

  final HttpRequest req = requestFactory.buildGetRequest(new GenericUrl(urlPath));
  req.setHeaders(heads);
  req.setContent(content);
  req.setRequestMethod("POST");
  ExponentialBackOff backoff =
      new ExponentialBackOff.Builder()
          .setInitialIntervalMillis(500)
          .setMaxElapsedTimeMillis(900000)
          .setMaxIntervalMillis(6000)
          .setMultiplier(1.5)
          .setRandomizationFactor(0.5)
          .build();
  req.setUnsuccessfulResponseHandler(new HttpBackOffUnsuccessfulResponseHandler(backoff));

  HttpResponse res = req.execute();
  System.out.println(res.getStatusCode());
  System.out.println(res.getStatusMessage());
}
 
Example 20
Source File: HttpExample.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
/** Publish an event or state message using Cloud IoT Core via the HTTP API. */
protected static void getConfig(
    String urlPath,
    String token,
    String projectId,
    String cloudRegion,
    String registryId,
    String deviceId,
    String version)
    throws IOException {
  // Build the resource path of the device that is going to be authenticated.
  String devicePath =
      String.format(
          "projects/%s/locations/%s/registries/%s/devices/%s",
          projectId, cloudRegion, registryId, deviceId);
  urlPath = urlPath + devicePath + "/config?local_version=" + version;

  HttpRequestFactory requestFactory =
      HTTP_TRANSPORT.createRequestFactory(
          new HttpRequestInitializer() {
            @Override
            public void initialize(HttpRequest request) {
              request.setParser(new JsonObjectParser(JSON_FACTORY));
            }
          });

  final HttpRequest req = requestFactory.buildGetRequest(new GenericUrl(urlPath));
  HttpHeaders heads = new HttpHeaders();

  heads.setAuthorization(String.format("Bearer %s", token));
  heads.setContentType("application/json; charset=UTF-8");
  heads.setCacheControl("no-cache");

  req.setHeaders(heads);
  ExponentialBackOff backoff =
      new ExponentialBackOff.Builder()
          .setInitialIntervalMillis(500)
          .setMaxElapsedTimeMillis(900000)
          .setMaxIntervalMillis(6000)
          .setMultiplier(1.5)
          .setRandomizationFactor(0.5)
          .build();
  req.setUnsuccessfulResponseHandler(new HttpBackOffUnsuccessfulResponseHandler(backoff));
  HttpResponse res = req.execute();
  System.out.println(res.getStatusCode());
  System.out.println(res.getStatusMessage());
  InputStream in = res.getContent();

  System.out.println(CharStreams.toString(new InputStreamReader(in, Charsets.UTF_8.name())));
}