Search in sources :

Example 1 with SourceResponse

use of org.apache.synapse.transport.passthru.SourceResponse in project wso2-synapse by wso2.

the class SourceResponseFactory method create.

public static SourceResponse create(MessageContext msgContext, SourceRequest sourceRequest, SourceConfiguration sourceConfiguration) {
    // determine the status code to be sent
    int statusCode = PassThroughTransportUtils.determineHttpStatusCode(msgContext);
    SourceResponse sourceResponse;
    String statusLine = PassThroughTransportUtils.determineHttpStatusLine(msgContext);
    if (msgContext.getProperty(PassThroughConstants.ORIGINAL_HTTP_SC) != null && statusCode == ((Integer) msgContext.getProperty(PassThroughConstants.ORIGINAL_HTTP_SC))) {
        sourceResponse = new SourceResponse(sourceConfiguration, statusCode, statusLine, sourceRequest);
    } else {
        if (msgContext.getProperty(PassThroughConstants.ORIGINAL_HTTP_REASON_PHRASE) != null && (statusLine.equals(msgContext.getProperty(PassThroughConstants.ORIGINAL_HTTP_REASON_PHRASE)))) {
            sourceResponse = new SourceResponse(sourceConfiguration, statusCode, sourceRequest);
        } else {
            sourceResponse = new SourceResponse(sourceConfiguration, statusCode, statusLine, sourceRequest);
        }
    }
    // set any transport headers
    Map transportHeaders = (Map) msgContext.getProperty(MessageContext.TRANSPORT_HEADERS);
    boolean forceContentLength = msgContext.isPropertyTrue(NhttpConstants.FORCE_HTTP_CONTENT_LENGTH);
    boolean forceContentLengthCopy = msgContext.isPropertyTrue(PassThroughConstants.COPY_CONTENT_LENGTH_FROM_INCOMING);
    if (forceContentLength && forceContentLengthCopy && msgContext.getProperty(PassThroughConstants.ORGINAL_CONTEN_LENGTH) != null) {
        sourceResponse.addHeader(HTTP.CONTENT_LEN, (String) msgContext.getProperty(PassThroughConstants.ORGINAL_CONTEN_LENGTH));
    }
    // set to sourceResponse.
    if (sourceRequest != null && PassThroughConstants.HTTP_HEAD.equalsIgnoreCase(sourceRequest.getRequest().getRequestLine().getMethod()) && msgContext.getProperty(PassThroughConstants.ORGINAL_CONTEN_LENGTH) != null) {
        sourceResponse.addHeader(PassThroughConstants.ORGINAL_CONTEN_LENGTH, (String) msgContext.getProperty(PassThroughConstants.ORGINAL_CONTEN_LENGTH));
    }
    if (transportHeaders != null && msgContext.getProperty(org.apache.axis2.Constants.Configuration.MESSAGE_TYPE) != null) {
        if (msgContext.getProperty(org.apache.axis2.Constants.Configuration.CONTENT_TYPE) != null && msgContext.getProperty(org.apache.axis2.Constants.Configuration.CONTENT_TYPE).toString().contains(PassThroughConstants.CONTENT_TYPE_MULTIPART_RELATED)) {
            transportHeaders.put(org.apache.axis2.Constants.Configuration.MESSAGE_TYPE, PassThroughConstants.CONTENT_TYPE_MULTIPART_RELATED);
        } else {
            Pipe pipe = (Pipe) msgContext.getProperty(PassThroughConstants.PASS_THROUGH_PIPE);
            if (pipe != null && !Boolean.TRUE.equals(msgContext.getProperty(PassThroughConstants.MESSAGE_BUILDER_INVOKED))) {
                transportHeaders.put(HTTP.CONTENT_TYPE, msgContext.getProperty(org.apache.axis2.Constants.Configuration.CONTENT_TYPE));
            }
        }
    }
    if (transportHeaders != null) {
        addResponseHeader(sourceResponse, transportHeaders);
    } else {
        Boolean noEntityBody = (Boolean) msgContext.getProperty(NhttpConstants.NO_ENTITY_BODY);
        if (noEntityBody == null || Boolean.FALSE == noEntityBody) {
            OMOutputFormat format = NhttpUtil.getOMOutputFormat(msgContext);
            transportHeaders = new HashMap();
            MessageFormatter messageFormatter = MessageFormatterDecoratorFactory.createMessageFormatterDecorator(msgContext);
            if (msgContext.getProperty(org.apache.axis2.Constants.Configuration.MESSAGE_TYPE) == null) {
                transportHeaders.put(HTTP.CONTENT_TYPE, messageFormatter.getContentType(msgContext, format, msgContext.getSoapAction()));
            }
            addResponseHeader(sourceResponse, transportHeaders);
        }
    }
    // Add excess response header.
    String excessProp = NhttpConstants.EXCESS_TRANSPORT_HEADERS;
    Map excessHeaders = (Map) msgContext.getProperty(excessProp);
    if (excessHeaders != null) {
        for (Iterator iterator = excessHeaders.keySet().iterator(); iterator.hasNext(); ) {
            String key = (String) iterator.next();
            for (String excessVal : (Collection<String>) excessHeaders.get(key)) {
                sourceResponse.addHeader(key, (String) excessVal);
            }
        }
    }
    // keep alive
    String noKeepAlive = (String) msgContext.getProperty(PassThroughConstants.NO_KEEPALIVE);
    if ("true".equals(noKeepAlive) || PassThroughConfiguration.getInstance().isKeepAliveDisabled()) {
        sourceResponse.setKeepAlive(false);
    } else {
        // that, disable keep-alive to avoid re-using the existing connection by client for the next request.
        if (sourceRequest != null) {
            String requestMethod = sourceRequest.getRequest().getRequestLine().getMethod();
            if (requestMethod != null && isPayloadOptionalMethod(requestMethod.toUpperCase()) && (sourceRequest.getHeaders().containsKey(HTTP.CONTENT_LEN) || sourceRequest.getHeaders().containsKey(HTTP.TRANSFER_ENCODING))) {
                if (log.isDebugEnabled()) {
                    log.debug("Disable keep-alive in the client connection : Content-length/Transfer-encoding" + " headers present for GET/HEAD/DELETE request");
                }
                sourceResponse.setKeepAlive(false);
            }
        }
    }
    return sourceResponse;
}
Also used : SourceResponse(org.apache.synapse.transport.passthru.SourceResponse) HashMap(java.util.HashMap) Iterator(java.util.Iterator) Collection(java.util.Collection) Pipe(org.apache.synapse.transport.passthru.Pipe) MessageFormatter(org.apache.axis2.transport.MessageFormatter) OMOutputFormat(org.apache.axiom.om.OMOutputFormat) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1 OMOutputFormat (org.apache.axiom.om.OMOutputFormat)1 MessageFormatter (org.apache.axis2.transport.MessageFormatter)1 Pipe (org.apache.synapse.transport.passthru.Pipe)1 SourceResponse (org.apache.synapse.transport.passthru.SourceResponse)1