Search in sources :

Example 1 with InvalidMediaTypeException

use of uk.gov.justice.services.messaging.exception.InvalidMediaTypeException in project microservice_framework by CJSCommonPlatform.

the class Name method fromMediaType.

/**
 * Extracts name from media type. <p>Expected media type format: "application/vnd.[NAME]+json"
 *
 * @param mediaType media type to extract the name from.
 * @return extracted media type name.
 * @throws InvalidMediaTypeException If name not found inside media type.
 */
public static Name fromMediaType(final String mediaType) {
    try {
        final Matcher matcher = MEDIA_TYPE_PATTERN.matcher(mediaType);
        matcher.find();
        return new Name(matcher.group(2));
    } catch (IllegalStateException e) {
        throw new InvalidMediaTypeException(format("Error extracting name from media type %s.", mediaType), e);
    }
}
Also used : Matcher(java.util.regex.Matcher) InvalidMediaTypeException(uk.gov.justice.services.messaging.exception.InvalidMediaTypeException)

Example 2 with InvalidMediaTypeException

use of uk.gov.justice.services.messaging.exception.InvalidMediaTypeException 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 3 with InvalidMediaTypeException

use of uk.gov.justice.services.messaging.exception.InvalidMediaTypeException in project microservice_framework by CJSCommonPlatform.

the class JsonSchemaValidationInterceptorTest method shouldThrowBadRequestExceptionIfValidatorFailsWithInvalidMediaTypeException.

@Test(expected = BadRequestException.class)
@SuppressWarnings("unchecked")
public void shouldThrowBadRequestExceptionIfValidatorFailsWithInvalidMediaTypeException() 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 InvalidMediaTypeException("", mock(Exception.class))).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) InvalidMediaTypeException(uk.gov.justice.services.messaging.exception.InvalidMediaTypeException) Test(org.junit.Test)

Aggregations

InvalidMediaTypeException (uk.gov.justice.services.messaging.exception.InvalidMediaTypeException)3 ByteArrayInputStream (java.io.ByteArrayInputStream)1 Matcher (java.util.regex.Matcher)1 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)1 Test (org.junit.Test)1 BadRequestException (uk.gov.justice.services.adapter.rest.exception.BadRequestException)1 JsonSchemaValidationException (uk.gov.justice.services.core.json.JsonSchemaValidationException)1 MediaType (uk.gov.justice.services.core.mapping.MediaType)1