Search in sources :

Example 66 with ResourceResponse

use of ddf.catalog.operation.ResourceResponse in project alliance by codice.

the class CatalogOutputAdapterTest method testGetImageNullInputStream.

@Test(expected = IllegalStateException.class)
public void testGetImageNullInputStream() throws IOException {
    ResourceResponse resourceResponse = mock(ResourceResponse.class);
    Resource resource = mock(Resource.class);
    when(resourceResponse.getResource()).thenReturn(resource);
    when(resource.getInputStream()).thenReturn(null);
    catalogOutputAdapter.getImage(resourceResponse);
}
Also used : ResourceResponse(ddf.catalog.operation.ResourceResponse) Resource(ddf.catalog.resource.Resource) Test(org.junit.Test)

Example 67 with ResourceResponse

use of ddf.catalog.operation.ResourceResponse in project alliance by codice.

the class DAGConverter method getThumbnail.

private byte[] getThumbnail(String thumbnailUrlStr) {
    byte[] thumbnail = null;
    try {
        URI thumbnailURI = new URI(thumbnailUrlStr);
        ResourceResponse resourceResponse = null;
        try {
            resourceResponse = resourceReader.retrieveResource(thumbnailURI, new HashMap<>());
            thumbnail = resourceResponse.getResource().getByteArray();
        } catch (ResourceNotSupportedException e) {
            LOGGER.debug("Resource is not supported: {} ", thumbnailURI, e);
        }
    } catch (IOException | ResourceNotFoundException | URISyntaxException e) {
        LOGGER.debug("Unable to get thumbnail from URL {}", thumbnailUrlStr, e);
    }
    return thumbnail;
}
Also used : ResourceResponse(ddf.catalog.operation.ResourceResponse) ResourceNotSupportedException(ddf.catalog.resource.ResourceNotSupportedException) HashMap(java.util.HashMap) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) ResourceNotFoundException(ddf.catalog.resource.ResourceNotFoundException) URI(java.net.URI)

Example 68 with ResourceResponse

use of ddf.catalog.operation.ResourceResponse in project ddf by codice.

the class SeedCommandTest method mockResourceResponse.

private ResourceResponse mockResourceResponse() throws IOException {
    InputStream mockInputStream = mock(InputStream.class);
    doReturn(-1).when(mockInputStream).read(any(byte[].class), anyInt(), anyInt());
    Resource mockResource = mock(Resource.class);
    when(mockResource.getInputStream()).thenReturn(mockInputStream);
    ResourceResponse mockResponse = mock(ResourceResponse.class);
    when(mockResponse.getResource()).thenReturn(mockResource);
    return mockResponse;
}
Also used : ResourceResponse(ddf.catalog.operation.ResourceResponse) InputStream(java.io.InputStream) Resource(ddf.catalog.resource.Resource)

Example 69 with ResourceResponse

use of ddf.catalog.operation.ResourceResponse in project ddf by codice.

the class ExportCommand method doContentExport.

