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;
}
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());
}
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());
}
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;
}
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);
}
Aggregations