Example usage for org.apache.http.nio.util ContentOutputBuffer produceContent

List of usage examples for org.apache.http.nio.util ContentOutputBuffer produceContent

Introduction

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

Prototype

int produceContent(ContentEncoder encoder) throws IOException;

Source Link

Document

Writes content from this buffer to the given ContentEncoder .

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 v a 2 s.  c o m
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.synapse.transport.nhttp.ClientHandler.java

/**
 * Process ready output (i.e. write request to remote server)
 * /*from w  w w .  j  a v a 2  s.  co  m*/
 * @param conn the connection being processed
 * @param encoder the encoder in use
 */
public void outputReady(final NHttpClientConnection conn, final ContentEncoder encoder) {
    HttpContext context = conn.getContext();

    ContentOutputBuffer outBuf = (ContentOutputBuffer) context.getAttribute(REQUEST_SOURCE_BUFFER);
    if (outBuf == null)
        return;

    try {
        int bytesWritten = outBuf.produceContent(encoder);
        if (metrics != null) {
            if (bytesWritten > 0) {
                if (metrics.getLevel() == MetricsCollector.LEVEL_FULL) {
                    metrics.incrementBytesSent(getMessageContext(conn), bytesWritten);
                } else {
                    metrics.incrementBytesSent(bytesWritten);
                }
            }

            if (encoder.isCompleted()) {
                if (metrics.getLevel() == MetricsCollector.LEVEL_FULL) {
                    metrics.incrementMessagesSent(getMessageContext(conn));
                } else {
                    metrics.incrementMessagesSent();
                }
            }
        }

        if (encoder.isCompleted()) {
            ClientConnectionDebug ccd = (ClientConnectionDebug) context.getAttribute(CLIENT_CONNECTION_DEBUG);
            if (ccd != null) {
                ccd.recordRequestCompletionTime();
            }

            setServerContextAttribute(NhttpConstants.REQ_TO_BACKEND_WRITE_END_TIME, System.currentTimeMillis(),
                    conn);
        }

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

From source file:org.apache.synapse.transport.passthru.SourceHandler.java

public void outputReady(NHttpServerConnection conn, ContentEncoder encoder) {
    try {//from ww w .  j av  a2  s .  c o  m
        ProtocolState protocolState = SourceContext.getState(conn);

        //special case to handle WSDLs
        if (protocolState == ProtocolState.WSDL_RESPONSE_DONE) {
            // we need to shut down if the shutdown flag is set
            HttpContext context = conn.getContext();
            ContentOutputBuffer outBuf = (ContentOutputBuffer) context
                    .getAttribute("synapse.response-source-buffer");
            int bytesWritten = outBuf.produceContent(encoder);
            if (metrics != null && bytesWritten > 0) {
                metrics.incrementBytesSent(bytesWritten);
            }

            conn.requestInput();
            if (outBuf instanceof SimpleOutputBuffer && !((SimpleOutputBuffer) outBuf).hasData()) {
                sourceConfiguration.getSourceConnections().releaseConnection(conn);
            }
            endTransaction(conn);
            return;
        }

        if (protocolState != ProtocolState.RESPONSE_HEAD && protocolState != ProtocolState.RESPONSE_BODY) {
            log.warn("Illegal incoming connection state: " + protocolState + " . Possibly two send backs "
                    + "are happening for the same request");

            handleInvalidState(conn, "Trying to write response body");
            endTransaction(conn);
            return;
        }

        SourceContext.updateState(conn, ProtocolState.RESPONSE_BODY);

        SourceResponse response = SourceContext.getResponse(conn);

        int bytesSent = response.write(conn, encoder);

        if (encoder.isCompleted()) {
            HttpContext context = conn.getContext();
            long departure = System.currentTimeMillis();
            context.setAttribute(PassThroughConstants.RES_TO_CLIENT_WRITE_END_TIME, departure);
            context.setAttribute(PassThroughConstants.RES_DEPARTURE_TIME, departure);
            updateLatencyView(context);
        }
        endTransaction(conn);
        metrics.incrementBytesSent(bytesSent);
    } catch (IOException e) {
        logIOException(conn, e);

        informWriterError(conn);

        SourceContext.updateState(conn, ProtocolState.CLOSING);
        sourceConfiguration.getSourceConnections().shutDownConnection(conn, true);
    }
}

From source file:org.siddhiesb.transport.passthru.SourceHandler.java

public void outputReady(NHttpServerConnection conn, ContentEncoder encoder) {
    try {//from   w ww . j  a v a 2 s.com
        org.siddhiesb.transport.passthru.ProtocolState protocolState = org.siddhiesb.transport.passthru.SourceContext
                .getState(conn);

        //special case to handle WSDLs
        if (protocolState == org.siddhiesb.transport.passthru.ProtocolState.WSDL_RESPONSE_DONE) {
            // we need to shut down if the shutdown flag is set
            HttpContext context = conn.getContext();
            ContentOutputBuffer outBuf = (ContentOutputBuffer) context
                    .getAttribute("synapse.response-source-buffer");
            int bytesWritten = outBuf.produceContent(encoder);
            conn.requestInput();
            if (outBuf instanceof SimpleOutputBuffer && !((SimpleOutputBuffer) outBuf).hasData()) {
                sourceConfiguration.getSourceConnections().releaseConnection(conn);
            }

            return;
        }

        if (protocolState != org.siddhiesb.transport.passthru.ProtocolState.RESPONSE_HEAD
                && protocolState != org.siddhiesb.transport.passthru.ProtocolState.RESPONSE_BODY) {
            log.warn("Illegal incoming connection state: " + protocolState + " . Possibly two send backs "
                    + "are happening for the same request");

            handleInvalidState(conn, "Trying to write response body");
            return;
        }

        org.siddhiesb.transport.passthru.SourceContext.updateState(conn,
                org.siddhiesb.transport.passthru.ProtocolState.RESPONSE_BODY);

        SourceResponse response = org.siddhiesb.transport.passthru.SourceContext.getResponse(conn);

        int bytesSent = response.write(conn, encoder);

        if (encoder.isCompleted()) {
            HttpContext context = conn.getContext();
            if (context.getAttribute(PassThroughConstants.REQ_ARRIVAL_TIME) != null
                    && context.getAttribute(PassThroughConstants.REQ_DEPARTURE_TIME) != null
                    && context.getAttribute(PassThroughConstants.RES_HEADER_ARRIVAL_TIME) != null) {
            }

            context.removeAttribute(PassThroughConstants.REQ_ARRIVAL_TIME);
            context.removeAttribute(PassThroughConstants.REQ_DEPARTURE_TIME);
            context.removeAttribute(PassThroughConstants.RES_HEADER_ARRIVAL_TIME);
        }

    } catch (IOException e) {
        logIOException(conn, e);

        informWriterError(conn);

        org.siddhiesb.transport.passthru.SourceContext.updateState(conn,
                org.siddhiesb.transport.passthru.ProtocolState.CLOSING);
        sourceConfiguration.getSourceConnections().shutDownConnection(conn);
    }
}