Search in sources :

Example 1 with JsonSchemaValidationException

use of uk.gov.justice.services.core.json.JsonSchemaValidationException in project microservice_framework by CJSCommonPlatform.

the class JsonSchemaValidationInterceptor method validate.

private void validate(final TextMessage message) throws JMSException {
    try {
        final String name = message.getStringProperty(JMS_HEADER_CPPNAME);
        final MediaType mediaType = nameToMediaTypeConverter.convert(name);
        jsonSchemaValidator.validate(message.getText(), name, of(mediaType));
    } catch (final JsonSchemaValidationException jsonSchemaValidationException) {
        logger.debug(format("JSON schema validation has failed for %s due to %s", jmsMessageLoggerHelper.toJmsTraceString(message), jsonValidationLoggerHelper.toValidationTrace(jsonSchemaValidationException)));
        throw jsonSchemaValidationException;
    }
}
Also used : JsonSchemaValidationException(uk.gov.justice.services.core.json.JsonSchemaValidationException) MediaType(uk.gov.justice.services.core.mapping.MediaType)

Example 2 with JsonSchemaValidationException

use of uk.gov.justice.services.core.json.JsonSchemaValidationException in project microservice_framework by CJSCommonPlatform.

the class JsonSchemaValidationInterceptorTest method shouldThrowExceptionIfValidatorFails.

@Test
public void shouldThrowExceptionIfValidatorFails() throws Exception {
    final TextMessage message = mock(TextMessage.class);
    final String payload = "test payload";
    final String name = "test-name";
    final MediaType mediaType = mock(MediaType.class);
    final JsonSchemaValidationException jsonSchemaValidationException = mock(JsonSchemaValidationException.class);
    when(message.getText()).thenReturn(payload);
    when(message.getStringProperty(JMS_HEADER_CPPNAME)).thenReturn(name);
    when(invocationContext.getParameters()).thenReturn(new Object[] { message });
    when(nameToMediaTypeConverter.convert(name)).thenReturn(mediaType);
    doThrow(jsonSchemaValidationException).when(jsonSchemaValidator).validate(payload, name, of(mediaType));
    when(jmsMessageLoggerHelper.toJmsTraceString(message)).thenReturn("message");
    when(jsonValidationLoggerHelper.toValidationTrace(jsonSchemaValidationException)).thenReturn("jsonSchemaValidationException");
    try {
        jsonSchemaValidationInterceptor.validate(invocationContext);
        fail();
    } catch (final JsonSchemaValidationException e) {
        verify(logger).debug("JSON schema validation has failed for message due to jsonSchemaValidationException");
    }
}
Also used : JsonSchemaValidationException(uk.gov.justice.services.core.json.JsonSchemaValidationException) MediaType(uk.gov.justice.services.core.mapping.MediaType) TextMessage(javax.jms.TextMessage) Test(org.junit.Test)

Example 3 with JsonSchemaValidationException

use of uk.gov.justice.services.core.json.JsonSchemaValidationException in project microservice_framework by CJSCommonPlatform.

the class JsonSchemaValidationInterceptorTest method shouldThrowBadRequestExceptionIfValidatorFailsWithValidationException.

@Test(expected = BadRequestException.class)
@SuppressWarnings("unchecked")
public void shouldThrowBadRequestExceptionIfValidatorFailsWithValidationException() throws Exception {
    final MultivaluedMap<String, String> headers = new MultivaluedHashMap();
    final String actionName = "example.action-name";
    when(nameToMediaTypeConverter.convert(CONVERTED_MEDIA_TYPE)).thenReturn(actionName);
    doThrow(new JsonSchemaValidationException("")).when(jsonSchemaValidator).validate(PAYLOAD, actionName, of(CONVERTED_MEDIA_TYPE));
    when(context.getHeaders()).thenReturn(headers);
    jsonSchemaValidationInterceptor.aroundReadFrom(context);
}
Also used : MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) JsonSchemaValidationException(uk.gov.justice.services.core.json.JsonSchemaValidationException) Test(org.junit.Test)

Example 4 with JsonSchemaValidationException

use of uk.gov.justice.services.core.json.JsonSchemaValidationException in project microservice_framework by CJSCommonPlatform.

the class JsonSchemaValidationInterceptor method aroundReadFrom.

@Override
public Object aroundReadFrom(final ReaderInterceptorContext context) throws IOException, WebApplicationException {
    final MediaType mediaType = new MediaType(context.getMediaType().toString());
    if (mediaType.getSubtype().endsWith(JSON_MEDIA_TYPE_SUFFIX)) {
        final String charset = charsetFrom(context.getMediaType());
        final String payload = IOUtils.toString(context.getInputStream(), charset);
        try {
            restJsonSchemaValidator.validate(payload, nameToMediaTypeConverter.convert(mediaType), of(mediaType));
        } catch (final JsonSchemaValidationException jsonSchemaValidationException) {
            final String message = format("JSON schema validation has failed on %s due to %s ", httpTraceLoggerHelper.toHttpHeaderTrace(context.getHeaders()), jsonValidationLoggerHelper.toValidationTrace(jsonSchemaValidationException));
            logger.debug(message);
            throw new BadRequestException(message, jsonSchemaValidationException);
        } catch (InvalidMediaTypeException ex) {
            throw new BadRequestException(ex.getMessage(), ex);
        }
        context.setInputStream(new ByteArrayInputStream(payload.getBytes(charset)));
    }
    return context.proceed();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) JsonSchemaValidationException(uk.gov.justice.services.core.json.JsonSchemaValidationException) MediaType(uk.gov.justice.services.core.mapping.MediaType) BadRequestException(uk.gov.justice.services.adapter.rest.exception.BadRequestException) InvalidMediaTypeException(uk.gov.justice.services.messaging.exception.InvalidMediaTypeException)

Example 5 with JsonSchemaValidationException

use of uk.gov.justice.services.core.json.JsonSchemaValidationException in project microservice_framework by CJSCommonPlatform.

the class BadRequestExceptionMapper method toResponse.

@Override
public Response toResponse(final BadRequestException exception) {
    logger.debug("Bad Request", exception);
    final JsonObjectBuilder builder = createObjectBuilder().add("error", exception.getMessage());
    if (exception.getCause() instanceof JsonSchemaValidationException) {
        builder.add("validationErrors", jsonValidationLoggerHelper.toJsonObject((JsonSchemaValidationException) exception.getCause()));
    }
    return status(BAD_REQUEST).entity(builder.build().toString()).build();
}
Also used : JsonSchemaValidationException(uk.gov.justice.services.core.json.JsonSchemaValidationException) JsonObjectBuilder(javax.json.JsonObjectBuilder)

Aggregations

JsonSchemaValidationException (uk.gov.justice.services.core.json.JsonSchemaValidationException)6 Test (org.junit.Test)3 MediaType (uk.gov.justice.services.core.mapping.MediaType)3 BadRequestException (uk.gov.justice.services.adapter.rest.exception.BadRequestException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 TextMessage (javax.jms.TextMessage)1 JsonObjectBuilder (javax.json.JsonObjectBuilder)1 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)1 Response (javax.ws.rs.core.Response)1 ValidationException (org.everit.json.schema.ValidationException)1 InvalidMediaTypeException (uk.gov.justice.services.messaging.exception.InvalidMediaTypeException)1