org.springframework.web.util.ContentCachingRequestWrapper Java Examples

The following examples show how to use org.springframework.web.util.ContentCachingRequestWrapper. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RequestLoggingFilterTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void payloadMaxLength() throws Exception {
	filter.setIncludePayload(true);
	filter.setMaxPayloadLength(3);

	final MockHttpServletRequest request = new MockHttpServletRequest("POST", "/hotels");
	MockHttpServletResponse response = new MockHttpServletResponse();

	final byte[] requestBody = "Hello World".getBytes(StandardCharsets.UTF_8);
	request.setContent(requestBody);

	FilterChain filterChain = (filterRequest, filterResponse) -> {
		((HttpServletResponse) filterResponse).setStatus(HttpServletResponse.SC_OK);
		byte[] buf = FileCopyUtils.copyToByteArray(filterRequest.getInputStream());
		assertArrayEquals(requestBody, buf);
		ContentCachingRequestWrapper wrapper =
				WebUtils.getNativeRequest(filterRequest, ContentCachingRequestWrapper.class);
		assertArrayEquals("Hel".getBytes(StandardCharsets.UTF_8), wrapper.getContentAsByteArray());
	};

	filter.doFilter(request, response, filterChain);

	assertNotNull(filter.afterRequestMessage);
	assertTrue(filter.afterRequestMessage.contains("Hel"));
	assertFalse(filter.afterRequestMessage.contains("Hello World"));
}
 
Example #2
Source File: RequestBodyCacheFilter.java    From summerframework with Apache License 2.0 6 votes vote down vote up
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
    throws IOException, ServletException {
    if (!StringUtils.isEmpty(request.getContentType())
        && MediaType.APPLICATION_JSON.isCompatibleWith(MediaType.valueOf(request.getContentType()))) {
        chain.doFilter(new ContentCachingRequestWrapper((HttpServletRequest)request), response);
    } else {
        chain.doFilter(request, response);
    }
}
 
Example #3
Source File: RequestLoggingUtil.java    From tutorials with MIT License 6 votes vote down vote up
public static String readPayload(final HttpServletRequest request) throws IOException {
    String payloadData = null;
    ContentCachingRequestWrapper contentCachingRequestWrapper = WebUtils.getNativeRequest(request, ContentCachingRequestWrapper.class);
    if (null != contentCachingRequestWrapper) {
        byte[] buf = contentCachingRequestWrapper.getContentAsByteArray();
        if (buf.length > 0) {
            payloadData = new String(buf, 0, buf.length, contentCachingRequestWrapper.getCharacterEncoding());
        }
    }
    return payloadData;
}
 
Example #4
Source File: RequestLoggingFilterTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void payloadMaxLength() throws Exception {
	filter.setIncludePayload(true);
	filter.setMaxPayloadLength(3);

	final MockHttpServletRequest request = new MockHttpServletRequest("POST", "/hotels");
	MockHttpServletResponse response = new MockHttpServletResponse();

	final byte[] requestBody = "Hello World".getBytes("UTF-8");
	request.setContent(requestBody);

	FilterChain filterChain = new FilterChain() {
		@Override
		public void doFilter(ServletRequest filterRequest, ServletResponse filterResponse)
				throws IOException, ServletException {
			((HttpServletResponse) filterResponse).setStatus(HttpServletResponse.SC_OK);
			byte[] buf = FileCopyUtils.copyToByteArray(filterRequest.getInputStream());
			assertArrayEquals(requestBody, buf);
			ContentCachingRequestWrapper wrapper =
					WebUtils.getNativeRequest(filterRequest, ContentCachingRequestWrapper.class);
			assertArrayEquals("Hel".getBytes("UTF-8"), wrapper.getContentAsByteArray());
		}
	};

	filter.doFilter(request, response, filterChain);

	assertNotNull(filter.afterRequestMessage);
	assertTrue(filter.afterRequestMessage.contains("Hel"));
	assertFalse(filter.afterRequestMessage.contains("Hello World"));
}