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