Search in sources :

Example 6 with HttpBackOffIOExceptionHandler

use of com.google.api.client.http.HttpBackOffIOExceptionHandler in project gradle by gradle.

the class RetryHttpInitializerWrapper method initialize.

@Override
public void initialize(HttpRequest request) {
    // Turn off request logging, this can end up logging OAUTH
    // tokens which should not ever be in a build log
    final boolean loggingEnabled = false;
    request.setLoggingEnabled(loggingEnabled);
    request.setCurlLoggingEnabled(loggingEnabled);
    disableHttpTransportLogging();
    request.setReadTimeout((int) DEFAULT_READ_TIMEOUT_MILLIS);
    final HttpUnsuccessfulResponseHandler backoffHandler = new HttpBackOffUnsuccessfulResponseHandler(new ExponentialBackOff()).setSleeper(sleeper);
    final Credential credential = credentialSupplier.get();
    request.setInterceptor(credential);
    request.setUnsuccessfulResponseHandler(new HttpUnsuccessfulResponseHandler() {

        @Override
        public boolean handleResponse(HttpRequest request, HttpResponse response, boolean supportsRetry) throws IOException {
            // Turn off request logging unless debug mode is enabled
            request.setLoggingEnabled(loggingEnabled);
            request.setCurlLoggingEnabled(loggingEnabled);
            if (credential.handleResponse(request, response, supportsRetry)) {
                // something specific to authentication, and no backoff is desired.
                return true;
            } else if (backoffHandler.handleResponse(request, response, supportsRetry)) {
                // Otherwise, we defer to the judgement of our internal backoff handler.
                LOG.info("Retrying {}", request.getUrl());
                return true;
            } else {
                return false;
            }
        }
    });
    request.setIOExceptionHandler(new HttpBackOffIOExceptionHandler(new ExponentialBackOff()).setSleeper(sleeper));
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) HttpBackOffIOExceptionHandler(com.google.api.client.http.HttpBackOffIOExceptionHandler) Credential(com.google.api.client.auth.oauth2.Credential) HttpBackOffUnsuccessfulResponseHandler(com.google.api.client.http.HttpBackOffUnsuccessfulResponseHandler) HttpUnsuccessfulResponseHandler(com.google.api.client.http.HttpUnsuccessfulResponseHandler) HttpResponse(com.google.api.client.http.HttpResponse) IOException(java.io.IOException) ExponentialBackOff(com.google.api.client.util.ExponentialBackOff)

Example 7 with HttpBackOffIOExceptionHandler

use of com.google.api.client.http.HttpBackOffIOExceptionHandler in project elasticsearch by elastic.

the class RetryHttpInitializerWrapper method initialize.

@Override
public void initialize(HttpRequest httpRequest) {
    final HttpUnsuccessfulResponseHandler backoffHandler = new HttpBackOffUnsuccessfulResponseHandler(new ExponentialBackOff.Builder().setMaxElapsedTimeMillis(((int) maxWait.getMillis())).build()).setSleeper(sleeper);
    httpRequest.setInterceptor(wrappedCredential);
    httpRequest.setUnsuccessfulResponseHandler(new HttpUnsuccessfulResponseHandler() {

        int retry = 0;

        @Override
        public boolean handleResponse(HttpRequest request, HttpResponse response, boolean supportsRetry) throws IOException {
            if (wrappedCredential.handleResponse(request, response, supportsRetry)) {
                // and no backoff is desired.
                return true;
            } else if (backoffHandler.handleResponse(request, response, supportsRetry)) {
                // Otherwise, we defer to the judgement of
                // our internal backoff handler.
                logger.debug("Retrying [{}] times : [{}]", retry, request.getUrl());
                return true;
            } else {
                return false;
            }
        }
    });
    httpRequest.setIOExceptionHandler(new HttpBackOffIOExceptionHandler(new ExponentialBackOff.Builder().setMaxElapsedTimeMillis(((int) maxWait.getMillis())).build()).setSleeper(sleeper));
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) HttpBackOffIOExceptionHandler(com.google.api.client.http.HttpBackOffIOExceptionHandler) HttpBackOffUnsuccessfulResponseHandler(com.google.api.client.http.HttpBackOffUnsuccessfulResponseHandler) HttpUnsuccessfulResponseHandler(com.google.api.client.http.HttpUnsuccessfulResponseHandler) HttpResponse(com.google.api.client.http.HttpResponse) IOException(java.io.IOException) ExponentialBackOff(com.google.api.client.util.ExponentialBackOff)

Example 8 with HttpBackOffIOExceptionHandler

use of com.google.api.client.http.HttpBackOffIOExceptionHandler in project google-auth-library-java by google.

the class ServiceAccountCredentials method refreshAccessToken.

/**
 * Refreshes the OAuth2 access token by getting a new access token using a JSON Web Token (JWT).
 */
