Search in sources :

Example 46 with ServletContext

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

the class DefaultMockMvcBuilder method initWebAppContext.

@Override
protected WebApplicationContext initWebAppContext() {
    ServletContext servletContext = this.webAppContext.getServletContext();
    Assert.state(servletContext != null, "No ServletContext");
    ApplicationContext rootWac = WebApplicationContextUtils.getWebApplicationContext(servletContext);
    if (rootWac == null) {
        rootWac = this.webAppContext;
        ApplicationContext parent = this.webAppContext.getParent();
        while (parent != null) {
            if (parent instanceof WebApplicationContext && !(parent.getParent() instanceof WebApplicationContext)) {
                rootWac = parent;
                break;
            }
            parent = parent.getParent();
        }
        servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, rootWac);
    }
    return this.webAppContext;
}
Also used : WebApplicationContext(org.springframework.web.context.WebApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) ServletContext(jakarta.servlet.ServletContext) WebApplicationContext(org.springframework.web.context.WebApplicationContext)

Example 47 with ServletContext

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

the class StandaloneMockMvcBuilder method registerMvcSingletons.

private void registerMvcSingletons(StubWebApplicationContext wac) {
    StandaloneConfiguration config = new StandaloneConfiguration();
    config.setApplicationContext(wac);
    ServletContext sc = wac.getServletContext();
    wac.addBeans(this.controllers);
    wac.addBeans(this.controllerAdvice);
    FormattingConversionService mvcConversionService = config.mvcConversionService();
    wac.addBean("mvcConversionService", mvcConversionService);
    ResourceUrlProvider resourceUrlProvider = config.mvcResourceUrlProvider();
    wac.addBean("mvcResourceUrlProvider", resourceUrlProvider);
    ContentNegotiationManager mvcContentNegotiationManager = config.mvcContentNegotiationManager();
    wac.addBean("mvcContentNegotiationManager", mvcContentNegotiationManager);
    Validator mvcValidator = config.mvcValidator();
    wac.addBean("mvcValidator", mvcValidator);
    RequestMappingHandlerMapping hm = config.getHandlerMapping(mvcConversionService, resourceUrlProvider);
    if (sc != null) {
        hm.setServletContext(sc);
    }
    hm.setApplicationContext(wac);
    hm.afterPropertiesSet();
    wac.addBean("requestMappingHandlerMapping", hm);
    RequestMappingHandlerAdapter ha = config.requestMappingHandlerAdapter(mvcContentNegotiationManager, mvcConversionService, mvcValidator);
    if (sc != null) {
        ha.setServletContext(sc);
    }
    ha.setApplicationContext(wac);
    ha.afterPropertiesSet();
    wac.addBean("requestMappingHandlerAdapter", ha);
    wac.addBean("handlerExceptionResolver", config.handlerExceptionResolver(mvcContentNegotiationManager));
    wac.addBeans(initViewResolvers(wac));
    wac.addBean(DispatcherServlet.LOCALE_RESOLVER_BEAN_NAME, this.localeResolver);
    wac.addBean(DispatcherServlet.THEME_RESOLVER_BEAN_NAME, new FixedThemeResolver());
    wac.addBean(DispatcherServlet.REQUEST_TO_VIEW_NAME_TRANSLATOR_BEAN_NAME, new DefaultRequestToViewNameTranslator());
    this.flashMapManager = new SessionFlashMapManager();
    wac.addBean(DispatcherServlet.FLASH_MAP_MANAGER_BEAN_NAME, this.flashMapManager);
    extendMvcSingletons(sc).forEach(wac::addBean);
}
Also used : ContentNegotiationManager(org.springframework.web.accept.ContentNegotiationManager) FixedThemeResolver(org.springframework.web.servlet.theme.FixedThemeResolver) DefaultRequestToViewNameTranslator(org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator) ServletContext(jakarta.servlet.ServletContext) MockServletContext(org.springframework.mock.web.MockServletContext) SessionFlashMapManager(org.springframework.web.servlet.support.SessionFlashMapManager) RequestMappingHandlerMapping(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping) FormattingConversionService(org.springframework.format.support.FormattingConversionService) DefaultFormattingConversionService(org.springframework.format.support.DefaultFormattingConversionService) RequestMappingHandlerAdapter(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter) Validator(org.springframework.validation.Validator) ResourceUrlProvider(org.springframework.web.servlet.resource.ResourceUrlProvider)

Example 48 with ServletContext

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

the class JavaConfigTests method verifyRootWacSupport.

