use of ddf.catalog.cache.impl.CacheKey in project ddf by codice.
the class ReliableResourceDownloadManager method download.
/**
* @param resourceRequest the original @ResourceRequest to retrieve the resource
* @param metacard the @Metacard associated with the resource being downloaded
* @param retriever the @ResourceRetriever to be used to get the resource
* @return the modified @ResourceResponse with the @ReliableResourceInputStream that the client
* should read from
* @throws DownloadException
*/
public ResourceResponse download(ResourceRequest resourceRequest, Metacard metacard, ResourceRetriever retriever) throws DownloadException {
ResourceResponse resourceResponse = null;
String downloadIdentifier = UUID.randomUUID().toString();
if (metacard == null) {
throw new DownloadException("Cannot download resource if metacard is null");
} else if (StringUtils.isBlank(metacard.getId())) {
throw new DownloadException("Metacard must have unique id.");
} else if (retriever == null) {
throw new DownloadException("Cannot download resource if retriever is null");
} else if (resourceRequest == null) {
throw new DownloadException("Cannot download resource if request is null");
}
if (downloaderConfig.isCacheEnabled()) {
Resource cachedResource = downloaderConfig.getResourceCache().getValid(new CacheKey(metacard, resourceRequest).generateKey(), metacard);
if (cachedResource != null) {
resourceResponse = new ResourceResponseImpl(resourceRequest, resourceRequest.getProperties(), cachedResource);
LOGGER.debug("Successfully retrieved product from cache for metacard ID = {}", metacard.getId());
} else {
LOGGER.debug("Unable to get resource from cache. Have to retrieve it from source");
}
}
if (resourceResponse == null) {
try {
resourceResponse = retriever.retrieveResource();
} catch (ResourceNotFoundException | ResourceNotSupportedException | IOException e) {
throw new DownloadException("Cannot download resource", e);
}
resourceResponse.getProperties().put(Metacard.ID, metacard.getId());
// Sources do not create ResourceResponses with the original ResourceRequest, hence
// it is added here because it will be needed for caching
resourceResponse = new ResourceResponseImpl(resourceRequest, resourceResponse.getProperties(), resourceResponse.getResource());
resourceResponse = startDownload(downloadIdentifier, resourceResponse, retriever, metacard);
}
return resourceResponse;
}
use of ddf.catalog.cache.impl.CacheKey in project ddf by codice.
the class CacheKeyTest method testKeyUniquenessProperties.
/**
* Tests keys will be unique if given different properties in the ResourceRequest.
*/
@Test
public void testKeyUniquenessProperties() {
Map<String, Serializable> propertyMap1 = new HashMap<>();
propertyMap1.put(ResourceRequest.OPTION_ARGUMENT, "pdf");
Map<String, Serializable> propertyMap2 = new HashMap<>();
propertyMap2.put(ResourceRequest.OPTION_ARGUMENT, "html");
CacheKey cacheKey1 = new CacheKey(getMetacardStub("sampleId1", "source1"), getResourceRequestStub(propertyMap1));
CacheKey cacheKey2 = new CacheKey(getMetacardStub("sampleId1", "source1"), getResourceRequestStub(propertyMap2));
String key1 = cacheKey1.generateKey();
String key2 = cacheKey2.generateKey();
assertThat("Keys must be different.", key1, not(equalTo(key2)));
}
use of ddf.catalog.cache.impl.CacheKey in project ddf by codice.
the class ReliableResourceDownloadManagerTest method testClientCancelProductDownloadCachingStops.
/**
* Tests that if user/client cancels a product retrieval that is in progress and
* actively being cached, and the admin has not configured caching to continue,
* that the (partially) cached file is deleted and not placed in the cache map.
*
* @throws Exception
*/
@Test
@Ignore
public void testClientCancelProductDownloadCachingStops() throws Exception {
mis = new MockInputStream(productInputFilename, true);
mis.setReadDelay(MONITOR_PERIOD - 2, TimeUnit.MILLISECONDS);
Metacard metacard = getMockMetacard(EXPECTED_METACARD_ID, EXPECTED_METACARD_SOURCE_ID);
resourceResponse = getMockResourceResponse();
// Need the product InputStream (MockInputStream) to read slower so that client has time to
// start reading from the ReliableResourceInputStream and close it, simulating a cancel of
// the product download
ResourceRetriever retriever = getMockResourceRetrieverWithRetryCapability(RetryType.CLIENT_CANCELS_DOWNLOAD, true);
int chunkSize = 50;
startDownload(true, chunkSize, false, metacard, retriever);
// On second read of ReliableResourceInputStream, client will close the stream simulating a cancel
// of the product download
clientRead(chunkSize, productInputStream, 2);
// Verify product was not cached, i.e., its pending caching entry was removed
String cacheKey = new CacheKey(metacard, resourceResponse.getRequest()).generateKey();
verify(resourceCache, timeout(3000)).removePendingCacheEntry(cacheKey);
cleanup();
}
use of ddf.catalog.cache.impl.CacheKey in project ddf by codice.
the class ReliableResourceDownloadManagerTest method testRetryAttemptsExhaustedDuringProductDownload.
/**
* Tests that if network connection drops repeatedly during a product retrieval such that all
* of the retry attempts are used up before entire product is downloaded, then the (partially) cached file is deleted and not
* placed in the cache map, and product download fails.
*
* @throws Exception
*/
@Test
@Ignore
public void testRetryAttemptsExhaustedDuringProductDownload() throws Exception {
mis = new MockInputStream(productInputFilename);
Metacard metacard = getMockMetacard(EXPECTED_METACARD_ID, EXPECTED_METACARD_SOURCE_ID);
resourceResponse = getMockResourceResponse();
ResourceRetriever retriever = getMockResourceRetrieverWithRetryCapability(RetryType.NETWORK_CONNECTION_UP_AND_DOWN);
int chunkSize = 50;
startDownload(true, chunkSize, false, metacard, retriever);
ByteArrayOutputStream clientBytesRead = clientRead(chunkSize, productInputStream);
// Verify client did not receive entire product download
assertTrue(clientBytesRead.size() < expectedFileSize);
// Verify product was not cached, i.e., its pending caching entry was removed
String cacheKey = new CacheKey(metacard, resourceResponse.getRequest()).generateKey();
verify(resourceCache, timeout(3000)).removePendingCacheEntry(cacheKey);
cleanup();
}
use of ddf.catalog.cache.impl.CacheKey in project ddf by codice.
the class ReliableResourceDownloadManagerTest method testNetworkConnectionDroppedDuringProductDownload.
/**
* Tests that if network connection dropped during a product retrieval that is in progress and
* actively being cached, that the (partially) cached file is deleted and not
* placed in the cache map.
*
* @throws Exception
*/
@Test
@Ignore
public void testNetworkConnectionDroppedDuringProductDownload() throws Exception {
mis = new MockInputStream(productInputFilename);
Metacard metacard = getMockMetacard(EXPECTED_METACARD_ID, EXPECTED_METACARD_SOURCE_ID);
resourceResponse = getMockResourceResponse();
ResourceRetriever retriever = getMockResourceRetrieverWithRetryCapability(RetryType.NETWORK_CONNECTION_DROPPED);
int chunkSize = 50;
startDownload(true, chunkSize, false, metacard, retriever);
ByteArrayOutputStream clientBytesRead = clientRead(chunkSize, productInputStream);
// Verify client did not receive entire product download
assertTrue(clientBytesRead.size() < expectedFileSize);
// Verify product was not cached, i.e., its pending caching entry was removed
String cacheKey = new CacheKey(metacard, resourceResponse.getRequest()).generateKey();
verify(resourceCache, timeout(3000)).removePendingCacheEntry(cacheKey);
cleanup();
}
Aggregations