Search in sources :

Example 1 with CatalogCommandRuntimeException

use of org.codice.ddf.commands.util.CatalogCommandRuntimeException 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 CatalogCommandRuntimeException

use of org.codice.ddf.commands.util.CatalogCommandRuntimeException in project ddf by codice.

the class ExportCommand method executeWithSubject.

@Override
protected Object executeWithSubject() throws Exception {
    Filter filter = getFilter();
    transformer = getServiceByFilter(MetacardTransformer.class, String.format("(%s=%s)", "id", DEFAULT_TRANSFORMER_ID)).orElseThrow(() -> new CatalogCommandRuntimeException("Could not get " + DEFAULT_TRANSFORMER_ID + " transformer"));
    revisionFilter = initRevisionFilter();
    final File outputFile = initOutputFile(output);
    if (outputFile.exists()) {
        printErrorMessage(String.format("File [%s] already exists!", outputFile.getPath()));
        return null;
    }
    final File parentDirectory = outputFile.getParentFile();
    if (parentDirectory == null || !parentDirectory.isDirectory()) {
        printErrorMessage(String.format("Directory [%s] must exist.", output));
        console.println("If the directory does indeed exist, try putting the path in quotes.");
        return null;
    }
    String filename = FilenameUtils.getName(outputFile.getPath());
    if (StringUtils.isBlank(filename) || !filename.endsWith(".zip")) {
        console.println("Filename must end with '.zip' and not be blank");
        return null;
    }
    if (delete && !force) {
        console.println("This action will remove all exported metacards and content from the catalog. Are you sure you wish to continue? (y/N):");
        String input = getUserInputModifiable().toString();
        if (!input.matches("^[yY][eE]?[sS]?$")) {
            console.println("ABORTED EXPORT.");
            return null;
        }
    }
    SecurityLogger.audit("Called catalog:export command with path : {}", output);
    ZipFile zipFile = new ZipFile(outputFile);
    console.println("Starting metacard export...");
    Instant start = Instant.now();
    List<ExportItem> exportedItems = doMetacardExport(zipFile, filter);
    console.println("Metacards exported in: " + getFormattedDuration(start));
    console.println("Number of metacards exported: " + exportedItems.size());
    console.println();
    SecurityLogger.audit("Ids of exported metacards and content:\n{}", exportedItems.stream().map(ExportItem::getId).distinct().collect(Collectors.joining(", ", "[", "]")));
    console.println("Starting content export...");
    start = Instant.now();
    List<ExportItem> exportedContentItems = doContentExport(zipFile, exportedItems);
    console.println("Content exported in: " + getFormattedDuration(start));
    console.println("Number of content exported: " + exportedContentItems.size());
    console.println();
    if (delete) {
        doDelete(exportedItems, exportedContentItems);
    }
    if (!unsafe) {
        SecurityLogger.audit("Signing exported data. file: [{}]", zipFile.getFile().getName());
        console.println("Signing zip file...");
        start = Instant.now();
        jarSigner.signJar(zipFile.getFile(), System.getProperty("org.codice.ddf.system.hostname"), System.getProperty("javax.net.ssl.keyStorePassword"), System.getProperty("javax.net.ssl.keyStore"), System.getProperty("javax.net.ssl.keyStorePassword"));
        console.println("zip file signed in: " + getFormattedDuration(start));
    }
    console.println("Export complete.");
    console.println("Exported to: " + zipFile.getFile().getCanonicalPath());
    return null;
}
Also used : ExportItem(org.codice.ddf.commands.catalog.export.ExportItem) ZipFile(net.lingala.zip4j.core.ZipFile) Filter(org.opengis.filter.Filter) Instant(java.time.Instant) CatalogCommandRuntimeException(org.codice.ddf.commands.util.CatalogCommandRuntimeException) ZipFile(net.lingala.zip4j.core.ZipFile) File(java.io.File)

Example 3 with CatalogCommandRuntimeException

use of org.codice.ddf.commands.util.CatalogCommandRuntimeException in project ddf by codice.

the class ExportCommand method writeToZip.

