use of ddf.catalog.transform.CatalogTransformerException in project ddf by codice.
the class XmlInputTransformer method transform.
/**
* Takes in an XML {@link InputStream} and returns a populated {@link Metacard} The Metacard is
* populated with all attributes that have been parsed by the {@link SaxEventHandler}s declared in
* {@link XmlInputTransformer#saxEventHandlerConfiguration}s
*
* @param inputStream an XML input stream to be turned into a Metacard
* @return a populated Metacard
* @throws CatalogTransformerException
* @throws IOException
*/
@Override
public Metacard transform(InputStream inputStream) throws CatalogTransformerException {
if (inputStream == null) {
throw new CatalogTransformerException();
}
/*
* Create the necessary new SaxEventHandlerDelegate
*/
SaxEventHandlerDelegate delegate = create();
/*
* Split the input stream, so that we can use it for parsing as well as read it into the Metacard.METADATA attribute
*/
try (OutputStream baos = new ByteArrayOutputStream();
OutputStream outputStream = new BufferedOutputStream(baos);
InputStream teeInputStream = new BufferedInputStream(delegate.getMetadataStream(inputStream, outputStream))) {
/*
* Read the input stream into the metacard - where all the magic happens
*/
Metacard metacard = delegate.read(teeInputStream).getMetacard(id);
/*
* Read the metadata from the split input stream and set it on the Metacard.METADATA attribute.
* However, if the metadata is null or empty, throw an exception - we can't return a metacard
* with no metadata
*/
outputStream.flush();
String metadata = baos.toString();
if (metadata.isEmpty()) {
throw new CatalogTransformerException("Metadata is empty from output stream. Could not properly parse metacard.");
}
metacard.setAttribute(new AttributeImpl(Metacard.METADATA, metadata));
return metacard;
} catch (IOException e) {
LOGGER.debug("IO Exception during parsing", e);
throw new CatalogTransformerException("Could not finish transforming metacard because of IOException", e);
}
}
use of ddf.catalog.transform.CatalogTransformerException in project ddf by codice.
the class CswQueryResponseTransformer method writeAcknowledgement.
private ByteArrayOutputStream writeAcknowledgement(GetRecordsType request) throws CatalogTransformerException {
try {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
JAXBContext jaxBContext = JAXBContext.newInstance("net.opengis.cat.csw.v_2_0_2:" + "net.opengis.filter.v_1_1_0:net.opengis.gml.v_3_1_1:net.opengis.ows.v_1_0_0");
Marshaller marshaller = jaxBContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
AcknowledgementType ack = new AcknowledgementType();
EchoedRequestType echoedRequest = new EchoedRequestType();
JAXBElement<GetRecordsType> jaxBRequest = new ObjectFactory().createGetRecords(request);
echoedRequest.setAny(jaxBRequest);
ack.setEchoedRequest(echoedRequest);
try {
ack.setTimeStamp(DatatypeFactory.newInstance().newXMLGregorianCalendar(new GregorianCalendar()));
} catch (DatatypeConfigurationException e) {
LOGGER.debug("Failed to set timestamp on Acknowledgement", e);
}
JAXBElement<AcknowledgementType> jaxBAck = new ObjectFactory().createAcknowledgement(ack);
marshaller.marshal(jaxBAck, byteArrayOutputStream);
return byteArrayOutputStream;
} catch (JAXBException e) {
throw new CatalogTransformerException(e);
}
}
use of ddf.catalog.transform.CatalogTransformerException in project ddf by codice.
the class CswRecordConverter method transform.
@Override
public BinaryContent transform(Metacard metacard, Map<String, Serializable> arguments) throws CatalogTransformerException {
if (StringUtils.isNotBlank(metacard.getMetadata())) {
// Check if the metadata is csw:Record
try {
StringReader xml = new StringReader(metacard.getMetadata());
XMLEventReader reader = factory.createXMLEventReader(xml);
boolean rootFound = false;
while (reader.hasNext() && !rootFound) {
XMLEvent event = reader.nextEvent();
if (event.isStartElement()) {
rootFound = true;
QName name = event.asStartElement().getName();
if (StringUtils.equals(CswConstants.CSW_RECORD_LOCAL_NAME, name.getLocalPart()) && StringUtils.equals(CswConstants.CSW_OUTPUT_SCHEMA, name.getNamespaceURI())) {
return new BinaryContentImpl(IOUtils.toInputStream(metacard.getMetadata(), StandardCharsets.UTF_8), XML_MIME_TYPE);
}
}
}
} catch (Exception e) {
// Ignore and proceed with the transform.
}
}
StringWriter stringWriter = new StringWriter();
Boolean omitXmlDec = null;
if (arguments != null) {
omitXmlDec = (Boolean) arguments.get(CswConstants.OMIT_XML_DECLARATION);
}
if (omitXmlDec == null || !omitXmlDec) {
stringWriter.append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n");
}
PrettyPrintWriter writer = new PrettyPrintWriter(stringWriter);
MarshallingContext context = new TreeMarshaller(writer, null, null);
context.put(CswConstants.WRITE_NAMESPACES, true);
copyArgumentsToContext(context, arguments);
this.marshal(metacard, writer, context);
BinaryContent transformedContent;
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(stringWriter.toString().getBytes(StandardCharsets.UTF_8));
transformedContent = new BinaryContentImpl(byteArrayInputStream, XML_MIME_TYPE);
return transformedContent;
}
use of ddf.catalog.transform.CatalogTransformerException in project ddf by codice.
the class TestCswSourceBase method setUp.
@Before
public void setUp() {
ServiceReference ref = mock(ServiceReference.class);
ServiceReference[] serviceRefs = new ServiceReference[] { ref };
try {
when(mockContext.getServiceReferences(eq(MetadataTransformer.class.getName()), anyString())).thenReturn(serviceRefs);
} catch (InvalidSyntaxException e) {
LOGGER.error(e.getMessage(), e);
}
MetadataTransformer transformer = mock(MetadataTransformer.class);
// Just return same Metacard that was passed in
when(mockContext.getService(any(ServiceReference.class))).thenReturn(transformer);
try {
when(transformer.transform(any(Metacard.class))).thenAnswer(invocation -> invocation.getArguments()[0]);
} catch (CatalogTransformerException e) {
LOGGER.error(e.getMessage(), e);
}
when(mockAvailabilityTask.isAvailable()).thenReturn(true);
}
use of ddf.catalog.transform.CatalogTransformerException in project ddf by codice.
the class GeoJsonInputTransformer method getMetacard.
private MetacardImpl getMetacard(String propertyTypeName, Map<String, Object> properties) throws CatalogTransformerException {
if (isEmpty(propertyTypeName) || metacardTypes == null) {
LOGGER.debug("MetacardType specified in input is null or empty. Trying all transformers in order...");
Optional<MetacardImpl> first = inputTransformers == null ? Optional.of(new MetacardImpl()) : inputTransformers.stream().map(service -> tryTransformers(properties, service)).filter(Objects::nonNull).findFirst();
return first.orElse(new MetacardImpl());
} else {
MetacardType metacardType = metacardTypes.stream().filter(type -> type.getName().equals(propertyTypeName)).findFirst().orElseThrow(() -> new CatalogTransformerException("MetacardType specified in input has not been registered with the system." + " Cannot parse input. MetacardType name: " + propertyTypeName));
LOGGER.debug("Found registered MetacardType: {}", propertyTypeName);
return new MetacardImpl(metacardType);
}
}
Aggregations