@SuppressWarnings("squid:S3776")
private List<ExportItem> doContentExport(ZipOutputStream zipOutputStream, List<ExportItem> exportedItems) {
    List<ExportItem> contentItemsToExport = exportedItems.stream().filter(ei -> ei.getResourceUri() != null).filter(ei -> ei.getResourceUri().getScheme() != null).filter(ei -> ei.getResourceUri().getScheme().startsWith(ContentItem.CONTENT_SCHEME)).filter(ei -> !ei.getMetacardTag().equals(DELETED_METACARD)).filter(ei -> !ei.getMetacardTag().equals(REVISION_METACARD) || ei.getResourceUri().getSchemeSpecificPart().equals(ei.getId())).filter(distinctByKey(ei -> ei.getResourceUri().getSchemeSpecificPart())).collect(Collectors.toList());
    List<ExportItem> exportedContentItems = new ArrayList<>();
    for (ExportItem contentItem : contentItemsToExport) {
        ResourceResponse resource;
        try {
            resource = catalogFramework.getLocalResource(new ResourceRequestByProductUri(contentItem.getResourceUri()));
        } catch (IOException | ResourceNotSupportedException e) {
            throw new CatalogCommandRuntimeException("Unable to retrieve resource for " + contentItem.getId(), e);
        } catch (ResourceNotFoundException e) {
            continue;
        }
        writeResourceToZip(zipOutputStream, contentItem, resource);
        exportedContentItems.add(contentItem);
        if (!contentItem.getMetacardTag().equals(REVISION_METACARD)) {
            for (String derivedUri : contentItem.getDerivedUris()) {
                URI uri;
                try {
                    uri = new URI(derivedUri);
                } catch (URISyntaxException e) {
                    LOGGER.debug("Uri [{}] is not a valid URI. Derived content will not be included in export", derivedUri);
                    continue;
                }
                ResourceResponse derivedResource;
                try {
                    derivedResource = catalogFramework.getLocalResource(new ResourceRequestByProductUri(uri));
                } catch (IOException e) {
                    throw new CatalogCommandRuntimeException("Unable to retrieve resource for " + contentItem.getId(), e);
                } catch (ResourceNotFoundException | ResourceNotSupportedException e) {
                    LOGGER.warn("Could not retreive resource [{}]", uri, e);
                    console.printf("%sUnable to retrieve resource for export : %s%s%n", Ansi.ansi().fg(Ansi.Color.RED).toString(), uri, Ansi.ansi().reset().toString());
                    continue;
                }
                writeResourceToZip(zipOutputStream, contentItem, derivedResource);
            }
        }
    }
    return exportedContentItems;
}
Also used : Ansi(org.fusesource.jansi.Ansi) StringUtils(org.apache.commons.lang.StringUtils) DeleteStorageRequestImpl(ddf.catalog.content.operation.impl.DeleteStorageRequestImpl) BinaryContent(ddf.catalog.data.BinaryContent) URISyntaxException(java.net.URISyntaxException) LoggerFactory(org.slf4j.LoggerFactory) Command(org.apache.karaf.shell.api.action.Command) MetacardTransformer(ddf.catalog.transform.MetacardTransformer) MetacardVersion(ddf.catalog.core.versioning.MetacardVersion) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) ExportItem(org.codice.ddf.commands.catalog.export.ExportItem) DeleteRequestImpl(ddf.catalog.operation.impl.DeleteRequestImpl) URI(java.net.URI) ZoneOffset(java.time.ZoneOffset) ParseException(java.text.ParseException) ZipEntry(java.util.zip.ZipEntry) Predicate(java.util.function.Predicate) DeletedMetacard(ddf.catalog.core.versioning.DeletedMetacard) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) PrivilegedAction(java.security.PrivilegedAction) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) FileNotFoundException(java.io.FileNotFoundException) BundleContext(org.osgi.framework.BundleContext) ResourceNotFoundException(ddf.catalog.resource.ResourceNotFoundException) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) Objects(java.util.Objects) StorageException(ddf.catalog.content.StorageException) IOUtils(org.apache.commons.io.IOUtils) List(java.util.List) ResultIterable.resultIterable(ddf.catalog.util.impl.ResultIterable.resultIterable) AccessController(java.security.AccessController) FilenameUtils(org.apache.commons.io.FilenameUtils) ResourceResponse(ddf.catalog.operation.ResourceResponse) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) ZipOutputStream(java.util.zip.ZipOutputStream) FilterBuilder(ddf.catalog.filter.FilterBuilder) CatalogFramework(ddf.catalog.CatalogFramework) LocalDateTime(java.time.LocalDateTime) CatalogCommandRuntimeException(org.codice.ddf.commands.util.CatalogCommandRuntimeException) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Reference(org.apache.karaf.shell.api.action.lifecycle.Reference) ContentItem(ddf.catalog.content.data.ContentItem) Metacard(ddf.catalog.data.Metacard) CQLException(org.geotools.filter.text.cql2.CQLException) StorageProvider(ddf.catalog.content.StorageProvider) QueryRequest(ddf.catalog.operation.QueryRequest) Result(ddf.catalog.data.Result) QueryImpl(ddf.catalog.operation.impl.QueryImpl) Logger(org.slf4j.Logger) SystemBaseUrl(org.codice.ddf.configuration.SystemBaseUrl) SecurityLogger(ddf.security.audit.SecurityLogger) IngestException(ddf.catalog.source.IngestException) ResourceRequestByProductUri(ddf.catalog.operation.impl.ResourceRequestByProductUri) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) FileInputStream(java.io.FileInputStream) IdAndUriMetacard(org.codice.ddf.commands.catalog.export.IdAndUriMetacard) File(java.io.File) ResourceNotSupportedException(ddf.catalog.resource.ResourceNotSupportedException) Paths(java.nio.file.Paths) DateTimeFormatter(java.time.format.DateTimeFormatter) Service(org.apache.karaf.shell.api.action.lifecycle.Service) Filter(org.opengis.filter.Filter) Option(org.apache.karaf.shell.api.action.Option) Collections(java.util.Collections) InputStream(java.io.InputStream) DigitalSignature(org.codice.ddf.commands.util.DigitalSignature) ExportItem(org.codice.ddf.commands.catalog.export.ExportItem) ArrayList(java.util.ArrayList) CatalogCommandRuntimeException(org.codice.ddf.commands.util.CatalogCommandRuntimeException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) ResourceRequestByProductUri(ddf.catalog.operation.impl.ResourceRequestByProductUri) URI(java.net.URI) ResourceResponse(ddf.catalog.operation.ResourceResponse) ResourceNotSupportedException(ddf.catalog.resource.ResourceNotSupportedException) ResourceNotFoundException(ddf.catalog.resource.ResourceNotFoundException)

Example 70 with ResourceResponse

use of ddf.catalog.operation.ResourceResponse 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)

Aggregations

ResourceResponse (ddf.catalog.operation.ResourceResponse)94 Test (org.junit.Test)49 URI (java.net.URI)30 HashMap (java.util.HashMap)28 Serializable (java.io.Serializable)26 Resource (ddf.catalog.resource.Resource)25 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)23 ResourceRequest (ddf.catalog.operation.ResourceRequest)18 IOException (java.io.IOException)18 ResourceNotFoundException (ddf.catalog.resource.ResourceNotFoundException)17 Metacard (ddf.catalog.data.Metacard)16 MimeType (javax.activation.MimeType)16 Response (javax.ws.rs.core.Response)15 ResourceNotSupportedException (ddf.catalog.resource.ResourceNotSupportedException)14 URLResourceReader (ddf.catalog.resource.impl.URLResourceReader)14 ResourceRequestById (ddf.catalog.operation.impl.ResourceRequestById)10 InputStream (java.io.InputStream)9 Map (java.util.Map)9 ResourceResponseImpl (ddf.catalog.operation.impl.ResourceResponseImpl)8 StringWriter (java.io.StringWriter)8