Search in sources :

Example 56 with AtmosphereRequest

use of org.atmosphere.runtime.AtmosphereRequest in project atmosphere by Atmosphere.

the class DefaultWebSocketProcessor method invokeInterceptors.

private void invokeInterceptors(WebSocketHandlerProxy webSocketHandler, WebSocket webSocket, Object webSocketMessageAsBody, int offset, int length) throws IOException {
    AtmosphereResourceImpl resource = AtmosphereResourceImpl.class.cast(webSocket.resource());
    if (resource == null) {
        return;
    }
    AtmosphereRequest request = resource.getRequest(false);
    if (String.class.isAssignableFrom(webSocketMessageAsBody.getClass())) {
        request.body(String.class.cast(webSocketMessageAsBody));
    } else if (Reader.class.isAssignableFrom(webSocketMessageAsBody.getClass())) {
        request.body(Reader.class.cast(webSocketMessageAsBody));
    } else if (InputStream.class.isAssignableFrom(webSocketMessageAsBody.getClass())) {
        request.body(InputStream.class.cast(webSocketMessageAsBody));
    } else {
        request.body(new ByteArrayInputStream((byte[]) webSocketMessageAsBody, offset, length));
    }
    String path = webSocketHandler.proxied.getClass().isAnnotationPresent(WebSocketHandlerService.class) ? webSocketHandler.proxied.getClass().getAnnotation(WebSocketHandlerService.class).path() : "/";
    AtmosphereFramework.AtmosphereHandlerWrapper w = framework.getAtmosphereHandlers().get(framework.normalizePath(path));
    List<AtmosphereInterceptor> l;
    if (w == null) {
        l = framework.interceptors();
    } else {
        l = w.interceptors;
    }
    try {
        // Globally defined
        int tracing = 0;
        Action a = asynchronousProcessor.invokeInterceptors(l, resource, tracing);
        if (a.type() != Action.TYPE.CONTINUE && a.type() != Action.TYPE.SKIP_ATMOSPHEREHANDLER) {
            return;
        }
        //Unit test mock the request and will throw NPE.
        boolean skipAtmosphereHandler = request.getAttribute(SKIP_ATMOSPHEREHANDLER.name()) != null ? (Boolean) request.getAttribute(SKIP_ATMOSPHEREHANDLER.name()) : Boolean.FALSE;
        if (!skipAtmosphereHandler) {
            try {
                if (String.class.isAssignableFrom(webSocketMessageAsBody.getClass())) {
                    webSocketHandler.onTextMessage(webSocket, String.class.cast(webSocketMessageAsBody));
                } else if (Reader.class.isAssignableFrom(webSocketMessageAsBody.getClass())) {
                    WebSocketStreamingHandler.class.cast(webSocketHandler.proxied).onTextStream(webSocket, Reader.class.cast(webSocketMessageAsBody));
                } else if (InputStream.class.isAssignableFrom(webSocketMessageAsBody.getClass())) {
                    WebSocketStreamingHandler.class.cast(webSocketHandler.proxied()).onBinaryStream(webSocket, InputStream.class.cast(webSocketMessageAsBody));
                } else {
                    webSocketHandler.onByteMessage(webSocket, (byte[]) webSocketMessageAsBody, offset, length);
                }
            } catch (IOException t) {
                resource.onThrowable(t);
                throw t;
            }
        }
        request.setAttribute(SKIP_ATMOSPHEREHANDLER.name(), Boolean.FALSE);
    } finally {
        asynchronousProcessor.postInterceptors(l, resource);
    }
}
Also used : AtmosphereInterceptor(org.atmosphere.runtime.AtmosphereInterceptor) Action(org.atmosphere.runtime.Action) WebSocketHandlerService(org.atmosphere.config.service.WebSocketHandlerService) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Reader(java.io.Reader) StringReader(java.io.StringReader) IOException(java.io.IOException) AtmosphereRequest(org.atmosphere.runtime.AtmosphereRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) AtmosphereFramework(org.atmosphere.runtime.AtmosphereFramework) AtmosphereResourceImpl(org.atmosphere.runtime.AtmosphereResourceImpl)

Example 57 with AtmosphereRequest

use of org.atmosphere.runtime.AtmosphereRequest in project atmosphere by Atmosphere.

the class DefaultWebSocketProcessor method invokeWebSocketProtocol.

@Override
public void invokeWebSocketProtocol(WebSocket webSocket, byte[] data, int offset, int length) {
    WebSocketHandlerProxy webSocketHandler = webSocketHandlerForMessage(webSocket);
    if (webSocketHandler == null) {
        if (!WebSocketProtocolStream.class.isAssignableFrom(webSocketProtocol.getClass())) {
            List<AtmosphereRequest> list = webSocketProtocol.onMessage(webSocket, data, offset, length);
            dispatch(webSocket, list);
        } else {
            logger.debug("The WebServer doesn't support streaming. Wrapping the message as stream.");
            invokeWebSocketProtocol(webSocket, new ByteArrayInputStream(data, offset, length));
            return;
        }
    } else {
        if (!WebSocketStreamingHandler.class.isAssignableFrom(webSocketHandler.proxied().getClass())) {
            try {
                if (invokeInterceptors) {
                    invokeInterceptors(webSocketHandler, webSocket, data, offset, length);
                } else {
                    webSocketHandler.onByteMessage(webSocket, data, offset, length);
                }
            } catch (Exception ex) {
                handleException(ex, webSocket, webSocketHandler);
            }
        } else {
            logger.debug("The WebServer doesn't support streaming. Wrapping the message as stream.");
            invokeWebSocketProtocol(webSocket, new ByteArrayInputStream(data, offset, length));
            return;
        }
    }
    notifyListener(webSocket, new WebSocketEventListener.WebSocketEvent<byte[]>(data, MESSAGE, webSocket));
}
Also used : AtmosphereRequest(org.atmosphere.runtime.AtmosphereRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) ServletException(javax.servlet.ServletException) AtmosphereMappingException(org.atmosphere.runtime.AtmosphereMappingException) IOException(java.io.IOException)

