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

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

Introduction

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

Prototype

final HttpResponse getCurrentResponse() 

Source Link

Usage

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

@Override
public void responseCompleted(final InternalState state, final AbstractClientExchangeHandler<?> handler)
        throws IOException, HttpException {
    final HttpClientContext localContext = state.getLocalContext();
    final HttpResponse currentResponse = handler.getCurrentResponse();

    if (!handler.isRouteEstablished()) {
        final int status = currentResponse.getStatusLine().getStatusCode();
        if (status == HttpStatus.SC_OK) {
            handler.setCurrentResponse(null);
            return;
        }/*www .j a  v a2  s . co m*/
    }

    final boolean keepAlive = handler.manageConnectionPersistence();
    if (!keepAlive) {
        handler.releaseConnection();
        final AuthState proxyAuthState = localContext.getProxyAuthState();
        if (proxyAuthState.getState() == AuthProtocolState.SUCCESS && proxyAuthState.getAuthScheme() != null
                && proxyAuthState.getAuthScheme().isConnectionBased()) {
            if (this.log.isDebugEnabled()) {
                this.log.debug("[exchange: " + state.getId() + "] Resetting proxy auth state");
            }
            proxyAuthState.reset();
        }
        final AuthState targetAuthState = localContext.getTargetAuthState();
        if (targetAuthState.getState() == AuthProtocolState.SUCCESS && targetAuthState.getAuthScheme() != null
                && targetAuthState.getAuthScheme().isConnectionBased()) {
            if (this.log.isDebugEnabled()) {
                this.log.debug("[exchange: " + state.getId() + "] Resetting target auth state");
            }
            targetAuthState.reset();
        }
    }

    Object userToken = localContext.getUserToken();
    if (userToken == null) {
        userToken = this.userTokenHandler.getUserToken(localContext);
        localContext.setAttribute(HttpClientContext.USER_TOKEN, userToken);
    }

    if (state.getFinalResponse() != null) {
        final HttpAsyncResponseConsumer<?> responseConsumer = state.getResponseConsumer();
        responseConsumer.responseCompleted(localContext);
        if (this.log.isDebugEnabled()) {
            this.log.debug("[exchange: " + state.getId() + "] Response processed");
        }
        handler.releaseConnection();
    } else {
        if (state.getRedirect() != null) {
            final HttpUriRequest redirect = state.getRedirect();
            final URI uri = redirect.getURI();
            if (this.log.isDebugEnabled()) {
                this.log.debug("[exchange: " + state.getId() + "] Redirecting to '" + uri + "'");
            }
            state.setRedirect(null);

            final HttpHost newTarget = URIUtils.extractHost(uri);
            if (newTarget == null) {
                throw new ProtocolException("Redirect URI does not specify a valid host name: " + uri);
            }

            // Reset auth states if redirecting to another host
            final HttpRoute route = handler.getRoute();
            if (!route.getTargetHost().equals(newTarget)) {
                final AuthState targetAuthState = localContext.getTargetAuthState();
                if (this.log.isDebugEnabled()) {
                    this.log.debug("[exchange: " + state.getId() + "] Resetting target auth state");
                }
                targetAuthState.reset();
                final AuthState proxyAuthState = localContext.getProxyAuthState();
                final AuthScheme authScheme = proxyAuthState.getAuthScheme();
                if (authScheme != null && authScheme.isConnectionBased()) {
                    if (this.log.isDebugEnabled()) {
                        this.log.debug("[exchange: " + state.getId() + "] Resetting proxy auth state");
                    }
                    proxyAuthState.reset();
                }
            }

            if (!redirect.headerIterator().hasNext()) {
                final HttpRequest original = state.getMainRequest().getOriginal();
                redirect.setHeaders(original.getAllHeaders());
            }

            final HttpRequestWrapper newRequest = HttpRequestWrapper.wrap(redirect);
            final HttpRoute newRoute = this.routePlanner.determineRoute(newTarget, newRequest, localContext);
            if (!route.equals(newRoute)) {
                handler.releaseConnection();
            }
            handler.setRoute(newRoute);
            handler.setCurrentRequest(newRequest);
            state.setMainRequest(newRequest);
            prepareRequest(state, handler);
        }
    }
    handler.setCurrentResponse(null);
}

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

private boolean handleConnectResponse(final InternalState state, final AbstractClientExchangeHandler<?> handler)
        throws HttpException {
    final HttpClientContext localContext = state.getLocalContext();
    final RequestConfig config = localContext.getRequestConfig();
    if (config.isAuthenticationEnabled()) {
        final CredentialsProvider credsProvider = localContext.getCredentialsProvider();
        if (credsProvider != null) {
            final HttpRoute route = handler.getRoute();
            final HttpHost proxy = route.getProxyHost();
            final HttpResponse currentResponse = handler.getCurrentResponse();
            final AuthState proxyAuthState = localContext.getProxyAuthState();
            if (this.authenticator.isAuthenticationRequested(proxy, currentResponse, this.proxyAuthStrategy,
                    proxyAuthState, localContext)) {
                return this.authenticator.handleAuthChallenge(proxy, currentResponse, this.proxyAuthStrategy,
                        proxyAuthState, localContext);
            }/*www  . ja v  a2  s .c om*/
        }
    }
    return false;
}

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);
            }// ww w .  ja v  a 2s  . c  om
            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;
}

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

private boolean needAuthentication(final InternalState state, final AbstractClientExchangeHandler<?> handler)
        throws HttpException {
    final HttpClientContext localContext = state.getLocalContext();
    final CredentialsProvider credsProvider = localContext.getCredentialsProvider();
    if (credsProvider != null) {
        final HttpRoute route = handler.getRoute();
        final HttpResponse currentResponse = handler.getCurrentResponse();
        HttpHost target = localContext.getTargetHost();
        if (target == null) {
            target = route.getTargetHost();
        }//w  w w  .  j a  va 2 s.  c o m
        if (target.getPort() < 0) {
            target = new HttpHost(target.getHostName(), route.getTargetHost().getPort(),
                    target.getSchemeName());
        }
        final AuthState targetAuthState = localContext.getTargetAuthState();
        final AuthState proxyAuthState = localContext.getProxyAuthState();

        final boolean targetAuthRequested = this.authenticator.isAuthenticationRequested(target,
                currentResponse, this.targetAuthStrategy, targetAuthState, localContext);

        HttpHost proxy = route.getProxyHost();
        // if proxy is not set use target host instead
        if (proxy == null) {
            proxy = route.getTargetHost();
        }
        final boolean proxyAuthRequested = this.authenticator.isAuthenticationRequested(proxy, currentResponse,
                this.proxyAuthStrategy, proxyAuthState, localContext);

        if (targetAuthRequested) {
            return this.authenticator.handleAuthChallenge(target, currentResponse, this.targetAuthStrategy,
                    targetAuthState, localContext);
        }
        if (proxyAuthRequested) {
            return this.authenticator.handleAuthChallenge(proxy, currentResponse, this.proxyAuthStrategy,
                    proxyAuthState, localContext);
        }
    }
    return false;
}