Example usage for org.apache.http.impl.nio.client AbstractClientExchangeHandler getCurrentRequest

List of usage examples for org.apache.http.impl.nio.client AbstractClientExchangeHandler getCurrentRequest

Introduction

In this page you can find the example usage for org.apache.http.impl.nio.client AbstractClientExchangeHandler getCurrentRequest.

Prototype

final HttpRequestWrapper getCurrentRequest() 

Source Link

Usage

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

@Override
public HttpRequest generateRequest(final InternalState state, final AbstractClientExchangeHandler<?> handler)
        throws IOException, HttpException {

    final HttpRoute route = handler.getRoute();

    handler.verifytRoute();//from   www. j av a  2  s.c  o  m

    if (!handler.isRouteEstablished()) {
        int step;
        loop: do {
            final HttpRoute fact = handler.getActualRoute();
            step = this.routeDirector.nextStep(route, fact);
            switch (step) {
            case HttpRouteDirector.CONNECT_TARGET:
                handler.onRouteToTarget();
                break;
            case HttpRouteDirector.CONNECT_PROXY:
                handler.onRouteToProxy();
                break;
            case HttpRouteDirector.TUNNEL_TARGET:
                if (this.log.isDebugEnabled()) {
                    this.log.debug("[exchange: " + state.getId() + "] Tunnel required");
                }
                final HttpRequest connect = createConnectRequest(route, state);
                handler.setCurrentRequest(HttpRequestWrapper.wrap(connect));
                break loop;
            case HttpRouteDirector.TUNNEL_PROXY:
                throw new HttpException("Proxy chains are not supported");
            case HttpRouteDirector.LAYER_PROTOCOL:
                handler.onRouteUpgrade();
                break;
            case HttpRouteDirector.UNREACHABLE:
                throw new HttpException(
                        "Unable to establish route: " + "planned = " + route + "; current = " + fact);
            case HttpRouteDirector.COMPLETE:
                handler.onRouteComplete();
                this.log.debug("Connection route established");
                break;
            default:
                throw new IllegalStateException("Unknown step indicator " + step + " from RouteDirector.");
            }
        } while (step > HttpRouteDirector.COMPLETE);
    }

    final HttpClientContext localContext = state.getLocalContext();
    HttpRequestWrapper currentRequest = handler.getCurrentRequest();
    if (currentRequest == null) {
        currentRequest = state.getMainRequest();
        handler.setCurrentRequest(currentRequest);
    }

    if (handler.isRouteEstablished()) {
        state.incrementExecCount();
        if (state.getExecCount() > 1) {
            final HttpAsyncRequestProducer requestProducer = state.getRequestProducer();
            if (!requestProducer.isRepeatable() && state.isRequestContentProduced()) {
                throw new NonRepeatableRequestException(
                        "Cannot retry request " + "with a non-repeatable request entity.");
            }
            requestProducer.resetRequest();
        }
        if (this.log.isDebugEnabled()) {
            this.log.debug("[exchange: " + state.getId() + "] Attempt " + state.getExecCount()
                    + " to execute request");
        }

        if (!currentRequest.containsHeader(AUTH.WWW_AUTH_RESP)) {
            final AuthState targetAuthState = localContext.getTargetAuthState();
            if (this.log.isDebugEnabled()) {
                this.log.debug("Target auth state: " + targetAuthState.getState());
            }
            this.authenticator.generateAuthResponse(currentRequest, targetAuthState, localContext);
        }
        if (!currentRequest.containsHeader(AUTH.PROXY_AUTH_RESP) && !route.isTunnelled()) {
            final AuthState proxyAuthState = localContext.getProxyAuthState();
            if (this.log.isDebugEnabled()) {
                this.log.debug("Proxy auth state: " + proxyAuthState.getState());
            }
            this.authenticator.generateAuthResponse(currentRequest, proxyAuthState, localContext);
        }
    } else {
        if (!currentRequest.containsHeader(AUTH.PROXY_AUTH_RESP)) {
            final AuthState proxyAuthState = localContext.getProxyAuthState();
            if (this.log.isDebugEnabled()) {
                this.log.debug("Proxy auth state: " + proxyAuthState.getState());
            }
            this.authenticator.generateAuthResponse(currentRequest, proxyAuthState, localContext);
        }
    }

    final NHttpClientConnection managedConn = handler.getConnection();
    localContext.setAttribute(HttpCoreContext.HTTP_CONNECTION, managedConn);
    final RequestConfig config = localContext.getRequestConfig();
    if (config.getSocketTimeout() > 0) {
        managedConn.setSocketTimeout(config.getSocketTimeout());
    }
    return currentRequest;
}

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

