Search in sources :

Example 11 with FederatedSource

use of ddf.catalog.source.FederatedSource 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 12 with FederatedSource

use of ddf.catalog.source.FederatedSource in project ddf by codice.

the class SourceOperations method getFederatedSourceDescriptors.

/**
 * Creates a {@link Set} of {@link SourceDescriptor} based on the incoming list of {@link Source}.
 *
 * @param sources {@link Collection} of {@link Source} to obtain descriptor information from
 * @return new {@link Set} of {@link SourceDescriptor}
 */
private Set<SourceDescriptor> getFederatedSourceDescriptors(Collection<FederatedSource> sources, boolean addCatalogProviderDescriptor) {
    SourceDescriptorImpl sourceDescriptor;
    Set<SourceDescriptor> sourceDescriptors = new TreeSet<>(new SourceDescriptorComparator());
    if (sources != null) {
        for (Source source : sources) {
            if (source != null) {
                String sourceId = source.getId();
                LOGGER.debug("adding sourceId: {}", sourceId);
                sourceDescriptor = new SourceDescriptorImpl(sourceId, contentTypesCache.getCachedValueForSource(source).orElseGet(() -> {
                    LOGGER.debug("Unknown content types for source id={}", source.getId());
                    return Collections.emptySet();
                }), sourceActionRegistry.list(source));
                sourceDescriptor.setVersion(source.getVersion());
                sourceDescriptor.setAvailable(isSourceAvailable(source));
                sourceDescriptors.add(sourceDescriptor);
            }
        }
    }
    if (addCatalogProviderDescriptor) {
        addCatalogSourceDescriptor(sourceDescriptors);
    }
    return sourceDescriptors;
}
Also used : SourceDescriptor(ddf.catalog.source.SourceDescriptor) SourceDescriptorComparator(ddf.catalog.util.impl.SourceDescriptorComparator) SourceDescriptorImpl(ddf.catalog.source.impl.SourceDescriptorImpl) TreeSet(java.util.TreeSet) Source(ddf.catalog.source.Source) FederatedSource(ddf.catalog.source.FederatedSource)

Example 13 with FederatedSource

use of ddf.catalog.source.FederatedSource in project ddf by codice.

the class SourceOperations method getFanoutSourceInfo.

/**
 * Retrieves the {@link SourceDescriptor} info for all {@link FederatedSource}s in the fanout
 * configuration, but the all of the source info, e.g., content types, for all of the available
 * {@link FederatedSource}s is packed into one {@link SourceDescriptor} for the fanout
 * configuration with the fanout's site name in it. This keeps the individual {@link
 * FederatedSource}s' source info hidden from the external client.
 */
