Search in sources :

Example 1 with ResourceRequestByProductUri

use of ddf.catalog.operation.impl.ResourceRequestByProductUri in project ddf by codice.

the class ExportCommand method doContentExport.

private List<ExportItem> doContentExport(/*Mutable,IO*/
ZipFile zipFile, List<ExportItem> exportedItems) throws ZipException {
    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")).filter(ei -> !ei.getMetacardTag().equals("revision") || 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;
        }
        writeToZip(zipFile, contentItem, resource);
        exportedContentItems.add(contentItem);
        if (!contentItem.getMetacardTag().equals("revision")) {
            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;
                }
                writeToZip(zipFile, contentItem, derivedResource);
            }
        }
    }
    return exportedContentItems;
}
Also used : Ansi(org.fusesource.jansi.Ansi) StringUtils(org.apache.commons.lang.StringUtils) DeleteStorageRequestImpl(ddf.catalog.content.operation.impl.DeleteStorageRequestImpl) Date(java.util.Date) URISyntaxException(java.net.URISyntaxException) BinaryContent(ddf.catalog.data.BinaryContent) LoggerFactory(org.slf4j.LoggerFactory) SecurityLogger(ddf.security.common.audit.SecurityLogger) Command(org.apache.karaf.shell.api.action.Command) MetacardTransformer(ddf.catalog.transform.MetacardTransformer) MetacardVersion(ddf.catalog.core.versioning.MetacardVersion) Map(java.util.Map) ExportItem(org.codice.ddf.commands.catalog.export.ExportItem) DeleteRequestImpl(ddf.catalog.operation.impl.DeleteRequestImpl) URI(java.net.URI) ParseException(java.text.ParseException) ZipFile(net.lingala.zip4j.core.ZipFile) TimeZone(java.util.TimeZone) Predicate(java.util.function.Predicate) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) DeletedMetacard(ddf.catalog.core.versioning.DeletedMetacard) Set(java.util.Set) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) ResourceNotFoundException(ddf.catalog.resource.ResourceNotFoundException) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) Objects(java.util.Objects) ZipException(net.lingala.zip4j.exception.ZipException) StorageException(ddf.catalog.content.StorageException) List(java.util.List) FilenameUtils(org.apache.commons.io.FilenameUtils) ResourceResponse(ddf.catalog.operation.ResourceResponse) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) SimpleDateFormat(java.text.SimpleDateFormat) CatalogCommandRuntimeException(org.codice.ddf.commands.util.CatalogCommandRuntimeException) HashMap(java.util.HashMap) Function(java.util.function.Function) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Reference(org.apache.karaf.shell.api.action.lifecycle.Reference) SortBy(org.opengis.filter.sort.SortBy) ContentItem(ddf.catalog.content.data.ContentItem) Metacard(ddf.catalog.data.Metacard) CQLException(org.geotools.filter.text.cql2.CQLException) StorageProvider(ddf.catalog.content.StorageProvider) Result(ddf.catalog.data.Result) QueryImpl(ddf.catalog.operation.impl.QueryImpl) Logger(org.slf4j.Logger) ZipParameters(net.lingala.zip4j.model.ZipParameters) QueryResultIterable(org.codice.ddf.commands.util.QueryResultIterable) IngestException(ddf.catalog.source.IngestException) ResourceRequestByProductUri(ddf.catalog.operation.impl.ResourceRequestByProductUri) IOException(java.io.IOException) IdAndUriMetacard(org.codice.ddf.commands.catalog.export.IdAndUriMetacard) File(java.io.File) JarSigner(org.codice.ddf.catalog.transformer.zip.JarSigner) TimeUnit(java.util.concurrent.TimeUnit) ResourceNotSupportedException(ddf.catalog.resource.ResourceNotSupportedException) Paths(java.nio.file.Paths) 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) 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 2 with ResourceRequestByProductUri

use of ddf.catalog.operation.impl.ResourceRequestByProductUri in project alliance by codice.

the class CatalogInputAdapter method buildReadRequest.

/**
 * @param metacard the Metacard containing the relevant information for the ResourceRequest. May
 *     not be null.
 * @return the ResourceRequest object to query the CatalogFramework service.
 */
public ResourceRequest buildReadRequest(Metacard metacard, String qualifier) {
    if (metacard == null) {
        throw new IllegalArgumentException("method argument 'metacard' may not be null.");
    }
    if (qualifier == null) {
        throw new IllegalArgumentException("method argument 'qualifier' may not be null.");
    }
    Attribute attribute = metacard.getAttribute(Metacard.DERIVED_RESOURCE_URI);
    if (attribute != null) {
        List<Serializable> values = attribute.getValues();
        if (values == null) {
            throw new IllegalStateException(String.format("metacard attribute %s has no values assigned.", Metacard.DERIVED_RESOURCE_URI));
        }
        URI qualifiedUri = findDerivedResourceUri(values, qualifier);
        return new ResourceRequestByProductUri(qualifiedUri);
    }
    throw new IllegalStateException(String.format("The supplied metacard does not contain the " + "'%s' attribute.", Metacard.DERIVED_RESOURCE_URI));
}
Also used : Serializable(java.io.Serializable) Attribute(ddf.catalog.data.Attribute) ResourceRequestByProductUri(ddf.catalog.operation.impl.ResourceRequestByProductUri) URI(java.net.URI)

Example 3 with ResourceRequestByProductUri

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

Aggregations

ResourceRequestByProductUri (ddf.catalog.operation.impl.ResourceRequestByProductUri)3 URI (java.net.URI)3 StorageException (ddf.catalog.content.StorageException)2 StorageProvider (ddf.catalog.content.StorageProvider)2 ContentItem (ddf.catalog.content.data.ContentItem)2 DeleteStorageRequestImpl (ddf.catalog.content.operation.impl.DeleteStorageRequestImpl)2 DeletedMetacard (ddf.catalog.core.versioning.DeletedMetacard)2 MetacardVersion (ddf.catalog.core.versioning.MetacardVersion)2 BinaryContent (ddf.catalog.data.BinaryContent)2 Metacard (ddf.catalog.data.Metacard)2 Result (ddf.catalog.data.Result)2 ResourceResponse (ddf.catalog.operation.ResourceResponse)2 DeleteRequestImpl (ddf.catalog.operation.impl.DeleteRequestImpl)2 QueryImpl (ddf.catalog.operation.impl.QueryImpl)2 QueryRequestImpl (ddf.catalog.operation.impl.QueryRequestImpl)2 ResourceNotFoundException (ddf.catalog.resource.ResourceNotFoundException)2 ResourceNotSupportedException (ddf.catalog.resource.ResourceNotSupportedException)2 IngestException (ddf.catalog.source.IngestException)2 CatalogTransformerException (ddf.catalog.transform.CatalogTransformerException)2 MetacardTransformer (ddf.catalog.transform.MetacardTransformer)2