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

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

Introduction

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

Prototype

public void setResponse(final HttpResponse response) 

Source Link

Usage

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 ww.  j a  v  a2s.  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);
    }
}