Search in sources :

Example 1 with Servlet

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

the class MeteorServiceInterceptor method mapAnnotatedService.

protected void mapAnnotatedService(boolean reMap, String path, AtmosphereRequest request, AtmosphereFramework.AtmosphereHandlerWrapper w) {
    synchronized (config.handlers()) {
        if (config.handlers().get(path) == null) {
            // MeteorService
            if (ReflectorServletProcessor.class.isAssignableFrom(w.atmosphereHandler.getClass())) {
                ReflectorServletProcessor r = (ReflectorServletProcessor) w.atmosphereHandler;
                Servlet s = r.getServlet();
                if (s == null) {
                    logger.warn("Invalid ReflectorServletProcessor state. Servlet is null");
                    return;
                }
                MeteorService m = s.getClass().getAnnotation(MeteorService.class);
                if (m != null) {
                    String targetPath = m.path();
                    if (targetPath.contains("{") && targetPath.contains("}")) {
                        try {
                            boolean singleton = s.getClass().getAnnotation(Singleton.class) != null;
                            if (!singleton) {
                                r = config.framework().newClassInstance(ReflectorServletProcessor.class, ReflectorServletProcessor.class);
                                r.setServlet(config.framework().newClassInstance(Servlet.class, s.getClass()));
                                r.init(config);
                            }
                            request.localAttributes().put(Named.class.getName(), path.substring(targetPath.indexOf("{")));
                            ((AtmosphereResourceImpl) request.resource()).atmosphereHandler(r);
                            config.framework().addAtmosphereHandler(path, r, config.getBroadcasterFactory().lookup(w.broadcaster.getClass(), path, true), w.interceptors);
                            request.setAttribute(FrameworkConfig.NEW_MAPPING, "true");
                        } catch (Throwable e) {
                            logger.warn("Unable to create AtmosphereHandler", e);
                        }
                    }
                }
            }
        } else if (reMap) {
            request.setAttribute(FrameworkConfig.NEW_MAPPING, "true");
        }
    }
}
Also used : MeteorService(org.atmosphere.config.service.MeteorService) Named(jakarta.inject.Named) Singleton(org.atmosphere.config.service.Singleton) Servlet(jakarta.servlet.Servlet) ReflectorServletProcessor(org.atmosphere.handler.ReflectorServletProcessor) AtmosphereResourceImpl(org.atmosphere.cpr.AtmosphereResourceImpl)

Example 2 with Servlet

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

the class MeteorTest method testMeteor.

@Test
public void testMeteor() throws IOException, ServletException {
    final AtomicReference<Meteor> meteor = new AtomicReference<Meteor>();
    final Servlet s = new HttpServlet() {

        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            meteor.set(Meteor.lookup(req));
        }
    };
    framework.addAtmosphereHandler("/a", new ReflectorServletProcessor(s));
    AtmosphereRequest request = new AtmosphereRequestImpl.Builder().pathInfo("/a").build();
    framework.interceptor(new AtmosphereInterceptorAdapter() {

        @Override
        public Action inspect(AtmosphereResource r) {
            Meteor m = Meteor.build(r.getRequest());
            return Action.CONTINUE;
        }
    });
    framework.doCometSupport(request, AtmosphereResponseImpl.newInstance());
    assertNotNull(meteor.get());
}
Also used : HttpServlet(jakarta.servlet.http.HttpServlet) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) HttpServlet(jakarta.servlet.http.HttpServlet) Servlet(jakarta.servlet.Servlet) ReflectorServletProcessor(org.atmosphere.handler.ReflectorServletProcessor) Test(org.testng.annotations.Test)

Example 3 with Servlet

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

the class MeteorTest method testMeteorNull.

@Test
public void testMeteorNull() throws IOException, ServletException {
    final AtomicReference<Meteor> meteor = new AtomicReference<Meteor>();
    final Servlet s = new HttpServlet() {

        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            meteor.set(Meteor.lookup(req));
        }
    };
    framework.addAtmosphereHandler("/a", new ReflectorServletProcessor(s));
    AtmosphereRequest request = new AtmosphereRequestImpl.Builder().pathInfo("/a").build();
    framework.interceptor(new AtmosphereInterceptorAdapter() {

        @Override
        public Action inspect(AtmosphereResource r) {
            return Action.CONTINUE;
        }
    });
    framework.doCometSupport(request, AtmosphereResponseImpl.newInstance());
    assertNull(meteor.get());
}
Also used : HttpServlet(jakarta.servlet.http.HttpServlet) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) HttpServlet(jakarta.servlet.http.HttpServlet) Servlet(jakarta.servlet.Servlet) ReflectorServletProcessor(org.atmosphere.handler.ReflectorServletProcessor) Test(org.testng.annotations.Test)

Example 4 with Servlet

use of jakarta.servlet.Servlet in project spring-framework by spring-projects.

the class HttpRequestHandlerTests method testHttpRequestHandlerServletPassThrough.

