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

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

Introduction

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

Prototype

void shutdown() throws IOException;

Source Link

Usage

From source file:org.siddhiesb.transport.passthru.connections.SourceConnections.java

/**
 * Shutdown a connection//from ww w. jav a 2  s  .co  m
 *
 * @param conn the connection that needs to be shut down
 */
public void shutDownConnection(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.shutdown();
        } catch (IOException ignored) {
        }
    } finally {
        lock.unlock();
    }
}

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

/**
 * Shutdown a connection/*w  ww.  j  a  v  a 2  s.  c o  m*/
 *
 * @param conn the connection that needs to be shut down
 * @param isError whether an error is causing this shutdown of the connection
 *                It is very important to set this flag correctly.
 *                When an error causing the shutdown of the connections we should not
 *                release associated writer buffer to the pool as it might lead into
 *                situations like same buffer is getting released to both source and target
 *                buffer factories
 */
public void shutDownConnection(NHttpServerConnection conn, boolean isError) {
    if (log.isDebugEnabled()) {
        log.debug("Shutting down connection forcefully " + conn);
    }
    lock.lock();
    try {
        SourceContext.get(conn).reset(isError);

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

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

From source file:org.apache.synapse.transport.passthru.api.PassThroughNHttpGetProcessor.java

/**
  * Handles browser exception.// w w  w.j  a  va 2  s.  co m
  *
  * @param response HttpResponse
  * @param conn     NHttpServerConnection
  * @param os       OutputStream
  * @param msg      message
  * @param e        Exception
  */
protected void handleBrowserException(HttpResponse response, MessageContext msgContext,
        NHttpServerConnection conn, OutputStream os, String msg, Exception e) {
    if (e == null) {
        log.error(msg);
    } else {
        log.error(msg, e);
    }

    if (!response.containsHeader(HTTP.TRANSFER_ENCODING)) {
        response.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
        response.setReasonPhrase(msg);
        response.addHeader(CONTENT_TYPE, TEXT_HTML);
        sourceHandler.commitResponseHideExceptions(conn, response);
        try {
            write(conn, os, msg.getBytes());
            os.close();
        } catch (IOException ignore) {
        }
    }

    if (conn != null) {
        try {
            conn.shutdown();
        } catch (IOException ignore) {
        }
    }
    msgContext.setProperty(GET_REQUEST_HANDLED, Boolean.TRUE);
}

From source file:org.wso2.carbon.mediation.transport.handlers.PassThroughNHttpGetProcessor.java

/**
 * Handles browser exception.//from w w w . j ava  2  s .c o m
 *
 * @param response HttpResponse
 * @param conn     NHttpServerConnection
 * @param os       OutputStream
 * @param msg      message
 * @param e        Exception
 */
protected void handleBrowserException(HttpResponse response, NHttpServerConnection conn, OutputStream os,
        String msg, Exception e) {
    if (e == null) {
        log.error(msg);
    } else {
        log.error(msg, e);
    }

    if (!response.containsHeader(HTTP.TRANSFER_ENCODING)) {
        response.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
        response.setReasonPhrase(msg);
        //response.addHeader(CONTENT_TYPE, TEXT_HTML);
        //serverHandler.commitResponseHideExceptions(conn, response);
        try {
            os.write(msg.getBytes());
            os.close();
        } catch (IOException ignore) {
        }
    }

    if (conn != null) {
        try {
            conn.shutdown();
        } catch (IOException ignore) {
        }
    }
}

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

/**
 * Handles browser exception./*  www.  j av a2s .  c  om*/
 *
 * @param response HttpResponse
 * @param conn     NHttpServerConnection
 * @param os       OutputStream
 * @param msg      message
 * @param e        Exception
 */
protected void handleBrowserException(HttpResponse response, NHttpServerConnection conn, OutputStream os,
        String msg, Exception e) {
    if (e == null) {
        log.error(msg);
    } else {
        log.error(msg, e);
    }

    if (!response.containsHeader(HTTP.TRANSFER_ENCODING)) {
        response.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
        response.setReasonPhrase(msg);
        response.addHeader(CONTENT_TYPE, TEXT_HTML);
        serverHandler.commitResponseHideExceptions(conn, response);
        try {
            os.write(msg.getBytes());
            os.close();
        } catch (IOException ignore) {
        }
    }

    if (conn != null) {
        try {
            conn.shutdown();
        } catch (IOException ignore) {
        }
    }
}

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

/**
 * Handles exception./* www. j  a  va2 s  . c  om*/
 *
 * @param response   HttpResponse
 * @param msgContext MessageContext
 * @param conn       NHttpServerConnection
 * @param os         OutputStream
 * @param msg        message
 * @param e          Exception
 */
protected void handleException(HttpResponse response, MessageContext msgContext, NHttpServerConnection conn,
        OutputStream os, String msg, Exception e) {
    if (e == null) {
        log.error(msg);
    } else {
        log.error(msg, e);
    }

    if (e == null) {
        e = new Exception(msg);
    }

    try {
        MessageContext faultContext = MessageContextBuilder.createFaultMessageContext(msgContext, e);
        AxisEngine.sendFault(faultContext);

    } catch (Exception ex) {
        response.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
        response.addHeader(CONTENT_TYPE, TEXT_XML);
        serverHandler.commitResponseHideExceptions(conn, response);

        try {
            os.write(msg.getBytes());
            if (ex != null) {
                os.write(ex.getMessage().getBytes());
            }
        } catch (IOException ignore) {
        }

        if (conn != null) {
            try {
                conn.shutdown();
            } catch (IOException ignore) {
            }
        }
    }
}

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

/**
 * Shutdown the connection ignoring any IO errors during the process
 * @param conn the connection to be shutdown
 * @param isError whether shutdown is due to an error
 * @param errorMsg error message if shutdown happens on error
 *//* ww  w.j a  va  2 s  .  co  m*/
private void shutdownConnection(final NHttpServerConnection conn, boolean isError, String errorMsg) {
    SharedOutputBuffer outputBuffer = (SharedOutputBuffer) conn.getContext()
            .getAttribute(RESPONSE_SOURCE_BUFFER);
    if (outputBuffer != null) {
        outputBuffer.close();
    }
    SharedInputBuffer inputBuffer = (SharedInputBuffer) conn.getContext().getAttribute(REQUEST_SINK_BUFFER);
    if (inputBuffer != null) {
        inputBuffer.close();
    }

    if (log.isWarnEnabled() && (isError || log.isDebugEnabled()) && conn instanceof HttpInetConnection) {

        HttpInetConnection inetConnection = (HttpInetConnection) conn;
        InetAddress remoteAddress = inetConnection.getRemoteAddress();
        int remotePort = inetConnection.getRemotePort();

        String msg;
        if (remotePort != -1 && remoteAddress != null) { // If connection is still alive
            msg = "Connection from remote address : " + remoteAddress + ":" + remotePort
                    + " to local address : " + inetConnection.getLocalAddress() + ":"
                    + inetConnection.getLocalPort() + " is closed!"
                    + (errorMsg != null ? " - On error : " + errorMsg : "");

        } else { // if connection is already closed. obtain params from http context
            HttpContext httpContext = conn.getContext();
            msg = "Connection from remote address : "
                    + httpContext.getAttribute(NhttpConstants.CLIENT_REMOTE_ADDR) + ":"
                    + httpContext.getAttribute(NhttpConstants.CLIENT_REMOTE_PORT) + " to local address : "
                    + inetConnection.getLocalAddress() + ":" + inetConnection.getLocalPort() + " is closed!"
                    + (errorMsg != null ? " - On error : " + errorMsg : "");
        }

        if (isError) {
            log.warn(msg);
        } else {
            log.debug(msg);
        }
    }

    synchronized (this) {
        if (!activeConnections.isEmpty() && activeConnections.remove(conn) && log.isDebugEnabled()) {
            log.debug("Removing the connection : " + conn + " from pool of size : " + activeConnections.size());
        }
    }

    try {
        conn.shutdown();
    } catch (IOException ignore) {
    }
}