Repository: camel Updated Branches: refs/heads/camel-2.17.x 3f8e0547c -> 5ae9c0dcc
CAMEL-10604 - Camel-JacksonXML: Add an option to allow the UnmarshallType header use Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/5ae9c0dc Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/5ae9c0dc Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/5ae9c0dc Branch: refs/heads/camel-2.17.x Commit: 5ae9c0dcc4843347cd01ffb58ce5dd0687755a14 Parents: 3f8e054 Author: Andrea Cosentino <anco...@gmail.com> Authored: Thu Dec 15 11:51:03 2016 +0100 Committer: Andrea Cosentino <anco...@gmail.com> Committed: Thu Dec 15 12:04:17 2016 +0100 ---------------------------------------------------------------------- .../model/dataformat/JacksonXMLDataFormat.java | 18 +++++++ .../jacksonxml/JacksonXMLDataFormat.java | 19 ++++++- ...arshalUnmarshalTypeHeaderNotAllowedTest.java | 53 ++++++++++++++++++++ .../JacksonMarshalUnmarshalTypeHeaderTest.java | 1 + 4 files changed, 90 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/5ae9c0dc/camel-core/src/main/java/org/apache/camel/model/dataformat/JacksonXMLDataFormat.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/model/dataformat/JacksonXMLDataFormat.java b/camel-core/src/main/java/org/apache/camel/model/dataformat/JacksonXMLDataFormat.java index 1780228..75ad17e 100644 --- a/camel-core/src/main/java/org/apache/camel/model/dataformat/JacksonXMLDataFormat.java +++ b/camel-core/src/main/java/org/apache/camel/model/dataformat/JacksonXMLDataFormat.java @@ -68,6 +68,8 @@ public class JacksonXMLDataFormat extends DataFormatDefinition { private String enableFeatures; @XmlAttribute private String disableFeatures; + @XmlAttribute + private Boolean allowUnmarshallType; public JacksonXMLDataFormat() { super("jacksonxml"); @@ -256,6 +258,19 @@ public class JacksonXMLDataFormat extends DataFormatDefinition { public void setDisableFeatures(String disableFeatures) { this.disableFeatures = disableFeatures; } + + public Boolean getAllowUnmarshallType() { + return allowUnmarshallType; + } + + /** + * If enabled then Jackson is allowed to attempt to use the CamelJacksonUnmarshalType header during the unmarshalling. + * <p/> + * This should only be enabled when desired to be used. + */ + public void setAllowUnmarshallType(Boolean allowUnmarshallType) { + this.allowUnmarshallType = allowUnmarshallType; + } @Override public String getDataFormatName() { @@ -326,6 +341,9 @@ public class JacksonXMLDataFormat extends DataFormatDefinition { if (disableFeatures != null) { setProperty(camelContext, dataFormat, "disableFeatures", disableFeatures); } + if (allowUnmarshallType != null) { + setProperty(camelContext, dataFormat, "allowUnmarshallType", allowUnmarshallType); + } } } http://git-wip-us.apache.org/repos/asf/camel/blob/5ae9c0dc/components/camel-jacksonxml/src/main/java/org/apache/camel/component/jacksonxml/JacksonXMLDataFormat.java ---------------------------------------------------------------------- diff --git a/components/camel-jacksonxml/src/main/java/org/apache/camel/component/jacksonxml/JacksonXMLDataFormat.java b/components/camel-jacksonxml/src/main/java/org/apache/camel/component/jacksonxml/JacksonXMLDataFormat.java index 869fe66..dd70cd5 100644 --- a/components/camel-jacksonxml/src/main/java/org/apache/camel/component/jacksonxml/JacksonXMLDataFormat.java +++ b/components/camel-jacksonxml/src/main/java/org/apache/camel/component/jacksonxml/JacksonXMLDataFormat.java @@ -68,6 +68,7 @@ public class JacksonXMLDataFormat extends ServiceSupport implements DataFormat, private String enableFeatures; private String disableFeatures; private boolean enableJacksonTypeConverter; + private boolean allowUnmarshallType; /** * Use the default Jackson {@link XmlMapper} and {@link Map} @@ -158,7 +159,10 @@ public class JacksonXMLDataFormat extends ServiceSupport implements DataFormat, // is there a header with the unmarshal type? Class<?> clazz = unmarshalType; - String type = exchange.getIn().getHeader(JacksonXMLConstants.UNMARSHAL_TYPE, String.class); + String type = null; + if (allowUnmarshallType) { + type = exchange.getIn().getHeader(JacksonXMLConstants.UNMARSHAL_TYPE, String.class); + } if (type == null && isAllowJmsType()) { type = exchange.getIn().getHeader("JMSType", String.class); } @@ -326,6 +330,19 @@ public class JacksonXMLDataFormat extends ServiceSupport implements DataFormat, public void setEnableJacksonTypeConverter(boolean enableJacksonTypeConverter) { this.enableJacksonTypeConverter = enableJacksonTypeConverter; } + + public boolean isAllowUnmarshallType() { + return allowUnmarshallType; + } + + /** + * If enabled then Jackson is allowed to attempt to use the CamelJacksonUnmarshalType header during the unmarshalling. + * <p/> + * This should only be enabled when desired to be used. + */ + public void setAllowUnmarshallType(boolean allowJacksonUnmarshallType) { + this.allowUnmarshallType = allowJacksonUnmarshallType; + } public String getEnableFeatures() { return enableFeatures; http://git-wip-us.apache.org/repos/asf/camel/blob/5ae9c0dc/components/camel-jacksonxml/src/test/java/org/apache/camel/component/jacksonxml/JacksonMarshalUnmarshalTypeHeaderNotAllowedTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jacksonxml/src/test/java/org/apache/camel/component/jacksonxml/JacksonMarshalUnmarshalTypeHeaderNotAllowedTest.java b/components/camel-jacksonxml/src/test/java/org/apache/camel/component/jacksonxml/JacksonMarshalUnmarshalTypeHeaderNotAllowedTest.java new file mode 100644 index 0000000..579e3be --- /dev/null +++ b/components/camel-jacksonxml/src/test/java/org/apache/camel/component/jacksonxml/JacksonMarshalUnmarshalTypeHeaderNotAllowedTest.java @@ -0,0 +1,53 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.jacksonxml; + +import java.util.LinkedHashMap; + +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.test.junit4.CamelTestSupport; +import org.junit.Test; + +public class JacksonMarshalUnmarshalTypeHeaderNotAllowedTest extends CamelTestSupport { + + @Test + public void testUnmarshalPojo() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:reversePojo"); + mock.expectedMessageCount(1); + + String json = "<pojo name=\"Camel\"/>"; + template.sendBodyAndHeader("direct:backPojo", json, JacksonXMLConstants.UNMARSHAL_TYPE, TestPojo.class.getName()); + + assertMockEndpointsSatisfied(); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + + @Override + public void configure() throws Exception { + JacksonXMLDataFormat format = new JacksonXMLDataFormat(); + + from("direct:backPojo").unmarshal(format).to("mock:reversePojo"); + + } + }; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/5ae9c0dc/components/camel-jacksonxml/src/test/java/org/apache/camel/component/jacksonxml/JacksonMarshalUnmarshalTypeHeaderTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jacksonxml/src/test/java/org/apache/camel/component/jacksonxml/JacksonMarshalUnmarshalTypeHeaderTest.java b/components/camel-jacksonxml/src/test/java/org/apache/camel/component/jacksonxml/JacksonMarshalUnmarshalTypeHeaderTest.java index 56e3b35..68dd3fe 100644 --- a/components/camel-jacksonxml/src/test/java/org/apache/camel/component/jacksonxml/JacksonMarshalUnmarshalTypeHeaderTest.java +++ b/components/camel-jacksonxml/src/test/java/org/apache/camel/component/jacksonxml/JacksonMarshalUnmarshalTypeHeaderTest.java @@ -46,6 +46,7 @@ public class JacksonMarshalUnmarshalTypeHeaderTest extends CamelTestSupport { @Override public void configure() throws Exception { JacksonXMLDataFormat format = new JacksonXMLDataFormat(); + format.setAllowUnmarshallType(true); from("direct:backPojo").unmarshal(format).to("mock:reversePojo");