Example usage for org.apache.http.protocol HttpContext removeAttribute

List of usage examples for org.apache.http.protocol HttpContext removeAttribute

Introduction

In this page you can find the example usage for org.apache.http.protocol HttpContext removeAttribute.

Prototype

Object removeAttribute(String str);

Source Link

Usage

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

private static void cleanConnectionReferences(NHttpClientConnection conn) {

    HttpContext ctx = conn.getContext();
    Axis2HttpRequest axis2Req = (Axis2HttpRequest) ctx.getAttribute(ClientHandler.AXIS2_HTTP_REQUEST);
    axis2Req.clear(); // this is linked via the selection key attachment and will free itself
                      // on timeout of the keep alive connection. Till then minimize the
                      // memory usage to a few bytes 

    ctx.removeAttribute(ClientHandler.ATTACHMENT_KEY);
    ctx.removeAttribute(ClientHandler.TUNNEL_HANDLER);
    ctx.removeAttribute(ClientHandler.AXIS2_HTTP_REQUEST);
    ctx.removeAttribute(ClientHandler.OUTGOING_MESSAGE_CONTEXT);
    ctx.removeAttribute(ClientHandler.REQUEST_SOURCE_BUFFER);
    ctx.removeAttribute(ClientHandler.RESPONSE_SINK_BUFFER);

    ctx.removeAttribute(ExecutionContext.HTTP_REQUEST);
    ctx.removeAttribute(ExecutionContext.HTTP_RESPONSE);

    conn.resetOutput();/*  www .  j av a2  s  . c  om*/
}

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

