use of org.springframework.web.util.ContentCachingResponseWrapper in project plumdo-work by wengwh.
the class RequestLogFilter method doFilterInternal.
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
long begintime = DateUtils.currentTimeMillis();
JsonContentCachingRequestWrapper requestWrapper = new JsonContentCachingRequestWrapper(request);
ContentCachingResponseWrapper responseWrapper = new ContentCachingResponseWrapper(response);
filterChain.doFilter(requestWrapper, responseWrapper);
long timecost = DateUtils.getTimeMillisConsume(begintime);
String requestURI = request.getRequestURI();
String ip = request.getRemoteAddr();
String requestParam = convertString(requestWrapper.getContentAsByteArray());
updateResponse(requestURI, responseWrapper);
if (isNotJsonContentType(responseWrapper.getContentType())) {
logger.debug("ip:{} 调用接口,请求地址为:{}, 请求参数为:{},[{}]ms", ip, requestURI, requestParam, timecost);
} else {
String result = convertString(responseWrapper.getContentAsByteArray());
logger.debug("ip:{} 调用接口,请求地址为:{}, 请求参数为:{},返回值是{},[{}]ms", ip, requestURI, requestParam, result, timecost);
}
}
use of org.springframework.web.util.ContentCachingResponseWrapper in project BroadleafCommerce by BroadleafCommerce.
the class CachingCompressedResponseFilter method processDynamic.
protected void processDynamic(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
ContentCachingResponseWrapper wrapper = new ContentCachingResponseWrapper(response);
chain.doFilter(request, wrapper);
FastByteArrayOutputStream baos = new FastByteArrayOutputStream(1024);
GZIPOutputStream zip = new GZIPOutputStream(baos);
StreamUtils.copy(wrapper.getContentInputStream(), zip);
zip.close();
response.addHeader("Content-Encoding", "gzip");
response.setContentLength(baos.size());
StreamUtils.copy(new ByteArrayInputStream(baos.toByteArray()), response.getOutputStream());
}
use of org.springframework.web.util.ContentCachingResponseWrapper in project spring-framework by spring-projects.
the class ContentCachingResponseWrapperTests method copyBodyToResponseWithTransferEncoding.
@Test
void copyBodyToResponseWithTransferEncoding() throws Exception {
byte[] responseBody = "6\r\nHello 5\r\nWorld0\r\n\r\n".getBytes(StandardCharsets.UTF_8);
MockHttpServletResponse response = new MockHttpServletResponse();
ContentCachingResponseWrapper responseWrapper = new ContentCachingResponseWrapper(response);
responseWrapper.setStatus(HttpServletResponse.SC_OK);
responseWrapper.setHeader(HttpHeaders.TRANSFER_ENCODING, "chunked");
FileCopyUtils.copy(responseBody, responseWrapper.getOutputStream());
responseWrapper.copyBodyToResponse();
assertThat(response.getStatus()).isEqualTo(200);
assertThat(response.getHeader(HttpHeaders.TRANSFER_ENCODING)).isEqualTo("chunked");
assertThat(response.getHeader(HttpHeaders.CONTENT_LENGTH)).isNull();
assertThat(response.getContentAsByteArray()).isEqualTo(responseBody);
}
use of org.springframework.web.util.ContentCachingResponseWrapper in project spring-framework by spring-projects.
the class ContentCachingResponseWrapperTests method copyBodyToResponse.
@Test
void copyBodyToResponse() throws Exception {
byte[] responseBody = "Hello World".getBytes(StandardCharsets.UTF_8);
MockHttpServletResponse response = new MockHttpServletResponse();
ContentCachingResponseWrapper responseWrapper = new ContentCachingResponseWrapper(response);
responseWrapper.setStatus(HttpServletResponse.SC_OK);
FileCopyUtils.copy(responseBody, responseWrapper.getOutputStream());
responseWrapper.copyBodyToResponse();
assertThat(response.getStatus()).isEqualTo(200);
assertThat(response.getContentLength() > 0).isTrue();
assertThat(response.getContentAsByteArray()).isEqualTo(responseBody);
}
use of org.springframework.web.util.ContentCachingResponseWrapper in project xm-ms-entity by xm-online.
the class ContentCachingWrappingFilter method doFilterInternal.
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
ContentCachingRequestWrapper requestWrapper = new ContentCachingRequestWrapper(request);
ContentCachingResponseWrapper responseWrapper = new ContentCachingResponseWrapper(response);
try {
filterChain.doFilter(requestWrapper, responseWrapper);
} finally {
responseWrapper.copyBodyToResponse();
}
}
Aggregations