Search in sources :

Example 1 with ContentCachingResponseWrapper

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);
    }
}
Also used : ContentCachingResponseWrapper(org.springframework.web.util.ContentCachingResponseWrapper)

Example 2 with ContentCachingResponseWrapper

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());
}
Also used : ContentCachingResponseWrapper(org.springframework.web.util.ContentCachingResponseWrapper) FastByteArrayOutputStream(org.springframework.util.FastByteArrayOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 3 with ContentCachingResponseWrapper

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);
}
Also used : ContentCachingResponseWrapper(org.springframework.web.util.ContentCachingResponseWrapper) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 4 with ContentCachingResponseWrapper

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);
}
Also used : ContentCachingResponseWrapper(org.springframework.web.util.ContentCachingResponseWrapper) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 5 with ContentCachingResponseWrapper

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();
    }
}
Also used : ContentCachingResponseWrapper(org.springframework.web.util.ContentCachingResponseWrapper) ContentCachingRequestWrapper(org.springframework.web.util.ContentCachingRequestWrapper)

Aggregations

ContentCachingResponseWrapper (org.springframework.web.util.ContentCachingResponseWrapper)10 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 ContentCachingRequestWrapper (org.springframework.web.util.ContentCachingRequestWrapper)3 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 Test (org.junit.jupiter.api.Test)2 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)2 Request (com.atlassian.oai.validator.model.Request)1 Response (com.atlassian.oai.validator.model.Response)1 ValidationReport (com.atlassian.oai.validator.report.ValidationReport)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 GZIPOutputStream (java.util.zip.GZIPOutputStream)1 ServletException (javax.servlet.ServletException)1 SneakyThrows (lombok.SneakyThrows)1 lombok.val (lombok.val)1 FastByteArrayOutputStream (org.springframework.util.FastByteArrayOutputStream)1 StopWatch (org.springframework.util.StopWatch)1