use of ddf.catalog.operation.ResourceRequest in project ddf by codice.
the class FilterPluginTest method setup.
@Before
public void setup() {
AuthorizingRealm realm = mock(AuthorizingRealm.class);
when(realm.getName()).thenReturn("mockRealm");
when(realm.isPermitted(any(PrincipalCollection.class), any(Permission.class))).then(makeDecision());
Collection<org.apache.shiro.realm.Realm> realms = new ArrayList<>();
realms.add(realm);
DefaultSecurityManager manager = new DefaultSecurityManager();
manager.setRealms(realms);
SimplePrincipalCollection principalCollection = new SimplePrincipalCollection(new Principal() {
@Override
public String getName() {
return "testuser";
}
}, realm.getName());
Subject systemSubject = new MockSubject(manager, principalCollection);
plugin = new FilterPlugin(new Security()) {
@Override
protected Subject getSystemSubject() {
return systemSubject;
}
};
plugin.setPermissions(new PermissionsImpl());
plugin.setSubjectOperations(new SubjectUtils());
plugin.setSecurityLogger(mock(SecurityLogger.class));
QueryRequestImpl request = getSampleRequest();
Map<String, Serializable> properties = new HashMap<>();
Subject subject = new MockSubject(manager, principalCollection);
properties.put(SecurityConstants.SECURITY_SUBJECT, subject);
request.setProperties(properties);
incomingResponse = new QueryResponseImpl(request);
ResourceRequest resourceRequest = mock(ResourceRequest.class);
when(resourceRequest.getProperties()).thenReturn(properties);
resourceResponse = new ResourceResponseImpl(resourceRequest, mock(Resource.class));
resourceResponse.setProperties(properties);
DeleteRequest deleteRequest = mock(DeleteRequest.class);
when(deleteRequest.getProperties()).thenReturn(properties);
List<Metacard> deletedMetacards = new ArrayList<>();
deletedMetacards.add(getExactRolesMetacard());
deleteResponse = new DeleteResponseImpl(deleteRequest, properties, deletedMetacards);
List<Metacard> badDeletedMetacards = new ArrayList<>();
badDeletedMetacards.add(getMoreRolesMetacard());
badDeleteResponse = new DeleteResponseImpl(deleteRequest, properties, badDeletedMetacards);
createRequest = new CreateRequestImpl(getExactRolesMetacard());
createRequest.setProperties(properties);
badCreateRequest = new CreateRequestImpl(getMoreRolesMetacard());
badCreateRequest.setProperties(properties);
updateRequest = new UpdateRequestImpl(getExactRolesMetacard().getId(), getExactRolesMetacard());
updateRequest.setProperties(properties);
ResultImpl result1 = new ResultImpl(getMoreRolesMetacard());
ResultImpl result2 = new ResultImpl(getMissingRolesMetacard());
ResultImpl result3 = new ResultImpl(getExactRolesMetacard());
ResultImpl result4 = new ResultImpl(getNoRolesMetacard());
ResultImpl result5 = new ResultImpl(getNoSecurityAttributeMetacard());
incomingResponse.addResult(result1, false);
incomingResponse.addResult(result2, false);
incomingResponse.addResult(result3, false);
incomingResponse.addResult(result4, false);
incomingResponse.addResult(result5, true);
}
use of ddf.catalog.operation.ResourceRequest in project ddf by codice.
the class CatalogPolicyTest method testPreResource.
@Test
public void testPreResource() throws StopProcessingException {
policyPlugin.setReadPermissions(new String[] { "role=admin" });
ResourceRequest mock = mock(ResourceRequest.class);
Map<String, Serializable> properties = new HashMap<>();
properties.put(Constants.LOCAL_DESTINATION_KEY, true);
when(mock.getProperties()).thenReturn(properties);
PolicyResponse response = policyPlugin.processPreResource(mock);
assertThat(response.itemPolicy().size(), equalTo(0));
assertThat(response.operationPolicy().size(), equalTo(1));
properties.put(Constants.LOCAL_DESTINATION_KEY, false);
response = policyPlugin.processPreResource(mock);
assertThat(response.itemPolicy().size(), equalTo(0));
assertThat(response.operationPolicy().size(), equalTo(0));
}
use of ddf.catalog.operation.ResourceRequest 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.operation.ResourceRequest in project ddf by codice.
the class MetacardResourceSizePlugin method process.
@Override
public QueryResponse process(QueryResponse input) throws PluginExecutionException, StopProcessingException {
List<Result> results = input.getResults();
for (Result result : results) {
Metacard metacard = result.getMetacard();
if (metacard != null) {
// Can only search cache based on Metacard - no way to generate ResourceRequest with
// any properties for use in generating the CacheKey
final ResourceRequest resourceRequest = new ResourceRequestById(metacard.getId());
CacheKey cacheKey;
String key = null;
ReliableResource cachedResource = null;
try {
cacheKey = new CacheKey(metacard, resourceRequest);
ClassLoader tccl = Thread.currentThread().getContextClassLoader();
key = cacheKey.generateKey();
try {
Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
cachedResource = (ReliableResource) cache.getValid(key, metacard);
} finally {
Thread.currentThread().setContextClassLoader(tccl);
}
} catch (IllegalArgumentException e) {
LOGGER.debug("Unable to retrieve cached resource for metacard id = {}", metacard.getId());
}
if (cachedResource != null) {
long resourceSize = cachedResource.getSize();
if (resourceSize > 0 && cachedResource.hasProduct()) {
LOGGER.debug("Setting resourceSize = {} for metacard ID = {}", resourceSize, metacard.getId());
Attribute resourceSizeAttribute = new AttributeImpl(Metacard.RESOURCE_SIZE, String.valueOf(resourceSize));
metacard.setAttribute(resourceSizeAttribute);
} else {
LOGGER.debug("resourceSize <= 0 for metacard ID = {}", metacard.getId());
}
} else {
LOGGER.debug("No cached resource for cache key = {}", key);
}
}
}
return input;
}
use of ddf.catalog.operation.ResourceRequest in project ddf by codice.
the class CatalogMetricsTest method catalogPostResourceLatencyMetric.
@Test
public void catalogPostResourceLatencyMetric() throws Exception {
Iterable<Tag> tags = Tags.of("successful", "true");
ResourceRequest request = mock(ResourceRequest.class);
ResourceResponse response = mock(ResourceResponse.class);
when(response.getRequest()).thenReturn(request);
when(request.getPropertyValue("metrics.catalog.operation.start")).thenReturn(System.currentTimeMillis() - 1000);
catalogMetrics.process(response);
assertThat(meterRegistry.summary("ddf.catalog.resource.latency", tags).count(), is(1L));
assertThat(meterRegistry.summary("ddf.catalog.resource.latency", tags).max(), greaterThanOrEqualTo(1000.0));
}
Aggregations