Search in sources :

Example 1 with ContentInputStream

use of org.apache.http.nio.entity.ContentInputStream in project kontraktor by RuedigerMoeller.

the class AsyncHttpActor method readContentBytes.

public static byte[] readContentBytes(HttpResponse resp, String enc) throws IOException {
    ContentBufferEntity entity = (ContentBufferEntity) resp.getEntity();
    ContentInputStream in = (ContentInputStream) entity.getContent();
    byte[] barr = new byte[in.available()];
    int read = in.read(barr);
    if ("gzip".equals(enc)) {
        barr = unGZip(barr, read);
    }
    in.close();
    return barr;
}
Also used : ContentInputStream(org.apache.http.nio.entity.ContentInputStream) ContentBufferEntity(org.apache.http.nio.entity.ContentBufferEntity)

Example 2 with ContentInputStream

use of org.apache.http.nio.entity.ContentInputStream in project wso2-synapse by wso2.

the class ServerHandler method requestReceived.

/**
 * Process a new incoming request
 * @param conn the connection
 */
public void requestReceived(final NHttpServerConnection conn) {
    HttpContext context = conn.getContext();
    context.setAttribute(NhttpConstants.REQ_ARRIVAL_TIME, System.currentTimeMillis());
    context.setAttribute(NhttpConstants.REQ_FROM_CLIENT_READ_START_TIME, System.currentTimeMillis());
    HttpRequest request = conn.getHttpRequest();
    context.setAttribute(ExecutionContext.HTTP_REQUEST, request);
    context.setAttribute(NhttpConstants.MESSAGE_IN_FLIGHT, "true");
    if (isMessageSizeValidationEnabled) {
        context.setAttribute(NhttpConstants.MESSAGE_SIZE_VALIDATION_SUM, 0);
    }
    // prepare to collect debug information
    conn.getContext().setAttribute(ServerHandler.SERVER_CONNECTION_DEBUG, new ServerConnectionDebug(conn));
    NHttpConfiguration cfg = NHttpConfiguration.getInstance();
    try {
        InputStream is;
        // Only create an input buffer and ContentInputStream if the request has content
        if (request instanceof HttpEntityEnclosingRequest) {
            // Mark request as not yet fully read, to detect timeouts from harmless keepalive deaths
            conn.getContext().setAttribute(NhttpConstants.REQUEST_READ, Boolean.FALSE);
            ContentInputBuffer inputBuffer = new SharedInputBuffer(cfg.getBufferSize(), conn, allocator);
            context.setAttribute(REQUEST_SINK_BUFFER, inputBuffer);
            is = new ContentInputStream(inputBuffer);
        } else {
            is = null;
            conn.getContext().removeAttribute(NhttpConstants.REQUEST_READ);
        }
        ContentOutputBuffer outputBuffer = new NhttpSharedOutputBuffer(bufferSize, conn, allocator, socketTimeout);
        context.setAttribute(RESPONSE_SOURCE_BUFFER, outputBuffer);
        OutputStream os = new ContentOutputStream(outputBuffer);
        // create the default response to this request
        ProtocolVersion httpVersion = request.getRequestLine().getProtocolVersion();
        HttpResponse response = responseFactory.newHttpResponse(httpVersion, HttpStatus.SC_OK, context);
        // create a basic HttpEntity using the source channel of the response pipe
        BasicHttpEntity entity = new BasicHttpEntity();
        if (httpVersion.greaterEquals(HttpVersion.HTTP_1_1)) {
            entity.setChunked(true);
        }
        response.setEntity(entity);
        if (metrics != null) {
            metrics.incrementMessagesReceived();
        }
        // hand off processing of the request to a thread off the pool
        ServerWorker worker = new ServerWorker(cfgCtx, scheme.getName(), metrics, conn, this, request, is, response, os, listenerContext.isRestDispatching(), listenerContext.getHttpGetRequestProcessor());
        if (workerPool != null) {
            workerPool.execute(worker);
        } else if (executor != null) {
            Map<String, String> headers = new HashMap<String, String>();
            for (Header header : request.getAllHeaders()) {
                headers.put(header.getName(), header.getValue());
            }
            EvaluatorContext evaluatorContext = new EvaluatorContext(request.getRequestLine().getUri(), headers);
            int priority = parser.parse(evaluatorContext);
            executor.execute(worker, priority);
        }
        // See if the client expects a 100-Continue
        Header expect = request.getFirstHeader(HTTP.EXPECT_DIRECTIVE);
        if (expect != null && HTTP.EXPECT_CONTINUE.equalsIgnoreCase(expect.getValue())) {
            HttpResponse ack = new BasicHttpResponse(request.getProtocolVersion(), HttpStatus.SC_CONTINUE, "Continue");
            conn.submitResponse(ack);
            if (log.isDebugEnabled()) {
                log.debug(conn + ": Expect :100 Continue hit, sending ack back to the server");
            }
            return;
        }
    } catch (Exception e) {
        if (metrics != null) {
            metrics.incrementFaultsReceiving();
        }
        handleException("Error processing request received for : " + request.getRequestLine().getUri(), e, conn);
    }
}
Also used : ServerConnectionDebug(org.apache.synapse.transport.nhttp.debug.ServerConnectionDebug) EvaluatorContext(org.apache.synapse.commons.evaluators.EvaluatorContext) ContentInputStream(org.apache.http.nio.entity.ContentInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ContentOutputStream(org.apache.http.nio.entity.ContentOutputStream) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) BasicHttpEntity(org.apache.http.entity.BasicHttpEntity) ContentOutputStream(org.apache.http.nio.entity.ContentOutputStream) IOException(java.io.IOException) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) ContentInputStream(org.apache.http.nio.entity.ContentInputStream) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with ContentInputStream

