Search in sources :

Example 1 with HttpServletRequestWrapper

use of jakarta.servlet.http.HttpServletRequestWrapper in project atmosphere by Atmosphere.

the class AtmosphereRequestImpl method getHeader.

@Override
public String getHeader(String s, boolean checkCase) {
    if ("content-type".equalsIgnoreCase(s)) {
        return getContentType();
    }
    String name = b.request.getHeader(s);
    if (name == null) {
        if (b.headers.get(s) != null) {
            return b.headers.get(s);
        }
        if (s.startsWith(X_ATMOSPHERE) && isNotNoOps()) {
            // Craziness with Struts 2 who wraps String attribute as BigDecimal
            // https://github.com/Atmosphere/atmosphere/issues/1367
            Object o = attributeWithoutException(b.request, s);
            if (o == null || String.class.isAssignableFrom(o.getClass())) {
                name = (String) o;
            } else {
                try {
                    if (HttpServletRequestWrapper.class.isAssignableFrom(b.request.getClass())) {
                        HttpServletRequest hsr = b.request;
                        while (hsr instanceof HttpServletRequestWrapper) {
                            hsr = (HttpServletRequest) ((HttpServletRequestWrapper) hsr).getRequest();
                            o = attributeWithoutException(hsr, s);
                            if (o == null || String.class.isAssignableFrom(o.getClass())) {
                                name = (String) o;
                                break;
                            }
                        }
                    }
                } catch (Exception ex) {
                    logger.warn("", ex);
                }
            }
        }
    }
    if (name == null && checkCase) {
        return getHeader(s.toLowerCase(), false);
    }
    if (name == null && "connection".equalsIgnoreCase(s)) {
        return "keep-alive";
    }
    return name;
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) HttpServletRequestWrapper(jakarta.servlet.http.HttpServletRequestWrapper) ServletException(jakarta.servlet.ServletException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 2 with HttpServletRequestWrapper

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

the class FilterChainProxyTests method setup.

@BeforeEach
public void setup() throws Exception {
    this.matcher = mock(RequestMatcher.class);
    this.filter = mock(Filter.class);
    willAnswer((Answer<Object>) (inv) -> {
        Object[] args = inv.getArguments();
        FilterChain fc = (FilterChain) args[2];
        HttpServletRequestWrapper extraWrapper = new HttpServletRequestWrapper((HttpServletRequest) args[0]);
        fc.doFilter(extraWrapper, (HttpServletResponse) args[1]);
        return null;
    }).given(this.filter).doFilter(any(), any(), any());
    this.fcp = new FilterChainProxy(new DefaultSecurityFilterChain(this.matcher, Arrays.asList(this.filter)));
    this.fcp.setFilterChainValidator(mock(FilterChainProxy.FilterChainValidator.class));
    this.request = new MockHttpServletRequest("GET", "");
    this.request.setServletPath("/path");
    this.response = new MockHttpServletResponse();
    this.chain = mock(FilterChain.class);
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken) RequestRejectedException(org.springframework.security.web.firewall.RequestRejectedException) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ServletException(jakarta.servlet.ServletException) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Filter(jakarta.servlet.Filter) HttpServletRequestWrapper(jakarta.servlet.http.HttpServletRequestWrapper) Mockito.verifyZeroInteractions(org.mockito.Mockito.verifyZeroInteractions) Answer(org.mockito.stubbing.Answer) BDDMockito.given(org.mockito.BDDMockito.given) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) SecurityContextHolder(org.springframework.security.core.context.SecurityContextHolder) RequestRejectedHandler(org.springframework.security.web.firewall.RequestRejectedHandler) FilterChain(jakarta.servlet.FilterChain) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) RequestMatcher(org.springframework.security.web.util.matcher.RequestMatcher) HttpFirewall(org.springframework.security.web.firewall.HttpFirewall) FirewalledRequest(org.springframework.security.web.firewall.FirewalledRequest) BDDMockito.willAnswer(org.mockito.BDDMockito.willAnswer) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) List(java.util.List) AfterEach(org.junit.jupiter.api.AfterEach) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) RequestMatcher(org.springframework.security.web.util.matcher.RequestMatcher) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) FilterChain(jakarta.servlet.FilterChain) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Filter(jakarta.servlet.Filter) HttpServletRequestWrapper(jakarta.servlet.http.HttpServletRequestWrapper) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 3 with HttpServletRequestWrapper

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

the class DispatcherServletTests method existingMultipartRequestButWrapped.

