final AsyncContext ctx = request.getAsyncContext();
final HttpServletResponse response = (HttpServletResponse) ctx.getResponse();
try {
if (message instanceof HttpResponse) {
final HttpResponse msg = (HttpResponse) message;
if (!response.isCommitted()) {
if (msg.getCookies() != null) {
for (Cookie wc : msg.getCookies())
response.addCookie(getServletCookie(wc));
}
if (msg.getHeaders() != null) {
for (Map.Entry<String, String> h : msg.getHeaders().entries())
response.addHeader(h.getKey(), h.getValue());
}
}
if (msg.getStatus() >= 400 && msg.getStatus() < 600) {
response.sendError(msg.getStatus(), msg.getError() != null ? msg.getError().toString() : null);
close();
return true;
}
if (msg.getRedirectPath() != null) {
response.sendRedirect(msg.getRedirectPath());
close();
return true;
}
if (!response.isCommitted()) {
response.setStatus(msg.getStatus());
if (msg.getContentType() != null)
response.setContentType(msg.getContentType());
if (msg.getCharacterEncoding() != null)
response.setCharacterEncoding(msg.getCharacterEncoding().name());
}
}
ServletOutputStream out = writeBody(message, response, !message.shouldStartActor());
out.flush(); // commits the response