use of ddf.catalog.transform.CatalogTransformerException in project ddf by codice.
the class CsvTransformer method writeMetacardsToCsv.
public static Appendable writeMetacardsToCsv(final List<Metacard> metacards, final List<AttributeDescriptor> orderedAttributeDescriptors, final Map<String, String> aliasMap) throws CatalogTransformerException {
StringBuilder stringBuilder = new StringBuilder();
try {
CSVPrinter csvPrinter = new CSVPrinter(stringBuilder, CSVFormat.RFC4180);
printColumnHeaders(csvPrinter, orderedAttributeDescriptors, aliasMap);
metacards.forEach(metacard -> printMetacard(csvPrinter, metacard, orderedAttributeDescriptors));
return csvPrinter.getOut();
} catch (IOException ioe) {
throw new CatalogTransformerException(ioe);
}
}
use of ddf.catalog.transform.CatalogTransformerException in project ddf by codice.
the class ZipCompression method createZip.
private InputStream createZip(SourceResponse sourceResponse, String transformerId, Map<String, Serializable> arguments) throws CatalogTransformerException {
ServiceReference<MetacardTransformer> serviceRef = getTransformerServiceReference(transformerId);
MetacardTransformer transformer = bundleContext.getService(serviceRef);
String extension = getFileExtensionFromService(serviceRef);
if (StringUtils.isNotBlank(extension)) {
extension = "." + extension;
}
try (FileBackedOutputStream fileBackedOutputStream = new FileBackedOutputStream(BUFFER_SIZE);
ZipOutputStream zipOutputStream = new ZipOutputStream(fileBackedOutputStream)) {
for (Result result : sourceResponse.getResults()) {
Metacard metacard = result.getMetacard();
BinaryContent binaryContent = getTransformedMetacard(metacard, arguments, transformer);
if (binaryContent != null) {
ZipEntry entry = new ZipEntry(METACARD_PATH + metacard.getId() + extension);
zipOutputStream.putNextEntry(entry);
zipOutputStream.write(binaryContent.getByteArray());
zipOutputStream.closeEntry();
} else {
LOGGER.debug("Metacard with id [{}] was not added to zip file", metacard.getId());
}
}
return fileBackedOutputStream.asByteSource().openStream();
} catch (IOException e) {
throw new CatalogTransformerException("An error occurred while initializing or closing output stream", e);
}
}
use of ddf.catalog.transform.CatalogTransformerException in project ddf by codice.
the class GeometryAdapter method marshalFrom.
public static GeometryElement marshalFrom(Attribute attribute) throws CatalogTransformerException {
GeometryElement element = new GeometryElement();
element.setName(attribute.getName());
if (attribute.getValue() != null) {
for (Serializable value : attribute.getValues()) {
if (!(value instanceof String)) {
continue;
}
String wkt = (String) value;
WKTReader wktReader = new WKTReader(geometryFactory);
Geometry jtsGeometry = null;
try {
jtsGeometry = wktReader.read(wkt);
} catch (ParseException e) {
throw new CatalogTransformerException("Could not transform Metacard to XML. Invalid WKT.", e);
}
JTSToGML311GeometryConverter converter = new JTSToGML311GeometryConverter();
@SuppressWarnings("unchecked") JAXBElement<AbstractGeometryType> gmlElement = (JAXBElement<AbstractGeometryType>) converter.createElement(jtsGeometry);
GeometryElement.Value geoValue = new GeometryElement.Value();
geoValue.setGeometry(gmlElement);
element.getValue().add(geoValue);
}
}
return element;
}
use of ddf.catalog.transform.CatalogTransformerException in project ddf by codice.
the class StringxmlAdapter method marshalFrom.
/**
* @param attribute
* @return JAXB representable attribute
* @throws CatalogTransformerException
*/
public static StringxmlElement marshalFrom(Attribute attribute) throws CatalogTransformerException {
StringxmlElement element = new StringxmlElement();
element.setName(attribute.getName());
if (attribute.getValue() != null) {
for (Serializable value : attribute.getValues()) {
if (!(value instanceof String)) {
continue;
}
String xmlString = (String) value;
Element anyElement = null;
DocumentBuilder builder = null;
try {
synchronized (FACTORY) {
builder = FACTORY.newDocumentBuilder();
builder.setErrorHandler(null);
}
anyElement = builder.parse(new ByteArrayInputStream(xmlString.getBytes(StandardCharsets.UTF_8))).getDocumentElement();
} catch (ParserConfigurationException | SAXException | IOException e) {
throw new CatalogTransformerException(TRANSFORMATION_FAILED_ERROR_MESSAGE, e);
}
Value anyValue = new StringxmlElement.Value();
anyValue.setAny(anyElement);
element.getValue().add(anyValue);
}
}
return element;
}
use of ddf.catalog.transform.CatalogTransformerException in project ddf by codice.
the class CswQueryResponseTransformerTest method testMarshalAcknowledgementWithFailedTransforms.
@Test
public void testMarshalAcknowledgementWithFailedTransforms() throws WebApplicationException, IOException, JAXBException, CatalogTransformerException {
GetRecordsType query = new GetRecordsType();
query.setResultType(ResultType.RESULTS);
query.setMaxRecords(BigInteger.valueOf(6));
query.setStartPosition(BigInteger.valueOf(0));
SourceResponse sourceResponse = createSourceResponse(query, 6);
Map<String, Serializable> args = new HashMap<>();
args.put(CswConstants.RESULT_TYPE_PARAMETER, ResultType.RESULTS);
args.put(CswConstants.GET_RECORDS, query);
PrintWriter printWriter = getSimplePrintWriter();
MetacardTransformer mockMetacardTransformer = mock(MetacardTransformer.class);
final AtomicLong atomicLong = new AtomicLong(0);
when(mockMetacardTransformer.transform(any(Metacard.class), anyMap())).then(invocationOnMock -> {
if (atomicLong.incrementAndGet() == 2) {
throw new CatalogTransformerException("");
}
Metacard metacard = (Metacard) invocationOnMock.getArguments()[0];
BinaryContentImpl bci = new BinaryContentImpl(IOUtils.toInputStream(metacard.getId() + ",", StandardCharsets.UTF_8), new MimeType("application/xml"));
return bci;
});
when(mockPrintWriterProvider.build((Class<Metacard>) notNull())).thenReturn(printWriter);
when(mockTransformerManager.getTransformerBySchema(anyString())).thenReturn(mockMetacardTransformer);
CswQueryResponseTransformer cswQueryResponseTransformer = new CswQueryResponseTransformer(mockTransformerManager, mockPrintWriterProvider);
cswQueryResponseTransformer.init();
BinaryContent content = cswQueryResponseTransformer.transform(sourceResponse, args);
cswQueryResponseTransformer.destroy();
String xml = new String(content.getByteArray());
assertThat(xml, containsString(CswQueryResponseTransformer.NUMBER_OF_RECORDS_MATCHED_ATTRIBUTE + " 6"));
assertThat(xml, containsString(CswQueryResponseTransformer.NUMBER_OF_RECORDS_RETURNED_ATTRIBUTE + " 5"));
assertThat(xml, containsString(CswQueryResponseTransformer.NEXT_RECORD_ATTRIBUTE + " 0"));
}
Aggregations