@Test
public void existingMultipartRequestButWrapped() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/locale.do;abc=def");
    request.addPreferredLocale(Locale.CANADA);
    request.addUserRole("role1");
    MockHttpServletResponse response = new MockHttpServletResponse();
    ComplexWebApplicationContext.MockMultipartResolver multipartResolver = (ComplexWebApplicationContext.MockMultipartResolver) complexDispatcherServlet.getWebApplicationContext().getBean("multipartResolver");
    MultipartHttpServletRequest multipartRequest = multipartResolver.resolveMultipart(request);
    complexDispatcherServlet.service(new HttpServletRequestWrapper(multipartRequest), response);
    multipartResolver.cleanupMultipart(multipartRequest);
    assertThat(request.getAttribute(SimpleMappingExceptionResolver.DEFAULT_EXCEPTION_ATTRIBUTE)).isNull();
    assertThat(request.getAttribute("cleanedUp")).isNotNull();
}
Also used : HttpServletRequestWrapper(jakarta.servlet.http.HttpServletRequestWrapper) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) MultipartHttpServletRequest(org.springframework.web.multipart.MultipartHttpServletRequest) Test(org.junit.jupiter.api.Test)

Example 4 with HttpServletRequestWrapper

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

the class RequestPartServletServerHttpRequestTests method getBodyWithWrappedRequest.

// SPR-13317
@Test
public void getBodyWithWrappedRequest() throws Exception {
    byte[] bytes = "content".getBytes("UTF-8");
    MultipartFile part = new MockMultipartFile("part", "", "application/json", bytes);
    this.mockRequest.addFile(part);
    HttpServletRequest wrapped = new HttpServletRequestWrapper(this.mockRequest);
    ServerHttpRequest request = new RequestPartServletServerHttpRequest(wrapped, "part");
    byte[] result = FileCopyUtils.copyToByteArray(request.getBody());
    assertThat(result).isEqualTo(bytes);
}
Also used : MockMultipartFile(org.springframework.web.testfixture.servlet.MockMultipartFile) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) MockMultipartHttpServletRequest(org.springframework.web.testfixture.servlet.MockMultipartHttpServletRequest) MultipartFile(org.springframework.web.multipart.MultipartFile) MockMultipartFile(org.springframework.web.testfixture.servlet.MockMultipartFile) HttpServletRequestWrapper(jakarta.servlet.http.HttpServletRequestWrapper) ServerHttpRequest(org.springframework.http.server.ServerHttpRequest) Test(org.junit.jupiter.api.Test)

Example 5 with HttpServletRequestWrapper

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

the class ServletWebRequestTests method decoratedNativeRequest.

@Test
public void decoratedNativeRequest() {
    HttpServletRequest decoratedRequest = new HttpServletRequestWrapper(servletRequest);
    HttpServletResponse decoratedResponse = new HttpServletResponseWrapper(servletResponse);
    ServletWebRequest request = new ServletWebRequest(decoratedRequest, decoratedResponse);
    assertThat(request.getNativeRequest()).isSameAs(decoratedRequest);
    assertThat(request.getNativeRequest(ServletRequest.class)).isSameAs(decoratedRequest);
    assertThat(request.getNativeRequest(HttpServletRequest.class)).isSameAs(decoratedRequest);
    assertThat(request.getNativeRequest(MockHttpServletRequest.class)).isSameAs(servletRequest);
    assertThat(request.getNativeRequest(MultipartRequest.class)).isNull();
    assertThat(request.getNativeResponse()).isSameAs(decoratedResponse);
    assertThat(request.getNativeResponse(ServletResponse.class)).isSameAs(decoratedResponse);
    assertThat(request.getNativeResponse(HttpServletResponse.class)).isSameAs(decoratedResponse);
    assertThat(request.getNativeResponse(MockHttpServletResponse.class)).isSameAs(servletResponse);
    assertThat(request.getNativeResponse(MultipartRequest.class)).isNull();
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) HttpServletRequestWrapper(jakarta.servlet.http.HttpServletRequestWrapper) HttpServletResponseWrapper(jakarta.servlet.http.HttpServletResponseWrapper) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) Test(org.junit.jupiter.api.Test)

Aggregations

HttpServletRequestWrapper (jakarta.servlet.http.HttpServletRequestWrapper)9 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)7 Test (org.junit.jupiter.api.Test)7 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)4 ServletException (jakarta.servlet.ServletException)2 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)2 HttpServletResponseWrapper (jakarta.servlet.http.HttpServletResponseWrapper)2 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)2 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)2 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)2 Filter (jakarta.servlet.Filter)1 FilterChain (jakarta.servlet.FilterChain)1 ServletRequest (jakarta.servlet.ServletRequest)1 Cookie (jakarta.servlet.http.Cookie)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 List (java.util.List)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1