This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch camel-4.10.x in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/camel-4.10.x by this push: new 781491b4469 CAMEL-21828: Fix DefaultHeaderFilterStrategy when filtering in lower-case mode. (#17351) 781491b4469 is described below commit 781491b446921341f87a13824be4f7b5063776fc Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Tue Mar 4 12:16:58 2025 +0000 CAMEL-21828: Fix DefaultHeaderFilterStrategy when filtering in lower-case mode. (#17351) --- .../http/PlatformHttpCamelHeadersTest.java | 60 ++++++++++++++++++++++ .../impl/DefaultHeaderFilterStrategyTest.java | 22 ++++++++ .../camel/support/DefaultHeaderFilterStrategy.java | 32 +++++++++--- 3 files changed, 107 insertions(+), 7 deletions(-) diff --git a/components/camel-platform-http/src/test/java/org/apache/camel/component/platform/http/PlatformHttpCamelHeadersTest.java b/components/camel-platform-http/src/test/java/org/apache/camel/component/platform/http/PlatformHttpCamelHeadersTest.java new file mode 100644 index 00000000000..15d8ee7b68a --- /dev/null +++ b/components/camel-platform-http/src/test/java/org/apache/camel/component/platform/http/PlatformHttpCamelHeadersTest.java @@ -0,0 +1,60 @@ +/* + * 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.platform.http; + +import org.apache.camel.Exchange; +import org.apache.camel.builder.RouteBuilder; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import static io.restassured.RestAssured.given; + +public class PlatformHttpCamelHeadersTest extends AbstractPlatformHttpTest { + + @Test + void testFilterCamelHeaders() { + given() + .header("Accept", "application/json") + .header("User-Agent", "User-Agent-Camel") + .header("caMElHttpResponseCode", "503") + .port(port) + .expect() + .statusCode(200) + .header("Accept", (String) null) + .header("User-Agent", (String) null) + .header("CamelHttpResponseCode", (String) null) + .when() + .get("/get"); + } + + @Override + protected RouteBuilder routes() { + return new RouteBuilder() { + @Override + public void configure() { + from("platform-http:/get") + .process(e -> { + Assertions.assertEquals("application/json", e.getMessage().getHeader("Accept")); + Assertions.assertEquals("User-Agent-Camel", e.getMessage().getHeader("User-Agent")); + Assertions.assertNull(e.getMessage().getHeader(Exchange.HTTP_RESPONSE_CODE)); + }) + .setBody().constant(""); + } + }; + } + +} diff --git a/core/camel-core/src/test/java/org/apache/camel/impl/DefaultHeaderFilterStrategyTest.java b/core/camel-core/src/test/java/org/apache/camel/impl/DefaultHeaderFilterStrategyTest.java index 6ff641362c2..cb00968b24d 100644 --- a/core/camel-core/src/test/java/org/apache/camel/impl/DefaultHeaderFilterStrategyTest.java +++ b/core/camel-core/src/test/java/org/apache/camel/impl/DefaultHeaderFilterStrategyTest.java @@ -175,4 +175,26 @@ public class DefaultHeaderFilterStrategyTest extends ContextTestSupport { assertTrue(comp.applyFilterToExternalHeaders("org.apache.camel.component.jetty.session", "true", exchange)); } + @Test + public void testInStartsWithLowerCase() { + DefaultHeaderFilterStrategy comp = new DefaultHeaderFilterStrategy(); + comp.setLowerCase(true); + + comp.setInFilterStartsWith(CAMEL_FILTER_STARTS_WITH); + + Exchange exchange = new DefaultExchange(context); + exchange.getIn().setHeader("bar", 123); + exchange.getIn().setHeader("foo", "cheese"); + exchange.getIn().setHeader("caMElVersion", "3.7"); + exchange.getIn().setHeader("org.apache.CAMEL.component.jetty.session", "true"); + + assertTrue(comp.applyFilterToExternalHeaders("caMElVersion", 123, exchange)); + assertTrue(comp.applyFilterToExternalHeaders("cAmelResponseCode", 503, exchange)); + + assertFalse(comp.applyFilterToExternalHeaders("bar", 123, exchange)); + assertFalse(comp.applyFilterToExternalHeaders("foo", "cheese", exchange)); + assertTrue(comp.applyFilterToExternalHeaders("CamelVersion", "3.7", exchange)); + assertTrue(comp.applyFilterToExternalHeaders("org.apache.camel.component.jetty.session", "true", exchange)); + } + } diff --git a/core/camel-support/src/main/java/org/apache/camel/support/DefaultHeaderFilterStrategy.java b/core/camel-support/src/main/java/org/apache/camel/support/DefaultHeaderFilterStrategy.java index ae4b11b9791..28ef62125e5 100644 --- a/core/camel-support/src/main/java/org/apache/camel/support/DefaultHeaderFilterStrategy.java +++ b/core/camel-support/src/main/java/org/apache/camel/support/DefaultHeaderFilterStrategy.java @@ -17,7 +17,6 @@ package org.apache.camel.support; import java.util.HashSet; -import java.util.Locale; import java.util.Set; import java.util.regex.Pattern; @@ -344,20 +343,28 @@ public class DefaultHeaderFilterStrategy implements HeaderFilterStrategy { startsWith = inFilterStartsWith; } + String lower = null; + if (startsWith != null) { if (tryHeaderMatch(headerName, startsWith)) { return filterOnMatch; } + if (lowerCase) { + lower = headerName.toLowerCase(); + if (tryHeaderMatch(lower, startsWith)) { + return filterOnMatch; + } + } } if (pattern != null) { - if (tryPattern(headerName, pattern)) { + if (tryPattern(headerName, lower, pattern)) { return filterOnMatch; } } if (filter != null) { - if (evalFilterMatch(headerName, filter)) { + if (evalFilterMatch(headerName, lower, filter)) { return filterOnMatch; } } @@ -365,7 +372,7 @@ public class DefaultHeaderFilterStrategy implements HeaderFilterStrategy { return extendedFilter(direction, headerName, headerValue, exchange); } - private boolean tryPattern(String headerName, Pattern pattern) { + private boolean tryPattern(String headerName, String lower, Pattern pattern) { // optimize if its the default pattern as we know the pattern is to check for keys starting with Camel if (pattern == CAMEL_FILTER_PATTERN) { boolean match = headerName.startsWith("Camel") || headerName.startsWith("camel") @@ -373,6 +380,15 @@ public class DefaultHeaderFilterStrategy implements HeaderFilterStrategy { if (match) { return true; } + if (lowerCase) { + if (lower == null) { + lower = headerName.toLowerCase(); + } + match = lower.startsWith("camel") || lower.startsWith("org.apache.camel."); + if (match) { + return true; + } + } } else if (pattern.matcher(headerName).matches()) { return true; } @@ -389,15 +405,17 @@ public class DefaultHeaderFilterStrategy implements HeaderFilterStrategy { return false; } - private boolean evalFilterMatch(String headerName, Set<String> filter) { + private boolean evalFilterMatch(String headerName, String lower, Set<String> filter) { if (isCaseInsensitive()) { for (String filterString : filter) { if (filterString.equalsIgnoreCase(headerName)) { return true; } } - } else if (isLowerCase()) { - String lower = headerName.toLowerCase(Locale.ENGLISH); + } else if (lowerCase) { + if (lower == null) { + lower = headerName.toLowerCase(); + } if (filter.contains(lower)) { return true; }