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.sonatype.nexus.ssl.plugin.internal.repository.RepositoryClientConnectionOperatorSelectorTest.java

/**
 * Verify that an no {@link ClientConnectionOperator} is returned when trust store is enabled but under
 * {@link HttpClientFactory#HTTP_CTX_KEY_REPOSITORY} key is not a repository.
 *//*from w  ww .  ja va2  s  . co  m*/
@Test
public void noOperatorReturnedWhenTrustStoreIsEnabledButHttpContextContainsAnotherTypeUnderKey()
        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(new Object());

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

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

From source file:cn.salesuite.saf.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 > maxRetries) {
        // Do not retry if over max retry count
        retry = false;/*  w w  w .  java 2  s . co  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;
    } 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 {
        exception.printStackTrace();
    }

    return retry;
}

From source file:com.microsoft.exchange.impl.http.PreemptiveAuthInterceptor.java

@Override
public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
    AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
    if (authState.getAuthScheme() == null) {
        AuthScheme preemptiveAuthScheme = (AuthScheme) context.getAttribute(PREEMPTIVE_AUTH);
        if (preemptiveAuthScheme != null) {
            CredentialsProvider credsProvider = (CredentialsProvider) context
                    .getAttribute(ClientContext.CREDS_PROVIDER);
            Credentials creds = credsProvider.getCredentials(authScope);
            if (creds == null) {
                throw new HttpException("No credentials for preemptive authentication");
            }/*w w  w  .j a v a2s.c o m*/
            authState.setAuthScheme(preemptiveAuthScheme);
            authState.setCredentials(creds);
            if (log.isTraceEnabled()) {
                log.trace("successfully set credentials " + creds + " and authScheme " + preemptiveAuthScheme
                        + " for request " + request);
            }
        } else {
            log.warn(PREEMPTIVE_AUTH
                    + " authScheme not found in context; make sure you are using the CustomHttpComponentsMessageSender and the preemptiveAuthEnabled property is set to true");
        }
    } else {
        log.warn("context's authState attribute (" + authState + ") has non-null AuthScheme for request "
                + request);
    }
}

From source file:glaze.oauth.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 .  ja v  a  2s .c o m*/
@SuppressWarnings("unchecked")
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 provider = (CredentialsProvider) context.getAttribute(ClientContext.CREDS_PROVIDER);
    AuthSchemeRegistry schemes = (AuthSchemeRegistry) context.getAttribute(ClientContext.AUTHSCHEME_REGISTRY);
    HttpParams params = request.getParams();

    for (String schemeName : (Iterable<String>) params.getParameter(AuthPNames.PROXY_AUTH_PREF)) {
        AuthScheme scheme = schemes.getAuthScheme(schemeName, params);
        if (scheme != null) {
            AuthScope targetScope = new AuthScope(target.getHostName(), target.getPort(), scheme.getRealm(),
                    scheme.getSchemeName());
            Credentials creds = provider.getCredentials(targetScope);

            if (creds != null) {
                authState.update(scheme, creds);
                return;
            }
        }
    }
}

From source file:org.jasig.schedassist.impl.caldav.PreemptiveAuthInterceptor.java

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) {
        AuthScheme authScheme = (AuthScheme) context.getAttribute(PREEMPTIVE_AUTH);
        CredentialsProvider credsProvider = (CredentialsProvider) context
                .getAttribute(ClientContext.CREDS_PROVIDER);
        if (authScheme != null) {
            Credentials creds = credsProvider.getCredentials(caldavAdminAuthScope);
            if (creds == null) {
                throw new HttpException("No credentials for preemptive authentication");
            }//w  w w. ja v  a 2s  .c om
            authState.setAuthScheme(authScheme);
            authState.setCredentials(creds);
            if (log.isTraceEnabled()) {
                log.trace("successfully set credentials " + creds + " and authScheme " + authScheme
                        + " for request " + request);
            }
        } else {
            log.warn(PREEMPTIVE_AUTH
                    + " authScheme not found in context, failed to set scheme and credentials for " + request);
        }
    } else {
        log.warn("context's authState attribute (" + authState + ") has non-null AuthScheme for request "
                + request);
    }

}

From source file:com.google.httputils.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 > maxRetries) {
        // Do not retry if over max retry count
        retry = false;//from   w w w.  j  av a  2s.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;
    } 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 {
        exception.printStackTrace();
    }

    return retry;
}

From source file:com.example.wechatsample.library.http.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  www. j  av a2s  . 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);
        String requestType = currentReq.getMethod();
        retry = !requestType.equals("POST");
    }

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

    return retry;
}

From source file:com.google.cloud.trace.apachehttp.TraceRequestInterceptorTest.java

@Test
public void testProcess() throws Exception {
    HttpContext context = new BasicHttpContext();
    requestInterceptor.process(requestWithHeaders, context);
    assertThat(context.getAttribute("TRACE-CONTEXT")).isEqualTo(testContext);

    ArgumentCaptor<HttpRequest> captor = ArgumentCaptor.forClass(HttpRequest.class);
    verify(mockDelegate).process(captor.capture());
    HttpRequest request = captor.getValue();
    assertThat(request.getMethod()).isEqualTo("GET");
    assertThat(request.getProtocol()).isEqualTo("HTTP");
    assertThat(request.getURI()).isEqualTo(URI.create("http://example.com/foo/bar"));
    assertThat(request.getHeader("User-Agent")).isEqualTo("test-user-agent");
}

From source file:com.damytech.HttpConn.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   w ww. ja v  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
        HttpRequest currentReq = (HttpRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);
        /*String requestType = currentReq.getMethod();
        retry = !requestType.equals("POST");*/
        retry = !(currentReq instanceof HttpEntityEnclosingRequest);
    }

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

    return retry;
}

From source file:com.intuit.karate.http.apache.RequestLoggingInterceptor.java

@Override
public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
    int id = counter.incrementAndGet();
    String uri = (String) context.getAttribute(ApacheHttpClient.URI_CONTEXT_KEY);
    StringBuilder sb = new StringBuilder();
    sb.append('\n').append(id).append(" > ").append(request.getRequestLine().getMethod()).append(' ')
            .append(uri).append('\n');
    LoggingUtils.logHeaders(sb, id, '>', request);
    if (request instanceof HttpEntityEnclosingRequest) {
        HttpEntityEnclosingRequest entityRequest = (HttpEntityEnclosingRequest) request;
        HttpEntity entity = entityRequest.getEntity();
        if (LoggingUtils.isPrintable(entity)) {
            LoggingEntityWrapper wrapper = new LoggingEntityWrapper(entity);
            String buffer = IOUtils.toString(wrapper.getContent(), "utf-8");
            sb.append(buffer).append('\n');
            entityRequest.setEntity(wrapper);
        }/*from w w  w. jav  a 2 s.c om*/
    }
    logger.debug(sb.toString());
}