use of org.atmosphere.runtime.AtmosphereRequest in project atmosphere by Atmosphere.
the class DefaultWebSocketProcessor method invokeWebSocketProtocol.
@Override
public void invokeWebSocketProtocol(final WebSocket webSocket, String webSocketMessage) {
WebSocketHandlerProxy webSocketHandler = webSocketHandlerForMessage(webSocket);
if (webSocketHandler == null) {
if (!WebSocketProtocolStream.class.isAssignableFrom(webSocketProtocol.getClass())) {
List<AtmosphereRequest> list = webSocketProtocol.onMessage(webSocket, webSocketMessage);
dispatch(webSocket, list);
} else {
logger.debug("The WebServer doesn't support streaming. Wrapping the message as stream.");
invokeWebSocketProtocol(webSocket, new StringReader(webSocketMessage));
return;
}
} else {
if (!WebSocketStreamingHandler.class.isAssignableFrom(webSocketHandler.proxied().getClass())) {
try {
if (invokeInterceptors) {
invokeInterceptors(webSocketHandler, webSocket, webSocketMessage);
} else {
webSocketHandler.onTextMessage(webSocket, webSocketMessage);
}
} catch (Exception ex) {
handleException(ex, webSocket, webSocketHandler);
}
} else {
logger.debug("The WebServer doesn't support streaming. Wrapping the message as stream.");
invokeWebSocketProtocol(webSocket, new StringReader(webSocketMessage));
return;
}
}
notifyListener(webSocket, new WebSocketEventListener.WebSocketEvent(webSocketMessage, MESSAGE, webSocket));
}
use of org.atmosphere.runtime.AtmosphereRequest in project atmosphere by Atmosphere.
the class DefaultWebSocketProcessor method close.
@Override
public void close(final WebSocket webSocket, int closeCode) {
WebSocketHandler webSocketHandler = webSocket.webSocketHandler();
// A message might be in the process of being processed and the websocket gets closed. In that corner
// case the webSocket.resource will be set to false and that might cause NPE in some WebSocketProcol implementation
// We could potentially synchronize on webSocket but since it is a rare case, it is better to not synchronize.
// synchronized (webSocket) {
final AtmosphereResourceImpl resource = (AtmosphereResourceImpl) webSocket.resource();
if (resource == null) {
logger.trace("Already closed {}", webSocket);
} else {
final boolean allowedToClose = allowedCloseCode(closeCode);
final AtmosphereRequest r = resource.getRequest(false);
final AtmosphereResponse s = resource.getResponse(false);
boolean ff = r.getAttribute("firefox") != null;
boolean completeLifecycle = true;
try {
webSocketProtocol.onClose(webSocket);
if (webSocketHandler != null) {
webSocketHandler.onClose(webSocket);
}
logger.trace("About to close AtmosphereResource for {} with code {}", resource, closeCode);
if (!resource.getAtmosphereResourceEvent().isClosedByClient() && !resource.getAtmosphereResourceEvent().isClosedByApplication() && !resource.isCancelled()) {
// Better to call onDisconnect that onResume.
if (allowedToClose) {
if (ff || closingTime > 0) {
completeLifecycle = false;
logger.debug("Delaying closing operation for firefox and resource {}", resource.uuid());
ExecutorsFactory.getScheduler(framework.getAtmosphereConfig()).schedule(new Callable<Object>() {
@Override
public Object call() throws Exception {
executeClose(webSocket, 1005);
finish(webSocket, resource, r, s, !allowedToClose);
return null;
}
}, ff ? (closingTime == 0 ? 1000 : closingTime) : closingTime, TimeUnit.MILLISECONDS);
resource.getAndSetPendingClose();
} else {
executeClose(webSocket, closeCode);
}
} else {
logger.debug("Timeout {}", resource.uuid());
asynchronousProcessor.endRequest(AtmosphereResourceImpl.class.cast(webSocket.resource()), false);
}
} else {
logger.trace("Unable to properly complete {}", resource == null ? "null" : resource.uuid());
completeLifecycle = false;
}
} finally {
if (completeLifecycle) {
finish(webSocket, resource, r, s, !allowedToClose);
}
}
}
}
use of org.atmosphere.runtime.AtmosphereRequest in project atmosphere by Atmosphere.
the class ProtocolUtil method constructRequest.
protected static AtmosphereRequestImpl.Builder constructRequest(WebSocket webSocket, String pathInfo, String requestURI, String methodType, String contentType, boolean destroyable) {
AtmosphereResource resource = webSocket.resource();
AtmosphereRequest request = AtmosphereResourceImpl.class.cast(resource).getRequest(false);
Map<String, Object> m = attributes(webSocket, request);
// We need to create a new AtmosphereRequest as WebSocket message may arrive concurrently on the same connection.
AtmosphereRequestImpl.Builder b = (new AtmosphereRequestImpl.Builder().request(request).method(methodType).contentType(contentType == null ? request.getContentType() : contentType).attributes(m).pathInfo(pathInfo).contextPath(request.getContextPath()).servletPath(request.getServletPath()).requestURI(requestURI).requestURL(request.requestURL()).destroyable(destroyable).headers(request.headersMap()).session(resource.session()));
return b;
}
use of org.atmosphere.runtime.AtmosphereRequest in project atmosphere by Atmosphere.
the class EncoderDecoderTest method testMessage.
@Test
public void testMessage() throws IOException, ServletException, InterruptedException {
AtmosphereRequest request = new AtmosphereRequestImpl.Builder().pathInfo("/f").method("GET").build();
framework.doCometSupport(request, AtmosphereResponseImpl.newInstance());
assertNotNull(r.get());
r.get().resume();
assertNotNull(message.get());
assertEquals(message.get(), "message");
}
use of org.atmosphere.runtime.AtmosphereRequest in project atmosphere by Atmosphere.
the class EncoderDecoderTest method testDecoder.
@Test
public void testDecoder() throws IOException, ServletException, InterruptedException {
AtmosphereRequest request = new AtmosphereRequestImpl.Builder().pathInfo("/g").method("GET").build();
framework.doCometSupport(request, AtmosphereResponseImpl.newInstance());
assertNotNull(r.get());
r.get().resume();
assertNotNull(message.get());
assertEquals(message.get(), "message");
}
Aggregations