Search in sources :

Example 11 with CatalogServiceException

use of org.codice.ddf.rest.api.CatalogServiceException in project ddf by codice.

the class CatalogServiceImplTest method assertExceptionThrown.

@SuppressWarnings({ "unchecked" })
private void assertExceptionThrown(Class<? extends Throwable> klass) throws Exception {
    CatalogFramework framework = mock(CatalogFramework.class);
    when(framework.create(isA(CreateRequest.class))).thenThrow(klass);
    when(framework.create(isA(CreateStorageRequest.class))).thenThrow(klass);
    HttpHeaders headers = createHeaders(Collections.singletonList(MediaType.APPLICATION_JSON));
    CatalogServiceImpl catalogService = new CatalogServiceImpl(framework, attachmentParser, attributeRegistry);
    addMatchingService(catalogService, Collections.singletonList(getSimpleTransformer()));
    try {
        catalogService.addDocument(headers.getRequestHeader(HttpHeaders.CONTENT_TYPE), mock(MultipartBody.class), null, new ByteArrayInputStream("".getBytes()));
    } catch (InternalServerErrorException e) {
        if (klass.getName().equals(SourceUnavailableException.class.getName())) {
            assertThat(e.getResponse().getStatus(), equalTo(INTERNAL_SERVER_ERROR));
        }
    } catch (CatalogServiceException e) {
        if (klass.getName().equals(IngestException.class.getName())) {
            assertEquals(e.getMessage(), "Error while storing entry in catalog: ");
        } else {
            fail();
        }
    }
}
Also used : HttpHeaders(javax.ws.rs.core.HttpHeaders) CatalogServiceException(org.codice.ddf.rest.api.CatalogServiceException) ByteArrayInputStream(java.io.ByteArrayInputStream) CreateRequest(ddf.catalog.operation.CreateRequest) MultipartBody(org.apache.cxf.jaxrs.ext.multipart.MultipartBody) CatalogFramework(ddf.catalog.CatalogFramework) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) CreateStorageRequest(ddf.catalog.content.operation.CreateStorageRequest)

Example 12 with CatalogServiceException

use of org.codice.ddf.rest.api.CatalogServiceException in project ddf by codice.

the class RESTEndpoint method getDocument.

/**
 * REST Get. Retrieves the metadata entry specified by the id from the federated source specified
 * by sourceid. Transformer argument is optional, but is used to specify what format the data
 * should be returned.
 *
 * @param encodedSourceId
 * @param encodedId
 * @param transformerParam
 * @param uriInfo
 * @return
 */
@Override
@GET
@Path("/sources/{sourceid}/{id}")
public Response getDocument(@Encoded @PathParam("sourceid") String encodedSourceId, @Encoded @PathParam("id") String encodedId, @QueryParam("transform") String transformerParam, @Context UriInfo uriInfo, @Context HttpServletRequest httpRequest) {
    try {
        Response.ResponseBuilder responseBuilder;
        String id = URLDecoder.decode(encodedId, CharEncoding.UTF_8);
        final BinaryContent content = catalogService.getDocument(encodedSourceId, encodedId, transformerParam, uriInfo.getAbsolutePath(), uriInfo.getQueryParameters(), httpRequest);
        if (content == null) {
            return Response.status(Status.NOT_FOUND).entity(String.format(PRE_FORMAT, UNABLE_TO_RETRIEVE_REQUESTED_METACARD)).type(MediaType.TEXT_HTML).build();
        }
        LOGGER.debug("Read and transform complete, preparing response.");
        responseBuilder = Response.ok(content.getInputStream(), content.getMimeTypeValue());
        // Add the Accept-ranges header to let the client know that we accept ranges in bytes
        responseBuilder.header(HEADER_ACCEPT_RANGES, BYTES);
        setFileNameOnResponseBuilder(id, content, responseBuilder);
        long size = content.getSize();
        if (size > 0) {
            responseBuilder.header(HEADER_CONTENT_LENGTH, size);
        }
        return responseBuilder.build();
    } catch (CatalogServiceException e) {
        return createBadRequestResponse(e.getMessage());
    } catch (DataUsageLimitExceededException e) {
        return Response.status(Status.REQUEST_ENTITY_TOO_LARGE).entity(String.format(PRE_FORMAT, e.getMessage())).type(MediaType.TEXT_HTML).build();
    } catch (OAuthPluginException e) {
        return Response.status(Status.SEE_OTHER).header(HttpHeaders.LOCATION, e.getUrl()).build();
    } catch (UnsupportedEncodingException e) {
        String exceptionMessage = "Unknown error occurred while processing request: ";
        LOGGER.info(exceptionMessage, e);
        throw new InternalServerErrorException(exceptionMessage);
    } catch (InternalServerErrorException e) {
        LOGGER.info(e.getMessage());
        return createErrorResponse(e.getMessage());
    }
}
Also used : Response(javax.ws.rs.core.Response) CatalogServiceException(org.codice.ddf.rest.api.CatalogServiceException) OAuthPluginException(ddf.catalog.plugin.OAuthPluginException) DataUsageLimitExceededException(ddf.catalog.resource.DataUsageLimitExceededException) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) UnsupportedEncodingException(java.io.UnsupportedEncodingException) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) BinaryContent(ddf.catalog.data.BinaryContent) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Aggregations

CatalogServiceException (org.codice.ddf.rest.api.CatalogServiceException)12 InternalServerErrorException (javax.ws.rs.InternalServerErrorException)8 BinaryContent (ddf.catalog.data.BinaryContent)6 SourceUnavailableException (ddf.catalog.source.SourceUnavailableException)5 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)4 Metacard (ddf.catalog.data.Metacard)3 MetacardCreationException (ddf.catalog.data.MetacardCreationException)3 IngestException (ddf.catalog.source.IngestException)3 InternalIngestException (ddf.catalog.source.InternalIngestException)3 CatalogTransformerException (ddf.catalog.transform.CatalogTransformerException)3 MimeType (javax.activation.MimeType)3 Path (javax.ws.rs.Path)3 Response (javax.ws.rs.core.Response)3 CatalogFramework (ddf.catalog.CatalogFramework)2 CreateStorageRequest (ddf.catalog.content.operation.CreateStorageRequest)2 Result (ddf.catalog.data.Result)2 FederationException (ddf.catalog.federation.FederationException)2 CreateRequest (ddf.catalog.operation.CreateRequest)2 QueryResponse (ddf.catalog.operation.QueryResponse)2 QueryImpl (ddf.catalog.operation.impl.QueryImpl)2