public void responseReceived(NHttpClientConnection conn) {
    HttpContext context = conn.getContext();
    HttpResponse response = conn.getHttpResponse();
    ProtocolState connState;/*from  w  w  w. j a v a2s .  c  o  m*/
    try {
        String method = null;
        ProxyTunnelHandler tunnelHandler = (ProxyTunnelHandler) context
                .getAttribute(PassThroughConstants.TUNNEL_HANDLER);
        if (tunnelHandler != null && !tunnelHandler.isCompleted()) {
            method = "CONNECT";
            context.removeAttribute(PassThroughConstants.TUNNEL_HANDLER);
            tunnelHandler.handleResponse(response, conn);
            if (tunnelHandler.isSuccessful()) {
                log.debug(conn + ": Tunnel established");
                conn.resetInput();
                conn.requestOutput();
                return;
            } else {
                TargetContext.updateState(conn, ProtocolState.REQUEST_DONE);
            }
        }

        int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode < HttpStatus.SC_OK) {
            if (log.isDebugEnabled()) {
                log.debug(conn + ": Received a 100 Continue response");
            }
            // Ignore 1xx response
            return;
        }
        boolean isError = false;
        context.setAttribute(PassThroughConstants.RES_HEADER_ARRIVAL_TIME, System.currentTimeMillis());
        connState = TargetContext.getState(conn);
        MessageContext requestMsgContext = TargetContext.get(conn).getRequestMsgCtx();
        NHttpServerConnection sourceConn = (NHttpServerConnection) requestMsgContext
                .getProperty(PassThroughConstants.PASS_THROUGH_SOURCE_CONNECTION);

        if (connState != ProtocolState.REQUEST_DONE) {
            isError = true;
            StatusLine errorStatus = response.getStatusLine();
            /* We might receive a 404 or a similar type, even before we write the request body. */
            if (errorStatus != null) {
                if (errorStatus.getStatusCode() >= HttpStatus.SC_BAD_REQUEST) {
                    TargetContext.updateState(conn, ProtocolState.REQUEST_DONE);
                    conn.resetOutput();
                    if (sourceConn != null) {
                        SourceContext.updateState(sourceConn, ProtocolState.REQUEST_DONE);
                        SourceContext.get(sourceConn).setShutDown(true);
                    }
                    if (log.isDebugEnabled()) {
                        log.debug(conn + ": Received response with status code : "
                                + response.getStatusLine().getStatusCode() + " in invalid state : "
                                + connState.name());
                    }
                }
            } else {
                handleInvalidState(conn, "Receiving response");
                return;
            }
        }
        context.setAttribute(PassThroughConstants.RES_FROM_BACKEND_READ_START_TIME, System.currentTimeMillis());
        TargetRequest targetRequest = TargetContext.getRequest(conn);

        if (targetRequest != null) {
            method = targetRequest.getMethod();
        }
        if (method == null) {
            method = "POST";
        }
        boolean canResponseHaveBody = isResponseHaveBodyExpected(method, response);
        if (!canResponseHaveBody) {
            if (log.isDebugEnabled()) {
                log.debug(conn + ": Received no-content response " + response.getStatusLine().getStatusCode());
            }
            conn.resetInput();
        }
        TargetResponse targetResponse = new TargetResponse(targetConfiguration, response, conn,
                canResponseHaveBody, isError);
        TargetContext.setResponse(conn, targetResponse);
        targetResponse.start(conn);

        if (statusCode == HttpStatus.SC_ACCEPTED && handle202(requestMsgContext)) {
            return;
        }

        targetConfiguration.getWorkerPool()
                .execute(new ClientWorker(targetConfiguration, requestMsgContext, targetResponse));

        targetConfiguration.getMetrics().incrementMessagesReceived();

        sourceConn = (NHttpServerConnection) requestMsgContext
                .getProperty(PassThroughConstants.PASS_THROUGH_SOURCE_CONNECTION);
        if (sourceConn != null) {
            sourceConn.getContext().setAttribute(PassThroughConstants.RES_HEADER_ARRIVAL_TIME,
                    conn.getContext().getAttribute(PassThroughConstants.RES_HEADER_ARRIVAL_TIME));
            conn.getContext().removeAttribute(PassThroughConstants.RES_HEADER_ARRIVAL_TIME);

            sourceConn.getContext().setAttribute(PassThroughConstants.REQ_DEPARTURE_TIME,
                    conn.getContext().getAttribute(PassThroughConstants.REQ_DEPARTURE_TIME));
            conn.getContext().removeAttribute(PassThroughConstants.REQ_DEPARTURE_TIME);
            sourceConn.getContext().setAttribute(PassThroughConstants.REQ_TO_BACKEND_WRITE_START_TIME,
                    conn.getContext().getAttribute(PassThroughConstants.REQ_TO_BACKEND_WRITE_START_TIME));

            conn.getContext().removeAttribute(PassThroughConstants.REQ_TO_BACKEND_WRITE_START_TIME);
            sourceConn.getContext().setAttribute(PassThroughConstants.REQ_TO_BACKEND_WRITE_END_TIME,
                    conn.getContext().getAttribute(PassThroughConstants.REQ_TO_BACKEND_WRITE_END_TIME));
            conn.getContext().removeAttribute(PassThroughConstants.REQ_TO_BACKEND_WRITE_END_TIME);
            sourceConn.getContext().setAttribute(PassThroughConstants.RES_FROM_BACKEND_READ_START_TIME,
                    conn.getContext().getAttribute(PassThroughConstants.RES_FROM_BACKEND_READ_START_TIME));
            conn.getContext().removeAttribute(PassThroughConstants.RES_FROM_BACKEND_READ_START_TIME);

        }

    } catch (Exception ex) {
        log.error("Exception occurred while processing response", ex);

        informReaderError(conn);

        TargetContext.updateState(conn, ProtocolState.CLOSED);
        targetConfiguration.getConnections().shutdownConnection(conn, true);
    }
}

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