/**
 * Verify that the breaking change introduced in <a
 * href="https://jira.spring.io/browse/SPR-12553">SPR-12553</a> has been reverted.
 *
 * <p>This code has been copied from
 * {@link org.springframework.test.context.hierarchies.web.ControllerIntegrationTests}.
 *
 * @see org.springframework.test.context.hierarchies.web.ControllerIntegrationTests#verifyRootWacSupport()
 */
private void verifyRootWacSupport() {
    assertThat(personDao).isNotNull();
    assertThat(personController).isNotNull();
    ApplicationContext parent = wac.getParent();
    assertThat(parent).isNotNull();
    boolean condition = parent instanceof WebApplicationContext;
    assertThat(condition).isTrue();
    WebApplicationContext root = (WebApplicationContext) parent;
    ServletContext childServletContext = wac.getServletContext();
    assertThat(childServletContext).isNotNull();
    ServletContext rootServletContext = root.getServletContext();
    assertThat(rootServletContext).isNotNull();
    assertThat(rootServletContext).isSameAs(childServletContext);
    assertThat(rootServletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE)).isSameAs(root);
    assertThat(childServletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE)).isSameAs(root);
}
Also used : WebApplicationContext(org.springframework.web.context.WebApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) ServletContext(jakarta.servlet.ServletContext) WebApplicationContext(org.springframework.web.context.WebApplicationContext)

Example 49 with ServletContext

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

the class WebApplicationObjectSupport method getTempDir.

/**
 * Return the temporary directory for the current web application,
 * as provided by the servlet container.
 * @return the File representing the temporary directory
 * @throws IllegalStateException if not running within a ServletContext
 * @see org.springframework.web.util.WebUtils#getTempDir(jakarta.servlet.ServletContext)
 */
protected final File getTempDir() throws IllegalStateException {
    ServletContext servletContext = getServletContext();
    Assert.state(servletContext != null, "ServletContext is required");
    return WebUtils.getTempDir(servletContext);
}
Also used : ServletContext(jakarta.servlet.ServletContext)

Example 50 with ServletContext

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

the class DelegatingFilterProxyTests method testDelegatingFilterProxyWithTargetFilterLifecycle.

@Test
public void testDelegatingFilterProxyWithTargetFilterLifecycle() throws ServletException, IOException {
    ServletContext sc = new MockServletContext();
    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    wac.setServletContext(sc);
    wac.registerSingleton("targetFilter", MockFilter.class);
    wac.refresh();
    sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
    MockFilter targetFilter = (MockFilter) wac.getBean("targetFilter");
    MockFilterConfig proxyConfig = new MockFilterConfig(sc);
    proxyConfig.addInitParameter("targetBeanName", "targetFilter");
    proxyConfig.addInitParameter("targetFilterLifecycle", "true");
    DelegatingFilterProxy filterProxy = new DelegatingFilterProxy();
    filterProxy.init(proxyConfig);
    assertThat(targetFilter.filterConfig).isEqualTo(proxyConfig);
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    filterProxy.doFilter(request, response, null);
    assertThat(targetFilter.filterConfig).isEqualTo(proxyConfig);
    assertThat(request.getAttribute("called")).isEqualTo(Boolean.TRUE);
    filterProxy.destroy();
    assertThat(targetFilter.filterConfig).isNull();
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) ServletContext(jakarta.servlet.ServletContext) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) MockFilterConfig(org.springframework.web.testfixture.servlet.MockFilterConfig) Test(org.junit.jupiter.api.Test)

Aggregations

ServletContext (jakarta.servlet.ServletContext)116 Test (org.junit.jupiter.api.Test)45 ServletConfig (jakarta.servlet.ServletConfig)34 Enumeration (java.util.Enumeration)29 MockServletContext (org.springframework.web.testfixture.servlet.MockServletContext)24 BeforeMethod (org.testng.annotations.BeforeMethod)22 IOException (java.io.IOException)17 FilterRegistration (jakarta.servlet.FilterRegistration)15 DelegatingFilterProxy (org.springframework.web.filter.DelegatingFilterProxy)15 ServletException (jakarta.servlet.ServletException)12 ServletContextAwareProcessor (org.springframework.web.context.support.ServletContextAwareProcessor)12 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)12 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)12 StaticWebApplicationContext (org.springframework.web.context.support.StaticWebApplicationContext)11 BlockingIOCometSupport (org.atmosphere.container.BlockingIOCometSupport)9 Filter (jakarta.servlet.Filter)8 WebApplicationContext (org.springframework.web.context.WebApplicationContext)8 Context (org.apache.catalina.Context)7 AtmosphereFramework (org.atmosphere.cpr.AtmosphereFramework)6 Test (org.junit.Test)6