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();
}
}
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));
}
}
}
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();
}
}
}
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);
}
}
}
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();
}
Aggregations