Search in sources :

Example 41 with ResourceNotFoundException

use of ddf.catalog.resource.ResourceNotFoundException 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)

Aggregations

ResourceNotFoundException (ddf.catalog.resource.ResourceNotFoundException)41 IOException (java.io.IOException)25 ResourceResponse (ddf.catalog.operation.ResourceResponse)17 URI (java.net.URI)16 ResourceNotSupportedException (ddf.catalog.resource.ResourceNotSupportedException)14 Serializable (java.io.Serializable)14 Metacard (ddf.catalog.data.Metacard)13 Test (org.junit.Test)12 HashMap (java.util.HashMap)11 URISyntaxException (java.net.URISyntaxException)10 Result (ddf.catalog.data.Result)8 ResourceRequest (ddf.catalog.operation.ResourceRequest)8 QueryRequestImpl (ddf.catalog.operation.impl.QueryRequestImpl)8 ResourceRequestById (ddf.catalog.operation.impl.ResourceRequestById)7 Resource (ddf.catalog.resource.Resource)7 QueryRequest (ddf.catalog.operation.QueryRequest)6 QueryResponse (ddf.catalog.operation.QueryResponse)6 ResourceResponseImpl (ddf.catalog.operation.impl.ResourceResponseImpl)6 CatalogFramework (ddf.catalog.CatalogFramework)5 FederationException (ddf.catalog.federation.FederationException)5