Skip to content

Commit 4491c08

Browse files
committed
[CAMEL-9297] Expose more configuration options from Camel's XStream
1 parent 0e1e821 commit 4491c08

File tree

12 files changed

+401
-6
lines changed

12 files changed

+401
-6
lines changed

camel-core/src/main/java/org/apache/camel/model/dataformat/XStreamDataFormat.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ public class XStreamDataFormat extends DataFormatDefinition {
5757
private String driverRef;
5858
@XmlAttribute
5959
private String mode;
60+
@XmlAttribute
61+
private String permissions;
6062

6163
@XmlJavaTypeAdapter(ConvertersAdapter.class)
6264
@XmlElement(name = "converters")
@@ -180,6 +182,17 @@ public void setImplicitCollections(Map<String, String[]> implicitCollections) {
180182
this.implicitCollections = implicitCollections;
181183
}
182184

185+
public String getPermissions() {
186+
return permissions;
187+
}
188+
189+
/**
190+
* Adds permissionsList
191+
*/
192+
public void setPermissions(String permissions) {
193+
this.permissions = permissions;
194+
}
195+
183196
@Override
184197
protected DataFormat createDataFormat(RouteContext routeContext) {
185198
if ("json".equals(this.driver)) {
@@ -210,6 +223,9 @@ protected void configureDataFormat(DataFormat dataFormat, CamelContext camelCont
210223
if (this.implicitCollections != null) {
211224
setProperty(camelContext, dataFormat, "implicitCollections", this.implicitCollections);
212225
}
226+
if (this.permissions != null) {
227+
setProperty(camelContext, dataFormat, "permissions", this.permissions);
228+
}
213229
if (this.mode != null) {
214230
setProperty(camelContext, dataFormat, "mode", mode);
215231
}
@@ -547,5 +563,4 @@ public String toString() {
547563
return "OmitField[" + clsName + ", fields=" + Arrays.asList(this.fields) + "]";
548564
}
549565
}
550-
551566
}

components/camel-xstream/src/main/java/org/apache/camel/dataformat/xstream/AbstractXStreamWrapper.java

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@
3232
import com.thoughtworks.xstream.io.HierarchicalStreamDriver;
3333
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
3434
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
35-
35+
import com.thoughtworks.xstream.security.AnyTypePermission;
36+
import com.thoughtworks.xstream.security.ExplicitTypePermission;
37+
import com.thoughtworks.xstream.security.TypePermission;
38+
import com.thoughtworks.xstream.security.WildcardTypePermission;
3639
import org.apache.camel.CamelContext;
3740
import org.apache.camel.Exchange;
3841
import org.apache.camel.converter.jaxp.StaxConverter;
@@ -47,14 +50,17 @@
4750
* ({@link DataFormat}) interface which leverage the XStream library for XML or JSON's marshaling and unmarshaling
4851
*/
4952
public abstract class AbstractXStreamWrapper extends ServiceSupport implements DataFormat, DataFormatName {
50-
53+
private static final String PERMISSIONS_PROPERTY_KEY = "org.apache.camel.xstream.permissions";
54+
private static final String PERMISSIONS_PROPERTY_DEFAULT = "-*,java.lang.*,java.util.*";
55+
5156
private XStream xstream;
5257
private HierarchicalStreamDriver xstreamDriver;
5358
private StaxConverter staxConverter;
5459
private List<String> converters;
5560
private Map<String, String> aliases;
5661
private Map<String, String[]> omitFields;
5762
private Map<String, String[]> implicitCollections;
63+
private String permissions;
5864
private String mode;
5965

6066
public AbstractXStreamWrapper() {
@@ -174,13 +180,59 @@ protected XStream createXStream(ClassResolver resolver, ClassLoader classLoader)
174180
}
175181
}
176182

183+
addDefaultPermissions(xstream);
184+
if (this.permissions != null) {
185+
// permissions ::= pterm (',' pterm)* # consits of one or more terms
186+
// pterm ::= aod? wterm # each term preceded by an optional sign
187+
// aod ::= '+' | '-' # indicates allow or deny where allow if omitted
188+
// wterm ::= a class name with optional wildcard characters
189+
addPermissions(xstream, permissions);
190+
}
177191
} catch (Exception e) {
178192
throw new RuntimeException("Unable to build XStream instance", e);
179193
}
180194

181195
return xstream;
182196
}
183197

