Search in sources :

Example 36 with ResourceNotFoundException

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

Example 37 with ResourceNotFoundException

use of ddf.catalog.resource.ResourceNotFoundException in project ddf by codice.

the class ResourceOperations method getResourceInfo.

/**
 * Retrieves a resource by URI.
 *
 * <p>The {@link ResourceRequest} can specify either the product's URI or ID. If the product ID is
 * specified, then the matching {@link Metacard} must first be retrieved and the product URI
 * extracted from this {@link Metacard}.
 *
 * @param resourceRequest
 * @param site
 * @param isEnterprise
 * @param federatedSite
 * @param requestProperties
 * @param fanoutEnabled
 * @return
 * @throws ResourceNotSupportedException
 * @throws ResourceNotFoundException
 */
protected ResourceInfo getResourceInfo(ResourceRequest resourceRequest, String site, boolean isEnterprise, StringBuilder federatedSite, Map<String, Serializable> requestProperties, boolean fanoutEnabled) throws ResourceNotSupportedException, ResourceNotFoundException {
    ResourceInfo resourceInfo;
    Query query = null;
    URI resourceUri = null;
    String name = resourceRequest.getAttributeName();
    Object value = resourceRequest.getAttributeValue();
    validateResourceInfoRequest(name, value);
    try {
        if (ResourceRequest.GET_RESOURCE_BY_PRODUCT_URI.equals(name)) {
            // because this is a get resource by product uri, we already
            // have the product uri to return
            LOGGER.debug("get resource by product uri");
            resourceUri = (URI) value;
            URI truncatedUri = resourceUri;
            if (StringUtils.isNotBlank(resourceUri.getFragment())) {
                resourceRequest.getProperties().put(ContentItem.QUALIFIER_KEYWORD, resourceUri.getFragment());
                try {
                    // Creating the truncated URL this way is important to preserve encoding!!
                    String uriString = resourceUri.toString();
                    truncatedUri = new URI(uriString.substring(0, uriString.lastIndexOf('#')));
                } catch (URISyntaxException e) {
                    throw new ResourceNotFoundException("Could not resolve URI by doing a URI based query: " + value);
                }
            }
            query = createPropertyStartsWithQuery(Metacard.RESOURCE_URI, truncatedUri.toString());
        } else if (ResourceRequest.GET_RESOURCE_BY_ID.equals(name)) {
            // since this is a get resource by id, we need to obtain the
            // product URI
            LOGGER.debug("get resource by id");
            String metacardId = (String) value;
            LOGGER.debug("metacardId = {},   site = {}", metacardId, site);
            query = createMetacardIdQuery(metacardId);
        }
        QueryRequest queryRequest = new QueryRequestImpl(anyTag(query, site, isEnterprise), isEnterprise, Collections.singletonList(site == null ? this.getId() : site), resourceRequest.getProperties());
        resourceInfo = getResourceInfo(queryRequest, resourceUri, requestProperties, federatedSite, fanoutEnabled);
    } catch (UnsupportedQueryException | FederationException e) {
        throw new ResourceNotFoundException(DEFAULT_RESOURCE_NOT_FOUND_MESSAGE, e);
    }
    if (resourceInfo.getResourceUri() == null) {
        throw new ResourceNotFoundException(DEFAULT_RESOURCE_NOT_FOUND_MESSAGE);
    }
    LOGGER.debug("Returning resourceURI: {}", resourceInfo.getResourceUri());
    return resourceInfo;
}
Also used : Query(ddf.catalog.operation.Query) QueryRequest(ddf.catalog.operation.QueryRequest) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) URISyntaxException(java.net.URISyntaxException) ResourceNotFoundException(ddf.catalog.resource.ResourceNotFoundException) FederationException(ddf.catalog.federation.FederationException) URI(java.net.URI)

Example 38 with ResourceNotFoundException

use of ddf.catalog.resource.ResourceNotFoundException in project ddf by codice.

the class LocalResourceRetriever method retrieveResource.

@Override
public ResourceResponse retrieveResource(long bytesToSkip) throws ResourceNotFoundException {
    final String methodName = "retrieveResource";
    LOGGER.trace("ENTERING: {}", methodName);
    ResourceResponse resource = null;
    if (resourceUri == null) {
        throw new ResourceNotFoundException("Unable to find resource due to null URI");
    }
    Map<String, Serializable> props = new HashMap<>(properties);
    if (bytesToSkip > 0) {
        props.put(BYTES_TO_SKIP, bytesToSkip);
    }
    URI derivedUri = null;
    Serializable serializable = props.get(ContentItem.QUALIFIER_KEYWORD);
    if (serializable != null && serializable instanceof String) {
        LOGGER.debug("Received qualifier in request properties, looking for qualified content on metacard with id [{}]", resourceMetacard.getId());
        String fragment = (String) serializable;
        derivedUri = getDerivedUriWithFragment(resourceMetacard, fragment);
    }
    String scheme;
    URI resourceRetrievalUri;
    if (derivedUri == null) {
        scheme = resourceUri.getScheme();
        resourceRetrievalUri = resourceUri;
    } else {
        scheme = derivedUri.getScheme();
        resourceRetrievalUri = derivedUri;
    }
    for (ResourceReader reader : resourceReaders) {
        if (reader != null && reader.getSupportedSchemes().contains(scheme)) {
            try {
                if (LOGGER.isDebugEnabled()) {
                    LOGGER.debug("Found an acceptable resource reader ({}) for URI {}", reader.getId(), resourceRetrievalUri.toASCIIString());
                }
                resource = reader.retrieveResource(resourceRetrievalUri, props);
                if (resource != null) {
                    break;
                } else {
                    LOGGER.debug("Resource returned from ResourceReader {} was null. Checking other readers for URI: {}", reader.getId(), resourceRetrievalUri);
                }
            } catch (ResourceNotFoundException | ResourceNotSupportedException | IOException e) {
                LOGGER.debug("Product not found using resource reader with name {}", reader.getId(), e);
            }
        }
    }
    if (resource == null) {
        throw new ResourceNotFoundException("Resource Readers could not find resource (or returned null resource) for URI: " + resourceRetrievalUri.toASCIIString() + ". Scheme: " + resourceRetrievalUri.getScheme());
    }
    LOGGER.debug("Received resource, sending back: {}", resource.getResource().getName());
    LOGGER.trace("EXITING: {}", methodName);
    return resource;
}
Also used : ResourceReader(ddf.catalog.resource.ResourceReader) Serializable(java.io.Serializable) ResourceResponse(ddf.catalog.operation.ResourceResponse) ResourceNotSupportedException(ddf.catalog.resource.ResourceNotSupportedException) HashMap(java.util.HashMap) IOException(java.io.IOException) ResourceNotFoundException(ddf.catalog.resource.ResourceNotFoundException) URI(java.net.URI)

