Search in sources :

Example 46 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 47 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 48 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 49 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)

Example 50 with CatalogTransformerException

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

the class Query method getMetacardForId.

/**
     * @param searchPhrase The search phrase used to query for the metacard.
     * @param proxyTicket  The CAS proxy ticket that will be used by the STS to get a SAML assertion.
     * @return
     */
private String getMetacardForId(String searchPhrase, String proxyTicket) {
    Filter filter = filterBuilder.attribute(Metacard.ANY_TEXT).is().like().text(searchPhrase);
    LOGGER.info("Query filter: {}", filter.toString());
    String queryError = "Unable to perform query " + filter.toString() + ".";
    QueryRequest request = new QueryRequestImpl(new QueryImpl(filter), true);
    StringBuilder responseString = new StringBuilder();
    try {
        Subject subject = securityManager.getSubject(new CasAuthenticationToken(proxyTicket));
        LOGGER.info("Adding {} property with value {} to request", SecurityConstants.SECURITY_SUBJECT, subject);
        request.getProperties().put(SecurityConstants.SECURITY_SUBJECT, subject);
    } catch (SecurityServiceException se) {
        LOGGER.error("Could not retrieve subject from securitymanager.", se);
        return queryError;
    }
    try {
        LOGGER.debug("About to query the catalog framework with query {}", filter.toString());
        QueryResponse queryResponse = catalogFramework.query(request, null);
        LOGGER.debug("Got query response from catalog framework for query {}", filter.toString());
        List<Result> results = queryResponse.getResults();
        if (results != null) {
            String message = "The query for " + filter.toString() + " returned " + results.size() + " results.";
            responseString.append(message);
            LOGGER.debug(message);
            for (Result curResult : results) {
                Metacard metacard = curResult.getMetacard();
                LOGGER.debug("Transforming the metacard with id [{}] to xml.", metacard.getId());
                BinaryContent content = catalogFramework.transform(metacard, "xml", null);
                StringWriter writer = new StringWriter();
                IOUtils.copy(content.getInputStream(), writer, "UTF8");
                LOGGER.debug("Formatting xml for metacard with id [{}].", metacard.getId());
                responseString.append(format(writer.toString()));
            }
        } else {
            String message = "The query for " + filter.toString() + " returned a null result.";
            responseString.append(message);
            LOGGER.warn(message);
        }
    } catch (SourceUnavailableException e) {
        LOGGER.error(queryError, e);
    } catch (UnsupportedQueryException e) {
        LOGGER.error(queryError, e);
    } catch (FederationException e) {
        LOGGER.error(queryError, e);
    } catch (CatalogTransformerException e) {
        LOGGER.error(queryError, e);
    } catch (IOException e) {
        LOGGER.error(queryError, e);
    }
    return responseString.toString();
}
Also used : SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) SecurityServiceException(ddf.security.service.SecurityServiceException) QueryRequest(ddf.catalog.operation.QueryRequest) CasAuthenticationToken(ddf.security.service.impl.cas.CasAuthenticationToken) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) IOException(java.io.IOException) BinaryContent(ddf.catalog.data.BinaryContent) FederationException(ddf.catalog.federation.FederationException) Subject(ddf.security.Subject) Result(ddf.catalog.data.Result) QueryImpl(ddf.catalog.operation.impl.QueryImpl) Metacard(ddf.catalog.data.Metacard) StringWriter(java.io.StringWriter) Filter(org.opengis.filter.Filter) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) QueryResponse(ddf.catalog.operation.QueryResponse)

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