Search in sources :

Example 1 with DownloadException

use of org.codice.ddf.catalog.resource.download.DownloadException in project ddf by codice.

the class ReliableResourceDownloadManager method download.

/**
 * @param resourceRequest the original @ResourceRequest to retrieve the resource
 * @param metacard the @Metacard associated with the resource being downloaded
 * @param retriever the @ResourceRetriever to be used to get the resource
 * @return the modified @ResourceResponse with the @ReliableResourceInputStream that the client
 *     should read from
 * @throws DownloadException
 */
@Override
public ResourceResponse download(ResourceRequest resourceRequest, Metacard metacard, ResourceRetriever retriever) throws DownloadException {
    ResourceResponse resourceResponse = null;
    String downloadIdentifier = UUID.randomUUID().toString();
    if (metacard == null) {
        throw new DownloadException("Cannot download resource if metacard is null");
    } else if (StringUtils.isBlank(metacard.getId())) {
        throw new DownloadException("Metacard must have unique id.");
    } else if (retriever == null) {
        throw new DownloadException("Cannot download resource if retriever is null");
    } else if (resourceRequest == null) {
        throw new DownloadException("Cannot download resource if request is null");
    }
    if (downloaderConfig.isCacheEnabled()) {
        Resource cachedResource = downloaderConfig.getResourceCache().getValid(new CacheKey(metacard, resourceRequest).generateKey(), metacard);
        if (cachedResource != null) {
            resourceResponse = new ResourceResponseImpl(resourceRequest, resourceRequest.getProperties(), cachedResource);
            LOGGER.debug("Successfully retrieved product from cache for metacard ID = {}", metacard.getId());
        } else {
            LOGGER.debug("Unable to get resource from cache. Have to retrieve it from source");
        }
    }
    if (resourceResponse == null) {
        try {
            resourceResponse = retriever.retrieveResource();
        } catch (ResourceNotFoundException | ResourceNotSupportedException | IOException e) {
            throw new DownloadException("Cannot download resource", e);
        }
        resourceResponse.getProperties().put(Metacard.ID, metacard.getId());
        // Sources do not create ResourceResponses with the original ResourceRequest, hence
        // it is added here because it will be needed for caching
        resourceResponse = new ResourceResponseImpl(resourceRequest, resourceResponse.getProperties(), resourceResponse.getResource());
        resourceResponse = startDownload(downloadIdentifier, resourceResponse, retriever, metacard);
    }
    return resourceResponse;
}
Also used : ResourceResponse(ddf.catalog.operation.ResourceResponse) ResourceNotSupportedException(ddf.catalog.resource.ResourceNotSupportedException) DownloadException(org.codice.ddf.catalog.resource.download.DownloadException) Resource(ddf.catalog.resource.Resource) ResourceResponseImpl(ddf.catalog.operation.impl.ResourceResponseImpl) IOException(java.io.IOException) ResourceNotFoundException(ddf.catalog.resource.ResourceNotFoundException) CacheKey(ddf.catalog.cache.impl.CacheKey)

Example 2 with DownloadException

use of org.codice.ddf.catalog.resource.download.DownloadException in project ddf by codice.

the class ResourceOperations method getResource.