private SourceInfoResponse getFanoutSourceInfo(SourceInfoRequest sourceInfoRequest) throws SourceUnavailableException {
    SourceInfoResponse response;
    SourceDescriptorImpl sourceDescriptor;
    try {
        // request
        if (sourceInfoRequest == null) {
            throw new IllegalArgumentException("SourceInfoRequest was null");
        }
        Set<SourceDescriptor> sourceDescriptors = new LinkedHashSet<>();
        Set<String> ids = sourceInfoRequest.getSourceIds();
        // specified
        if (ids != null) {
            Optional<String> notLocal = ids.stream().filter(s -> !s.equals(getId())).findFirst();
            if (notLocal.isPresent()) {
                SourceUnavailableException sourceUnavailableException = new SourceUnavailableException("Unknown source: " + notLocal.get());
                LOGGER.debug("Throwing SourceUnavailableException for unknown source: {}", notLocal.get(), sourceUnavailableException);
                throw sourceUnavailableException;
            }
        }
        // Fanout will only add one source descriptor with all the contents
        // Using a List here instead of a Set because we should not rely on how Sources are compared
        final List<Source> availableSources = frameworkProperties.getFederatedSources().stream().filter(this::isSourceAvailable).collect(Collectors.toList());
        final Set<ContentType> contentTypes = availableSources.stream().map(source -> contentTypesCache.getCachedValueForSource(source).orElseGet(() -> {
            LOGGER.debug("Unknown content types for source id={}", source.getId());
            return Collections.emptySet();
        })).flatMap(Collection::stream).collect(Collectors.toSet());
        if (isSourceAvailable(catalog)) {
            availableSources.add(catalog);
            final Optional<Set<ContentType>> catalogContentTypes = contentTypesCache.getCachedValueForSource(catalog);
            if (catalogContentTypes.isPresent()) {
                contentTypes.addAll(catalogContentTypes.get());
            } else {
                LOGGER.debug("Unknown content types for the localSource");
            }
        }
        List<Action> actions = getSourceActions(catalog);
        // only reveal this sourceDescriptor, not the federated sources
        sourceDescriptor = new SourceDescriptorImpl(this.getId(), contentTypes, actions);
        if (this.getVersion() != null) {
            sourceDescriptor.setVersion(this.getVersion());
        }
        sourceDescriptor.setAvailable(!availableSources.isEmpty());
        sourceDescriptors.add(sourceDescriptor);
        response = new SourceInfoResponseImpl(sourceInfoRequest, null, sourceDescriptors);
    } catch (RuntimeException re) {
        throw new SourceUnavailableException(GET_SOURCE_EXCEPTION_MSG, re);
    }
    return response;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) SourceStatus(org.codice.ddf.catalog.sourcepoller.SourceStatus) LoggerFactory(org.slf4j.LoggerFactory) TreeSet(java.util.TreeSet) Source(ddf.catalog.source.Source) SourceDescriptor(ddf.catalog.source.SourceDescriptor) HashSet(java.util.HashSet) Action(ddf.action.Action) StorageProvider(ddf.catalog.content.StorageProvider) SourceInfoResponseImpl(ddf.catalog.operation.impl.SourceInfoResponseImpl) Describable(ddf.catalog.util.Describable) SourceDescriptorComparator(ddf.catalog.util.impl.SourceDescriptorComparator) LinkedHashSet(java.util.LinkedHashSet) DescribableImpl(ddf.catalog.util.impl.DescribableImpl) SourceInfoResponse(ddf.catalog.operation.SourceInfoResponse) ContentType(ddf.catalog.data.ContentType) SourcePoller(org.codice.ddf.catalog.sourcepoller.SourcePoller) Logger(org.slf4j.Logger) SourceDescriptorImpl(ddf.catalog.source.impl.SourceDescriptorImpl) FederatedSource(ddf.catalog.source.FederatedSource) Collection(java.util.Collection) Set(java.util.Set) FrameworkProperties(ddf.catalog.impl.FrameworkProperties) Collectors(java.util.stream.Collectors) SourceInfoRequest(ddf.catalog.operation.SourceInfoRequest) List(java.util.List) CatalogProvider(ddf.catalog.source.CatalogProvider) Optional(java.util.Optional) ActionRegistry(ddf.action.ActionRegistry) Collections(java.util.Collections) Validate(org.apache.commons.lang.Validate) SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) SourceDescriptor(ddf.catalog.source.SourceDescriptor) Action(ddf.action.Action) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) ContentType(ddf.catalog.data.ContentType) SourceDescriptorImpl(ddf.catalog.source.impl.SourceDescriptorImpl) Source(ddf.catalog.source.Source) FederatedSource(ddf.catalog.source.FederatedSource) SourceInfoResponseImpl(ddf.catalog.operation.impl.SourceInfoResponseImpl) SourceInfoResponse(ddf.catalog.operation.SourceInfoResponse)

Example 14 with FederatedSource

use of ddf.catalog.source.FederatedSource in project ddf by codice.

the class CatalogFrameworkImplTest method testFederatedQueryPermissions.

@Test
public void testFederatedQueryPermissions() throws Exception {
    MockEventProcessor eventAdmin = new MockEventProcessor();
    MockMemoryProvider provider = new MockMemoryProvider("Provider", "Provider", "v1.0", "DDF", new HashSet<>(), true, new Date());
    List<CatalogStore> storeList = new ArrayList<>();
    List<FederatedSource> sourceList = new ArrayList<>();
    Map<String, Set<String>> securityAttributes = new HashMap<>();
    securityAttributes.put("role", Collections.singleton("myRole"));
    MockCatalogStore store = new MockCatalogStore("catalogStoreId-1", true, securityAttributes);
    storeList.add(store);
    sourceList.add(store);
    CatalogFramework framework = createDummyCatalogFramework(provider, storeList, sourceList, eventAdmin);
    List<Metacard> metacards = new ArrayList<>();
    MetacardImpl newCard = new MetacardImpl();
    newCard.setId(null);
    newCard.setContentTypeName("someType");
    metacards.add(newCard);
    Map<String, Serializable> reqProps = new HashMap<>();
    HashSet<String> destinations = new HashSet<>();
    // ==== test writing to store and not local ====
    destinations.add("catalogStoreId-1");
    framework.create(new CreateRequestImpl(metacards, reqProps, destinations));
    FilterBuilder builder = new GeotoolsFilterBuilder();
    Subject subject = mock(Subject.class);
    when(subject.isPermitted(any(KeyValueCollectionPermission.class))).thenReturn(true);
    HashMap<String, Serializable> properties = new HashMap<>();
    properties.put(SecurityConstants.SECURITY_SUBJECT, subject);
    QueryImpl query = new QueryImpl(builder.attribute(Metacard.CONTENT_TYPE).is().like().text("someType"));
    QueryRequestImpl request = new QueryRequestImpl(query, false, Collections.singletonList("catalogStoreId-1"), properties);
    QueryResponse response = framework.query(request);
    assertThat(response.getResults().size(), is(1));
}
Also used : Serializable(java.io.Serializable) Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) CatalogStore(ddf.catalog.source.CatalogStore) QueryImpl(ddf.catalog.operation.impl.QueryImpl) GeotoolsFilterBuilder(ddf.catalog.filter.proxy.builder.GeotoolsFilterBuilder) FilterBuilder(ddf.catalog.filter.FilterBuilder) GeotoolsFilterBuilder(ddf.catalog.filter.proxy.builder.GeotoolsFilterBuilder) CatalogFramework(ddf.catalog.CatalogFramework) HashSet(java.util.HashSet) KeyValueCollectionPermission(ddf.security.permission.KeyValueCollectionPermission) Date(java.util.Date) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) Subject(ddf.security.Subject) FederatedSource(ddf.catalog.source.FederatedSource) Metacard(ddf.catalog.data.Metacard) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) QueryResponse(ddf.catalog.operation.QueryResponse) CreateRequestImpl(ddf.catalog.operation.impl.CreateRequestImpl) Test(org.junit.Test)

