This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel.git
commit a42ba335d0f041c54eb3a0889503f79260a5b47a Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Sun Feb 14 15:38:09 2021 +0100 CAMEL-16199: camel-http - Optimize headers added as request headers in producer --- .../apache/camel/component/http/HttpProducer.java | 102 +++++++++++---------- 1 file changed, 56 insertions(+), 46 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 df4ba01..660fcb0 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 @@ -130,61 +130,71 @@ public class HttpProducer extends DefaultProducer { } // propagate headers as HTTP headers - final TypeConverter tc = exchange.getContext().getTypeConverter(); - for (Map.Entry<String, Object> entry : in.getHeaders().entrySet()) { - String key = entry.getKey(); - Object headerValue = entry.getValue(); - - if (headerValue != null) { - - if (headerValue instanceof String) { - // optimise for string values - String value = (String) headerValue; - if (strategy == null || !strategy.applyFilterToCamelHeaders(key, value, exchange)) { - httpRequest.addHeader(key, value); + if (strategy != null) { + final TypeConverter tc = exchange.getContext().getTypeConverter(); + for (Map.Entry<String, Object> entry : in.getHeaders().entrySet()) { + String key = entry.getKey(); + Object headerValue = entry.getValue(); + + if (headerValue != null) { + + if (headerValue instanceof String) { + // optimise for string values + String value = (String) headerValue; + if (!strategy.applyFilterToCamelHeaders(key, value, exchange)) { + httpRequest.addHeader(key, value); + } + continue; + } else if (headerValue instanceof Long || headerValue instanceof Integer + || headerValue instanceof Boolean) { + // optimise for other common types + String value = tc.convertTo(String.class, exchange, headerValue); + if (!strategy.applyFilterToCamelHeaders(key, value, exchange)) { + httpRequest.addHeader(key, value); + } + continue; } - continue; - } - // 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); + // 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; + // 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 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()); - // we should not add headers for the parameters in the uri if we bridge the endpoint - // as then we would duplicate headers on both the endpoint uri, and in HTTP headers as well - if (skipRequestHeaders != null && skipRequestHeaders.containsKey(key)) { - continue; - } - if (value != null && strategy != 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); + // we should not add headers for the parameters in the uri if we bridge the endpoint + // as then we would duplicate headers on both the endpoint uri, and in HTTP headers as well + if (skipRequestHeaders != null && skipRequestHeaders.containsKey(key)) { + continue; + } + 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); } - 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); + // 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); + } } } }