Search in sources :

Example 21 with Resource

use of ddf.catalog.resource.Resource in project ddf by codice.

the class DumpCommand method getAllMetacardContent.

private Map<String, Resource> getAllMetacardContent(Metacard metacard) {
    Attribute attribute = metacard.getAttribute(Metacard.DERIVED_RESOURCE_URI);
    Map<String, Resource> resourceMap = getResourceMap(metacard, attribute);
    URI resourceUri = metacard.getResourceURI();
    Resource resource = getResource(metacard);
    if (resource != null) {
        resourceMap.put(CONTENT + File.separator + resourceUri.getSchemeSpecificPart() + "-" + resource.getName(), resource);
    }
    return resourceMap;
}
Also used : Attribute(ddf.catalog.data.Attribute) Resource(ddf.catalog.resource.Resource) URI(java.net.URI)

Example 22 with Resource

use of ddf.catalog.resource.Resource in project ddf by codice.

the class ResourceCacheImplTest method getDefaultResourceInCache.

@Test
public void getDefaultResourceInCache() {
    ReliableResource cachedResource = createCachedResource(cachedMetacard);
    resourceCache.put(cachedResource);
    Optional<Resource> optionalResource = newResourceCache.get(cachedMetacard);
    assertFalse("cache should be noop", optionalResource.isPresent());
}
Also used : ReliableResource(ddf.catalog.resource.data.ReliableResource) Resource(ddf.catalog.resource.Resource) ReliableResource(ddf.catalog.resource.data.ReliableResource) Test(org.junit.Test)

Example 23 with Resource

use of ddf.catalog.resource.Resource in project ddf by codice.

the class ResourceCacheImplTest method getSpecificResourceNotInCache.

@Test
public void getSpecificResourceNotInCache() {
    Optional<Resource> optionalResource = newResourceCache.get(notCachedMetacard, new ResourceRequestById(NOT_CACHED_METACARD_ID));
    assertFalse(optionalResource.isPresent());
}
Also used : ReliableResource(ddf.catalog.resource.data.ReliableResource) Resource(ddf.catalog.resource.Resource) ResourceRequestById(ddf.catalog.operation.impl.ResourceRequestById) Test(org.junit.Test)

Example 24 with Resource

use of ddf.catalog.resource.Resource 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
 */
@Override
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) DownloadException(org.codice.ddf.catalog.resource.download.DownloadException) 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 25 with Resource

use of ddf.catalog.resource.Resource in project ddf by codice.

the class CswSourceTest method testRetrieveResourceUsingGetRecordByIdWithNoId.

@Test(expected = ResourceNotFoundException.class)
public void testRetrieveResourceUsingGetRecordByIdWithNoId() throws CswException, ResourceNotFoundException, IOException, ResourceNotSupportedException, URISyntaxException {
    Csw csw = createMockCsw();
    CswRecordCollection collection = mock(CswRecordCollection.class);
    Resource resource = mock(Resource.class);
    when(collection.getResource()).thenReturn(resource);
    when(csw.getRecordById(any(GetRecordByIdRequest.class), anyString())).thenReturn(collection);
    AbstractCswSource cswSource = getCswSource(csw, mockContext, null, null, null, null, permissions);
    ResourceReader reader = mock(ResourceReader.class);
    when(reader.retrieveResource(any(URI.class), any(Map.class))).thenReturn(mock(ResourceResponse.class));
    cswSource.setResourceReader(reader);
    Map<String, Serializable> props = new HashMap<>();
    cswSource.retrieveResource(new URI("http://example.com/resource"), props);
}
Also used : ResourceReader(ddf.catalog.resource.ResourceReader) Serializable(java.io.Serializable) HashMap(java.util.HashMap) Csw(org.codice.ddf.spatial.ogc.csw.catalog.common.Csw) Resource(ddf.catalog.resource.Resource) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) GetRecordByIdRequest(org.codice.ddf.spatial.ogc.csw.catalog.common.GetRecordByIdRequest) URI(java.net.URI) ResourceResponse(ddf.catalog.operation.ResourceResponse) CswRecordCollection(org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection) Map(java.util.Map) ArgumentMatchers.anyMap(org.mockito.ArgumentMatchers.anyMap) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) Test(org.junit.Test)

Aggregations

Resource (ddf.catalog.resource.Resource)59 ResourceResponse (ddf.catalog.operation.ResourceResponse)29 Test (org.junit.Test)21 ByteArrayInputStream (java.io.ByteArrayInputStream)16 ResourceRequest (ddf.catalog.operation.ResourceRequest)14 CswRecordCollection (org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection)13 URI (java.net.URI)11 HashMap (java.util.HashMap)11 InputStream (java.io.InputStream)10 Serializable (java.io.Serializable)10 MimeType (javax.activation.MimeType)10 Result (ddf.catalog.data.Result)9 ResourceRequestById (ddf.catalog.operation.impl.ResourceRequestById)9 ResourceImpl (ddf.catalog.resource.impl.ResourceImpl)9 IOException (java.io.IOException)9 Map (java.util.Map)9 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)9 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)8 ResourceResponseImpl (ddf.catalog.operation.impl.ResourceResponseImpl)8 Metacard (ddf.catalog.data.Metacard)7