use of ddf.catalog.data.Attribute 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.data.Attribute in project ddf by codice.
the class TestMetacardResourceSizePlugin method testMetacardResourceSizePopulatedButNoProduct.
/**
* Verifies case where product has been cached previously but has since
* been deleted from the product-cache directory, so there is still an
* entry in the cache map but no cache file on disk.
*
* @throws Exception
*/
@Test
public void testMetacardResourceSizePopulatedButNoProduct() throws Exception {
ResourceCacheInterface cache = mock(ResourceCacheInterface.class);
ReliableResource cachedResource = mock(ReliableResource.class);
when(cachedResource.getSize()).thenReturn(999L);
when(cachedResource.hasProduct()).thenReturn(false);
when(cache.getValid(anyString(), (Metacard) anyObject())).thenReturn(cachedResource);
MetacardImpl metacard = new MetacardImpl();
metacard.setId("abc123");
metacard.setSourceId("ddf-1");
metacard.setResourceSize("N/A");
Result result = new ResultImpl(metacard);
List<Result> results = new ArrayList<Result>();
results.add(result);
QueryResponse input = mock(QueryResponse.class);
when(input.getResults()).thenReturn(results);
MetacardResourceSizePlugin plugin = new MetacardResourceSizePlugin(cache);
QueryResponse queryResponse = plugin.process(input);
assertThat(queryResponse.getResults().size(), is(1));
Metacard resultMetacard = queryResponse.getResults().get(0).getMetacard();
assertThat(metacard, is(notNullValue()));
// Since using Metacard vs. MetacardImpl have to get resource-size as an
// Attribute vs. String
Attribute resourceSizeAttr = resultMetacard.getAttribute(Metacard.RESOURCE_SIZE);
assertThat((String) resourceSizeAttr.getValue(), equalTo("N/A"));
}
use of ddf.catalog.data.Attribute in project ddf by codice.
the class TestMetacardResourceSizePlugin method testWhenCachedResourceSizeIsZero.
@Test
public void testWhenCachedResourceSizeIsZero() throws Exception {
ResourceCacheInterface cache = mock(ResourceCacheInterface.class);
ReliableResource cachedResource = mock(ReliableResource.class);
when(cachedResource.getSize()).thenReturn(0L);
when(cache.getValid(anyString(), (Metacard) anyObject())).thenReturn(cachedResource);
MetacardImpl metacard = new MetacardImpl();
metacard.setId("abc123");
metacard.setSourceId("ddf-1");
metacard.setResourceSize("N/A");
Result result = new ResultImpl(metacard);
List<Result> results = new ArrayList<Result>();
results.add(result);
QueryResponse input = mock(QueryResponse.class);
when(input.getResults()).thenReturn(results);
MetacardResourceSizePlugin plugin = new MetacardResourceSizePlugin(cache);
QueryResponse queryResponse = plugin.process(input);
assertThat(queryResponse.getResults().size(), is(1));
Metacard resultMetacard = queryResponse.getResults().get(0).getMetacard();
assertThat(metacard, is(notNullValue()));
// Since using Metacard vs. MetacardImpl have to get resource-size as an
// Attribute vs. Long
Attribute resourceSizeAttr = resultMetacard.getAttribute(Metacard.RESOURCE_SIZE);
assertThat((String) resourceSizeAttr.getValue(), equalTo("N/A"));
}
use of ddf.catalog.data.Attribute in project ddf by codice.
the class MetacardResourceStatusTest method testMetacardResourceIsLocal2.
/**
* Metacard source id is local, Metacard contains remote resource uri, Metacard resource is cached
*/
@Test
public void testMetacardResourceIsLocal2() throws Exception {
setupCache(true);
setupSingleResultResponseMock(getBasicMetacard(LOCAL_SITE_NAME, REMOTE_RESOURCE_URI));
MetacardResourceStatus plugin = getMetacardResourceStatusPlugin();
Attribute resourceStatusAttribute = getInternalLocalResurceAttribute(plugin.process(queryResponse));
assertThat(resourceStatusAttribute.getValue(), is(true));
}
use of ddf.catalog.data.Attribute in project ddf by codice.
the class MetacardResourceStatusTest method testMetacardResourceIsLocal4.
/**
* Metacard source id is remote, Metacard contains remote resource uri, Metacard resource is
* cached
*/
@Test
public void testMetacardResourceIsLocal4() throws Exception {
setupCache(true);
setupSingleResultResponseMock(getBasicMetacard(REMOTE_SITE_NAME, REMOTE_RESOURCE_URI));
MetacardResourceStatus plugin = getMetacardResourceStatusPlugin();
Attribute resourceStatusAttribute = getInternalLocalResurceAttribute(plugin.process(queryResponse));
assertThat(resourceStatusAttribute.getValue(), is(true));
}
Aggregations