Search in sources :

Example 1 with RequestResponseTransport

use of org.apache.axis2.transport.RequestResponseTransport in project wso2-synapse by wso2.

the class ServerWorker method sendAck.

public void sendAck(MessageContext msgContext) {
    String respWritten = "";
    if (msgContext.getOperationContext() != null) {
        respWritten = (String) msgContext.getOperationContext().getProperty(Constants.RESPONSE_WRITTEN);
    }
    if (msgContext.getProperty(PassThroughConstants.FORCE_SOAP_FAULT) != null) {
        respWritten = "SKIP";
    }
    boolean respWillFollow = !Constants.VALUE_TRUE.equals(respWritten) && !"SKIP".equals(respWritten);
    boolean ack = (((RequestResponseTransport) msgContext.getProperty(RequestResponseTransport.TRANSPORT_CONTROL)).getStatus() == RequestResponseTransport.RequestResponseTransportStatus.ACKED);
    boolean forced = msgContext.isPropertyTrue(NhttpConstants.FORCE_SC_ACCEPTED);
    boolean nioAck = msgContext.isPropertyTrue("NIO-ACK-Requested", false);
    if (respWillFollow || ack || forced || nioAck) {
        NHttpServerConnection conn = request.getConnection();
        SourceResponse sourceResponse;
        if (!nioAck) {
            msgContext.removeProperty(MessageContext.TRANSPORT_HEADERS);
            sourceResponse = SourceResponseFactory.create(msgContext, request, sourceConfiguration);
            sourceResponse.setStatus(HttpStatus.SC_ACCEPTED);
        } else {
            if (log.isDebugEnabled()) {
                log.debug("Sending ACK response with status " + msgContext.getProperty(NhttpConstants.HTTP_SC) + ", for MessageID : " + msgContext.getMessageID());
            }
            sourceResponse = SourceResponseFactory.create(msgContext, request, sourceConfiguration);
            sourceResponse.setStatus(Integer.parseInt(msgContext.getProperty(NhttpConstants.HTTP_SC).toString()));
        }
        SourceContext.setResponse(conn, sourceResponse);
        ProtocolState state = SourceContext.getState(conn);
        if (state != null && state.compareTo(ProtocolState.REQUEST_DONE) <= 0) {
            conn.requestOutput();
        } else {
            SourceContext.updateState(conn, ProtocolState.CLOSED);
            sourceConfiguration.getSourceConnections().shutDownConnection(conn);
        }
    }
}
Also used : NHttpServerConnection(org.apache.http.nio.NHttpServerConnection) HttpCoreRequestResponseTransport(org.apache.synapse.transport.nhttp.HttpCoreRequestResponseTransport) RequestResponseTransport(org.apache.axis2.transport.RequestResponseTransport)

Example 2 with RequestResponseTransport

use of org.apache.axis2.transport.RequestResponseTransport in project wso2-synapse by wso2.

the class ServerWorker method run.

/**
 * Process the incoming request
 */
