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

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

Introduction

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

Prototype

public MessageState getRequestState() 

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));
    }//from  w w w. java 2s . c o  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 outputReady(final NHttpClientConnection conn, final ContentEncoder encoder) {
    HttpContext context = conn.getContext();
    HttpExchange httpexchange = getHttpExchange(context);
    HttpAsyncExchangeHandler<?> handler = getHandler(context);
    if (this.log.isDebugEnabled()) {
        this.log.debug("Output ready " + formatState(conn, httpexchange));
    }/*ww  w  .j  a  v a 2 s  . c  o  m*/
    try {
        if (httpexchange.getRequestState() == MessageState.ACK) {
            conn.suspendOutput();
            return;
        }
        handler.produceContent(encoder, conn);
        if (encoder.isCompleted()) {
            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);
    }
}

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  a 2  s.c  o 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 .co  m
    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);
    }
}

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

private String formatState(final NHttpConnection conn, final HttpExchange httpexchange) {
    StringBuilder buf = new StringBuilder();
    buf.append("[");
    if (conn.isOpen() && (conn instanceof HttpInetConnection)) {
        HttpInetConnection inetconn = (HttpInetConnection) conn;
        buf.append(inetconn.getRemoteAddress());
        buf.append(":");
        buf.append(inetconn.getRemotePort());
    }//w ww.ja v  a  2s.co  m
    buf.append("(");
    buf.append(conn.isOpen() ? "open" : "closed");
    buf.append("),request=");
    buf.append(httpexchange.getRequestState());
    if (httpexchange.getRequest() != null) {
        buf.append("(");
        buf.append(httpexchange.getRequest().getRequestLine());
        buf.append(")");
    }
    buf.append(",response=");
    buf.append(httpexchange.getResponseState());
    if (httpexchange.getResponse() != null) {
        buf.append("(");
        buf.append(httpexchange.getResponse().getStatusLine());
        buf.append(")");
    }
    buf.append(",valid=");
    buf.append(httpexchange.isValid());
    buf.append("]");
    return buf.toString();
}