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;
}
Aggregations