Search in sources :

Example 76 with MimeType

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

the class ResourceMetacardTransformerTest method testNullMetacardId.

@Test
public void testNullMetacardId() throws Exception {
    thrown.expect(CatalogTransformerException.class);
    thrown.expectMessage("Could not transform metacard to a resource because the metacard is not valid.");
    String filePath = ABSOLUTE_PATH + TEST_PATH + TS_FILE_NAME_1;
    URI uri = getUri(filePath);
    Metacard metacard = getMockMetacard(uri);
    when(metacard.getId()).thenReturn(null);
    boolean expectSuccess = false;
    MimeType mimeType = getMimeType(VIDEO_MIME_TYPE);
    CatalogFramework framework = getFramework(getResourceResponse(getResource(mimeType, uri)));
    testGetResource(metacard, filePath, mimeType, framework, expectSuccess);
}
Also used : Metacard(ddf.catalog.data.Metacard) CatalogFramework(ddf.catalog.CatalogFramework) URI(java.net.URI) MimeType(javax.activation.MimeType) Test(org.junit.Test)

Example 77 with MimeType

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

the class ResourceMetacardTransformerTest method testResourceHasNullMimeType.

@Test
public void testResourceHasNullMimeType() throws Exception {
    String filePath = ABSOLUTE_PATH + TEST_PATH + TS_FILE_NAME_1;
    URI uri = getUri(filePath);
    Metacard metacard = getMockMetacard(uri);
    boolean expectSuccess = true;
    MimeType mimeType = getMimeType(DEFAULT_MIME_TYPE);
    CatalogFramework framework = getFramework(getResourceResponse(getResource(null, uri)));
    testGetResource(metacard, filePath, mimeType, framework, expectSuccess);
}
Also used : Metacard(ddf.catalog.data.Metacard) CatalogFramework(ddf.catalog.CatalogFramework) URI(java.net.URI) MimeType(javax.activation.MimeType) Test(org.junit.Test)

Example 78 with MimeType

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

the class ResourceMetacardTransformer method transform.

@Override
public BinaryContent transform(Metacard metacard, Map<String, Serializable> arguments) throws CatalogTransformerException {
    LOGGER.trace("Entering resource ResourceMetacardTransformer.transform");
    if (!isValid(metacard)) {
        throw new CatalogTransformerException("Could not transform metacard to a resource because the metacard is not valid.");
    }
    if (StringUtils.isNotEmpty(metacard.getResourceSize())) {
        arguments.put(Metacard.RESOURCE_SIZE, metacard.getResourceSize());
    }
    String id = metacard.getId();
    LOGGER.debug("executing resource request with id '{}'", id);
    final ResourceRequest resourceRequest = new ResourceRequestById(id, arguments);
    ResourceResponse resourceResponse = null;
    String sourceName = metacard.getSourceId();
    if (StringUtils.isBlank(sourceName)) {
        sourceName = catalogFramework.getId();
    }
    String resourceUriAscii = "";
    if (metacard.getResourceURI() != null) {
        resourceUriAscii = metacard.getResourceURI().toASCIIString();
    }
    try {
        resourceResponse = catalogFramework.getResource(resourceRequest, sourceName);
    } catch (IOException | ResourceNotFoundException | ResourceNotSupportedException e) {
        throw new CatalogTransformerException(retrieveResourceFailureMessage(id, sourceName, resourceUriAscii, e.getMessage()), e);
    }
    if (resourceResponse == null) {
        throw new CatalogTransformerException(retrieveResourceFailureMessage(id, sourceName, resourceUriAscii));
    }
    Resource transformedContent = resourceResponse.getResource();
    MimeType mimeType = transformedContent.getMimeType();
    if (mimeType == null) {
        try {
            mimeType = new MimeType(DEFAULT_MIME_TYPE_STR);
            // There is no method to set the MIME type, so in order to set it to our default
            // one, we need to create a new object.
            transformedContent = new ResourceImpl(transformedContent.getInputStream(), mimeType, transformedContent.getName());
        } catch (MimeTypeParseException e) {
            throw new CatalogTransformerException("Could not create default mime type upon null mimeType, for default mime type '" + DEFAULT_MIME_TYPE_STR + "'.", e);
        }
    }
    LOGGER.debug("Found mime type: '{}' for product of metacard with id: '{}'.\nGetting associated resource from input stream. \n", mimeType, id);
    LOGGER.trace("Exiting resource transform for metacard id: '{}'", id);
    return transformedContent;
}
Also used : MimeTypeParseException(javax.activation.MimeTypeParseException) Resource(ddf.catalog.resource.Resource) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) 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) ResourceRequest(ddf.catalog.operation.ResourceRequest) ResourceNotFoundException(ddf.catalog.resource.ResourceNotFoundException)

Example 79 with MimeType

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

the class InputTransformerProducer method transform.

