Search in sources :

Example 11 with AtmosphereRequest

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));
}
Also used : AtmosphereRequest(org.atmosphere.runtime.AtmosphereRequest) StringReader(java.io.StringReader) WebSocketEvent(org.atmosphere.websocket.WebSocketEventListener.WebSocketEvent) ServletException(javax.servlet.ServletException) AtmosphereMappingException(org.atmosphere.runtime.AtmosphereMappingException) IOException(java.io.IOException)

Example 12 with AtmosphereRequest

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);
            }
        }
    }
}
Also used : AtmosphereResponse(org.atmosphere.runtime.AtmosphereResponse) AtmosphereRequest(org.atmosphere.runtime.AtmosphereRequest) AtmosphereResourceImpl(org.atmosphere.runtime.AtmosphereResourceImpl) ServletException(javax.servlet.ServletException) AtmosphereMappingException(org.atmosphere.runtime.AtmosphereMappingException) IOException(java.io.IOException)

Example 13 with AtmosphereRequest

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;
}
Also used : AtmosphereRequestImpl(org.atmosphere.runtime.AtmosphereRequestImpl) AtmosphereRequest(org.atmosphere.runtime.AtmosphereRequest) AtmosphereResource(org.atmosphere.runtime.AtmosphereResource) AtmosphereResourceImpl(org.atmosphere.runtime.AtmosphereResourceImpl)

Example 14 with AtmosphereRequest

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");
}
Also used : AtmosphereRequestImpl(org.atmosphere.runtime.AtmosphereRequestImpl) AtmosphereRequest(org.atmosphere.runtime.AtmosphereRequest) Test(org.testng.annotations.Test)

Example 15 with AtmosphereRequest

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");
}
Also used : AtmosphereRequestImpl(org.atmosphere.runtime.AtmosphereRequestImpl) AtmosphereRequest(org.atmosphere.runtime.AtmosphereRequest) Test(org.testng.annotations.Test)

Aggregations

AtmosphereRequest (org.atmosphere.runtime.AtmosphereRequest)76 Test (org.testng.annotations.Test)39 AtmosphereRequestImpl (org.atmosphere.runtime.AtmosphereRequestImpl)38 IOException (java.io.IOException)23 AtmosphereResourceImpl (org.atmosphere.runtime.AtmosphereResourceImpl)20 AtmosphereResponse (org.atmosphere.runtime.AtmosphereResponse)18 ServletException (javax.servlet.ServletException)10 AsynchronousProcessor (org.atmosphere.runtime.AsynchronousProcessor)8 AtmosphereFramework (org.atmosphere.runtime.AtmosphereFramework)7 AtmosphereResource (org.atmosphere.runtime.AtmosphereResource)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 AsyncIOWriter (org.atmosphere.runtime.AsyncIOWriter)6 SimpleBroadcaster (org.atmosphere.util.SimpleBroadcaster)6 BeforeMethod (org.testng.annotations.BeforeMethod)6 ArrayList (java.util.ArrayList)5 AtmosphereInterceptorWriter (org.atmosphere.runtime.AtmosphereInterceptorWriter)5 AtmosphereResourceEvent (org.atmosphere.runtime.AtmosphereResourceEvent)5 Reader (java.io.Reader)4 Enumeration (java.util.Enumeration)4 ServletConfig (javax.servlet.ServletConfig)4