Search in sources :

Example 1 with BadRequestException

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"));
    }
}
Also used : InputPart(org.jboss.resteasy.plugins.providers.multipart.InputPart) MultipartFormDataInput(org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataInput) HashMap(java.util.HashMap) InputStream(java.io.InputStream) BadRequestException(uk.gov.justice.services.adapter.rest.exception.BadRequestException) Collections.emptyList(java.util.Collections.emptyList) Collections.singletonList(java.util.Collections.singletonList) List(java.util.List) Arrays.asList(java.util.Arrays.asList) Test(org.junit.Test)

Example 2 with BadRequestException

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'"));
    }
}
Also used : InputPart(org.jboss.resteasy.plugins.providers.multipart.InputPart) BadRequestException(uk.gov.justice.services.adapter.rest.exception.BadRequestException) Test(org.junit.Test)

Example 3 with BadRequestException

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"));
    }
}
Also used : InputPart(org.jboss.resteasy.plugins.providers.multipart.InputPart) BadRequestException(uk.gov.justice.services.adapter.rest.exception.BadRequestException) Test(org.junit.Test)

Example 4 with BadRequestException

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

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));
}
Also used : InputPart(org.jboss.resteasy.plugins.providers.multipart.InputPart) BadRequestException(uk.gov.justice.services.adapter.rest.exception.BadRequestException)

Aggregations

BadRequestException (uk.gov.justice.services.adapter.rest.exception.BadRequestException)10 Test (org.junit.Test)7 InputPart (org.jboss.resteasy.plugins.providers.multipart.InputPart)6 InputStream (java.io.InputStream)2 Arrays.asList (java.util.Arrays.asList)2 Collections.emptyList (java.util.Collections.emptyList)2 Collections.singletonList (java.util.Collections.singletonList)2 List (java.util.List)2 Response (javax.ws.rs.core.Response)2 MultipartFormDataInput (org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataInput)2 JsonSchemaValidationException (uk.gov.justice.services.core.json.JsonSchemaValidationException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 HashMap (java.util.HashMap)1 Matcher (java.util.regex.Matcher)1 ValidationException (org.everit.json.schema.ValidationException)1 MediaType (uk.gov.justice.services.core.mapping.MediaType)1 InvalidMediaTypeException (uk.gov.justice.services.messaging.exception.InvalidMediaTypeException)1