Search in sources :

Example 1 with SimpleOutputBuffer

use of org.apache.http.nio.util.SimpleOutputBuffer in project wso2-synapse by wso2.

the class SourceHandler method getOutputStream.

/**
 * Create synapse.response-source-buffer for GET and HEAD Http methods
 * @param method  Http Method
 * @param request Source Request
 * @return OutputStream
 */
public OutputStream getOutputStream(String method, SourceRequest request) {
    OutputStream os = null;
    if (HttpMethod.GET.equals(method) || HttpMethod.HEAD.equals(method)) {
        HttpContext context = request.getConnection().getContext();
        ContentOutputBuffer outputBuffer = new SimpleOutputBuffer(sourceConfiguration.getIOBufferSize(), new HeapByteBufferAllocator());
        context.setAttribute("synapse.response-source-buffer", outputBuffer);
        os = new ContentOutputStream(outputBuffer);
    }
    return os;
}
Also used : SimpleOutputBuffer(org.apache.http.nio.util.SimpleOutputBuffer) HeapByteBufferAllocator(org.apache.http.nio.util.HeapByteBufferAllocator) OutputStream(java.io.OutputStream) ContentOutputStream(org.apache.http.nio.entity.ContentOutputStream) HttpContext(org.apache.http.protocol.HttpContext) ContentOutputStream(org.apache.http.nio.entity.ContentOutputStream) ContentOutputBuffer(org.apache.http.nio.util.ContentOutputBuffer)

Example 2 with SimpleOutputBuffer

use of org.apache.http.nio.util.SimpleOutputBuffer in project wso2-synapse by wso2.

the class SourceHandler method outputReady.

public void outputReady(NHttpServerConnection conn, ContentEncoder encoder) {
    try {
        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);
    }
}
Also used : SimpleOutputBuffer(org.apache.http.nio.util.SimpleOutputBuffer) HttpContext(org.apache.http.protocol.HttpContext) IOException(java.io.IOException) ContentOutputBuffer(org.apache.http.nio.util.ContentOutputBuffer)

Example 3 with SimpleOutputBuffer

use of org.apache.http.nio.util.SimpleOutputBuffer in project wso2-synapse by wso2.

the class PassThroughNHttpGetProcessor method process.

public void process(HttpRequest request, HttpResponse response, MessageContext msgContext, NHttpServerConnection conn, OutputStream ostream, boolean isRestDispatching) {
    String uri = request.getRequestLine().getUri();
    String serviceName = getServiceName(request);
    Map<String, String> parameters = new HashMap<String, String>();
    int pos = uri.indexOf("?");
    if (pos != -1) {
        msgContext.setTo(new EndpointReference(uri.substring(0, pos)));
        StringTokenizer st = new StringTokenizer(uri.substring(pos + 1), "&");
        while (st.hasMoreTokens()) {
            String param = st.nextToken();
            pos = param.indexOf("=");
            if (pos != -1) {
                parameters.put(param.substring(0, pos), param.substring(pos + 1));
            } else {
                parameters.put(param, null);
            }
        }
    } else {
        msgContext.setTo(new EndpointReference(uri));
    }
    SimpleOutputBuffer outputBuffer = (SimpleOutputBuffer) conn.getContext().getAttribute(PASS_THROUGH_RESPONSE_SOURCE_BUFFER);
    ContentOutputStream os = new ContentOutputStream(outputBuffer);
    if (isServiceListBlocked(uri)) {
        sendResponseAndFinish(response, HttpStatus.SC_FORBIDDEN, conn, os, msgContext);
    } else if (uri.equals("/favicon.ico")) {
        response.addHeader(LOCATION, "http://ws.apache.org/favicon.ico");
        sendResponseAndFinish(response, HttpStatus.SC_MOVED_PERMANENTLY, conn, os, msgContext);
    } else if (serviceName != null && parameters.containsKey("wsdl")) {
        generateWsdl(response, msgContext, conn, os, serviceName, parameters);
    } else if (serviceName != null && parameters.containsKey("wsdl2")) {
        generateWsdl2(response, msgContext, conn, os, serviceName);
    } else if (serviceName != null && parameters.containsKey("xsd")) {
        generateXsd(response, msgContext, conn, os, serviceName, parameters);
    } else {
        msgContext.setProperty(PassThroughConstants.REST_GET_DELETE_INVOKE, true);
    }
}
Also used : StringTokenizer(java.util.StringTokenizer) SimpleOutputBuffer(org.apache.http.nio.util.SimpleOutputBuffer) HashMap(java.util.HashMap) ContentOutputStream(org.apache.http.nio.entity.ContentOutputStream) EndpointReference(org.apache.axis2.addressing.EndpointReference)

Aggregations

SimpleOutputBuffer (org.apache.http.nio.util.SimpleOutputBuffer)3 ContentOutputStream (org.apache.http.nio.entity.ContentOutputStream)2 ContentOutputBuffer (org.apache.http.nio.util.ContentOutputBuffer)2 HttpContext (org.apache.http.protocol.HttpContext)2 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 HashMap (java.util.HashMap)1 StringTokenizer (java.util.StringTokenizer)1 EndpointReference (org.apache.axis2.addressing.EndpointReference)1 HeapByteBufferAllocator (org.apache.http.nio.util.HeapByteBufferAllocator)1