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:org.xwiki.extension.repository.http.internal.PreemptiveAuth.java

@Override
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
    AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);

    // If no auth scheme available yet, try to initialize it
    // preemptively
    if (authState.getAuthScheme() == null) {
        AuthScheme authScheme = (AuthScheme) context.getAttribute("preemptive-auth");
        CredentialsProvider credsProvider = (CredentialsProvider) context
                .getAttribute(ClientContext.CREDS_PROVIDER);
        HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
        if (authScheme != null) {
            Credentials credentials = credsProvider
                    .getCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()));
            if (credentials == null) {
                throw new HttpException("No credentials for preemptive authentication");
            }// www. j av a2 s .co m
            authState.update(authScheme, credentials);
        }
    }

}

From source file:com.uber.jaeger.httpclient.TracingResponseInterceptor.java

@Override
public void process(HttpResponse httpResponse, HttpContext httpContext) throws HttpException, IOException {
    try {/*from w ww.j a v a2s  .c  o m*/
        Span clientSpan = (Span) httpContext.getAttribute(Constants.CURRENT_SPAN_CONTEXT_KEY);
        if (clientSpan != null) {
            Tags.HTTP_STATUS.set(clientSpan, httpResponse.getStatusLine().getStatusCode());
            beforeSpanFinish(clientSpan, httpResponse, httpContext);
            clientSpan.finish();
        } else {
            logger.warn("The ResponseInterceptor did not find a clientSpan. "
                    + "Verify that the RequestInterceptor is correctly set up.");
        }
    } catch (Exception e) {
        logger.error("Could not finish client tracing span.", e);
    }
}

From source file:code.google.restclient.core.CustomRetryHandler.java

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

    HttpRequest request = (HttpRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);

    // idempotent are those methods which returns same value irrespective of number of execution

    boolean idempotent = !(request instanceof HttpEntityEnclosingRequest);
    if (!idempotent) {
        if (DEBUG_ENABLED) {
            LOG.debug("retryRequest(): not retrying because request is non-idempotent");
        }// w w w  .  j  a  v  a2  s .c o m
        // Retry if the request is considered idempotent
        return false;
    }

    if (executionCount >= 5) {
        if (DEBUG_ENABLED) {
            LOG.debug("retryRequest(): not retrying because execution count exceeds 5");
        }
        // Do not retry if over max retry count
        return false;
    }

    if (exception instanceof NoHttpResponseException) {
        if (DEBUG_ENABLED) {
            LOG.debug("retryRequest(): retrying because server dropped connection");
        }
        // Retry if the server dropped connection on us
        return true;
    }

    if (exception instanceof SSLHandshakeException) {
        if (DEBUG_ENABLED) {
            LOG.debug("retryRequest(): not retrying because of SSL handshake exception");
        }
        // Do not retry on SSL handshake exception
        return false;
    }

    if (idempotent) {
        if (DEBUG_ENABLED) {
            LOG.debug("retryRequest(): retrying because request is idempotent");
        }
        // Retry if the request is considered idempotent
        return true;
    }

    return false;
}

From source file:com.hippoapp.asyncmvp.http.RetryHandler.java

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

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

    if (executionCount > DEFAULT_MAX_RETRIES) {
        retry = false;/* ww  w .  j a v  a2s . c  o m*/
    } else if (sUnretriedExceptionSet.contains(exception.getClass())) {
        retry = false;
    } else if (sRetriedExceptionSet.contains(exception.getClass())) {
        retry = true;
    } else if (!sent) {
        // for most other errors, retry only if request hasn't been fully
        // sent yet
        retry = true;
    } else {
        // resend all idempotent requests
        HttpUriRequest currentReq = (HttpUriRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);
        String requestType = currentReq.getMethod();
        if (!requestType.equals("POST")) {
            retry = true;
        } else {
            // otherwise do not retry
            retry = false;
        }
    }

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

    return retry;
}

From source file:eu.masconsult.bgbanking.utils.CookieQuotesFixerResponseInterceptor.java