// 
// 
// 
@SuppressWarnings("javadoc")
ResourceResponse getResource(ResourceRequest resourceRequest, boolean isEnterprise, String resourceSiteName, boolean fanoutEnabled) throws IOException, ResourceNotFoundException, ResourceNotSupportedException {
    ResourceResponse resourceResponse = null;
    ResourceRequest resourceReq = resourceRequest;
    ResourceRetriever retriever = null;
    if (fanoutEnabled) {
        isEnterprise = true;
    }
    if (resourceSiteName == null && !isEnterprise) {
        throw new ResourceNotFoundException("resourceSiteName cannot be null when obtaining resource.");
    }
    validateGetResourceRequest(resourceReq);
    try {
        resourceReq = preProcessPreAuthorizationPlugins(resourceReq);
        resourceReq = processPreResourcePolicyPlugins(resourceReq);
        resourceReq = processPreResourceAccessPlugins(resourceReq);
        resourceReq = processPreResourcePlugins(resourceReq);
        Map<String, Serializable> requestProperties = resourceReq.getProperties();
        LOGGER.debug("Attempting to get resource from siteName: {}", resourceSiteName);
        // At this point we pull out the properties and use them.
        Serializable sourceIdProperty = requestProperties.get(ResourceRequest.SOURCE_ID);
        final String namedSource = (sourceIdProperty != null) ? sourceIdProperty.toString() : resourceSiteName;
        Serializable enterpriseProperty = requestProperties.get(ResourceRequest.IS_ENTERPRISE);
        if (enterpriseProperty != null && Boolean.parseBoolean(enterpriseProperty.toString())) {
            isEnterprise = true;
        }
        // check if the resourceRequest has an ID only
        // If so, the metacard needs to be found and the Resource URI
        StringBuilder resolvedSourceIdHolder = new StringBuilder();
        ResourceInfo resourceInfo = getResourceInfo(resourceReq, namedSource, isEnterprise, resolvedSourceIdHolder, requestProperties, fanoutEnabled);
        if (resourceInfo == null) {
            throw new ResourceNotFoundException("Resource could not be found for the given attribute value: " + resourceReq.getAttributeValue());
        }
        final URI responseURI = resourceInfo.getResourceUri();
        final Metacard metacard = resourceInfo.getMetacard();
        final String resolvedSourceId = resolvedSourceIdHolder.toString();
        LOGGER.debug("resolvedSourceId = {}", resolvedSourceId);
        LOGGER.debug("ID = {}", getId());
        // since resolvedSourceId specifies what source the product
        // metacard resides on, we can just
        // change resourceSiteName to be that value, and then the
        // following if-else statements will
        // handle retrieving the product on the correct source
        final String resourceSourceName = (isEnterprise) ? resolvedSourceId : namedSource;
        // retrieve product from specified federated site if not in cache
        if (!resourceSourceName.equals(getId())) {
            LOGGER.debug("Searching federatedSource {} for resource.", resourceSourceName);
            LOGGER.debug("metacard for product found on source: {}", resolvedSourceId);
            final List<FederatedSource> sources = frameworkProperties.getFederatedSources().stream().filter(e -> e.getId().equals(resourceSourceName)).collect(Collectors.toList());
            if (!sources.isEmpty()) {
                if (sources.size() != 1) {
                    LOGGER.debug("Found multiple federatedSources for id: {}", resourceSourceName);
                }
                FederatedSource source = sources.get(0);
                LOGGER.debug("Adding federated site to federated query: {}", source.getId());
                LOGGER.debug("Retrieving product from remote source {}", source.getId());
                retriever = new RemoteResourceRetriever(source, responseURI, requestProperties);
            } else {
                LOGGER.debug("Could not find federatedSource: {}", resourceSourceName);
            }
        } else {
            LOGGER.debug("Retrieving product from local source {}", resourceSourceName);
            retriever = new LocalResourceRetriever(frameworkProperties.getResourceReaders(), responseURI, metacard, requestProperties);
        }
        try {
            resourceResponse = frameworkProperties.getDownloadManager().download(resourceRequest, metacard, retriever);
        } catch (DownloadException e) {
            LOGGER.info("Unable to download resource", e);
        }
        resourceResponse = putPropertiesInResponse(resourceRequest, resourceResponse);
        resourceResponse = validateFixGetResourceResponse(resourceResponse, resourceReq);
        resourceResponse = postProcessPreAuthorizationPlugins(resourceResponse, metacard);
        resourceResponse = processPostResourcePolicyPlugins(resourceResponse, metacard);
        resourceResponse = processPostResourceAccessPlugins(resourceResponse, metacard);
        resourceResponse = processPostResourcePlugins(resourceResponse);
        resourceResponse.getProperties().put(Constants.METACARD_PROPERTY, metacard);
    } catch (DataUsageLimitExceededException e) {
        LOGGER.info("RuntimeException caused by: ", e);
        throw e;
    } catch (RuntimeException e) {
        LOGGER.info("RuntimeException caused by: ", e);
        throw new ResourceNotFoundException("Unable to find resource");
    } catch (StopProcessingException e) {
        LOGGER.info("Resource not supported", e);
        throw new ResourceNotSupportedException(FAILED_BY_GET_RESOURCE_PLUGIN + e.getMessage());
    }
    return resourceResponse;
}
Also used : LiteralImpl(ddf.catalog.filter.impl.LiteralImpl) StringUtils(org.apache.commons.lang.StringUtils) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) URISyntaxException(java.net.URISyntaxException) LoggerFactory(org.slf4j.LoggerFactory) ResourceRetriever(ddf.catalog.resourceretriever.ResourceRetriever) DataUsageLimitExceededException(ddf.catalog.resource.DataUsageLimitExceededException) PluginExecutionException(ddf.catalog.plugin.PluginExecutionException) SystemInfo(org.codice.ddf.configuration.SystemInfo) Map(java.util.Map) PropertyIsEqualToLiteral(ddf.catalog.filter.impl.PropertyIsEqualToLiteral) PreResourcePlugin(ddf.catalog.plugin.PreResourcePlugin) URI(java.net.URI) PostResourcePlugin(ddf.catalog.plugin.PostResourcePlugin) PolicyPlugin(ddf.catalog.plugin.PolicyPlugin) FederatedSource(ddf.catalog.source.FederatedSource) Set(java.util.Set) ResourceReader(ddf.catalog.resource.ResourceReader) Collectors(java.util.stream.Collectors) ResourceNotFoundException(ddf.catalog.resource.ResourceNotFoundException) Serializable(java.io.Serializable) QueryResponse(ddf.catalog.operation.QueryResponse) List(java.util.List) PreAuthorizationPlugin(ddf.catalog.plugin.PreAuthorizationPlugin) ResourceResponseImpl(ddf.catalog.operation.impl.ResourceResponseImpl) AccessPlugin(ddf.catalog.plugin.AccessPlugin) FilterDelegate(ddf.catalog.filter.FilterDelegate) ResourceResponse(ddf.catalog.operation.ResourceResponse) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) PropertyNameImpl(ddf.catalog.filter.impl.PropertyNameImpl) HashMap(java.util.HashMap) ContentItem(ddf.catalog.content.data.ContentItem) Constants(ddf.catalog.Constants) Metacard(ddf.catalog.data.Metacard) ResourceRequest(ddf.catalog.operation.ResourceRequest) QueryRequest(ddf.catalog.operation.QueryRequest) RemoteResourceRetriever(ddf.catalog.resourceretriever.RemoteResourceRetriever) DownloadException(org.codice.ddf.catalog.resource.download.DownloadException) LocalResourceRetriever(ddf.catalog.resourceretriever.LocalResourceRetriever) Result(ddf.catalog.data.Result) DescribableImpl(ddf.catalog.util.impl.DescribableImpl) QueryImpl(ddf.catalog.operation.impl.QueryImpl) PolicyResponse(ddf.catalog.plugin.PolicyResponse) Logger(org.slf4j.Logger) StopProcessingException(ddf.catalog.plugin.StopProcessingException) IOException(java.io.IOException) FederationException(ddf.catalog.federation.FederationException) FrameworkProperties(ddf.catalog.impl.FrameworkProperties) ResourceNotSupportedException(ddf.catalog.resource.ResourceNotSupportedException) Query(ddf.catalog.operation.Query) Filter(org.opengis.filter.Filter) Collections(java.util.Collections) RemoteResourceRetriever(ddf.catalog.resourceretriever.RemoteResourceRetriever) Serializable(java.io.Serializable) ResourceRetriever(ddf.catalog.resourceretriever.ResourceRetriever) RemoteResourceRetriever(ddf.catalog.resourceretriever.RemoteResourceRetriever) LocalResourceRetriever(ddf.catalog.resourceretriever.LocalResourceRetriever) StopProcessingException(ddf.catalog.plugin.StopProcessingException) URI(java.net.URI) Metacard(ddf.catalog.data.Metacard) FederatedSource(ddf.catalog.source.FederatedSource) ResourceResponse(ddf.catalog.operation.ResourceResponse) ResourceNotSupportedException(ddf.catalog.resource.ResourceNotSupportedException) DataUsageLimitExceededException(ddf.catalog.resource.DataUsageLimitExceededException) DownloadException(org.codice.ddf.catalog.resource.download.DownloadException) LocalResourceRetriever(ddf.catalog.resourceretriever.LocalResourceRetriever) ResourceRequest(ddf.catalog.operation.ResourceRequest) ResourceNotFoundException(ddf.catalog.resource.ResourceNotFoundException)

