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