@Override
public void process(HttpResponse response, HttpContext context) throws HttpException, IOException {
    CookieStore cookieStore = (CookieStore) context.getAttribute(ClientContext.COOKIE_STORE);
    for (Header header : response.getAllHeaders()) {
        if (!header.getName().equalsIgnoreCase("Set-Cookie")) {
            continue;
        }/* www. ja  v a2 s  .  c  om*/
        Matcher matcher = pattern.matcher(header.getValue());
        if (!matcher.find()) {
            continue;
        }
        for (Cookie cookie : cookieStore.getCookies()) {
            if (cookie.getName().equalsIgnoreCase(matcher.group(1))) {
                if (cookie instanceof BasicClientCookie) {
                    ((BasicClientCookie) cookie).setValue('"' + cookie.getValue() + '"');
                } else if (cookie instanceof BasicClientCookie2) {
                    ((BasicClientCookie2) cookie).setValue('"' + cookie.getValue() + '"');
                } else {
                    Log.w(TAG, "unhandled cookie implementation " + cookie.getClass().getName());
                }
                break;
            }
        }
    }
}

From source file:de.escidoc.core.http.PreemtiveAuthHttpRequestInterceptor.java

@Override
public void process(final HttpRequest httpRequest, final HttpContext httpContext)
        throws HttpException, IOException {
    final AuthState authState = (AuthState) httpContext.getAttribute(ClientContext.TARGET_AUTH_STATE);
    final CredentialsProvider credsProvider = (CredentialsProvider) httpContext
            .getAttribute(ClientContext.CREDS_PROVIDER);
    final HttpHost targetHost = (HttpHost) httpContext.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
    // If not auth scheme has been initialized yet
    if (authState.getAuthScheme() == null) {
        final AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort());
        // Obtain credentials matching the target host
        final Credentials creds = credsProvider.getCredentials(authScope);
        // If found, generate BasicScheme preemptively
        if (creds != null) {
            authState.setAuthScheme(new BasicScheme());
            authState.setCredentials(creds);
        }//from   ww  w. j  a  va  2s .  c  o m
    }
}

From source file:com.examination.afinal.http.RetryHandler.java

@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.booleanValue());

    if (executionCount > maxRetries) {
        // ????????5            retry = false;
    } else if (exceptionBlacklist.contains(exception.getClass())) {
        // ????????
        retry = false;//from  www.j a  v  a2  s  . co m
    } else if (exceptionWhitelist.contains(exception.getClass())) {
        retry = true;
    } else if (!sent) {
        retry = true;
    }

    if (retry) {
        HttpUriRequest currentReq = (HttpUriRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);
        retry = currentReq != null && !"POST".equals(currentReq.getMethod());
    }

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

    return retry;
}

From source file:cn.com.dfc.pl.afinal.http.RetryHandler.java

@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.booleanValue());

    if (executionCount > maxRetries) {
        // ?5/*  w  w  w.j  a  va2s. c  o  m*/
        retry = false;
    } else if (exceptionBlacklist.contains(exception.getClass())) {
        // ??
        retry = false;
    } else if (exceptionWhitelist.contains(exception.getClass())) {
        retry = true;
    } else if (!sent) {
        retry = true;
    }

    if (retry) {
        HttpUriRequest currentReq = (HttpUriRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);
        retry = currentReq != null && !"POST".equals(currentReq.getMethod());
    }

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

    return retry;
}

From source file:com.amazonaws.http.protocol.SdkHttpRequestExecutor.java

@Override
protected HttpResponse doSendRequest(final HttpRequest request, final HttpClientConnection conn,
        final HttpContext context) throws IOException, HttpException {
    AWSRequestMetrics awsRequestMetrics = (AWSRequestMetrics) context
            .getAttribute(AWSRequestMetrics.class.getSimpleName());
    if (awsRequestMetrics == null) {
        return super.doSendRequest(request, conn, context);
    }/*from  w w  w  . ja v  a 2 s .  co m*/
    awsRequestMetrics.startEvent(Field.HttpClientSendRequestTime);
    try {
        return super.doSendRequest(request, conn, context);
    } finally {
        awsRequestMetrics.endEvent(Field.HttpClientSendRequestTime);
    }
}

From source file:com.amazonaws.http.protocol.SdkHttpRequestExecutor.java

@Override
protected HttpResponse doReceiveResponse(final HttpRequest request, final HttpClientConnection conn,
        final HttpContext context) throws HttpException, IOException {
    AWSRequestMetrics awsRequestMetrics = (AWSRequestMetrics) context
            .getAttribute(AWSRequestMetrics.class.getSimpleName());
    if (awsRequestMetrics == null) {
        return super.doReceiveResponse(request, conn, context);
    }/*  w ww  . j a va  2 s .  co  m*/
    awsRequestMetrics.startEvent(Field.HttpClientReceiveResponseTime);
    try {
        return super.doReceiveResponse(request, conn, context);
    } finally {
        awsRequestMetrics.endEvent(Field.HttpClientReceiveResponseTime);
    }
}