Search in sources :

Example 41 with Resource

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;
}
Also used : ResourceResponse(ddf.catalog.operation.ResourceResponse) ResourceNotSupportedException(ddf.catalog.resource.ResourceNotSupportedException) Resource(ddf.catalog.resource.Resource) ResourceRequestById(ddf.catalog.operation.impl.ResourceRequestById) ResourceRequest(ddf.catalog.operation.ResourceRequest) IOException(java.io.IOException) ResourceNotFoundException(ddf.catalog.resource.ResourceNotFoundException)

Example 42 with 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());
}
Also used : ArrayList(java.util.ArrayList) Resource(ddf.catalog.resource.Resource) ResourceRequest(ddf.catalog.operation.ResourceRequest) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) Result(ddf.catalog.data.Result) Test(org.junit.Test)

Example 43 with Resource

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);
}
Also used : ArrayList(java.util.ArrayList) Resource(ddf.catalog.resource.Resource) ResourceRequest(ddf.catalog.operation.ResourceRequest) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) Result(ddf.catalog.data.Result) Test(org.junit.Test)

Example 44 with Resource

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));
}
Also used : Serializable(java.io.Serializable) ResourceResponse(ddf.catalog.operation.ResourceResponse) HashMap(java.util.HashMap) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) Resource(ddf.catalog.resource.Resource) Matchers.containsString(org.hamcrest.Matchers.containsString) File(java.io.File) URI(java.net.URI)

Example 45 with Resource

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;
}
Also used : ReliableResource(ddf.catalog.resource.data.ReliableResource) Resource(ddf.catalog.resource.Resource) ResourceResponseImpl(ddf.catalog.operation.impl.ResourceResponseImpl) IOException(java.io.IOException) MimeType(javax.activation.MimeType) ReliableResource(ddf.catalog.resource.data.ReliableResource) CountingOutputStream(com.google.common.io.CountingOutputStream) ResourceImpl(ddf.catalog.resource.impl.ResourceImpl) FileBackedOutputStream(com.google.common.io.FileBackedOutputStream) File(java.io.File) CacheKey(ddf.catalog.cache.impl.CacheKey)

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