Skip to content

Commit

Permalink
CAMEL-10567: Camel-Jackson: Add an option to allow the UnmarshallType…
Browse files Browse the repository at this point in the history
… header use
  • Loading branch information
oscerd committed Dec 7, 2016
1 parent 31cce6b commit 7567488
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public class JacksonDataFormat extends ServiceSupport implements DataFormat, Dat
private String enableFeatures;
private String disableFeatures;
private boolean enableJacksonTypeConverter;
private boolean allowJacksonUnmarshallType;

/**
* Use the default Jackson {@link ObjectMapper} and {@link Object}
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(JacksonConstants.UNMARSHAL_TYPE, String.class);
String type = null;
if (allowJacksonUnmarshallType) {
type = exchange.getIn().getHeader(JacksonConstants.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 isAllowJacksonUnmarshallType() {
return allowJacksonUnmarshallType;
}

/**
* 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 setAllowJacksonUnmarshallType(boolean allowJacksonUnmarshallType) {
this.allowJacksonUnmarshallType = allowJacksonUnmarshallType;
}

public String getEnableFeatures() {
return enableFeatures;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* 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.jackson;

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);
mock.message(0).body().isInstanceOf(LinkedHashMap.class);

String json = "{\"name\":\"Camel\"}";
template.sendBodyAndHeader("direct:backPojo", json, JacksonConstants.UNMARSHAL_TYPE, TestPojo.class.getName());

assertMockEndpointsSatisfied();
}

@Override
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {

@Override
public void configure() throws Exception {
JacksonDataFormat format = new JacksonDataFormat();

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 {
JacksonDataFormat format = new JacksonDataFormat();
format.setAllowJacksonUnmarshallType(true);

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

Expand Down

0 comments on commit 7567488

Please sign in to comment.