Search in sources :

Example 6 with ServletResponse

use of jakarta.servlet.ServletResponse in project tomcat by apache.

the class AsyncContextImpl method dispatch.

@Override
public void dispatch(ServletContext servletContext, String path) {
    synchronized (asyncContextLock) {
        if (log.isDebugEnabled()) {
            logDebug("dispatch   ");
        }
        check();
        if (dispatch != null) {
            throw new IllegalStateException(sm.getString("asyncContextImpl.dispatchingStarted"));
        }
        if (request.getAttribute(ASYNC_REQUEST_URI) == null) {
            request.setAttribute(ASYNC_REQUEST_URI, request.getRequestURI());
            request.setAttribute(ASYNC_CONTEXT_PATH, request.getContextPath());
            request.setAttribute(ASYNC_SERVLET_PATH, request.getServletPath());
            request.setAttribute(ASYNC_PATH_INFO, request.getPathInfo());
            request.setAttribute(ASYNC_QUERY_STRING, request.getQueryString());
        }
        final RequestDispatcher requestDispatcher = servletContext.getRequestDispatcher(path);
        if (!(requestDispatcher instanceof AsyncDispatcher)) {
            throw new UnsupportedOperationException(sm.getString("asyncContextImpl.noAsyncDispatcher"));
        }
        final AsyncDispatcher applicationDispatcher = (AsyncDispatcher) requestDispatcher;
        final ServletRequest servletRequest = getRequest();
        final ServletResponse servletResponse = getResponse();
        this.dispatch = new AsyncRunnable(request, applicationDispatcher, servletRequest, servletResponse);
        this.request.getCoyoteRequest().action(ActionCode.ASYNC_DISPATCH, null);
        clearServletRequestResponse();
    }
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) ServletRequest(jakarta.servlet.ServletRequest) ServletResponse(jakarta.servlet.ServletResponse) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) AsyncDispatcher(org.apache.catalina.AsyncDispatcher) RequestDispatcher(jakarta.servlet.RequestDispatcher)

Example 7 with ServletResponse

use of jakarta.servlet.ServletResponse in project tomcat by apache.

the class TestMimeHeadersIntegration method setupHeadersTest.

private void setupHeadersTest(Tomcat tomcat) {
    Context ctx = tomcat.addContext("", getTemporaryDirectory().getAbsolutePath());
    Tomcat.addServlet(ctx, "servlet", new HttpServlet() {

        private static final long serialVersionUID = 1L;

        @Override
        public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
            res.setContentType("text/plain; charset=ISO-8859-1");
            res.getWriter().write("OK");
        }
    });
    ctx.addServletMappingDecoded("/", "servlet");
    alv = new HeaderCountLogValve();
    tomcat.getHost().getPipeline().addValve(alv);
}
Also used : Context(org.apache.catalina.Context) ServletException(jakarta.servlet.ServletException) ServletRequest(jakarta.servlet.ServletRequest) ServletResponse(jakarta.servlet.ServletResponse) HttpServlet(jakarta.servlet.http.HttpServlet) IOException(java.io.IOException)

Example 8 with ServletResponse

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

the class RequestContextFilterTests method testFilterInvocation.

private void testFilterInvocation(final ServletException sex) throws Exception {
    final MockHttpServletRequest req = new MockHttpServletRequest();
    req.setAttribute("myAttr", "myValue");
    final MockHttpServletResponse resp = new MockHttpServletResponse();
    // Expect one invocation by the filter being tested
    class DummyFilterChain implements FilterChain {

        public int invocations = 0;

        @Override
        public void doFilter(ServletRequest req, ServletResponse resp) throws IOException, ServletException {
            ++invocations;
            if (invocations == 1) {
                assertThat(RequestContextHolder.currentRequestAttributes().getAttribute("myAttr", RequestAttributes.SCOPE_REQUEST)).isSameAs("myValue");
                if (sex != null) {
                    throw sex;
                }
            } else {
                throw new IllegalStateException("Too many invocations");
            }
        }
    }
    DummyFilterChain fc = new DummyFilterChain();
    MockFilterConfig mfc = new MockFilterConfig(new MockServletContext(), "foo");
    RequestContextFilter rbf = new RequestContextFilter();
    rbf.init(mfc);
    try {
        rbf.doFilter(req, resp, fc);
        assertThat(sex).isNull();
    } catch (ServletException ex) {
        assertThat(sex).isNotNull();
    }
    assertThatIllegalStateException().isThrownBy(RequestContextHolder::currentRequestAttributes);
    assertThat(fc.invocations).isEqualTo(1);
}
Also used : ServletException(jakarta.servlet.ServletException) RequestContextHolder(org.springframework.web.context.request.RequestContextHolder) ServletRequest(jakarta.servlet.ServletRequest) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) ServletResponse(jakarta.servlet.ServletResponse) Assertions.assertThatIllegalStateException(org.assertj.core.api.Assertions.assertThatIllegalStateException) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) FilterChain(jakarta.servlet.FilterChain) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) MockFilterConfig(org.springframework.web.testfixture.servlet.MockFilterConfig)

