Search in sources :

Example 81 with CatalogTransformerException

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

the class GeoJsonMetacardTransformer method convertValue.

private static Object convertValue(Serializable value, AttributeType.AttributeFormat format) throws CatalogTransformerException {
    if (value == null) {
        return null;
    }
    switch(format) {
        case BOOLEAN:
            return value;
        case DATE:
            SimpleDateFormat dateFormat = new SimpleDateFormat(ISO_8601_DATE_FORMAT);
            dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
            return dateFormat.format((Date) value);
        case BINARY:
            byte[] bytes = (byte[]) value;
            String base64 = DatatypeConverter.printBase64Binary(bytes);
            return base64;
        case DOUBLE:
        case LONG:
        case FLOAT:
        case INTEGER:
        case SHORT:
        case STRING:
        case XML:
            return value.toString();
        case GEOMETRY:
            WKTReader reader = new WKTReader();
            try {
                Geometry geometry = reader.read(value.toString());
                CompositeGeometry geoJsonGeometry = CompositeGeometry.getCompositeGeometry(geometry);
                if (geoJsonGeometry == null) {
                    throw new CatalogTransformerException("Could not perform transform: unsupported geometry [" + value + "]");
                }
                return geoJsonGeometry.toJsonMap();
            } catch (ParseException e) {
                throw new CatalogTransformerException("Could not perform transform: could not parse geometry [" + value + "]", e);
            }
        case OBJECT:
        default:
            return null;
    }
}
Also used : CompositeGeometry(ddf.geo.formatter.CompositeGeometry) Geometry(org.locationtech.jts.geom.Geometry) CompositeGeometry(ddf.geo.formatter.CompositeGeometry) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) MimeTypeParseException(javax.activation.MimeTypeParseException) ParseException(org.locationtech.jts.io.ParseException) WKTReader(org.locationtech.jts.io.WKTReader) SimpleDateFormat(java.text.SimpleDateFormat)

Example 82 with CatalogTransformerException

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

the class GeoJsonQueryResponseTransformer method transform.

@Override
public BinaryContent transform(SourceResponse upstreamResponse, Map<String, Serializable> arguments) throws CatalogTransformerException {
    if (upstreamResponse == null) {
        throw new CatalogTransformerException("Cannot transform null " + SourceResponse.class.getName());
    }
    JSONObject rootObject = new JSONObject();
    addNonNullObject(rootObject, "hits", upstreamResponse.getHits());
    JSONArray resultsList = new JSONArray();
    if (upstreamResponse.getResults() != null) {
        for (Result result : upstreamResponse.getResults()) {
            if (result == null) {
                throw new CatalogTransformerException("Cannot transform null " + Result.class.getName());
            }
            resultsList.add(convertToJSON(result));
        }
    }
    addNonNullObject(rootObject, "results", resultsList);
    String jsonText = JSONValue.toJSONString(rootObject);
    return new BinaryContentImpl(new ByteArrayInputStream(jsonText.getBytes(StandardCharsets.UTF_8)), DEFAULT_MIME_TYPE);
}
Also used : JSONObject(net.minidev.json.JSONObject) ByteArrayInputStream(java.io.ByteArrayInputStream) JSONArray(net.minidev.json.JSONArray) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) BinaryContentImpl(ddf.catalog.data.impl.BinaryContentImpl) Result(ddf.catalog.data.Result)

Example 83 with CatalogTransformerException

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

the class GeoJsonQueryResponseTransformer method createGeoJSON.

private Object createGeoJSON(Metacard metacard) throws CatalogTransformerException {
    if (metacardTransformer == null) {
        throw new CatalogTransformerException("The metacard transformer cannot be null");
    }
    BinaryContent rawContent = metacardTransformer.transform(metacard, null);
    JSONParser jsonParser = new JSONParser(JSONParser.DEFAULT_PERMISSIVE_MODE);
    try {
        return jsonParser.parse(rawContent.getInputStream());
    } catch (ParseException | UnsupportedEncodingException e) {
        throw new CatalogTransformerException("Unable to parse transformed metacard content as JSON", e);
    }
}
Also used : UnsupportedEncodingException(java.io.UnsupportedEncodingException) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) JSONParser(net.minidev.json.parser.JSONParser) MimeTypeParseException(javax.activation.MimeTypeParseException) ParseException(net.minidev.json.parser.ParseException) BinaryContent(ddf.catalog.data.BinaryContent)

Example 84 with CatalogTransformerException

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

the class AttributeMetacardTransformer method transform.

