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 2020/12/27 14:09:16 UTC

[camel] branch master updated: CAMEL-15992: camel-paho - Add support for DynamicAware endpoints for toD

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

davsclaus 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 5c153d8  CAMEL-15992: camel-paho - Add support for DynamicAware endpoints for toD
5c153d8 is described below

commit 5c153d8e0269fa5473c028767f4c77499879ba6f
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Sun Dec 27 15:05:55 2020 +0100

    CAMEL-15992: camel-paho - Add support for DynamicAware endpoints for toD
---
 .../services/org/apache/camel/send-dynamic/paho    |   2 +
 .../camel/component/paho/PahoSendDynamicAware.java | 125 +++++++++++++++++++++
 .../apache/camel/component/paho/PahoToDTest.java   |  81 +++++++++++++
 .../component/paho/PathToDSendDynamicTest.java     |  85 ++++++++++++++
 4 files changed, 293 insertions(+)

diff --git a/components/camel-paho/src/generated/resources/META-INF/services/org/apache/camel/send-dynamic/paho b/components/camel-paho/src/generated/resources/META-INF/services/org/apache/camel/send-dynamic/paho
new file mode 100644
index 0000000..16ba4d6
--- /dev/null
+++ b/components/camel-paho/src/generated/resources/META-INF/services/org/apache/camel/send-dynamic/paho
@@ -0,0 +1,2 @@
+# Generated by camel build tools - do NOT edit this file!
+class=org.apache.camel.component.paho.PahoSendDynamicAware
diff --git a/components/camel-paho/src/main/java/org/apache/camel/component/paho/PahoSendDynamicAware.java b/components/camel-paho/src/main/java/org/apache/camel/component/paho/PahoSendDynamicAware.java
new file mode 100644
index 0000000..3645399
--- /dev/null
+++ b/components/camel-paho/src/main/java/org/apache/camel/component/paho/PahoSendDynamicAware.java
@@ -0,0 +1,125 @@
+/*
+ * 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.paho;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.spi.SendDynamicAware;
+import org.apache.camel.spi.annotations.SendDynamic;
+import org.apache.camel.support.service.ServiceSupport;
+import org.apache.camel.util.StringHelper;
+
+/**
+ * Paho based {@link org.apache.camel.spi.SendDynamicAware} which allows to optimise Paho components with the toD
+ * (dynamic to) DSL in Camel. This implementation optimises by allowing to provide dynamic parameters via
+ * {@link PahoConstants#CAMEL_PAHO_OVERRIDE_TOPIC} header instead of the endpoint uri. That allows to use a static
+ * endpoint and its producer to service dynamic requests.
+ */
+@SendDynamic("paho")
+public class PahoSendDynamicAware extends ServiceSupport implements SendDynamicAware {
+
+    private CamelContext camelContext;
+    private String scheme;
+
+    @Override
+    public String getScheme() {
+        return scheme;
+    }
+
+    @Override
+    public void setScheme(String scheme) {
+        this.scheme = scheme;
+    }
+
+    @Override
+    public CamelContext getCamelContext() {
+        return camelContext;
+    }
+
+    @Override
+    public void setCamelContext(CamelContext camelContext) {
+        this.camelContext = camelContext;
+    }
+
+    @Override
+    public boolean isOnlyDynamicQueryParameters() {
+        return true;
+    }
+
+    @Override
+    public boolean isLenientProperties() {
+        return false;
+    }
+
+    @Override
+    public DynamicAwareEntry prepare(Exchange exchange, String uri, String originalUri) throws Exception {
+        return new DynamicAwareEntry(uri, originalUri, null, null);
+    }
+
+    @Override
+    public String resolveStaticUri(Exchange exchange, DynamicAwareEntry entry) throws Exception {
+        String topic = parseTopicName(entry.getUri());
+        if (topic != null) {
+            String originalTopic = parseTopicName(entry.getOriginalUri());
+            if (!topic.equals(originalTopic)) {
+                // okay the topic was dynamic, so use the original as endpoint name
+                String answer = entry.getUri();
+                answer = StringHelper.replaceFirst(answer, topic, originalTopic);
+                return answer;
+            }
+        }
+        return null;
+    }
+
+    @Override
+    public Processor createPreProcessor(Exchange exchange, DynamicAwareEntry entry) throws Exception {
+        if (exchange.getMessage().getHeader(PahoConstants.CAMEL_PAHO_OVERRIDE_TOPIC) != null) {
+            return null;
+        }
+
+        final String destinationName = parseTopicName(entry.getUri());
+        return new Processor() {
+            @Override
+            public void process(Exchange exchange) throws Exception {
+                exchange.getMessage().setHeader(PahoConstants.CAMEL_PAHO_OVERRIDE_TOPIC, destinationName);
+            }
+        };
+    }
+
+    @Override
+    public Processor createPostProcessor(Exchange exchange, DynamicAwareEntry entry) throws Exception {
+        // no post processor is needed
+        return null;
+    }
+
+    private String parseTopicName(String uri) {
+        // strip query
+        int pos = uri.indexOf('?');
+        if (pos != -1) {
+            uri = uri.substring(0, pos);
+        }
+        // topic name is after first colon
+        pos = uri.indexOf(':');
+        if (pos != -1) {
+            return uri.substring(pos + 1);
+        } else {
+            return null;
+        }
+    }
+
+}
diff --git a/components/camel-paho/src/test/java/org/apache/camel/component/paho/PahoToDTest.java b/components/camel-paho/src/test/java/org/apache/camel/component/paho/PahoToDTest.java
new file mode 100644
index 0000000..b42584a
--- /dev/null
+++ b/components/camel-paho/src/test/java/org/apache/camel/component/paho/PahoToDTest.java
@@ -0,0 +1,81 @@
+/*
+ * 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.paho;
+
+import org.apache.activemq.broker.BrokerService;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.test.AvailablePortFinder;
+import org.apache.camel.test.junit5.CamelTestSupport;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Test;
+
+public class PahoToDTest extends CamelTestSupport {
+
+    BrokerService broker;
+
+    int mqttPort = AvailablePortFinder.getNextAvailable();
+
+    @Override
+    protected boolean useJmx() {
+        return false;
+    }
+
+    @Override
+    public void doPreSetup() throws Exception {
+        super.doPreSetup();
+        broker = new BrokerService();
+        broker.setPersistent(false);
+        broker.addConnector("mqtt://localhost:" + mqttPort);
+        broker.start();
+    }
+
+    @Override
+    @AfterEach
+    public void tearDown() throws Exception {
+        super.tearDown();
+        broker.stop();
+    }
+
+    @Test
+    public void testToD() throws Exception {
+        getMockEndpoint("mock:bar").expectedBodiesReceived("Hello bar");
+        getMockEndpoint("mock:beer").expectedBodiesReceived("Hello beer");
+
+        template.sendBodyAndHeader("direct:start", "Hello bar", "where", "bar");
+        template.sendBodyAndHeader("direct:start", "Hello beer", "where", "beer");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                PahoComponent paho = context.getComponent("paho", PahoComponent.class);
+                paho.getConfiguration().setBrokerUrl("tcp://localhost:" + mqttPort);
+
+                // route message dynamic using toD
+                from("direct:start").toD("paho:${header.where}");
+
+                from("paho:bar").to("mock:bar");
+                from("paho:beer").to("mock:beer");
+            }
+        };
+    }
+
+}
diff --git a/components/camel-paho/src/test/java/org/apache/camel/component/paho/PathToDSendDynamicTest.java b/components/camel-paho/src/test/java/org/apache/camel/component/paho/PathToDSendDynamicTest.java
new file mode 100644
index 0000000..7b83cc5
--- /dev/null
+++ b/components/camel-paho/src/test/java/org/apache/camel/component/paho/PathToDSendDynamicTest.java
@@ -0,0 +1,85 @@
+/*
+ * 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.paho;
+
+import org.apache.activemq.broker.BrokerService;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.test.AvailablePortFinder;
+import org.apache.camel.test.junit5.CamelTestSupport;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class PathToDSendDynamicTest extends CamelTestSupport {
+
+    BrokerService broker;
+
+    int mqttPort = AvailablePortFinder.getNextAvailable();
+
+    @Override
+    protected boolean useJmx() {
+        return false;
+    }
+
+    @Override
+    public void doPreSetup() throws Exception {
+        super.doPreSetup();
+        broker = new BrokerService();
+        broker.setPersistent(false);
+        broker.addConnector("mqtt://localhost:" + mqttPort);
+        broker.start();
+    }
+
+    @Override
+    @AfterEach
+    public void tearDown() throws Exception {
+        super.tearDown();
+        broker.stop();
+    }
+
+    @Test
+    public void testToD() throws Exception {
+        template.sendBodyAndHeader("direct:start", "Hello bar", "where", "bar");
+        template.sendBodyAndHeader("direct:start", "Hello beer", "where", "beer");
+
+        // there should only be one paho endpoint
+        long count = context.getEndpoints().stream().filter(e -> e.getEndpointUri().startsWith("paho:")).count();
+        assertEquals(1, count, "There should only be 1 paho endpoint");
+
+        // and the messages should be in the queues
+        String out = consumer.receiveBody("paho:bar", 2000, String.class);
+        assertEquals("Hello bar", out);
+        out = consumer.receiveBody("paho:beer", 2000, String.class);
+        assertEquals("Hello beer", out);
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                PahoComponent paho = context.getComponent("paho", PahoComponent.class);
+                paho.getConfiguration().setBrokerUrl("tcp://localhost:" + mqttPort);
+
+                // route message dynamic using toD
+                from("direct:start").toD("paho:${header.where}?retained=true");
+            }
+        };
+    }
+
+}