use of org.codice.ddf.rest.api.CatalogServiceException in project ddf by codice.
the class CatalogServiceImplTest method testAddDocumentNullMessage.
@Test
public void testAddDocumentNullMessage() {
CatalogFramework framework = mock(CatalogFramework.class);
CatalogServiceImpl catalogService = new CatalogServiceImpl(framework, attachmentParser, attributeRegistry);
HttpHeaders headers = mock(HttpHeaders.class);
try {
catalogService.addDocument(headers.getRequestHeader(HttpHeaders.CONTENT_TYPE), mock(MultipartBody.class), null, null);
} catch (CatalogServiceException e) {
assertEquals(e.getMessage(), "No content found, cannot do CREATE.");
}
}
use of org.codice.ddf.rest.api.CatalogServiceException in project ddf by codice.
the class RESTEndpoint method getHeaders.
/**
* REST Head. Returns headers only. Primarily used to let the client know that range requests
* (though limited) are accepted.
*
* @param sourceid
* @param id
* @param uriInfo
* @param httpRequest
* @return
*/
@HEAD
@Path("/sources/{sourceid}/{id}")
public Response getHeaders(@PathParam("sourceid") String sourceid, @PathParam("id") String id, @Context UriInfo uriInfo, @Context HttpServletRequest httpRequest) {
try {
Response.ResponseBuilder responseBuilder;
final BinaryContent content = catalogService.getHeaders(sourceid, id, uriInfo.getAbsolutePath(), uriInfo.getQueryParameters());
if (content == null) {
return Response.status(Status.NOT_FOUND).entity(String.format(PRE_FORMAT, UNABLE_TO_RETRIEVE_REQUESTED_METACARD)).type(MediaType.TEXT_HTML).build();
}
responseBuilder = Response.noContent();
// 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 (InternalServerErrorException e) {
LOGGER.info(e.getMessage());
return createErrorResponse(e.getMessage());
}
}
use of org.codice.ddf.rest.api.CatalogServiceException in project ddf by codice.
the class RESTEndpoint method createMetacard.
@POST
@Path("/metacard")
public Response createMetacard(MultipartBody multipartBody, @Context UriInfo requestUriInfo, @QueryParam("transform") String transformerParam) {
try {
final BinaryContent content = catalogService.createMetacard(multipartBody, transformerParam);
Response.ResponseBuilder responseBuilder = Response.ok(content.getInputStream(), content.getMimeTypeValue());
return responseBuilder.build();
} catch (CatalogServiceException e) {
return createBadRequestResponse(e.getMessage());
}
}
use of org.codice.ddf.rest.api.CatalogServiceException in project ddf by codice.
the class RESTEndpoint method addDocument.
/**
* REST Post. Creates a new metadata entry in the catalog.
*
* @param message
* @return
*/
@Override
@POST
@Consumes("multipart/*")
public Response addDocument(@Context HttpHeaders headers, @Context UriInfo requestUriInfo, @Context HttpServletRequest httpRequest, MultipartBody multipartBody, @QueryParam("transform") String transformerParam, InputStream message) {
try {
List<String> contentTypeList = headers.getRequestHeader(HttpHeaders.CONTENT_TYPE);
String id = catalogService.addDocument(contentTypeList, multipartBody, transformerParam, message);
UriBuilder uriBuilder = requestUriInfo.getAbsolutePathBuilder().path("/" + id);
ResponseBuilder responseBuilder = Response.created(uriBuilder.build());
responseBuilder.header(Metacard.ID, id);
return responseBuilder.build();
} catch (CatalogServiceException e) {
return createBadRequestResponse(e.getMessage());
}
}
use of org.codice.ddf.rest.api.CatalogServiceException in project ddf by codice.
the class AbstractCatalogService method createMetacard.
private BinaryContent createMetacard(InputStream stream, String contentType, String transformerParam) throws CatalogServiceException {
String transformer = DEFAULT_METACARD_TRANSFORMER;
if (transformerParam != null) {
transformer = transformerParam;
}
MimeType mimeType = null;
if (contentType != null) {
try {
mimeType = new MimeType(contentType);
} catch (MimeTypeParseException e) {
LOGGER.debug("Unable to create MimeType from raw data {}", contentType);
}
} else {
LOGGER.debug("No content type specified in request");
}
try {
Metacard metacard = generateMetacard(mimeType, null, stream, null);
String metacardId = metacard.getId();
LOGGER.debug("Metacard {} created", metacardId);
LOGGER.debug("Transforming metacard {} to {} to be able to return it to client", metacardId, LogSanitizer.sanitize(transformer));
final BinaryContent content = catalogFramework.transform(metacard, transformer, null);
LOGGER.debug("Metacard to {} transform complete for {}, preparing response.", LogSanitizer.sanitize(transformer), metacardId);
LOGGER.trace("EXITING: createMetacard");
return content;
} catch (MetacardCreationException | CatalogTransformerException e) {
throw new CatalogServiceException("Unable to create metacard");
} finally {
try {
if (stream != null) {
stream.close();
}
} catch (IOException e) {
LOGGER.debug("Unexpected error closing stream", e);
}
}
}
Aggregations