Skip to content

Commit

Permalink
CAMEL-10604 - Camel-JacksonXML: Add an option to allow the Unmarshall…
Browse files Browse the repository at this point in the history
…Type header use
  • Loading branch information
oscerd committed Dec 15, 2016
1 parent a2209dc commit 881e509
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ public class JacksonXMLDataFormat extends DataFormatDefinition {
private String enableFeatures;
@XmlAttribute
private String disableFeatures;
@XmlAttribute
private Boolean allowUnmarshallType;

public JacksonXMLDataFormat() {
super("jacksonxml");
Expand Down Expand Up @@ -256,6 +258,19 @@ public String getDisableFeatures() {
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() {
Expand Down Expand Up @@ -326,6 +341,9 @@ protected void configureDataFormat(DataFormat dataFormat, CamelContext camelCont
if (disableFeatures != null) {
setProperty(camelContext, dataFormat, "disableFeatures", disableFeatures);
}
if (allowUnmarshallType != null) {
setProperty(camelContext, dataFormat, "allowUnmarshallType", allowUnmarshallType);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ public class JacksonXMLDataFormatConfiguration {
* separated by comma
*/
private String disableFeatures;
/**
* If enabled then Jackson is allowed to attempt to use the
* CamelJacksonUnmarshalType header during the unmarshalling. This should
* only be enabled when desired to be used.
*/
private Boolean allowUnmarshallType = false;

public String getXmlMapper() {
return xmlMapper;
Expand Down Expand Up @@ -205,4 +211,12 @@ public String getDisableFeatures() {
public void setDisableFeatures(String disableFeatures) {
this.disableFeatures = disableFeatures;
}

public Boolean getAllowUnmarshallType() {
return allowUnmarshallType;
}

public void setAllowUnmarshallType(Boolean allowUnmarshallType) {
this.allowUnmarshallType = allowUnmarshallType;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ JacksonXML Options


// dataformat options: START
The JacksonXML dataformat supports 13 options which are listed below.
The JacksonXML dataformat supports 14 options which are listed below.



Expand All @@ -57,6 +57,7 @@ The JacksonXML dataformat supports 13 options which are listed below.
| moduleRefs | | String | To use custom Jackson modules referred from the Camel registry. Multiple modules can be separated by comma.
| enableFeatures | | String | Set of features to enable on the Jackson com.fasterxml.jackson.databind.ObjectMapper. The features should be a name that matches a enum from com.fasterxml.jackson.databind.SerializationFeature com.fasterxml.jackson.databind.DeserializationFeature or com.fasterxml.jackson.databind.MapperFeature Multiple features can be separated by comma
| disableFeatures | | String | Set of features to disable on the Jackson com.fasterxml.jackson.databind.ObjectMapper. The features should be a name that matches a enum from com.fasterxml.jackson.databind.SerializationFeature com.fasterxml.jackson.databind.DeserializationFeature or com.fasterxml.jackson.databind.MapperFeature Multiple features can be separated by comma
| allowUnmarshallType | false | Boolean | If enabled then Jackson is allowed to attempt to use the CamelJacksonUnmarshalType header during the unmarshalling. This should only be enabled when desired to be used.
|=======================================================================
{% endraw %}
// dataformat options: END
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -158,7 +159,10 @@ public Object unmarshal(Exchange exchange, InputStream stream) throws Exception

// 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);
}
Expand Down Expand Up @@ -326,6 +330,19 @@ public boolean isEnableJacksonTypeConverter() {
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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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");

}
};
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ protected RouteBuilder createRouteBuilder() throws Exception {
@Override
public void configure() throws Exception {
JacksonXMLDataFormat format = new JacksonXMLDataFormat();
format.setAllowUnmarshallType(true);

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

Expand Down

0 comments on commit 881e509

Please sign in to comment.