ServerWorker worker = (ServerWorker) msgContext.getProperty(Constants.OUT_TRANSPORT_INFO);
HttpResponse response = worker.getResponse();
OMOutputFormat format = NhttpUtil.getOMOutputFormat(msgContext);
MessageFormatter messageFormatter =
MessageFormatterDecoratorFactory.createMessageFormatterDecorator(msgContext);
Boolean noEntityBody = (Boolean) msgContext.getProperty(NhttpConstants.NO_ENTITY_BODY);
if (noEntityBody == null || Boolean.FALSE == noEntityBody) {
response.setHeader(
HTTP.CONTENT_TYPE,
messageFormatter.getContentType(msgContext, format, msgContext.getSoapAction()));
}
response.setStatusCode(determineHttpStatusCode(msgContext, response));
// set any transport headers
Map transportHeaders = (Map) msgContext.getProperty(MessageContext.TRANSPORT_HEADERS);
if (transportHeaders != null && !transportHeaders.values().isEmpty()) {
Iterator iter = transportHeaders.keySet().iterator();
while (iter.hasNext()) {
Object header = iter.next();
Object value = transportHeaders.get(header);
if (value != null && header instanceof String && value instanceof String) {
response.setHeader((String) header, (String) value);
}
}
}
boolean forceContentLength = msgContext.isPropertyTrue(
NhttpConstants.FORCE_HTTP_CONTENT_LENGTH);
boolean forceContentLengthCopy = msgContext.isPropertyTrue(
NhttpConstants.COPY_CONTENT_LENGTH_FROM_INCOMING);
BasicHttpEntity entity = (BasicHttpEntity) response.getEntity();
MetricsCollector lstMetrics = worker.getServiceHandler().getMetrics();
try {
if (forceContentLength) {
entity.setChunked(false);
if (forceContentLengthCopy && contentLength > 0) {
entity.setContentLength(contentLength);
} else {
setStreamAsTempData(entity, messageFormatter, msgContext, format);
}
}
worker.getServiceHandler().commitResponse(worker.getConn(), response);
lstMetrics.reportResponseCode(response.getStatusLine().getStatusCode());
OutputStream out = worker.getOutputStream();
/*
* if this is a dummy message to handle http 202 case with non-blocking IO
* write an empty byte array as body
*/
if (msgContext.isPropertyTrue(NhttpConstants.SC_ACCEPTED)
|| Boolean.TRUE == noEntityBody) {
out.write(new byte[0]);
} else {
if (forceContentLength) {
if (forceContentLengthCopy && contentLength > 0) {
messageFormatter.writeTo(msgContext, format, out, false);
} else {
writeMessageFromTempData(out, msgContext);
}
} else {
messageFormatter.writeTo(msgContext, format, out, false);
}
}
out.close();
if (lstMetrics != null) {
lstMetrics.incrementMessagesSent();