Skip to content

Commit

Permalink
[CAMEL-9297] Expose more configuration options from Camel's XStream. …
Browse files Browse the repository at this point in the history
…Allow to configure the types/permissions in the Java DSL. Updated the unit tests to do it like the end user would do it.

Conflicts:
	camel-core/src/main/java/org/apache/camel/builder/DataFormatClause.java
	components/camel-xstream/src/test/java/org/apache/camel/dataformat/xstream/MarshalDomainObjectJSONTest.java
	components/camel-xstream/src/test/resources/org/apache/camel/dataformat/xstream/SpringMarshalDomainObjectJSONTest.xml
  • Loading branch information
davsclaus committed Nov 13, 2015
1 parent 933a245 commit 4cdc6b1
Show file tree
Hide file tree
Showing 15 changed files with 156 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@
*/
package org.apache.camel.builder;

import java.nio.charset.Charset;
import java.util.Map;
import java.util.zip.Deflater;

import org.w3c.dom.Node;

import org.apache.camel.model.DataFormatDefinition;
import org.apache.camel.model.ProcessorDefinition;
import org.apache.camel.model.dataformat.AvroDataFormat;
Expand Down Expand Up @@ -52,7 +51,9 @@
import org.apache.camel.model.dataformat.XmlJsonDataFormat;
import org.apache.camel.model.dataformat.ZipDataFormat;
import org.apache.camel.model.dataformat.ZipFileDataFormat;
import org.apache.camel.util.CollectionStringBuffer;
import org.apache.camel.util.jsse.KeyStoreParameters;
import org.w3c.dom.Node;

/**
* An expression for constructing the different possible {@link org.apache.camel.spi.DataFormat}
Expand Down Expand Up @@ -531,17 +532,60 @@ public T tidyMarkup() {
}

/**
* Uses the XStream data format
* Uses the XStream data format.
* <p/>
* Favor using {@link #xstream(String)} to pass in a permission
*/
public T xstream() {
return dataFormat(new XStreamDataFormat());
}

/**
* Uses the xstream by setting the encoding or permission
*
* @param encodingOrPermission is either an encoding or permission syntax
*/
public T xstream(String encodingOrPermission) {
// is it an encoding? if not we assume its a permission
if (Charset.isSupported(encodingOrPermission)) {
return xstream(encodingOrPermission, (String) null);
} else {
return xstream(null, encodingOrPermission);
}
}

