Search in sources :

Example 26 with MimeType

use of javax.activation.MimeType in project ddf by codice.

the class CswEndpoint method queryProductById.

private CswRecordCollection queryProductById(String id, String rangeValue) throws CswException, UnsupportedQueryException {
    final ResourceRequestById resourceRequest = new ResourceRequestById(id);
    long bytesToSkip = getRange(rangeValue);
    if (bytesToSkip > 0) {
        LOGGER.debug("Bytes to skip: {}", String.valueOf(bytesToSkip));
        resourceRequest.getProperties().put(CswConstants.BYTES_TO_SKIP, bytesToSkip);
    }
    ResourceResponse resourceResponse;
    try {
        resourceResponse = framework.getLocalResource(resourceRequest);
    } catch (IOException | ResourceNotFoundException | ResourceNotSupportedException e) {
        throw new CswException(String.format(ERROR_ID_PRODUCT_RETRIEVAL, id), e);
    }
    Resource resource = resourceResponse.getResource();
    MimeType mimeType = resource.getMimeType();
    if (mimeType == null) {
        try {
            mimeType = new MimeType(MediaType.APPLICATION_OCTET_STREAM);
            resource = new ResourceImpl(resource.getInputStream(), mimeType, resource.getName());
        } catch (MimeTypeParseException e) {
            throw new CswException(String.format("Could not create mime type upon null mimeType, for mime %s.", MediaType.APPLICATION_OCTET_STREAM), e);
        }
    }
    CswRecordCollection cswRecordCollection = new CswRecordCollection();
    cswRecordCollection.setResource(resource);
    cswRecordCollection.setOutputSchema(OCTET_STREAM_OUTPUT_SCHEMA);
    LOGGER.debug("{} successfully retrieved product for ID: {}", id);
    return cswRecordCollection;
}
Also used : MimeTypeParseException(javax.activation.MimeTypeParseException) CswException(org.codice.ddf.spatial.ogc.csw.catalog.common.CswException) Resource(ddf.catalog.resource.Resource) IOException(java.io.IOException) MimeType(javax.activation.MimeType) ResourceImpl(ddf.catalog.resource.impl.ResourceImpl) ResourceResponse(ddf.catalog.operation.ResourceResponse) ResourceNotSupportedException(ddf.catalog.resource.ResourceNotSupportedException) ResourceRequestById(ddf.catalog.operation.impl.ResourceRequestById) CswRecordCollection(org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection) ResourceNotFoundException(ddf.catalog.resource.ResourceNotFoundException)

Example 27 with MimeType

use of javax.activation.MimeType in project ddf by codice.

the class Configuration method setMimeType.

public void setMimeType(String mimeType) {
    MimeType mime = null;
    try {
        mime = new MimeType(mimeType);
    } catch (MimeTypeParseException e) {
        LOGGER.info("Failed to create mimetype: {}.", mimeType);
    }
    this.mimeType = mime;
}
Also used : MimeTypeParseException(javax.activation.MimeTypeParseException) MimeType(javax.activation.MimeType)

Example 28 with MimeType

use of javax.activation.MimeType in project ddf by codice.

the class RESTEndpoint method addDocument.

/**
     * REST Post. Creates a new metadata entry in the catalog.
     *
     * @param message
     * @return
     */
