use of ddf.catalog.resource.ResourceReader in project ddf by codice.
the class DownloadsStatusEventListenerTest method testGetDownloadStatus.
@Test
public void testGetDownloadStatus() throws URISyntaxException, DownloadException, InterruptedException, IOException {
File downloadSrcFile = new File(this.getClass().getResource("/125bytes.txt").toURI());
File downloadFile = prepareDownloadFile(downloadSrcFile);
MetacardImpl testMetacard = new MetacardImpl();
testMetacard.setId("easyas123");
testMetacard.setResourceURI(downloadFile.toURI());
testMetacard.setResourceSize("125");
ClientBuilderFactory clientBuilderFactory = new ClientBuilderFactoryImpl();
URLResourceReader testURLResourceReader = new URLResourceReader(clientBuilderFactory);
testURLResourceReader.setRootResourceDirectories(new HashSet<String>(Arrays.asList(localResourcePath.toString())));
List<ResourceReader> testResourceReaderList = Collections.singletonList((ResourceReader) testURLResourceReader);
Map<String, Serializable> tmpMap = Collections.emptyMap();
Map<String, Integer> idToBytes = new HashMap<String, Integer>();
testGetDownloadStatusHelper(null, null, null);
testDownloadManager.download(mock(ResourceRequest.class), testMetacard, new LocalResourceRetriever(testResourceReaderList, testMetacard.getResourceURI(), null, tmpMap));
TimeUnit.SECONDS.sleep(2);
testGetDownloadStatusHelper(idToBytes, DownloadManagerState.DownloadState.COMPLETED.name(), downloadFile.getName());
}
use of ddf.catalog.resource.ResourceReader 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);
}
use of ddf.catalog.resource.ResourceReader in project ddf by codice.
the class CswSourceTest method testRetrieveResourceUsingReader.
@Test
public void testRetrieveResourceUsingReader() throws URISyntaxException, ResourceNotFoundException, IOException, ResourceNotSupportedException, CswException {
configureMockCsw();
AbstractCswSource cswSource = getCswSource(mockCsw, 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<>();
props.put(Core.ID, "ID");
cswSource.retrieveResource(new URI("http://example.com/resource"), props);
// Verify
verify(reader, times(1)).retrieveResource(any(URI.class), anyMap());
}
use of ddf.catalog.resource.ResourceReader in project ddf by codice.
the class CswSourceTest method testRetrieveResourceUsingReaderBasicAuth.
@Test
public void testRetrieveResourceUsingReaderBasicAuth() throws URISyntaxException, ResourceNotFoundException, IOException, ResourceNotSupportedException, CswException {
configureMockCsw();
AbstractCswSource cswSource = getCswSource(mockCsw, 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);
cswSource.setUsername("user");
cswSource.setPassword("secret");
Map<String, Serializable> props = new HashMap<>();
props.put(Core.ID, "ID");
cswSource.retrieveResource(new URI("http://example.com/resource"), props);
verify(reader, times(1)).retrieveResource(any(URI.class), argThat(allOf(hasEntry("username", (Serializable) "user"), hasEntry("password", (Serializable) "secret"))));
}
use of ddf.catalog.resource.ResourceReader in project ddf by codice.
the class LocalResourceRetriever method retrieveResource.
@Override
public ResourceResponse retrieveResource(long bytesToSkip) throws ResourceNotFoundException {
final String methodName = "retrieveResource";
LOGGER.trace("ENTERING: {}", methodName);
ResourceResponse resource = null;
if (resourceUri == null) {
throw new ResourceNotFoundException("Unable to find resource due to null URI");
}
Map<String, Serializable> props = new HashMap<>(properties);
if (bytesToSkip > 0) {
props.put(BYTES_TO_SKIP, bytesToSkip);
}
URI derivedUri = null;
Serializable serializable = props.get(ContentItem.QUALIFIER_KEYWORD);
if (serializable != null && serializable instanceof String) {
LOGGER.debug("Received qualifier in request properties, looking for qualified content on metacard with id [{}]", resourceMetacard.getId());
String fragment = (String) serializable;
derivedUri = getDerivedUriWithFragment(resourceMetacard, fragment);
}
String scheme;
URI resourceRetrievalUri;
if (derivedUri == null) {
scheme = resourceUri.getScheme();
resourceRetrievalUri = resourceUri;
} else {
scheme = derivedUri.getScheme();
resourceRetrievalUri = derivedUri;
}
for (ResourceReader reader : resourceReaders) {
if (reader != null && reader.getSupportedSchemes().contains(scheme)) {
try {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Found an acceptable resource reader ({}) for URI {}", reader.getId(), resourceRetrievalUri.toASCIIString());
}
resource = reader.retrieveResource(resourceRetrievalUri, props);
if (resource != null) {
break;
} else {
LOGGER.debug("Resource returned from ResourceReader {} was null. Checking other readers for URI: {}", reader.getId(), resourceRetrievalUri);
}
} catch (ResourceNotFoundException | ResourceNotSupportedException | IOException e) {
LOGGER.debug("Product not found using resource reader with name {}", reader.getId(), e);
}
}
}
if (resource == null) {
throw new ResourceNotFoundException("Resource Readers could not find resource (or returned null resource) for URI: " + resourceRetrievalUri.toASCIIString() + ". Scheme: " + resourceRetrievalUri.getScheme());
}
LOGGER.debug("Received resource, sending back: {}", resource.getResource().getName());
LOGGER.trace("EXITING: {}", methodName);
return resource;
}
Aggregations