/** * Extracts the message payload portion of the message created by * {@link #createMessage(HttpServletRequest, String, String)} when * {@link #isIncludePayload()} returns true. * @since 5.0.3 */ @Nullable protected String getMessagePayload(HttpServletRequest request) { ContentCachingRequestWrapper wrapper = WebUtils.getNativeRequest(request, ContentCachingRequestWrapper.class); if (wrapper != null) { byte[] buf = wrapper.getContentAsByteArray(); if (buf.length > 0) { int length = Math.min(buf.length, getMaxPayloadLength()); try { return new String(buf, 0, length, wrapper.getCharacterEncoding()); } catch (UnsupportedEncodingException ex) { return "[unknown]"; } } } return null; }
/** * Extracts the message payload portion of the message created by * {@link #createMessage(HttpServletRequest, String, String)} when * {@link #isIncludePayload()} returns true. * @since 5.0.3 */ @Nullable protected String getMessagePayload(HttpServletRequest request) { ContentCachingRequestWrapper wrapper = WebUtils.getNativeRequest(request, ContentCachingRequestWrapper.class); if (wrapper != null) { byte[] buf = wrapper.getContentAsByteArray(); if (buf.length > 0) { int length = Math.min(buf.length, getMaxPayloadLength()); try { return new String(buf, 0, length, wrapper.getCharacterEncoding()); } catch (UnsupportedEncodingException ex) { return "[unknown]"; } } } return null; }
@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()); } };
@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()); }
@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()))); }
@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()); }
public static String getRequestData(final HttpServletRequest request) throws UnsupportedEncodingException { String payload = null; ContentCachingRequestWrapper wrapper = WebUtils.getNativeRequest(request, ContentCachingRequestWrapper.class); if (wrapper != null) { byte[] buf = wrapper.getContentAsByteArray(); if (buf.length > 0) { payload = new String(buf, 0, buf.length, wrapper.getCharacterEncoding()); } } return payload; }
@Deprecated private String getBody(ContentCachingRequestWrapper request) { // wrap request to make sure we can read the body of the request (otherwise it will be consumed by the actual // request handler) ContentCachingRequestWrapper wrapper = WebUtils .getNativeRequest(request, ContentCachingRequestWrapper.class); if (wrapper != null) { byte[] buf = wrapper.getContentAsByteArray(); if (buf.length > 0) { String payload; try { payload = new String(buf, 0, buf.length, wrapper.getCharacterEncoding()); } catch (UnsupportedEncodingException ex) { payload = "unknown"; } return payload; } } return "no body"; }
@ControllerAdvice(annotations = RestController.class) public class GlobalExceptionHandlingControllerAdvice { private ContentCachingRequestWrapper getUnderlyingCachingRequest(ServletRequest request) { if (ContentCachingRequestWrapper.class.isAssignableFrom(request.getClass())) { return (ContentCachingRequestWrapper) request; } if (request instanceof ServletRequestWrapper) { return getUnderlyingCachingRequest(((ServletRequestWrapper)request).getRequest()); } return null; } @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR) @ExceptionHandler(Throwable.class) public @ResponseBody Map<String, String> conflict(Throwable exception, HttpServletRequest request) { ContentCachingRequestWrapper underlyingCachingRequest = getUnderlyingCachingRequest(request); String body = new String(underlyingCachingRequest.getContentAsByteArray(),Charsets.UTF_8); .... } }
/** * Extracts the message payload portion of the message created by * {@link #createMessage(HttpServletRequest, String, String)} when * {@link #isIncludePayload()} returns true. * @since 5.0.3 */ @Nullable protected String getMessagePayload(HttpServletRequest request) { ContentCachingRequestWrapper wrapper = WebUtils.getNativeRequest(request, ContentCachingRequestWrapper.class); if (wrapper != null) { byte[] buf = wrapper.getContentAsByteArray(); if (buf.length > 0) { int length = Math.min(buf.length, getMaxPayloadLength()); try { return new String(buf, 0, length, wrapper.getCharacterEncoding()); } catch (UnsupportedEncodingException ex) { return "[unknown]"; } } } return null; }
@SneakyThrows public static String curlBody(final HttpServletRequest request) { final Charset charset = RequestUtlis.findCharset(request); try { // read raw inputStream first. (may be has not been read, for example 404) final String raw = StreamUtils.copyToString(request.getInputStream(), charset); final String result; if (isBlank(raw)) { // if no content in raw inputStream, 那应该是读过了, try to read cached. final ContentCachingRequestWrapper wrapper = findWrapper(request, ContentCachingRequestWrapper.class); if (wrapper != null) { result = new String(((ContentCachingRequestWrapper) request).getContentAsByteArray(), charset); } else { result = ""; } } else { result = raw; } return result; } catch (final IOException ex) { log.warn("error reading request body.", ex); } return ""; } }
byte[] buf = wrapper.getContentAsByteArray(); if (buf.length > 0) { int length = Math.min(buf.length, getRequestMaxPayloadLength());
/** * Extracts the message payload portion of the message created by * {@link #createMessage(HttpServletRequest, String, String)} when * {@link #isIncludePayload()} returns true. * @since 5.0.3 */ @Nullable protected String getMessagePayload(HttpServletRequest request) { ContentCachingRequestWrapper wrapper = WebUtils.getNativeRequest(request, ContentCachingRequestWrapper.class); if (wrapper != null) { byte[] buf = wrapper.getContentAsByteArray(); if (buf.length > 0) { int length = Math.min(buf.length, getMaxPayloadLength()); try { return new String(buf, 0, length, wrapper.getCharacterEncoding()); } catch (UnsupportedEncodingException ex) { return "[unknown]"; } } } return null; }
} finally { String requestBody = new String(requestWrapper.getContentAsByteArray()); String responseBody = new String(responseWrapper.getContentAsByteArray());