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.github.avarabyeu.restendpoint.http.PreemptiveAuthInterceptor.java

/**
 * Adds provided auth scheme to the client if there are no another provided
 * auth schemes//from   w  w  w.j av  a2s .c om
 */
@Override
public final void process(final HttpRequest request, final HttpContext context)
        throws HttpException, IOException {

    AuthState authState = (AuthState) context.getAttribute(HttpClientContext.TARGET_AUTH_STATE);
    if (authState.getAuthScheme() == null) {

        HttpHost targetHost = (HttpHost) context.getAttribute(HttpCoreContext.HTTP_TARGET_HOST);
        AuthCache authCache = new BasicAuthCache();
        authCache.put(targetHost, new BasicScheme());
        context.setAttribute(HttpClientContext.AUTH_CACHE, authCache);
    }
}

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

@Override
public void end(HttpResponse resp, HttpContext ctx) throws HttpException, IOException {
    Map<String, Object> dataStore = (Map<String, Object>) ctx.getAttribute(ATTR);
    if (dataStore != null) {
        try {/*from ww w  .ja va2s  .  c  o m*/
            requestContext.invalidate();
            requestContext.deactivate();
        } finally {
            requestContext.dissociate(dataStore);
        }
    }
}

From source file:io.github.kitarek.elasthttpd.server.producers.HttpConnectionCompliantResponseProducer.java

private HttpServerConnection getConnectionFromContext(HttpContext httpContext) {
    return (HttpServerConnection) notNull(httpContext.getAttribute(HTTP_CONNECTION),
            "HTTP connection needs to be defined in HTTP context");
}

From source file:net.oneandone.shared.artifactory.PreemptiveRequestInterceptor.java

@Override
public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
    final HttpHost httpHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
    final String httpHostString = httpHost.toHostString();
    final UsernamePasswordCredentials credentials = credentialsMap.get(httpHostString);
    if (credentials != null) {
        final String userName = credentials.getUserName();
        final String auth = userName + ":" + credentials.getPassword();
        LOG.debug("Adding authorization for host {}, userName={}", httpHostString, userName);
        request.addHeader(AUTHORIZATION_HEADER,
                "Basic " + BaseEncoding.base64().encode(auth.getBytes(Charsets.UTF_8)));
    } else {//from   w  w w  . j a  va 2 s . c  om
        LOG.debug("No authorization for host {}", httpHostString);
    }
}

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

public void process(org.apache.http.HttpResponse response, HttpContext context)
        throws HttpException, IOException {
    TraceContext traceContext = (TraceContext) context.getAttribute(TraceInterceptorUtil.TRACE_CONTEXT_KEY);
    interceptor.process(new ResponseAdapter(response), traceContext);
}

From source file:io.github.kitarek.elasthttpd.server.producers.HttpConnectionCompliantResponseProducer.java

private Optional<HttpMethod> getHttpMethodOptionalFromContext(HttpContext httpContext) {
    final Object requestObject = notNull(httpContext.getAttribute(HTTP_REQUEST),
            "HTTP request must be defined in context");
    return (requestObject instanceof HttpRequest)
            ? getHttpMethodOptionalFromRequest((HttpRequest) requestObject)
            : Optional.<HttpMethod>empty();
}

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

@Override
public void process(HttpRequest httpRequest, HttpContext httpContext) throws HttpException, IOException {
    try {/*from   www.j  a  v a 2 s  . com*/
        Span clientSpan = (Span) httpContext.getAttribute(Constants.CURRENT_SPAN_CONTEXT_KEY);

        tracer.inject(clientSpan.context(), Format.Builtin.HTTP_HEADERS, new ClientRequestCarrier(httpRequest));

    } catch (Exception e) {
        log.error("Could not start client tracing span.", e);
    }
}

From source file:de.escidoc.core.common.util.security.PreemptiveAuthInterceptor.java

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

    // If no auth scheme avaialble yet, try to initialize it preemptively
    if (authState.getAuthScheme() == null) {
        final AuthScheme authScheme = (AuthScheme) context.getAttribute("preemptive-auth");
        final CredentialsProvider credsProvider = (CredentialsProvider) context
                .getAttribute(ClientContext.CREDS_PROVIDER);
        final HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
        if (authScheme != null) {
            final Credentials creds = credsProvider
                    .getCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()));
            if (creds == null) {
                throw new HttpException("No credentials for preemptive authentication.");
            }/*w w w. j  a v  a2s .  co m*/
            authState.setAuthScheme(authScheme);
            authState.setCredentials(creds);
        }
    }

}

From source file:com.subgraph.vega.internal.http.proxy.ConnectionTask.java

private void processRequestContext(HttpContext context) throws IOException {
    final ProxyTransaction transaction = (ProxyTransaction) context
            .getAttribute(HttpProxy.PROXY_HTTP_TRANSACTION);
    if (transaction != null) {
        proxy.completeRequest(transaction);
    }/*w  w w . j  a  va 2  s  .  c om*/
}

From source file:org.nuxeo.ecm.automation.client.jaxrs.impl.HttpPreemptiveAuthInterceptor.java

@Override
public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
    AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
    // If not auth scheme has been initialized yet
    if (authState.getAuthScheme() != null) {
        return;/*from  ww w  .  j  av a2  s .  c  o  m*/
    }

    // fetch credentials
    CredentialsProvider credsProvider = (CredentialsProvider) context
            .getAttribute(ClientContext.CREDS_PROVIDER);
    HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
    AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort());

    // Obtain credentials matching the target host
    Credentials creds = credsProvider.getCredentials(authScope);

    if (creds == null) {
        log.warn("no credentials provided for " + authScope);
        return;
    }

    authState.setAuthScheme(authScheme);
    authState.setCredentials(creds);
}