Search in sources :

Example 21 with Servlet

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

the class ServletWebServerApplicationContextTests method servletAndFilterBeans.

@Test
void servletAndFilterBeans() {
    addWebServerFactoryBean();
    Servlet servlet = mock(Servlet.class);
    Filter filter1 = mock(Filter.class, withSettings().extraInterfaces(Ordered.class));
    given(((Ordered) filter1).getOrder()).willReturn(1);
    Filter filter2 = mock(Filter.class, withSettings().extraInterfaces(Ordered.class));
    given(((Ordered) filter2).getOrder()).willReturn(2);
    this.context.registerBeanDefinition("servletBean", beanDefinition(servlet));
    this.context.registerBeanDefinition("filterBean2", beanDefinition(filter2));
    this.context.registerBeanDefinition("filterBean1", beanDefinition(filter1));
    this.context.refresh();
    MockServletWebServerFactory factory = getWebServerFactory();
    ServletContext servletContext = factory.getServletContext();
    InOrder ordered = inOrder(servletContext);
    then(factory.getServletContext()).should().addServlet("servletBean", servlet);
    then(factory.getRegisteredServlet(0).getRegistration()).should().addMapping("/");
    then(factory.getServletContext()).should(ordered).addFilter("filterBean1", filter1);
    then(factory.getServletContext()).should(ordered).addFilter("filterBean2", filter2);
    then(factory.getRegisteredFilter(0).getRegistration()).should().addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), false, "/*");
    then(factory.getRegisteredFilter(1).getRegistration()).should().addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), false, "/*");
}
Also used : MockServletWebServerFactory(org.springframework.boot.web.servlet.server.MockServletWebServerFactory) InOrder(org.mockito.InOrder) Filter(jakarta.servlet.Filter) Ordered(org.springframework.core.Ordered) Servlet(jakarta.servlet.Servlet) ServletContext(jakarta.servlet.ServletContext) Test(org.junit.jupiter.api.Test)

Example 22 with Servlet

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

the class ServletWebServerApplicationContextTests method singleServletBean.

@Test
void singleServletBean() {
    addWebServerFactoryBean();
    Servlet servlet = mock(Servlet.class);
    this.context.registerBeanDefinition("servletBean", beanDefinition(servlet));
    this.context.refresh();
    MockServletWebServerFactory factory = getWebServerFactory();
    then(factory.getServletContext()).should().addServlet("servletBean", servlet);
    then(factory.getRegisteredServlet(0).getRegistration()).should().addMapping("/");
}
Also used : MockServletWebServerFactory(org.springframework.boot.web.servlet.server.MockServletWebServerFactory) Servlet(jakarta.servlet.Servlet) Test(org.junit.jupiter.api.Test)

Example 23 with Servlet

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

the class AnnotationConfigServletWebServerApplicationContextTests method verifyContext.

private void verifyContext() {
    MockServletWebServerFactory factory = this.context.getBean(MockServletWebServerFactory.class);
    Servlet servlet = this.context.getBean(Servlet.class);
    then(factory.getServletContext()).should().addServlet("servlet", servlet);
}
Also used : MockServletWebServerFactory(org.springframework.boot.web.servlet.server.MockServletWebServerFactory) MockServlet(org.springframework.boot.web.servlet.mock.MockServlet) GenericServlet(jakarta.servlet.GenericServlet) Servlet(jakarta.servlet.Servlet)

Example 24 with Servlet

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

the class ServletContextInitializerBeans method addServletContextInitializerBean.

private void addServletContextInitializerBean(String beanName, ServletContextInitializer initializer, ListableBeanFactory beanFactory) {
    if (initializer instanceof ServletRegistrationBean) {
        Servlet source = ((ServletRegistrationBean<?>) initializer).getServlet();
        addServletContextInitializerBean(Servlet.class, beanName, initializer, beanFactory, source);
    } else if (initializer instanceof FilterRegistrationBean) {
        Filter source = ((FilterRegistrationBean<?>) initializer).getFilter();
        addServletContextInitializerBean(Filter.class, beanName, initializer, beanFactory, source);
    } else if (initializer instanceof DelegatingFilterProxyRegistrationBean) {
        String source = ((DelegatingFilterProxyRegistrationBean) initializer).getTargetBeanName();
        addServletContextInitializerBean(Filter.class, beanName, initializer, beanFactory, source);
    } else if (initializer instanceof ServletListenerRegistrationBean) {
        EventListener source = ((ServletListenerRegistrationBean<?>) initializer).getListener();
        addServletContextInitializerBean(EventListener.class, beanName, initializer, beanFactory, source);
    } else {
        addServletContextInitializerBean(ServletContextInitializer.class, beanName, initializer, beanFactory, initializer);
    }
}
Also used : Filter(jakarta.servlet.Filter) Servlet(jakarta.servlet.Servlet) EventListener(java.util.EventListener)

Example 25 with Servlet

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

the class DispatcherServletsMappingDescriptionProvider method determineDispatcherServlets.

private Map<String, DispatcherServlet> determineDispatcherServlets(WebApplicationContext context) {
    Map<String, DispatcherServlet> dispatcherServlets = new LinkedHashMap<>();
    context.getBeansOfType(ServletRegistrationBean.class).values().forEach((registration) -> {
        Servlet servlet = registration.getServlet();
        if (servlet instanceof DispatcherServlet && !dispatcherServlets.containsValue(servlet)) {
            dispatcherServlets.put(registration.getServletName(), (DispatcherServlet) servlet);
        }
    });
    context.getBeansOfType(DispatcherServlet.class).forEach((name, dispatcherServlet) -> {
        if (!dispatcherServlets.containsValue(dispatcherServlet)) {
            dispatcherServlets.put(name, dispatcherServlet);
        }
    });
    return dispatcherServlets;
}
Also used : DispatcherServlet(org.springframework.web.servlet.DispatcherServlet) DispatcherServlet(org.springframework.web.servlet.DispatcherServlet) Servlet(jakarta.servlet.Servlet) LinkedHashMap(java.util.LinkedHashMap)

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