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;
}
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;
}
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;
}
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));
}
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);
}
Aggregations