Search in sources :

Example 1 with ServletException

use of jakarta.servlet.ServletException in project atmosphere by Atmosphere.

the class AtmosphereFramework method loadConfiguration.

public void loadConfiguration(ServletConfig sc) throws ServletException {
    if (!autoDetectHandlers)
        return;
    try {
        URL url = sc.getServletContext().getResource(handlersPath);
        ClassLoader urlC = url == null ? getClass().getClassLoader() : new URLClassLoader(new URL[] { url }, Thread.currentThread().getContextClassLoader());
        loadAtmosphereDotXml(sc.getServletContext().getResourceAsStream(atmosphereDotXmlPath), urlC);
        if (atmosphereHandlers.isEmpty()) {
            autoDetectAtmosphereHandlers(sc.getServletContext(), urlC);
            if (atmosphereHandlers.isEmpty()) {
                detectSupportedFramework(sc);
            }
        }
        autoDetectWebSocketHandler(sc.getServletContext(), urlC);
    } catch (Throwable t) {
        throw new ServletException(t);
    }
}
Also used : ServletException(jakarta.servlet.ServletException) URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) URL(java.net.URL)

Example 2 with ServletException

use of jakarta.servlet.ServletException 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) IOException(java.io.IOException) ServletException(jakarta.servlet.ServletException) ExecutionException(java.util.concurrent.ExecutionException) WebSocket(org.atmosphere.websocket.WebSocket) Test(org.testng.annotations.Test)

Example 3 with ServletException

use of jakarta.servlet.ServletException in project atmosphere by Atmosphere.

the class WebSocketProcessorTest method create.

@BeforeMethod
public void create() throws Throwable {
    framework = new AtmosphereFramework();
    framework.setAsyncSupport(new AsynchronousProcessor(framework.getAtmosphereConfig()) {

        @Override
        public Action service(AtmosphereRequest req, AtmosphereResponse res) throws IOException, ServletException {
            return action(req, res);
        }
    });
    framework.addInitParameter(RECYCLE_ATMOSPHERE_REQUEST_RESPONSE, "false");
    framework.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 : ServletException(jakarta.servlet.ServletException) Enumeration(java.util.Enumeration) ServletConfig(jakarta.servlet.ServletConfig) ServletContext(jakarta.servlet.ServletContext) IOException(java.io.IOException) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 4 with ServletException

use of jakarta.servlet.ServletException in project atmosphere by Atmosphere.

the class AtmosphereSessionTest method testTrackAndTryAcquire.

@Test
public void testTrackAndTryAcquire() throws IOException, ServletException, InterruptedException {
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicReference<AtmosphereSession> session = new AtomicReference<AtmosphereSession>();
    framework.addAtmosphereHandler("/acquire", new AtmosphereHandlerAdapter() {

        @Override
        public void onRequest(AtmosphereResource resource) throws IOException {
            if (session.get() == null) {
                session.set(new AtmosphereSession(resource));
            }
            resource.suspend(2, TimeUnit.SECONDS);
        }

        @Override
        public void onStateChange(AtmosphereResourceEvent event) throws IOException {
            latch.countDown();
        }
    });
    final String qs = "&X-Atmosphere-tracking-id=c8834462-c46e-4dad-a22f-b86aabe3f883&X-Atmosphere-Framework=2.0.4-javascript&X-Atmosphere-Transport=sse&X-Atmosphere-TrackMessageSize=true&X-atmo-protocol=true&_=1380799455333";
    AtmosphereRequest request = new AtmosphereRequestImpl.Builder().queryString(qs).pathInfo("/acquire").build();
    framework.doCometSupport(request, AtmosphereResponseImpl.newInstance());
    latch.await(10, TimeUnit.SECONDS);
    assertNull(session.get().acquire());
    final AtomicReference<AtmosphereResource> rrr = new AtomicReference<AtmosphereResource>();
    final CountDownLatch _latch = new CountDownLatch(1);
    framework.addAtmosphereHandler("/acquire", new AtmosphereHandlerAdapter() {

        @Override
        public void onRequest(final AtmosphereResource resource) throws IOException {
            resource.suspend(2, TimeUnit.SECONDS);
        }

        @Override
        public void onStateChange(AtmosphereResourceEvent event) throws IOException {
            try {
                rrr.set(session.get().tryAcquire());
                _latch.countDown();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    });
    request = new AtmosphereRequestImpl.Builder().queryString(qs).pathInfo("/acquire").build();
    framework.doCometSupport(request, AtmosphereResponseImpl.newInstance(request));
    _latch.await(10, TimeUnit.SECONDS);
    assertNotNull(rrr.get());
    new Thread() {

        public void run() {
            try {
                Thread.sleep(1000);
                AtmosphereRequest request = new AtmosphereRequestImpl.Builder().queryString(qs).pathInfo("/acquire").build();
                framework.doCometSupport(request, AtmosphereResponseImpl.newInstance(request));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }.start();
    assertNotNull(session.get().tryAcquire());
}
Also used : AtmosphereHandlerAdapter(org.atmosphere.handler.AtmosphereHandlerAdapter) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) ServletException(jakarta.servlet.ServletException) Test(org.testng.annotations.Test)

Example 5 with ServletException

use of jakarta.servlet.ServletException 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(ManagedGet.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 ApplicationConfig.CLIENT_HEARTBEAT_INTERVAL_IN_SECONDS.equals(name) ? "10" : null;
        }

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

Aggregations

ServletException (jakarta.servlet.ServletException)128 IOException (java.io.IOException)79 Test (org.junit.jupiter.api.Test)32 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)24 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)24 ServletContext (jakarta.servlet.ServletContext)19 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)17 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)15 FilterChain (jakarta.servlet.FilterChain)14 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)13 Enumeration (java.util.Enumeration)12 BeforeEach (org.junit.jupiter.api.BeforeEach)12 HttpHeaders (org.springframework.http.HttpHeaders)11 BeforeMethod (org.testng.annotations.BeforeMethod)11 ServletConfig (jakarta.servlet.ServletConfig)10 ServletRequest (jakarta.servlet.ServletRequest)10 ServletResponse (jakarta.servlet.ServletResponse)10 Arrays (java.util.Arrays)10 UnavailableException (jakarta.servlet.UnavailableException)9 HttpMethod (org.springframework.http.HttpMethod)9