use of org.apache.http.nio.entity.ContentInputStream in project wso2-synapse by wso2.

the class ClientHandler method processResponse.

/**
 * Perform processing of the received response though Axis2
 *
 * @param conn HTTP connection to be processed
 * @param context HTTP context associated with the connection
 * @param response HTTP response associated with the connection
 */
private void processResponse(final NHttpClientConnection conn, HttpContext context, HttpResponse response) {
    ContentInputBuffer inputBuffer = null;
    MessageContext outMsgContext = (MessageContext) context.getAttribute(OUTGOING_MESSAGE_CONTEXT);
    String endptPrefix = (String) context.getAttribute(NhttpConstants.ENDPOINT_PREFIX);
    String requestMethod = (String) context.getAttribute(NhttpConstants.HTTP_REQ_METHOD);
    int statusCode = response.getStatusLine().getStatusCode();
    boolean expectEntityBody = false;
    if (!"HEAD".equals(requestMethod) && !"OPTIONS".equals(requestMethod) && statusCode >= HttpStatus.SC_OK && statusCode != HttpStatus.SC_NO_CONTENT && statusCode != HttpStatus.SC_NOT_MODIFIED && statusCode != HttpStatus.SC_RESET_CONTENT) {
        expectEntityBody = true;
    } else if (NhttpConstants.HTTP_HEAD.equals(requestMethod)) {
        // When invoking http HEAD request esb set content length as 0 to response header. Since there is no message
        // body content length cannot be calculated inside synapse. Hence additional two headers are added to
        // which contains content length of the backend response and the request method. These headers are removed
        // before submitting the actual response.
        response.addHeader(NhttpConstants.HTTP_REQUEST_METHOD, requestMethod);
        if (response.getFirstHeader(HTTP.CONTENT_LEN) != null) {
            response.addHeader(NhttpConstants.ORIGINAL_CONTENT_LEN, response.getFirstHeader(HTTP.CONTENT_LEN).getValue());
        }
    }
    if (expectEntityBody) {
        inputBuffer = new SharedInputBuffer(cfg.getBufferSize(), conn, allocator);
        context.setAttribute(RESPONSE_SINK_BUFFER, inputBuffer);
        BasicHttpEntity entity = new BasicHttpEntity();
        if (response.getStatusLine().getProtocolVersion().greaterEquals(HttpVersion.HTTP_1_1)) {
            entity.setChunked(true);
        }
        response.setEntity(entity);
        context.setAttribute(ExecutionContext.HTTP_RESPONSE, response);
    } else {
        conn.resetInput();
        conn.resetOutput();
        if (context.getAttribute(NhttpConstants.DISCARD_ON_COMPLETE) != null || !connStrategy.keepAlive(response, context)) {
            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 {
            connpool.release(conn);
        }
    }
    workerPool.execute(new ClientWorker(cfgCtx, inputBuffer == null ? null : new ContentInputStream(inputBuffer), response, outMsgContext, endptPrefix));
}
Also used : SharedInputBuffer(org.apache.http.nio.util.SharedInputBuffer) ContentInputStream(org.apache.http.nio.entity.ContentInputStream) BasicHttpEntity(org.apache.http.entity.BasicHttpEntity) MessageContext(org.apache.axis2.context.MessageContext) URISyntaxException(java.net.URISyntaxException) HttpException(org.apache.http.HttpException) IOException(java.io.IOException) ConnectionClosedException(org.apache.http.ConnectionClosedException) ContentInputBuffer(org.apache.http.nio.util.ContentInputBuffer)

Aggregations

ContentInputStream (org.apache.http.nio.entity.ContentInputStream)3 IOException (java.io.IOException)2 BasicHttpEntity (org.apache.http.entity.BasicHttpEntity)2 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 URISyntaxException (java.net.URISyntaxException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 MessageContext (org.apache.axis2.context.MessageContext)1 ConnectionClosedException (org.apache.http.ConnectionClosedException)1 HttpException (org.apache.http.HttpException)1 BasicHttpResponse (org.apache.http.message.BasicHttpResponse)1 ContentBufferEntity (org.apache.http.nio.entity.ContentBufferEntity)1 ContentOutputStream (org.apache.http.nio.entity.ContentOutputStream)1 ContentInputBuffer (org.apache.http.nio.util.ContentInputBuffer)1 SharedInputBuffer (org.apache.http.nio.util.SharedInputBuffer)1 EvaluatorContext (org.apache.synapse.commons.evaluators.EvaluatorContext)1 ServerConnectionDebug (org.apache.synapse.transport.nhttp.debug.ServerConnectionDebug)1