This is an automated email from the ASF dual-hosted git repository.

lburgazzoli pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/master by this push:
     new c5930d3  default-component: add an option to control if RAW values 
need to be resolved when computing endpoint parameters
c5930d3 is described below

commit c5930d3bf811f8fb52669e435cc63a56de12a61c
Author: Luca Burgazzoli <lburgazz...@gmail.com>
AuthorDate: Tue Nov 24 10:39:23 2020 +0100

    default-component: add an option to control if RAW values need to be 
resolved when computing endpoint parameters
---
 .../apache/camel/support/DefaultComponentTest.java | 104 +++++++++++++++++++++
 .../org/apache/camel/support/DefaultComponent.java |  24 ++++-
 2 files changed, 124 insertions(+), 4 deletions(-)

diff --git 
a/core/camel-core/src/test/java/org/apache/camel/support/DefaultComponentTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/support/DefaultComponentTest.java
new file mode 100644
index 0000000..9e92738
--- /dev/null
+++ 
b/core/camel-core/src/test/java/org/apache/camel/support/DefaultComponentTest.java
@@ -0,0 +1,104 @@
+/*
+ * 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.support;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.camel.Consumer;
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Endpoint;
+import org.apache.camel.Processor;
+import org.apache.camel.Producer;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class DefaultComponentTest extends ContextTestSupport {
+    @Test
+    public void testResolveRawDefault() {
+        context.addComponent("dummy", new MyComponent());
+        MyEndpoint endpoint = 
context.getEndpoint("dummy:test?test=RAW(value)", MyEndpoint.class);
+        assertEquals("value", endpoint.getParameters().get("test"));
+    }
+
+    @Test
+    public void testResolveRawTrue() {
+        context.addComponent("dummy", new MyComponent(true));
+        MyEndpoint endpoint = 
context.getEndpoint("dummy:test?test=RAW(value)", MyEndpoint.class);
+        assertEquals("value", endpoint.getParameters().get("test"));
+    }
+
+    @Test
+    public void testResolveRawFalse() {
+        context.addComponent("dummy", new MyComponent(false));
+        MyEndpoint endpoint = 
context.getEndpoint("dummy:test?test=RAW(value)", MyEndpoint.class);
+        assertEquals("RAW(value)", endpoint.getParameters().get("test"));
+    }
+
+    private static class MyComponent extends DefaultComponent {
+        private final Boolean raw;
+
+        public MyComponent() {
+            this(null);
+        }
+
+        public MyComponent(Boolean raw) {
+            this.raw = raw;
+        }
+
+        @Override
+        protected Endpoint createEndpoint(String uri, String remaining, 
Map<String, Object> parameters) throws Exception {
+            MyEndpoint answer = new MyEndpoint(parameters);
+            parameters.clear();
+
+            return answer;
+        }
+
+        @Override
+        protected boolean resolveRawParameterValues() {
+            return raw != null ? raw : super.resolveRawParameterValues();
+        }
+    }
+
+    private static class MyEndpoint extends DefaultEndpoint {
+        private final Map<String, Object> parameters;
+
+        public MyEndpoint(Map<String, Object> parameters) {
+            this.parameters = new HashMap<>(parameters);
+        }
+
+        @Override
+        public Producer createProducer() throws Exception {
+            return null;
+        }
+
+        @Override
+        public Consumer createConsumer(Processor processor) throws Exception {
+            return null;
+        }
+
+        @Override
+        public boolean isSingleton() {
+            return false;
+        }
+
+        public Map<String, Object> getParameters() {
+            return parameters;
+        }
+    }
+}
diff --git 
a/core/camel-support/src/main/java/org/apache/camel/support/DefaultComponent.java
 
b/core/camel-support/src/main/java/org/apache/camel/support/DefaultComponent.java
index 03f11d3..6df994b 100644
--- 
a/core/camel-support/src/main/java/org/apache/camel/support/DefaultComponent.java
+++ 
b/core/camel-support/src/main/java/org/apache/camel/support/DefaultComponent.java
@@ -127,10 +127,13 @@ public abstract class DefaultComponent extends 
ServiceSupport implements Compone
         }
         // This special property is only to identify endpoints in a unique 
manner
         parameters.remove("hash");
-        // parameters using raw syntax: RAW(value)
-        // should have the token removed, so its only the value we have in 
parameters, as we are about to create
-        // an endpoint and want to have the parameter values without the RAW 
tokens
-        URISupport.resolveRawParameterValues(parameters);
+
+        if (resolveRawParameterValues()) {
+            // parameters using raw syntax: RAW(value)
+            // should have the token removed, so its only the value we have in 
parameters, as we are about to create
+            // an endpoint and want to have the parameter values without the 
RAW tokens
+            URISupport.resolveRawParameterValues(parameters);
+        }
 
         // use encoded or raw uri?
         uri = useRawUri() ? uri : encodedUri;
@@ -327,6 +330,19 @@ public abstract class DefaultComponent extends 
ServiceSupport implements Compone
         }
     }
 
+    /**
+     * Configure if the parameters using the RAW token syntax need to be 
resolved before being consumed by
+     * {@link #createEndpoint(String, Map)}.
+     * <p/>
+     * As the parameters are used to create an endpoint, by default they 
should have the token removed so its only the
+     * value we have in parameters however there are some cases where the 
endpoint may act as a proxy for another
+     * endpoint and you need to preserve the values as they are.
+     */
+    protected boolean resolveRawParameterValues() {
+        // should resolve raw parameters by default
+        return true;
+    }
+
     @Override
     public CamelContext getCamelContext() {
         return camelContext;

Reply via email to