You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by lb...@apache.org on 2019/11/26 10:43:14 UTC

[camel-k-runtime] 18/18: YAML: add support for WireTap EIP

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-k-runtime.git

commit 4a9ebf38c4940d0e4a753577244d120a7429a54e
Author: lburgazzoli <lb...@gmail.com>
AuthorDate: Sun Nov 24 23:07:56 2019 +0100

    YAML: add support for WireTap EIP
---
 .../k/loader/yaml/parser/WireTapStepParser.java    | 110 +++++++++++++++++++++
 .../camel/k/loader/yaml/parser/WireTapTest.groovy  |  60 +++++++++++
 2 files changed, 170 insertions(+)

diff --git a/camel-k-loader-yaml/src/main/java/org/apache/camel/k/loader/yaml/parser/WireTapStepParser.java b/camel-k-loader-yaml/src/main/java/org/apache/camel/k/loader/yaml/parser/WireTapStepParser.java
new file mode 100644
index 0000000..b9aa194
--- /dev/null
+++ b/camel-k-loader-yaml/src/main/java/org/apache/camel/k/loader/yaml/parser/WireTapStepParser.java
@@ -0,0 +1,110 @@
+/*
+ * 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.k.loader.yaml.parser;
+
+import java.io.UnsupportedEncodingException;
+import java.net.URISyntaxException;
+import java.util.List;
+import java.util.Map;
+
+import com.fasterxml.jackson.annotation.JsonIgnore;
+import org.apache.camel.k.annotation.yaml.YAMLStepParser;
+import org.apache.camel.k.loader.yaml.model.Step;
+import org.apache.camel.model.ExpressionSubElementDefinition;
+import org.apache.camel.model.ProcessorDefinition;
+import org.apache.camel.model.SetHeaderDefinition;
+import org.apache.camel.model.ToDynamicDefinition;
+import org.apache.camel.model.WireTapDefinition;
+import org.apache.camel.model.language.ExpressionDefinition;
+import org.apache.camel.reifier.ProcessorReifier;
+import org.apache.camel.reifier.WireTapReifier;
+import org.apache.camel.util.ObjectHelper;
+import org.apache.camel.util.URISupport;
+
+@YAMLStepParser("wiretap")
+public class WireTapStepParser implements ProcessorStepParser {
+    static {
+        ProcessorReifier.registerReifier(Definition.class, WireTapReifier::new);
+    }
+
+    @Override
+    public ProcessorDefinition<?> toProcessor(Context context) {
+        Definition definition = context.node(Definition.class);
+
+        WireTapDefinition answer = new WireTapDefinition();
+        ObjectHelper.ifNotEmpty(definition.processorRef, answer::setNewExchangeProcessorRef);
+        ObjectHelper.ifNotEmpty(definition.executorServiceRef, answer::setExecutorServiceRef);
+        ObjectHelper.ifNotEmpty(definition.onPrepareRef, answer::onPrepareRef);
+        ObjectHelper.ifNotEmpty(definition.copy, answer::setCopy);
+        ObjectHelper.ifNotEmpty(definition.dynamicUri, answer::setDynamicUri);
+
+        if (definition.newExchange != null) {
+            answer.setNewExchangeExpression(definition.newExchange);
+
+            if (definition.newExchange.headers != null) {
+                answer.setHeaders(definition.newExchange.headers);
+            }
+        }
+
+        answer.setUri(definition.getEndpointUri());
+
+        return answer;
+    }
+
+    public static final class Definition extends ToDynamicDefinition implements Step.Definition {
+        public String processorRef;
+        public String executorServiceRef;
+        public String onPrepareRef;
+        public Boolean copy;
+        public Boolean dynamicUri;
+        public NewExchangeDefinition newExchange;
+        public Map<String, Object> parameters;
+
+        @JsonIgnore
+        public String getEndpointUri() {
+            String answer = getUri();
+
+            if (parameters != null) {
+                try {
+                    answer = URISupport.appendParametersToURI(answer, parameters);
+                } catch (URISyntaxException | UnsupportedEncodingException e) {
+                    throw new RuntimeException(e);
+                }
+            }
+
+            return answer;
+        }
+    }
+
+    public static final class NewExchangeDefinition extends ExpressionSubElementDefinition implements HasExpression {
+        public List<HeaderDefinition> headers;
+
+        @Override
+        public void setExpression(ExpressionDefinition expressionDefinition) {
+            super.setExpressionType(expressionDefinition);
+        }
+
+        @Override
+        public ExpressionDefinition getExpression() {
+            return super.getExpressionType();
+        }
+    }
+
+    public static final class HeaderDefinition extends SetHeaderDefinition implements HasExpression, Step.Definition {
+    }
+}
+
diff --git a/camel-k-loader-yaml/src/test/groovy/org/apache/camel/k/loader/yaml/parser/WireTapTest.groovy b/camel-k-loader-yaml/src/test/groovy/org/apache/camel/k/loader/yaml/parser/WireTapTest.groovy
new file mode 100644
index 0000000..d08429d
--- /dev/null
+++ b/camel-k-loader-yaml/src/test/groovy/org/apache/camel/k/loader/yaml/parser/WireTapTest.groovy
@@ -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.k.loader.yaml.parser
+
+import org.apache.camel.k.loader.yaml.TestSupport
+import org.apache.camel.model.WireTapDefinition
+import org.apache.camel.model.language.ConstantExpression
+import org.apache.camel.model.language.SimpleExpression
+
+class WireTapTest extends TestSupport {
+
+    def "definition"() {
+        given:
+            def stepContext = stepContext('''
+                 uri: "direct:wt"
+                 new-exchange:
+                     simple: "${body}"
+                     headers:
+                         - name: "Header_1"
+                           simple: "${header.MyHeader1}"
+                         - name: "Header_2"
+                           constant: "test"                 
+                     body:
+            ''')
+        when:
+            def processor = new WireTapStepParser().toProcessor(stepContext)
+        then:
+            with (processor, WireTapDefinition) {
+                with (newExchangeExpression?.expression, SimpleExpression) {
+                    language == 'simple'
+                    expression == '${body}'
+                }
+
+                headers?.size() == 2
+
+                with (headers[0].expression, SimpleExpression) {
+                    language == 'simple'
+                    expression == '${header.MyHeader1}'
+                }
+                with (headers[1].expression, ConstantExpression) {
+                    language == 'constant'
+                    expression == 'test'
+                }
+            }
+    }
+}