@SuppressWarnings({ "unchecked" })
public void run() {
    CustomLogSetter.getInstance().clearThreadLocalContent();
    conn.getContext().setAttribute(NhttpConstants.SERVER_WORKER_START_TIME, System.currentTimeMillis());
    conn.getContext().setAttribute(NhttpConstants.SERVER_WORKER_THREAD_ID, Thread.currentThread().getId());
    String method = request.getRequestLine().getMethod().toUpperCase();
    msgContext.setProperty(Constants.Configuration.HTTP_METHOD, request.getRequestLine().getMethod());
    if (NHttpConfiguration.getInstance().isHttpMethodDisabled(method)) {
        handleException("Unsupported method : " + method, null);
    }
    // String uri = request.getRequestLine().getUri();
    String oriUri = request.getRequestLine().getUri();
    String restUrlPostfix = NhttpUtil.getRestUrlPostfix(oriUri, cfgCtx.getServicePath());
    msgContext.setProperty(NhttpConstants.REST_URL_POSTFIX, restUrlPostfix);
    String servicePrefix = oriUri.substring(0, oriUri.indexOf(restUrlPostfix));
    if (servicePrefix.indexOf("://") == -1) {
        HttpInetConnection inetConn = (HttpInetConnection) conn;
        InetAddress localAddr = inetConn.getLocalAddress();
        if (localAddr != null) {
            servicePrefix = schemeName + "://" + localAddr.getHostName() + ":" + inetConn.getLocalPort() + servicePrefix;
        }
    }
    msgContext.setProperty(NhttpConstants.SERVICE_PREFIX, servicePrefix);
    if ("GET".equals(method)) {
        httpGetRequestProcessor.process(request, response, msgContext, conn, os, isRestDispatching);
    } else if ("POST".equals(method)) {
        processEntityEnclosingMethod();
    } else if ("PUT".equals(method)) {
        processEntityEnclosingMethod();
    } else if ("HEAD".equals(method)) {
        processNonEntityEnclosingMethod();
    } else if ("OPTIONS".equals(method)) {
        processNonEntityEnclosingMethod();
    } else if ("DELETE".equals(method)) {
        processGetAndDelete("DELETE");
    } else if ("TRACE".equals(method)) {
        processNonEntityEnclosingMethod();
    } else if ("PATCH".equals(method)) {
        processEntityEnclosingMethod();
    } else {
        handleException("Unsupported method : " + method, null);
    }
    // client.
    if (isAckRequired()) {
        String respWritten = "";
        if (msgContext.getOperationContext() != null) {
            respWritten = (String) msgContext.getOperationContext().getProperty(Constants.RESPONSE_WRITTEN);
        }
        boolean respWillFollow = !Constants.VALUE_TRUE.equals(respWritten) && !"SKIP".equals(respWritten);
        boolean acked = (((RequestResponseTransport) msgContext.getProperty(RequestResponseTransport.TRANSPORT_CONTROL)).getStatus() == RequestResponseTransport.RequestResponseTransportStatus.ACKED);
        boolean forced = msgContext.isPropertyTrue(NhttpConstants.FORCE_SC_ACCEPTED);
        boolean nioAck = msgContext.isPropertyTrue("NIO-ACK-Requested", false);
        if (respWillFollow || acked || forced || nioAck) {
            if (!nioAck) {
                if (log.isDebugEnabled()) {
                    log.debug("Sending 202 Accepted response for MessageID : " + msgContext.getMessageID() + " response written : " + respWritten + " response will follow : " + respWillFollow + " acked : " + acked + " forced ack : " + forced);
                }
                response.setStatusCode(HttpStatus.SC_ACCEPTED);
            } else {
                if (log.isDebugEnabled()) {
                    log.debug("Sending ACK response with status " + msgContext.getProperty(NhttpConstants.HTTP_SC) + ", for MessageID : " + msgContext.getMessageID());
                }
                response.setStatusCode(Integer.parseInt(msgContext.getProperty(NhttpConstants.HTTP_SC).toString()));
                Map<String, String> responseHeaders = (Map<String, String>) msgContext.getProperty(MessageContext.TRANSPORT_HEADERS);
                if (responseHeaders != null) {
                    for (String headerName : responseHeaders.keySet()) {
                        response.addHeader(headerName, responseHeaders.get(headerName));
                        String excessProp = NhttpConstants.EXCESS_TRANSPORT_HEADERS;
                        Map map = (Map) msgContext.getProperty(excessProp);
                        if (map != null) {
                            log.debug("Number of excess values for " + headerName + " header is : " + ((Collection) (map.get(headerName))).size());
                            for (Iterator iterator = map.keySet().iterator(); iterator.hasNext(); ) {
                                String key = (String) iterator.next();
                                for (String excessVal : (Collection<String>) map.get(key)) {
                                    response.addHeader(headerName, (String) excessVal);
                                }
                            }
                        }
                    }
                }
            }
            if (metrics != null) {
                metrics.incrementMessagesSent();
            }
            try {
                /* 
                     * Remove Content-Length and Transfer-Encoding headers, if already present.
                     * */
                response.removeHeaders(HTTP.TRANSFER_ENCODING);
                response.removeHeaders(HTTP.CONTENT_LEN);
                serverHandler.commitResponse(conn, response);
            } catch (HttpException e) {
                if (metrics != null) {
                    metrics.incrementFaultsSending();
                }
                handleException("Unexpected HTTP protocol error : " + e.getMessage(), e);
            } catch (ConnectionClosedException e) {
                if (metrics != null) {
                    metrics.incrementFaultsSending();
                }
                log.warn("Connection closed by client (Connection closed)");
            } catch (IllegalStateException e) {
                if (metrics != null) {
                    metrics.incrementFaultsSending();
                }
                log.warn("Connection closed by client (Buffer closed)");
            } catch (IOException e) {
                if (metrics != null) {
                    metrics.incrementFaultsSending();
                }
                handleException("IO Error sending response message", e);
            } catch (Exception e) {
                if (metrics != null) {
                    metrics.incrementFaultsSending();
                }
                handleException("General Error sending response message", e);
            }
            if (is != null) {
                try {
                    is.close();
                } catch (IOException ignore) {
                }
            }
            // make sure that the output stream is flushed and closed properly
            try {
                os.flush();
                os.close();
            } catch (IOException ignore) {
            }
        }
    }
}
Also used : RequestResponseTransport(org.apache.axis2.transport.RequestResponseTransport) IOException(java.io.IOException) IOException(java.io.IOException) SSLPeerUnverifiedException(javax.net.ssl.SSLPeerUnverifiedException) InetAddress(java.net.InetAddress) MultiValueMap(org.apache.commons.collections.map.MultiValueMap)

Aggregations

RequestResponseTransport (org.apache.axis2.transport.RequestResponseTransport)2 IOException (java.io.IOException)1 InetAddress (java.net.InetAddress)1 SSLPeerUnverifiedException (javax.net.ssl.SSLPeerUnverifiedException)1 MultiValueMap (org.apache.commons.collections.map.MultiValueMap)1 NHttpServerConnection (org.apache.http.nio.NHttpServerConnection)1 HttpCoreRequestResponseTransport (org.apache.synapse.transport.nhttp.HttpCoreRequestResponseTransport)1