Search in sources :

Example 1 with RestException

use of com.okta.sdk.impl.http.RestException in project okta-sdk-java by okta.

the class HttpClientRequestExecutor method executeRequest.

@Override
public Response executeRequest(Request request) throws RestException {
    Assert.notNull(request, "Request argument cannot be null.");
    int retryCount = 0;
    URI redirectUri = null;
    HttpEntity entity = null;
    RestException exception = null;
    // Make a copy of the original request params and headers so that we can
    // permute them in the loop and start over with the original every time.
    QueryString originalQuery = new QueryString();
    originalQuery.putAll(request.getQueryString());
    HttpHeaders originalHeaders = new HttpHeaders();
    originalHeaders.putAll(request.getHeaders());
    while (true) {
        if (redirectUri != null) {
            request = new DefaultRequest(request.getMethod(), redirectUri.toString(), null, null, request.getBody(), request.getHeaders().getContentLength());
        }
        if (retryCount > 0) {
            request.setQueryString(originalQuery);
            request.setHeaders(originalHeaders);
        }
        // Sign the request
        this.requestAuthenticator.authenticate(request);
        HttpRequestBase httpRequest = this.httpClientRequestFactory.createHttpClientRequest(request, entity);
        if (httpRequest instanceof HttpEntityEnclosingRequest) {
            entity = ((HttpEntityEnclosingRequest) httpRequest).getEntity();
        }
        HttpResponse httpResponse = null;
        try {
            // before executing the request below.
            if (retryCount > 0 && redirectUri == null) {
                pauseExponentially(retryCount, exception);
                if (entity != null) {
                    InputStream content = entity.getContent();
                    if (content.markSupported()) {
                        content.reset();
                    }
                }
            }
            // reset redirectUri so that if there is an exception, we will pause on retry
            redirectUri = null;
            exception = null;
            retryCount++;
            httpResponse = httpClient.execute(httpRequest);
            if (isRedirect(httpResponse)) {
                Header[] locationHeaders = httpResponse.getHeaders("Location");
                String location = locationHeaders[0].getValue();
                log.debug("Redirecting to: {}", location);
                redirectUri = URI.create(location);
                httpRequest.setURI(redirectUri);
            } else {
                Response response = toSdkResponse(httpResponse);
                int httpStatus = response.getHttpStatus();
                if (httpStatus == 429) {
                    throw new RestException("HTTP 429: Too Many Requests.  Exceeded request rate limit in the allotted amount of time.");
                }
                if ((httpStatus == 503 || httpStatus == 504) && retryCount <= this.numRetries) {
                    // allow the loop to continue to execute a retry request
                    continue;
                }
                return response;
            }
        } catch (Throwable t) {
            log.warn("Unable to execute HTTP request: ", t.getMessage(), t);
            if (t instanceof RestException) {
                exception = (RestException) t;
            }
            if (!shouldRetry(httpRequest, t, retryCount)) {
                throw new RestException("Unable to execute HTTP request: " + t.getMessage(), t);
            }
        } finally {
            try {
                httpResponse.getEntity().getContent().close();
            } catch (Throwable ignored) {
            // NOPMD
            }
        }
    }
}
Also used : HttpHeaders(com.okta.sdk.impl.http.HttpHeaders) HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) HttpEntity(org.apache.http.HttpEntity) DefaultRequest(com.okta.sdk.impl.http.support.DefaultRequest) QueryString(com.okta.sdk.impl.http.QueryString) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) RestException(com.okta.sdk.impl.http.RestException) HttpResponse(org.apache.http.HttpResponse) QueryString(com.okta.sdk.impl.http.QueryString) URI(java.net.URI) DefaultResponse(com.okta.sdk.impl.http.support.DefaultResponse) Response(com.okta.sdk.impl.http.Response) HttpResponse(org.apache.http.HttpResponse) Header(org.apache.http.Header) HttpEntityEnclosingRequest(org.apache.http.HttpEntityEnclosingRequest)

Example 2 with RestException

use of com.okta.sdk.impl.http.RestException in project okta-sdk-java by okta.

the class HttpClientRequestExecutor method pauseExponentially.

/**
 * Exponential sleep on failed request to avoid flooding a service with
 * retries.
 *
 * @param retries           Current retry count.
 * @param previousException Exception information for the previous attempt, if any.
 */
private void pauseExponentially(int retries, RestException previousException) {
    long delay;
    if (backoffStrategy != null) {
        delay = this.backoffStrategy.getDelayMillis(retries);
    } else {
        long scaleFactor = 300;
        if (previousException != null && isThrottlingException(previousException)) {
            scaleFactor = 500 + random.nextInt(100);
        }
        delay = (long) (Math.pow(2, retries) * scaleFactor);
    }
    delay = Math.min(delay, MAX_BACKOFF_IN_MILLISECONDS);
    log.debug("Retryable condition detected, will retry in {}ms, attempt number: {}", delay, retries);
    try {
        Thread.sleep(delay);
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        throw new RestException(e.getMessage(), e);
    }
}
Also used : RestException(com.okta.sdk.impl.http.RestException)

Aggregations

RestException (com.okta.sdk.impl.http.RestException)2 HttpHeaders (com.okta.sdk.impl.http.HttpHeaders)1 QueryString (com.okta.sdk.impl.http.QueryString)1 Response (com.okta.sdk.impl.http.Response)1 DefaultRequest (com.okta.sdk.impl.http.support.DefaultRequest)1 DefaultResponse (com.okta.sdk.impl.http.support.DefaultResponse)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 URI (java.net.URI)1 Header (org.apache.http.Header)1 HttpEntity (org.apache.http.HttpEntity)1 HttpEntityEnclosingRequest (org.apache.http.HttpEntityEnclosingRequest)1 HttpResponse (org.apache.http.HttpResponse)1 HttpRequestBase (org.apache.http.client.methods.HttpRequestBase)1