@Test public void cachedContentWithLimit() throws Exception { this.request.setMethod("GET"); this.request.setCharacterEncoding(CHARSET); this.request.setContent("Hello World".getBytes(CHARSET)); ContentCachingRequestWrapper wrapper = new ContentCachingRequestWrapper(this.request, 3); byte[] response = FileCopyUtils.copyToByteArray(wrapper.getInputStream()); assertArrayEquals("Hello World".getBytes(CHARSET), response); assertArrayEquals("Hel".getBytes(CHARSET), wrapper.getContentAsByteArray()); }
/** * Forwards the request to the next filter in the chain and delegates down to the subclasses * to perform the actual request logging both before and after the request is processed. * @see #beforeRequest * @see #afterRequest */ @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { boolean isFirstRequest = !isAsyncDispatch(request); HttpServletRequest requestToUse = request; if (isIncludePayload() && isFirstRequest && !(request instanceof ContentCachingRequestWrapper)) { requestToUse = new ContentCachingRequestWrapper(request, getMaxPayloadLength()); } boolean shouldLog = shouldLog(requestToUse); if (shouldLog && isFirstRequest) { beforeRequest(requestToUse, getBeforeMessage(requestToUse)); } try { filterChain.doFilter(requestToUse, response); } finally { if (shouldLog && !isAsyncStarted(requestToUse)) { afterRequest(requestToUse, getAfterMessage(requestToUse)); } } }
@Test public void cachedContent() throws Exception { this.request.setMethod("GET"); this.request.setCharacterEncoding(CHARSET); this.request.setContent("Hello World".getBytes(CHARSET)); ContentCachingRequestWrapper wrapper = new ContentCachingRequestWrapper(this.request); byte[] response = FileCopyUtils.copyToByteArray(wrapper.getInputStream()); assertArrayEquals(response, wrapper.getContentAsByteArray()); }
@Test public void requestParams() throws Exception { this.request.setMethod("POST"); this.request.setContentType(FORM_CONTENT_TYPE); this.request.setCharacterEncoding(CHARSET); this.request.setParameter("first", "value"); this.request.setParameter("second", "foo", "bar"); ContentCachingRequestWrapper wrapper = new ContentCachingRequestWrapper(this.request); // getting request parameters will consume the request body assertFalse(wrapper.getParameterMap().isEmpty()); assertEquals("first=value&second=foo&second=bar", new String(wrapper.getContentAsByteArray())); // SPR-12810 : inputstream body should be consumed assertEquals("", new String(FileCopyUtils.copyToByteArray(wrapper.getInputStream()))); }
/** * Forwards the request to the next filter in the chain and delegates down to the subclasses * to perform the actual request logging both before and after the request is processed. * @see #beforeRequest * @see #afterRequest */ @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { boolean isFirstRequest = !isAsyncDispatch(request); HttpServletRequest requestToUse = request; if (isIncludePayload() && isFirstRequest && !(request instanceof ContentCachingRequestWrapper)) { requestToUse = new ContentCachingRequestWrapper(request, getMaxPayloadLength()); } boolean shouldLog = shouldLog(requestToUse); if (shouldLog && isFirstRequest) { beforeRequest(requestToUse, getBeforeMessage(requestToUse)); } try { filterChain.doFilter(requestToUse, response); } finally { if (shouldLog && !isAsyncStarted(requestToUse)) { afterRequest(requestToUse, getAfterMessage(requestToUse)); } } }
@Test // SPR-12810 public void inputStreamFormPostRequest() throws Exception { this.request.setMethod("POST"); this.request.setContentType(FORM_CONTENT_TYPE); this.request.setCharacterEncoding(CHARSET); this.request.setParameter("first", "value"); this.request.setParameter("second", "foo", "bar"); ContentCachingRequestWrapper wrapper = new ContentCachingRequestWrapper(this.request); byte[] response = FileCopyUtils.copyToByteArray(wrapper.getInputStream()); assertArrayEquals(response, wrapper.getContentAsByteArray()); }
import org.springframework.web.util.ContentCachingRequestWrapper; public class RequestBodyCachingFilter implements Filter { public void init(FilterConfig fc) throws ServletException { } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { chain.doFilter(new ContentCachingRequestWrapper((HttpServletRequest)request), response); } public void destroy() { } }
@Override protected void doFilterInternal( // final HttpServletRequest request, // final HttpServletResponse response, // final FilterChain filterChain // ) throws ServletException, IOException { final RequestMethod requestMethod = RequestMethod.valueOf(request.getMethod()); final String contentType = request.getContentType(); final Boolean interested = requestMethod != GET && requestMethod != OPTIONS && // contentType != null && (contentType.contains("xml") || contentType.contains("json")); if (interested) { final ContentCachingRequestWrapper found = findWrapper(request, ContentCachingRequestWrapper.class); if (found == null) { filterChain.doFilter(new ContentCachingRequestWrapper(request), response); } else { filterChain.doFilter(request, response); } } else { filterChain.doFilter(request, response); } } }
@Override protected void doFilterInternal( // final HttpServletRequest request, // final HttpServletResponse response, // final FilterChain filterChain // ) throws ServletException, IOException { final RequestMethod requestMethod = RequestMethod.valueOf(request.getMethod()); final String contentType = request.getContentType(); final Boolean interested = requestMethod != GET && requestMethod != OPTIONS && // contentType != null && (contentType.contains("xml") || contentType.contains("json")); if (interested) { final ContentCachingRequestWrapper found = findWrapper(request, ContentCachingRequestWrapper.class); if (found == null) { filterChain.doFilter(new ContentCachingRequestWrapper(request), response); } else { filterChain.doFilter(request, response); } } else { filterChain.doFilter(request, response); } } }
throws IOException, ServletException { ContentCachingRequestWrapper requestWrapper = new ContentCachingRequestWrapper((HttpServletRequest) request); ContentCachingResponseWrapper responseWrapper = new ContentCachingResponseWrapper((HttpServletResponse) response);
requestToUse = new ContentCachingRequestWrapper(requestToUse);
/** * Forwards the request to the next filter in the chain and delegates down to the subclasses * to perform the actual request logging both before and after the request is processed. * @see #beforeRequest * @see #afterRequest */ @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { boolean isFirstRequest = !isAsyncDispatch(request); HttpServletRequest requestToUse = request; if (isIncludePayload() && isFirstRequest && !(request instanceof ContentCachingRequestWrapper)) { requestToUse = new ContentCachingRequestWrapper(request, getMaxPayloadLength()); } boolean shouldLog = shouldLog(requestToUse); if (shouldLog && isFirstRequest) { beforeRequest(requestToUse, getBeforeMessage(requestToUse)); } try { filterChain.doFilter(requestToUse, response); } finally { if (shouldLog && !isAsyncStarted(requestToUse)) { afterRequest(requestToUse, getAfterMessage(requestToUse)); } } }
/** * Forwards the request to the next filter in the chain and delegates down to the subclasses * to perform the actual request logging both before and after the request is processed. * @see #beforeRequest * @see #afterRequest */ @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { boolean isFirstRequest = !isAsyncDispatch(request); HttpServletRequest requestToUse = request; if (isIncludePayload() && isFirstRequest && !(request instanceof ContentCachingRequestWrapper)) { requestToUse = new ContentCachingRequestWrapper(request, getMaxPayloadLength()); } boolean shouldLog = shouldLog(requestToUse); if (shouldLog && isFirstRequest) { beforeRequest(requestToUse, getBeforeMessage(requestToUse)); } try { filterChain.doFilter(requestToUse, response); } finally { if (shouldLog && !isAsyncStarted(requestToUse)) { afterRequest(requestToUse, getAfterMessage(requestToUse)); } } }