Search in sources :

Example 16 with CatalogTransformerException

use of ddf.catalog.transform.CatalogTransformerException in project ddf by codice.

the class RestReplicatorPlugin method transform.

private String transform(Metacard m, WebClient client) throws PluginExecutionException {
    BinaryContent binaryContent;
    try {
        binaryContent = transformer.transform(m, new HashMap<>());
        client.type(getValidMimeType(binaryContent.getMimeTypeValue()));
        return new String(binaryContent.getByteArray(), StandardCharsets.UTF_8);
    } catch (IOException e) {
        LOGGER.debug("Could not understand metacard.", e);
        throw new PluginExecutionException("Could not send metacard.");
    } catch (CatalogTransformerException e) {
        LOGGER.debug("Could not transform metacard.", e);
        throw new PluginExecutionException("Could not send metacard.");
    }
}
Also used : HashMap(java.util.HashMap) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) IOException(java.io.IOException) BinaryContent(ddf.catalog.data.BinaryContent) PluginExecutionException(ddf.catalog.plugin.PluginExecutionException)

Example 17 with CatalogTransformerException

use of ddf.catalog.transform.CatalogTransformerException in project ddf by codice.

the class ExportCommand method writeResultToZip.

private void writeResultToZip(/*Mutable,IO*/
ZipOutputStream zipOutputStream, Result result) {
    String id = result.getMetacard().getId();
    ZipEntry zipEntry = new ZipEntry(Paths.get("metacards", id.substring(0, 3), id, "metacard", id + ".xml").toString());
    try {
        BinaryContent binaryMetacard = transformer.transform(result.getMetacard(), Collections.emptyMap());
        try (InputStream metacardStream = binaryMetacard.getInputStream()) {
            zipOutputStream.putNextEntry(zipEntry);
            IOUtils.copy(metacardStream, zipOutputStream);
        }
    } catch (CatalogTransformerException | IOException 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 : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) IOException(java.io.IOException) BinaryContent(ddf.catalog.data.BinaryContent)

Example 18 with CatalogTransformerException

use of ddf.catalog.transform.CatalogTransformerException in project ddf by codice.

the class IngestCommand method processIncludeContent.

private void processIncludeContent(ArrayBlockingQueue<Metacard> metacardQueue) {
    File inputFile = new File(filePath);
    Map<String, Serializable> arguments = new HashMap<>();
    arguments.put(DumpCommand.FILE_PATH, inputFile.getParent() + File.separator);
    arguments.put(FILE_NAME, inputFile.getName());
    ByteSource byteSource = com.google.common.io.Files.asByteSource(inputFile);
    Optional<InputCollectionTransformer> zipDecompression = getZipDecompression();
    if (zipDecompression.isPresent()) {
        try (InputStream inputStream = byteSource.openBufferedStream()) {
            List<Metacard> metacardList = zipDecompression.get().transform(inputStream, arguments).stream().filter(Objects::nonNull).collect(Collectors.toList());
            if (!metacardList.isEmpty()) {
                metacardFileMapping = generateFileMap(new File(inputFile.getParent(), CONTENT_PATH));
                fileCount.set(metacardList.size());
                for (Metacard metacard : metacardList) {
                    putMetacardOnQueue(metacardQueue, metacard);
                }
            }
        } catch (IOException | CatalogTransformerException e) {
            LOGGER.info("Unable to transform zip file into metacard list.", e);
            INGEST_LOGGER.warn("Unable to transform zip file into metacard list.", e);
        }
    } else {
        LOGGER.info("No Zip Transformer found. Unable to transform zip file into metacard list.");
        INGEST_LOGGER.warn("No Zip Transformer found. Unable to transform zip file into metacard list.");
    }
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) ObjectInputStream(java.io.ObjectInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) InputCollectionTransformer(ddf.catalog.transform.InputCollectionTransformer) Metacard(ddf.catalog.data.Metacard) ByteSource(com.google.common.io.ByteSource) File(java.io.File)

Example 19 with CatalogTransformerException

use of ddf.catalog.transform.CatalogTransformerException in project ddf by codice.

the class DumpCommand method exportMetacard.

private void exportMetacard(File dumpLocation, Metacard metacard, AtomicLong resultCount) throws IOException {
    if (SERIALIZED_OBJECT_ID.matches(transformerId)) {
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(getOutputFile(dumpLocation, metacard)))) {
            oos.writeObject(new MetacardImpl(metacard));
            oos.flush();
            resultCount.incrementAndGet();
        }
    } else {
        BinaryContent binaryContent;
        if (metacard != null) {
            for (MetacardTransformer transformer : transformers) {
                try {
                    binaryContent = transformer.transform(metacard, new HashMap<>());
                    if (binaryContent != null) {
                        try (FileOutputStream fos = new FileOutputStream(getOutputFile(dumpLocation, metacard))) {
                            fos.write(binaryContent.getByteArray());
                            fos.flush();
                        }
                        resultCount.incrementAndGet();
                        break;
                    }
                } catch (CatalogTransformerException e) {
                    LOGGER.info("One or more metacards failed to transform. Enable debug log for more details.");
                }
            }
        }
    }
}
Also used : MetacardTransformer(ddf.catalog.transform.MetacardTransformer) HashMap(java.util.HashMap) FileOutputStream(java.io.FileOutputStream) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) ObjectOutputStream(java.io.ObjectOutputStream) BinaryContent(ddf.catalog.data.BinaryContent) MetacardImpl(ddf.catalog.data.impl.MetacardImpl)

