Skip to content

Commit ccf149c

Browse files
committed
CAMEL-10604 - Camel-JacksonXML: Add an option to allow the UnmarshallType header use
1 parent b093ebd commit ccf149c

File tree

4 files changed

+90
-1
lines changed

4 files changed

+90
-1
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ public class JacksonXMLDataFormat extends DataFormatDefinition {
6666
private String enableFeatures;
6767
@XmlAttribute
6868
private String disableFeatures;
69+
@XmlAttribute
70+
private Boolean allowUnmarshallType;
6971

7072
public JacksonXMLDataFormat() {
7173
super("jacksonxml");
@@ -243,6 +245,19 @@ public String getDisableFeatures() {
243245
public void setDisableFeatures(String disableFeatures) {
244246
this.disableFeatures = disableFeatures;
245247
}
248+
249+
public Boolean getAllowUnmarshallType() {
250+
return allowUnmarshallType;
251+
}
252+
253+
/**
254+
* If enabled then Jackson is allowed to attempt to use the CamelJacksonUnmarshalType header during the unmarshalling.
255+
* <p/>
256+
* This should only be enabled when desired to be used.
257+
*/
258+
public void setAllowUnmarshallType(Boolean allowUnmarshallType) {
259+
this.allowUnmarshallType = allowUnmarshallType;
260+
}
246261

247262
@Override
248263
public String getDataFormatName() {
@@ -308,6 +323,9 @@ protected void configureDataFormat(DataFormat dataFormat, CamelContext camelCont
308323
if (disableFeatures != null) {
309324
setProperty(camelContext, dataFormat, "disableFeatures", disableFeatures);
310325
}
326+
if (allowUnmarshallType != null) {
327+
setProperty(camelContext, dataFormat, "allowUnmarshallType", allowUnmarshallType);
328+
}
311329
}
312330

313331
}

components/camel-jacksonxml/src/main/java/org/apache/camel/component/jacksonxml/JacksonXMLDataFormat.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public class JacksonXMLDataFormat extends ServiceSupport implements DataFormat,
6868
private String enableFeatures;
6969
private String disableFeatures;
7070
private boolean enableJacksonTypeConverter;
71+
private boolean allowUnmarshallType;
7172

7273
/**
7374
* Use the default Jackson {@link XmlMapper} and {@link Map}
@@ -159,7 +160,10 @@ public Object unmarshal(Exchange exchange, InputStream stream) throws Exception
159160

160161
// is there a header with the unmarshal type?
161162
Class<?> clazz = unmarshalType;
162-
String type = exchange.getIn().getHeader(JacksonXMLConstants.UNMARSHAL_TYPE, String.class);
163+
String type = null;
164+
if (allowUnmarshallType) {
165+
type = exchange.getIn().getHeader(JacksonXMLConstants.UNMARSHAL_TYPE, String.class);
166+
}
163167
if (type == null && isAllowJmsType()) {
164168
type = exchange.getIn().getHeader("JMSType", String.class);
165169
}
@@ -323,6 +327,19 @@ public boolean isEnableJacksonTypeConverter() {
323327
public void setEnableJacksonTypeConverter(boolean enableJacksonTypeConverter) {
324328
this.enableJacksonTypeConverter = enableJacksonTypeConverter;
325329
}
330+
331+
public boolean isAllowUnmarshallType() {
332+
return allowUnmarshallType;
333+
}
334+
335+
/**
336+
* If enabled then Jackson is allowed to attempt to use the CamelJacksonUnmarshalType header during the unmarshalling.
337+
* <p/>
338+
* This should only be enabled when desired to be used.
339+
*/
340+
public void setAllowUnmarshallType(boolean allowJacksonUnmarshallType) {
341+
this.allowUnmarshallType = allowJacksonUnmarshallType;
342+
}
326343

327344
public String getEnableFeatures() {
328345
return enableFeatures;
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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.component.jacksonxml;
18+
19+
import java.util.LinkedHashMap;
20+
21+
import org.apache.camel.builder.RouteBuilder;
22+
import org.apache.camel.component.mock.MockEndpoint;
23+
import org.apache.camel.test.junit4.CamelTestSupport;
24+
import org.junit.Test;
25+
26+
public class JacksonMarshalUnmarshalTypeHeaderNotAllowedTest extends CamelTestSupport {
27+
28+
@Test
29+
public void testUnmarshalPojo() throws Exception {
30+
MockEndpoint mock = getMockEndpoint("mock:reversePojo");
31+
mock.expectedMessageCount(1);
32+
33+
String json = "<pojo name=\"Camel\"/>";
34+
template.sendBodyAndHeader("direct:backPojo", json, JacksonXMLConstants.UNMARSHAL_TYPE, TestPojo.class.getName());
35+
36+
assertMockEndpointsSatisfied();
37+
}
38+
39+
@Override
40+
protected RouteBuilder createRouteBuilder() throws Exception {
41+
return new RouteBuilder() {
42+
43+
@Override
44+
public void configure() throws Exception {
45+
JacksonXMLDataFormat format = new JacksonXMLDataFormat();
46+
47+
from("direct:backPojo").unmarshal(format).to("mock:reversePojo");
48+
49+
}
50+
};
51+
}
52+
53+
}

components/camel-jacksonxml/src/test/java/org/apache/camel/component/jacksonxml/JacksonMarshalUnmarshalTypeHeaderTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ protected RouteBuilder createRouteBuilder() throws Exception {
4646
@Override
4747
public void configure() throws Exception {
4848
JacksonXMLDataFormat format = new JacksonXMLDataFormat();
49+
format.setAllowUnmarshallType(true);
4950

5051
from("direct:backPojo").unmarshal(format).to("mock:reversePojo");
5152

0 commit comments

Comments
 (0)