Search in sources :

Example 6 with ResourceReader

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());
}
Also used : URLResourceReader(ddf.catalog.resource.impl.URLResourceReader) ResourceReader(ddf.catalog.resource.ResourceReader) Serializable(java.io.Serializable) HashMap(java.util.HashMap) ClientBuilderFactoryImpl(org.codice.ddf.cxf.client.impl.ClientBuilderFactoryImpl) ClientBuilderFactory(org.codice.ddf.cxf.client.ClientBuilderFactory) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) URLResourceReader(ddf.catalog.resource.impl.URLResourceReader) LocalResourceRetriever(ddf.catalog.resourceretriever.LocalResourceRetriever) ResourceRequest(ddf.catalog.operation.ResourceRequest) File(java.io.File) Test(org.junit.Test)

Example 7 with ResourceReader

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

Example 8 with ResourceReader

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());
}
Also used : ResourceReader(ddf.catalog.resource.ResourceReader) Serializable(java.io.Serializable) ResourceResponse(ddf.catalog.operation.ResourceResponse) HashMap(java.util.HashMap) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) URI(java.net.URI) Map(java.util.Map) ArgumentMatchers.anyMap(org.mockito.ArgumentMatchers.anyMap) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 9 with ResourceReader

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"))));
}
Also used : ResourceReader(ddf.catalog.resource.ResourceReader) Serializable(java.io.Serializable) ResourceResponse(ddf.catalog.operation.ResourceResponse) HashMap(java.util.HashMap) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) URI(java.net.URI) Map(java.util.Map) ArgumentMatchers.anyMap(org.mockito.ArgumentMatchers.anyMap) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 10 with ResourceReader

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;
}
Also used : ResourceReader(ddf.catalog.resource.ResourceReader) Serializable(java.io.Serializable) ResourceResponse(ddf.catalog.operation.ResourceResponse) ResourceNotSupportedException(ddf.catalog.resource.ResourceNotSupportedException) HashMap(java.util.HashMap) IOException(java.io.IOException) ResourceNotFoundException(ddf.catalog.resource.ResourceNotFoundException) URI(java.net.URI)

Aggregations

ResourceReader (ddf.catalog.resource.ResourceReader)15 URI (java.net.URI)14 ResourceResponse (ddf.catalog.operation.ResourceResponse)12 HashMap (java.util.HashMap)12 Test (org.junit.Test)12 Serializable (java.io.Serializable)11 Map (java.util.Map)9 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)8 Resource (ddf.catalog.resource.Resource)5 ImmutableMap (com.google.common.collect.ImmutableMap)4 ResourceResponseImpl (ddf.catalog.operation.impl.ResourceResponseImpl)3 ResourceImpl (ddf.catalog.resource.impl.ResourceImpl)3 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)3 Csw (org.codice.ddf.spatial.ogc.csw.catalog.common.Csw)3 CswRecordCollection (org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection)3 GetRecordByIdRequest (org.codice.ddf.spatial.ogc.csw.catalog.common.GetRecordByIdRequest)3 ArgumentMatchers.anyMap (org.mockito.ArgumentMatchers.anyMap)3 Matchers.anyMap (org.mockito.Matchers.anyMap)3 Matchers.anyString (org.mockito.Matchers.anyString)3 Metacard (ddf.catalog.data.Metacard)2