use of org.atmosphere.runtime.AtmosphereRequest in project atmosphere by Atmosphere.
the class IdleResourceInterceptor method idleResources.
protected void idleResources() {
if (logger.isTraceEnabled()) {
logger.trace("{} monitoring {} AtmosphereResources", getClass().getSimpleName(), config.resourcesFactory().findAll().size());
}
for (AtmosphereResource r : config.resourcesFactory().findAll()) {
if (Utils.pollableTransport(r.transport())) {
continue;
}
AtmosphereRequest req = AtmosphereResourceImpl.class.cast(r).getRequest(false);
try {
if (req.getAttribute(MAX_INACTIVE) == null) {
logger.warn("Invalid state {}", r);
r.removeFromAllBroadcasters();
config.resourcesFactory().unRegisterUuidForFindCandidate(r);
continue;
}
long l = (Long) req.getAttribute(MAX_INACTIVE);
if (logger.isTraceEnabled() && l > 0) {
logger.trace("Expiring {} in {}", r.uuid(), System.currentTimeMillis() - l);
}
if (l > 0 && System.currentTimeMillis() - l > maxInactiveTime) {
try {
req.setAttribute(MAX_INACTIVE, (long) -1);
logger.debug("IdleResourceInterceptor disconnecting {}", r);
Future<?> f = (Future<?>) req.getAttribute(HeartbeatInterceptor.HEARTBEAT_FUTURE);
if (f != null)
f.cancel(false);
req.removeAttribute(HeartbeatInterceptor.HEARTBEAT_FUTURE);
WebSocket webSocket = AtmosphereResourceImpl.class.cast(r).webSocket();
if (webSocket != null) {
webSocket.close();
} else {
AsynchronousProcessor.class.cast(config.framework().getAsyncSupport()).endRequest(AtmosphereResourceImpl.class.cast(r), true);
}
} finally {
r.removeFromAllBroadcasters();
config.resourcesFactory().unRegisterUuidForFindCandidate(r);
}
}
} catch (Throwable e) {
logger.warn("IdleResourceInterceptor", e);
}
}
}
use of org.atmosphere.runtime.AtmosphereRequest in project atmosphere by Atmosphere.
the class PaddingAtmosphereInterceptor method writePadding.
private void writePadding(AtmosphereResponse response) {
AtmosphereRequest request = response.request();
if (request != null && request.getAttribute("paddingWritten") != null)
return;
if (response.resource() != null && response.resource().transport().equals(TRANSPORT.STREAMING)) {
request.setAttribute(FrameworkConfig.TRANSPORT_IN_USE, HeaderConfig.STREAMING_TRANSPORT);
response.setContentType("text/plain");
}
response.write(padding, true);
try {
response.flushBuffer();
} catch (IOException e) {
logger.trace("", e);
}
}
use of org.atmosphere.runtime.AtmosphereRequest in project atmosphere by Atmosphere.
the class PaddingAtmosphereInterceptor method inspect.
@Override
public Action inspect(final AtmosphereResource r) {
if (Utils.webSocketMessage(r))
return Action.CONTINUE;
final AtmosphereResponse response = r.getResponse();
final AtmosphereRequest request = r.getRequest();
String uuid = request.getHeader(HeaderConfig.X_ATMOSPHERE_TRACKING_ID);
boolean padding = r.transport().equals(TRANSPORT.STREAMING) || r.transport().equals(TRANSPORT.LONG_POLLING);
if (uuid != null && !uuid.equals("0") && r.transport().equals(TRANSPORT.WEBSOCKET) && request.getAttribute(INJECTED_ATMOSPHERE_RESOURCE) != null) {
padding = true;
}
if (padding) {
r.addEventListener(new ForcePreSuspend(response));
super.inspect(r);
AsyncIOWriter writer = response.getAsyncIOWriter();
if (AtmosphereInterceptorWriter.class.isAssignableFrom(writer.getClass())) {
AtmosphereInterceptorWriter.class.cast(writer).interceptor(new AsyncIOInterceptorAdapter() {
private void padding() {
if (!r.isSuspended()) {
writePadding(response);
request.setAttribute("paddingWritten", "true");
}
}
@Override
public void prePayload(AtmosphereResponse response, byte[] data, int offset, int length) {
padding();
}
@Override
public void postPayload(AtmosphereResponse response, byte[] data, int offset, int length) {
}
});
} else {
logger.warn("Unable to apply {}. Your AsyncIOWriter must implement {}", getClass().getName(), AtmosphereInterceptorWriter.class.getName());
}
}
return Action.CONTINUE;
}
use of org.atmosphere.runtime.AtmosphereRequest in project atmosphere by Atmosphere.
the class SSEAtmosphereInterceptor method inspect.
@Override
public Action inspect(final AtmosphereResource r) {
if (Utils.webSocketMessage(r))
return Action.CONTINUE;
final AtmosphereResponse response = r.getResponse();
final AtmosphereRequest request = r.getRequest();
String accept = request.getHeader("Accept") == null ? "text/plain" : request.getHeader("Accept").trim();
if (r.transport().equals(AtmosphereResource.TRANSPORT.SSE) || contentType.equalsIgnoreCase(accept)) {
super.inspect(r);
r.addEventListener(new P(response));
AsyncIOWriter writer = response.getAsyncIOWriter();
if (AtmosphereInterceptorWriter.class.isAssignableFrom(writer.getClass())) {
AtmosphereInterceptorWriter.class.cast(writer).interceptor(new AsyncIOInterceptorAdapter() {
private boolean padding() {
if (!r.isSuspended()) {
return writePadding(response);
}
return false;
}
@Override
public void prePayload(AtmosphereResponse response, byte[] data, int offset, int length) {
boolean noPadding = padding();
// In that case, we must pad/protocol indenendently of the state of the AtmosphereResource
if (!noPadding || r.getRequest().getAttribute(CALLBACK_JAVASCRIPT_PROTOCOL) != null) {
// write other meta info such as (id, event, etc)?
response.write(DATA, true);
}
}
@Override
public byte[] transformPayload(AtmosphereResponse response, byte[] responseDraft, byte[] data) throws IOException {
boolean noPadding = padding();
// In that case, we must pad/protocol indenendently of the state of the AtmosphereResource
if (!noPadding || r.getRequest().getAttribute(CALLBACK_JAVASCRIPT_PROTOCOL) != null) {
if (isMultilineData(responseDraft)) {
// return a padded multiline-data
return encodeMultilineData(responseDraft);
}
}
return responseDraft;
}
@Override
public void postPayload(AtmosphereResponse response, byte[] data, int offset, int length) {
// In that case, we must pad/protocol indenendently of the state of the AtmosphereResource
if (r.isSuspended() || r.getRequest().getAttribute(CALLBACK_JAVASCRIPT_PROTOCOL) != null || r.getRequest().getAttribute(CONTAINER_RESPONSE) != null) {
response.write(END, true);
}
/**
* When used with https://github.com/remy/polyfills/blob/master/EventSource.js , we
* resume after every message.
*/
String ua = r.getRequest().getHeader("User-Agent");
if (ua != null && ua.contains("MSIE")) {
try {
response.flushBuffer();
} catch (IOException e) {
logger.trace("", e);
}
r.resume();
}
}
});
} else {
logger.warn("Unable to apply {}. Your AsyncIOWriter must implement {}", getClass().getName(), AtmosphereInterceptorWriter.class.getName());
}
}
return Action.CONTINUE;
}
use of org.atmosphere.runtime.AtmosphereRequest in project atmosphere by Atmosphere.
the class BroadcastOnPostAtmosphereInterceptor method postInspect.
@Override
public void postInspect(AtmosphereResource r) {
if (r.getRequest().getMethod().equalsIgnoreCase("POST")) {
AtmosphereRequest request = r.getRequest();
Object o;
try {
o = readEntirely(r);
} catch (IOException e) {
logger.warn("", e);
return;
}
if (isBodyEmpty(o)) {
logger.warn("{} received an empty body", request);
return;
}
r.getBroadcaster().broadcast(o);
}
}
Aggregations