Example usage for org.apache.http.protocol ExecutionContext HTTP_RESPONSE

List of usage examples for org.apache.http.protocol ExecutionContext HTTP_RESPONSE

Introduction

In this page you can find the example usage for org.apache.http.protocol ExecutionContext HTTP_RESPONSE.

Prototype

String HTTP_RESPONSE

To view the source code for org.apache.http.protocol ExecutionContext HTTP_RESPONSE.

Click Source Link

Usage

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

/**
 * Perform processing of the received response though Axis2
 *
 * @param conn HTTP connection to be processed
 * @param context HTTP context associated with the connection
 * @param response HTTP response associated with the connection
 *///from ww w.j a v a2s  .co m
private void processResponse(final NHttpClientConnection conn, HttpContext context, HttpResponse response) {

    ContentInputBuffer inputBuffer = null;
    MessageContext outMsgContext = (MessageContext) context.getAttribute(OUTGOING_MESSAGE_CONTEXT);
    String endptPrefix = (String) context.getAttribute(NhttpConstants.ENDPOINT_PREFIX);
    String requestMethod = (String) context.getAttribute(NhttpConstants.HTTP_REQ_METHOD);
    int statusCode = response.getStatusLine().getStatusCode();

    boolean expectEntityBody = false;
    if (!"HEAD".equals(requestMethod) && !"OPTIONS".equals(requestMethod) && statusCode >= HttpStatus.SC_OK
            && statusCode != HttpStatus.SC_NO_CONTENT && statusCode != HttpStatus.SC_NOT_MODIFIED
            && statusCode != HttpStatus.SC_RESET_CONTENT) {
        expectEntityBody = true;
    } else if (NhttpConstants.HTTP_HEAD.equals(requestMethod)) {
        // When invoking http HEAD request esb set content length as 0 to response header. Since there is no message
        // body content length cannot be calculated inside synapse. Hence additional two headers are added to
        // which contains content length of the backend response and the request method. These headers are removed
        // before submitting the actual response.
        response.addHeader(NhttpConstants.HTTP_REQUEST_METHOD, requestMethod);

        if (response.getFirstHeader(HTTP.CONTENT_LEN) != null) {
            response.addHeader(NhttpConstants.ORIGINAL_CONTENT_LEN,
                    response.getFirstHeader(HTTP.CONTENT_LEN).getValue());
        }
    }

    if (expectEntityBody) {
        inputBuffer = new SharedInputBuffer(cfg.getBufferSize(), conn, allocator);
        context.setAttribute(RESPONSE_SINK_BUFFER, inputBuffer);

        BasicHttpEntity entity = new BasicHttpEntity();
        if (response.getStatusLine().getProtocolVersion().greaterEquals(HttpVersion.HTTP_1_1)) {
            entity.setChunked(true);
        }
        response.setEntity(entity);
        context.setAttribute(ExecutionContext.HTTP_RESPONSE, response);

    } else {
        conn.resetInput();
        conn.resetOutput();

        if (context.getAttribute(NhttpConstants.DISCARD_ON_COMPLETE) != null
                || !connStrategy.keepAlive(response, context)) {
            try {
                // this is a connection we should not re-use
                connpool.forget(conn);
                shutdownConnection(conn, false, null);
                context.removeAttribute(RESPONSE_SINK_BUFFER);
                context.removeAttribute(REQUEST_SOURCE_BUFFER);
            } catch (Exception ignore) {
            }
        } else {
            connpool.release(conn);
        }
    }

    workerPool
            .execute(new ClientWorker(cfgCtx, inputBuffer == null ? null : new ContentInputStream(inputBuffer),
                    response, outMsgContext, endptPrefix));
}

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();//from www .  j  av  a  2s  .  com
}

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 . ja v  a 2 s .c  om*/
        } 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);
    }
}

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

/**
 * Starts the response by writing the headers
 * @param conn connection//from   w w  w .  ja va  2 s . c  om
 * @throws java.io.IOException if an error occurs
 * @throws org.apache.http.HttpException if an error occurs
 */
