Example usage for org.apache.http.protocol HttpContext getAttribute

List of usage examples for org.apache.http.protocol HttpContext getAttribute

Introduction

In this page you can find the example usage for org.apache.http.protocol HttpContext getAttribute.

Prototype

Object getAttribute(String str);

Source Link

Usage

From source file:com.amazonaws.http.impl.client.SdkHttpRequestRetryHandler.java

@Override
public boolean retryRequest(final IOException exception, int executionCount, final HttpContext context) {
    boolean retry = super.retryRequest(exception, executionCount, context);
    if (retry) {/*  ww  w . java  2 s  . com*/
        AWSRequestMetrics awsRequestMetrics = (AWSRequestMetrics) context
                .getAttribute(AWSRequestMetrics.class.getSimpleName());
        if (awsRequestMetrics != null) {
            awsRequestMetrics.incrementCounter(Field.HttpClientRetryCount);
        }
    }
    return retry;
}

From source file:com.ksc.http.impl.client.SdkHttpRequestRetryHandler.java

@Override
public boolean retryRequest(final IOException exception, int executionCount, final HttpContext context) {
    boolean retry = super.retryRequest(exception, executionCount, context);
    if (retry) {/*from ww w .  java2  s.  c  o  m*/
        KscRequestMetrics awsRequestMetrics = (KscRequestMetrics) context
                .getAttribute(KscRequestMetrics.class.getSimpleName());
        if (awsRequestMetrics != null) {
            awsRequestMetrics.incrementCounter(Field.HttpClientRetryCount);
        }
    }
    return retry;
}

From source file:com.ab.http.RetryHandler.java

/**
 * ??TODO/*from ww  w .j  a  v a 2 s  . co m*/
 * @see org.apache.http.client.HttpRequestRetryHandler#retryRequest(java.io.IOException, int, org.apache.http.protocol.HttpContext)
 * @author: zhaoqp
 * @date2013-10-22 ?4:23:15
 * @version v1.0
 */
@Override
public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
    boolean retry = true;

    Boolean b = (Boolean) context.getAttribute(ExecutionContext.HTTP_REQ_SENT);
    boolean sent = (b != null && b);

    if (executionCount > maxRetries) {
        // Do not retry if over max retry count
        retry = false;
    } else if (isInList(exceptionBlacklist, exception)) {
        // immediately cancel retry if the error is blacklisted
        retry = false;
    } else if (isInList(exceptionWhitelist, exception)) {
        // immediately retry if error is whitelisted
        retry = true;
    } else if (!sent) {
        // for most other errors, retry only if request hasn't been fully sent yet
        retry = true;
    }

    if (retry) {
        // resend all idempotent requests
        HttpUriRequest currentReq = (HttpUriRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);
        if (currentReq == null) {
            return false;
        }
        String requestType = currentReq.getMethod();
        retry = !requestType.equals("POST");
    }

    if (retry) {
        SystemClock.sleep(retrySleepTimeMS);
    } else {
        exception.printStackTrace();
    }

    return retry;
}

From source file:com.DGSD.DGUtils.Http.BetterHttpRequestRetryHandler.java

public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
    boolean retry;

    this.timesRetried = executionCount;

    Boolean b = (Boolean) context.getAttribute(ExecutionContext.HTTP_REQ_SENT);
    boolean sent = (b != null && b.booleanValue());

    if (executionCount > maxRetries) {
        // Do not retry if over max retry count
        retry = false;/*  w ww  .  java  2 s  .  com*/
    } else if (exceptionBlacklist.contains(exception.getClass())) {
        // immediately cancel retry if the error is blacklisted
        retry = false;
    } else if (exceptionWhitelist.contains(exception.getClass())) {
        // immediately retry if error is whitelisted
        retry = true;
    } else if (!sent) {
        // for all other errors, retry only if request hasn't been fully sent yet
        // TODO: refine to resend all idempotent requests
        retry = true;
    } else {
        // otherwise do not retry
        retry = false;
    }

    if (retry) {
        Log.e(BetterHttp.LOG_TAG,
                "request failed (" + exception.getClass().getCanonicalName() + ": " + exception.getMessage()
                        + " / attempt " + executionCount + "), will retry in "
                        + RETRY_SLEEP_TIME_MILLIS / 1000.0 + " seconds");
        SystemClock.sleep(RETRY_SLEEP_TIME_MILLIS);
    } else {
        Log.e(BetterHttp.LOG_TAG, "request failed after " + executionCount + " attempts");
        exception.printStackTrace();
    }

    return retry;
}

From source file:com.suse.studio.api.lib.HttpClient.java

private DefaultHttpClient getHttpClient(final String userName, final String apiKey) {
    HttpRequestInterceptor preemptiveAuth = new HttpRequestInterceptor() {
        public void process(final HttpRequest request, final HttpContext context)
                throws HttpException, IOException {
            AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
            if (authState.getAuthScheme() == null) {
                authState.setAuthScheme(new BasicScheme());
                authState.setCredentials(new UsernamePasswordCredentials(userName, apiKey));
            }/*from ww  w.j a  v a2s. co m*/
        }
    };
    DefaultHttpClient client = new DefaultHttpClient();
    client.addRequestInterceptor(preemptiveAuth, 0);
    return client;
}

From source file:com.cloudhopper.httpclient.util.PreemptiveBasicAuthHttpRequestInterceptor.java

public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
    ////  www  .  j a  va2s.  c  o m
    // get the authentication state for the request
    //
    AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);

    //
    // if no auth scheme avaialble yet, try to initialize it preemptively
    //
    if (authState.getAuthScheme() == null) {
        // check if credentials are set
        if (this.credentials == null) {
            throw new HttpException("No credentials for preemptive authentication");
        }

        // add the credentials to the auth state
        authState.setAuthScheme(this.basicAuthScheme);
        authState.setCredentials(this.credentials);

        // HttpHost targetHost = (HttpHost)context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
        //CredentialsProvider credsProvider = (CredentialsProvider)context.getAttribute(ClientContext.CREDS_PROVIDER);
    }
}

