use of org.atmosphere.runtime.AtmosphereResourceImpl in project atmosphere by Atmosphere.
the class AbstractReflectorAtmosphereHandler method postStateChange.
/**
* Inspect the event and decide if the underlying connection must be resumed.
*
* @param event
*/
protected final void postStateChange(AtmosphereResourceEvent event) {
if (event.isCancelled() || event.isResuming())
return;
AtmosphereResourceImpl r = AtmosphereResourceImpl.class.cast(event.getResource());
// Between event.isCancelled and resource, the connection has been remotly closed.
if (r == null) {
logger.trace("Event {} returned a null AtmosphereResource", event);
return;
}
Boolean resumeOnBroadcast = r.resumeOnBroadcast();
if (!resumeOnBroadcast) {
// For legacy reason, check the attribute as well
Object o = r.getRequest(false).getAttribute(ApplicationConfig.RESUME_ON_BROADCAST);
if (o != null && Boolean.class.isAssignableFrom(o.getClass())) {
resumeOnBroadcast = Boolean.class.cast(o);
}
}
if (resumeOnBroadcast != null && resumeOnBroadcast) {
r.resume();
}
}
use of org.atmosphere.runtime.AtmosphereResourceImpl in project atmosphere by Atmosphere.
the class AtmosphereResourceLifecycleInterceptor method postInspect.
@Override
public void postInspect(final AtmosphereResource r) {
if (r.transport().equals(UNDEFINED) || Utils.webSocketMessage(r) || r.transport().equals(POLLING))
return;
AtmosphereResourceImpl impl = AtmosphereResourceImpl.class.cast(r);
if ((force || impl.getRequest(false).getMethod().equalsIgnoreCase(method)) && !impl.action().equals(Action.CANCELLED) && impl.isInScope()) {
logger.trace("Marking AtmosphereResource {} for suspend operation", r.uuid());
r.addEventListener(new OnBroadcast() {
@Override
public void onBroadcast(AtmosphereResourceEvent event) {
switch(r.transport()) {
case JSONP:
case AJAX:
case LONG_POLLING:
break;
default:
try {
r.getResponse().flushBuffer();
} catch (IOException e) {
logger.trace("", e);
}
break;
}
}
}).suspend(timeoutInMilli);
}
}
use of org.atmosphere.runtime.AtmosphereResourceImpl in project atmosphere by Atmosphere.
the class SimpleHttpProtocol method onMessage.
@Override
public List<AtmosphereRequest> onMessage(WebSocket webSocket, String message) {
AtmosphereResourceImpl resource = (AtmosphereResourceImpl) webSocket.resource();
if (resource == null) {
logger.trace("The WebSocket has been closed before the message was processed.");
return null;
}
AtmosphereRequest request = resource.getRequest(false);
request.setAttribute(FrameworkConfig.WEBSOCKET_SUBPROTOCOL, FrameworkConfig.SIMPLE_HTTP_OVER_WEBSOCKET);
if (!resource.isInScope())
return Collections.emptyList();
String pathInfo = request.getPathInfo();
String requestURI = request.getRequestURI();
// This confuse some JAXRS servers like RestEasy
if (rewriteUri && (requestURI.startsWith("http://") || requestURI.startsWith("https://"))) {
logger.debug("Rewriting requestURI {}. To disable, add {} set to true as init-param", requestURI, ApplicationConfig.REWRITE_WEBSOCKET_REQUESTURI);
requestURI = URI.create(requestURI).getPath();
request.requestURI(requestURI);
}
if (message.startsWith(delimiter)) {
int delimiterLength = delimiter.length();
int bodyBeginIndex = message.indexOf(delimiter, delimiterLength);
if (bodyBeginIndex != -1) {
pathInfo = message.substring(delimiterLength, bodyBeginIndex);
requestURI += pathInfo;
message = message.substring(bodyBeginIndex + delimiterLength);
}
}
List<AtmosphereRequest> list = new ArrayList<AtmosphereRequest>();
list.add(constructRequest(webSocket, pathInfo, requestURI, methodType, contentType.equalsIgnoreCase(TEXT) ? null : contentType, destroyable).body(message).build());
return list;
}
use of org.atmosphere.runtime.AtmosphereResourceImpl in project atmosphere by Atmosphere.
the class SimpleHttpProtocol method onMessage.
@Override
public List<AtmosphereRequest> onMessage(WebSocket webSocket, byte[] d, final int offset, final int length) {
//Converting to a string and delegating to onMessage(WebSocket webSocket, String d) causes issues because the binary data may not be a valid string.
AtmosphereResourceImpl resource = (AtmosphereResourceImpl) webSocket.resource();
if (resource == null) {
logger.trace("The WebSocket has been closed before the message was processed.");
return null;
}
AtmosphereRequest request = resource.getRequest(false);
request.setAttribute(FrameworkConfig.WEBSOCKET_SUBPROTOCOL, FrameworkConfig.SIMPLE_HTTP_OVER_WEBSOCKET);
if (!resource.isInScope())
return Collections.emptyList();
List<AtmosphereRequest> list = new ArrayList<AtmosphereRequest>();
list.add(constructRequest(webSocket, request.getPathInfo(), request.getRequestURI(), methodType, contentType.equalsIgnoreCase(TEXT) ? null : contentType, destroyable).body(d, offset, length).build());
return list;
}
use of org.atmosphere.runtime.AtmosphereResourceImpl in project atmosphere by Atmosphere.
the class StreamingHttpProtocol method onTextStream.
@Override
public List<AtmosphereRequest> onTextStream(WebSocket webSocket, Reader r) {
//Converting to a string and delegating to onMessage(WebSocket webSocket, String d) causes issues because the binary data may not be a valid string.
AtmosphereResourceImpl resource = (AtmosphereResourceImpl) webSocket.resource();
if (resource == null) {
logger.trace("The WebSocket has been closed before the message was processed.");
return null;
}
AtmosphereRequest request = resource.getRequest();
request.setAttribute(FrameworkConfig.WEBSOCKET_SUBPROTOCOL, FrameworkConfig.STREAMING_HTTP_OVER_WEBSOCKET);
List<AtmosphereRequest> list = new ArrayList<AtmosphereRequest>();
list.add(constructRequest(webSocket, request.getPathInfo(), request.getRequestURI(), methodType, contentType.equalsIgnoreCase(TEXT) ? null : contentType, destroyable).reader(r).build());
return list;
}
Aggregations