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();
}
} catch (ProtocolException e) {
log.error(e + " (Synapse may be trying to send an exact response more than once )");
} catch (HttpException e) {
if (lstMetrics != null) {
lstMetrics.incrementFaultsSending();
}
handleException("Unexpected HTTP protocol error sending response to : " +
worker.getRemoteAddress(), e);
} catch (ConnectionClosedException e) {
if (lstMetrics != null) {
lstMetrics.incrementFaultsSending();
}
log.warn("Connection closed by client : " + worker.getRemoteAddress());
} catch (IllegalStateException e) {
if (lstMetrics != null) {
lstMetrics.incrementFaultsSending();
}
log.warn("Connection closed by client : " + worker.getRemoteAddress());
} catch (IOException e) {
if (lstMetrics != null) {
lstMetrics.incrementFaultsSending();
}
handleException("IO Error sending response message to : " +
worker.getRemoteAddress(), e);
} catch (Exception e) {
if (lstMetrics != null) {
lstMetrics.incrementFaultsSending();
}
handleException("General Error sending response message to : " +
worker.getRemoteAddress(), e);
}