Example usage for org.apache.http.client.protocol ClientContext REQUEST_CONFIG

List of usage examples for org.apache.http.client.protocol ClientContext REQUEST_CONFIG

Introduction

In this page you can find the example usage for org.apache.http.client.protocol ClientContext REQUEST_CONFIG.

Prototype

String REQUEST_CONFIG

To view the source code for org.apache.http.client.protocol ClientContext REQUEST_CONFIG.

Click Source Link

Document

Attribute name of a org.apache.http.client.config.RequestConfig object that represents the actual request configuration.

Usage

From source file:org.apache.http.impl.client.AbstractHttpClient.java

@Override
protected final CloseableHttpResponse doExecute(final HttpHost target, final HttpRequest request,
        final HttpContext context) throws IOException, ClientProtocolException {

    Args.notNull(request, "HTTP request");
    // a null target may be acceptable, this depends on the route planner
    // a null context is acceptable, default context created below

    HttpContext execContext = null;
    RequestDirector director = null;/*from   w w  w .j  av a2s.c  o m*/
    HttpRoutePlanner routePlanner = null;
    ConnectionBackoffStrategy connectionBackoffStrategy = null;
    BackoffManager backoffManager = null;

    // Initialize the request execution context making copies of
    // all shared objects that are potentially threading unsafe.
    synchronized (this) {

        final HttpContext defaultContext = createHttpContext();
        if (context == null) {
            execContext = defaultContext;
        } else {
            execContext = new DefaultedHttpContext(context, defaultContext);
        }
        final HttpParams params = determineParams(request);
        final RequestConfig config = HttpClientParamConfig.getRequestConfig(params);
        execContext.setAttribute(ClientContext.REQUEST_CONFIG, config);

        // Create a director for this request
        director = createClientRequestDirector(getRequestExecutor(), getConnectionManager(),
                getConnectionReuseStrategy(), getConnectionKeepAliveStrategy(), getRoutePlanner(),
                getProtocolProcessor(), getHttpRequestRetryHandler(), getRedirectStrategy(),
                getTargetAuthenticationStrategy(), getProxyAuthenticationStrategy(), getUserTokenHandler(),
                params);
        routePlanner = getRoutePlanner();
        connectionBackoffStrategy = getConnectionBackoffStrategy();
        backoffManager = getBackoffManager();
    }

    try {
        if (connectionBackoffStrategy != null && backoffManager != null) {
            final HttpHost targetForRoute = (target != null) ? target
                    : (HttpHost) determineParams(request).getParameter(ClientPNames.DEFAULT_HOST);
            final HttpRoute route = routePlanner.determineRoute(targetForRoute, request, execContext);

            final CloseableHttpResponse out;
            try {
                out = CloseableHttpResponseProxy.newProxy(director.execute(target, request, execContext));
            } catch (final RuntimeException re) {
                if (connectionBackoffStrategy.shouldBackoff(re)) {
                    backoffManager.backOff(route);
                }
                throw re;
            } catch (final Exception e) {
                if (connectionBackoffStrategy.shouldBackoff(e)) {
                    backoffManager.backOff(route);
                }
                if (e instanceof HttpException) {
                    throw (HttpException) e;
                }
                if (e instanceof IOException) {
                    throw (IOException) e;
                }
                throw new UndeclaredThrowableException(e);
            }
            if (connectionBackoffStrategy.shouldBackoff(out)) {
                backoffManager.backOff(route);
            } else {
                backoffManager.probe(route);
            }
            return out;
        } else {
            return CloseableHttpResponseProxy.newProxy(director.execute(target, request, execContext));
        }
    } catch (final HttpException httpException) {
        throw new ClientProtocolException(httpException);
    }
}

From source file:org.apache.http.impl.nio.client.DefaultAsyncRequestDirector.java

public synchronized void start() {
    try {/*from   ww w. ja  v  a 2s  . co  m*/
        if (this.log.isDebugEnabled()) {
            this.log.debug("[exchange: " + this.id + "] start execution");
        }
        this.localContext.setAttribute(ClientContext.TARGET_AUTH_STATE, this.targetAuthState);
        this.localContext.setAttribute(ClientContext.PROXY_AUTH_STATE, this.proxyAuthState);

        final HttpHost target = this.requestProducer.getTarget();
        final HttpRequest request = this.requestProducer.generateRequest();
        if (request instanceof AbortableHttpRequest) {
            ((AbortableHttpRequest) request).setReleaseTrigger(new ConnectionReleaseTrigger() {

                @Override
                public void releaseConnection() throws IOException {
                }

                @Override
                public void abortConnection() throws IOException {
                    cancel();
                }

            });
        }
        this.params = new ClientParamsStack(null, this.clientParams, request.getParams(), null);
        final RequestWrapper wrapper = wrapRequest(request);
        wrapper.setParams(this.params);
        final HttpRoute route = determineRoute(target, wrapper, this.localContext);
        this.mainRequest = new RoutedRequest(wrapper, route);
        final RequestConfig config = ParamConfig.getRequestConfig(params);
        this.localContext.setAttribute(ClientContext.REQUEST_CONFIG, config);
        this.requestContentProduced = false;
        requestConnection();
    } catch (final Exception ex) {
        failed(ex);
    }
}