Example 9 with ServletResponse

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

the class ViewResolverTests method internalResourceViewResolverWithContextBeans.

@Test
public void internalResourceViewResolverWithContextBeans() throws Exception {
    this.wac.registerSingleton("myBean", TestBean.class);
    this.wac.registerSingleton("myBean2", TestBean.class);
    this.wac.refresh();
    InternalResourceViewResolver vr = new InternalResourceViewResolver();
    Properties props = new Properties();
    props.setProperty("key1", "value1");
    vr.setAttributes(props);
    Map<String, Object> map = new HashMap<>();
    map.put("key2", 2);
    vr.setAttributesMap(map);
    vr.setExposeContextBeansAsAttributes(true);
    vr.setApplicationContext(this.wac);
    HttpServletRequest request = new MockHttpServletRequest(this.sc) {

        @Override
        public RequestDispatcher getRequestDispatcher(String path) {
            return new MockRequestDispatcher(path) {

                @Override
                public void forward(ServletRequest forwardRequest, ServletResponse forwardResponse) {
                    assertThat(forwardRequest.getAttribute("rc") == null).as("Correct rc attribute").isTrue();
                    assertThat(forwardRequest.getAttribute("key1")).isEqualTo("value1");
                    assertThat(forwardRequest.getAttribute("key2")).isEqualTo(2);
                    assertThat(forwardRequest.getAttribute("myBean")).isSameAs(wac.getBean("myBean"));
                    assertThat(forwardRequest.getAttribute("myBean2")).isSameAs(wac.getBean("myBean2"));
                }
            };
        }
    };
    request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.wac);
    request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
    View view = vr.resolveViewName("example1", Locale.getDefault());
    view.render(new HashMap<String, Object>(), request, this.response);
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) ServletRequest(jakarta.servlet.ServletRequest) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) ServletResponse(jakarta.servlet.ServletResponse) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) HashMap(java.util.HashMap) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) Properties(java.util.Properties) AcceptHeaderLocaleResolver(org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver) View(org.springframework.web.servlet.View) MockRequestDispatcher(org.springframework.web.testfixture.servlet.MockRequestDispatcher) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) Test(org.junit.jupiter.api.Test)

Example 10 with ServletResponse

use of jakarta.servlet.ServletResponse in project spring-security by spring-projects.

the class GrantedAuthorityDefaultsJcTests method doFilterIsUserInRole.

// SEC-2926
@Test
public void doFilterIsUserInRole() throws Exception {
    SecurityContext context = SecurityContextHolder.getContext();
    this.request.getSession().setAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY, context);
    this.chain = new MockFilterChain() {

        @Override
        public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
            HttpServletRequest httpRequest = (HttpServletRequest) request;
            assertThat(httpRequest.isUserInRole("USER")).isTrue();
            assertThat(httpRequest.isUserInRole("INVALID")).isFalse();
            super.doFilter(request, response);
        }
    };
    this.springSecurityFilterChain.doFilter(this.request, this.response, this.chain);
    assertThat(this.chain.getRequest()).isNotNull();
}
Also used : ServletException(jakarta.servlet.ServletException) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) ServletRequest(jakarta.servlet.ServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) ServletResponse(jakarta.servlet.ServletResponse) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) SecurityContext(org.springframework.security.core.context.SecurityContext) IOException(java.io.IOException) MockFilterChain(org.springframework.mock.web.MockFilterChain) Test(org.junit.jupiter.api.Test)

Aggregations

ServletResponse (jakarta.servlet.ServletResponse)21 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)17 ServletRequest (jakarta.servlet.ServletRequest)13 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)10 ServletException (jakarta.servlet.ServletException)9 IOException (java.io.IOException)8 Test (org.junit.jupiter.api.Test)5 ServletResponseWrapper (jakarta.servlet.ServletResponseWrapper)4 MockFilterChain (org.springframework.mock.web.MockFilterChain)4 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)4 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)4 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)4 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)4 Response (org.apache.catalina.connector.Response)3 AsyncEvent (jakarta.servlet.AsyncEvent)2 ServletOutputStream (jakarta.servlet.ServletOutputStream)2 HashMap (java.util.HashMap)2 Properties (java.util.Properties)2 Context (org.apache.catalina.Context)2 ResponseFacade (org.apache.catalina.connector.ResponseFacade)2