Example usage for org.apache.http.nio.protocol HttpAsyncRequestProducer resetRequest

List of usage examples for org.apache.http.nio.protocol HttpAsyncRequestProducer resetRequest

Introduction

In this page you can find the example usage for org.apache.http.nio.protocol HttpAsyncRequestProducer resetRequest.

Prototype

void resetRequest() throws IOException;

Source Link

Document

Invoked to reset the producer to its initial state.

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();/*w  w  w. j a v  a  2s .co  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

@Override
public void produceContent(final InternalState state, final ContentEncoder encoder, final IOControl ioctrl)
        throws IOException {
    if (this.log.isDebugEnabled()) {
        this.log.debug("[exchange: " + state.getId() + "] produce content");
    }//from w  w w  . ja  v a  2  s .c o m
    final HttpAsyncRequestProducer requestProducer = state.getRequestProducer();
    state.setRequestContentProduced();
    requestProducer.produceContent(encoder, ioctrl);
    if (encoder.isCompleted()) {
        requestProducer.resetRequest();
    }
}

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

@Override
public void produceContent(final ContentEncoder encoder, final IOControl ioctrl) throws IOException {
    if (this.log.isDebugEnabled()) {
        this.log.debug("[exchange: " + getId() + "] produce content");
    }/*from ww w  . j  a va 2s .co  m*/
    final HttpAsyncRequestProducer requestProducer = this.requestProducerRef.get();
    Asserts.check(requestProducer != null, "Inconsistent state: request producer is null");
    requestProducer.produceContent(encoder, ioctrl);
    if (encoder.isCompleted()) {
        requestProducer.resetRequest();
    }
}