private void writeToZip(/*Mutable,IO*/
ZipFile zipFile, Result result) {
    ZipParameters parameters = new ZipParameters();
    parameters.setSourceExternalStream(true);
    String id = result.getMetacard().getId();
    parameters.setFileNameInZip(Paths.get("metacards", id.substring(0, 3), id, "metacard", id + ".xml").toString());
    try {
        BinaryContent binaryMetacard = transformer.transform(result.getMetacard(), Collections.emptyMap());
        zipFile.addStream(binaryMetacard.getInputStream(), parameters);
    } catch (ZipException e) {
        LOGGER.error("Error processing result and adding to ZIP", e);
        throw new CatalogCommandRuntimeException(e);
    } catch (CatalogTransformerException e) {
        LOGGER.warn("Could not transform metacard. Metacard will not be added to zip [{}]", result.getMetacard().getId());
        console.printf("%sCould not transform metacard. Metacard will not be included in export. %s - %s%s%n", Ansi.ansi().fg(Ansi.Color.RED).toString(), result.getMetacard().getId(), result.getMetacard().getTitle(), Ansi.ansi().reset().toString());
    }
}
Also used : ZipException(net.lingala.zip4j.exception.ZipException) CatalogCommandRuntimeException(org.codice.ddf.commands.util.CatalogCommandRuntimeException) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) BinaryContent(ddf.catalog.data.BinaryContent) ZipParameters(net.lingala.zip4j.model.ZipParameters)

Example 4 with CatalogCommandRuntimeException

use of org.codice.ddf.commands.util.CatalogCommandRuntimeException in project ddf by codice.

the class ImportCommand method executeWithSubject.