From source file:com.fheebiy.http.lite.apache.DefaultHttpRequestRetryHandler.java

protected boolean retryRequest(final HttpContext context) {
    final HttpRequest request = (HttpRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);
    final Object obj = context.getAttribute(ExecutionContext.HTTP_REQ_SENT);
    final boolean isSent = obj == null ? false : (Boolean) obj;

    if (requestIsAborted(request)) {
        return false;
    }//from  www. ja  v a  2 s.  c o  m

    if (handleAsIdempotent(request)) {
        // Retry if the request is considered idempotent
        return true;
    }
    if (!isSent || this.requestSentRetryEnabled) {
        // Retry if the request has not been sent fully or
        // if it's OK to retry methods that have been sent
        return true;
    }
    return false;
}

From source file:org.aludratest.service.gui.web.selenium.httpproxy.ProxyHandler.java

public void handle(HttpRequest request, HttpResponse response, HttpContext context)
        throws HttpException, IOException {

    HttpClientConnection conn = (HttpClientConnection) context.getAttribute(HTTP_OUT_CONN);

    context.setAttribute(HttpCoreContext.HTTP_CONNECTION, conn);
    context.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, this.target);

    // Remove hop-by-hop headers
    /*request.removeHeaders(HTTP.CONTENT_LEN);
    request.removeHeaders(HTTP.TRANSFER_ENCODING);
    request.removeHeaders(HTTP.CONN_DIRECTIVE);
    request.removeHeaders("Keep-Alive");
    request.removeHeaders("Proxy-Authenticate");
    request.removeHeaders("TE");/*from  w  w w. j a va2  s .c  om*/
    request.removeHeaders("Trailers");
    request.removeHeaders("Upgrade");*/

    this.httpexecutor.preProcess(request, this.httpproc, context);

    LOGGER.debug(">> Request URI: " + request.getRequestLine().getUri());
    if (LOGGER.isTraceEnabled()) {
        for (Header header : request.getAllHeaders()) {
            LOGGER.trace(header.toString());
        }
    }

    HttpResponse targetResponse = this.httpexecutor.execute(request, conn, context);
    this.httpexecutor.postProcess(response, this.httpproc, context);

    // Remove hop-by-hop headers
    /*
    targetResponse.removeHeaders(HTTP.CONTENT_LEN);
    targetResponse.removeHeaders(HTTP.TRANSFER_ENCODING);
    targetResponse.removeHeaders(HTTP.CONN_DIRECTIVE);
    targetResponse.removeHeaders("Keep-Alive");
    targetResponse.removeHeaders("TE");
    targetResponse.removeHeaders("Trailers");
    targetResponse.removeHeaders("Upgrade");
    */
    response.setStatusLine(targetResponse.getStatusLine());
    response.setHeaders(targetResponse.getAllHeaders());
    response.setEntity(targetResponse.getEntity());

    LOGGER.debug("<< Response: " + response.getStatusLine());

    boolean keepalive = this.connStrategy.keepAlive(response, context);
    context.setAttribute(HTTP_CONN_KEEPALIVE, Boolean.valueOf(keepalive));
}

From source file:org.dataconservancy.archive.impl.fcrepo.ri.MultiThreadedHttpClient.java

public MultiThreadedHttpClient(HttpClientConfig config) {
    this.config = config;
    if (config.getPreemptiveAuthN()) {
        HttpRequestInterceptor interceptor = new HttpRequestInterceptor() {
            @Override//from   www . j a v a  2  s . c om
            public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
                AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
                CredentialsProvider credsProvider = (CredentialsProvider) context
                        .getAttribute(ClientContext.CREDS_PROVIDER);
                HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
                // If no auth scheme has been initialized yet
                if (authState.getAuthScheme() == null) {
                    AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort());
                    // Obtain credentials matching the target host
                    Credentials creds = credsProvider.getCredentials(authScope);
                    // If found, generate BasicScheme preemptively
                    if (creds != null) {
                        authState.setAuthScheme(new BasicScheme());
                        authState.setCredentials(creds);
                    }
                }
            }

        };
        addRequestInterceptor(interceptor, 0);
    }

}

From source file:com.vdisk.net.session.RetryHandler.java

public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
    boolean retry = true;

    Log.d("Test", "retry count->" + executionCount);

    Boolean b = (Boolean) context.getAttribute(ExecutionContext.HTTP_REQ_SENT);
    boolean sent = (b != null && b.booleanValue());

    if (executionCount > maxRetries) {
        // Do not retry if over max retry count
        retry = false;/*from   w w w  . j av  a  2  s  .c  o m*/
    } else if (exceptionBlacklist.contains(exception.getClass())) {
        // immediately cancel retry if the error is blacklisted
        retry = false;
    } else if (exceptionWhitelist.contains(exception.getClass())) {
        // immediately retry if error is whitelisted
        retry = true;
    } else if (!sent) {
        // for most other errors, retry only if request hasn't been fully sent yet
        retry = true;
    }

    if (retry) {
        // resend all idempotent requests
        HttpUriRequest currentReq = (HttpUriRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);

        Log.d("Test", "HttpUriRequest:" + currentReq);
        if (currentReq != null) {
            String requestType = currentReq.getMethod();
            retry = !requestType.equals("POST") && !requestType.equals("PUT");
        }

    }

    if (retry) {
        SystemClock.sleep(RETRY_SLEEP_TIME_MILLIS);
    } else {
        exception.printStackTrace();
    }

    return retry;
}