public void start(NHttpServerConnection conn) throws IOException, HttpException {
    // create the response
    response = sourceConfiguration.getResponseFactory().newHttpResponse(request.getVersion(), this.status,
            request.getConnection().getContext());

    if (statusLine != null) {
        response.setStatusLine(version, status, statusLine);
    } else {
        response.setStatusCode(status);
    }

    BasicHttpEntity entity = null;

    if (canResponseHaveBody(request.getRequest(), response)) {
        entity = new BasicHttpEntity();

        int contentLength = -1;
        String contentLengthHeader = null;
        if (headers.get(HTTP.CONTENT_LEN) != null && headers.get(HTTP.CONTENT_LEN).size() > 0) {
            contentLengthHeader = headers.get(HTTP.CONTENT_LEN).first();
        }

        if (contentLengthHeader != null) {
            contentLength = Integer.parseInt(contentLengthHeader);
            headers.remove(HTTP.CONTENT_LEN);
        }

        if (contentLength != -1) {
            entity.setChunked(false);
            entity.setContentLength(contentLength);
        } else {
            entity.setChunked(true);
        }

    }

    response.setEntity(entity);

    // set any transport headers
    Set<Map.Entry<String, TreeSet<String>>> entries = headers.entrySet();

    for (Map.Entry<String, TreeSet<String>> entry : entries) {
        if (entry.getKey() != null) {
            Iterator<String> i = entry.getValue().iterator();
            while (i.hasNext()) {
                response.addHeader(entry.getKey(), i.next());
            }
        }
    }
    response.setParams(new DefaultedHttpParams(response.getParams(), sourceConfiguration.getHttpParams()));

    SourceContext.updateState(conn, ProtocolState.RESPONSE_HEAD);

    // Pre-process HTTP response
    conn.getContext().setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
    conn.getContext().setAttribute(ExecutionContext.HTTP_RESPONSE, response);
    conn.getContext().setAttribute(ExecutionContext.HTTP_REQUEST, SourceContext.getRequest(conn).getRequest());

    sourceConfiguration.getHttpProcessor().process(response, conn.getContext());
    conn.submitResponse(response);

    // Handle non entity body responses
    if (entity == null) {
        hasEntity = false;
        // Reset connection state
        sourceConfiguration.getSourceConnections().releaseConnection(conn);
        // Make ready to deal with a new request
        conn.requestInput();
    }
}

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

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

        org.siddhiesb.transport.passthru.ProtocolState state = org.siddhiesb.transport.passthru.SourceContext
                .getState(conn);// w w  w .  j  av  a 2s  . c o  m
        if (state == org.siddhiesb.transport.passthru.ProtocolState.REQUEST_BODY
                || state == org.siddhiesb.transport.passthru.ProtocolState.REQUEST_HEAD) {
            informReaderError(conn);
        } else if (state == org.siddhiesb.transport.passthru.ProtocolState.RESPONSE_BODY
                || state == org.siddhiesb.transport.passthru.ProtocolState.RESPONSE_HEAD) {
            informWriterError(conn);
        } else if (state == org.siddhiesb.transport.passthru.ProtocolState.REQUEST_DONE) {
            informWriterError(conn);
        } else if (state == org.siddhiesb.transport.passthru.ProtocolState.RESPONSE_DONE) {
            informWriterError(conn);
        }

        org.siddhiesb.transport.passthru.SourceContext.updateState(conn,
                org.siddhiesb.transport.passthru.ProtocolState.CLOSED);
        sourceConfiguration.getSourceConnections().shutDownConnection(conn);
    } else if (ex instanceof HttpException) {
        try {
            if (conn.isResponseSubmitted()) {
                sourceConfiguration.getSourceConnections().shutDownConnection(conn);
                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);
            org.siddhiesb.transport.passthru.SourceContext.updateState(conn,
                    org.siddhiesb.transport.passthru.ProtocolState.CLOSED);
            conn.close();
        } catch (Exception ex1) {
            log.error(ex.getMessage(), ex);
            org.siddhiesb.transport.passthru.SourceContext.updateState(conn,
                    org.siddhiesb.transport.passthru.ProtocolState.CLOSED);
            sourceConfiguration.getSourceConnections().shutDownConnection(conn);
        }
    } else {
        log.error("Unexoected error: " + ex.getMessage(), ex);
        org.siddhiesb.transport.passthru.SourceContext.updateState(conn,
                org.siddhiesb.transport.passthru.ProtocolState.CLOSED);
        sourceConfiguration.getSourceConnections().shutDownConnection(conn);
    }
}