Author: ningjiang
Date: Tue Dec 18 07:06:50 2012
New Revision: 1423299

URL: http://svn.apache.org/viewvc?rev=1423299&view=rev
Log:
CAMEL-5890 fixed the NPE when jaxb fallbackConverter is used with 
RequestEntityConverter

Added:
    
camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/jaxb/JaxbFallbackTypeConverterTest.java
    
camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/jaxb/example/
    
camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/jaxb/example/Bar.java
Modified:
    
camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/RequestEntityConverter.java
    
camel/trunk/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/FallbackTypeConverter.java

Modified: 
camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/RequestEntityConverter.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/RequestEntityConverter.java?rev=1423299&r1=1423298&r2=1423299&view=diff
==============================================================================
--- 
camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/RequestEntityConverter.java
 (original)
+++ 
camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/RequestEntityConverter.java
 Tue Dec 18 07:06:50 2012
@@ -48,7 +48,7 @@ public final class RequestEntityConverte
 
     @Converter
     public static RequestEntity toRequestEntity(String str, Exchange exchange) 
throws Exception {
-        if (GZIPHelper.isGzip(exchange.getIn())) {
+        if (exchange != null && GZIPHelper.isGzip(exchange.getIn())) {
             byte[] data = 
exchange.getContext().getTypeConverter().convertTo(byte[].class, str);
             return asRequestEntity(data, exchange);
         } else {

Modified: 
camel/trunk/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/FallbackTypeConverter.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/FallbackTypeConverter.java?rev=1423299&r1=1423298&r2=1423299&view=diff
==============================================================================
--- 
camel/trunk/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/FallbackTypeConverter.java
 (original)
+++ 
camel/trunk/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/FallbackTypeConverter.java
 Tue Dec 18 07:06:50 2012
@@ -219,7 +219,8 @@ public class FallbackTypeConverter exten
             } else {
                 marshaller.marshal(value, buffer);
             }
-            answer = parentTypeConverter.convertTo(type, buffer.toString());
+            // we need to pass the exchange
+            answer = parentTypeConverter.convertTo(type, exchange, 
buffer.toString());
         }
 
         return answer;

Added: 
camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/jaxb/JaxbFallbackTypeConverterTest.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/jaxb/JaxbFallbackTypeConverterTest.java?rev=1423299&view=auto
==============================================================================
--- 
camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/jaxb/JaxbFallbackTypeConverterTest.java
 (added)
+++ 
camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/jaxb/JaxbFallbackTypeConverterTest.java
 Tue Dec 18 07:06:50 2012
@@ -0,0 +1,50 @@
+package org.apache.camel.itest.jaxb;
+
+import java.io.InputStream;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Message;
+import org.apache.camel.Processor;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.itest.jaxb.example.Bar;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.apache.commons.httpclient.methods.RequestEntity;
+import org.junit.Test;
+
+public class JaxbFallbackTypeConverterTest extends CamelTestSupport {
+    
+    @Test
+    public void testJaxbFallbackTypeConverter() {
+        Bar bar = new Bar();
+        bar.setName("camel");
+        bar.setValue("cool");
+        String result = template.requestBody("direct:start", bar, 
String.class);
+        assertNotNull(result);
+        assertTrue("Get a wrong xml string", result.indexOf("<bar 
name=\"camel\" value=\"cool\"/>") > 0);
+    }
+    
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+
+                from("direct:start").process(new Processor() {
+
+                    @Override
+                    public void process(Exchange exchange) throws Exception {
+                        Message in = exchange.getIn();
+                        RequestEntity entity = in.getBody(RequestEntity.class);
+                        assertNull("We should not get the entity here", 
entity);
+                        InputStream is = 
in.getMandatoryBody(InputStream.class);
+                        // make sure we can get the InputStream rightly.
+                        exchange.getOut().setBody(is);
+                    }
+                    
+                });
+            }
+        };
+    }
+
+}

Added: 
camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/jaxb/example/Bar.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/jaxb/example/Bar.java?rev=1423299&view=auto
==============================================================================
--- 
camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/jaxb/example/Bar.java
 (added)
+++ 
camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/jaxb/example/Bar.java
 Tue Dec 18 07:06:50 2012
@@ -0,0 +1,52 @@
+/**
+ * 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.itest.jaxb.example;
+
+import javax.xml.bind.annotation.XmlAccessType;
+import javax.xml.bind.annotation.XmlAccessorType;
+import javax.xml.bind.annotation.XmlAttribute;
+import javax.xml.bind.annotation.XmlRootElement;
+
+@XmlRootElement
+@XmlAccessorType(XmlAccessType.FIELD)
+public class Bar {
+    
+    @XmlAttribute
+    private String name;
+    @XmlAttribute
+    private String value;
+    
+    public Bar() {
+    }
+    
+    public void setName(String name) {
+        this.name = name;
+    }
+    
+    public void setValue(String value) {
+        this.value = value;
+    }
+    
+    public String getName() {
+        return name;
+    }
+    
+    public String getValue() {
+        return value;
+    }
+
+}


Reply via email to