Aggregations

ResourceResponse (ddf.catalog.operation.ResourceResponse)2 ResourceResponseImpl (ddf.catalog.operation.impl.ResourceResponseImpl)2 ResourceNotFoundException (ddf.catalog.resource.ResourceNotFoundException)2 ResourceNotSupportedException (ddf.catalog.resource.ResourceNotSupportedException)2 IOException (java.io.IOException)2 Constants (ddf.catalog.Constants)1 CacheKey (ddf.catalog.cache.impl.CacheKey)1 ContentItem (ddf.catalog.content.data.ContentItem)1 Metacard (ddf.catalog.data.Metacard)1 Result (ddf.catalog.data.Result)1 FederationException (ddf.catalog.federation.FederationException)1 FilterDelegate (ddf.catalog.filter.FilterDelegate)1 LiteralImpl (ddf.catalog.filter.impl.LiteralImpl)1 PropertyIsEqualToLiteral (ddf.catalog.filter.impl.PropertyIsEqualToLiteral)1 PropertyNameImpl (ddf.catalog.filter.impl.PropertyNameImpl)1 FrameworkProperties (ddf.catalog.impl.FrameworkProperties)1 Query (ddf.catalog.operation.Query)1 QueryRequest (ddf.catalog.operation.QueryRequest)1 QueryResponse (ddf.catalog.operation.QueryResponse)1 ResourceRequest (ddf.catalog.operation.ResourceRequest)1