Search in sources :

Example 11 with HttpServletRequestWrapper

use of javax.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());
    assertArrayEquals(bytes, result);
}
Also used : MockMultipartFile(org.springframework.mock.web.test.MockMultipartFile) MockMultipartHttpServletRequest(org.springframework.mock.web.test.MockMultipartHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) MockMultipartFile(org.springframework.mock.web.test.MockMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) HttpServletRequestWrapper(javax.servlet.http.HttpServletRequestWrapper) ServerHttpRequest(org.springframework.http.server.ServerHttpRequest) Test(org.junit.Test)

Example 12 with HttpServletRequestWrapper

use of javax.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);
    assertNull(request.getAttribute(SimpleMappingExceptionResolver.DEFAULT_EXCEPTION_ATTRIBUTE));
    assertNotNull(request.getAttribute("cleanedUp"));
}
Also used : HttpServletRequestWrapper(javax.servlet.http.HttpServletRequestWrapper) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) MultipartHttpServletRequest(org.springframework.web.multipart.MultipartHttpServletRequest) Test(org.junit.Test)

Example 13 with HttpServletRequestWrapper

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

the class FilterChainProxyTests method setup.

@Before
public void setup() throws Exception {
    matcher = mock(RequestMatcher.class);
    filter = mock(Filter.class);
    doAnswer(new Answer<Object>() {

        public Object answer(InvocationOnMock inv) throws Throwable {
            Object[] args = inv.getArguments();
            FilterChain fc = (FilterChain) args[2];
            HttpServletRequestWrapper extraWrapper = new HttpServletRequestWrapper((HttpServletRequest) args[0]);
            fc.doFilter(extraWrapper, (HttpServletResponse) args[1]);
            return null;
        }
    }).when(filter).doFilter(any(HttpServletRequest.class), any(HttpServletResponse.class), any(FilterChain.class));
    fcp = new FilterChainProxy(new DefaultSecurityFilterChain(matcher, Arrays.asList(filter)));
    fcp.setFilterChainValidator(mock(FilterChainProxy.FilterChainValidator.class));
    request = new MockHttpServletRequest();
    request.setServletPath("/path");
    response = new MockHttpServletResponse();
    chain = mock(FilterChain.class);
}
Also used : RequestMatcher(org.springframework.security.web.util.matcher.RequestMatcher) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) FilterChain(javax.servlet.FilterChain) HttpServletResponse(javax.servlet.http.HttpServletResponse) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) Filter(javax.servlet.Filter) HttpServletRequestWrapper(javax.servlet.http.HttpServletRequestWrapper) InvocationOnMock(org.mockito.invocation.InvocationOnMock) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Before(org.junit.Before)

Example 14 with HttpServletRequestWrapper

use of javax.servlet.http.HttpServletRequestWrapper in project guice by google.

the class ServletTest method testRequestAndResponseBindings_matchesPassedParameters.

