You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2023/12/30 15:15:11 UTC

(camel) 14/25: CAMEL-19749: Add variables as concept to Camel

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

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

commit f2febe6a26e21c25782722763f058a707778c7c9
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Fri Dec 29 19:52:20 2023 +0100

    CAMEL-19749: Add variables as concept to Camel
---
 .../camel/processor/RemoveVariableProcessor.java   | 48 ++++++++++++--
 .../camel/reifier/RemoveVariableReifier.java       | 13 +++-
 .../camel/processor/RemoveGlobalVariableTest.java  | 77 ++++++++++++++++++++++
 3 files changed, 131 insertions(+), 7 deletions(-)

diff --git a/core/camel-core-processor/src/main/java/org/apache/camel/processor/RemoveVariableProcessor.java b/core/camel-core-processor/src/main/java/org/apache/camel/processor/RemoveVariableProcessor.java
index e9e9238a80d..bcc150ebfbd 100644
--- a/core/camel-core-processor/src/main/java/org/apache/camel/processor/RemoveVariableProcessor.java
+++ b/core/camel-core-processor/src/main/java/org/apache/camel/processor/RemoveVariableProcessor.java
@@ -17,28 +17,60 @@
 package org.apache.camel.processor;
 
 import org.apache.camel.AsyncCallback;
+import org.apache.camel.CamelContext;
+import org.apache.camel.CamelContextAware;
 import org.apache.camel.Exchange;
+import org.apache.camel.Expression;
 import org.apache.camel.Traceable;
 import org.apache.camel.spi.IdAware;
 import org.apache.camel.spi.RouteIdAware;
+import org.apache.camel.spi.VariableRepository;
+import org.apache.camel.spi.VariableRepositoryFactory;
 import org.apache.camel.support.AsyncProcessorSupport;
+import org.apache.camel.util.StringHelper;
 
 /**
  * A processor which removes the variable
  */
-public class RemoveVariableProcessor extends AsyncProcessorSupport implements Traceable, IdAware, RouteIdAware {
+public class RemoveVariableProcessor extends AsyncProcessorSupport
+        implements Traceable, IdAware, RouteIdAware, CamelContextAware {
+    private CamelContext camelContext;
     private String id;
     private String routeId;
-    private final String variableName;
+    private final Expression variableName;
+    private VariableRepositoryFactory factory;
 
-    public RemoveVariableProcessor(String variableName) {
+    public RemoveVariableProcessor(Expression variableName) {
         this.variableName = variableName;
     }
 
+    @Override
+    public CamelContext getCamelContext() {
+        return camelContext;
+    }
+
+    @Override
+    public void setCamelContext(CamelContext camelContext) {
+        this.camelContext = camelContext;
+    }
+
     @Override
     public boolean process(Exchange exchange, AsyncCallback callback) {
         try {
-            exchange.removeVariable(variableName);
+            String key = variableName.evaluate(exchange, String.class);
+            String id = StringHelper.before(key, ":");
+            if (id != null) {
+                VariableRepository repo = factory.getVariableRepository(id);
+                if (repo != null) {
+                    key = StringHelper.after(key, ":");
+                    repo.removeVariable(key);
+                } else {
+                    exchange.setException(
+                            new IllegalArgumentException("VariableRepository with id: " + id + " does not exists"));
+                }
+            } else {
+                exchange.removeVariable(key);
+            }
         } catch (Exception e) {
             exchange.setException(e);
         }
@@ -47,6 +79,12 @@ public class RemoveVariableProcessor extends AsyncProcessorSupport implements Tr
         return true;
     }
 
+    @Override
+    protected void doBuild() throws Exception {
+        super.doBuild();
+        factory = getCamelContext().getCamelContextExtension().getContextPlugin(VariableRepositoryFactory.class);
+    }
+
     @Override
     public String toString() {
         return id;
@@ -78,7 +116,7 @@ public class RemoveVariableProcessor extends AsyncProcessorSupport implements Tr
     }
 
     public String getVariableName() {
-        return variableName;
+        return variableName.toString();
     }
 
 }
diff --git a/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/RemoveVariableReifier.java b/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/RemoveVariableReifier.java
index 79fe16b3586..4774ac0c231 100644
--- a/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/RemoveVariableReifier.java
+++ b/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/RemoveVariableReifier.java
@@ -16,11 +16,13 @@
  */
 package org.apache.camel.reifier;
 
+import org.apache.camel.Expression;
 import org.apache.camel.Processor;
 import org.apache.camel.Route;
 import org.apache.camel.model.ProcessorDefinition;
 import org.apache.camel.model.RemoveVariableDefinition;
 import org.apache.camel.processor.RemoveVariableProcessor;
+import org.apache.camel.support.LanguageSupport;
 
 public class RemoveVariableReifier extends ProcessorReifier<RemoveVariableDefinition> {
 
@@ -30,7 +32,14 @@ public class RemoveVariableReifier extends ProcessorReifier<RemoveVariableDefini
 
     @Override
     public Processor createProcessor() throws Exception {
-        String name = definition.getName();
-        return new RemoveVariableProcessor(parseString(name));
+        Expression nameExpr;
+        String key = parseString(definition.getName());
+        if (LanguageSupport.hasSimpleFunction(key)) {
+            nameExpr = camelContext.resolveLanguage("simple").createExpression(key);
+        } else {
+            nameExpr = camelContext.resolveLanguage("constant").createExpression(key);
+        }
+        nameExpr.init(camelContext);
+        return new RemoveVariableProcessor(nameExpr);
     }
 }
diff --git a/core/camel-core/src/test/java/org/apache/camel/processor/RemoveGlobalVariableTest.java b/core/camel-core/src/test/java/org/apache/camel/processor/RemoveGlobalVariableTest.java
new file mode 100644
index 00000000000..44ab6ae7a34
--- /dev/null
+++ b/core/camel-core/src/test/java/org/apache/camel/processor/RemoveGlobalVariableTest.java
@@ -0,0 +1,77 @@
+/*
+ * 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.processor;
+
+import java.util.List;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertNull;
+
+public class RemoveGlobalVariableTest extends ContextTestSupport {
+    private MockEndpoint end;
+    private MockEndpoint mid;
+    private String variableName = "foo";
+    private String expectedVariableValue = "bar";
+
+    @Test
+    public void testSetHeaderMidRouteThenRemove() throws Exception {
+        mid.expectedMessageCount(1);
+        end.expectedMessageCount(1);
+
+        template.sendBody("direct:start", "<blah/>");
+
+        // make sure we got the message
+        assertMockEndpointsSatisfied();
+
+        List<Exchange> midExchanges = mid.getExchanges();
+        Exchange midExchange = midExchanges.get(0);
+        String actualVariableValue = midExchange.getVariable(variableName, String.class);
+        // should be stored on global so null
+        assertNull(actualVariableValue);
+
+        List<Exchange> endExchanges = end.getExchanges();
+        Exchange endExchange = endExchanges.get(0);
+
+        // should be stored as global variable (but removed)
+        assertNull(context.getVariable(variableName));
+    }
+
+    @Override
+    @BeforeEach
+    public void setUp() throws Exception {
+        super.setUp();
+        end = getMockEndpoint("mock:end");
+        mid = getMockEndpoint("mock:mid");
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            public void configure() {
+                from("direct:start").setVariable("global:" + variableName).constant(expectedVariableValue).to("mock:mid")
+                        .removeVariable("global:" + variableName)
+                        .to("mock:end");
+            }
+        };
+    }
+}