Package org.atmosphere.websocket

Examples of org.atmosphere.websocket.WebSocketProcessor


    @Test
    public void onCloseAtmosphereRequestAttribute() throws IOException, ServletException, ExecutionException, InterruptedException {
        ByteArrayOutputStream b = new ByteArrayOutputStream();
        final WebSocket w = new ArrayBaseWebSocket(b);
        final WebSocketProcessor processor = WebSocketProcessorFactory.getDefault()
                .getWebSocketProcessor(framework);
        final AtomicReference<String> uuid = new AtomicReference<String>();

        framework.addAtmosphereHandler("/*", new AtmosphereHandler() {

            @Override
            public void onRequest(AtmosphereResource resource) throws IOException {
                resource.addEventListener(new WebSocketEventListenerAdapter() {
                    @Override
                    public void onClose(WebSocketEvent event) {
                        uuid.set((String) event.webSocket().resource().getRequest().getAttribute(SUSPENDED_ATMOSPHERE_RESOURCE_UUID));
                    }
                });
            }

            @Override
            public void onStateChange(AtmosphereResourceEvent event) throws IOException {
            }

            @Override
            public void destroy() {
            }
        });

        AtmosphereRequest request = new AtmosphereRequest.Builder().destroyable(false).body("yoComet").pathInfo("/a").build();
        processor.open(w, request, AtmosphereResponse.newInstance(framework.getAtmosphereConfig(), request, w));
        processor.invokeWebSocketProtocol(w, "yoWebSocket");
        processor.notifyListener(w, new WebSocketEventListener.WebSocketEvent("Close", WebSocketEventListener.WebSocketEvent.TYPE.CLOSE, w));

        assertNotNull(uuid.get());
        assertEquals(uuid.get(), request.getAttribute(SUSPENDED_ATMOSPHERE_RESOURCE_UUID));
    }
View Full Code Here


    @Test
    public void basicProgrammaticAPIWorkflow() throws IOException, ServletException, ExecutionException, InterruptedException {
        ByteArrayOutputStream b = new ByteArrayOutputStream();
        final WebSocket w = new ArrayBaseWebSocket(b);
        final WebSocketProcessor processor = WebSocketProcessorFactory.getDefault()
                .getWebSocketProcessor(framework);

        framework.addWebSocketHandler("/*", new WebSocketProcessor.WebSocketHandlerProxy(new WebSocketHandlerAdapter() {

            @Override
            public void onTextMessage(WebSocket webSocket, String data) throws IOException {
                webSocket.write(data);
            }

            @Override
            public void onOpen(WebSocket webSocket) throws IOException {
                webSocket.write(webSocket.resource().getRequest().getReader().readLine());
            }
        }));

        AtmosphereRequest request = new AtmosphereRequest.Builder().destroyable(false).body("yoComet").pathInfo("/a").build();
        processor.open(w, request, AtmosphereResponse.newInstance(framework.getAtmosphereConfig(), request, w));
        processor.invokeWebSocketProtocol(w, "yoWebSocket");
        framework.getBroadcasterFactory().lookup("/*").broadcast("yoBroadcast").get();

        assertEquals(b.toString(), "yoCometyoWebSocketyoBroadcast");

    }
