Search in sources :

Example 6 with WebSocketProcessor

use of org.atmosphere.websocket.WebSocketProcessor in project atmosphere by Atmosphere.

the class WebSocketStreamingHandlerTest method invalidPathHandler.

@Test
public void invalidPathHandler() throws IOException, ServletException, ExecutionException, InterruptedException {
    ByteArrayOutputStream b = new ByteArrayOutputStream();
    final WebSocket w = new ArrayBaseWebSocket(b);
    final WebSocketProcessor processor = WebSocketProcessorFactory.getDefault().getWebSocketProcessor(framework);
    registerWebSocketHandler("/a", new EchoHandler());
    AtmosphereRequest request = new AtmosphereRequestImpl.Builder().destroyable(false).body("yoComet").pathInfo("/abcd").build();
    try {
        processor.open(w, request, AtmosphereResponseImpl.newInstance(framework.getAtmosphereConfig(), request, w));
        fail();
    } catch (Exception ex) {
        assertEquals(ex.getClass(), AtmosphereMappingException.class);
    }
}
Also used : WebSocketProcessor(org.atmosphere.websocket.WebSocketProcessor) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) WebSocket(org.atmosphere.websocket.WebSocket) Test(org.testng.annotations.Test)

Example 7 with WebSocketProcessor

use of org.atmosphere.websocket.WebSocketProcessor in project atmosphere by Atmosphere.

the class WebSocketHandlerTest method invalidPathHandler.

@Test
public void invalidPathHandler() throws IOException, ServletException, ExecutionException, InterruptedException {
    ByteArrayOutputStream b = new ByteArrayOutputStream();
    final WebSocket w = new ArrayBaseWebSocket(b);
    final WebSocketProcessor processor = WebSocketProcessorFactory.getDefault().getWebSocketProcessor(framework);
    registerWebSocketHandler("/a", new WebSocketProcessor.WebSocketHandlerProxy(new EchoHandler()));
    AtmosphereRequest request = new AtmosphereRequestImpl.Builder().destroyable(false).body("yoComet").pathInfo("/abcd").build();
    try {
        processor.open(w, request, AtmosphereResponseImpl.newInstance(framework.getAtmosphereConfig(), request, w));
        fail();
    } catch (Exception ex) {
        assertEquals(ex.getClass(), AtmosphereMappingException.class);
    }
}
Also used : WebSocketProcessor(org.atmosphere.websocket.WebSocketProcessor) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) WebSocket(org.atmosphere.websocket.WebSocket) Test(org.testng.annotations.Test)

Example 8 with WebSocketProcessor

use of org.atmosphere.websocket.WebSocketProcessor in project atmosphere by Atmosphere.

the class WebSocketHandlerTest method multipleWebSocketHandler.

@Test
public void multipleWebSocketHandler() throws IOException, ServletException, ExecutionException, InterruptedException {
    ByteArrayOutputStream b = new ByteArrayOutputStream();
    final WebSocket w = new ArrayBaseWebSocket(b);
    final WebSocketProcessor processor = WebSocketProcessorFactory.getDefault().getWebSocketProcessor(framework);
    registerWebSocketHandler("/a", new WebSocketProcessor.WebSocketHandlerProxy(new EchoHandler()));
    registerWebSocketHandler("/b", new WebSocketProcessor.WebSocketHandlerProxy(new EchoHandler()));
    AtmosphereRequest request = new AtmosphereRequestImpl.Builder().destroyable(false).body("a").pathInfo("/a").build();
    processor.open(w, request, AtmosphereResponseImpl.newInstance(framework.getAtmosphereConfig(), request, w));
    processor.invokeWebSocketProtocol(w, "a");
    assertEquals(b.toString(), "a");
    request = new AtmosphereRequestImpl.Builder().destroyable(false).body("b").pathInfo("/b").build();
    processor.open(w, request, AtmosphereResponseImpl.newInstance(framework.getAtmosphereConfig(), request, w));
    processor.invokeWebSocketProtocol(w, "b");
    // The WebSocketHandler is shared.
    assertEquals(b.toString(), "ab");
}
Also used : WebSocketProcessor(org.atmosphere.websocket.WebSocketProcessor) ByteArrayOutputStream(java.io.ByteArrayOutputStream) WebSocket(org.atmosphere.websocket.WebSocket) Test(org.testng.annotations.Test)

