use of ddf.catalog.transform.CatalogTransformerException in project ddf by codice.
the class ExportCommand method writeToZip.
private void writeToZip(/*Mutable,IO*/
ZipFile zipFile, Result result) {
ZipParameters parameters = new ZipParameters();
parameters.setSourceExternalStream(true);
String id = result.getMetacard().getId();
parameters.setFileNameInZip(Paths.get("metacards", id.substring(0, 3), id, "metacard", id + ".xml").toString());
try {
BinaryContent binaryMetacard = transformer.transform(result.getMetacard(), Collections.emptyMap());
zipFile.addStream(binaryMetacard.getInputStream(), parameters);
} catch (ZipException e) {
LOGGER.error("Error processing result and adding to ZIP", e);
throw new CatalogCommandRuntimeException(e);
} catch (CatalogTransformerException 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());
}
}
use of ddf.catalog.transform.CatalogTransformerException in project ddf by codice.
the class OpenSearchEndpoint method executeQuery.
/**
* Executes the OpenSearchQuery and formulates the response
*
* @param format - of the results in the response
* @param query - the query to execute
* @param ui -the ui information to use to format the results
* @param properties
* @return the response on the query
*/
private Response executeQuery(String format, OpenSearchQuery query, UriInfo ui, Map<String, Serializable> properties) {
Response response = null;
String queryFormat = format;
MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
List<String> subscriptionList = queryParams.get(Constants.SUBSCRIPTION_KEY);
LOGGER.debug("Attempting to execute query: {}", query.toString());
try {
Map<String, Serializable> arguments = new HashMap<String, Serializable>();
String organization = framework.getOrganization();
String url = ui.getRequestUri().toString();
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("organization: {}", organization);
LOGGER.debug("url: {}", url);
}
arguments.put("organization", organization);
arguments.put("url", url);
// interval
if (subscriptionList != null && !subscriptionList.isEmpty()) {
String subscription = subscriptionList.get(0);
LOGGER.debug("Subscription: {}", subscription);
arguments.put(Constants.SUBSCRIPTION_KEY, subscription);
List<String> intervalList = queryParams.get(UPDATE_QUERY_INTERVAL);
if (intervalList != null && !intervalList.isEmpty()) {
arguments.put(UPDATE_QUERY_INTERVAL, intervalList.get(0));
}
}
if (StringUtils.isEmpty(queryFormat)) {
queryFormat = DEFAULT_FORMAT;
}
if (query.getFilter() != null) {
QueryRequest queryRequest = new QueryRequestImpl(query, query.isEnterprise(), query.getSiteIds(), properties);
QueryResponse queryResponse;
LOGGER.debug("Sending query");
queryResponse = framework.query(queryRequest);
// pass in the format for the transform
BinaryContent content = framework.transform(queryResponse, queryFormat, arguments);
response = Response.ok(content.getInputStream(), content.getMimeTypeValue()).build();
} else {
// No query was specified
QueryRequest queryRequest = new QueryRequestImpl(query, query.isEnterprise(), query.getSiteIds(), null);
// Create a dummy QueryResponse with zero results
QueryResponseImpl queryResponseQueue = new QueryResponseImpl(queryRequest, new ArrayList<Result>(), 0);
// pass in the format for the transform
BinaryContent content = framework.transform(queryResponseQueue, queryFormat, arguments);
if (null != content) {
response = Response.ok(content.getInputStream(), content.getMimeTypeValue()).build();
}
}
} catch (UnsupportedQueryException ce) {
LOGGER.info("Unsupported query", ce);
response = Response.status(Response.Status.BAD_REQUEST).entity(wrapStringInPreformattedTags("Unsupported query")).build();
} catch (CatalogTransformerException e) {
LOGGER.info("Error transforming response", e);
response = Response.serverError().entity(wrapStringInPreformattedTags("Error transforming response")).build();
} catch (FederationException e) {
LOGGER.info("Error executing query", e);
response = Response.serverError().entity(wrapStringInPreformattedTags("Error executing query")).build();
} catch (SourceUnavailableException e) {
LOGGER.info("Error executing query because the underlying source was unavailable.", e);
response = Response.serverError().entity(wrapStringInPreformattedTags("Error executing query because the underlying source was unavailable.")).build();
} catch (RuntimeException e) {
// Account for any runtime exceptions and send back a server error
// this prevents full stacktraces returning to the client
// this allows for a graceful server error to be returned
LOGGER.info("RuntimeException on executing query", e);
response = Response.serverError().entity(wrapStringInPreformattedTags("RuntimeException on executing query")).build();
}
return response;
}
use of ddf.catalog.transform.CatalogTransformerException in project ddf by codice.
the class KMLTransformerImpl method createKmlGeo.
private Geometry createKmlGeo(com.vividsolutions.jts.geom.Geometry geo) throws CatalogTransformerException {
Geometry kmlGeo = null;
if (Point.class.getSimpleName().equals(geo.getGeometryType())) {
Point jtsPoint = (Point) geo;
kmlGeo = KmlFactory.createPoint().addToCoordinates(jtsPoint.getX(), jtsPoint.getY());
} else if (LineString.class.getSimpleName().equals(geo.getGeometryType())) {
LineString jtsLS = (LineString) geo;
de.micromata.opengis.kml.v_2_2_0.LineString kmlLS = KmlFactory.createLineString();
List<Coordinate> kmlCoords = kmlLS.createAndSetCoordinates();
for (com.vividsolutions.jts.geom.Coordinate coord : jtsLS.getCoordinates()) {
kmlCoords.add(new Coordinate(coord.x, coord.y));
}
kmlGeo = kmlLS;
} else if (Polygon.class.getSimpleName().equals(geo.getGeometryType())) {
Polygon jtsPoly = (Polygon) geo;
de.micromata.opengis.kml.v_2_2_0.Polygon kmlPoly = KmlFactory.createPolygon();
List<Coordinate> kmlCoords = kmlPoly.createAndSetOuterBoundaryIs().createAndSetLinearRing().createAndSetCoordinates();
for (com.vividsolutions.jts.geom.Coordinate coord : jtsPoly.getCoordinates()) {
kmlCoords.add(new Coordinate(coord.x, coord.y));
}
kmlGeo = kmlPoly;
} else if (geo instanceof GeometryCollection) {
List<Geometry> geos = new ArrayList<Geometry>();
for (int xx = 0; xx < geo.getNumGeometries(); xx++) {
geos.add(createKmlGeo(geo.getGeometryN(xx)));
}
kmlGeo = KmlFactory.createMultiGeometry().withGeometry(geos);
} else {
throw new CatalogTransformerException("Unknown / Unsupported Geometry Type '" + geo.getGeometryType() + "'. Unale to preform KML Transform.");
}
return kmlGeo;
}
use of ddf.catalog.transform.CatalogTransformerException in project ddf by codice.
the class ZipDecompression method transform.
/**
* Transforms a Zip InputStream into a List of {@link Metacard}s. This method expects there to be a
* filePath and fileName key-value pair passed in the arguments map.
*
* @param inputStream - the InputStream to transform
* @param arguments - the arguments for the transformation ("filePath" and "fileName").
* @return the List of {@link Metacard}s produced from the transformation.
* @throws CatalogTransformerException when the transformation fails.
*/
@Override
public List<Metacard> transform(InputStream inputStream, Map<String, Serializable> arguments) throws CatalogTransformerException {
if (inputStream == null) {
throw new CatalogTransformerException("Unable to transform InputStream : InputStream was null.");
}
if (MapUtils.isEmpty(arguments) || !arguments.containsKey(FILE_PATH) || !arguments.containsKey(FILE_NAME)) {
throw new CatalogTransformerException("Unable to transform InputStream : Invalid arguments passed.");
}
String zipFileName = (String) arguments.get(FILE_PATH);
String zipPath = zipFileName + arguments.get(FILE_NAME);
try {
zipValidator.validateZipFile(zipPath);
} catch (ZipValidationException e) {
throw new CatalogTransformerException("Unable to validate Zip InputStream. Ensure that the zip file is signed properly using jarsigner and the appropriate certificates.", e);
}
Map<String, Metacard> metacards = decompressFile(inputStream, zipFileName);
return metacards.values().stream().filter(Objects::nonNull).collect(Collectors.toList());
}
use of ddf.catalog.transform.CatalogTransformerException in project ddf by codice.
the class InputTransformerProducer method generateMetacard.
private Metacard generateMetacard(MimeType mimeType, MimeTypeToTransformerMapper mapper, InputStream message) throws MetacardCreationException {
LOGGER.trace("ENTERING: generateMetacard");
List<InputTransformer> listOfCandidates = mapper.findMatches(InputTransformer.class, mimeType);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("List of matches for mimeType [{}]: {}", mimeType, listOfCandidates);
}
Metacard generatedMetacard = null;
try (TemporaryFileBackedOutputStream fileBackedOutputStream = new TemporaryFileBackedOutputStream()) {
try {
IOUtils.copy(message, fileBackedOutputStream);
} catch (IOException e) {
throw new MetacardCreationException("Could not copy bytes of content message.", e);
}
// can create the metacard, then do not need to try any remaining InputTransformers.
for (InputTransformer transformer : listOfCandidates) {
try (InputStream inputStreamMessageCopy = fileBackedOutputStream.asByteSource().openStream()) {
generatedMetacard = transformer.transform(inputStreamMessageCopy);
} catch (IOException | CatalogTransformerException e) {
LOGGER.debug("Transformer [" + transformer + "] could not create metacard.", e);
}
if (generatedMetacard != null) {
break;
}
}
if (generatedMetacard == null) {
throw new MetacardCreationException("Could not create metacard with mimeType " + mimeType + ". No valid transformers found.");
}
LOGGER.trace("EXITING: generateMetacard");
} catch (IOException e) {
throw new MetacardCreationException("Could not create metacard.", e);
}
return generatedMetacard;
}
Aggregations