Search in sources :

Example 21 with InputTransformer

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;
}
Also used : ConversionException(com.thoughtworks.xstream.converters.ConversionException) Metacard(ddf.catalog.data.Metacard) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) IOException(java.io.IOException) InputTransformer(ddf.catalog.transform.InputTransformer)

Example 22 with InputTransformer

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;
}
Also used : Metacard(ddf.catalog.data.Metacard) MetacardCreationException(ddf.catalog.data.MetacardCreationException) TemporaryFileBackedOutputStream(org.codice.ddf.platform.util.TemporaryFileBackedOutputStream) InputStream(java.io.InputStream) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) IOException(java.io.IOException) InputTransformer(ddf.catalog.transform.InputTransformer)

Example 23 with InputTransformer

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();
}
Also used : MimeTypeToTransformerMapper(ddf.mime.MimeTypeToTransformerMapper) Metacard(ddf.catalog.data.Metacard) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Hashtable(java.util.Hashtable) InputStream(java.io.InputStream) InputTransformer(ddf.catalog.transform.InputTransformer) MimeType(javax.activation.MimeType) Test(org.junit.Test)

Example 24 with InputTransformer

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);
}
Also used : HashMap(java.util.HashMap) XppReader(com.thoughtworks.xstream.io.xml.XppReader) InputStream(java.io.InputStream) StringReader(java.io.StringReader) HierarchicalStreamReader(com.thoughtworks.xstream.io.HierarchicalStreamReader) UnmarshallingContext(com.thoughtworks.xstream.converters.UnmarshallingContext) Matchers.anyString(org.mockito.Matchers.anyString) InputTransformer(ddf.catalog.transform.InputTransformer) TreeUnmarshaller(com.thoughtworks.xstream.core.TreeUnmarshaller) Test(org.junit.Test)

Example 25 with InputTransformer

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);
}
Also used : StaxDriver(com.thoughtworks.xstream.io.xml.StaxDriver) XppReader(com.thoughtworks.xstream.io.xml.XppReader) InputStream(java.io.InputStream) StringReader(java.io.StringReader) HierarchicalStreamReader(com.thoughtworks.xstream.io.HierarchicalStreamReader) UnmarshallingContext(com.thoughtworks.xstream.converters.UnmarshallingContext) Matchers.anyString(org.mockito.Matchers.anyString) InputTransformer(ddf.catalog.transform.InputTransformer) TreeUnmarshaller(com.thoughtworks.xstream.core.TreeUnmarshaller) Test(org.junit.Test)

Aggregations

InputTransformer (ddf.catalog.transform.InputTransformer)29 InputStream (java.io.InputStream)23 Metacard (ddf.catalog.data.Metacard)17 CatalogTransformerException (ddf.catalog.transform.CatalogTransformerException)15 IOException (java.io.IOException)13 Test (org.junit.Test)12 ByteArrayInputStream (java.io.ByteArrayInputStream)11 ArrayList (java.util.ArrayList)8 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)7 MimeType (javax.activation.MimeType)7 MimeTypeToTransformerMapper (ddf.mime.MimeTypeToTransformerMapper)6 ServiceReference (org.osgi.framework.ServiceReference)5 Result (ddf.catalog.data.Result)4 Matchers.anyString (org.mockito.Matchers.anyString)4 BundleContext (org.osgi.framework.BundleContext)4 UnmarshallingContext (com.thoughtworks.xstream.converters.UnmarshallingContext)3 TreeUnmarshaller (com.thoughtworks.xstream.core.TreeUnmarshaller)3 HierarchicalStreamReader (com.thoughtworks.xstream.io.HierarchicalStreamReader)3 XppReader (com.thoughtworks.xstream.io.xml.XppReader)3 MockMemoryStorageProvider (ddf.catalog.content.impl.MockMemoryStorageProvider)3