public void testRequestAndResponseBindings_matchesPassedParameters() throws Exception {
    final int[] filterInvoked = new int[1];
    final boolean[] servletInvoked = new boolean[1];
    createInjector(new ServletModule() {

        @Override
        protected void configureServlets() {
            final HttpServletRequest[] previousReq = new HttpServletRequest[1];
            final HttpServletResponse[] previousResp = new HttpServletResponse[1];
            final Provider<ServletRequest> servletReqProvider = getProvider(ServletRequest.class);
            final Provider<HttpServletRequest> reqProvider = getProvider(HttpServletRequest.class);
            final Provider<ServletResponse> servletRespProvider = getProvider(ServletResponse.class);
            final Provider<HttpServletResponse> respProvider = getProvider(HttpServletResponse.class);
            Filter filter = new Filter() {

                @Override
                public void init(FilterConfig filterConfig) {
                }

                @Override
                public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
                    filterInvoked[0]++;
                    assertSame(req, servletReqProvider.get());
                    assertSame(req, reqProvider.get());
                    if (previousReq[0] != null) {
                        assertEquals(req, previousReq[0]);
                    }
                    assertSame(resp, servletRespProvider.get());
                    assertSame(resp, respProvider.get());
                    if (previousResp[0] != null) {
                        assertEquals(resp, previousResp[0]);
                    }
                    chain.doFilter(previousReq[0] = new HttpServletRequestWrapper((HttpServletRequest) req), previousResp[0] = new HttpServletResponseWrapper((HttpServletResponse) resp));
                    assertSame(req, reqProvider.get());
                    assertSame(resp, respProvider.get());
                }

                @Override
                public void destroy() {
                }
            };
            filter("/*").through(filter);
            // filter twice to test wrapping in filters
            filter("/*").through(filter);
            serve("/*").with(new HttpServlet() {

                @Override
                protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
                    servletInvoked[0] = true;
                    assertSame(req, servletReqProvider.get());
                    assertSame(req, reqProvider.get());
                    assertSame(resp, servletRespProvider.get());
                    assertSame(resp, respProvider.get());
                }
            });
        }
    });
    GuiceFilter filter = new GuiceFilter();
    filter.doFilter(newFakeHttpServletRequest(), newFakeHttpServletResponse(), new FilterChain() {

        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) {
            throw new IllegalStateException("Shouldn't get here");
        }
    });
    assertEquals(2, filterInvoked[0]);
    assertTrue(servletInvoked[0]);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequest(javax.servlet.ServletRequest) ServletTestUtils.newFakeHttpServletRequest(com.google.inject.servlet.ServletTestUtils.newFakeHttpServletRequest) ServletTestUtils.newFakeHttpServletResponse(com.google.inject.servlet.ServletTestUtils.newFakeHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) HttpServlet(javax.servlet.http.HttpServlet) FilterChain(javax.servlet.FilterChain) HttpServletResponseWrapper(javax.servlet.http.HttpServletResponseWrapper) ServletTestUtils.newFakeHttpServletResponse(com.google.inject.servlet.ServletTestUtils.newFakeHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) Provider(com.google.inject.Provider) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletTestUtils.newFakeHttpServletRequest(com.google.inject.servlet.ServletTestUtils.newFakeHttpServletRequest) ServletException(javax.servlet.ServletException) Filter(javax.servlet.Filter) HttpServletRequestWrapper(javax.servlet.http.HttpServletRequestWrapper) FilterConfig(javax.servlet.FilterConfig)

Example 15 with HttpServletRequestWrapper

use of javax.servlet.http.HttpServletRequestWrapper in project guice by google.

the class ServletTest method testRequestAndResponseBindings_wrappingFilter.