private void prepareRequest(final InternalState state, final AbstractClientExchangeHandler<?> handler)
        throws IOException, HttpException {
    final HttpClientContext localContext = state.getLocalContext();
    final HttpRequestWrapper currentRequest = handler.getCurrentRequest();
    final HttpRoute route = handler.getRoute();

    final HttpRequest original = currentRequest.getOriginal();
    URI uri = null;/*  www . j ava  2 s. c  om*/
    if (original instanceof HttpUriRequest) {
        uri = ((HttpUriRequest) original).getURI();
    } else {
        final String uriString = original.getRequestLine().getUri();
        try {
            uri = URI.create(uriString);
        } catch (final IllegalArgumentException ex) {
            if (this.log.isDebugEnabled()) {
                this.log.debug("Unable to parse '" + uriString + "' as a valid URI; "
                        + "request URI and Host header may be inconsistent", ex);
            }
        }

    }
    currentRequest.setURI(uri);

    // Re-write request URI if needed
    rewriteRequestURI(currentRequest, route);

    HttpHost target = null;
    if (uri != null && uri.isAbsolute() && uri.getHost() != null) {
        target = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme());
    }
    if (target == null) {
        target = route.getTargetHost();
    }

    // Get user info from the URI
    if (uri != null) {
        final String userinfo = uri.getUserInfo();
        if (userinfo != null) {
            final CredentialsProvider credsProvider = localContext.getCredentialsProvider();
            credsProvider.setCredentials(new AuthScope(target), new UsernamePasswordCredentials(userinfo));
        }
    }

    localContext.setAttribute(HttpCoreContext.HTTP_REQUEST, currentRequest);
    localContext.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, target);
    localContext.setAttribute(HttpClientContext.HTTP_ROUTE, route);
    this.httpProcessor.process(currentRequest, localContext);
}

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

private boolean handleResponse(final InternalState state, final AbstractClientExchangeHandler<?> handler)
        throws HttpException {
    final HttpClientContext localContext = state.getLocalContext();
    final RequestConfig config = localContext.getRequestConfig();
    if (config.isAuthenticationEnabled()) {
        if (needAuthentication(state, handler)) {
            // discard previous auth headers
            final HttpRequestWrapper currentRequest = handler.getCurrentRequest();
            final HttpRequest original = currentRequest.getOriginal();
            if (!original.containsHeader(AUTH.WWW_AUTH_RESP)) {
                currentRequest.removeHeaders(AUTH.WWW_AUTH_RESP);
            }//  w w  w. j  ava 2  s .  c  o m
            if (!original.containsHeader(AUTH.PROXY_AUTH_RESP)) {
                currentRequest.removeHeaders(AUTH.PROXY_AUTH_RESP);
            }
            return true;
        }
    }
    if (config.isRedirectsEnabled()) {
        final HttpRequest currentRequest = handler.getCurrentRequest();
        final HttpResponse currentResponse = handler.getCurrentResponse();
        if (this.redirectStrategy.isRedirected(currentRequest, currentResponse, localContext)) {
            final int maxRedirects = config.getMaxRedirects() >= 0 ? config.getMaxRedirects() : 100;
            if (state.getRedirectCount() >= maxRedirects) {
                throw new RedirectException("Maximum redirects (" + maxRedirects + ") exceeded");
            }
            state.incrementRedirectCount();
            final HttpUriRequest redirect = this.redirectStrategy.getRedirect(currentRequest, currentResponse,
                    localContext);
            state.setRedirect(redirect);
            return true;
        }
    }
    return false;
}