@POST
@Consumes("multipart/*")
public Response addDocument(@Context HttpHeaders headers, @Context UriInfo requestUriInfo, @Context HttpServletRequest httpRequest, MultipartBody multipartBody, @QueryParam("transform") String transformerParam, InputStream message) {
    LOGGER.debug("POST");
    Response response;
    MimeType mimeType = getMimeType(headers);
    try {
        if (message != null) {
            CreateInfo createInfo = null;
            if (multipartBody != null) {
                List<Attachment> contentParts = multipartBody.getAllAttachments();
                if (contentParts != null && contentParts.size() > 0) {
                    createInfo = parseAttachments(contentParts, transformerParam);
                } else {
                    LOGGER.debug("No file contents attachment found");
                }
            }
            CreateResponse createResponse;
            if (createInfo == null) {
                CreateRequest createRequest = new CreateRequestImpl(generateMetacard(mimeType, null, message, transformerParam));
                createResponse = catalogFramework.create(createRequest);
            } else {
                CreateStorageRequest streamCreateRequest = new CreateStorageRequestImpl(Collections.singletonList(new IncomingContentItem(uuidGenerator, createInfo.getStream(), createInfo.getContentType(), createInfo.getFilename(), createInfo.getMetacard())), null);
                createResponse = catalogFramework.create(streamCreateRequest);
            }
            String id = createResponse.getCreatedMetacards().get(0).getId();
            LOGGER.debug("Create Response id [{}]", id);
            UriBuilder uriBuilder = requestUriInfo.getAbsolutePathBuilder().path("/" + id);
            ResponseBuilder responseBuilder = Response.created(uriBuilder.build());
            responseBuilder.header(Metacard.ID, id);
            response = responseBuilder.build();
            LOGGER.debug("Entry successfully saved, id: {}", id);
            if (INGEST_LOGGER.isInfoEnabled()) {
                INGEST_LOGGER.info("Entry successfully saved, id: {}", id);
            }
        } else {
            String errorMessage = "No content found, cannot do CREATE.";
            LOGGER.info(errorMessage);
            throw new ServerErrorException(errorMessage, Status.BAD_REQUEST);
        }
    } catch (SourceUnavailableException e) {
        String exceptionMessage = "Cannot create catalog entry because source is unavailable: ";
        LOGGER.info(exceptionMessage, e);
        // Catalog framework logs these exceptions to the ingest logger so we don't have to.
        throw new ServerErrorException(exceptionMessage, Status.INTERNAL_SERVER_ERROR);
    } catch (InternalIngestException e) {
        String exceptionMessage = "Error while storing entry in catalog: ";
        LOGGER.info(exceptionMessage, e);
        // Catalog framework logs these exceptions to the ingest logger so we don't have to.
        throw new ServerErrorException(exceptionMessage, Status.INTERNAL_SERVER_ERROR);
    } catch (MetacardCreationException | IngestException e) {
        String exceptionMessage = "Error while storing entry in catalog: ";
        LOGGER.info(exceptionMessage, e);
        // Catalog framework logs these exceptions to the ingest logger so we don't have to.
        throw new ServerErrorException(exceptionMessage, Status.BAD_REQUEST);
    } finally {
        IOUtils.closeQuietly(message);
    }
    return response;
}
Also used : SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) MetacardCreationException(ddf.catalog.data.MetacardCreationException) InternalIngestException(ddf.catalog.source.InternalIngestException) CreateResponse(ddf.catalog.operation.CreateResponse) CreateRequest(ddf.catalog.operation.CreateRequest) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment) MimeType(javax.activation.MimeType) SourceInfoResponse(ddf.catalog.operation.SourceInfoResponse) QueryResponse(ddf.catalog.operation.QueryResponse) Response(javax.ws.rs.core.Response) CreateResponse(ddf.catalog.operation.CreateResponse) CreateStorageRequestImpl(ddf.catalog.content.operation.impl.CreateStorageRequestImpl) CreateRequestImpl(ddf.catalog.operation.impl.CreateRequestImpl) IngestException(ddf.catalog.source.IngestException) InternalIngestException(ddf.catalog.source.InternalIngestException) UriBuilder(javax.ws.rs.core.UriBuilder) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) CreateStorageRequest(ddf.catalog.content.operation.CreateStorageRequest) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes)

Example 29 with MimeType

use of javax.activation.MimeType in project ddf by codice.

the class RESTEndpoint method getMimeType.

private MimeType getMimeType(HttpHeaders headers) {
    List<String> contentTypeList = headers.getRequestHeader(HttpHeaders.CONTENT_TYPE);
    String singleMimeType = null;
    if (contentTypeList != null && !contentTypeList.isEmpty()) {
        singleMimeType = contentTypeList.get(0);
        LOGGER.debug("Encountered [{}] {}", singleMimeType, HttpHeaders.CONTENT_TYPE);
    }
    MimeType mimeType = null;
    // Sending a null argument to MimeType causes NPE
    if (singleMimeType != null) {
        try {
            mimeType = new MimeType(singleMimeType);
        } catch (MimeTypeParseException e) {
            LOGGER.debug("Could not parse mime type from headers.", e);
        }
    }
    return mimeType;
}
Also used : MimeTypeParseException(javax.activation.MimeTypeParseException) MimeType(javax.activation.MimeType)

Example 30 with MimeType

use of javax.activation.MimeType in project ddf by codice.

the class RESTEndpoint method parseMetadata.

private Metacard parseMetadata(String transformerParam, Metacard metacard, Attachment attachment, InputStream inputStream) {
    String transformer = DEFAULT_METACARD_TRANSFORMER;
    if (transformerParam != null) {
        transformer = transformerParam;
    }
    try {
        MimeType mimeType = new MimeType(attachment.getContentType().toString());
        metacard = generateMetacard(mimeType, "assigned-when-ingested", inputStream, transformer);
    } catch (MimeTypeParseException | MetacardCreationException e) {
        LOGGER.debug("Unable to parse metadata {}", attachment.getContentType().toString());
    } finally {
        IOUtils.closeQuietly(inputStream);
    }
    return metacard;
}
Also used : MimeTypeParseException(javax.activation.MimeTypeParseException) MetacardCreationException(ddf.catalog.data.MetacardCreationException) MimeType(javax.activation.MimeType)

Aggregations

MimeType (javax.activation.MimeType)67 Test (org.junit.Test)38 Metacard (ddf.catalog.data.Metacard)21 URI (java.net.URI)14 HashMap (java.util.HashMap)14 MimeTypeParseException (javax.activation.MimeTypeParseException)14 MimeTypeToTransformerMapper (ddf.mime.MimeTypeToTransformerMapper)13 BundleContext (org.osgi.framework.BundleContext)13 ServiceReference (org.osgi.framework.ServiceReference)13 CatalogFramework (ddf.catalog.CatalogFramework)10 ResourceResponse (ddf.catalog.operation.ResourceResponse)10 Serializable (java.io.Serializable)9 Resource (ddf.catalog.resource.Resource)8 IOException (java.io.IOException)8 File (java.io.File)7 Matchers.anyString (org.mockito.Matchers.anyString)7 MetacardCreationException (ddf.catalog.data.MetacardCreationException)6 CatalogTransformerException (ddf.catalog.transform.CatalogTransformerException)6 Response (javax.ws.rs.core.Response)6 BinaryContentImpl (ddf.catalog.data.impl.BinaryContentImpl)5