@Override
protected Object transform(Message in, String mimeType, String transformerId, MimeTypeToTransformerMapper mapper) throws MimeTypeParseException, CatalogTransformerException {
    MimeType derivedMimeType = null;
    try (InputStream message = in.getBody(InputStream.class);
        TemporaryFileBackedOutputStream tfbos = new TemporaryFileBackedOutputStream()) {
        if (message == null) {
            throw new CatalogTransformerException("Message body was null; unable to generate Metacard!");
        }
        // First try to get mimeType from file extension passed in the Camel Message headers
        IOUtils.copy(message, tfbos);
        String fileExtensionHeader = getHeaderAsStringAndRemove(in, FILE_EXTENSION_HEADER);
        if (StringUtils.isNotEmpty(fileExtensionHeader)) {
            Optional<String> fileMimeType = getMimeTypeFor(tfbos.asByteSource().openBufferedStream(), fileExtensionHeader);
            if (fileMimeType.isPresent()) {
                LOGGER.trace("Setting mimetype to [{}] from Message header [{}]", fileMimeType.get(), FILE_EXTENSION_HEADER);
                derivedMimeType = new MimeType(fileMimeType.get());
            }
        }
        if (derivedMimeType == null) {
            if (StringUtils.isNotEmpty(mimeType)) {
                // mimeType and tranformerId
                if (StringUtils.isNotEmpty(transformerId)) {
                    derivedMimeType = new MimeType(mimeType + ";" + MimeTypeToTransformerMapper.ID_KEY + "=" + transformerId);
                    LOGGER.trace("Using mimeType to [{}]", derivedMimeType);
                } else {
                    LOGGER.trace("Using CatalogEndpoint's configured mimeType [{}]", mimeType);
                    derivedMimeType = new MimeType(mimeType);
                }
            } else {
                LOGGER.debug("Unable to determine mimeType. Defaulting to [{}]", DEFAULT_MIME_TYPE);
                derivedMimeType = new MimeType(DEFAULT_MIME_TYPE);
            }
        }
        String metacardUpdateID = getHeaderAsStringAndRemove(in, METACARD_ID_HEADER);
        return generateMetacard(derivedMimeType, mapper, tfbos, metacardUpdateID).orElseThrow(() -> new CatalogTransformerException(String.format("Did not find an InputTransformer for MIME Type [%s] and %s [%s]", mimeType, MimeTypeToTransformerMapper.ID_KEY, transformerId)));
    } catch (IOException e) {
        throw new CatalogTransformerException("Unable to transform incoming product", e);
    }
}
Also used : TemporaryFileBackedOutputStream(org.codice.ddf.platform.util.TemporaryFileBackedOutputStream) InputStream(java.io.InputStream) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) IOException(java.io.IOException) MimeType(javax.activation.MimeType)

Example 80 with MimeType

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

the class BinaryContentStringTypeConverter method convertTo.

@Override
public <T> T convertTo(Class<T> type, Exchange exchange, Object value) {
    String mimeTypeString = exchange.getMessage().getHeader(HttpHeaders.CONTENT_TYPE, String.class);
    if (null == mimeTypeString) {
        mimeTypeString = MediaType.TEXT_PLAIN;
    }
    MimeType mimeType = null;
    try {
        mimeType = new MimeType(mimeTypeString);
    } catch (MimeTypeParseException e) {
        LOGGER.debug("Failed to parse mimetype: " + mimeTypeString, e);
    }
    T result = null;
    try {
        result = type.cast(new BinaryContentImpl(exchange.getMessage().getBody(InputStream.class), mimeType));
    } catch (ClassCastException e) {
        LOGGER.debug("Failed to create BinaryContent", e);
    }
    return result;
}
Also used : MimeTypeParseException(javax.activation.MimeTypeParseException) InputStream(java.io.InputStream) BinaryContentImpl(ddf.catalog.data.impl.BinaryContentImpl) MimeType(javax.activation.MimeType)

Aggregations

MimeType (javax.activation.MimeType)146 Test (org.junit.Test)74 Metacard (ddf.catalog.data.Metacard)36 MimeTypeParseException (javax.activation.MimeTypeParseException)34 URI (java.net.URI)28 MimeTypeToTransformerMapper (ddf.mime.MimeTypeToTransformerMapper)25 HashMap (java.util.HashMap)25 BundleContext (org.osgi.framework.BundleContext)25 ServiceReference (org.osgi.framework.ServiceReference)25 CatalogFramework (ddf.catalog.CatalogFramework)20 Serializable (java.io.Serializable)16 ResourceResponse (ddf.catalog.operation.ResourceResponse)15 IOException (java.io.IOException)14 BinaryContentImpl (ddf.catalog.data.impl.BinaryContentImpl)11 InputStream (java.io.InputStream)10 StringWriter (java.io.StringWriter)10 MetacardCreationException (ddf.catalog.data.MetacardCreationException)9 Resource (ddf.catalog.resource.Resource)9 CatalogTransformerException (ddf.catalog.transform.CatalogTransformerException)9 File (java.io.File)9