@Override
protected Object executeWithSubject() throws Exception {
    int metacards = 0;
    int content = 0;
    int derivedContent = 0;
    ZipValidator zipValidator = initZipValidator();
    File file = initImportFile(importFile);
    InputTransformer transformer = getServiceByFilter(InputTransformer.class, String.format("(%s=%s)", "id", DEFAULT_TRANSFORMER_ID)).orElseThrow(() -> new CatalogCommandRuntimeException("Could not get " + DEFAULT_TRANSFORMER_ID + " input transformer"));
    if (unsafe) {
        if (!force) {
            console.println("This will import data with no check to see if data is modified/corrupt. Do you wish to continue?");
            String input = getUserInputModifiable().toString();
            if (!input.matches("^[yY][eE]?[sS]?$")) {
                console.println("ABORTED IMPORT.");
                return null;
            }
        }
        SecurityLogger.audit("Skipping validation check of imported data. There are no " + "guarantees of integrity or authenticity of the imported data." + "File being imported: {}", importFile);
    } else {
        if (!zipValidator.validateZipFile(importFile)) {
            throw new CatalogCommandRuntimeException("Signature on zip file is not valid");
        }
    }
    SecurityLogger.audit("Called catalog:import command on the file: {}", importFile);
    console.println("Importing file");
    Instant start = Instant.now();
    try (InputStream fis = new FileInputStream(file);
        ZipInputStream zipInputStream = new ZipInputStream(fis)) {
        ZipEntry entry = zipInputStream.getNextEntry();
        while (entry != null) {
            String filename = entry.getName();
            if (filename.startsWith("META-INF")) {
                entry = zipInputStream.getNextEntry();
                continue;
            }
            String[] pathParts = filename.split("\\" + File.separator);
            if (pathParts.length < 5) {
                console.println("Entry is not valid! " + filename);
                entry = zipInputStream.getNextEntry();
                continue;
            }
            String id = pathParts[ID];
            String type = pathParts[TYPE];
            switch(type) {
                case "metacard":
                    {
                        metacards++;
                        String metacardName = pathParts[NAME];
                        Metacard metacard = null;
                        try {
                            metacard = transformer.transform(new UncloseableBufferedInputStreamWrapper(zipInputStream), id);
                        } catch (IOException | CatalogTransformerException e) {
                            LOGGER.debug("Could not transform metacard: {}", id);
                        }
                        catalogProvider.create(new CreateRequestImpl(metacard));
                        break;
                    }
                case "content":
                    {
                        content++;
                        String contentFilename = pathParts[NAME];
                        ContentItem contentItem = new ContentItemImpl(id, new ZipEntryByteSource(new UncloseableBufferedInputStreamWrapper(zipInputStream)), null, contentFilename, entry.getSize(), null);
                        CreateStorageRequestImpl createStorageRequest = new CreateStorageRequestImpl(Collections.singletonList(contentItem), id, new HashMap<>());
                        storageProvider.create(createStorageRequest);
                        storageProvider.commit(createStorageRequest);
                        break;
                    }
                case "derived":
                    {
                        derivedContent++;
                        String qualifier = pathParts[NAME];
                        String derivedContentName = pathParts[DERIVED_NAME];
                        ContentItem contentItem = new ContentItemImpl(id, qualifier, new ZipEntryByteSource(new UncloseableBufferedInputStreamWrapper(zipInputStream)), null, derivedContentName, entry.getSize(), null);
                        CreateStorageRequestImpl createStorageRequest = new CreateStorageRequestImpl(Collections.singletonList(contentItem), id, new HashMap<>());
                        storageProvider.create(createStorageRequest);
                        storageProvider.commit(createStorageRequest);
                        break;
                    }
                default:
                    {
                        LOGGER.debug("Cannot interpret type of {}", type);
                    }
            }
            entry = zipInputStream.getNextEntry();
        }
    } catch (Exception e) {
        printErrorMessage(String.format("Exception while importing metacards (%s)%nFor more information set the log level to INFO (log:set INFO org.codice.ddf.commands.catalog) ", e.getMessage()));
        LOGGER.info("Exception while importing metacards", e);
        throw e;
    }
    console.println("File imported successfully. Imported in: " + getFormattedDuration(start));
    console.println("Number of metacards imported: " + metacards);
    console.println("Number of content imported: " + content);
    console.println("Number of derived content imported: " + derivedContent);
    return null;
}
Also used : HashMap(java.util.HashMap) BufferedInputStream(java.io.BufferedInputStream) ZipInputStream(java.util.zip.ZipInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Instant(java.time.Instant) ZipEntry(java.util.zip.ZipEntry) CatalogCommandRuntimeException(org.codice.ddf.commands.util.CatalogCommandRuntimeException) InputTransformer(ddf.catalog.transform.InputTransformer) FileInputStream(java.io.FileInputStream) CatalogCommandRuntimeException(org.codice.ddf.commands.util.CatalogCommandRuntimeException) IOException(java.io.IOException) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) ZipInputStream(java.util.zip.ZipInputStream) Metacard(ddf.catalog.data.Metacard) CreateStorageRequestImpl(ddf.catalog.content.operation.impl.CreateStorageRequestImpl) CreateRequestImpl(ddf.catalog.operation.impl.CreateRequestImpl) File(java.io.File) ZipValidator(org.codice.ddf.catalog.transformer.zip.ZipValidator) ContentItem(ddf.catalog.content.data.ContentItem) ContentItemImpl(ddf.catalog.content.data.impl.ContentItemImpl)

Aggregations

CatalogTransformerException (ddf.catalog.transform.CatalogTransformerException)3 File (java.io.File)3 Instant (java.time.Instant)3 CatalogCommandRuntimeException (org.codice.ddf.commands.util.CatalogCommandRuntimeException)3 ContentItem (ddf.catalog.content.data.ContentItem)2 BinaryContent (ddf.catalog.data.BinaryContent)2 Metacard (ddf.catalog.data.Metacard)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 StorageException (ddf.catalog.content.StorageException)1 StorageProvider (ddf.catalog.content.StorageProvider)1 ContentItemImpl (ddf.catalog.content.data.impl.ContentItemImpl)1 CreateStorageRequestImpl (ddf.catalog.content.operation.impl.CreateStorageRequestImpl)1 DeleteStorageRequestImpl (ddf.catalog.content.operation.impl.DeleteStorageRequestImpl)1 DeletedMetacard (ddf.catalog.core.versioning.DeletedMetacard)1 MetacardVersion (ddf.catalog.core.versioning.MetacardVersion)1 Result (ddf.catalog.data.Result)1 ResourceResponse (ddf.catalog.operation.ResourceResponse)1 CreateRequestImpl (ddf.catalog.operation.impl.CreateRequestImpl)1 DeleteRequestImpl (ddf.catalog.operation.impl.DeleteRequestImpl)1