Search in sources :

Example 6 with ContentCachingRequestWrapper

use of org.springframework.web.util.ContentCachingRequestWrapper 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)

Example 7 with ContentCachingRequestWrapper

use of org.springframework.web.util.ContentCachingRequestWrapper in project spring-framework by spring-projects.

the class AbstractRequestLoggingFilter method doFilterInternal.

/**
 * 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));
        }
    }
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) ContentCachingRequestWrapper(org.springframework.web.util.ContentCachingRequestWrapper)

Example 8 with ContentCachingRequestWrapper

use of org.springframework.web.util.ContentCachingRequestWrapper in project loc-framework by lord-of-code.

the class LocAccessLogFilter method doFilterInternal.

@Override
protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {
    if (ignoreRequest(httpServletRequest)) {
        filterChain.doFilter(httpServletRequest, httpServletResponse);
    } else {
        final boolean isFirstRequest = !isAsyncDispatch(httpServletRequest);
        final LocAccessLogger accessLogger = new LocAccessLogger(this.properties);
        HttpServletRequest requestToUse = httpServletRequest;
        ContentCachingResponseWrapper responseToUse = new ContentCachingResponseWrapper(httpServletResponse);
        StopWatch watch = new StopWatch();
        watch.start();
        if (isFirstRequest && !(httpServletRequest instanceof ContentCachingRequestWrapper)) {
            requestToUse = new ContentCachingRequestWrapper(httpServletRequest, properties.getRequestBodyLength());
        }
        try {
            filterChain.doFilter(requestToUse, responseToUse);
        } finally {
            if (isFirstRequest) {
                accessLogger.appendRequestCommonMessage(requestToUse);
                accessLogger.appendRequestDetailMessage(properties.isIncludeRequest(), requestToUse);
            }
            watch.stop();
            if (!isAsyncStarted(requestToUse)) {
                accessLogger.appendResponseCommonMessage(responseToUse, watch.getTotalTimeMillis());
                if (properties.isIncludeResponse() && !isBinaryContent(httpServletResponse) && !isMultipart(httpServletResponse)) {
                    accessLogger.appendResponseDetailMessage(responseToUse);
                }
                accessLogger.appendResponseLast();
            }
            responseToUse.copyBodyToResponse();
            accessLogger.printLog();
        }
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ContentCachingResponseWrapper(org.springframework.web.util.ContentCachingResponseWrapper) ContentCachingRequestWrapper(org.springframework.web.util.ContentCachingRequestWrapper) StopWatch(org.springframework.util.StopWatch)

Example 9 with ContentCachingRequestWrapper

use of org.springframework.web.util.ContentCachingRequestWrapper in project FP-PSP-SERVER by FundacionParaguaya.

the class LogRequestFilter method getBody.

private void getBody(ContentCachingRequestWrapper request, Map<String, Object> trace) {
    // 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]";
            }
            trace.put("body", payload);
        }
    }
}
Also used : UnsupportedEncodingException(java.io.UnsupportedEncodingException) ContentCachingRequestWrapper(org.springframework.web.util.ContentCachingRequestWrapper)

Example 10 with ContentCachingRequestWrapper

use of org.springframework.web.util.ContentCachingRequestWrapper in project syndesis by syndesisio.

the class VerifierExceptionMapper method toResponse.

@Override
public Response toResponse(final Throwable exception) {
    // the proxy @Context would provide would not let us access the wrapped
    // request
    final HttpServletRequest request = ResteasyProviderFactory.getContextData(HttpServletRequest.class);
    LOG.error("Exception while handling request: {} {}", request.getMethod(), request.getRequestURI(), exception);
    if (LOG.isDebugEnabled()) {
        final ContentCachingRequestWrapper requestCache = WebUtils.getNativeRequest(request, ContentCachingRequestWrapper.class);
        final Enumeration<String> headers = request.getHeaderNames();
        final StringJoiner headersJoined = new StringJoiner("\n");
        while (headers.hasMoreElements()) {
            final String header = headers.nextElement();
            headersJoined.add(header + ": " + String.join("|", Collections.list(request.getHeaders(header))));
        }
        LOG.debug("Headers: \n{}", headersJoined.toString());
        LOG.debug("Request content: \n{}", new String(requestCache.getContentAsByteArray(), StandardCharsets.UTF_8));
    }
    final Error error = new Error(500, rootCauseMessage(exception), exception.getMessage());
    return Response.serverError().entity(error).build();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ContentCachingRequestWrapper(org.springframework.web.util.ContentCachingRequestWrapper) StringJoiner(java.util.StringJoiner)

Aggregations

ContentCachingRequestWrapper (org.springframework.web.util.ContentCachingRequestWrapper)11 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 ServletServerHttpRequest (org.springframework.http.server.ServletServerHttpRequest)2 ContentCachingResponseWrapper (org.springframework.web.util.ContentCachingResponseWrapper)2 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 FilterChain (jakarta.servlet.FilterChain)1 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)1 InputStreamReader (java.io.InputStreamReader)1 StandardCharsets (java.nio.charset.StandardCharsets)1 ArrayList (java.util.ArrayList)1 StringJoiner (java.util.StringJoiner)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 Test (org.junit.jupiter.api.Test)1 MessageHeaders (org.springframework.messaging.MessageHeaders)1 FileCopyUtils (org.springframework.util.FileCopyUtils)1 StopWatch (org.springframework.util.StopWatch)1 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)1 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)1