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 9a124f1  CAMEL-15989: camel-vertx-kafka - Add support for DynamicAware 
endpoints for toD
9a124f1 is described below

commit 9a124f1b07f2f90c0a1ba22a076436f06246facc
Author: Claus Ibsen <[email protected]>
AuthorDate: Sun Dec 27 13:08:50 2020 +0100

    CAMEL-15989: camel-vertx-kafka - Add support for DynamicAware endpoints for 
toD
---
 .../org/apache/camel/send-dynamic/vertx-kafka      |   2 +
 .../vertx/kafka/VertxKafkaSendDynamicAware.java    | 125 +++++++++++++++++++++
 .../operations/VertxKafkaProducerOperations.java   |  12 +-
 .../component/vertx/kafka/VertxKafkaToDTest.java   |  47 ++++++++
 4 files changed, 178 insertions(+), 8 deletions(-)

diff --git 
a/components/camel-vertx-kafka/camel-vertx-kafka-component/src/generated/resources/META-INF/services/org/apache/camel/send-dynamic/vertx-kafka
 
b/components/camel-vertx-kafka/camel-vertx-kafka-component/src/generated/resources/META-INF/services/org/apache/camel/send-dynamic/vertx-kafka
new file mode 100644
index 0000000..338945c
--- /dev/null
+++ 
b/components/camel-vertx-kafka/camel-vertx-kafka-component/src/generated/resources/META-INF/services/org/apache/camel/send-dynamic/vertx-kafka
@@ -0,0 +1,2 @@
+# Generated by camel build tools - do NOT edit this file!
+class=org.apache.camel.component.vertx.kafka.VertxKafkaSendDynamicAware
diff --git 
a/components/camel-vertx-kafka/camel-vertx-kafka-component/src/main/java/org/apache/camel/component/vertx/kafka/VertxKafkaSendDynamicAware.java
 
b/components/camel-vertx-kafka/camel-vertx-kafka-component/src/main/java/org/apache/camel/component/vertx/kafka/VertxKafkaSendDynamicAware.java
new file mode 100644
index 0000000..73adccd
--- /dev/null
+++ 
b/components/camel-vertx-kafka/camel-vertx-kafka-component/src/main/java/org/apache/camel/component/vertx/kafka/VertxKafkaSendDynamicAware.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.vertx.kafka;
+
+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;
+
+/**
+ * Kafka based {@link org.apache.camel.spi.SendDynamicAware} which allows to 
optimise Vertx Kafka components with the
+ * toD (dynamic to) DSL in Camel. This implementation optimises by allowing to 
provide dynamic parameters via
+ * {@link VertxKafkaConstants#OVERRIDE_TOPIC} header instead of the endpoint 
uri. That allows to use a static endpoint
+ * and its producer to service dynamic requests.
+ */
+@SendDynamic("vertx-kafka")
+public class VertxKafkaSendDynamicAware 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 destination = parseTopicName(entry.getUri());
+        if (destination != null) {
+            String originalDestination = 
parseTopicName(entry.getOriginalUri());
+            if (!destination.equals(originalDestination)) {
+                // okay the topic was dynamic, so use the original as endpoint 
name
+                String answer = entry.getUri();
+                answer = StringHelper.replaceFirst(answer, destination, 
originalDestination);
+                return answer;
+            }
+        }
+        return null;
+    }
+
+    @Override
+    public Processor createPreProcessor(Exchange exchange, DynamicAwareEntry 
entry) throws Exception {
+        if 
(exchange.getMessage().getHeader(VertxKafkaConstants.OVERRIDE_TOPIC) != null) {
+            return null;
+        }
+
+        final String topicName = parseTopicName(entry.getUri());
+        return new Processor() {
+            @Override
+            public void process(Exchange exchange) throws Exception {
+                
exchange.getMessage().setHeader(VertxKafkaConstants.OVERRIDE_TOPIC, topicName);
+            }
+        };
+    }
+
+    @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-vertx-kafka/camel-vertx-kafka-component/src/main/java/org/apache/camel/component/vertx/kafka/operations/VertxKafkaProducerOperations.java
 
b/components/camel-vertx-kafka/camel-vertx-kafka-component/src/main/java/org/apache/camel/component/vertx/kafka/operations/VertxKafkaProducerOperations.java
index bdfb5e4..d29460c 100644
--- 
a/components/camel-vertx-kafka/camel-vertx-kafka-component/src/main/java/org/apache/camel/component/vertx/kafka/operations/VertxKafkaProducerOperations.java
+++ 
b/components/camel-vertx-kafka/camel-vertx-kafka-component/src/main/java/org/apache/camel/component/vertx/kafka/operations/VertxKafkaProducerOperations.java
@@ -56,26 +56,22 @@ public class VertxKafkaProducerOperations {
     }
 
     public boolean sendEvents(final Message inMessage, final AsyncCallback 
callback) {
-        ObjectHelper.notNull(inMessage, "exchange cannot be null");
-        ObjectHelper.notNull(callback, "callback cannot be null");
-
         return sendEvents(inMessage, unused -> LOG.debug("Processed one 
event..."), callback);
     }
 
     public boolean sendEvents(
             final Message inMessage, final Consumer<List<RecordMetadata>> 
resultCallback, final AsyncCallback callback) {
-        ObjectHelper.notNull(inMessage, "inMessage cannot be null");
-        ObjectHelper.notNull(callback, "callback cannot be null");
-
         sendAsyncEvents(inMessage)
                 .subscribe(resultCallback, error -> {
                     // error but we continue
-                    LOG.debug("Error processing async exchange with error:" + 
error.getMessage());
+                    if (LOG.isDebugEnabled()) {
+                        LOG.debug("Error processing async exchange with error: 
{}", error.getMessage());
+                    }
                     inMessage.getExchange().setException(error);
                     callback.done(false);
                 }, () -> {
                     // we are done from everything, so mark it as sync done
-                    LOG.debug("All events with exchange have been sent 
successfully.");
+                    LOG.trace("All events with exchange have been sent 
successfully.");
                     callback.done(false);
                 });
 
diff --git 
a/components/camel-vertx-kafka/camel-vertx-kafka-component/src/test/java/org/apache/camel/component/vertx/kafka/VertxKafkaToDTest.java
 
b/components/camel-vertx-kafka/camel-vertx-kafka-component/src/test/java/org/apache/camel/component/vertx/kafka/VertxKafkaToDTest.java
new file mode 100644
index 0000000..970a868
--- /dev/null
+++ 
b/components/camel-vertx-kafka/camel-vertx-kafka-component/src/test/java/org/apache/camel/component/vertx/kafka/VertxKafkaToDTest.java
@@ -0,0 +1,47 @@
+/*
+ * 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.vertx.kafka;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class VertxKafkaToDTest extends BaseEmbeddedKafkaTest {
+
+    @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 vertx-kafka endpoint
+        long count = context.getEndpoints().stream().filter(e -> 
e.getEndpointUri().startsWith("vertx-kafka:")).count();
+        assertEquals(1, count, "There should only be 1 vertx-kafka endpoint");
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                // route message dynamic using toD
+                from("direct:start").toD("vertx-kafka:${header.where}");
+            }
+        };
+    }
+
+}

Reply via email to