View Full Code Here

    public void undetectedCloseWebSocketTest() throws IOException, ServletException, ExecutionException, InterruptedException {
        final AtomicReference<Cookie> cValue = new AtomicReference<Cookie>();
        final AtomicReference<AtmosphereResource> r = new AtomicReference<AtmosphereResource>();
        ByteArrayOutputStream b = new ByteArrayOutputStream();
        WebSocket w = new ArrayBaseWebSocket(b);
        final WebSocketProcessor processor = WebSocketProcessorFactory.getDefault()
                .getWebSocketProcessor(framework);

        framework.addAtmosphereHandler("/*", new AtmosphereHandler() {

            @Override
            public void onRequest(AtmosphereResource resource) throws IOException {
                r.set(resource);
                resource.getBroadcaster().addAtmosphereResource(resource);
            }

            @Override
            public void onStateChange(AtmosphereResourceEvent event) throws IOException {
                Cookie[] c = event.getResource().getRequest().getCookies();
                cValue.set(c[0]);
            }

            @Override
            public void destroy() {
            }
        });
        Map<String,String> m = new HashMap<String, String>();
        m.put(HeaderConfig.X_ATMOSPHERE_TRANSPORT, HeaderConfig.WEBSOCKET_TRANSPORT);

        AtmosphereRequest request = new AtmosphereRequest.Builder().headers(m).pathInfo("/a").build();
        request.setAttribute(FrameworkConfig.WEBSOCKET_MESSAGE, null);
        processor.open(w, request, AtmosphereResponse.newInstance(framework.getAtmosphereConfig(), request, w));

        final AtomicBoolean dirtyDisconnect = new AtomicBoolean();
        request.setAttribute(SUSPENDED_ATMOSPHERE_RESOURCE_UUID, w.resource().uuid());
        m.put(HeaderConfig.X_ATMOSPHERE_TRANSPORT, HeaderConfig.WEBSOCKET_TRANSPORT);
        request.headers(m);
        AtmosphereResource dup = framework.getAtmosphereConfig().resourcesFactory()
                .create(framework.config, w.resource().uuid(), request).suspend();
        w.resource(dup);
        dup.addEventListener(new AtmosphereResourceEventListenerAdapter.OnDisconnect() {
            @Override
            public void onDisconnect(AtmosphereResourceEvent event) {
                if (event.isCancelled()) dirtyDisconnect.set(true);
            }
        });
        request.setAttribute(FrameworkConfig.WEBSOCKET_MESSAGE, null);

        processor.open(w, request, AtmosphereResponse.newInstance(framework.getAtmosphereConfig(), request, w));

        r.get().getBroadcaster().broadcast("yo").get();
        assertTrue(dirtyDisconnect.get());
    }
View Full Code Here

    public void testManagedWebSocketPathMessage() throws IOException, ServletException {
        instanceCount = 0;

        ByteArrayOutputStream b = new ByteArrayOutputStream();
        final WebSocket w = new ArrayBaseWebSocket(b);
        final WebSocketProcessor processor = WebSocketProcessorFactory.getDefault()
                .getWebSocketProcessor(framework);

        AtmosphereRequest request = new AtmosphereRequest.Builder().pathInfo("/ws/bar").method("GET").build();
        processor.open(w, request, AtmosphereResponse.newInstance(framework.getAtmosphereConfig(), request, w));
        assertEquals(instanceCount, 1);
        assertNotNull(r.get());
        assertEquals(r.get(), "/ws/bar");
    }
View Full Code Here

    public void testSingletonManagedWebSocketPathMessage() throws IOException, ServletException {
        instanceCount = 0;

        ByteArrayOutputStream b = new ByteArrayOutputStream();
        final WebSocket w = new ArrayBaseWebSocket(b);
        final WebSocketProcessor processor = WebSocketProcessorFactory.getDefault()
                .getWebSocketProcessor(framework);

        AtmosphereRequest request = new AtmosphereRequest.Builder().pathInfo("/singleton/ws/bar").method("GET").build();
        processor.open(w, request, AtmosphereResponse.newInstance(framework.getAtmosphereConfig(), request, w));
        assertEquals(instanceCount, 0);
        assertNotNull(r.get());
        assertEquals(r.get(), "/singleton/ws/bar");
    }