@Test
public void testHttpRequestHandlerServletPassThrough() throws Exception {
    MockServletContext servletContext = new MockServletContext();
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    wac.getBeanFactory().registerSingleton("myHandler", (HttpRequestHandler) (req, res) -> {
        assertThat(req).isSameAs(request);
        assertThat(res).isSameAs(response);
        String exception = request.getParameter("exception");
        if ("ServletException".equals(exception)) {
            throw new ServletException("test");
        }
        if ("IOException".equals(exception)) {
            throw new IOException("test");
        }
        res.getWriter().write("myResponse");
    });
    wac.setServletContext(servletContext);
    wac.refresh();
    servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
    Servlet servlet = new HttpRequestHandlerServlet();
    servlet.init(new MockServletConfig(servletContext, "myHandler"));
    servlet.service(request, response);
    assertThat(response.getContentAsString()).isEqualTo("myResponse");
    request.setParameter("exception", "ServletException");
    assertThatExceptionOfType(ServletException.class).isThrownBy(() -> servlet.service(request, response)).withMessage("test");
    request.setParameter("exception", "IOException");
    assertThatIOException().isThrownBy(() -> servlet.service(request, response)).withMessage("test");
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) WebApplicationContext(org.springframework.web.context.WebApplicationContext) MockServletConfig(org.springframework.web.testfixture.servlet.MockServletConfig) IOException(java.io.IOException) ServletException(jakarta.servlet.ServletException) Assertions.assertThatIOException(org.assertj.core.api.Assertions.assertThatIOException) Test(org.junit.jupiter.api.Test) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) Servlet(jakarta.servlet.Servlet) HttpRequestHandler(org.springframework.web.HttpRequestHandler) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) ServletException(jakarta.servlet.ServletException) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) Servlet(jakarta.servlet.Servlet) MockServletConfig(org.springframework.web.testfixture.servlet.MockServletConfig) IOException(java.io.IOException) Assertions.assertThatIOException(org.assertj.core.api.Assertions.assertThatIOException) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 5 with Servlet

use of jakarta.servlet.Servlet in project spring-boot by spring-projects.

the class ServletWebServerApplicationContextTests method multipleServletBeansWithMainDispatcher.

@Test
void multipleServletBeansWithMainDispatcher() {
    addWebServerFactoryBean();
    Servlet servlet1 = mock(Servlet.class, withSettings().extraInterfaces(Ordered.class));
    given(((Ordered) servlet1).getOrder()).willReturn(1);
    Servlet servlet2 = mock(Servlet.class, withSettings().extraInterfaces(Ordered.class));
    given(((Ordered) servlet2).getOrder()).willReturn(2);
    this.context.registerBeanDefinition("servletBean2", beanDefinition(servlet2));
    this.context.registerBeanDefinition("dispatcherServlet", beanDefinition(servlet1));
    this.context.refresh();
    MockServletWebServerFactory factory = getWebServerFactory();
    ServletContext servletContext = factory.getServletContext();
    InOrder ordered = inOrder(servletContext);
    then(servletContext).should(ordered).addServlet("dispatcherServlet", servlet1);
    then(servletContext).should(ordered).addServlet("servletBean2", servlet2);
    then(factory.getRegisteredServlet(0).getRegistration()).should().addMapping("/");
    then(factory.getRegisteredServlet(1).getRegistration()).should().addMapping("/servletBean2/");
}
Also used : MockServletWebServerFactory(org.springframework.boot.web.servlet.server.MockServletWebServerFactory) InOrder(org.mockito.InOrder) Ordered(org.springframework.core.Ordered) Servlet(jakarta.servlet.Servlet) ServletContext(jakarta.servlet.ServletContext) Test(org.junit.jupiter.api.Test)

Aggregations

Servlet (jakarta.servlet.Servlet)32 Test (org.junit.jupiter.api.Test)11 ServletContext (jakarta.servlet.ServletContext)6 ServletException (jakarta.servlet.ServletException)6 HttpServlet (jakarta.servlet.http.HttpServlet)6 MockServletWebServerFactory (org.springframework.boot.web.servlet.server.MockServletWebServerFactory)6 GenericServlet (jakarta.servlet.GenericServlet)5 IOException (java.io.IOException)5 Context (org.apache.catalina.Context)5 Filter (jakarta.servlet.Filter)4 UnavailableException (jakarta.servlet.UnavailableException)4 Tomcat (org.apache.catalina.startup.Tomcat)4 AsyncContext (jakarta.servlet.AsyncContext)3 ServletRequestWrapper (jakarta.servlet.ServletRequestWrapper)3 ServletResponseWrapper (jakarta.servlet.ServletResponseWrapper)3 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)3 Wrapper (org.apache.catalina.Wrapper)3 ByteChunk (org.apache.tomcat.util.buf.ByteChunk)3 ReflectorServletProcessor (org.atmosphere.handler.ReflectorServletProcessor)3 InOrder (org.mockito.InOrder)3