Example 15 with FederatedSource

use of ddf.catalog.source.FederatedSource in project ddf by codice.

the class CatalogFrameworkImplTest method testFederatedQueryPermissionsNotPermitted.

@Test(expected = FederationException.class)
public void testFederatedQueryPermissionsNotPermitted() throws Exception {
    MockEventProcessor eventAdmin = new MockEventProcessor();
    MockMemoryProvider provider = new MockMemoryProvider("Provider", "Provider", "v1.0", "DDF", new HashSet<>(), true, new Date());
    List<CatalogStore> storeList = new ArrayList<>();
    List<FederatedSource> sourceList = new ArrayList<>();
    Map<String, Set<String>> securityAttributes = new HashMap<>();
    securityAttributes.put("role", Collections.singleton("myRole"));
    MockCatalogStore store = new MockCatalogStore("catalogStoreId-1", true, securityAttributes);
    storeList.add(store);
    sourceList.add(store);
    CatalogFramework framework = createDummyCatalogFramework(provider, storeList, sourceList, eventAdmin);
    FilterBuilder builder = new GeotoolsFilterBuilder();
    Subject subject = mock(Subject.class);
    when(subject.isPermitted(any(KeyValueCollectionPermission.class))).thenReturn(false);
    HashMap<String, Serializable> properties = new HashMap<>();
    properties.put(SecurityConstants.SECURITY_SUBJECT, subject);
    QueryImpl query = new QueryImpl(builder.attribute(Metacard.CONTENT_TYPE).is().like().text("someType"));
    QueryRequestImpl request = new QueryRequestImpl(query, false, Collections.singletonList("catalogStoreId-1"), properties);
    framework.query(request);
}
Also used : KeyValueCollectionPermission(ddf.security.permission.KeyValueCollectionPermission) Serializable(java.io.Serializable) Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Date(java.util.Date) Subject(ddf.security.Subject) CatalogStore(ddf.catalog.source.CatalogStore) FederatedSource(ddf.catalog.source.FederatedSource) QueryImpl(ddf.catalog.operation.impl.QueryImpl) GeotoolsFilterBuilder(ddf.catalog.filter.proxy.builder.GeotoolsFilterBuilder) FilterBuilder(ddf.catalog.filter.FilterBuilder) GeotoolsFilterBuilder(ddf.catalog.filter.proxy.builder.GeotoolsFilterBuilder) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) CatalogFramework(ddf.catalog.CatalogFramework) Test(org.junit.Test)

Aggregations

FederatedSource (ddf.catalog.source.FederatedSource)21 ArrayList (java.util.ArrayList)13 Test (org.junit.Test)12 HashSet (java.util.HashSet)10 Date (java.util.Date)9 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)9 GeotoolsFilterBuilder (ddf.catalog.filter.proxy.builder.GeotoolsFilterBuilder)7 HashMap (java.util.HashMap)7 CatalogFramework (ddf.catalog.CatalogFramework)6 Metacard (ddf.catalog.data.Metacard)6 SourceInfoRequest (ddf.catalog.operation.SourceInfoRequest)6 SourceInfoResponse (ddf.catalog.operation.SourceInfoResponse)6 QueryImpl (ddf.catalog.operation.impl.QueryImpl)6 QueryRequestImpl (ddf.catalog.operation.impl.QueryRequestImpl)6 CatalogProvider (ddf.catalog.source.CatalogProvider)6 List (java.util.List)6 Set (java.util.Set)6 Result (ddf.catalog.data.Result)5 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)5 QueryRequest (ddf.catalog.operation.QueryRequest)5