Example usage for org.apache.http.impl.nio.client MessageState BODY_STREAM

List of usage examples for org.apache.http.impl.nio.client MessageState BODY_STREAM

Introduction

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

Prototype

MessageState BODY_STREAM

To view the source code for org.apache.http.impl.nio.client MessageState BODY_STREAM.

Click 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 a 2s  .co m*/
    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);
    }
}

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

public void responseReceived(final NHttpClientConnection conn) {
    HttpContext context = conn.getContext();
    HttpExchange httpexchange = getHttpExchange(context);
    HttpAsyncExchangeHandler<?> handler = getHandler(context);
    if (this.log.isDebugEnabled()) {
        this.log.debug("Response received " + formatState(conn, httpexchange));
    }//from  w  w w . ja  v  a2s  . co m
    try {
        HttpResponse response = conn.getHttpResponse();
        HttpRequest request = httpexchange.getRequest();

        int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode < HttpStatus.SC_OK) {
            // 1xx intermediate response
            if (statusCode == HttpStatus.SC_CONTINUE && httpexchange.getRequestState() == MessageState.ACK) {
                continueRequest(conn, httpexchange);
                httpexchange.setRequestState(MessageState.BODY_STREAM);
            }
            return;
        } else {
            httpexchange.setResponse(response);
            if (httpexchange.getRequestState() == MessageState.ACK) {
                cancelRequest(conn, httpexchange);
                httpexchange.setRequestState(MessageState.COMPLETED);
            } else if (httpexchange.getRequestState() == MessageState.BODY_STREAM) {
                // Early response
                cancelRequest(conn, httpexchange);
                httpexchange.invalidate();
                conn.suspendOutput();
            }
        }
        handler.responseReceived(response);
        if (!canResponseHaveBody(request, response)) {
            processResponse(conn, httpexchange, handler);
        }
    } 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);
    }
}

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

public void timeout(final NHttpClientConnection conn) {
    HttpContext context = conn.getContext();
    HttpExchange httpexchange = getHttpExchange(context);
    HttpAsyncExchangeHandler<?> handler = getHandler(context);
    if (this.log.isDebugEnabled()) {
        this.log.debug("Timeout " + formatState(conn, httpexchange));
    }/*from   w  w  w . j  a v a2 s  .com*/
    try {
        if (httpexchange.getRequestState() == MessageState.ACK) {
            continueRequest(conn, httpexchange);
            httpexchange.setRequestState(MessageState.BODY_STREAM);
        } else {
            if (conn.getStatus() == NHttpConnection.ACTIVE) {
                conn.close();
                if (conn.getStatus() == NHttpConnection.CLOSING) {
                    // Give the connection some grace time to
                    // close itself nicely
                    conn.setSocketTimeout(250);
                }
            } else {
                conn.shutdown();
            }
        }
    } catch (IOException ex) {
        if (this.log.isDebugEnabled()) {
            this.log.debug("I/O error: " + ex.getMessage(), ex);
        }
        shutdownConnection(conn);
        handler.failed(ex);
    }
}