Example usage for org.apache.http.impl.nio.client InternalState getFinalResponse

List of usage examples for org.apache.http.impl.nio.client InternalState getFinalResponse

Introduction

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

Prototype

public HttpResponse getFinalResponse() 

Source Link

Usage

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

@Override
public void responseReceived(final HttpResponse response, final InternalState state,
        final AbstractClientExchangeHandler<?> handler) throws IOException, HttpException {
    if (this.log.isDebugEnabled()) {
        this.log.debug("[exchange: " + state.getId() + "] Response received " + response.getStatusLine());
    }/*from ww  w  .java 2s  .  c om*/
    final HttpClientContext context = state.getLocalContext();
    context.setAttribute(HttpCoreContext.HTTP_RESPONSE, response);
    this.httpProcessor.process(response, context);

    handler.setCurrentResponse(response);

    if (!handler.isRouteEstablished()) {
        final int status = response.getStatusLine().getStatusCode();
        if (status < 200) {
            throw new HttpException("Unexpected response to CONNECT request: " + response.getStatusLine());
        }
        if (status == HttpStatus.SC_OK) {
            handler.onRouteTunnelToTarget();
            handler.setCurrentRequest(null);
        } else {
            if (!handleConnectResponse(state, handler)) {
                state.setFinalResponse(response);
            }
        }
    } else {
        if (!handleResponse(state, handler)) {
            state.setFinalResponse(response);
        }
    }
    if (state.getFinalResponse() != null) {
        final HttpAsyncResponseConsumer<?> responseConsumer = state.getResponseConsumer();
        responseConsumer.responseReceived(response);
    }
}

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

@Override
public void consumeContent(final InternalState state, final ContentDecoder decoder, final IOControl ioctrl)
        throws IOException {
    if (this.log.isDebugEnabled()) {
        this.log.debug("[exchange: " + state.getId() + "] Consume content");
    }/*w  w w.java2 s . co  m*/
    if (state.getFinalResponse() != null) {
        final HttpAsyncResponseConsumer<?> responseConsumer = state.getResponseConsumer();
        responseConsumer.consumeContent(decoder, ioctrl);
    } else {
        final ByteBuffer tmpbuf = state.getTmpbuf();
        tmpbuf.clear();
        decoder.read(tmpbuf);
    }
}

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;
        }//from  w  w  w  .  j  a  v a 2 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);
}