Search in sources :

Example 6 with BadRequestException

use of uk.gov.justice.services.adapter.rest.exception.BadRequestException in project microservice_framework by CJSCommonPlatform.

the class InputPartFileNameExtractor method extractFileName.

public String extractFileName(final InputPart filePart) {
    final String headerValue = filePart.getHeaders().getFirst(CONTENT_DISPOSITION_HEADER_NAME);
    if (headerValue == null) {
        throw new BadRequestException("No header found named '" + CONTENT_DISPOSITION_HEADER_NAME + "'");
    }
    final Matcher matcher = FIND_FILENAME_PATTERN.matcher(headerValue);
    if (matcher.find()) {
        return matcher.group(FILENAME_MATCHER_GROUP);
    }
    throw new BadRequestException("Failed to find 'filename' in '" + CONTENT_DISPOSITION_HEADER_NAME + "' header");
}
Also used : Matcher(java.util.regex.Matcher) BadRequestException(uk.gov.justice.services.adapter.rest.exception.BadRequestException)

Example 7 with BadRequestException

use of uk.gov.justice.services.adapter.rest.exception.BadRequestException in project microservice_framework by CJSCommonPlatform.

the class DefaultFileInputDetailsFactoryTest method shouldThrowBadRequestExceptionIfTheListOfFilePartsForAFieldNameIsEmpty.

@Test
public void shouldThrowBadRequestExceptionIfTheListOfFilePartsForAFieldNameIsEmpty() 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 = ImmutableMap.of(fieldName, emptyList());
    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));
    } catch (final BadRequestException expected) {
        assertThat(expected.getMessage(), is("The list of input parts named 'myFieldName' is empty"));
    }
}
Also used : InputPart(org.jboss.resteasy.plugins.providers.multipart.InputPart) MultipartFormDataInput(org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataInput) 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 8 with BadRequestException

use of uk.gov.justice.services.adapter.rest.exception.BadRequestException in project microservice_framework by CJSCommonPlatform.

the class InputPartFileNameExtractorTest method shouldThrowABadRequestExceptionIfNoContentDispositionHeaderFound.

@Test
public void shouldThrowABadRequestExceptionIfNoContentDispositionHeaderFound() throws Exception {
    final String headerName = "Some-Other-Header-Name";
    final String headerValue = "form-data; name=\"file\"; filename=\"your_file.zip\"";
    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("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 9 with BadRequestException

use of uk.gov.justice.services.adapter.rest.exception.BadRequestException in project microservice_framework by CJSCommonPlatform.

the class BadRequestExceptionMapperTest method shouldReturn400ResponseForBadRequestException.

@Test
public void shouldReturn400ResponseForBadRequestException() throws Exception {
    final Response response = exceptionMapper.toResponse(new BadRequestException(TEST_ERROR_MESSAGE));
    assertThat(response.getStatus(), is(BAD_REQUEST.getStatusCode()));
    assertThat(response.getEntity(), notNullValue());
    assertThat(response.getEntity().toString(), hasJsonPath("$.error", equalTo(TEST_ERROR_MESSAGE)));
}
Also used : Response(javax.ws.rs.core.Response) BadRequestException(uk.gov.justice.services.adapter.rest.exception.BadRequestException) Test(org.junit.Test)

Example 10 with BadRequestException

use of uk.gov.justice.services.adapter.rest.exception.BadRequestException in project microservice_framework by CJSCommonPlatform.

the class BadRequestExceptionMapperTest method shouldAddJsonValidationErrorsToResponse.

@Test
public void shouldAddJsonValidationErrorsToResponse() {
    final ValidationException validationException = new ValidationException(schema, "Test Json");
    final JsonSchemaValidationException jsonSchemaValidationException = new JsonSchemaValidationException(validationException.getMessage(), validationException);
    final BadRequestException badRequestException = new BadRequestException(TEST_ERROR_MESSAGE, jsonSchemaValidationException);
    final Response response = exceptionMapper.toResponse(badRequestException);
    final String body = response.getEntity().toString();
    assertThat(body, hasJsonPath("$.validationErrors.message", equalTo("#: Test Json")));
}
Also used : Response(javax.ws.rs.core.Response) ValidationException(org.everit.json.schema.ValidationException) JsonSchemaValidationException(uk.gov.justice.services.core.json.JsonSchemaValidationException) JsonSchemaValidationException(uk.gov.justice.services.core.json.JsonSchemaValidationException) BadRequestException(uk.gov.justice.services.adapter.rest.exception.BadRequestException) Test(org.junit.Test)

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