use of ddf.catalog.transform.InputTransformer in project ddf by codice.
the class CswTransformProvider method unmarshal.
/**
* Creates a Metacard from the given XML. This method is not typically be called directly, instead it is
* called by another XStream Converter using UnmarshallingContext.convertAnother();
*
* @param reader
* @param context
* @return
*/
@Override
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
String keyArg = (String) context.get(CswConstants.TRANSFORMER_LOOKUP_KEY);
String valArg = (String) context.get(CswConstants.TRANSFORMER_LOOKUP_VALUE);
InputTransformer transformer;
if (StringUtils.isNotBlank(keyArg) && StringUtils.isNotBlank(valArg)) {
transformer = inputTransformerManager.getTransformerByProperty(keyArg, valArg);
} else {
transformer = inputTransformerManager.getTransformerBySchema(CswConstants.CSW_OUTPUT_SCHEMA);
}
if (transformer == null) {
throw new ConversionException(String.format("Unable to locate a transformer for %s = %s", keyArg, valArg));
}
/* The CswRecordConverter uses unmarshal, which requires the context */
if (transformer instanceof CswRecordConverter) {
return ((CswRecordConverter) transformer).unmarshal(reader, context);
}
Metacard metacard;
try (InputStream is = readXml(reader, context)) {
InputStream inputStream = is;
if (LOGGER.isDebugEnabled()) {
String originalInputStream = IOUtils.toString(inputStream, StandardCharsets.UTF_8.name());
LOGGER.debug("About to transform\n{}", originalInputStream);
inputStream = new ByteArrayInputStream(originalInputStream.getBytes(StandardCharsets.UTF_8.name()));
}
metacard = transformer.transform(inputStream);
} catch (IOException | CatalogTransformerException e) {
throw new ConversionException("Unable to transform Metacard", e);
}
return metacard;
}
use of ddf.catalog.transform.InputTransformer 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;
}
use of ddf.catalog.transform.InputTransformer in project ddf by codice.
the class CatalogComponentTest method testTransformMetacard.
@Test
public void testTransformMetacard() throws Exception {
LOGGER.debug("Running testTransformMetacard()");
MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedMinimumMessageCount(1);
// Mock a XML InputTransformer and register it in the OSGi Registry
// (PojoSR)
InputTransformer mockTransformer = getMockInputTransformer();
Hashtable<String, String> props = new Hashtable<String, String>();
props.put(MimeTypeToTransformerMapper.ID_KEY, "xml");
props.put(MimeTypeToTransformerMapper.MIME_TYPE_KEY, "text/xml");
bundleContext.registerService(InputTransformer.class.getName(), mockTransformer, props);
// Mock the MimeTypeToTransformerMapper and register it in the OSGi
// Registry (PojoSR)
MimeTypeToTransformerMapper matchingService = mock(MimeTypeToTransformerMapper.class);
// HUGH bundleContext.registerService(
// MimeTypeToTransformerMapper.class.getName(), matchingService, null );
catalogComponent.setMimeTypeToTransformerMapper(matchingService);
// Mock the MimeTypeToTransformerMapper returning the mock XML
// InputTransformer
when(matchingService.findMatches(eq(InputTransformer.class), isA(MimeType.class))).thenReturn((List) Arrays.asList(mockTransformer));
// Send in sample XML as InputStream to InputTransformer
InputStream input = IOUtils.toInputStream(xmlInput);
// Get the InputTransformer registered with the ID associated with the
// <from> node in the Camel route
InputTransformer transformer = getTransformer("text/xml", "identity");
assertNotNull("InputTransformer for mimeType=text/xml&id=identity not found", transformer);
// Transform the XML input into a Metacard
Metacard metacard = transformer.transform(input);
assertNotNull(metacard);
assertMockEndpointsSatisfied();
}
use of ddf.catalog.transform.InputTransformer in project ddf by codice.
the class TestCswTransformProvider method testUnmarshalMissingNamespaces.
@Test
public void testUnmarshalMissingNamespaces() throws Exception {
InputTransformer mockInputTransformer = mock(InputTransformer.class);
when(mockInputManager.getTransformerBySchema(anyString())).thenReturn(mockInputTransformer);
Map<String, String> namespaces = new HashMap<>();
namespaces.put("xmlns:csw", "http://www.opengis.net/cat/csw/2.0.2");
namespaces.put("xmlns:dc", "http://purl.org/dc/elements/1.1/");
namespaces.put("xmlns:dct", "http://purl.org/dc/terms/");
HierarchicalStreamReader reader = new XppReader(new StringReader(getRecordMissingNamespaces()), XmlPullParserFactory.newInstance().newPullParser());
CswTransformProvider provider = new CswTransformProvider(null, mockInputManager);
UnmarshallingContext context = new TreeUnmarshaller(null, null, null, null);
context.put(CswConstants.NAMESPACE_DECLARATIONS, namespaces);
context.put(CswConstants.OUTPUT_SCHEMA_PARAMETER, OTHER_SCHEMA);
ArgumentCaptor<InputStream> captor = ArgumentCaptor.forClass(InputStream.class);
provider.unmarshal(reader, context);
// Verify the context arguments were set correctly
verify(mockInputTransformer, times(1)).transform(captor.capture());
InputStream inStream = captor.getValue();
String result = IOUtils.toString(inStream);
XMLUnit.setIgnoreWhitespace(true);
XMLAssert.assertXMLEqual(getRecord(), result);
}
use of ddf.catalog.transform.InputTransformer in project ddf by codice.
the class TestCswTransformProvider method testUnmarshalCopyPreservesNamespaces.
@Test
public void testUnmarshalCopyPreservesNamespaces() throws Exception {
InputTransformer mockInputTransformer = mock(InputTransformer.class);
when(mockInputManager.getTransformerBySchema(anyString())).thenReturn(mockInputTransformer);
StaxDriver driver = new StaxDriver();
driver.setRepairingNamespace(true);
driver.getQnameMap().setDefaultNamespace(CswConstants.CSW_OUTPUT_SCHEMA);
driver.getQnameMap().setDefaultPrefix(CswConstants.CSW_NAMESPACE_PREFIX);
// Have to use XppReader in order to preserve the namespaces.
HierarchicalStreamReader reader = new XppReader(new StringReader(getRecord()), XmlPullParserFactory.newInstance().newPullParser());
CswTransformProvider provider = new CswTransformProvider(null, mockInputManager);
UnmarshallingContext context = new TreeUnmarshaller(null, null, null, null);
context.put(CswConstants.OUTPUT_SCHEMA_PARAMETER, "http://example.com/schema");
ArgumentCaptor<InputStream> captor = ArgumentCaptor.forClass(InputStream.class);
provider.unmarshal(reader, context);
// Verify the context arguments were set correctly
verify(mockInputTransformer, times(1)).transform(captor.capture());
InputStream inStream = captor.getValue();
String result = IOUtils.toString(inStream);
XMLUnit.setIgnoreWhitespace(true);
XMLAssert.assertXMLEqual(getRecord(), result);
}
Aggregations