View Full Code Here

    private static final Logger logger = LoggerFactory.getLogger(JettyAsyncSupportWithWebSocket.class);
    private final WebSocketFactory webSocketFactory;

    public JettyAsyncSupportWithWebSocket(final AtmosphereConfig config) {
        super(config);
        final WebSocketProcessor webSocketProcessor = WebSocketProcessorFactory.getDefault().getWebSocketProcessor(config.framework());

        WebSocketFactory wsf;
        try {
            String[] jettyVersion = config.getServletContext().getServerInfo().substring(6).split("\\.");
            if (Integer.valueOf(jettyVersion[0]) > 7 || Integer.valueOf(jettyVersion[0]) == 7 && Integer.valueOf(jettyVersion[1]) > 4) {
View Full Code Here

    private static final Logger logger = LoggerFactory.getLogger(JettyServlet30AsyncSupportWithWebSocket.class);
    private final WebSocketFactory webSocketFactory;

    public JettyServlet30AsyncSupportWithWebSocket(final AtmosphereConfig config) {
        super(config);
        final WebSocketProcessor webSocketProcessor = WebSocketProcessorFactory.getDefault().getWebSocketProcessor(config.framework());

        webSocketFactory = JettyWebSocketUtil.getFactory(config, webSocketProcessor);
    }
View Full Code Here

            }
        } catch (Exception ex) {
            logger.warn("", ex);
        }

        final WebSocketProcessor webSocketProcessor = WebSocketProcessorFactory.getDefault().getWebSocketProcessor(config.framework());

        webSocketFactory = new WebSocketServerFactory(policy) {
            @Override
            public boolean acceptWebSocket(final HttpServletRequest request, final HttpServletResponse response) throws IOException {
                setCreator(new WebSocketCreator() {

                    // @Override  9.0.x
                    public Object createWebSocket(UpgradeRequest upgradeRequest, UpgradeResponse upgradeResponse) {

                        ServletWebSocketRequest r = ServletWebSocketRequest.class.cast(upgradeRequest);
                        r.getExtensions().clear();

                        if (!webSocketProcessor.handshake(request)) {
                            try {
                                response.sendError(HttpServletResponse.SC_FORBIDDEN, "WebSocket requests rejected.");
                            } catch (IOException e) {
                                logger.trace("", e);
                            }
                            return null;
                        }

                        return new Jetty9WebSocketHandler(request, config.framework(), webSocketProcessor);
                    }

                    // @Override 9.1.x
                    public Object createWebSocket(ServletUpgradeRequest req, ServletUpgradeResponse resp) {
                        req.getExtensions().clear();

                        if (!webSocketProcessor.handshake(request)) {
                            try {
                                response.sendError(HttpServletResponse.SC_FORBIDDEN, "WebSocket requests rejected.");
                            } catch (IOException e) {
                                logger.trace("", e);
                            }
View Full Code Here

     *
     * @param framework {@link AtmosphereFramework}
     * @return an instance of {@link WebSocketProcessor}
     */
    public WebSocketProcessor getWebSocketProcessor(AtmosphereFramework framework) {
        WebSocketProcessor processor = processors.get(framework);
        if (processor == null) {
            synchronized (framework) {
                processor = createProcessor(framework);
                processors.put(framework, processor);
            }
View Full Code Here

        }
        processors.clear();
    }

    private WebSocketProcessor createProcessor(AtmosphereFramework framework) {
        WebSocketProcessor processor = null;

        String webSocketProcessorName = framework.getWebSocketProcessorClassName();
        if (!webSocketProcessorName.equalsIgnoreCase(DefaultWebSocketProcessor.class.getName())) {
            try {
                processor =  framework.newClassInstance(WebSocketProcessor.class,
                        (Class<WebSocketProcessor>) IOUtils.loadClass(getClass(), webSocketProcessorName));
            } catch (Exception ex) {
                logger.error("Unable to create {}", webSocketProcessorName);
                processor = new DefaultWebSocketProcessor();
            }
        }

        if (processor == null) {
            processor = new DefaultWebSocketProcessor();
        }
        processor.configure(framework.getAtmosphereConfig());

        return processor;
    }
View Full Code Here

TOP

Related Classes of org.atmosphere.websocket.WebSocketProcessor

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.