Search in sources :

Example 76 with CatalogTransformerException

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

the class KmlInputTransformer method transform.

@Override
public Metacard transform(InputStream inputStream, String id) throws IOException, CatalogTransformerException {
    MetacardImpl metacard;
    try (TemporaryFileBackedOutputStream fileBackedOutputStream = new TemporaryFileBackedOutputStream()) {
        try {
            IOUtils.copy(inputStream, fileBackedOutputStream);
        } catch (IOException e) {
            throw new CatalogTransformerException("Unable to transform KML to Metacard. Error reading input stream.", e);
        } finally {
            IOUtils.closeQuietly(inputStream);
        }
        try (InputStream inputStreamCopy = fileBackedOutputStream.asByteSource().openStream()) {
            metacard = (MetacardImpl) unmarshal(inputStreamCopy);
        }
        if (metacard == null) {
            throw new CatalogTransformerException("Unable to transform Kml to Metacard.");
        } else if (StringUtils.isNotEmpty(id)) {
            metacard.setAttribute(Metacard.ID, id);
        }
        try (Reader reader = fileBackedOutputStream.asByteSource().asCharSource(Charsets.UTF_8).openStream()) {
            String kmlString = CharStreams.toString(reader);
            metacard.setAttribute(Metacard.METADATA, kmlString);
        }
    } catch (IOException e) {
        throw new CatalogTransformerException("Unable to transform KML to Metacard. Error using file-backed stream.", e);
    }
    return metacard;
}
Also used : TemporaryFileBackedOutputStream(org.codice.ddf.platform.util.TemporaryFileBackedOutputStream) InputStream(java.io.InputStream) Reader(java.io.Reader) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) IOException(java.io.IOException) MetacardImpl(ddf.catalog.data.impl.MetacardImpl)

Example 77 with CatalogTransformerException

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

the class KmzInputTransformer method transform.

@Override
public Metacard transform(InputStream inputStream, String id) throws IOException, CatalogTransformerException {
    ZipInputStream zipInputStream = new ZipInputStream(inputStream);
    ZipEntry entry;
    while ((entry = zipInputStream.getNextEntry()) != null) {
        // so we stop at the first one we find.
        if (entry.getName().endsWith(KML_EXTENSION)) {
            return kmlInputTransformer.transform(zipInputStream, id);
        }
    }
    throw new CatalogTransformerException("Unable to parse any KML from KMZ file");
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) ZipEntry(java.util.zip.ZipEntry) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException)

Example 78 with CatalogTransformerException

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

the class KmzTransformer method transform.

/**
 * Transforms a single metacard to a zipped KMZ file.
 *
 * @param metacard the {@link Metacard} to be transformed
 * @param arguments any arguments to be used in the transformation. Keys are specific to each
 *     {@link MetacardTransformer} implementation
 * @return KMZ BinaryContent
 * @throws CatalogTransformerException
 */
@Override
public BinaryContent transform(Metacard metacard, Map<String, Serializable> arguments) throws CatalogTransformerException {
    BinaryContent kml = kmlTransformer.transform(metacard, arguments);
    BinaryContent kmz = transformKmlToKmz(kml);
    if (kmz == null) {
        throw new CatalogTransformerException("Unable to transform to KMZ for metacard ID: " + metacard.getId());
    }
    return kmz;
}
Also used : CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) BinaryContent(ddf.catalog.data.BinaryContent)

Example 79 with CatalogTransformerException

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

the class KmzTransformer method transform.

/**
 * Transforms a {@link SourceResponse} to a zipped KMZ file.
 *
 * @param upstreamResponse {@link SourceResponse} containing the results of a query.
 * @param arguments the arguments that may be used to execute the transform
 * @return KMZ BinaryContent
 * @throws CatalogTransformerException
 */
@Override
public BinaryContent transform(SourceResponse upstreamResponse, Map<String, Serializable> arguments) throws CatalogTransformerException {
    BinaryContent kml = kmlTransformer.transform(upstreamResponse, arguments);
    BinaryContent kmz = transformKmlToKmz(kml);
    if (kmz == null) {
        throw new CatalogTransformerException("Unable to transform KML to KMZ.");
    }
    return kmz;
}
Also used : CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) BinaryContent(ddf.catalog.data.BinaryContent)

Example 80 with CatalogTransformerException

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

the class GeoJsonMetacardTransformer method convertToJSON.

public static JSONObject convertToJSON(Metacard metacard) throws CatalogTransformerException {
    if (metacard == null) {
        throw new CatalogTransformerException("Cannot transform null metacard.");
    }
    // must be LinkedHashMap to maintain order
    JSONObject rootObject = new JSONObject();
    rootObject.put("type", "Feature");
    JSONObject properties = new JSONObject();
    for (AttributeDescriptor ad : metacard.getMetacardType().getAttributeDescriptors()) {
        Attribute attribute = metacard.getAttribute(ad.getName());
        if (attribute != null) {
            Object value = convertAttribute(attribute, ad);
            if (value != null) {
                if (Metacard.GEOGRAPHY.equals(attribute.getName())) {
                    rootObject.put(CompositeGeometry.GEOMETRY_KEY, value);
                } else {
                    properties.put(attribute.getName(), value);
                }
            }
        }
    }
    if (rootObject.get(CompositeGeometry.GEOMETRY_KEY) == null) {
        rootObject.put(CompositeGeometry.GEOMETRY_KEY, null);
    }
    properties.put(METACARD_TYPE_PROPERTY_KEY, metacard.getMetacardType().getName());
    if (metacard.getSourceId() != null && !"".equals(metacard.getSourceId())) {
        properties.put(SOURCE_ID_PROPERTY, metacard.getSourceId());
    }
    rootObject.put(CompositeGeometry.PROPERTIES_KEY, properties);
    return rootObject;
}
Also used : JSONObject(org.json.simple.JSONObject) Attribute(ddf.catalog.data.Attribute) AttributeDescriptor(ddf.catalog.data.AttributeDescriptor) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) JSONObject(org.json.simple.JSONObject)

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