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

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

Introduction

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

Prototype

void close() throws IOException;

Source Link

Usage

From source file:org.apache.synapse.transport.passthru.connections.SourceConnections.java

/**
 * Close a connection gracefully.//w ww .  j a  v  a2s .c  o  m
 *
 * @param conn the connection that needs to be closed.
 */
public void closeConnection(NHttpServerConnection conn) {
    if (log.isDebugEnabled()) {
        log.debug("Shutting down connection forcefully " + conn);
    }
    lock.lock();
    try {
        SourceContext.get(conn).reset();

        if (!busyConnections.remove(conn)) {
            freeConnections.remove(conn);
        }

        try {
            conn.close();
        } catch (IOException ignored) {
        }
    } finally {
        lock.unlock();
    }
}

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

public void endOfInput(NHttpServerConnection conn) throws IOException {
    conn.close();
}

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

public void endOfInput(final NHttpServerConnection conn) throws IOException {
    conn.close();
}

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

public void responseReady(NHttpServerConnection conn) {

    if (JavaUtils.isTrueExplicitly(conn.getContext().getAttribute(NhttpConstants.FORCE_CLOSING))
            && !JavaUtils.isTrueExplicitly(conn.getContext().getAttribute(NhttpConstants.MESSAGE_IN_FLIGHT))) {

        try {//ww w. ja va  2 s  .  c  o  m
            if (log.isDebugEnabled()) {
                log.debug(conn + ": Closing a persisted connection since it is forced : " + conn);
            }
            conn.close();
        } catch (IOException ignore) {
        }

        return;
    }

    metrics.notifyReceivedMessageSize(conn.getMetrics().getReceivedBytesCount());
    metrics.notifySentMessageSize(conn.getMetrics().getSentBytesCount());
    conn.getMetrics().reset();
    conn.getContext().removeAttribute(NhttpConstants.MESSAGE_IN_FLIGHT);

    if (log.isTraceEnabled()) {
        log.trace(conn + ": Ready to send response");
    }
}

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
 */// w  w w.j av a  2  s .c  om
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.axis2.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. j a  va2  s  . c om
public void outputReady(final NHttpServerConnection conn, final ContentEncoder encoder) {

    HttpContext context = conn.getContext();
    HttpResponse response = conn.getHttpResponse();
    Pipe.SourceChannel source = (Pipe.SourceChannel) context.getAttribute(RESPONSE_SOURCE_CHANNEL);
    ByteBuffer outbuf = (ByteBuffer) context.getAttribute(RESPONSE_BUFFER);

    try {
        int bytesRead = source.read(outbuf);
        if (bytesRead == -1) {
            encoder.complete();
        } else {
            outbuf.flip();
            encoder.write(outbuf);
            outbuf.compact();
        }

        if (encoder.isCompleted()) {
            source.close();
            if (!connStrategy.keepAlive(response, context)) {
                conn.close();
            }
        }

    } catch (IOException e) {
        handleException("I/O Error : " + e.getMessage(), e, conn);
    }
}

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

public void exception(NHttpServerConnection conn, Exception ex) {
    boolean isFault = false;
    if (ex instanceof IOException) {
        logIOException(conn, (IOException) ex);

        metrics.incrementFaultsReceiving();

        ProtocolState state = SourceContext.getState(conn);
        if (state == ProtocolState.REQUEST_BODY || state == ProtocolState.REQUEST_HEAD) {
            informReaderError(conn);//from   w w  w.  j  av a 2  s .c o  m
        } else if (state == ProtocolState.RESPONSE_BODY || state == ProtocolState.RESPONSE_HEAD) {
            informWriterError(conn);
        } else if (state == ProtocolState.REQUEST_DONE) {
            informWriterError(conn);
        } else if (state == ProtocolState.RESPONSE_DONE) {
            informWriterError(conn);
        }
        isFault = true;
        SourceContext.updateState(conn, ProtocolState.CLOSED);
        sourceConfiguration.getSourceConnections().shutDownConnection(conn, true);
    } else if (ex instanceof HttpException) {
        try {
            if (conn.isResponseSubmitted()) {
                sourceConfiguration.getSourceConnections().shutDownConnection(conn, true);
                return;
            }
            HttpContext httpContext = conn.getContext();

            HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_BAD_REQUEST,
                    "Bad request");
            response.setParams(
                    new DefaultedHttpParams(sourceConfiguration.getHttpParams(), response.getParams()));
            response.addHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_CLOSE);

            // Pre-process HTTP request
            httpContext.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
            httpContext.setAttribute(ExecutionContext.HTTP_REQUEST, null);
            httpContext.setAttribute(ExecutionContext.HTTP_RESPONSE, response);

            sourceConfiguration.getHttpProcessor().process(response, httpContext);

            conn.submitResponse(response);
            SourceContext.updateState(conn, ProtocolState.CLOSED);
            conn.close();
        } catch (Exception ex1) {
            log.error(ex.getMessage(), ex);
            SourceContext.updateState(conn, ProtocolState.CLOSED);
            sourceConfiguration.getSourceConnections().shutDownConnection(conn, true);
            isFault = true;
        }
    } else {
        log.error("Unexpected error: " + ex.getMessage(), ex);
        SourceContext.updateState(conn, ProtocolState.CLOSED);
        sourceConfiguration.getSourceConnections().shutDownConnection(conn, true);
        isFault = true;
    }

    if (isFault) {
        rollbackTransaction(conn);
    }
}