Search in sources :

Example 1 with Converter

use of com.thoughtworks.xstream.converters.Converter in project camel by apache.

the class RichInputConverter method marshal.

@Override
public void marshal(final Object source, final HierarchicalStreamWriter writer, final MarshallingContext context) {
    if (source instanceof Map) {
        @SuppressWarnings("unchecked") final Map<String, String> map = (Map) source;
        for (final Map.Entry<String, String> e : map.entrySet()) {
            writer.startNode(e.getKey());
            writer.setValue(e.getValue());
            writer.endNode();
        }
    } else {
        final Class<?> clazz = source.getClass();
        writer.startNode(clazz.getSimpleName());
        final Converter converter = converterLookup.lookupConverterForType(source.getClass());
        converter.marshal(source, writer, context);
        writer.endNode();
    }
}
Also used : Converter(com.thoughtworks.xstream.converters.Converter) Map(java.util.Map)

Example 2 with Converter

use of com.thoughtworks.xstream.converters.Converter in project camel by apache.

the class AbstractXStreamWrapper method createXStream.

protected XStream createXStream(ClassResolver resolver, ClassLoader classLoader) {
    if (xstreamDriver != null) {
        xstream = new XStream(xstreamDriver);
    } else {
        xstream = new XStream();
    }
    if (mode != null) {
        xstream.setMode(getModeFromString(mode));
    }
    ClassLoader xstreamLoader = xstream.getClassLoader();
    if (classLoader != null && xstreamLoader instanceof CompositeClassLoader) {
        ((CompositeClassLoader) xstreamLoader).add(classLoader);
    }
    try {
        if (this.implicitCollections != null) {
            for (Entry<String, String[]> entry : this.implicitCollections.entrySet()) {
                for (String name : entry.getValue()) {
                    xstream.addImplicitCollection(resolver.resolveMandatoryClass(entry.getKey()), name);
                }
            }
        }
        if (this.aliases != null) {
            for (Entry<String, String> entry : this.aliases.entrySet()) {
                xstream.alias(entry.getKey(), resolver.resolveMandatoryClass(entry.getValue()));
                // It can turn the auto-detection mode off
                xstream.processAnnotations(resolver.resolveMandatoryClass(entry.getValue()));
            }
        }
        if (this.omitFields != null) {
            for (Entry<String, String[]> entry : this.omitFields.entrySet()) {
                for (String name : entry.getValue()) {
                    xstream.omitField(resolver.resolveMandatoryClass(entry.getKey()), name);
                }
            }
        }
        if (this.converters != null) {
            for (String name : this.converters) {
                Class<Converter> converterClass = resolver.resolveMandatoryClass(name, Converter.class);
                Converter converter;
                Constructor<Converter> con = null;
                try {
                    con = converterClass.getDeclaredConstructor(new Class[] { XStream.class });
                } catch (Exception e) {
                //swallow as we null check in a moment.
                }
                if (con != null) {
                    converter = con.newInstance(xstream);
                } else {
                    converter = converterClass.newInstance();
                    try {
                        Method method = converterClass.getMethod("setXStream", new Class[] { XStream.class });
                        if (method != null) {
                            ObjectHelper.invokeMethod(method, converter, xstream);
                        }
                    } catch (Throwable e) {
                    // swallow, as it just means the user never add an XStream setter, which is optional
                    }
                }
                xstream.registerConverter(converter);
            }
        }
        addDefaultPermissions(xstream);
        if (this.permissions != null) {
            // permissions ::= pterm (',' pterm)*   # consits of one or more terms
            // pterm       ::= aod? wterm           # each term preceded by an optional sign 
            // aod         ::= '+' | '-'            # indicates allow or deny where allow if omitted
            // wterm       ::= a class name with optional wildcard characters
            addPermissions(xstream, permissions);
        }
    } catch (Exception e) {
        throw new RuntimeException("Unable to build XStream instance", e);
    }
    return xstream;
}
Also used : XStream(com.thoughtworks.xstream.XStream) Method(java.lang.reflect.Method) XMLStreamException(javax.xml.stream.XMLStreamException) CompositeClassLoader(com.thoughtworks.xstream.core.util.CompositeClassLoader) StaxConverter(org.apache.camel.converter.jaxp.StaxConverter) Converter(com.thoughtworks.xstream.converters.Converter) CompositeClassLoader(com.thoughtworks.xstream.core.util.CompositeClassLoader)

Example 3 with Converter

use of com.thoughtworks.xstream.converters.Converter in project ddf by codice.

the class TestTransactionMessageBodyReader method testReadInsertFrom.

