Example usage for org.apache.http.nio NHttpServerConnection getHttpResponse

List of usage examples for org.apache.http.nio NHttpServerConnection getHttpResponse

Introduction

In this page you can find the example usage for org.apache.http.nio NHttpServerConnection getHttpResponse.

Prototype

HttpResponse getHttpResponse();

Source Link

Document

Returns the current HTTP response if one is being received / transmitted.

Usage

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

/**
 * Process ready output by writing into the channel
 * @param conn the connection being processed
 * @param encoder the content encoder in use
 *///from  w  w w .  ja va  2s  .  com
public void outputReady(final NHttpServerConnection conn, final ContentEncoder encoder) {

    HttpContext context = conn.getContext();
    HttpResponse response = conn.getHttpResponse();
    ContentOutputBuffer outBuf = (ContentOutputBuffer) context.getAttribute(RESPONSE_SOURCE_BUFFER);

    if (outBuf == null) {
        // fix for SYNAPSE 584. This is a temporaly fix becuase of HTTPCORE-208
        shutdownConnection(conn, false, null);
        return;
    }

    try {
        int bytesWritten = outBuf.produceContent(encoder);
        if (metrics != null && bytesWritten > 0) {
            metrics.incrementBytesSent(bytesWritten);
        }

        if (encoder.isCompleted()) {
            long currentTime = System.currentTimeMillis();
            context.setAttribute(NhttpConstants.RES_TO_CLIENT_WRITE_END_TIME, currentTime);
            context.setAttribute(NhttpConstants.RES_DEPARTURE_TIME, currentTime);
            updateLatencyView(context);

            context.removeAttribute(NhttpConstants.REQ_ARRIVAL_TIME);
            context.removeAttribute(NhttpConstants.REQ_DEPARTURE_TIME);
            context.removeAttribute(NhttpConstants.RES_ARRIVAL_TIME);

            ((ServerConnectionDebug) conn.getContext().getAttribute(SERVER_CONNECTION_DEBUG))
                    .recordResponseCompletionTime();

            Boolean reqRead = (Boolean) conn.getContext().getAttribute(NhttpConstants.REQUEST_READ);
            Boolean forceConnectionClose = (Boolean) conn.getContext()
                    .getAttribute(NhttpConstants.FORCE_CONNECTION_CLOSE);
            if (reqRead != null && !reqRead) {
                try {
                    // this is a connection we should not re-use
                    conn.close();
                } catch (Exception ignore) {
                }
            } else if (!connStrategy.keepAlive(response, context)) {
                conn.close();
            } else if (forceConnectionClose != null && forceConnectionClose) {
                conn.close();
            } else {
                conn.requestInput();
            }
        }

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

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

/**
 * Process ready output by writing into the channel
 * @param conn the connection being processed
 * @param encoder the content encoder in use
 *//*from   w ww. jav a  2s.com*/
public void outputReady(final NHttpServerConnection conn, final ContentEncoder encoder) {

    HttpContext context = conn.getContext();
    HttpResponse response = conn.getHttpResponse();
    Pipe.SourceChannel source = (Pipe.SourceChannel) context.getAttribute(RESPONSE_SOURCE_CHANNEL);
    ByteBuffer outbuf = (ByteBuffer) context.getAttribute(RESPONSE_BUFFER);

    try {
        int bytesRead = source.read(outbuf);
        if (bytesRead == -1) {
            encoder.complete();
        } else {
            outbuf.flip();
            encoder.write(outbuf);
            outbuf.compact();
        }

        if (encoder.isCompleted()) {
            source.close();
            if (!connStrategy.keepAlive(response, context)) {
                conn.close();
            }
        }

    } catch (IOException e) {
        handleException("I/O Error : " + e.getMessage(), e, conn);
    }
}