@Override
public BinaryContent transform(Metacard metacard, Map<String, Serializable> arguments) throws CatalogTransformerException {
    if (metacard == null) {
        throw new CatalogTransformerException("No attribute [" + attributeName + "] found in Metacard.");
    }
    LOGGER.debug("Attempting transformation of [{}] with transformer [{}]", metacard, this);
    Attribute attribute = metacard.getAttribute(attributeName);
    if (attribute != null && attribute.getValue() != null) {
        if (byte[].class.isAssignableFrom(attribute.getValue().getClass())) {
            return new BinaryContentImpl(new ByteArrayInputStream((byte[]) attribute.getValue()), mimeType);
        }
        if (String.class.isAssignableFrom(attribute.getValue().getClass())) {
            return new BinaryContentImpl(new ByteArrayInputStream(attribute.getValue().toString().getBytes(StandardCharsets.UTF_8)), mimeType);
        }
    }
    throw new CatalogTransformerException("No attribute [" + attributeName + "] found in Metacard.");
}
Also used : Attribute(ddf.catalog.data.Attribute) ByteArrayInputStream(java.io.ByteArrayInputStream) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) BinaryContentImpl(ddf.catalog.data.impl.BinaryContentImpl)

Example 85 with CatalogTransformerException

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

the class XsltResponseQueueTransformer method transform.