Example 20 with CatalogTransformerException

use of ddf.catalog.transform.CatalogTransformerException in project ddf by codice.

the class DumpCommand method createZip.

private void createZip(CatalogFacade catalog, QueryRequest queryRequest, File outputFile, AtomicLong resultCount) throws CatalogTransformerException {
    try (FileOutputStream fileOutputStream = new FileOutputStream(outputFile);
        ZipOutputStream zipOutputStream = new ZipOutputStream(fileOutputStream)) {
        // write the metacards to the zip
        ResultIterable.resultIterable(catalog::query, queryRequest).stream().map(Result::getMetacard).forEach(metacard -> {
            writeMetacardToZip(zipOutputStream, metacard);
            if (hasLocalResources(metacard)) {
                // write the resources to the zip
                Map<String, Resource> resourceMap = getAllMetacardContent(metacard);
                resourceMap.forEach((filename, resource) -> writeResourceToZip(zipOutputStream, filename, resource));
            }
            resultCount.incrementAndGet();
        });
    } catch (IOException e) {
        throw new CatalogTransformerException(String.format("Error occurred when initializing/closing ZipOutputStream with path %s.", outputFile.getAbsoluteFile()), e);
    }
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) Resource(ddf.catalog.resource.Resource) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) IOException(java.io.IOException)

Aggregations

CatalogTransformerException (ddf.catalog.transform.CatalogTransformerException)112 IOException (java.io.IOException)53 Metacard (ddf.catalog.data.Metacard)44 InputStream (java.io.InputStream)40 ByteArrayInputStream (java.io.ByteArrayInputStream)29 BinaryContent (ddf.catalog.data.BinaryContent)25 InputTransformer (ddf.catalog.transform.InputTransformer)21 Serializable (java.io.Serializable)21 HashMap (java.util.HashMap)21 Result (ddf.catalog.data.Result)16 BinaryContentImpl (ddf.catalog.data.impl.BinaryContentImpl)15 TemporaryFileBackedOutputStream (org.codice.ddf.platform.util.TemporaryFileBackedOutputStream)14 ArrayList (java.util.ArrayList)13 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)12 Test (org.junit.Test)12 AttributeImpl (ddf.catalog.data.impl.AttributeImpl)10 MimeType (javax.activation.MimeType)10 SourceResponse (ddf.catalog.operation.SourceResponse)9 MetacardTransformer (ddf.catalog.transform.MetacardTransformer)8 List (java.util.List)8