use of ddf.catalog.resource.Resource in project ddf by codice.
the class DumpCommand method getResource.
private Resource getResource(Metacard metacard) {
Resource resource = null;
try {
ResourceRequest resourceRequest = new ResourceRequestById(metacard.getId());
ResourceResponse resourceResponse = catalogFramework.getLocalResource(resourceRequest);
resource = resourceResponse.getResource();
} catch (IOException | ResourceNotFoundException | ResourceNotSupportedException e) {
LOGGER.debug("Unable to retrieve content from metacard : {}", metacard.getId(), e);
}
return resource;
}
use of ddf.catalog.resource.Resource in project ddf by codice.
the class ResourceOperationsTest method testGetResourceThrowsDownloadException.
@Test(expected = ResourceNotFoundException.class)
public void testGetResourceThrowsDownloadException() throws Exception {
ArrayList<Result> mockResultList = new ArrayList<>();
mockResultList.add(resultMock);
setGetResourceMocks();
when(queryResponseMock.getResults()).thenReturn(mockResultList);
when(queryResponseMock.getResults().get(0).getMetacard()).thenReturn(metacard);
when(reliableResourceDownloadManagerMock.download(any(ResourceRequest.class), any(MetacardImpl.class), isNull())).thenThrow(DownloadException.class);
Resource resourceMock = mock(Resource.class);
when(resourceResponseMock.getResource()).thenReturn(resourceMock);
frameworkProperties.setReliableResourceDownloadManager(reliableResourceDownloadManagerMock);
resourceOperations.getResource(resourceRequestMock, isEnterprise, resourceName, fanoutEnabled);
verify(queryResponseMock).getResults();
verify(reliableResourceDownloadManagerMock).download(any(ResourceRequest.class), any(MetacardImpl.class), isNull());
}
use of ddf.catalog.resource.Resource in project ddf by codice.
the class ResourceOperationsTest method testValidateFixGetResourceReturnsResourceResponse.
@Test
public void testValidateFixGetResourceReturnsResourceResponse() throws Exception {
ArrayList<Result> mockResultList = new ArrayList<>();
mockResultList.add(resultMock);
setGetResourceMocks();
when(queryResponseMock.getResults()).thenReturn(mockResultList);
when(queryResponseMock.getResults().get(0).getMetacard()).thenReturn(metacard);
when(reliableResourceDownloadManagerMock.download(any(ResourceRequest.class), any(MetacardImpl.class), isNull())).thenReturn(resourceResponseMock);
Resource resourceMock = mock(Resource.class);
when(resourceResponseMock.getResource()).thenReturn(resourceMock);
frameworkProperties.setReliableResourceDownloadManager(reliableResourceDownloadManagerMock);
resourceOperations.getResource(resourceRequestMock, isEnterprise, resourceName, fanoutEnabled);
}
use of ddf.catalog.resource.Resource in project ddf by codice.
the class URLResourceReaderTest method verifyFile.
private void verifyFile(String filePath, String filename, String expectedMimeType, String... rootResourceDirectories) throws Exception {
URLResourceReader resourceReader = new URLResourceReader(mimeTypeMapper, clientBuilderFactory);
resourceReader.setRootResourceDirectories(new HashSet<String>(Arrays.asList(rootResourceDirectories)));
HashMap<String, Serializable> arguments = new HashMap<String, Serializable>();
LOGGER.info("Getting resource: {}", filePath);
// Test using the URL ResourceReader
File file = new File(filePath);
URI uri = file.toURI();
LOGGER.info("URI: {}", uri.toString());
ResourceResponse resourceResponse = resourceReader.retrieveResource(uri, arguments);
Resource resource = resourceResponse.getResource();
assert (resource != null);
LOGGER.info("MimeType: {}", resource.getMimeType());
LOGGER.info("Got resource: {}", resource.getName());
String name = resource.getName();
assertNotNull(name);
assertThat(name, is(filename));
assertThat(resource.getMimeType().toString(), containsString(expectedMimeType));
}
use of ddf.catalog.resource.Resource in project ddf by codice.
the class ReliableResourceDownloader method setupDownload.
public ResourceResponse setupDownload(Metacard metacard, DownloadStatusInfo downloadStatusInfo) {
Resource resource = resourceResponse.getResource();
MimeType mimeType = resource.getMimeType();
String resourceName = resource.getName();
fbos = new FileBackedOutputStream(DEFAULT_FILE_BACKED_OUTPUT_STREAM_THRESHOLD);
countingFbos = new CountingOutputStream(fbos);
streamReadByClient = new ReliableResourceInputStream(fbos, countingFbos, downloadState, downloadIdentifier, resourceResponse);
this.metacard = metacard;
// Create new ResourceResponse to return that will encapsulate the
// ReliableResourceInputStream that will be read by the client simultaneously as the product
// is cached to disk (if caching is enabled)
ResourceImpl newResource = new ResourceImpl(streamReadByClient, mimeType, resourceName);
resourceResponse = new ResourceResponseImpl(resourceResponse.getRequest(), resourceResponse.getProperties(), newResource);
// Get handle to retrieved product's InputStream
resourceInputStream = resource.getInputStream();
eventListener.setDownloadMap(downloadIdentifier, resourceResponse);
downloadStatusInfo.addDownloadInfo(downloadIdentifier, this, resourceResponse);
if (downloaderConfig.isCacheEnabled()) {
CacheKey keyMaker = null;
String key = null;
try {
keyMaker = new CacheKey(metacard, resourceResponse.getRequest());
key = keyMaker.generateKey();
} catch (IllegalArgumentException e) {
LOGGER.info("Cannot create cache key for resource with metacard ID = {}", metacard.getId());
return resourceResponse;
}
if (!resourceCache.isPending(key)) {
// Fully qualified path to cache file that will be written to.
// Example:
// <INSTALL-DIR>/data/product-cache/<source-id>-<metacard-id>
// <INSTALL-DIR>/data/product-cache/ddf.distribution-abc123
filePath = FilenameUtils.concat(resourceCache.getProductCacheDirectory(), key);
if (filePath == null) {
LOGGER.info("Unable to create cache for cache directory {} and key {} - no caching will be done.", resourceCache.getProductCacheDirectory(), key);
return resourceResponse;
}
reliableResource = new ReliableResource(key, filePath, mimeType, resourceName, metacard);
resourceCache.addPendingCacheEntry(reliableResource);
try {
fos = FileUtils.openOutputStream(new File(filePath));
doCaching = true;
this.downloadState.setCacheEnabled(true);
} catch (IOException e) {
LOGGER.info("Unable to open cache file {} - no caching will be done.", filePath);
}
} else {
LOGGER.debug("Cache key {} is already pending caching", key);
}
}
return resourceResponse;
}
Aggregations