// Force the status code to 200 events independently of the value of the entity (null or not)
if (response.getStatus() == 204) {
response.setStatus(200);
}
BroadcasterFactory broadcasterFactory = (BroadcasterFactory) servletReq
.getAttribute(AtmosphereServlet.BROADCASTER_FACTORY);
// Do not add location header if already there.
if (!sessionSupported && !resumeOnBroadcast && response.getHttpHeaders().getFirst("Location") == null) {
String uuid = UUID.randomUUID().toString();
response.getHttpHeaders().putSingle(
HttpHeaders.LOCATION,
uriInfo.getAbsolutePathBuilder().path(uuid).build(""));
resumeCandidates.put(uuid, r);
servletReq.setAttribute(RESUME_UUID, uuid);
servletReq.setAttribute(RESUME_CANDIDATES, resumeCandidates);
}
if (bc == null) {
bc = r.getBroadcaster();
}
if (sessionSupported && servletReq.getSession().getAttribute(SUSPENDED_RESOURCE) != null) {
AtmosphereResource<HttpServletRequest, HttpServletResponse> cached =
(AtmosphereResource) servletReq.getSession().getAttribute(SUSPENDED_RESOURCE);
bc = cached.getBroadcaster();
// Just in case something went wrong.
bc.removeAtmosphereResource(cached);
}
if (response.getEntity() instanceof Broadcastable) {
Broadcastable b = (Broadcastable) response.getEntity();
bc = b.getBroadcaster();
response.setEntity(b.getResponseMessage());
if ((scope == Suspend.SCOPE.REQUEST) && (bc.getScope() != Broadcaster.SCOPE.REQUEST)) {
bc.setScope(Broadcaster.SCOPE.REQUEST);
}
} else if ((scope == Suspend.SCOPE.REQUEST) && (bc.getScope() != Broadcaster.SCOPE.REQUEST)) {
try {
String id = bc.getID();
bc.setID(bc.getClass().getSimpleName() + "-" + UUID.randomUUID());
// Re-generate a new one with proper scope.
bc = broadcasterFactory.get();
bc.setScope(Broadcaster.SCOPE.REQUEST);
bc.setID(id);
}
catch (Exception ex) {
logger.error("failed to instantiate broadcaster with factory: " + broadcasterFactory, ex);