public boolean trySend(HttpResponse message) {
final HttpServletRequest request = ((ServletHttpRequest) message.getRequest()).request;
if (!request.isAsyncStarted())
return false;
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
if (message.shouldStartActor()) {
try {
message.getFrom().send(new HttpStreamOpened(new HttpStreamActor(request, response).ref(), message));
} catch (SuspendExecution e) {
throw new AssertionError(e);
}
} else {
out.close();
ctx.complete();
}
return true;
} catch (IOException ex) {
request.getServletContext().log("IOException", ex);
ctx.complete();
this.exception = ex;
return false;
}
}