Search in sources :

Example 6 with CacheKey

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;
}
Also used : ResourceResponse(ddf.catalog.operation.ResourceResponse) ResourceNotSupportedException(ddf.catalog.resource.ResourceNotSupportedException) Resource(ddf.catalog.resource.Resource) ResourceResponseImpl(ddf.catalog.operation.impl.ResourceResponseImpl) IOException(java.io.IOException) ResourceNotFoundException(ddf.catalog.resource.ResourceNotFoundException) CacheKey(ddf.catalog.cache.impl.CacheKey)

Example 7 with CacheKey

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)));
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) CacheKey(ddf.catalog.cache.impl.CacheKey) Test(org.junit.Test)

Example 8 with CacheKey

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();
}
Also used : MockInputStream(ddf.catalog.cache.MockInputStream) Metacard(ddf.catalog.data.Metacard) ResourceRetriever(ddf.catalog.resourceretriever.ResourceRetriever) CacheKey(ddf.catalog.cache.impl.CacheKey) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 9 with CacheKey

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();
}
Also used : MockInputStream(ddf.catalog.cache.MockInputStream) Metacard(ddf.catalog.data.Metacard) ResourceRetriever(ddf.catalog.resourceretriever.ResourceRetriever) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CacheKey(ddf.catalog.cache.impl.CacheKey) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 10 with CacheKey

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();
}
Also used : MockInputStream(ddf.catalog.cache.MockInputStream) Metacard(ddf.catalog.data.Metacard) ResourceRetriever(ddf.catalog.resourceretriever.ResourceRetriever) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CacheKey(ddf.catalog.cache.impl.CacheKey) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

CacheKey (ddf.catalog.cache.impl.CacheKey)20 Test (org.junit.Test)17 Metacard (ddf.catalog.data.Metacard)7 MockInputStream (ddf.catalog.cache.MockInputStream)6 ResourceRetriever (ddf.catalog.resourceretriever.ResourceRetriever)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 ReliableResource (ddf.catalog.resource.data.ReliableResource)4 Ignore (org.junit.Ignore)4 Serializable (java.io.Serializable)3 HashMap (java.util.HashMap)3 ResourceResponseImpl (ddf.catalog.operation.impl.ResourceResponseImpl)2 Resource (ddf.catalog.resource.Resource)2 IOException (java.io.IOException)2 CountingOutputStream (com.google.common.io.CountingOutputStream)1 FileBackedOutputStream (com.google.common.io.FileBackedOutputStream)1 Attribute (ddf.catalog.data.Attribute)1 Result (ddf.catalog.data.Result)1 AttributeImpl (ddf.catalog.data.impl.AttributeImpl)1 ResourceRequest (ddf.catalog.operation.ResourceRequest)1 ResourceResponse (ddf.catalog.operation.ResourceResponse)1