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;
}
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);
}
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);
}
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);
}
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();
}
Aggregations