Example usage for org.apache.http.nio.util ContentInputBuffer consumeContent

List of usage examples for org.apache.http.nio.util ContentInputBuffer consumeContent

Introduction

In this page you can find the example usage for org.apache.http.nio.util ContentInputBuffer consumeContent.

Prototype

int consumeContent(ContentDecoder decoder) throws IOException;

Source Link

Document

Reads content from the given ContentDecoder and stores it in this buffer.

Usage

From source file:org.apache.synapse.transport.nhttp.ClientHandler.java

/**
 * Process ready input (i.e. response from remote server)
 * //from   w  ww  .  j  av a2s  . c  o m
 * @param conn connection being processed
 * @param decoder the content decoder in use
 */
public void inputReady(final NHttpClientConnection conn, final ContentDecoder decoder) {
    HttpContext context = conn.getContext();
    HttpResponse response = conn.getHttpResponse();
    ContentInputBuffer inBuf = (ContentInputBuffer) context.getAttribute(RESPONSE_SINK_BUFFER);

    try {
        int bytesRead = inBuf.consumeContent(decoder);
        if (metrics != null && bytesRead > 0) {
            if (metrics.getLevel() == MetricsCollector.LEVEL_FULL) {
                metrics.incrementBytesReceived(getMessageContext(conn), bytesRead);
            } else {
                metrics.incrementBytesReceived(bytesRead);
            }
        }

        if (decoder.isCompleted()) {
            setServerContextAttribute(NhttpConstants.RES_ARRIVAL_TIME, System.currentTimeMillis(), conn);
            setServerContextAttribute(NhttpConstants.RES_FROM_BACKEND_READ_END_TIME, System.currentTimeMillis(),
                    conn);
            ClientConnectionDebug ccd = (ClientConnectionDebug) conn.getContext()
                    .getAttribute(CLIENT_CONNECTION_DEBUG);
            if (ccd != null) {
                ccd.recordResponseCompletionTime();
            }

            if (metrics != null) {
                if (metrics.getLevel() == MetricsCollector.LEVEL_FULL) {
                    MessageContext mc = getMessageContext(conn);
                    metrics.incrementMessagesReceived(mc);
                    metrics.notifyReceivedMessageSize(mc, conn.getMetrics().getReceivedBytesCount());
                    metrics.notifySentMessageSize(mc, conn.getMetrics().getSentBytesCount());
                    metrics.reportResponseCode(mc, response.getStatusLine().getStatusCode());
                } else {
                    metrics.incrementMessagesReceived();
                    metrics.notifyReceivedMessageSize(conn.getMetrics().getReceivedBytesCount());
                    metrics.notifySentMessageSize(conn.getMetrics().getSentBytesCount());
                }
            }
            // reset metrics on connection
            conn.getMetrics().reset();
            if (context.getAttribute(NhttpConstants.DISCARD_ON_COMPLETE) != null) {
                try {
                    // this is a connection we should not re-use
                    connpool.forget(conn);
                    shutdownConnection(conn, false, null);
                    context.removeAttribute(RESPONSE_SINK_BUFFER);
                    context.removeAttribute(REQUEST_SOURCE_BUFFER);
                } catch (Exception ignore) {
                }
            } else if (!connStrategy.keepAlive(response, context)) {
                shutdownConnection(conn, false, null);
                context.removeAttribute(RESPONSE_SINK_BUFFER);
                context.removeAttribute(REQUEST_SOURCE_BUFFER);
            } else {
                connpool.release(conn);
            }
        }

    } catch (IOException e) {
        if (metrics != null) {
            if (metrics.getLevel() == MetricsCollector.LEVEL_FULL) {
                metrics.incrementFaultsReceiving(NhttpConstants.SND_IO_ERROR_RECEIVING,
                        getMessageContext(conn));
            } else {
                metrics.incrementFaultsReceiving();
            }
        }
        handleException("I/O Error at inputReady : " + e.getMessage(), e, conn);
    }
}

From source file:org.apache.synapse.transport.nhttp.ServerHandler.java

/**
 * Process ready input by writing it into the Pipe
 * @param conn the connection being processed
 * @param decoder the content decoder in use
 *///www  .ja  v  a  2s. c o m
public void inputReady(final NHttpServerConnection conn, final ContentDecoder decoder) {

    HttpContext context = conn.getContext();
    ContentInputBuffer inBuf = (ContentInputBuffer) context.getAttribute(REQUEST_SINK_BUFFER);

    try {
        int bytesRead = inBuf.consumeContent(decoder);
        if (metrics != null && bytesRead > 0) {
            metrics.incrementBytesReceived(bytesRead);
        }

        if (decoder.isCompleted()) {

            ((ServerConnectionDebug) conn.getContext().getAttribute(SERVER_CONNECTION_DEBUG))
                    .recordRequestCompletionTime();
            // remove the request we have fully read, to detect harmless keepalive timeouts from
            // real timeouts while reading requests
            context.setAttribute(NhttpConstants.REQUEST_READ, Boolean.TRUE);
            context.setAttribute(NhttpConstants.REQ_FROM_CLIENT_READ_END_TIME, System.currentTimeMillis());
        }

    } catch (IOException e) {
        if (metrics != null) {
            metrics.incrementFaultsReceiving();
        }
        handleException("I/O Error at inputReady : " + e.getMessage(), e, conn);
    }
}