Example 58 with AtmosphereRequest

use of org.atmosphere.runtime.AtmosphereRequest in project atmosphere by Atmosphere.

the class PathTest method testSingletonManaged.

@Test
public void testSingletonManaged() throws IOException, ServletException {
    instanceCount = 0;
    AtmosphereRequest request = new AtmosphereRequestImpl.Builder().pathInfo("/singleton/managed/yes").method("GET").build();
    framework.doCometSupport(request, AtmosphereResponseImpl.newInstance());
    assertEquals(instanceCount, 0);
    assertNotNull(r.get());
    assertEquals(r.get(), "/singleton/managed/yes");
}
Also used : AtmosphereRequestImpl(org.atmosphere.runtime.AtmosphereRequestImpl) AtmosphereRequest(org.atmosphere.runtime.AtmosphereRequest) Test(org.testng.annotations.Test)

Example 59 with AtmosphereRequest

use of org.atmosphere.runtime.AtmosphereRequest in project atmosphere by Atmosphere.

the class ManagedAtmosphereHandlerTest method create.

@BeforeMethod
public void create() throws Throwable {
    framework = new AtmosphereFramework();
    framework.setDefaultBroadcasterClassName(SimpleBroadcaster.class.getName());
    framework.addAnnotationPackage(BroadcasterCacheTest.class);
    framework.setAsyncSupport(new AsynchronousProcessor(framework.getAtmosphereConfig()) {

        @Override
        public Action service(AtmosphereRequest req, AtmosphereResponse res) throws IOException, ServletException {
            return suspended(req, res);
        }

        public void action(AtmosphereResourceImpl r) {
            try {
                resumed(r.getRequest(), r.getResponse());
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ServletException e) {
                e.printStackTrace();
            }
        }
    }).init(new ServletConfig() {

        @Override
        public String getServletName() {
            return "void";
        }

        @Override
        public ServletContext getServletContext() {
            return mock(ServletContext.class);
        }

        @Override
        public String getInitParameter(String name) {
            return null;
        }

        @Override
        public Enumeration<String> getInitParameterNames() {
            return null;
        }
    });
}
Also used : AtmosphereResponse(org.atmosphere.runtime.AtmosphereResponse) Enumeration(java.util.Enumeration) ServletConfig(javax.servlet.ServletConfig) IOException(java.io.IOException) SimpleBroadcaster(org.atmosphere.util.SimpleBroadcaster) ServletException(javax.servlet.ServletException) AtmosphereRequest(org.atmosphere.runtime.AtmosphereRequest) AsynchronousProcessor(org.atmosphere.runtime.AsynchronousProcessor) AtmosphereFramework(org.atmosphere.runtime.AtmosphereFramework) ServletContext(javax.servlet.ServletContext) AtmosphereResourceImpl(org.atmosphere.runtime.AtmosphereResourceImpl) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 60 with AtmosphereRequest

use of org.atmosphere.runtime.AtmosphereRequest in project atmosphere by Atmosphere.

the class CustomAnnotationTest method create.

@BeforeMethod
public void create() throws Throwable {
    framework = new AtmosphereFramework();
    framework.setDefaultBroadcasterClassName(SimpleBroadcaster.class.getName());
    framework.addAnnotationPackage(MyAnnotation.class);
    framework.setAsyncSupport(new AsynchronousProcessor(framework.getAtmosphereConfig()) {

        @Override
        public Action service(AtmosphereRequest req, AtmosphereResponse res) throws IOException, ServletException {
            return suspended(req, res);
        }

        public void action(AtmosphereResourceImpl r) {
            try {
                resumed(r.getRequest(), r.getResponse());
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ServletException e) {
                e.printStackTrace();
            }
        }
    }).init(new ServletConfig() {

        @Override
        public String getServletName() {
            return "void";
        }

        @Override
        public ServletContext getServletContext() {
            return mock(ServletContext.class);
        }

        @Override
        public String getInitParameter(String name) {
            return null;
        }

        @Override
        public Enumeration<String> getInitParameterNames() {
            return null;
        }
    });
}
Also used : AtmosphereResponse(org.atmosphere.runtime.AtmosphereResponse) Enumeration(java.util.Enumeration) ServletConfig(javax.servlet.ServletConfig) IOException(java.io.IOException) SimpleBroadcaster(org.atmosphere.util.SimpleBroadcaster) ServletException(javax.servlet.ServletException) AtmosphereRequest(org.atmosphere.runtime.AtmosphereRequest) AsynchronousProcessor(org.atmosphere.runtime.AsynchronousProcessor) AtmosphereFramework(org.atmosphere.runtime.AtmosphereFramework) ServletContext(javax.servlet.ServletContext) AtmosphereResourceImpl(org.atmosphere.runtime.AtmosphereResourceImpl) BeforeMethod(org.testng.annotations.BeforeMethod)

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