Search in sources :

Example 16 with HttpServletResponseWrapper

use of jakarta.servlet.http.HttpServletResponseWrapper in project tomcat by apache.

the class Response method setResponse.

/**
 * Set a wrapped HttpServletResponse to pass to the application. Components
 * wishing to wrap the response should obtain the response via
 * {@link #getResponse()}, wrap it and then call this method with the
 * wrapped response.
 *
 * @param applicationResponse The wrapped response to pass to the
 *        application
 */
public void setResponse(HttpServletResponse applicationResponse) {
    // Check the wrapper wraps this request
    ServletResponse r = applicationResponse;
    while (r instanceof HttpServletResponseWrapper) {
        r = ((HttpServletResponseWrapper) r).getResponse();
    }
    if (r != facade) {
        throw new IllegalArgumentException(sm.getString("response.illegalWrap"));
    }
    this.applicationResponse = applicationResponse;
}
Also used : ServletResponse(jakarta.servlet.ServletResponse) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) HttpServletResponseWrapper(jakarta.servlet.http.HttpServletResponseWrapper)

Example 17 with HttpServletResponseWrapper

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

the class HttpSessionSecurityContextRepositoryTests method traverseWrappedRequests.

// SEC-2578
@Test
public void traverseWrappedRequests() {
    HttpSessionSecurityContextRepository repo = new HttpSessionSecurityContextRepository();
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    HttpRequestResponseHolder holder = new HttpRequestResponseHolder(request, response);
    SecurityContext context = repo.loadContext(holder);
    assertThat(request.getSession(false)).isNull();
    // Simulate authentication during the request
    context.setAuthentication(this.testToken);
    repo.saveContext(context, new HttpServletRequestWrapper(holder.getRequest()), new HttpServletResponseWrapper(holder.getResponse()));
    assertThat(request.getSession(false)).isNotNull();
    assertThat(request.getSession().getAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY)).isEqualTo(context);
}
Also used : HttpServletRequestWrapper(jakarta.servlet.http.HttpServletRequestWrapper) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) TransientSecurityContext(org.springframework.security.core.context.TransientSecurityContext) SecurityContext(org.springframework.security.core.context.SecurityContext) HttpServletResponseWrapper(jakarta.servlet.http.HttpServletResponseWrapper) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Aggregations

HttpServletResponseWrapper (jakarta.servlet.http.HttpServletResponseWrapper)17 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)16 Test (org.junit.jupiter.api.Test)16 ServletResponse (jakarta.servlet.ServletResponse)14 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)14 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)14 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)14 RequestDispatcher (jakarta.servlet.RequestDispatcher)13 ServletException (jakarta.servlet.ServletException)13 ServletRequest (jakarta.servlet.ServletRequest)13 IOException (java.io.IOException)13 Enumeration (java.util.Enumeration)13 HashMap (java.util.HashMap)13 Map (java.util.Map)13 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)13 ClientAbortException (org.apache.catalina.connector.ClientAbortException)13 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)13 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)13 BDDMockito.given (org.mockito.BDDMockito.given)13 BDDMockito.then (org.mockito.BDDMockito.then)13