You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2009/06/23 09:19:08 UTC

svn commit: r787571 - in /camel/branches/camel-1.x: camel-core/src/main/java/org/apache/camel/impl/MessageSupport.java components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java

Author: davsclaus
Date: Tue Jun 23 07:19:08 2009
New Revision: 787571

URL: http://svn.apache.org/viewvc?rev=787571&view=rev
Log:
CAMEL-1742: fallback type convertering the Message itself should not throw exception. And it should be optimized like the regular. Fixed problem with http component related to this.

Modified:
    camel/branches/camel-1.x/camel-core/src/main/java/org/apache/camel/impl/MessageSupport.java
    camel/branches/camel-1.x/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java

Modified: camel/branches/camel-1.x/camel-core/src/main/java/org/apache/camel/impl/MessageSupport.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-1.x/camel-core/src/main/java/org/apache/camel/impl/MessageSupport.java?rev=787571&r1=787570&r2=787571&view=diff
==============================================================================
--- camel/branches/camel-1.x/camel-core/src/main/java/org/apache/camel/impl/MessageSupport.java (original)
+++ camel/branches/camel-1.x/camel-core/src/main/java/org/apache/camel/impl/MessageSupport.java Tue Jun 23 07:19:08 2009
@@ -53,6 +53,11 @@
     }
 
     protected <T> T getBody(Class<T> type, Object body) {
+        // same instance type
+        if (type.isInstance(body)) {
+            return type.cast(body);
+        }
+
         Exchange e = getExchange();
         if (e != null) {
             CamelContext camelContext = e.getContext();
@@ -64,11 +69,10 @@
                 // the StreamCachingInterceptor will attempt to convert the payload to a StremCache for caching purpose
                 // so we get invoked on each node the exchange passes. So this is a little performance optimization
                 // to avoid the excessive exception handling
-                if (body != null && converter instanceof DefaultTypeConverter) {
-                    DefaultTypeConverter defaultTypeConverter = (DefaultTypeConverter) converter;
+                if (body != null) {
                     // we can only check if there is no converter meaning we have tried to convert it beforehand
                     // and then knows for sure there is no converter possible
-                    tryConvert = !defaultTypeConverter.hasNoConverterFor(type, body.getClass());
+                    tryConvert = !hasNoConverterFor(converter, type, body.getClass());
                 }
                 if (tryConvert) {
                     try {
@@ -81,11 +85,31 @@
                         // ignore
                     }
                 }
-                // fallback to the message itself
-                return converter.convertTo(type, this);
+
+                // fallback to the message itself (e.g. used in camel-http)
+                tryConvert = !hasNoConverterFor(converter, type, this.getClass());
+                if (tryConvert) {
+                    try {
+                        return converter.convertTo(type, e, this);
+                    } catch (NoTypeConversionAvailableException ex) {
+                        // ignore
+                    }
+                }
             }
         }
-        return (T)getBody();
+
+        // not possible to convert
+        throw new NoTypeConversionAvailableException(body, type);
+    }
+
+    private boolean hasNoConverterFor(TypeConverter converter, Class toType, Class fromType) {
+        if (converter instanceof DefaultTypeConverter) {
+            DefaultTypeConverter defaultTypeConverter = (DefaultTypeConverter) converter;
+            // we can only check if there is no converter meaning we have tried to convert it beforehand
+            // and then knows for sure there is no converter possible
+            return defaultTypeConverter.hasNoConverterFor(toType, fromType);
+        }
+        return false;
     }
 
     public void setBody(Object body) {

Modified: camel/branches/camel-1.x/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-1.x/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java?rev=787571&r1=787570&r2=787571&view=diff
==============================================================================
--- camel/branches/camel-1.x/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java (original)
+++ camel/branches/camel-1.x/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java Tue Jun 23 07:19:08 2009
@@ -235,9 +235,13 @@
             return null;
         }
 
+        RequestEntity answer = null;
         try {
-            return in.getBody(RequestEntity.class);
+            answer = in.getBody(RequestEntity.class);
         } catch (NoTypeConversionAvailableException ex) {
+            // ignore
+        }
+        if (answer == null) {
             try {
                 String data = in.getBody(String.class);
                 if (data != null) {
@@ -251,7 +255,8 @@
             } catch (UnsupportedEncodingException e) {
                 throw new RuntimeCamelException(e);
             }
-        }        
+        }
+        return answer;
     }
 
     public HttpClient getHttpClient() {