Search in sources :

Example 26 with XStream

use of com.thoughtworks.xstream.XStream in project camel by apache.

the class RestErrorTest method shouldDeserializeFromXml.

@Test
public void shouldDeserializeFromXml() {
    final XStream xStream = new XStream();
    xStream.processAnnotations(RestError.class);
    xStream.alias("errors", RestError.class);
    final RestError gotWithErrorCode = (RestError) xStream.fromXML("<errors><fields>field1</fields><fields>field2</fields><message>message</message><errorCode>errorCode</errorCode></errors>");
    assertEquals(gotWithErrorCode, error);
    final RestError gotWithStatusCode = (RestError) xStream.fromXML("<errors><fields>field1</fields><fields>field2</fields><message>message</message><statusCode>errorCode</statusCode></errors>");
    assertEquals(gotWithStatusCode, error);
}
Also used : XStream(com.thoughtworks.xstream.XStream) Test(org.junit.Test)

Example 27 with XStream

use of com.thoughtworks.xstream.XStream in project camel by apache.

the class SObjectTreeTest method shouldSerializeToXml.

@Test
public void shouldSerializeToXml() {
    final SObjectTree tree = new SObjectTree();
    final SObjectNode account1 = new SObjectNode(tree, simpleAccount);
    account1.addChild("Contacts", smith);
    account1.addChild("Contacts", evans);
    tree.addNode(account1);
    final SObjectNode account2 = new SObjectNode(tree, simpleAccount2);
    tree.addNode(account2);
    final XStream xStream = new XStream();
    xStream.processAnnotations(new Class[] { SObjectTree.class, Account.class, Contact.class, Asset.class });
    final String xml = xStream.toXML(tree);
    assertEquals("Should serialize to XML as in Salesforce example", //
    "<SObjectTreeRequest>\n" + //
    "  <records type=\"Account\" referenceId=\"ref1\">\n" + //
    "    <Name>SampleAccount</Name>\n" + //
    "    <Phone>1234567890</Phone>\n" + //
    "    <Website>www.salesforce.com</Website>\n" + //
    "    <Industry>Banking</Industry>\n" + //
    "    <NumberOfEmployees>100</NumberOfEmployees>\n" + //
    "    <Contacts>\n" + //
    "      <records type=\"Contact\" referenceId=\"ref2\">\n" + //
    "        <Email>sample@salesforce.com</Email>\n" + //
    "        <LastName>Smith</LastName>\n" + //
    "        <Title>President</Title>\n" + //
    "      </records>\n" + //
    "      <records type=\"Contact\" referenceId=\"ref3\">\n" + //
    "        <Email>sample@salesforce.com</Email>\n" + //
    "        <LastName>Evans</LastName>\n" + //
    "        <Title>Vice President</Title>\n" + //
    "      </records>\n" + //
    "    </Contacts>\n" + //
    "  </records>\n" + //
    "  <records type=\"Account\" referenceId=\"ref4\">\n" + //
    "    <Name>SampleAccount2</Name>\n" + //
    "    <Phone>1234567890</Phone>\n" + //
    "    <Website>www.salesforce2.com</Website>\n" + //
    "    <Industry>Banking</Industry>\n" + //
    "    <NumberOfEmployees>100</NumberOfEmployees>\n" + //
    "  </records>\n" + "</SObjectTreeRequest>", xml);
}
Also used : XStream(com.thoughtworks.xstream.XStream) Test(org.junit.Test)

Example 28 with XStream

use of com.thoughtworks.xstream.XStream 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 29 with XStream

use of com.thoughtworks.xstream.XStream in project camel by apache.

the class XStreamDataFormatPermissionsTest method testDenyAny.

@Test
public void testDenyAny() {
    XStreamDataFormat xStreamDataFormat = new XStreamDataFormat();
    xStreamDataFormat.setPermissions("-*");
    XStream xStream = xStreamDataFormat.createXStream(context.getClassResolver(), context.getApplicationContextClassLoader());
    try {
        xStream.fromXML(XML_PURCHASE_ORDER);
        fail("should fail to unmarshall");
    } catch (ForbiddenClassException e) {
    // OK
    }
}
Also used : XStream(com.thoughtworks.xstream.XStream) ForbiddenClassException(com.thoughtworks.xstream.security.ForbiddenClassException) Test(org.junit.Test)

Example 30 with XStream

use of com.thoughtworks.xstream.XStream in project camel by apache.

the class XStreamDataFormatPermissionsTest method testAllowAndDeny.

@Test
public void testAllowAndDeny() {
    XStreamDataFormat xStreamDataFormat = new XStreamDataFormat();
    xStreamDataFormat.setPermissions("org.apache.camel.dataformat.xstream.PurchaseOrder,-org.apache.camel.dataformat.xstream.*");
    XStream xStream = xStreamDataFormat.createXStream(context.getClassResolver(), context.getApplicationContextClassLoader());
    try {
        xStream.fromXML(XML_PURCHASE_ORDER);
        fail("should fail to unmarshall");
    } catch (ForbiddenClassException e) {
    // OK
    }
}
Also used : XStream(com.thoughtworks.xstream.XStream) ForbiddenClassException(com.thoughtworks.xstream.security.ForbiddenClassException) Test(org.junit.Test)

Aggregations

XStream (com.thoughtworks.xstream.XStream)183 Test (org.junit.Test)54 IOException (java.io.IOException)31 InputStream (java.io.InputStream)28 WstxDriver (com.thoughtworks.xstream.io.xml.WstxDriver)22 Metacard (ddf.catalog.data.Metacard)21 DomDriver (com.thoughtworks.xstream.io.xml.DomDriver)16 HashMap (java.util.HashMap)14 ByteArrayInputStream (java.io.ByteArrayInputStream)12 Matchers.anyString (org.mockito.Matchers.anyString)11 HierarchicalStreamWriter (com.thoughtworks.xstream.io.HierarchicalStreamWriter)10 CswRecordCollection (org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection)10 GmlGeometryConverter (org.codice.ddf.spatial.ogc.wfs.catalog.converter.impl.GmlGeometryConverter)8 XStreamException (com.thoughtworks.xstream.XStreamException)7 MarshallingContext (com.thoughtworks.xstream.converters.MarshallingContext)7 StaxDriver (com.thoughtworks.xstream.io.xml.StaxDriver)7 FileNotFoundException (java.io.FileNotFoundException)7 ArrayList (java.util.ArrayList)7 ForbiddenClassException (com.thoughtworks.xstream.security.ForbiddenClassException)6 FileWriter (java.io.FileWriter)6