davsclaus commented on code in PR #27787:
URL: https://github.com/apache/camel/pull/27787#discussion_r4266930908


##########
components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java:
##########
@@ -340,12 +347,29 @@ public void onDone(Exchange exchange1) {
     }
 
     private static void applyHeader(HttpUriRequest httpRequest, String key, 
List<String> multiValues, String prev) {
-        // add the value(s) as a http request header
+        // add the value(s) as http request header(s), one header line per 
value
+        if (multiValues != null) {
+            for (String value : multiValues) {
+                httpRequest.addHeader(key, value);
+            }
+        } else if (prev != null) {
+            httpRequest.addHeader(key, prev);
+        }
+    }
+
+    private static boolean isSentAsOneJoinedHeader(Object value) {
+        if (value instanceof Iterator || (value instanceof Iterable && !(value 
instanceof Collection))) {

Review Comment:
   `ObjectHelper.createIterator` (which the old code used for every value) 
iterates a `java.util.stream.Stream` too. It is the first check in 
`createIterator`. A Stream is not an `Iterator` or `Iterable`, so it now falls 
through to `applyHeaderValue`. As far as I can see, it is then converted with 
`toString()`, which gives an object identity such as 
`java.util.stream.ReferencePipeline$Head@...`. To keep the old `[a, b]` form, 
add it here (and import `java.util.stream.Stream`):
   
   ```java
   if (value instanceof Iterator || value instanceof Stream
           || (value instanceof Iterable && !(value instanceof Collection))) {
   ```
   
   Please also update the comment at line 215 and the upgrade guide sentence 
that lists the values kept in the `[a, b]` form.



##########
docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc:
##########
@@ -226,6 +226,19 @@ of a `JsonArray`.
 A route that needs the old single header of the producer can set the header to 
a String, for example with
 `String.valueOf(list)`.
 
+=== camel-http - a header with several values is sent as several headers
+
+A message header whose value is a `java.util.Collection` or an object array 
(as the HTTP consumers map a request
+header that occurs more than once) was sent as one HTTP header with the values 
in the form `[a, b]`. The producer now
+sends one HTTP header per value, as camel-vertx-http and camel-knative-http 
do. Null values are skipped, and a

Review Comment:
   Nit: camel-graphql sends through the camel-http producer and copies all 
message headers into it (`GraphqlProducer`, `MessageHelper.copyHeaders`). So 
this change applies to camel-graphql too. A half sentence would help graphql 
users who search the guide for their component, for example: "This also applies 
to camel-graphql, which sends its requests with camel-http."
   



##########
components/camel-http/src/test/java/org/apache/camel/component/http/HttpProducerMultiValueHeaderTest.java:
##########
@@ -0,0 +1,105 @@
+/*
+ * 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.http;
+
+import java.nio.charset.StandardCharsets;
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Collectors;
+
+import org.apache.hc.core5.http.Header;
+import org.apache.hc.core5.http.HttpStatus;
+import org.apache.hc.core5.http.impl.bootstrap.HttpServer;
+import org.apache.hc.core5.http.impl.bootstrap.ServerBootstrap;
+import org.apache.hc.core5.http.io.entity.StringEntity;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class HttpProducerMultiValueHeaderTest extends BaseHttpTest {
+
+    private HttpServer localServer;
+
+    @Override
+    public void setupResources() throws Exception {
+        localServer = ServerBootstrap.bootstrap()
+                
.setCanonicalHostName("localhost").setHttpProcessor(getBasicHttpProcessor())
+                
.setConnectionReuseStrategy(getConnectionReuseStrategy()).setResponseFactory(getHttpResponseFactory())
+                .setSslContext(getSSLContext())
+                .register("/myapp", (request, response, context) -> {
+                    // reply with the values of the received header lines, 
separated by |
+                    String body = Arrays.stream(request.getHeaders("test"))
+                            .map(Header::getValue)
+                            .collect(Collectors.joining("|"));
+                    response.setEntity(new StringEntity(body, 
StandardCharsets.US_ASCII));
+                    response.setCode(HttpStatus.SC_OK);
+                }).create();
+        localServer.start();
+    }
+
+    @Override
+    public void cleanupResources() throws Exception {
+        if (localServer != null) {
+            localServer.stop();
+        }
+    }
+
+    @Test
+    public void testListIsOneHeaderPerValue() {
+        assertEquals("a|b|c", send(List.of("a", "b", "c")));
+    }
+
+    @Test
+    public void testArrayIsOneHeaderPerValue() {
+        assertEquals("a|b", send(new String[] { "a", "b" }));
+    }
+
+    @Test
+    public void testStringWithCommaIsOneHeader() {
+        assertEquals("a, b", send("a, b"));
+    }
+
+    @Test
+    public void testNullElementsAreSkipped() {
+        assertEquals("a|b", send(Arrays.asList("a", null, "b")));
+    }
+
+    @Test
+    public void testSingleElementListIsOneHeader() {
+        assertEquals("x", send(List.of("x")));
+    }
+
+    @Test
+    public void testIntArrayIsOneHeaderAsBefore() {
+        assertEquals("[1, 2]", send(new int[] { 1, 2 }));
+    }
+
+    @Test
+    public void testIteratorIsOneHeaderAsBefore() {
+        assertEquals("[a, b]", send(List.of("a", "b").iterator()));
+    }
+
+    @Test
+    public void testByteArrayIsSentAsText() {
+        assertEquals("abc", send("abc".getBytes(StandardCharsets.US_ASCII)));
+    }

Review Comment:
   Optional: a few more cases would pin what the upgrade guide documents:
   
   ```java
   @Test
   public void testStreamIsOneHeaderAsBefore() {
       assertEquals("[a, b]", send(Stream.of("a", "b")));
   }
   
   @Test
   public void testCharArrayIsSentAsText() {
       assertEquals("ab", send(new char[] { 'a', 'b' }));
   }
   
   @Test
   public void testIterableIsOneHeaderAsBefore() {
       Iterable<String> iterable = () -> List.of("a", "b").iterator();
       assertEquals("[a, b]", send(iterable));
   }
   ```
   
   A `Map` test is less useful, because its `toString()` order depends on the 
map type.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to