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 2012/02/24 17:35:04 UTC

svn commit: r1293330 - in /camel/trunk: camel-core/src/main/java/org/apache/camel/util/ camel-core/src/test/java/org/apache/camel/util/ components/camel-http/src/main/java/org/apache/camel/component/http/ components/camel-http4/src/main/java/org/apache...

Author: davsclaus
Date: Fri Feb 24 16:35:03 2012
New Revision: 1293330

URL: http://svn.apache.org/viewvc?rev=1293330&view=rev
Log:
CAMEL-4984: Fixed http producers to send http headers with empty values.

Added:
    camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpProducerSendEmptyHeaderTest.java
    camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/jettyproducer/JettyHttpProducerSendEmptyHeaderTest.java
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/util/ObjectHelperTest.java
    camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java
    camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpProducer.java
    camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpProducer.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java?rev=1293330&r1=1293329&r2=1293330&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java Fri Feb 24 16:35:03 2012
@@ -460,6 +460,7 @@ public final class ObjectHelper {
      * we just create a singleton collection iterator over a single value
      * <p/>
      * Will default use comma for String separating String values.
+     * This method does <b>not</b> allow empty values
      *
      * @param value  the value
      * @return the iterator
@@ -473,13 +474,31 @@ public final class ObjectHelper {
      * Object[], a String with values separated by the given delimiter,
      * or a primitive type array; otherwise to simplify the caller's
      * code, we just create a singleton collection iterator over a single value
+     * <p/>
+     * This method does <b>not</b> allow empty values
      *
-     * @param value  the value
-     * @param  delimiter  delimiter for separating String values
+     * @param value      the value
+     * @param delimiter  delimiter for separating String values
      * @return the iterator
      */
-    @SuppressWarnings("unchecked")
     public static Iterator<Object> createIterator(Object value, String delimiter) {
+        return createIterator(value, delimiter, false);
+    }
+
+    /**
+     * Creates an iterator over the value if the value is a collection, an
+     * Object[], a String with values separated by the given delimiter,
+     * or a primitive type array; otherwise to simplify the caller's
+     * code, we just create a singleton collection iterator over a single value
+     *
+     * @param value             the value
+     * @param delimiter         delimiter for separating String values
+     * @param allowEmptyValues  whether to allow empty values
+     * @return the iterator
+     */
+    @SuppressWarnings("unchecked")
+    public static Iterator<Object> createIterator(Object value, String delimiter, final boolean allowEmptyValues) {
+
         // if its a message than we want to iterate its body
         if (value instanceof Message) {
             value = ((Message) value).getBody();
@@ -545,8 +564,7 @@ public final class ObjectHelper {
                     int idx = -1;
 
                     public boolean hasNext() {
-                        // empty string should not be regarded as having next
-                        return idx + 1 == 0 && ObjectHelper.isNotEmpty(s);
+                        return idx + 1 == 0 && (allowEmptyValues || ObjectHelper.isNotEmpty(s));
                     }
 
                     public String next() {

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/util/ObjectHelperTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/util/ObjectHelperTest.java?rev=1293330&r1=1293329&r2=1293330&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/util/ObjectHelperTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/util/ObjectHelperTest.java Fri Feb 24 16:35:03 2012
@@ -139,6 +139,15 @@ public class ObjectHelperTest extends Te
         assertSame("Should return the same iterator", iterator, ObjectHelper.createIterator(iterator));
     }
 
+    public void testCreateIteratorAllowEmpty() {
+        String s = "a,b,,c";
+        Iterator<String> it = CastUtils.cast(ObjectHelper.createIterator(s, ",", true));
+        assertEquals("a", it.next());
+        assertEquals("b", it.next());
+        assertEquals("", it.next());
+        assertEquals("c", it.next());
+    }
+
     public void testCreateIteratorWithStringAndCommaSeparator() {
         String s = "a,b,c";
         Iterator<String> it = CastUtils.cast(ObjectHelper.createIterator(s, ","));
@@ -147,6 +156,13 @@ public class ObjectHelperTest extends Te
         assertEquals("c", it.next());
     }
 
+    public void testCreateIteratorWithStringAndCommaSeparatorEmptyString() {
+        String s = "";
+        Iterator<String> it = CastUtils.cast(ObjectHelper.createIterator(s, ",", true));
+        assertEquals("", it.next());
+        assertFalse(it.hasNext());
+    }
+
     public void testCreateIteratorWithStringAndSemiColonSeparator() {
         String s = "a;b;c";
         Iterator<String> it = CastUtils.cast(ObjectHelper.createIterator(s, ";"));

Modified: camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java?rev=1293330&r1=1293329&r2=1293330&view=diff
==============================================================================
--- camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java (original)
+++ camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java Fri Feb 24 16:35:03 2012
@@ -103,8 +103,8 @@ public class HttpProducer extends Defaul
             Object headerValue = in.getHeader(key);
 
             if (headerValue != null) {
-                // use an iterator as there can be multiple values. (must not use a delimiter)
-                final Iterator<?> it = ObjectHelper.createIterator(headerValue, null);
+                // use an iterator as there can be multiple values. (must not use a delimiter, and allow empty values)
+                final Iterator<?> it = ObjectHelper.createIterator(headerValue, null, true);
 
                 // the value to add as request header
                 final List<String> values = new ArrayList<String>();

Modified: camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpProducer.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpProducer.java?rev=1293330&r1=1293329&r2=1293330&view=diff
==============================================================================
--- camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpProducer.java (original)
+++ camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpProducer.java Fri Feb 24 16:35:03 2012
@@ -103,8 +103,8 @@ public class HttpProducer extends Defaul
             Object headerValue = in.getHeader(key);
 
             if (headerValue != null) {
-                // use an iterator as there can be multiple values. (must not use a delimiter)
-                final Iterator<?> it = ObjectHelper.createIterator(headerValue, null);
+                // use an iterator as there can be multiple values. (must not use a delimiter, and allow empty values)
+                final Iterator<?> it = ObjectHelper.createIterator(headerValue, null, true);
 
                 // the value to add as request header
                 final List<String> values = new ArrayList<String>();

Modified: camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpProducer.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpProducer.java?rev=1293330&r1=1293329&r2=1293330&view=diff
==============================================================================
--- camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpProducer.java (original)
+++ camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpProducer.java Fri Feb 24 16:35:03 2012
@@ -153,8 +153,8 @@ public class JettyHttpProducer extends D
             Object headerValue = in.getHeader(key);
 
             if (headerValue != null) {
-                // use an iterator as there can be multiple values. (must not use a delimiter)
-                final Iterator<?> it = ObjectHelper.createIterator(headerValue, null);
+                // use an iterator as there can be multiple values. (must not use a delimiter, and allow empty values)
+                final Iterator<?> it = ObjectHelper.createIterator(headerValue, null, true);
 
                 // the values to add as a request header
                 final List<String> values = new ArrayList<String>();

Added: camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpProducerSendEmptyHeaderTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpProducerSendEmptyHeaderTest.java?rev=1293330&view=auto
==============================================================================
--- camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpProducerSendEmptyHeaderTest.java (added)
+++ camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpProducerSendEmptyHeaderTest.java Fri Feb 24 16:35:03 2012
@@ -0,0 +1,50 @@
+/**
+ * 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.jetty;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.junit.Test;
+
+/**
+ *
+ */
+public class HttpProducerSendEmptyHeaderTest extends BaseJettyTest {
+
+    @Test
+    public void testHttpProducerSendEmptyHeader() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(1);
+        mock.expectedHeaderReceived("foo", "");
+
+        template.sendBodyAndHeader("http://localhost:{{port}}/myapp/mytest", "Hello World", "foo", "");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("jetty:http://localhost:{{port}}/myapp/mytest")
+                    .convertBodyTo(String.class)
+                    .to("mock:result");
+            }
+        };
+    }
+}

Added: camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/jettyproducer/JettyHttpProducerSendEmptyHeaderTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/jettyproducer/JettyHttpProducerSendEmptyHeaderTest.java?rev=1293330&view=auto
==============================================================================
--- camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/jettyproducer/JettyHttpProducerSendEmptyHeaderTest.java (added)
+++ camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/jettyproducer/JettyHttpProducerSendEmptyHeaderTest.java Fri Feb 24 16:35:03 2012
@@ -0,0 +1,51 @@
+/**
+ * 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.jetty.jettyproducer;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.jetty.BaseJettyTest;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.junit.Test;
+
+/**
+ *
+ */
+public class JettyHttpProducerSendEmptyHeaderTest extends BaseJettyTest {
+
+    @Test
+    public void testHttpProducerSendEmptyHeader() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(1);
+        mock.expectedHeaderReceived("foo", "");
+
+        template.sendBodyAndHeader("jetty:http://localhost:{{port}}/myapp/mytest", "Hello World", "foo", "");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("jetty:http://localhost:{{port}}/myapp/mytest")
+                    .convertBodyTo(String.class)
+                    .to("mock:result");
+            }
+        };
+    }
+}