This is an automated email from the ASF dual-hosted git repository. orpiske pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel.git
commit 0d1a2a52a96dfbd1217c6788f80c5d77c76a9ed6 Author: Otavio Rodolfo Piske <[email protected]> AuthorDate: Thu Nov 30 10:41:43 2023 -0300 CAMEL-20163: consolidate header manipulation methods --- .../apache/camel/component/http/HttpProducer.java | 45 +++++++--------------- .../http/vertx/VertxPlatformHttpSupport.java | 23 +++-------- .../org/apache/camel/support/http/HttpUtil.java | 39 +++++++++++++++++++ 3 files changed, 59 insertions(+), 48 deletions(-) diff --git a/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java b/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java index 53c7d7d7c8c..3622a57b8f3 100644 --- a/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java +++ b/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java @@ -205,37 +205,8 @@ public class HttpProducer extends DefaultProducer { // 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 - List<String> multiValues = null; - String prev = null; - - // if its a multi value then check each value if we can add it and for multi values they - // should be combined into a single value - while (it.hasNext()) { - String value = tc.convertTo(String.class, it.next()); - if (value != null && !strategy.applyFilterToCamelHeaders(key, value, exchange)) { - if (prev == null) { - prev = value; - } else { - // only create array for multi values when really needed - if (multiValues == null) { - multiValues = new ArrayList<>(); - multiValues.add(prev); - } - multiValues.add(value); - } - } - } - - // add the value(s) as a http request header - if (multiValues != null) { - // use the default toString of a ArrayList to create in the form [xxx, yyy] - // if multi valued, for a single value, then just output the value as is - String s = multiValues.size() > 1 ? multiValues.toString() : multiValues.get(0); - httpRequest.addHeader(key, s); - } else if (prev != null) { - httpRequest.addHeader(key, prev); - } + HttpUtil.applyHeader(strategy, exchange, it, tc, key, + (multiValues, prev) -> applyHeader(httpRequest, key, multiValues, prev)); } } } @@ -354,6 +325,18 @@ public class HttpProducer extends DefaultProducer { } } + private static void applyHeader(HttpUriRequest httpRequest, String key, List<String> multiValues, String prev) { + // add the value(s) as a http request header + if (multiValues != null) { + // use the default toString of a ArrayList to create in the form [xxx, yyy] + // if multi valued, for a single value, then just output the value as is + String s = multiValues.size() > 1 ? multiValues.toString() : multiValues.get(0); + httpRequest.addHeader(key, s); + } else if (prev != null) { + httpRequest.addHeader(key, prev); + } + } + @Override public HttpEndpoint getEndpoint() { return (HttpEndpoint) super.getEndpoint(); diff --git a/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpSupport.java b/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpSupport.java index fa3d46c62d6..595cee8af9d 100644 --- a/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpSupport.java +++ b/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpSupport.java @@ -46,6 +46,7 @@ import org.apache.camel.spi.HeaderFilterStrategy; import org.apache.camel.support.ExchangeHelper; import org.apache.camel.support.MessageHelper; import org.apache.camel.support.ObjectHelper; +import org.apache.camel.support.http.HttpUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -146,24 +147,12 @@ public final class VertxPlatformHttpSupport { private static void putHeader( HttpServerResponse response, HeaderFilterStrategy headerFilterStrategy, Exchange exchange, Iterator<?> it, TypeConverter tc, String key) { - String firstValue = null; - List<String> values = null; - - while (it.hasNext()) { - final String headerValue = tc.convertTo(String.class, it.next()); - if (headerValue != null && !headerFilterStrategy.applyFilterToCamelHeaders(key, headerValue, exchange)) { - if (firstValue == null) { - firstValue = headerValue; - } else { - if (values == null) { - values = new ArrayList<>(); - values.add(firstValue); - } - values.add(headerValue); - } - } - } + HttpUtil.applyHeader(headerFilterStrategy, exchange, it, tc, key, + (values, firstValue) -> applyHeader(response, key, values, firstValue)); + } + + private static void applyHeader(HttpServerResponse response, String key, List<String> values, String firstValue) { if (values != null) { response.putHeader(key, values); } else if (firstValue != null) { diff --git a/core/camel-support/src/main/java/org/apache/camel/support/http/HttpUtil.java b/core/camel-support/src/main/java/org/apache/camel/support/http/HttpUtil.java index dcb6a15fa76..7623a6b30f1 100644 --- a/core/camel-support/src/main/java/org/apache/camel/support/http/HttpUtil.java +++ b/core/camel-support/src/main/java/org/apache/camel/support/http/HttpUtil.java @@ -17,6 +17,9 @@ package org.apache.camel.support.http; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; import java.util.Map; import java.util.Set; import java.util.function.BiConsumer; @@ -24,6 +27,8 @@ import java.util.function.BiConsumer; import org.apache.camel.Exchange; import org.apache.camel.ExchangePropertyKey; import org.apache.camel.Message; +import org.apache.camel.TypeConverter; +import org.apache.camel.spi.HeaderFilterStrategy; import org.apache.camel.util.IOHelper; import org.apache.camel.util.StringHelper; import org.apache.camel.util.URISupport; @@ -188,4 +193,38 @@ public final class HttpUtil { return false; } + + /** + * Iterates over a list of values and passes them to the consumer after applying the filter strategy. This is mostly used to + * simplify setting headers for HTTP responses + * @param headerFilterStrategy the filter strategy to apply + * @param exchange an exchange to apply the header strategy + * @param it the iterator providing the values + * @param tc a type converter instance so that the values can be converted to string + * @param key a key associated with the values being iterated + * @param consumer a consumer method to receive the converted values. It can receive either a list of values or a single + * value. + */ + public static void applyHeader(HeaderFilterStrategy headerFilterStrategy, Exchange exchange, Iterator<?> it, + TypeConverter tc, String key, BiConsumer<List<String>, String> consumer) { + String firstValue = null; + List<String> values = null; + + while (it.hasNext()) { + final String headerValue = tc.convertTo(String.class, it.next()); + if (headerValue != null && !headerFilterStrategy.applyFilterToCamelHeaders(key, headerValue, exchange)) { + if (firstValue == null) { + firstValue = headerValue; + } else { + if (values == null) { + values = new ArrayList<>(); + values.add(firstValue); + } + values.add(headerValue); + } + } + } + + consumer.accept(values, firstValue); + } }