@Override
public AccessToken refreshAccessToken() throws IOException {
    if (createScopedRequired()) {
        throw new IOException("Scopes not configured for service account. Scoped should be specified" + " by calling createScoped or passing scopes to constructor.");
    }
    JsonFactory jsonFactory = OAuth2Utils.JSON_FACTORY;
    long currentTime = clock.currentTimeMillis();
    String assertion = createAssertion(jsonFactory, currentTime);
    GenericData tokenRequest = new GenericData();
    tokenRequest.set("grant_type", GRANT_TYPE);
    tokenRequest.set("assertion", assertion);
    UrlEncodedContent content = new UrlEncodedContent(tokenRequest);
    HttpRequestFactory requestFactory = transportFactory.create().createRequestFactory();
    HttpRequest request = requestFactory.buildPostRequest(new GenericUrl(tokenServerUri), content);
    request.setParser(new JsonObjectParser(jsonFactory));
    request.setIOExceptionHandler(new HttpBackOffIOExceptionHandler(new ExponentialBackOff()));
    request.setUnsuccessfulResponseHandler(new HttpBackOffUnsuccessfulResponseHandler(new ExponentialBackOff()).setBackOffRequired(new BackOffRequired() {

        public boolean isRequired(HttpResponse response) {
            int code = response.getStatusCode();
            return (// Server error --- includes timeout errors, which use 500 instead of 408
            code / 100 == 5 || // https://github.com/google/google-api-java-client/issues/662
            code == 403);
        }
    }));
    HttpResponse response;
    try {
        response = request.execute();
    } catch (IOException e) {
        throw new IOException("Error getting access token for service account: ", e);
    }
    GenericData responseData = response.parseAs(GenericData.class);
    String accessToken = OAuth2Utils.validateString(responseData, "access_token", PARSE_ERROR_PREFIX);
    int expiresInSeconds = OAuth2Utils.validateInt32(responseData, "expires_in", PARSE_ERROR_PREFIX);
    long expiresAtMilliseconds = clock.currentTimeMillis() + expiresInSeconds * 1000L;
    return new AccessToken(accessToken, new Date(expiresAtMilliseconds));
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) HttpBackOffIOExceptionHandler(com.google.api.client.http.HttpBackOffIOExceptionHandler) HttpBackOffUnsuccessfulResponseHandler(com.google.api.client.http.HttpBackOffUnsuccessfulResponseHandler) HttpRequestFactory(com.google.api.client.http.HttpRequestFactory) JsonFactory(com.google.api.client.json.JsonFactory) UrlEncodedContent(com.google.api.client.http.UrlEncodedContent) HttpResponse(com.google.api.client.http.HttpResponse) IOException(java.io.IOException) GenericUrl(com.google.api.client.http.GenericUrl) BackOffRequired(com.google.api.client.http.HttpBackOffUnsuccessfulResponseHandler.BackOffRequired) GenericData(com.google.api.client.util.GenericData) ExponentialBackOff(com.google.api.client.util.ExponentialBackOff) Date(java.sql.Date) JsonObjectParser(com.google.api.client.json.JsonObjectParser)

Example 9 with HttpBackOffIOExceptionHandler

use of com.google.api.client.http.HttpBackOffIOExceptionHandler in project DataflowJavaSDK-examples by GoogleCloudPlatform.

the class RetryHttpInitializerWrapper method initialize.

/**
 * Initializes the given request.
 */
@Override
public final void initialize(final HttpRequest request) {
    // 2 minutes read timeout
    request.setReadTimeout(2 * ONEMINITUES);
    final HttpUnsuccessfulResponseHandler backoffHandler = new HttpBackOffUnsuccessfulResponseHandler(new ExponentialBackOff()).setSleeper(sleeper);
    request.setInterceptor(wrappedCredential);
    request.setUnsuccessfulResponseHandler(new HttpUnsuccessfulResponseHandler() {

        @Override
        public boolean handleResponse(final HttpRequest request, final HttpResponse response, final boolean supportsRetry) throws IOException {
            if (wrappedCredential.handleResponse(request, response, supportsRetry)) {
                // and no backoff is desired.
                return true;
            } else if (backoffHandler.handleResponse(request, response, supportsRetry)) {
                // Otherwise, we defer to the judgement of
                // our internal backoff handler.
                LOG.info("Retrying " + request.getUrl().toString());
                return true;
            } else {
                return false;
            }
        }
    });
    request.setIOExceptionHandler(new HttpBackOffIOExceptionHandler(new ExponentialBackOff()).setSleeper(sleeper));
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) HttpBackOffIOExceptionHandler(com.google.api.client.http.HttpBackOffIOExceptionHandler) HttpBackOffUnsuccessfulResponseHandler(com.google.api.client.http.HttpBackOffUnsuccessfulResponseHandler) HttpUnsuccessfulResponseHandler(com.google.api.client.http.HttpUnsuccessfulResponseHandler) HttpResponse(com.google.api.client.http.HttpResponse) IOException(java.io.IOException) ExponentialBackOff(com.google.api.client.util.ExponentialBackOff)

Aggregations

HttpBackOffIOExceptionHandler (com.google.api.client.http.HttpBackOffIOExceptionHandler)9 HttpBackOffUnsuccessfulResponseHandler (com.google.api.client.http.HttpBackOffUnsuccessfulResponseHandler)9 ExponentialBackOff (com.google.api.client.util.ExponentialBackOff)9 HttpRequest (com.google.api.client.http.HttpRequest)8 HttpResponse (com.google.api.client.http.HttpResponse)7 IOException (java.io.IOException)7 HttpUnsuccessfulResponseHandler (com.google.api.client.http.HttpUnsuccessfulResponseHandler)6 GenericUrl (com.google.api.client.http.GenericUrl)3 Credential (com.google.api.client.auth.oauth2.Credential)1 BackOffRequired (com.google.api.client.http.HttpBackOffUnsuccessfulResponseHandler.BackOffRequired)1 HttpRequestFactory (com.google.api.client.http.HttpRequestFactory)1 HttpTransport (com.google.api.client.http.HttpTransport)1 UrlEncodedContent (com.google.api.client.http.UrlEncodedContent)1 NetHttpTransport (com.google.api.client.http.javanet.NetHttpTransport)1 JsonFactory (com.google.api.client.json.JsonFactory)1 JsonObjectParser (com.google.api.client.json.JsonObjectParser)1 GenericData (com.google.api.client.util.GenericData)1 ListeningExecutorService (com.google.common.util.concurrent.ListeningExecutorService)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1