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