Search in sources :

Example 6 with CatalogServiceException

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.");
    }
}
Also used : HttpHeaders(javax.ws.rs.core.HttpHeaders) CatalogServiceException(org.codice.ddf.rest.api.CatalogServiceException) MultipartBody(org.apache.cxf.jaxrs.ext.multipart.MultipartBody) CatalogFramework(ddf.catalog.CatalogFramework) Test(org.junit.Test)

Example 7 with CatalogServiceException

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());
    }
}
Also used : Response(javax.ws.rs.core.Response) CatalogServiceException(org.codice.ddf.rest.api.CatalogServiceException) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) BinaryContent(ddf.catalog.data.BinaryContent) Path(javax.ws.rs.Path) HEAD(javax.ws.rs.HEAD)

Example 8 with CatalogServiceException

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());
    }
}
Also used : Response(javax.ws.rs.core.Response) CatalogServiceException(org.codice.ddf.rest.api.CatalogServiceException) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) BinaryContent(ddf.catalog.data.BinaryContent) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 9 with CatalogServiceException

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());
    }
}
Also used : CatalogServiceException(org.codice.ddf.rest.api.CatalogServiceException) UriBuilder(javax.ws.rs.core.UriBuilder) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes)

Example 10 with CatalogServiceException

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);
        }
    }
}
Also used : MimeTypeParseException(javax.activation.MimeTypeParseException) Metacard(ddf.catalog.data.Metacard) CatalogServiceException(org.codice.ddf.rest.api.CatalogServiceException) MetacardCreationException(ddf.catalog.data.MetacardCreationException) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) IOException(java.io.IOException) BinaryContent(ddf.catalog.data.BinaryContent) MimeType(javax.activation.MimeType)

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