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