Example 9 with WebSocketProcessor

use of org.atmosphere.websocket.WebSocketProcessor in project atmosphere by Atmosphere.

the class WebSocketProcessorTest method basicWebSocketCookieTest.

@Test
public void basicWebSocketCookieTest() 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() {
        }
    });
    Set<Cookie> c = new HashSet<Cookie>();
    c.add(new Cookie("yo", "man"));
    AtmosphereRequest request = new AtmosphereRequestImpl.Builder().cookies(c).pathInfo("/a").build();
    processor.open(w, request, AtmosphereResponseImpl.newInstance(framework.getAtmosphereConfig(), request, w));
    r.get().getBroadcaster().broadcast("yo").get();
    assertNotNull(cValue.get());
    Cookie i = c.iterator().next();
    assertEquals(i.getName(), cValue.get().getName());
    assertEquals(i.getValue(), cValue.get().getValue());
}
Also used : Cookie(javax.servlet.http.Cookie) AtomicReference(java.util.concurrent.atomic.AtomicReference) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) WebSocket(org.atmosphere.websocket.WebSocket) WebSocketProcessor(org.atmosphere.websocket.WebSocketProcessor) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 10 with WebSocketProcessor

use of org.atmosphere.websocket.WebSocketProcessor in project atmosphere by Atmosphere.

the class WebSocketProcessorTest method encodeURLProxyTest.

@Test
public void encodeURLProxyTest() 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> url = new AtomicReference<String>();
    framework.addAtmosphereHandler("/*", new AtmosphereHandler() {

        @Override
        public void onRequest(AtmosphereResource resource) throws IOException {
            url.set(resource.getResponse().encodeRedirectURL("http://127.0.0.1:8080"));
        }

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

        @Override
        public void destroy() {
        }
    });
    AtmosphereRequest request = new AtmosphereRequestImpl.Builder().destroyable(false).body("yoComet").pathInfo("/a").build();
    processor.open(w, request, AtmosphereResponseImpl.newInstance(framework.getAtmosphereConfig(), request, w));
    processor.invokeWebSocketProtocol(w, "yoWebSocket");
    assertEquals(url.get(), "http://127.0.0.1:8080");
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) WebSocket(org.atmosphere.websocket.WebSocket) WebSocketProcessor(org.atmosphere.websocket.WebSocketProcessor) Test(org.testng.annotations.Test)

Aggregations

WebSocketProcessor (org.atmosphere.websocket.WebSocketProcessor)26 ByteArrayOutputStream (java.io.ByteArrayOutputStream)22 WebSocket (org.atmosphere.websocket.WebSocket)22 Test (org.testng.annotations.Test)21 IOException (java.io.IOException)12 AtomicReference (java.util.concurrent.atomic.AtomicReference)5 AtmosphereRequest (org.atmosphere.runtime.AtmosphereRequest)3 DefaultWebSocketProcessor (org.atmosphere.websocket.DefaultWebSocketProcessor)3 WebSocketEventListenerAdapter (org.atmosphere.websocket.WebSocketEventListenerAdapter)3 HashMap (java.util.HashMap)2 ExecutionException (java.util.concurrent.ExecutionException)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 ServletException (javax.servlet.ServletException)2 Cookie (javax.servlet.http.Cookie)2 AtmosphereRequestImpl (org.atmosphere.runtime.AtmosphereRequestImpl)2 WebSocketEventListener (org.atmosphere.websocket.WebSocketEventListener)2 Reader (java.io.Reader)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 ExecutorService (java.util.concurrent.ExecutorService)1