You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ac...@apache.org on 2022/10/05 10:50:19 UTC
[camel-kamelets] 01/02: Add a bean to rename headers and better understanding the source of an exchange
This is an automated email from the ASF dual-hosted git repository.
acosentino pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-kamelets.git
commit fa6c6e6c66ac4e966ac95d70e5679e4a75bacd36
Author: Andrea Cosentino <an...@gmail.com>
AuthorDate: Wed Oct 5 12:30:38 2022 +0200
Add a bean to rename headers and better understanding the source of an exchange
---
.../kamelets/utils/headers/RenameHeaders.java | 78 ++++++++++++++++++++++
.../kamelets/utils/headers/RenameHeadersTest.java | 58 ++++++++++++++++
2 files changed, 136 insertions(+)
diff --git a/library/camel-kamelets-utils/src/main/java/org/apache/camel/kamelets/utils/headers/RenameHeaders.java b/library/camel-kamelets-utils/src/main/java/org/apache/camel/kamelets/utils/headers/RenameHeaders.java
new file mode 100644
index 00000000..fdd02267
--- /dev/null
+++ b/library/camel-kamelets-utils/src/main/java/org/apache/camel/kamelets/utils/headers/RenameHeaders.java
@@ -0,0 +1,78 @@
+/*
+ * 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.kamelets.utils.headers;
+
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.InvalidPayloadException;
+import org.apache.camel.Processor;
+
+public class RenameHeaders implements Processor {
+
+ String prefix;
+ String renamingPrefix;
+
+ /**
+ * Default constructor.
+ */
+ public RenameHeaders() {
+ }
+
+ /**
+ * Constructor using fields.
+ * @param prefix a prefix to find all the headers to rename.
+ * @param renamingPrefix the renaming prefix to use on all the matching headers
+ */
+ public RenameHeaders(String prefix, String renamingPrefix) {
+ this.prefix = prefix;
+ this.renamingPrefix = renamingPrefix;
+ }
+
+ public void process(Exchange ex) throws InvalidPayloadException {
+ Map<String, Object> originalHeaders = ex.getMessage().getHeaders();
+ Map<String, Object> modifiedHeaders = new HashMap<>();
+ for (Map.Entry<String, Object> entry : originalHeaders.entrySet()) {
+ String key = entry.getKey();
+ Object val = entry.getValue();
+ if (key.startsWith(prefix)) {
+ String newKey = key.replaceFirst(prefix,renamingPrefix);
+ String subKey = newKey.substring(renamingPrefix.length());
+ String suffix = subKey.replaceAll(
+ String.format("%s|%s|%s", "(?<=[A-Z])(?=[A-Z][a-z])",
+ "(?<=[^A-Z])(?=[A-Z])",
+ "(?<=[A-Za-z])(?=[^A-Za-z])"), ".").toLowerCase();
+ modifiedHeaders.put(renamingPrefix+suffix, val);
+ } else {
+ modifiedHeaders.put(key, val);
+ }
+ }
+ ex.getMessage().setHeaders(modifiedHeaders);
+ }
+
+ public void setPrefix(String prefix) {
+ this.prefix = prefix;
+ }
+
+ public void setRenamingPrefix(String renamingPrefix) {
+ this.renamingPrefix = renamingPrefix;
+ }
+
+
+}
diff --git a/library/camel-kamelets-utils/src/test/java/org/apache/camel/kamelets/utils/headers/RenameHeadersTest.java b/library/camel-kamelets-utils/src/test/java/org/apache/camel/kamelets/utils/headers/RenameHeadersTest.java
new file mode 100644
index 00000000..f753e1e7
--- /dev/null
+++ b/library/camel-kamelets-utils/src/test/java/org/apache/camel/kamelets/utils/headers/RenameHeadersTest.java
@@ -0,0 +1,58 @@
+/*
+ * 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.kamelets.utils.headers;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.apache.camel.Exchange;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.apache.camel.support.DefaultExchange;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+class RenameHeadersTest {
+
+ private DefaultCamelContext camelContext;
+
+ private final ObjectMapper mapper = new ObjectMapper();
+
+ private RenameHeaders processor;
+
+ @BeforeEach
+ void setup() {
+ camelContext = new DefaultCamelContext();
+ }
+
+ @Test
+ void shouldRenameHeaders() throws Exception {
+ Exchange exchange = new DefaultExchange(camelContext);
+
+ exchange.getMessage().setHeader("CamelAwsS3Key", "test.txt");
+ exchange.getMessage().setHeader("CamelAwsS3BucketName", "kamelets-demo");
+ exchange.getMessage().setHeader("my-header", "header");
+
+ processor = new RenameHeaders();
+ processor.setPrefix("CamelAwsS3");
+ processor.setRenamingPrefix("aws.s3.");
+ processor.process(exchange);
+
+ Assertions.assertTrue(exchange.getMessage().getHeaders().containsKey("aws.s3.key"));
+ Assertions.assertTrue(exchange.getMessage().getHeaders().containsKey("aws.s3.bucket.name"));
+ Assertions.assertTrue(exchange.getMessage().getHeaders().containsKey("my-header"));
+ }
+
+}