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:cn.com.loopj.android.http.PreemptiveAuthorizationHttpRequestInterceptor.java

public void process(final HttpRequest request, final 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 (authState.getAuthScheme() == null) {
        AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort());
        Credentials creds = credsProvider.getCredentials(authScope);
        if (creds != null) {
            authState.setAuthScheme(new BasicScheme());
            authState.setCredentials(creds);
        }//from   ww w  . j a v  a 2s  .  c  o  m
    }
}

From source file:org.apache.solr.client.solrj.impl.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) {
        CredentialsProvider credsProvider = (CredentialsProvider) context
                .getAttribute(ClientContext.CREDS_PROVIDER);
        Credentials creds = credsProvider.getCredentials(AuthScope.ANY);
        authState.update(authScheme, creds);
    }/*w w w . ja  va2  s  .  c  o m*/
}

From source file:ca.uhn.fhir.rest.client.HttpBasicAuthInterceptor.java

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

    if (authState.getAuthScheme() == null) {
        Credentials creds = new UsernamePasswordCredentials(myUsername, myPassword);
        authState.update(new BasicScheme(), creds);
    }// w  ww .  j  a  v  a 2 s  .c  om

}

From source file:com.github.parisoft.resty.request.retry.RequestRetryHandler.java

private boolean isRequestIdempotent(HttpContext context) {
    final HttpUriRequest request = (HttpUriRequest) context.getAttribute(HttpCoreContext.HTTP_REQUEST);
    return IdempotentRequestMethods.getInstance().contains(request.getMethod());
}

From source file:org.callimachusproject.client.GUnzipInterceptor.java

@Override
public void process(HttpResponse resp, HttpContext context) throws HttpException, IOException {
    HttpRequest req = (HttpRequest) context.getAttribute(HttpCoreContext.HTTP_REQUEST);
    HttpEntity entity = resp.getEntity();
    if (entity == null)
        return;/* ww w  . j av a 2  s .  c om*/
    Header cache = req.getFirstHeader("Cache-Control");
    if (cache != null && cache.getValue().contains("no-transform"))
        return;
    Header encoding = resp.getFirstHeader("Content-Encoding");
    if (encoding != null && "gzip".equals(encoding.getValue())) {
        resp.removeHeaders("Content-MD5");
        resp.removeHeaders("Content-Length");
        resp.setHeader("Content-Encoding", "identity");
        resp.addHeader("Warning", WARN_214);
        resp.setEntity(gunzip(entity));
    }
}

From source file:com.controlj.experiment.bulktrend.trendclient.PreemptiveAuthRequestInterceptor.java

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 avaialble 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 a  v a  2 s  .com*/
            authState.setAuthScheme(authScheme);
            authState.setCredentials(creds);
        }
    }
}

From source file:org.orbeon.oxf.resources.handler.PreemptiveAuthHttpRequestInterceptor.java

public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
    AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
    CredentialsProvider credentialsProvider = (CredentialsProvider) context
            .getAttribute(ClientContext.CREDS_PROVIDER);
    HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);

    // If not 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 credentials = credentialsProvider.getCredentials(authScope);
        // If found, generate BasicScheme preemptively
        if (credentials != null) {
            authState.setAuthScheme(credentials instanceof NTCredentials ? new NTLMScheme(new JCIFSEngine())
                    : new BasicScheme());
            authState.setCredentials(credentials);
        }/*from   w  ww.  j  av  a  2  s. co  m*/
    }
}

From source file:org.apache.camel.component.http4.handler.AuthenticationValidationHandler.java

public void handle(final HttpRequest request, final HttpResponse response, final HttpContext context)
        throws HttpException, IOException {
    if (!getExpectedCredential().equals(context.getAttribute("creds"))) {
        response.setStatusCode(HttpStatus.SC_UNAUTHORIZED);
        return;//w  ww  . j ava  2  s  .c  o m
    }

    super.handle(request, response, context);
}

From source file:onl.area51.httpd.service.RequestScopeHandler.java

@Override
public void begin(HttpRequest req, HttpContext ctx) throws HttpException, IOException {
    Map<String, Object> dataStore = (Map<String, Object>) ctx.getAttribute(ATTR);
    if (dataStore == null) {
        dataStore = new HashMap<>();
        ctx.setAttribute(ATTR, dataStore);
        requestContext.associate(dataStore);
        requestContext.activate();// w ww.  j av a 2s.c o  m
    }
}

From source file:org.apache.camel.component.http4.handler.ProxyAuthenticationValidationHandler.java

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

    if (!getExpectedCredential().equals(context.getAttribute("proxy-creds"))) {
        response.setStatusCode(HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED);
        return;/* w w w . j a  va2 s .  c  om*/
    }

    super.handle(request, response, context);
}