/**
* Uses the xstream by setting the encoding
*/
public T xstream(String encoding) {
return dataFormat(new XStreamDataFormat(encoding));
public T xstream(String encoding, String permission) {
XStreamDataFormat xdf = new XStreamDataFormat();
xdf.setPermissions(permission);
xdf.setEncoding(encoding);
return dataFormat(xdf);
}

/**
* Uses the xstream by permitting the java type
*
* @param type the pojo xstream should use as allowed permission
*/
public T xstream(Class<?> type) {
return xstream(null, type);
}

/**
* Uses the xstream by permitting the java type
*
* @param encoding encoding to use
* @param type the pojo class(es) xstream should use as allowed permission
*/
public T xstream(String encoding, Class<?>... type) {
CollectionStringBuffer csb = new CollectionStringBuffer(",");
for (Class<?> clazz : type) {
csb.append("+");
csb.append(clazz.getName());
}
return xstream(encoding, csb.toString());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.apache.camel.spi.DataFormat;
import org.apache.camel.spi.Metadata;
import org.apache.camel.spi.RouteContext;
import org.apache.camel.util.CollectionStringBuffer;
import org.apache.camel.util.ObjectHelper;

/**
Expand Down Expand Up @@ -68,6 +69,8 @@ public class JsonDataFormat extends DataFormatDefinition {
private String enableFeatures;
@XmlAttribute
private String disableFeatures;
@XmlAttribute
private String permissions;

public JsonDataFormat() {
super("json");
Expand Down Expand Up @@ -253,6 +256,42 @@ public void setDisableFeatures(String disableFeatures) {
this.disableFeatures = disableFeatures;
}

public String getPermissions() {
return permissions;
}

/**
* Adds permissions that controls which Java packages and classes XStream is allowed to use during
* unmarshal from xml/json to Java beans.
* <p/>
* A permission must be configured either here or globally using a JVM system property. The permission
* can be specified in a syntax where a plus sign is allow, and minus sign is deny.
* <br/>
* Wildcards is supported by using <tt>.*</tt> as prefix. For example to allow <tt>com.foo</tt> and all subpackages
* then specfy <tt>+com.foo.*</tt>. Multiple permissions can be configured separated by comma, such as
* <tt>+com.foo.*,-com.foo.bar.MySecretBean</tt>.
* <br/>
* The following default permission is always included: <tt>"-*,java.lang.*,java.util.*"</tt> unless
* its overridden by specifying a JVM system property with they key <tt>org.apache.camel.xstream.permissions</tt>.
*/
public void setPermissions(String permissions) {
this.permissions = permissions;
}

/**
* To add permission for the given pojo classes.
* @param type the pojo class(es) xstream should use as allowed permission
* @see #setPermissions(String)
*/
public void setPermissions(Class<?>... type) {
CollectionStringBuffer csb = new CollectionStringBuffer(",");
for (Class<?> clazz : type) {
csb.append("+");
csb.append(clazz.getName());
}
setPermissions(csb.toString());
}

@Override
public String getDataFormatName() {
// json data format is special as the name can be from different bundles
Expand Down Expand Up @@ -325,6 +364,14 @@ protected void configureDataFormat(DataFormat dataFormat, CamelContext camelCont
if (disableFeatures != null) {
setProperty(camelContext, dataFormat, "disableFeatures", disableFeatures);
}
if (permissions != null) {
setProperty(camelContext, dataFormat, "permissions", permissions);
}
// if we have the unmarshal type, but no permission set, then use it to be allowed
if (permissions == null && unmarshalType != null) {
String allow = "+" + unmarshalType.getName();
setProperty(camelContext, dataFormat, "permissions", allow);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.apache.camel.spi.Metadata;
import org.apache.camel.spi.RouteContext;
import org.apache.camel.util.CamelContextHelper;
import org.apache.camel.util.CollectionStringBuffer;
import org.apache.camel.util.ObjectHelper;

/**
Expand All @@ -49,6 +50,8 @@
@XmlRootElement(name = "xstream")
@XmlAccessorType(XmlAccessType.NONE)
public class XStreamDataFormat extends DataFormatDefinition {
@XmlAttribute
private String permissions;
@XmlAttribute
private String encoding;
@XmlAttribute
Expand All @@ -57,9 +60,7 @@ public class XStreamDataFormat extends DataFormatDefinition {
private String driverRef;
@XmlAttribute
private String mode;
@XmlAttribute
private String permissions;


@XmlJavaTypeAdapter(ConvertersAdapter.class)
@XmlElement(name = "converters")
private List<String> converters;
Expand Down Expand Up @@ -187,12 +188,37 @@ public String getPermissions() {
}

/**
* Adds permissionsList
* Adds permissions that controls which Java packages and classes XStream is allowed to use during
* unmarshal from xml/json to Java beans.
* <p/>
* A permission must be configured either here or globally using a JVM system property. The permission
* can be specified in a syntax where a plus sign is allow, and minus sign is deny.
* <br/>
* Wildcards is supported by using <tt>.*</tt> as prefix. For example to allow <tt>com.foo</tt> and all subpackages
* then specfy <tt>+com.foo.*</tt>. Multiple permissions can be configured separated by comma, such as
* <tt>+com.foo.*,-com.foo.bar.MySecretBean</tt>.
* <br/>
* The following default permission is always included: <tt>"-*,java.lang.*,java.util.*"</tt> unless
* its overridden by specifying a JVM system property with they key <tt>org.apache.camel.xstream.permissions</tt>.
*/
public void setPermissions(String permissions) {
this.permissions = permissions;
}

/**
* To add permission for the given pojo classes.
* @param type the pojo class(es) xstream should use as allowed permission
* @see #setPermissions(String)
*/
public void setPermissions(Class<?>... type) {
CollectionStringBuffer csb = new CollectionStringBuffer(",");
for (Class<?> clazz : type) {
csb.append("+");
csb.append(clazz.getName());
}
setPermissions(csb.toString());
}

@Override
protected DataFormat createDataFormat(RouteContext routeContext) {
if ("json".equals(this.driver)) {
Expand All @@ -208,6 +234,9 @@ protected DataFormat createDataFormat(RouteContext routeContext) {

@Override
protected void configureDataFormat(DataFormat dataFormat, CamelContext camelContext) {
if (this.permissions != null) {
setProperty(camelContext, dataFormat, "permissions", this.permissions);
}
if (encoding != null) {
setProperty(camelContext, dataFormat, "encoding", encoding);
}
Expand All @@ -223,9 +252,6 @@ protected void configureDataFormat(DataFormat dataFormat, CamelContext camelCont
if (this.implicitCollections != null) {
setProperty(camelContext, dataFormat, "implicitCollections", this.implicitCollections);
}
if (this.permissions != null) {
setProperty(camelContext, dataFormat, "permissions", this.permissions);
}
if (this.mode != null) {
setProperty(camelContext, dataFormat, "mode", mode);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.apache.camel.dataformat.xstream;

import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.model.dataformat.JsonLibrary;

public class MarshalDomainObjectJSONTest extends MarshalDomainObjectTest {

Expand All @@ -28,7 +29,10 @@ public void configure() throws Exception {
// just used for helping to marhsal
from("direct:marshal").marshal().json();

from("direct:reverse").unmarshal().json().to("mock:reverse");
from("direct:reverse").unmarshal().json(JsonLibrary.XStream, PurchaseOrder.class).to("mock:reverse");

from("direct:inPretty").marshal().json(true);
from("direct:backPretty").unmarshal().json(JsonLibrary.XStream, PurchaseOrder.class, true).to("mock:reverse");
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,13 @@
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

/**
* Marshal tests with domain objects.
*/
public class MarshalDomainObjectTest extends CamelTestSupport {

@BeforeClass
public static void setup() {
XStreamTestUtils.setPermissionSystemProperty("");
}

@AfterClass
public static void cleanup() {
XStreamTestUtils.revertPermissionSystemProperty();
}

@Test
public void testMarshalDomainObject() throws Exception {
MockEndpoint mock = getMockEndpoint("mock:result");
Expand Down Expand Up @@ -96,12 +84,12 @@ public void testMarshalAndUnmarshal() throws Exception {
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
public void configure() throws Exception {
from("direct:in").marshal().xstream().to("mock:result");
from("direct:in").marshal().xstream(PurchaseOrder.class).to("mock:result");

// just used for helping to marshal
from("direct:marshal").marshal().xstream("UTF-8");
from("direct:marshal").marshal().xstream("UTF-8", PurchaseOrder.class);

from("direct:reverse").unmarshal().xstream("UTF-8").to("mock:reverse");
from("direct:reverse").unmarshal().xstream("UTF-8", PurchaseOrder.class).to("mock:reverse");
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,20 @@
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.model.dataformat.JsonLibrary;

public class UnmarshalThenMarshalJSONTest extends UnmarshalThenMarshalTest {
protected RouteBuilder createRouteBuilder() {
return new RouteBuilder() {
public void configure() {
from("direct:start").
marshal().json().
marshal().json(JsonLibrary.XStream, PurchaseOrder.class).
process(new Processor() {
public void process(Exchange exchange) throws Exception {
log.debug("marshalled: " + exchange.getIn().getBody(String.class));
}
}).
unmarshal().json().
unmarshal().json(JsonLibrary.XStream, PurchaseOrder.class).
to("mock:result");
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,13 @@
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

/**
* @version
*/
public class UnmarshalThenMarshalTest extends CamelTestSupport {

@BeforeClass
public static void setup() {
XStreamTestUtils.setPermissionSystemProperty("");
}

@AfterClass
public static void cleanup() {
XStreamTestUtils.revertPermissionSystemProperty();
}

@Test
public void testSendXmlAndUnmarshal() throws Exception {

Expand Down Expand Up @@ -69,13 +57,13 @@ protected RouteBuilder createRouteBuilder() {
return new RouteBuilder() {
public void configure() {
from("direct:start").
marshal().xstream().
marshal().xstream(PurchaseOrder.class).
process(new Processor() {
public void process(Exchange exchange) throws Exception {
log.debug("marshalled: " + exchange.getIn().getBody(String.class));
}
}).
unmarshal().xstream().
unmarshal().xstream(PurchaseOrder.class).
to("mock:result");
}
};
Expand Down
Loading

0 comments on commit 4cdc6b1

Please # to comment.