public void testRequestAndResponseBindings_wrappingFilter() throws Exception {
    final HttpServletRequest request = newFakeHttpServletRequest();
    final ImmutableMap<String, String[]> wrappedParamMap = ImmutableMap.of("wrap", new String[] { "a", "b" });
    final HttpServletRequestWrapper requestWrapper = new HttpServletRequestWrapper(request) {

        @Override
        public Map getParameterMap() {
            return wrappedParamMap;
        }

        @Override
        public Object getAttribute(String attr) {
            // Ensure that attributes are stored on the original request object.
            throw new UnsupportedOperationException();
        }
    };
    final HttpServletResponse response = newFakeHttpServletResponse();
    final HttpServletResponseWrapper responseWrapper = new HttpServletResponseWrapper(response);
    final boolean[] filterInvoked = new boolean[1];
    final Injector injector = createInjector(new ServletModule() {

        @Override
        protected void configureServlets() {
            filter("/*").through(new Filter() {

                @Inject
                Provider<ServletRequest> servletReqProvider;

                @Inject
                Provider<HttpServletRequest> reqProvider;

                @Inject
                Provider<ServletResponse> servletRespProvider;

                @Inject
                Provider<HttpServletResponse> respProvider;

                @Override
                public void init(FilterConfig filterConfig) {
                }

                @Override
                public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
                    filterInvoked[0] = true;
                    assertSame(req, servletReqProvider.get());
                    assertSame(req, reqProvider.get());
                    assertSame(resp, servletRespProvider.get());
                    assertSame(resp, respProvider.get());
                    chain.doFilter(requestWrapper, responseWrapper);
                    assertSame(req, reqProvider.get());
                    assertSame(resp, respProvider.get());
                }

                @Override
                public void destroy() {
                }
            });
        }
    });
    GuiceFilter filter = new GuiceFilter();
    final boolean[] chainInvoked = new boolean[1];
    FilterChain filterChain = new FilterChain() {

        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) {
            chainInvoked[0] = true;
            assertSame(requestWrapper, servletRequest);
            assertSame(requestWrapper, injector.getInstance(ServletRequest.class));
            assertSame(requestWrapper, injector.getInstance(HTTP_REQ_KEY));
            assertSame(responseWrapper, servletResponse);
            assertSame(responseWrapper, injector.getInstance(ServletResponse.class));
            assertSame(responseWrapper, injector.getInstance(HTTP_RESP_KEY));
            assertSame(servletRequest.getParameterMap(), injector.getInstance(REQ_PARAMS_KEY));
            InRequest inRequest = injector.getInstance(InRequest.class);
            assertSame(inRequest, injector.getInstance(InRequest.class));
        }
    };
    filter.doFilter(request, response, filterChain);
    assertTrue(chainInvoked[0]);
    assertTrue(filterInvoked[0]);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequest(javax.servlet.ServletRequest) ServletTestUtils.newFakeHttpServletRequest(com.google.inject.servlet.ServletTestUtils.newFakeHttpServletRequest) ServletTestUtils.newFakeHttpServletResponse(com.google.inject.servlet.ServletTestUtils.newFakeHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) FilterChain(javax.servlet.FilterChain) HttpServletResponseWrapper(javax.servlet.http.HttpServletResponseWrapper) ServletTestUtils.newFakeHttpServletResponse(com.google.inject.servlet.ServletTestUtils.newFakeHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) Provider(com.google.inject.Provider) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletTestUtils.newFakeHttpServletRequest(com.google.inject.servlet.ServletTestUtils.newFakeHttpServletRequest) HttpServletRequestWrapper(javax.servlet.http.HttpServletRequestWrapper) Filter(javax.servlet.Filter) Injector(com.google.inject.Injector) FilterConfig(javax.servlet.FilterConfig)

Aggregations

HttpServletRequestWrapper (javax.servlet.http.HttpServletRequestWrapper)51 HttpServletRequest (javax.servlet.http.HttpServletRequest)40 HttpServletResponse (javax.servlet.http.HttpServletResponse)17 ServletRequest (javax.servlet.ServletRequest)12 ServletResponse (javax.servlet.ServletResponse)10 IOException (java.io.IOException)9 FilterChain (javax.servlet.FilterChain)9 Test (org.junit.Test)9 ServletException (javax.servlet.ServletException)8 HttpServletResponseWrapper (javax.servlet.http.HttpServletResponseWrapper)8 Principal (java.security.Principal)6 FilterConfig (javax.servlet.FilterConfig)6 ArrayList (java.util.ArrayList)5 Filter (javax.servlet.Filter)5 Provider (com.google.inject.Provider)4 ServletTestUtils.newFakeHttpServletRequest (com.google.inject.servlet.ServletTestUtils.newFakeHttpServletRequest)4 ServletTestUtils.newFakeHttpServletResponse (com.google.inject.servlet.ServletTestUtils.newFakeHttpServletResponse)4 OutputStream (java.io.OutputStream)4 Cookie (javax.servlet.http.Cookie)4 Injector (com.google.inject.Injector)2