use of org.springframework.web.util.ContentCachingResponseWrapper in project cas by apereo.
the class AbstractDelegatingCasView method renderMergedOutputModel.
@Override
@SneakyThrows
protected void renderMergedOutputModel(final Map<String, Object> model, final HttpServletRequest request, final HttpServletResponse response) {
val requestWrapper = new ContentCachingRequestWrapper(request);
val responseWrapper = new ContentCachingResponseWrapper(response);
LOGGER.debug("Preparing the output model [{}] to render view [{}]", model.keySet(), getClass().getSimpleName());
prepareMergedOutputModel(model, request, response);
LOGGER.trace("Prepared output model with objects [{}]. Now rendering view...", model.keySet().toArray());
try {
getView().render(model, requestWrapper, responseWrapper);
} finally {
val responseArray = responseWrapper.getContentAsByteArray();
val output = new String(responseArray, responseWrapper.getCharacterEncoding());
val message = String.format("Final CAS response for [%s] is:%n%s%n", getView().toString(), output);
LOGGER.debug(message);
responseWrapper.copyBodyToResponse();
}
}
use of org.springframework.web.util.ContentCachingResponseWrapper in project metron by apache.
the class ResponseLoggingFilter method doFilter.
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
if (LOG.isDebugEnabled()) {
response = new ContentCachingResponseWrapper((HttpServletResponse) response);
}
try {
filterChain.doFilter(request, response);
} finally {
if (LOG.isDebugEnabled()) {
int status = -1;
String responseBody = "";
ContentCachingResponseWrapper wrapper = WebUtils.getNativeResponse(response, ContentCachingResponseWrapper.class);
if (wrapper != null) {
status = wrapper.getStatus();
responseBody = new String(wrapper.getContentAsByteArray(), StandardCharsets.UTF_8);
// try to pretty print
try {
responseBody = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(objectMapper.readValue(responseBody, Object.class));
} catch (Exception e) {
// if pretty printing fails or response is not json, just move on
}
// need to copy the response back since we're reading it here
wrapper.copyBodyToResponse();
}
LOG.debug(String.format("response: status=%d;payload=%s", status, responseBody));
}
}
}
use of org.springframework.web.util.ContentCachingResponseWrapper 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.ContentCachingResponseWrapper in project easy-tests by malinink.
the class SwaggerValidationFilter method doFilterInternal.
@Override
protected void doFilterInternal(final HttpServletRequest servletRequest, final HttpServletResponse servletResponse, final FilterChain filterChain) throws ServletException, IOException {
final HttpServletRequest requestToUse = wrapValidatableServletRequest(servletRequest);
final ContentCachingResponseWrapper responseToUse = this.wrapValidatableServletResponse(servletResponse);
filterChain.doFilter(requestToUse, responseToUse);
responseToUse.copyBodyToResponse();
}
use of org.springframework.web.util.ContentCachingResponseWrapper in project easy-tests by malinink.
the class SwaggerValidationInterceptor method postHandle.
@Override
public void postHandle(HttpServletRequest servletRequest, HttpServletResponse servletResponse, Object handler, @Nullable ModelAndView modelAndView) throws Exception {
// only wrapped servlet requests can be validated - see: SwaggerValidationFilter
if (!(servletRequest instanceof ResettableRequestServletWrapper) || !(servletResponse instanceof ContentCachingResponseWrapper)) {
return;
}
// validate the request
final ResettableRequestServletWrapper resettableRequest = (ResettableRequestServletWrapper) servletRequest;
final ContentCachingResponseWrapper cachingResponse = (ContentCachingResponseWrapper) servletResponse;
final Request request = swaggerRequestValidationService.buildRequest(resettableRequest);
final Response response = swaggerRequestValidationService.buildResponse(cachingResponse);
final String requestLoggingKey = servletRequest.getMethod() + "#" + servletRequest.getRequestURI() + "#" + response.getStatus();
LOG.info("Swagger response validation: {}", requestLoggingKey);
final ValidationReport validationReport = swaggerRequestValidationService.validateResponse(request.getPath(), request.getMethod(), response);
if (!validationReport.hasErrors()) {
LOG.debug("Swagger validation: {} - The response is valid.", requestLoggingKey);
} else if (!swaggerRequestValidationService.isDefinedSwaggerRequest(validationReport)) {
LOG.info("Swagger validation: {} - The request/response is not defined in the Swagger schema. Ignoring it.", requestLoggingKey);
} else {
final InvalidResponseException invalidReponseException = new InvalidResponseException(validationReport);
LOG.warn("Swagger validation: {} - The REST response is invalid: {}", requestLoggingKey, invalidReponseException.getMessage());
throw invalidReponseException;
}
}
Aggregations