Example 39 with ResourceNotFoundException

use of ddf.catalog.resource.ResourceNotFoundException in project ddf by codice.

the class AbstractCswSource method retrieveResource.

@Override
public ResourceResponse retrieveResource(URI resourceUri, Map<String, Serializable> requestProperties) throws IOException, ResourceNotFoundException, ResourceNotSupportedException {
    Serializable serializableId = null;
    String username = cswSourceConfiguration.getUsername();
    String password = cswSourceConfiguration.getPassword();
    if (requestProperties != null) {
        serializableId = requestProperties.get(Core.ID);
        if (StringUtils.isNotBlank(username)) {
            requestProperties.put(USERNAME_PROPERTY, username);
            requestProperties.put(PASSWORD_PROPERTY, password);
        }
        if (OAUTH_AUTH_TYPE.equals(cswSourceConfiguration.getAuthenticationType()) && StringUtils.isNotBlank(cswSourceConfiguration.getOauthDiscoveryUrl()) && StringUtils.isNotBlank(cswSourceConfiguration.getOauthClientId()) && StringUtils.isNotBlank(cswSourceConfiguration.getOauthClientSecret())) {
            requestProperties.put(ID_PROPERTY, cswSourceConfiguration.getId());
            requestProperties.put(OAUTH_DISCOVERY_URL, cswSourceConfiguration.getOauthDiscoveryUrl());
            requestProperties.put(OAUTH_CLIENT_ID, cswSourceConfiguration.getOauthClientId());
            requestProperties.put(OAUTH_CLIENT_SECRET, cswSourceConfiguration.getOauthClientSecret());
            requestProperties.put(OAUTH_FLOW, cswSourceConfiguration.getOauthFlow());
        }
    }
    if (canRetrieveResourceById()) {
        // If no resource reader was found, retrieve the product through a GetRecordById request
        if (serializableId == null) {
            throw new ResourceNotFoundException("Unable to retrieve resource because no metacard ID was found.");
        }
        String metacardId = serializableId.toString();
        LOGGER.debug("Retrieving resource for ID : {}", metacardId);
        ResourceResponse response = retrieveResourceById(requestProperties, metacardId);
        if (response != null) {
            return response;
        }
    }
    if (resourceUri == null) {
        throw new IllegalArgumentException("Unable to retrieve resource because no resource URI was given");
    }
    LOGGER.debug("Retrieving resource at : {}", resourceUri);
    return resourceReader.retrieveResource(resourceUri, requestProperties);
}
Also used : Serializable(java.io.Serializable) ResourceResponse(ddf.catalog.operation.ResourceResponse) ResourceNotFoundException(ddf.catalog.resource.ResourceNotFoundException)

Example 40 with ResourceNotFoundException

use of ddf.catalog.resource.ResourceNotFoundException in project ddf by codice.

the class ResourceMetacardTransformerTest method testFrameworkThrowsResourceNotFoundException.

@Test
public void testFrameworkThrowsResourceNotFoundException() throws Exception {
    String filePath = ABSOLUTE_PATH + TEST_PATH + JPEG_FILE_NAME_1;
    URI uri = getUri(filePath);
    Metacard metacard = getMockMetacard(uri);
    thrown.expect(CatalogTransformerException.class);
    thrown.expectMessage("Unable to retrieve resource.");
    thrown.expectMessage("Metacard id: " + TEST_ID);
    thrown.expectMessage("Uri: " + uri);
    thrown.expectMessage("Source: " + TEST_SITE);
    boolean expectSuccess = false;
    MimeType mimeType = getMimeType(JPEG_MIME_TYPE);
    CatalogFramework framework = getFrameworkException(new ResourceNotFoundException("Test Resource Not Found Exception"));
    testGetResource(metacard, filePath, mimeType, framework, expectSuccess);
}
Also used : Metacard(ddf.catalog.data.Metacard) CatalogFramework(ddf.catalog.CatalogFramework) ResourceNotFoundException(ddf.catalog.resource.ResourceNotFoundException) URI(java.net.URI) MimeType(javax.activation.MimeType) Test(org.junit.Test)

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