Return-Path: X-Original-To: apmail-camel-commits-archive@www.apache.org Delivered-To: apmail-camel-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 316C818F4D for ; Wed, 10 Feb 2016 08:50:29 +0000 (UTC) Received: (qmail 86351 invoked by uid 500); 10 Feb 2016 08:50:29 -0000 Delivered-To: apmail-camel-commits-archive@camel.apache.org Received: (qmail 86262 invoked by uid 500); 10 Feb 2016 08:50:29 -0000 Mailing-List: contact commits-help@camel.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@camel.apache.org Delivered-To: mailing list commits@camel.apache.org Received: (qmail 86252 invoked by uid 99); 10 Feb 2016 08:50:28 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 10 Feb 2016 08:50:28 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 9C641E0248; Wed, 10 Feb 2016 08:50:28 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: davsclaus@apache.org To: commits@camel.apache.org Date: Wed, 10 Feb 2016 08:50:28 -0000 Message-Id: X-Mailer: ASF-Git Admin Mailer Subject: [1/2] camel git commit: CAMEL-9583: camel-jackson - Allow the FallbackTypeConverter to convert to/from string types Repository: camel Updated Branches: refs/heads/master 2c78e2f30 -> 2b4b9df67 CAMEL-9583: camel-jackson - Allow the FallbackTypeConverter to convert to/from string types Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/7009d640 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/7009d640 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/7009d640 Branch: refs/heads/master Commit: 7009d64091876c3b1f9e36e074f8cc7484cfb434 Parents: 2c78e2f Author: Claus Ibsen Authored: Wed Feb 10 08:16:35 2016 +0100 Committer: Claus Ibsen Committed: Wed Feb 10 08:16:35 2016 +0100 ---------------------------------------------------------------------- .../component/jackson/JacksonConstants.java | 1 + .../converter/JacksonTypeConverters.java | 36 ++++++++++++++++---- 2 files changed, 30 insertions(+), 7 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/7009d640/components/camel-jackson/src/main/java/org/apache/camel/component/jackson/JacksonConstants.java ---------------------------------------------------------------------- diff --git a/components/camel-jackson/src/main/java/org/apache/camel/component/jackson/JacksonConstants.java b/components/camel-jackson/src/main/java/org/apache/camel/component/jackson/JacksonConstants.java index 1429dc0..d4f5c26 100644 --- a/components/camel-jackson/src/main/java/org/apache/camel/component/jackson/JacksonConstants.java +++ b/components/camel-jackson/src/main/java/org/apache/camel/component/jackson/JacksonConstants.java @@ -19,6 +19,7 @@ package org.apache.camel.component.jackson; public final class JacksonConstants { public static final String ENABLE_TYPE_CONVERTER = "CamelJacksonEnableTypeConverter"; + public static final String TYPE_CONVERTER_POJO_ONLY = "CamelJacksonTypeConverterPojoOnly"; public static final String UNMARSHAL_TYPE = "CamelJacksonUnmarshalType"; http://git-wip-us.apache.org/repos/asf/camel/blob/7009d640/components/camel-jackson/src/main/java/org/apache/camel/component/jackson/converter/JacksonTypeConverters.java ---------------------------------------------------------------------- diff --git a/components/camel-jackson/src/main/java/org/apache/camel/component/jackson/converter/JacksonTypeConverters.java b/components/camel-jackson/src/main/java/org/apache/camel/component/jackson/converter/JacksonTypeConverters.java index 5cb2451..110b6ce 100644 --- a/components/camel-jackson/src/main/java/org/apache/camel/component/jackson/converter/JacksonTypeConverters.java +++ b/components/camel-jackson/src/main/java/org/apache/camel/component/jackson/converter/JacksonTypeConverters.java @@ -16,10 +16,10 @@ */ package org.apache.camel.component.jackson.converter; -import java.util.Map; import java.util.Set; import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule; import org.apache.camel.Exchange; import org.apache.camel.FallbackConverter; import org.apache.camel.component.jackson.JacksonConstants; @@ -28,21 +28,33 @@ import org.apache.camel.spi.TypeConverterRegistry; public final class JacksonTypeConverters { - private final ObjectMapper defaultMapper = new ObjectMapper(); + private final ObjectMapper defaultMapper; private boolean init; private Boolean enabled; + private boolean pojoOnly = true; public JacksonTypeConverters() { + defaultMapper = new ObjectMapper(); + // Enables JAXB processing + JaxbAnnotationModule module = new JaxbAnnotationModule(); + defaultMapper.registerModule(module); } @FallbackConverter - public T convertTo(Class type, Exchange exchange, Object value, TypeConverterRegistry registry) { + public T convertTo(Class type, Exchange exchange, Object value, TypeConverterRegistry registry) throws Exception { // only do this if enabled if (!init && exchange != null) { // init to see if this is enabled String text = exchange.getContext().getProperties().get(JacksonConstants.ENABLE_TYPE_CONVERTER); enabled = "true".equalsIgnoreCase(text); + + // pojo only by default + text = exchange.getContext().getProperties().get(JacksonConstants.TYPE_CONVERTER_POJO_ONLY); + if (text != null) { + pojoOnly = "true".equalsIgnoreCase(text); + } + init = true; } @@ -50,14 +62,24 @@ public final class JacksonTypeConverters { return null; } - - if (isNotPojoType(type)) { + if (pojoOnly && isNotPojoType(type)) { return null; } - if (exchange != null && value instanceof Map) { + if (exchange != null) { ObjectMapper mapper = resolveObjectMapper(exchange.getContext().getRegistry()); - if (mapper.canSerialize(type)) { + if (String.class.isAssignableFrom(type)) { + String out = mapper.writeValueAsString(value); + return type.cast(out); + } else if (byte[].class.isAssignableFrom(type)) { + byte[] out = mapper.writeValueAsBytes(value); + return type.cast(out); + } else if (mapper.canSerialize(type)) { + // TODO: favor using mapper readValue + // if the input is string or input stream + if (String.class.isAssignableFrom(value.getClass())) { + return mapper.readValue((String) value, type); + } return mapper.convertValue(value, type); } }