use of uk.gov.justice.services.adapter.rest.exception.BadRequestException in project microservice_framework by CJSCommonPlatform.
the class DefaultFileInputDetailsFactoryTest method shouldThrowBadRequestExceptionIfNoInputPartFoundWithTheNameSpecifiedInTheRaml.
@Test
public void shouldThrowBadRequestExceptionIfNoInputPartFoundWithTheNameSpecifiedInTheRaml() throws Exception {
final String fileName = "the-file-name.jpeg";
final String fieldName = "myFieldName";
final MultipartFormDataInput multipartFormDataInput = mock(MultipartFormDataInput.class);
final InputPart inputPart = mock(InputPart.class);
final InputStream inputStream = mock(InputStream.class);
final Map<String, List<InputPart>> formDataMap = new HashMap<>();
when(multipartFormDataInput.getFormDataMap()).thenReturn(formDataMap);
when(inputPartFileNameExtractor.extractFileName(inputPart)).thenReturn(fileName);
when(inputPart.getBody(InputStream.class, null)).thenReturn(inputStream);
try {
fileInputDetailsFactory.createFileInputDetailsFrom(multipartFormDataInput, singletonList(fieldName));
fail();
} catch (final BadRequestException expected) {
assertThat(expected.getMessage(), is("Failed to find input part named 'myFieldName' as specified in the raml"));
}
}
use of uk.gov.justice.services.adapter.rest.exception.BadRequestException in project microservice_framework by CJSCommonPlatform.
the class InputPartFileNameExtractorTest method shouldThrowABadRequestExceptionIfNoHeadersFound.
@Test
public void shouldThrowABadRequestExceptionIfNoHeadersFound() throws Exception {
final InputPart inputPart = mock(InputPart.class);
when(inputPart.getHeaders()).thenReturn(new MultivaluedHashMap<>());
try {
inputPartFileNameExtractor.extractFileName(inputPart);
} catch (final BadRequestException expected) {
assertThat(expected.getMessage(), is("No header found named 'Content-Disposition'"));
}
}
use of uk.gov.justice.services.adapter.rest.exception.BadRequestException in project microservice_framework by CJSCommonPlatform.
the class InputPartFileNameExtractorTest method shouldThrowABadRequestExceptionIfNoFilenameFoundInContentDispositionHeader.
@Test
public void shouldThrowABadRequestExceptionIfNoFilenameFoundInContentDispositionHeader() throws Exception {
final String headerName = "Content-Disposition";
final String headerValue = "form-data; name=\"file\"";
final Map<String, String> headers = of(headerName, headerValue);
final InputPart inputPart = mock(InputPart.class);
when(inputPart.getHeaders()).thenReturn(new MultivaluedHashMap<>(headers));
try {
inputPartFileNameExtractor.extractFileName(inputPart);
} catch (final BadRequestException expected) {
assertThat(expected.getMessage(), is("Failed to find 'filename' in 'Content-Disposition' header"));
}
}
use of uk.gov.justice.services.adapter.rest.exception.BadRequestException 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.adapter.rest.exception.BadRequestException in project microservice_framework by CJSCommonPlatform.
the class DefaultFileInputDetailsFactory method fileInputDetailsFrom.
private FileInputDetails fileInputDetailsFrom(final Map<String, List<InputPart>> formDataMap, final String fieldName) {
if (!formDataMap.containsKey(fieldName)) {
throw new BadRequestException(format("Failed to find input part named '%s' as specified in the raml", fieldName));
}
final List<InputPart> inputParts = formDataMap.get(fieldName);
if (inputParts.isEmpty()) {
throw new BadRequestException(format("The list of input parts named '%s' is empty", fieldName));
}
final InputPart inputPart = inputParts.get(0);
final String fileName = inputPartFileNameExtractor.extractFileName(inputPart);
return new DefaultFileInputDetails(fileName, fieldName, inputStreamFrom(inputPart, fileName));
}
Aggregations