Search in sources :

Example 6 with ContentCachingResponseWrapper

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

Example 7 with ContentCachingResponseWrapper

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));
        }
    }
}
Also used : ContentCachingResponseWrapper(org.springframework.web.util.ContentCachingResponseWrapper) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 8 with ContentCachingResponseWrapper

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

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

Example 10 with ContentCachingResponseWrapper

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;
    }
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(com.atlassian.oai.validator.model.Response) ContentCachingResponseWrapper(org.springframework.web.util.ContentCachingResponseWrapper) ValidationReport(com.atlassian.oai.validator.report.ValidationReport) Request(com.atlassian.oai.validator.model.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest)

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