}
protected HttpResponse createResponse(Object src, String encoding, MuleMessage msg)
throws IOException, TransformerException
{
HttpResponse response = new HttpResponse();
Object tmp = msg.getOutboundProperty(HttpConnector.HTTP_STATUS_PROPERTY);
int status = HttpConstants.SC_OK;
if (tmp != null)
{
status = Integer.valueOf(tmp.toString());
}
else if (msg.getExceptionPayload() != null)
{
status = HttpConstants.SC_INTERNAL_SERVER_ERROR;
}
String version = msg.getInboundProperty(HttpConnector.HTTP_VERSION_PROPERTY);
if (version == null)
{
version = HttpConstants.HTTP11;
}
String date = new SimpleDateFormat(HttpConstants.DATE_FORMAT, Locale.US).format(new Date());
String contentType = msg.getInboundProperty(HttpConstants.HEADER_CONTENT_TYPE);
if (contentType == null)
{
contentType = msg.getInvocationProperty(HttpConstants.HEADER_CONTENT_TYPE);
}
// MULE-4047 Don't explicitly set the content-type to a default value here,
// otherwise any settings on the servlet/transport will be happily ignored.
//if (contentType == null)
//{
// contentType = HttpConstants.DEFAULT_CONTENT_TYPE;
//
// if (encoding != null)
// {
// contentType += "; charset=" + encoding;
// }
// logger.warn("Content-Type was not set, defaulting to: " + contentType);
//}
response.setStatusLine(HttpVersion.parse(version), status);
if (contentType != null)
{
response.setHeader(new Header(HttpConstants.HEADER_CONTENT_TYPE, contentType));
}
response.setHeader(new Header(HttpConstants.HEADER_DATE, date));
response.setHeader(new Header(HttpConstants.HEADER_SERVER, server));
if (msg.getOutboundProperty(HttpConstants.HEADER_EXPIRES) == null)
{
response.setHeader(new Header(HttpConstants.HEADER_EXPIRES, date));
}
String etag = msg.getOutboundProperty(HttpConstants.HEADER_ETAG);
if (etag != null)
{
response.setHeader(new Header(HttpConstants.HEADER_ETAG, etag));
}
response.setFallbackCharset(encoding);
Collection<String> headerNames = HttpConstants.RESPONSE_HEADER_NAMES.values();
for (String headerName : headerNames)
{
if (HttpConstants.HEADER_COOKIE_SET.equals(headerName))
{
// TODO This have to be improved. We shouldn't have to look in all
// scopes
Object cookiesObject = msg.getInvocationProperty(headerName);
if (cookiesObject == null)
{
cookiesObject = msg.getOutboundProperty(headerName);
}
if (cookiesObject == null)
{
cookiesObject = msg.getInboundProperty(headerName);
}
if (cookiesObject == null)
{
continue;
}
if (!(cookiesObject instanceof Cookie[]))
{
response.addHeader(new Header(headerName, cookiesObject.toString()));
}
else
{
Cookie[] arrayOfCookies = CookieHelper.asArrayOfCookies(cookiesObject);
for (Cookie cookie : arrayOfCookies)
{
response.addHeader(new Header(headerName,
CookieHelper.formatCookieForASetCookieHeader(cookie)));
}
}
}
else
{
String value = msg.getInvocationProperty(headerName);
if (value == null)
{
value = msg.getOutboundProperty(headerName);
}
if (value != null)
{
response.setHeader(new Header(headerName, value));
}
}
}
Map customHeaders = msg.getOutboundProperty(HttpConnector.HTTP_CUSTOM_HEADERS_MAP_PROPERTY);
if (customHeaders != null)
{
throw new TransformerException(HttpMessages.customHeaderMapDeprecated(), this);
}
//attach the outbound properties to the message
for (String headerName : msg.getOutboundPropertyNames())
{
Object v = msg.getOutboundProperty(headerName);
if (v != null)
{
response.setHeader(new Header(headerName, v.toString()));
}
}
// Mule properties
String user = msg.getOutboundProperty(MuleProperties.MULE_USER_PROPERTY);
if (user != null)
{
response.setHeader(new Header(CUSTOM_HEADER_PREFIX + MuleProperties.MULE_USER_PROPERTY, user));
}
if (msg.getCorrelationId() != null)
{
response.setHeader(new Header(CUSTOM_HEADER_PREFIX + MuleProperties.MULE_CORRELATION_ID_PROPERTY,
msg.getCorrelationId()));
response.setHeader(new Header(CUSTOM_HEADER_PREFIX
+ MuleProperties.MULE_CORRELATION_GROUP_SIZE_PROPERTY,
String.valueOf(msg.getCorrelationGroupSize())));
response.setHeader(new Header(CUSTOM_HEADER_PREFIX
+ MuleProperties.MULE_CORRELATION_SEQUENCE_PROPERTY,
String.valueOf(msg.getCorrelationSequence())));
}
if (msg.getReplyTo() != null)
{
response.setHeader(new Header(CUSTOM_HEADER_PREFIX + MuleProperties.MULE_REPLY_TO_PROPERTY,
msg.getReplyTo().toString()));
}
try
{
response.setBody(msg);
}
catch (Exception e)
{
throw new TransformerException(this, e);
}