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.my.cloudcontact.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//from  www  .j  a  v a  2s .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.mycompany.projecta.PreemptiveAuth.java

@Override
public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
    // Get the AuthState
    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 creds = credsProvider
                    .getCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()));
            if (creds == null) {
                throw new HttpException("No credentials for preemptive authentication");
            }/*from w w w  .  j ava2s.  c  o m*/
            authState.setAuthScheme(authScheme);
            authState.setCredentials(creds);
        }
    }

}

From source file:org.greencheek.spring.rest.SSLCachingHttpComponentsClientHttpRequest.java

private void saveSSLPrinciple(HttpContext context) {
    Principal current = (Principal) context.getAttribute(ClientContext.USER_TOKEN);
    if (current != null)
        sslTokenCache.compareAndSet(null, current);
}

From source file:guru.nidi.ramltester.httpcomponents.RamlHttpClient.java

private boolean alreadyTested(HttpContext context) {
    if (context.getAttribute(RAML_TESTED) != null) {
        return true;
    }//  w w w .  j av  a2 s .c o m
    context.setAttribute(RAML_TESTED, true);
    return false;
}

From source file:ch.cyberduck.core.s3.S3HttpRequestRetryHandler.java

@Override
public boolean retryRequest(final IOException exception, final int executionCount, final HttpContext context) {
    if (super.retryRequest(exception, executionCount, context)) {
        final Object attribute = context.getAttribute(HttpCoreContext.HTTP_REQUEST);
        if (attribute instanceof HttpUriRequest) {
            final HttpUriRequest method = (HttpUriRequest) attribute;
            log.warn(String.format("Retrying request %s", method));
            try {
                // Build the authorization string for the method.
                authorizer.authorizeHttpRequest(method, context, null);
                return true;
            } catch (ServiceException e) {
                log.warn("Unable to generate updated authorization string for retried request", e);
            }/* www  .  j  ava2s.c o m*/
        }
    }
    return false;
}

From source file:net.oauth.client.httpclient4.PreemptiveAuthorizer.java

/**
 * If no auth scheme has been selected for the given context, consider each
 * of the preferred auth schemes and select the first one for which an
 * AuthScheme and matching Credentials are available.
 *///from   w  w w . j  ava  2s  . co  m
public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
    AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
    if (authState != null && authState.getAuthScheme() != null) {
        return;
    }
    HttpHost target = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
    CredentialsProvider creds = (CredentialsProvider) context.getAttribute(ClientContext.CREDS_PROVIDER);
    AuthSchemeRegistry schemes = (AuthSchemeRegistry) context.getAttribute(ClientContext.AUTHSCHEME_REGISTRY);
    for (Object schemeName : (Iterable) context.getAttribute(ClientContext.AUTH_SCHEME_PREF)) {
        AuthScheme scheme = schemes.getAuthScheme(schemeName.toString(), request.getParams());
        if (scheme != null) {
            AuthScope targetScope = new AuthScope(target.getHostName(), target.getPort(), scheme.getRealm(),
                    scheme.getSchemeName());
            Credentials cred = creds.getCredentials(targetScope);
            if (cred != null) {
                authState.setAuthScheme(scheme);
                authState.setCredentials(cred);
                return;
            }
        }
    }
}

From source file:com.blazeroni.reddit.http.RetryHandler.java

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 > this.maxRetries) {
        // Do not retry if over max retry count
        retry = false;/*from  www.  j ava 2 s  .  c  o  m*/
    } else if (EXCEPTION_MAP.containsKey(exception.getClass())) {
        // immediately cancel retry if the error is blacklisted
        // immediately retry if error is whitelisted
        return EXCEPTION_MAP.get(exception.getClass());
    } 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_MILLIS);
    } else {
        Log.debug("Not retrying request", exception);
    }

    return retry;
}

From source file:com.sonatype.nexus.ssl.plugin.internal.repository.RepositoryClientConnectionOperatorSelectorTest.java

/**
 * Verify that an {@link ClientConnectionOperator} is returned when trust store is enabled for repository
 * (repository present in context under {@link HttpClientFactory#HTTP_CTX_KEY_REPOSITORY} key).
 *//*from   w ww. ja  v  a2  s .  c  o m*/
@Test
public void operatorReturnedWhenTrustStoreEnabled() throws Exception {
    final Repository repository = mock(Repository.class);
    when(repository.getId()).thenReturn("foo");

    final TrustStore trustStore = mock(TrustStore.class);
    when(trustStore.getSSLContextFor(repositoryTrustStoreKey("foo"))).thenReturn(SSLContext.getDefault());

    final HttpContext httpContext = mock(HttpContext.class);
    when(httpContext.getAttribute(HttpClientFactory.HTTP_CTX_KEY_REPOSITORY)).thenReturn(repository);

    final RepositoryClientConnectionOperatorSelector underTest = new RepositoryClientConnectionOperatorSelector(
            trustStore);
    final SSLContext context = underTest.select(httpContext);

    assertThat(context, is(notNullValue()));
}

From source file:com.sonatype.nexus.ssl.plugin.internal.repository.RepositoryClientConnectionOperatorSelectorTest.java

/**
 * Verify that an no {@link ClientConnectionOperator} is returned when trust store is not enabled for repository
 * (repository present in context under {@link HttpClientFactory#HTTP_CTX_KEY_REPOSITORY} key).
 *//*  ww w  .j  a  v  a2  s .c o m*/
@Test
public void noOperatorReturnedWhenTrustStoreIsNotEnabled() throws Exception {
    final Repository repository = mock(Repository.class);
    when(repository.getId()).thenReturn("foo");

    final TrustStore trustStore = mock(TrustStore.class);
    when(trustStore.getSSLContextFor(repositoryTrustStoreKey("foo"))).thenReturn(null);

    final HttpContext httpContext = mock(HttpContext.class);
    when(httpContext.getAttribute(HttpClientFactory.HTTP_CTX_KEY_REPOSITORY)).thenReturn(repository);

    final RepositoryClientConnectionOperatorSelector underTest = new RepositoryClientConnectionOperatorSelector(
            trustStore);
    final SSLContext context = underTest.select(httpContext);

    assertThat(context, is(nullValue()));
}

From source file:com.cndatacom.ordersystem.manager.RetryHandler.java

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) {
        // Do not retry if over max retry count
        retry = false;/*from ww w.  j ava  2  s.  c om*/
    } 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);
        String requestType = currentReq.getMethod();
        retry = !requestType.equals("POST");
    }

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

    return retry;
}