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);
}
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);
}
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;
}
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
}
}
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
}
}
Aggregations