Search in sources :

Example 21 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)

Example 22 with ServletContext

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

the class DelegatingFilterProxyTests method testDelegatingFilterProxyWithFilterName.

@Test
public void testDelegatingFilterProxyWithFilterName() 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, "targetFilter");
    DelegatingFilterProxy filterProxy = new DelegatingFilterProxy();
    filterProxy.init(proxyConfig);
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    filterProxy.doFilter(request, response, null);
    assertThat(targetFilter.filterConfig).isNull();
    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)

Example 23 with ServletContext

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

the class DelegatingFilterProxyTests method testDelegatingFilterProxyNotInjectedWacServletAttrPreferred.

@Test
public void testDelegatingFilterProxyNotInjectedWacServletAttrPreferred() throws ServletException, IOException {
    ServletContext sc = new MockServletContext();
    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    wac.setServletContext(sc);
    wac.refresh();
    sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
    sc.setAttribute("org.springframework.web.servlet.FrameworkServlet.CONTEXT.dispatcher", wac);
    StaticWebApplicationContext wacToUse = new StaticWebApplicationContext();
    wacToUse.setServletContext(sc);
    String beanName = "targetFilter";
    String attrName = "customAttrName";
    wacToUse.registerSingleton(beanName, MockFilter.class);
    wacToUse.refresh();
    sc.setAttribute(attrName, wacToUse);
    MockFilter targetFilter = (MockFilter) wacToUse.getBean(beanName);
    DelegatingFilterProxy filterProxy = new DelegatingFilterProxy(beanName);
    filterProxy.setContextAttribute(attrName);
    filterProxy.setServletContext(sc);
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    filterProxy.doFilter(request, response, null);
    assertThat(targetFilter.filterConfig).isNull();
    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) Test(org.junit.jupiter.api.Test)

Example 24 with ServletContext

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

the class ServletForwardingController method handleRequestInternal.

@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
    ServletContext servletContext = getServletContext();
    Assert.state(servletContext != null, "No ServletContext");
    RequestDispatcher rd = servletContext.getNamedDispatcher(this.servletName);
    if (rd == null) {
        throw new ServletException("No servlet with name '" + this.servletName + "' defined in web.xml");
    }
    // If already included, include again, else forward.
    if (useInclude(request, response)) {
        rd.include(request, response);
        if (logger.isTraceEnabled()) {
            logger.trace("Included servlet [" + this.servletName + "] in ServletForwardingController '" + this.beanName + "'");
        }
    } else {
        rd.forward(request, response);
        if (logger.isTraceEnabled()) {
            logger.trace("Forwarded to servlet [" + this.servletName + "] in ServletForwardingController '" + this.beanName + "'");
        }
    }
    return null;
}
Also used : ServletException(jakarta.servlet.ServletException) ServletContext(jakarta.servlet.ServletContext) RequestDispatcher(jakarta.servlet.RequestDispatcher)

Example 25 with ServletContext

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

the class JettyRequestUpgradeStrategy method upgrade.

@Override
public void upgrade(ServerHttpRequest request, ServerHttpResponse response, @Nullable String selectedProtocol, List<WebSocketExtension> selectedExtensions, @Nullable Principal user, WebSocketHandler handler, Map<String, Object> attributes) throws HandshakeFailureException {
    Assert.isInstanceOf(ServletServerHttpRequest.class, request, "ServletServerHttpRequest required");
    HttpServletRequest servletRequest = ((ServletServerHttpRequest) request).getServletRequest();
    ServletContext servletContext = servletRequest.getServletContext();
    Assert.isInstanceOf(ServletServerHttpResponse.class, response, "ServletServerHttpResponse required");
    HttpServletResponse servletResponse = ((ServletServerHttpResponse) response).getServletResponse();
    JettyWebSocketSession session = new JettyWebSocketSession(attributes, user);
    JettyWebSocketHandlerAdapter handlerAdapter = new JettyWebSocketHandlerAdapter(handler, session);
    JettyWebSocketCreator webSocketCreator = (upgradeRequest, upgradeResponse) -> {
        if (selectedProtocol != null) {
            upgradeResponse.setAcceptedSubProtocol(selectedProtocol);
        }
        return handlerAdapter;
    };
    JettyWebSocketServerContainer container = JettyWebSocketServerContainer.getContainer(servletContext);
    try {
        container.upgrade(webSocketCreator, servletRequest, servletResponse);
    } catch (UndeclaredThrowableException ex) {
        throw new HandshakeFailureException("Failed to upgrade", ex.getUndeclaredThrowable());
    } catch (Exception ex) {
        throw new HandshakeFailureException("Failed to upgrade", ex);
    }
}
Also used : WebSocketHandler(org.springframework.web.socket.WebSocketHandler) RequestUpgradeStrategy(org.springframework.web.socket.server.RequestUpgradeStrategy) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) ServerHttpRequest(org.springframework.http.server.ServerHttpRequest) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) ServerHttpResponse(org.springframework.http.server.ServerHttpResponse) JettyWebSocketSession(org.springframework.web.socket.adapter.jetty.JettyWebSocketSession) List(java.util.List) Principal(java.security.Principal) JettyWebSocketCreator(org.eclipse.jetty.websocket.server.JettyWebSocketCreator) ServletServerHttpResponse(org.springframework.http.server.ServletServerHttpResponse) JettyWebSocketHandlerAdapter(org.springframework.web.socket.adapter.jetty.JettyWebSocketHandlerAdapter) JettyWebSocketServerContainer(org.eclipse.jetty.websocket.server.JettyWebSocketServerContainer) ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) Map(java.util.Map) HandshakeFailureException(org.springframework.web.socket.server.HandshakeFailureException) Nullable(org.springframework.lang.Nullable) ServletContext(jakarta.servlet.ServletContext) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) WebSocketExtension(org.springframework.web.socket.WebSocketExtension) Collections(java.util.Collections) Assert(org.springframework.util.Assert) ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) JettyWebSocketSession(org.springframework.web.socket.adapter.jetty.JettyWebSocketSession) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) ServletServerHttpResponse(org.springframework.http.server.ServletServerHttpResponse) JettyWebSocketCreator(org.eclipse.jetty.websocket.server.JettyWebSocketCreator) JettyWebSocketHandlerAdapter(org.springframework.web.socket.adapter.jetty.JettyWebSocketHandlerAdapter) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) HandshakeFailureException(org.springframework.web.socket.server.HandshakeFailureException) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) JettyWebSocketServerContainer(org.eclipse.jetty.websocket.server.JettyWebSocketServerContainer) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) HandshakeFailureException(org.springframework.web.socket.server.HandshakeFailureException) ServletContext(jakarta.servlet.ServletContext)

Aggregations

ServletContext (jakarta.servlet.ServletContext)148 Test (org.junit.jupiter.api.Test)63 ServletConfig (jakarta.servlet.ServletConfig)40 Enumeration (java.util.Enumeration)29 MockServletContext (org.springframework.web.testfixture.servlet.MockServletContext)24 BeforeMethod (org.testng.annotations.BeforeMethod)22 IOException (java.io.IOException)20 ServletException (jakarta.servlet.ServletException)17 FilterRegistration (jakarta.servlet.FilterRegistration)16 DelegatingFilterProxy (org.springframework.web.filter.DelegatingFilterProxy)15 Filter (jakarta.servlet.Filter)13 Test (org.junit.Test)13 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 WebApplicationContext (org.springframework.web.context.WebApplicationContext)9 File (java.io.File)8 Context (org.apache.catalina.Context)7 BlockingIOCometSupport (org.atmosphere.container.BlockingIOCometSupport)7