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

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

Introduction

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

Prototype

HttpRequest getHttpRequest();

Source Link

Document

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

Usage

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

/**
 * Handle HTTP Protocol violations with an error response
 * @param conn the connection being processed
 * @param e the exception encountered// w ww.ja va 2  s  .  c  om
 */
public void exception(final NHttpServerConnection conn, final HttpException e) {
    HttpContext context = conn.getContext();
    HttpRequest request = conn.getHttpRequest();
    HttpVersion ver = request.getRequestLine().getHttpVersion();
    HttpResponse response = responseFactory.newHttpResponse(ver, HttpStatus.SC_BAD_REQUEST, context);
    byte[] msg = EncodingUtils.getAsciiBytes("Malformed HTTP request: " + e.getMessage());
    ByteArrayEntity entity = new ByteArrayEntity(msg);
    entity.setContentType("text/plain; charset=US-ASCII");
    response.setEntity(entity);
    commitResponse(conn, response);
}

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

/**
 * Process a new incoming request/*  w  w w. j a v a2s .c om*/
 * @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");

    // 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 SharedOutputBuffer(cfg.getBufferSize(), conn, allocator);
        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);
    }
}

From source file:org.apache.synapse.transport.utils.logging.LoggingServerEventHandler.java

public void requestReceived(final NHttpServerConnection conn) throws IOException, HttpException {
    HttpRequest request = conn.getHttpRequest();
    if (this.log.isDebugEnabled()) {
        this.log.debug("HTTP InRequest Received on connection " + conn + ": " + request.getRequestLine());
    }/*from w  w  w. j  a v  a  2  s .c  o  m*/
    this.handler.requestReceived(conn);
}

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

public void requestReceived(NHttpServerConnection conn) {
    try {/*from   w w  w  .ja  v a 2  s .c  o m*/

        //System.out.println("============ SourceHandler : requestReceived ===============");

        HttpContext _context = conn.getContext();
        _context.setAttribute(PassThroughConstants.REQ_ARRIVAL_TIME, System.currentTimeMillis());

        if (!org.siddhiesb.transport.passthru.SourceContext.assertState(conn,
                org.siddhiesb.transport.passthru.ProtocolState.REQUEST_READY)
                && !org.siddhiesb.transport.passthru.SourceContext.assertState(conn,
                        org.siddhiesb.transport.passthru.ProtocolState.WSDL_RESPONSE_DONE)) {
            handleInvalidState(conn, "Request received");
            return;
        }
        // we have received a message over this connection. So we must inform the pool
        sourceConfiguration.getSourceConnections().useConnection(conn);

        // at this point we have read the HTTP Headers
        org.siddhiesb.transport.passthru.SourceContext.updateState(conn,
                org.siddhiesb.transport.passthru.ProtocolState.REQUEST_HEAD);

        SourceRequest request = new SourceRequest(sourceConfiguration, conn.getHttpRequest(), conn);

        org.siddhiesb.transport.passthru.SourceContext.setRequest(conn, request);

        request.start(conn);

        String method = request.getRequest() != null
                ? request.getRequest().getRequestLine().getMethod().toUpperCase()
                : "";
        OutputStream os = null;
        if ("GET".equals(method) || "HEAD".equals(method)) {
            HttpContext context = request.getConnection().getContext();
            ContentOutputBuffer outputBuffer = new SimpleOutputBuffer(8192, new HeapByteBufferAllocator());
            context.setAttribute("synapse.response-source-buffer", outputBuffer);
            os = new ContentOutputStream(outputBuffer);
        }

        sourceConfiguration.getWorkerPool().execute(new org.siddhiesb.transport.passthru.ServerWorker(request,
                sourceConfiguration, os, mediationEngine));
    } catch (HttpException e) {
        log.error(e.getMessage(), e);

        informReaderError(conn);

        org.siddhiesb.transport.passthru.SourceContext.updateState(conn,
                org.siddhiesb.transport.passthru.ProtocolState.CLOSED);
        sourceConfiguration.getSourceConnections().shutDownConnection(conn);
    } catch (IOException e) {
        logIOException(conn, e);

        informReaderError(conn);

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