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.LoggingNHttpServiceHandler.java

public void requestReceived(final NHttpServerConnection conn) {
    HttpRequest request = conn.getHttpRequest();
    if (this.log.isDebugEnabled()) {
        this.log.debug("HTTP connection " + conn + ": " + request.getRequestLine());
    }/*  w  w  w  .  ja  v  a 2s. c  o m*/
    this.handler.requestReceived(conn);
    if (this.headerlog.isDebugEnabled()) {
        this.headerlog.debug(">> " + request.getRequestLine().toString());
        Header[] headers = request.getAllHeaders();
        for (int i = 0; i < headers.length; i++) {
            this.headerlog.debug(">> " + headers[i].toString());
        }
    }
}

From source file:org.siddhiesb.transport.http.conn.LoggingNHttpServiceHandler.java

public void requestReceived(final NHttpServerConnection conn) throws IOException, HttpException {
    HttpRequest request = conn.getHttpRequest();
    if (this.log.isDebugEnabled()) {
        this.log.debug(conn + ": " + request.getRequestLine());
    }// w  w  w  . j av  a 2  s . c  om
    this.handler.requestReceived(conn);
}

From source file:org.apache.synapse.transport.nhttp.debug.ServerConnectionDebug.java

public ServerConnectionDebug(NHttpServerConnection conn) {

    super();/*from   ww  w.ja v  a 2 s  .c o  m*/
    this.connectionCreationTime = (Long) conn.getContext().getAttribute(ServerHandler.CONNECTION_CREATION_TIME);
    this.requestStartTime = System.currentTimeMillis();

    // assume an entity body is not present. If present this would be overwritten
    this.requestCompletionTime = System.currentTimeMillis();

    RequestLine reqLine = conn.getHttpRequest().getRequestLine();
    this.requestURLPAth = reqLine.getUri();
    this.requestHTTPMethod = reqLine.getMethod();
    this.requestHTTPProtocol = reqLine.getProtocolVersion().toString();

    if (conn instanceof HttpInetConnection) {
        HttpInetConnection inetConn = (HttpInetConnection) conn;
        InetAddress remoteAddr = inetConn.getRemoteAddress();
        if (remoteAddr != null) {
            this.remoteClientIP = remoteAddr.getHostAddress();
        }
    }

    HttpRequest req = conn.getHttpRequest();
    this.headers = req.getAllHeaders();
}

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

/**
 * Handle  errors while reading or writing to underlying channels
 * @param conn the connection being processed
 * @param e the exception encountered/*from w ww. ja v a2  s.co  m*/
 */
public void exception(NHttpServerConnection conn, Exception e) {
    String errMsg = "I/O error : " + e.getMessage();
    if (e instanceof HttpException) {
        if (metrics != null) {
            metrics.incrementFaultsReceiving();
        }

        HttpContext context = conn.getContext();
        HttpRequest request = conn.getHttpRequest();
        ProtocolVersion ver = HttpVersion.HTTP_1_0;
        if (request != null && request.getRequestLine() != null) {
            ver = request.getRequestLine().getProtocolVersion();
        }
        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);
        try {
            commitResponseHideExceptions(conn, response);
        } catch (Exception ignore) {
        }
    } else if (e instanceof ConnectionClosedException
            || (e.getMessage() != null && (e.getMessage().contains("Connection reset by peer")
                    || e.getMessage().contains("forcibly closed")))) {
        if (log.isDebugEnabled()) {
            errMsg = "I/O error (Probably the keepalive connection " + "was closed):" + e.getMessage();
            log.debug(errMsg);
        }
        shutdownConnection(conn, true, errMsg);
    } else if (e instanceof IOException && e.getMessage() != null) {
        errMsg = e.getMessage().toLowerCase();
        if (errMsg.indexOf("broken") != -1) {
            log.warn("I/O error (Probably the connection " + "was closed by the remote party):"
                    + e.getMessage());
        } else {
            log.error("I/O error: " + e.getMessage(), e);
        }
        if (metrics != null) {
            metrics.incrementFaultsReceiving();
        }
        shutdownConnection(conn, true, errMsg);
    } else {
        errMsg = "Unexpected I/O error: " + e.getClass().getName();
        log.error(errMsg, e);
        if (metrics != null) {
            metrics.incrementFaultsReceiving();
        }
        shutdownConnection(conn, true, errMsg);
    }
}

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

/**
 * Create SourceRequest from NHttpServerConnection conn
 * @param conn the connection being processed
 * @return SourceRequest/*w ww  .j  a va 2s  .  co m*/
 * @throws IOException
 * @throws HttpException
 */
public SourceRequest getSourceRequest(NHttpServerConnection conn) throws IOException, HttpException {
    HttpContext context = conn.getContext();
    context.setAttribute(PassThroughConstants.REQ_ARRIVAL_TIME, System.currentTimeMillis());

    if (!SourceContext.assertState(conn, ProtocolState.REQUEST_READY)
            && !SourceContext.assertState(conn, ProtocolState.WSDL_RESPONSE_DONE)) {
        handleInvalidState(conn, "Request received");
        return null;
    }
    // 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
    SourceContext.updateState(conn, ProtocolState.REQUEST_HEAD);

    SourceRequest request = new SourceRequest(sourceConfiguration, conn.getHttpRequest(), conn);
    SourceContext.setRequest(conn, request);
    request.start(conn);
    metrics.incrementMessagesReceived();
    return request;
}

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

/**
 * Process a new incoming request/* www . j a  va  2  s  .  c  o m*/
 * @param conn the connection
 */
public void requestReceived(final NHttpServerConnection conn) {

    HttpContext context = conn.getContext();
    HttpRequest request = conn.getHttpRequest();
    context.setAttribute(HttpContext.HTTP_REQUEST, request);

    // allocate temporary buffers to process this request
    context.setAttribute(REQUEST_BUFFER, ByteBuffer.allocate(2048));
    context.setAttribute(RESPONSE_BUFFER, ByteBuffer.allocate(2048));

    try {
        Pipe requestPipe = Pipe.open(); // the pipe used to process the request
        Pipe responsePipe = Pipe.open(); // the pipe used to process the response
        context.setAttribute(REQUEST_SINK_CHANNEL, requestPipe.sink());
        context.setAttribute(RESPONSE_SOURCE_CHANNEL, responsePipe.source());

        // create the default response to this request
        HttpVersion httpVersion = request.getRequestLine().getHttpVersion();
        HttpResponse response = responseFactory.newHttpResponse(httpVersion, HttpStatus.SC_OK, context);
        response.setParams(this.params);

        // create a basic HttpEntity using the source channel of the response pipe
        BasicHttpEntity entity = new BasicHttpEntity();
        entity.setContent(Channels.newInputStream(responsePipe.source()));
        if (httpVersion.greaterEquals(HttpVersion.HTTP_1_1)) {
            entity.setChunked(true);
        }
        response.setEntity(entity);

        // hand off processing of the request to a thread off the pool
        workerPool.execute(
                new ServerWorker(cfgCtx, conn, this, request, Channels.newInputStream(requestPipe.source()),
                        response, Channels.newOutputStream(responsePipe.sink())));

    } catch (IOException e) {
        handleException("Error processing request received for : " + request.getRequestLine().getUri(), e,
                conn);
    }
}