Example usage for org.apache.http.impl.nio.client HttpExchange setTimeout

List of usage examples for org.apache.http.impl.nio.client HttpExchange setTimeout

Introduction

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

Prototype

public void setTimeout(int timeout) 

Source Link

Usage

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

public void requestReady(final NHttpClientConnection conn) {
    HttpContext context = conn.getContext();
    HttpExchange httpexchange = getHttpExchange(context);
    HttpAsyncExchangeHandler<?> handler = getHandler(context);
    if (this.log.isDebugEnabled()) {
        this.log.debug("Request ready " + formatState(conn, httpexchange));
    }//  w  w w .j  av  a2s  .com
    if (httpexchange.getRequestState() != MessageState.READY) {
        return;
    }
    if (handler == null || handler.isDone()) {
        if (this.log.isDebugEnabled()) {
            this.log.debug("No request submitted " + formatState(conn, httpexchange));
        }
        return;
    }
    try {
        HttpRequest request = handler.generateRequest();
        httpexchange.setRequest(request);

        HttpEntityEnclosingRequest entityReq = null;
        if (request instanceof HttpEntityEnclosingRequest) {
            entityReq = (HttpEntityEnclosingRequest) request;
        }

        conn.submitRequest(request);

        if (entityReq != null) {
            if (entityReq.expectContinue()) {
                int timeout = conn.getSocketTimeout();
                httpexchange.setTimeout(timeout);
                timeout = request.getParams().getIntParameter(CoreProtocolPNames.WAIT_FOR_CONTINUE, 3000);
                conn.setSocketTimeout(timeout);
                httpexchange.setRequestState(MessageState.ACK);
            } else {
                httpexchange.setRequestState(MessageState.BODY_STREAM);
            }
        } else {
            httpexchange.setRequestState(MessageState.COMPLETED);
        }
    } catch (IOException ex) {
        if (this.log.isDebugEnabled()) {
            this.log.debug("I/O error: " + ex.getMessage(), ex);
        }
        shutdownConnection(conn);
        handler.failed(ex);
    } catch (HttpException ex) {
        if (this.log.isDebugEnabled()) {
            this.log.debug("HTTP protocol exception: " + ex.getMessage(), ex);
        }
        closeConnection(conn);
        handler.failed(ex);
    }
}