@Test
public void testReadInsertFrom() throws Exception {
    Converter mockConverter = mock(Converter.class);
    when(mockConverter.canConvert(any(Metacard.class.getClass()))).thenReturn(true);
    when(mockConverter.unmarshal(any(HierarchicalStreamReader.class), any(UnmarshallingContext.class))).thenReturn(mock(Metacard.class));
    TransactionMessageBodyReader reader = new TransactionMessageBodyReader(mockConverter, CswQueryFactoryTest.getCswMetacardType(), registry);
    CswTransactionRequest request = reader.readFrom(CswTransactionRequest.class, null, null, null, null, IOUtils.toInputStream(getInsertRequest(COUNT)));
    assertThat(request, notNullValue());
    assertThat(request.getInsertActions().size(), is(1));
    assertThat(request.getDeleteActions().size(), is(0));
    assertThat(request.getUpdateActions().size(), is(0));
    InsertAction insertAction = request.getInsertActions().get(0);
    assertThat(insertAction, notNullValue());
    assertThat(insertAction.getRecords().size(), is(COUNT));
    assertThat(request.getService(), is(CswConstants.CSW));
    assertThat(request.getVersion(), is(CswConstants.VERSION_2_0_2));
    assertThat(request.isVerbose(), is(true));
}
Also used : Metacard(ddf.catalog.data.Metacard) CswTransactionRequest(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.CswTransactionRequest) InsertAction(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.InsertAction) Converter(com.thoughtworks.xstream.converters.Converter) CswRecordConverter(org.codice.ddf.spatial.ogc.csw.catalog.converter.CswRecordConverter) HierarchicalStreamReader(com.thoughtworks.xstream.io.HierarchicalStreamReader) UnmarshallingContext(com.thoughtworks.xstream.converters.UnmarshallingContext) CswQueryFactoryTest(org.codice.ddf.spatial.ogc.csw.catalog.endpoint.CswQueryFactoryTest) Test(org.junit.Test)

Example 4 with Converter

use of com.thoughtworks.xstream.converters.Converter in project ddf by codice.

the class TestTransactionMessageBodyReader method testReadInsertAndDeleteFrom.

@Test
public void testReadInsertAndDeleteFrom() throws IOException {
    Converter mockConverter = mock(Converter.class);
    when(mockConverter.canConvert(any(Metacard.class.getClass()))).thenReturn(true);
    when(mockConverter.unmarshal(any(HierarchicalStreamReader.class), any(UnmarshallingContext.class))).thenReturn(mock(Metacard.class));
    TransactionMessageBodyReader reader = new TransactionMessageBodyReader(mockConverter, CswQueryFactoryTest.getCswMetacardType(), registry);
    CswTransactionRequest request = reader.readFrom(CswTransactionRequest.class, null, null, null, null, IOUtils.toInputStream(INSERT_AND_DELETE_REQUEST_XML));
    assertThat(request, notNullValue());
    assertThat(request.getDeleteActions().size(), is(1));
    assertThat(request.getInsertActions().size(), is(1));
    assertThat(request.getUpdateActions().size(), is(0));
    DeleteAction deleteAction = request.getDeleteActions().get(0);
    assertThat(deleteAction, notNullValue());
    assertThat(deleteAction.getTypeName(), is(CswConstants.CSW_RECORD));
    assertThat(deleteAction.getHandle(), is("something"));
    assertThat(deleteAction.getConstraint(), notNullValue());
    assertThat(deleteAction.getConstraint().getCqlText().trim(), is("title = 'foo'"));
    InsertAction insertAction = request.getInsertActions().get(0);
    assertThat(insertAction, notNullValue());
    assertThat(insertAction.getRecords().size(), is(1));
    assertThat(request.getService(), is(CswConstants.CSW));
    assertThat(request.getVersion(), is(CswConstants.VERSION_2_0_2));
    assertThat(request.isVerbose(), is(true));
}
Also used : Metacard(ddf.catalog.data.Metacard) CswTransactionRequest(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.CswTransactionRequest) InsertAction(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.InsertAction) Converter(com.thoughtworks.xstream.converters.Converter) CswRecordConverter(org.codice.ddf.spatial.ogc.csw.catalog.converter.CswRecordConverter) HierarchicalStreamReader(com.thoughtworks.xstream.io.HierarchicalStreamReader) DeleteAction(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.DeleteAction) UnmarshallingContext(com.thoughtworks.xstream.converters.UnmarshallingContext) CswQueryFactoryTest(org.codice.ddf.spatial.ogc.csw.catalog.endpoint.CswQueryFactoryTest) Test(org.junit.Test)

Aggregations

Converter (com.thoughtworks.xstream.converters.Converter)4 UnmarshallingContext (com.thoughtworks.xstream.converters.UnmarshallingContext)2 HierarchicalStreamReader (com.thoughtworks.xstream.io.HierarchicalStreamReader)2 Metacard (ddf.catalog.data.Metacard)2 CswTransactionRequest (org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.CswTransactionRequest)2 InsertAction (org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.InsertAction)2 CswRecordConverter (org.codice.ddf.spatial.ogc.csw.catalog.converter.CswRecordConverter)2 CswQueryFactoryTest (org.codice.ddf.spatial.ogc.csw.catalog.endpoint.CswQueryFactoryTest)2 Test (org.junit.Test)2 XStream (com.thoughtworks.xstream.XStream)1 CompositeClassLoader (com.thoughtworks.xstream.core.util.CompositeClassLoader)1 Method (java.lang.reflect.Method)1 Map (java.util.Map)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 StaxConverter (org.apache.camel.converter.jaxp.StaxConverter)1 DeleteAction (org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.DeleteAction)1