Search in sources :

Example 1 with AbstractReflectorAtmosphereHandler

use of org.atmosphere.handler.AbstractReflectorAtmosphereHandler in project atmosphere by Atmosphere.

the class AtmosphereFramework method checkWebSocketSupportState.

public void checkWebSocketSupportState() {
    if (atmosphereHandlers.isEmpty() && !SimpleHttpProtocol.class.isAssignableFrom(webSocketProtocol.getClass())) {
        logger.debug("Adding a void AtmosphereHandler mapped to /* to allow WebSocket application only");
        addAtmosphereHandler(Broadcaster.ROOT_MASTER, new AbstractReflectorAtmosphereHandler() {

            @Override
            public void onRequest(AtmosphereResource r) throws IOException {
                logger.debug("No AtmosphereHandler defined.");
                if (!r.transport().equals(AtmosphereResource.TRANSPORT.WEBSOCKET)) {
                    WebSocket.notSupported(r.getRequest(), r.getResponse());
                }
            }

            @Override
            public void destroy() {
            }
        });
    }
}
Also used : AbstractReflectorAtmosphereHandler(org.atmosphere.handler.AbstractReflectorAtmosphereHandler) IOException(java.io.IOException)

Example 2 with AbstractReflectorAtmosphereHandler

use of org.atmosphere.handler.AbstractReflectorAtmosphereHandler in project atmosphere by Atmosphere.

the class AtmosphereRequestTest method testNormalQueryStringBuilder.

@Test
public void testNormalQueryStringBuilder() throws IOException, ServletException {
    framework.addAtmosphereHandler("/a", new AbstractReflectorAtmosphereHandler() {

        @Override
        public void onRequest(AtmosphereResource resource) throws IOException {
        }

        @Override
        public void destroy() {
        }
    });
    AtmosphereRequest request = new AtmosphereRequestImpl.Builder().pathInfo("/a").build();
    request.queryString("a=b");
    final AtomicReference<String> e = new AtomicReference<String>();
    framework.interceptor(new AtmosphereInterceptor() {

        @Override
        public void configure(AtmosphereConfig config) {
        }

        @Override
        public Action inspect(AtmosphereResource r) {
            e.set(r.getRequest().getQueryString());
            return Action.CANCELLED;
        }

        @Override
        public void postInspect(AtmosphereResource r) {
        }

        @Override
        public void destroy() {
        }
    });
    framework.doCometSupport(request, AtmosphereResponseImpl.newInstance());
    assertEquals(e.get(), "a=b");
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) AbstractReflectorAtmosphereHandler(org.atmosphere.handler.AbstractReflectorAtmosphereHandler) IOException(java.io.IOException) Test(org.testng.annotations.Test)

Example 3 with AbstractReflectorAtmosphereHandler

use of org.atmosphere.handler.AbstractReflectorAtmosphereHandler in project atmosphere by Atmosphere.

the class AtmosphereRequestTest method testStrinpQueryStringBuilder.

@Test
public void testStrinpQueryStringBuilder() throws IOException, ServletException {
    framework.addAtmosphereHandler("/a", new AbstractReflectorAtmosphereHandler() {

        @Override
        public void onRequest(AtmosphereResource resource) throws IOException {
        }

        @Override
        public void destroy() {
        }
    });
    AtmosphereRequest request = new AtmosphereRequestImpl.Builder().pathInfo("/a").build();
    request.queryString("a=b&X-Atmosphere-Transport=websocket");
    final AtomicReference<String> e = new AtomicReference<String>();
    framework.interceptor(new AtmosphereInterceptor() {

        @Override
        public void configure(AtmosphereConfig config) {
        }

        @Override
        public Action inspect(AtmosphereResource r) {
            e.set(r.getRequest().getQueryString());
            return Action.CANCELLED;
        }

        @Override
        public void postInspect(AtmosphereResource r) {
        }

        @Override
        public void destroy() {
        }
    });
    framework.doCometSupport(request, AtmosphereResponseImpl.newInstance());
    assertEquals(e.get(), "a=b");
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) AbstractReflectorAtmosphereHandler(org.atmosphere.handler.AbstractReflectorAtmosphereHandler) IOException(java.io.IOException) Test(org.testng.annotations.Test)

Example 4 with AbstractReflectorAtmosphereHandler

use of org.atmosphere.handler.AbstractReflectorAtmosphereHandler in project atmosphere by Atmosphere.

the class AtmosphereRequestTest method testEncodingOnPreSuspend.

@Test
public void testEncodingOnPreSuspend() throws IOException, ServletException {
    final AtomicReference<AtmosphereResponse> e = new AtomicReference<AtmosphereResponse>();
    framework.addAtmosphereHandler("/a", new AbstractReflectorAtmosphereHandler() {

        @Override
        public void onRequest(AtmosphereResource resource) throws IOException {
            resource.addEventListener(new AtmosphereResourceEventListenerAdapter() {

                @Override
                public void onPreSuspend(AtmosphereResourceEvent event) {
                    AtmosphereResponse response = event.getResource().getResponse();
                    response.setCharacterEncoding("utf-8");
                    e.set(response);
                }
            }).suspend(10);
        }

        @Override
        public void destroy() {
        }
    });
    AtmosphereRequest request = new AtmosphereRequestImpl.Builder().pathInfo("/a").build();
    framework.doCometSupport(request, AtmosphereResponseImpl.newInstance().delegateToNativeResponse(false));
    assertEquals(e.get().getCharacterEncoding(), "utf-8");
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) AbstractReflectorAtmosphereHandler(org.atmosphere.handler.AbstractReflectorAtmosphereHandler) IOException(java.io.IOException) Test(org.testng.annotations.Test)

Example 5 with AbstractReflectorAtmosphereHandler

use of org.atmosphere.handler.AbstractReflectorAtmosphereHandler in project atmosphere by Atmosphere.

the class AtmosphereRequestTest method testRequestBodyString.

@Test
public void testRequestBodyString() throws IOException, ServletException {
    final AtomicReference<AtmosphereRequestImpl.Body> e = new AtomicReference<AtmosphereRequestImpl.Body>();
    framework.addAtmosphereHandler("/a", new AbstractReflectorAtmosphereHandler() {

        @Override
        public void onRequest(AtmosphereResource resource) throws IOException {
            e.set(resource.getRequest().body());
        }

        @Override
        public void destroy() {
        }
    });
    AtmosphereRequest request = new AtmosphereRequestImpl.Builder().pathInfo("/a").body("test").build();
    framework.doCometSupport(request, AtmosphereResponseImpl.newInstance().delegateToNativeResponse(false));
    assertNotNull(e.get());
    assertTrue(e.get().hasString());
    assertFalse(e.get().hasBytes());
    assertEquals(e.get().asString(), "test");
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) AbstractReflectorAtmosphereHandler(org.atmosphere.handler.AbstractReflectorAtmosphereHandler) IOException(java.io.IOException) Test(org.testng.annotations.Test)

Aggregations

IOException (java.io.IOException)12 AbstractReflectorAtmosphereHandler (org.atmosphere.handler.AbstractReflectorAtmosphereHandler)12 Test (org.testng.annotations.Test)11 AtomicReference (java.util.concurrent.atomic.AtomicReference)10 HashMap (java.util.HashMap)2 ExecutionException (java.util.concurrent.ExecutionException)2 CountDownLatch (java.util.concurrent.CountDownLatch)1 ServletException (javax.servlet.ServletException)1