Search in sources :

Example 1 with Response

use of com.atlassian.oai.validator.model.Response 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

Request (com.atlassian.oai.validator.model.Request)1 Response (com.atlassian.oai.validator.model.Response)1 ValidationReport (com.atlassian.oai.validator.report.ValidationReport)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 ContentCachingResponseWrapper (org.springframework.web.util.ContentCachingResponseWrapper)1