public void outputReady(NHttpServerConnection conn, ContentEncoder encoder) {
    try {/*  w  w w  . j a  v a  2  s.  c  o m*/
        org.siddhiesb.transport.passthru.ProtocolState protocolState = org.siddhiesb.transport.passthru.SourceContext
                .getState(conn);

        //special case to handle WSDLs
        if (protocolState == org.siddhiesb.transport.passthru.ProtocolState.WSDL_RESPONSE_DONE) {
            // we need to shut down if the shutdown flag is set
            HttpContext context = conn.getContext();
            ContentOutputBuffer outBuf = (ContentOutputBuffer) context
                    .getAttribute("synapse.response-source-buffer");
            int bytesWritten = outBuf.produceContent(encoder);
            conn.requestInput();
            if (outBuf instanceof SimpleOutputBuffer && !((SimpleOutputBuffer) outBuf).hasData()) {
                sourceConfiguration.getSourceConnections().releaseConnection(conn);
            }

            return;
        }

        if (protocolState != org.siddhiesb.transport.passthru.ProtocolState.RESPONSE_HEAD
                && protocolState != org.siddhiesb.transport.passthru.ProtocolState.RESPONSE_BODY) {
            log.warn("Illegal incoming connection state: " + protocolState + " . Possibly two send backs "
                    + "are happening for the same request");

            handleInvalidState(conn, "Trying to write response body");
            return;
        }

        org.siddhiesb.transport.passthru.SourceContext.updateState(conn,
                org.siddhiesb.transport.passthru.ProtocolState.RESPONSE_BODY);

        SourceResponse response = org.siddhiesb.transport.passthru.SourceContext.getResponse(conn);

        int bytesSent = response.write(conn, encoder);

        if (encoder.isCompleted()) {
            HttpContext context = conn.getContext();
            if (context.getAttribute(PassThroughConstants.REQ_ARRIVAL_TIME) != null
                    && context.getAttribute(PassThroughConstants.REQ_DEPARTURE_TIME) != null
                    && context.getAttribute(PassThroughConstants.RES_HEADER_ARRIVAL_TIME) != null) {
            }

            context.removeAttribute(PassThroughConstants.REQ_ARRIVAL_TIME);
            context.removeAttribute(PassThroughConstants.REQ_DEPARTURE_TIME);
            context.removeAttribute(PassThroughConstants.RES_HEADER_ARRIVAL_TIME);
        }

    } catch (IOException e) {
        logIOException(conn, e);

        informWriterError(conn);

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

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

public void responseReceived(NHttpClientConnection conn) {
    //System.out.println("============ TargetHandler :  responseReceived ===============");

    HttpContext context = conn.getContext();
    HttpResponse response = conn.getHttpResponse();
    org.siddhiesb.transport.passthru.ProtocolState connState;
    try {/*  w w w.j  a  v  a 2s.  com*/
        String method = null;
        ProxyTunnelHandler tunnelHandler = (ProxyTunnelHandler) context
                .getAttribute(PassThroughConstants.TUNNEL_HANDLER);
        if (tunnelHandler != null && !tunnelHandler.isCompleted()) {
            method = "CONNECT";
            context.removeAttribute(PassThroughConstants.TUNNEL_HANDLER);
            tunnelHandler.handleResponse(response, conn);
            if (tunnelHandler.isSuccessful()) {
                log.debug(conn + ": Tunnel established");
                conn.resetInput();
                conn.requestOutput();
                return;
            } else {
                org.siddhiesb.transport.passthru.TargetContext.updateState(conn,
                        org.siddhiesb.transport.passthru.ProtocolState.REQUEST_DONE);
            }
        }

        context.setAttribute(PassThroughConstants.RES_HEADER_ARRIVAL_TIME, System.currentTimeMillis());
        connState = org.siddhiesb.transport.passthru.TargetContext.getState(conn);
        if (connState != org.siddhiesb.transport.passthru.ProtocolState.REQUEST_DONE) {
            handleInvalidState(conn, "Receiving response");
            return;
        }

        TargetRequest targetRequest = org.siddhiesb.transport.passthru.TargetContext.getRequest(conn);

        int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode < HttpStatus.SC_OK) {
            if (log.isDebugEnabled()) {
                log.debug(conn + ": Received a 100 Continue response");
            }
            // Ignore 1xx response
            return;
        }

        if (targetRequest != null) {
            method = targetRequest.getMethod();
        }
        if (method == null) {
            method = "POST";
        }
        boolean canResponseHaveBody = isResponseHaveBodyExpected(method, response);
        TargetResponse targetResponse = new TargetResponse(targetConfiguration, response, conn,
                canResponseHaveBody);
        org.siddhiesb.transport.passthru.TargetContext.setResponse(conn, targetResponse);
        targetResponse.start(conn);

        CommonContext requestContext = org.siddhiesb.transport.passthru.TargetContext.get(conn)
                .getCommonContext();

        targetConfiguration.getWorkerPool()
                .execute(new ClientWorker(requestContext, targetResponse, mediationEngine));

    } catch (Exception ex) {
        log.error(ex.getMessage(), ex);

        informReaderError(conn);

        org.siddhiesb.transport.passthru.TargetContext.updateState(conn,
                org.siddhiesb.transport.passthru.ProtocolState.CLOSED);
        targetConfiguration.getConnections().shutdownConnection(conn);
    }
}