@Override
public BinaryContent transform(SourceResponse upstreamResponse, Map<String, Serializable> arguments) throws CatalogTransformerException {
    LOGGER.debug("Transforming ResponseQueue with XSLT tranformer");
    long grandTotal = -1;
    try {
        DocumentBuilder builder = XML_UTILS.getSecureDocumentBuilder(true);
        Document doc = builder.newDocument();
        Node resultsElement = doc.appendChild(createElement(doc, XML_RESULTS_NAMESPACE, "results", null));
        // TODO use streaming XSLT, not DOM
        List<Result> results = upstreamResponse.getResults();
        grandTotal = upstreamResponse.getHits();
        for (Result result : results) {
            Metacard metacard = result.getMetacard();
            if (metacard != null) {
                String metadata = metacard.getMetadata();
                if (metadata != null) {
                    Element metacardElement = createElement(doc, XML_RESULTS_NAMESPACE, "metacard", null);
                    if (metacard.getId() != null) {
                        metacardElement.appendChild(createElement(doc, XML_RESULTS_NAMESPACE, "id", metacard.getId()));
                    }
                    if (metacard.getMetacardType().toString() != null) {
                        metacardElement.appendChild(createElement(doc, XML_RESULTS_NAMESPACE, "type", metacard.getMetacardType().getName()));
                    }
                    if (metacard.getTitle() != null) {
                        metacardElement.appendChild(createElement(doc, XML_RESULTS_NAMESPACE, "title", metacard.getTitle()));
                    }
                    if (result.getRelevanceScore() != null) {
                        metacardElement.appendChild(createElement(doc, XML_RESULTS_NAMESPACE, "score", result.getRelevanceScore().toString()));
                    }
                    if (result.getDistanceInMeters() != null) {
                        metacardElement.appendChild(createElement(doc, XML_RESULTS_NAMESPACE, "distance", result.getDistanceInMeters().toString()));
                    }
                    if (metacard.getSourceId() != null) {
                        metacardElement.appendChild(createElement(doc, XML_RESULTS_NAMESPACE, "site", metacard.getSourceId()));
                    }
                    if (metacard.getContentTypeName() != null) {
                        String contentType = metacard.getContentTypeName();
                        Element typeElement = createElement(doc, XML_RESULTS_NAMESPACE, "content-type", contentType);
                        // TODO revisit what to put in the qualifier
                        typeElement.setAttribute("qualifier", "content-type");
                        metacardElement.appendChild(typeElement);
                    }
                    if (metacard.getResourceURI() != null) {
                        try {
                            metacardElement.appendChild(createElement(doc, XML_RESULTS_NAMESPACE, "product", metacard.getResourceURI().toString()));
                        } catch (DOMException e) {
                            LOGGER.debug(" Unable to create resource uri element", e);
                        }
                    }
                    if (metacard.getThumbnail() != null) {
                        metacardElement.appendChild(createElement(doc, XML_RESULTS_NAMESPACE, "thumbnail", Base64.getEncoder().encodeToString(metacard.getThumbnail())));
                        try {
                            String mimeType = URLConnection.guessContentTypeFromStream(new ByteArrayInputStream(metacard.getThumbnail()));
                            metacardElement.appendChild(createElement(doc, XML_RESULTS_NAMESPACE, "t_mimetype", mimeType));
                        } catch (IOException e) {
                            metacardElement.appendChild(createElement(doc, XML_RESULTS_NAMESPACE, "t_mimetype", "image/png"));
                        }
                    }
                    DateTimeFormatter fmt = ISODateTimeFormat.dateTime();
                    if (metacard.getCreatedDate() != null) {
                        metacardElement.appendChild(createElement(doc, XML_RESULTS_NAMESPACE, "created", fmt.print(metacard.getCreatedDate().getTime())));
                    }
                    // looking at the date last modified
                    if (metacard.getModifiedDate() != null) {
                        metacardElement.appendChild(createElement(doc, XML_RESULTS_NAMESPACE, "updated", fmt.print(metacard.getModifiedDate().getTime())));
                    }
                    if (metacard.getEffectiveDate() != null) {
                        metacardElement.appendChild(createElement(doc, XML_RESULTS_NAMESPACE, "effective", fmt.print(metacard.getEffectiveDate().getTime())));
                    }
                    if (metacard.getLocation() != null) {
                        metacardElement.appendChild(createElement(doc, XML_RESULTS_NAMESPACE, "location", metacard.getLocation()));
                    }
                    Element documentElement = doc.createElementNS(XML_RESULTS_NAMESPACE, "document");
                    metacardElement.appendChild(documentElement);
                    resultsElement.appendChild(metacardElement);
                    Node importedNode = doc.importNode(new XPathHelper(metadata).getDocument().getFirstChild(), true);
                    documentElement.appendChild(importedNode);
                } else {
                    LOGGER.debug("Null content/document returned to XSLT ResponseQueueTransformer");
                }
            }
        }
        if (LOGGER.isDebugEnabled()) {
            DOMImplementationLS domImplementation = (DOMImplementationLS) doc.getImplementation();
            LSSerializer lsSerializer = domImplementation.createLSSerializer();
            LOGGER.debug("Generated XML input for transform: " + lsSerializer.writeToString(doc));
        }
        LOGGER.debug("Starting responsequeue xslt transform.");
        Transformer transformer;
        Map<String, Object> mergedMap = new HashMap<String, Object>();
        mergedMap.put(GRAND_TOTAL, grandTotal);
        if (arguments != null) {
            mergedMap.putAll(arguments);
        }
        BinaryContent resultContent;
        StreamResult resultOutput = null;
        Source source = new DOMSource(doc);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        resultOutput = new StreamResult(baos);
        try {
            transformer = templates.newTransformer();
        } catch (TransformerConfigurationException tce) {
            throw new CatalogTransformerException(TRANSFORMATION_FAILURE_MSG, tce);
        }
        for (Map.Entry<String, Object> entry : mergedMap.entrySet()) {
            LOGGER.trace("Adding parameter to transform {{}:{}}", entry.getKey(), entry.getValue());
            transformer.setParameter(entry.getKey(), entry.getValue());
        }
        try {
            transformer.transform(source, resultOutput);
            byte[] bytes = baos.toByteArray();
            LOGGER.debug("Transform complete.");
            resultContent = new XsltTransformedContent(bytes, mimeType);
        } catch (TransformerException te) {
            LOGGER.debug(TRANSFORMATION_FAILURE_MSG, te);
            throw new CatalogTransformerException(TRANSFORMATION_FAILURE_MSG, te);
        }
        return resultContent;
    } catch (ParserConfigurationException e) {
        LOGGER.debug("Error creating new document: ", e);
        throw new CatalogTransformerException("Error merging entries to xml feed.", e);
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) QueryResponseTransformer(ddf.catalog.transform.QueryResponseTransformer) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) XPathHelper(ddf.util.XPathHelper) HashMap(java.util.HashMap) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) DOMImplementationLS(org.w3c.dom.ls.DOMImplementationLS) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) Document(org.w3c.dom.Document) BinaryContent(ddf.catalog.data.BinaryContent) DOMSource(javax.xml.transform.dom.DOMSource) Source(javax.xml.transform.Source) StreamResult(javax.xml.transform.stream.StreamResult) Result(ddf.catalog.data.Result) DOMException(org.w3c.dom.DOMException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) TransformerException(javax.xml.transform.TransformerException) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) StreamResult(javax.xml.transform.stream.StreamResult) LSSerializer(org.w3c.dom.ls.LSSerializer) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Metacard(ddf.catalog.data.Metacard) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) HashMap(java.util.HashMap) Map(java.util.Map)

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