198+
private static void addPermissions(XStream xstream, String permissions) {
199+
for (String pterm : permissions.split(",")) {
200+
boolean aod;
201+
pterm = pterm.trim();
202+
if (pterm.startsWith("-")) {
203+
aod = false;
204+
pterm = pterm.substring(1);
205+
} else {
206+
aod = true;
207+
if (pterm.startsWith("+")) {
208+
pterm = pterm.substring(1);
209+
}
210+
}
211+
TypePermission typePermission = null;
212+
if ("*".equals(pterm)) {
213+
// accept or deny any
214+
typePermission = AnyTypePermission.ANY;
215+
} else if (pterm.indexOf('*') < 0) {
216+
// exact type
217+
typePermission = new ExplicitTypePermission(new String[]{pterm});
218+
} else if (pterm.length() > 0) {
219+
// wildcard type
220+
typePermission = new WildcardTypePermission(new String[]{pterm});
221+
}
222+
if (typePermission != null) {
223+
if (aod) {
224+
xstream.addPermission(typePermission);
225+
} else {
226+
xstream.denyPermission(typePermission);
227+
}
228+
}
229+
}
230+
}
231+
232+
private static void addDefaultPermissions(XStream xstream) {
233+
addPermissions(xstream, System.getProperty(PERMISSIONS_PROPERTY_KEY, PERMISSIONS_PROPERTY_DEFAULT));
234+
}
235+
184236
protected int getModeFromString(String modeString) {
185237
int result;
186238
if ("NO_REFERENCES".equalsIgnoreCase(modeString)) {
@@ -252,6 +304,14 @@ public void setXstreamDriver(HierarchicalStreamDriver xstreamDriver) {
252304
this.xstreamDriver = xstreamDriver;
253305
}
254306

307+
public String getPermissions() {
308+
return permissions;
309+
}
310+
311+
public void setPermissions(String permissions) {
312+
this.permissions = permissions;
313+
}
314+
255315
public String getMode() {
256316
return mode;
257317
}

components/camel-xstream/src/test/java/org/apache/camel/dataformat/xstream/MarshalDomainObjectTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,25 @@
1919
import org.apache.camel.builder.RouteBuilder;
2020
import org.apache.camel.component.mock.MockEndpoint;
2121
import org.apache.camel.test.junit4.CamelTestSupport;
22+
import org.junit.AfterClass;
23+
import org.junit.BeforeClass;
2224
import org.junit.Test;
2325

2426
/**
2527
* Marshal tests with domain objects.
2628
*/
2729
public class MarshalDomainObjectTest extends CamelTestSupport {
2830

31+
@BeforeClass
32+
public static void setup() {
33+
XStreamTestUtils.setPermissionSystemProperty("");
34+
}
35+
36+
@AfterClass
37+
public static void cleanup() {
38+
XStreamTestUtils.revertPermissionSystemProperty();
39+
}
40+
2941
@Test
3042
public void testMarshalDomainObject() throws Exception {
3143
MockEndpoint mock = getMockEndpoint("mock:result");

components/camel-xstream/src/test/java/org/apache/camel/dataformat/xstream/UnmarshalThenMarshalTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,25 @@
2323
import org.apache.camel.builder.RouteBuilder;
2424
import org.apache.camel.component.mock.MockEndpoint;
2525
import org.apache.camel.test.junit4.CamelTestSupport;
26+
import org.junit.AfterClass;
27+
import org.junit.BeforeClass;
2628
import org.junit.Test;
2729

2830
/**
2931
* @version
3032
*/
3133
public class UnmarshalThenMarshalTest extends CamelTestSupport {
34+
35+
@BeforeClass
36+
public static void setup() {
37+
XStreamTestUtils.setPermissionSystemProperty("");
38+
}
39+
40+
@AfterClass
41+
public static void cleanup() {
42+
XStreamTestUtils.revertPermissionSystemProperty();
43+
}
44+
3245
@Test
3346
public void testSendXmlAndUnmarshal() throws Exception {
3447

components/camel-xstream/src/test/java/org/apache/camel/dataformat/xstream/XStreamConcurrencyTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,25 @@
2222

2323
import org.apache.camel.builder.RouteBuilder;
2424
import org.apache.camel.test.junit4.CamelTestSupport;
25+
import org.junit.AfterClass;
26+
import org.junit.BeforeClass;
2527
import org.junit.Test;
2628

2729
/**
2830
* @version
2931
*/
3032
public class XStreamConcurrencyTest extends CamelTestSupport {
3133

34+
@BeforeClass
35+
public static void setup() {
36+
XStreamTestUtils.setPermissionSystemProperty("");
37+
}
38+
39+
@AfterClass
40+
public static void cleanup() {
41+
XStreamTestUtils.revertPermissionSystemProperty();
42+
}
43+
3244
@Test
3345
public void testNoConcurrentProducers() throws Exception {
3446
doSendMessages(1, 1);

components/camel-xstream/src/test/java/org/apache/camel/dataformat/xstream/XStreamConfigurationTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
import org.apache.camel.component.mock.MockEndpoint;
3434
import org.apache.camel.model.dataformat.XStreamDataFormat;
3535
import org.apache.camel.test.junit4.CamelTestSupport;
36+
import org.junit.AfterClass;
37+
import org.junit.BeforeClass;
3638
import org.junit.Test;
3739

3840
/**
@@ -42,6 +44,16 @@ public class XStreamConfigurationTest extends CamelTestSupport {
4244

4345
private static volatile boolean constructorInjected;
4446
private static volatile boolean methodInjected;
47+
48+
@BeforeClass
49+
public static void setup() {
50+
XStreamTestUtils.setPermissionSystemProperty("");
51+
}
52+
53+
@AfterClass
54+
public static void cleanup() {
55+
XStreamTestUtils.revertPermissionSystemProperty();
56+
}
4557

4658
@Override
4759
public void setUp() throws Exception {

components/camel-xstream/src/test/java/org/apache/camel/dataformat/xstream/XStreamDataFormatDriverConfigTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import com.thoughtworks.xstream.XStream;
2020
import com.thoughtworks.xstream.io.json.JsonHierarchicalStreamDriver;
21-
import org.apache.camel.impl.DefaultClassResolver;
2221
import org.apache.camel.test.junit4.CamelTestSupport;
2322
import org.junit.Test;
2423

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.camel.dataformat.xstream;
18+
19+
import com.thoughtworks.xstream.XStream;
20+
import org.junit.AfterClass;
21+
import org.junit.BeforeClass;
22+
import org.junit.Test;
23+
24+
public class XStreamDataFormatPermissionsSystemPropertyTest extends XStreamDataFormatPermissionsTest {
25+
26+
@BeforeClass
27+
public static void setup() {
28+
// clear the default permissions system property
29+
// see AbstractXStreamWrapper.PERMISSIONS_PROPERTY_DEFAULT
30+
XStreamTestUtils.setPermissionSystemProperty("");
31+
}
32+
33+
@AfterClass
34+
public static void cleanup() {
35+
XStreamTestUtils.revertPermissionSystemProperty();
36+
}
37+
38+
@Test
39+
@Override
40+
public void testNone() {
41+
XStreamDataFormat xStreamDataFormat = new XStreamDataFormat();
42+
XStream xStream = xStreamDataFormat.createXStream(context.getClassResolver(), context.getApplicationContextClassLoader());
43+
44+
Object po = xStream.fromXML(XML_PURCHASE_ORDER);
45+
assertNotNull(po);
46+
}
47+
}

0 commit comments

Comments
 (0)