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/02/16 15:55:31 UTC

[camel] 07/13: CAMEL-14565: camel-webhook refactor a bit and add docs for missing option.

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

commit 4fce14747bceffc4e82b1e76ee749a8218c7e166
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Sun Feb 16 08:49:36 2020 +0100

    CAMEL-14565: camel-webhook refactor a bit and add docs for missing option.
---
 .../camel/component/telegram/TelegramEndpoint.java    |  7 +++++--
 .../component/telegram/TelegramWebhookCallTest.java   |  4 +++-
 .../component/webhook/WebhookCapableEndpoint.java     |  8 +++++---
 .../camel/component/webhook/WebhookConfiguration.java | 19 +++++++++++++++++++
 .../camel/component/webhook/WebhookEndpoint.java      |  2 +-
 .../camel/component/webhook/support/TestEndpoint.java |  5 ++++-
 .../modules/ROOT/pages/webhook-component.adoc         | 12 ++++++------
 7 files changed, 43 insertions(+), 14 deletions(-)

diff --git a/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/TelegramEndpoint.java b/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/TelegramEndpoint.java
index 59bfe5d..ce66c59 100644
--- a/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/TelegramEndpoint.java
+++ b/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/TelegramEndpoint.java
@@ -29,6 +29,7 @@ import org.apache.camel.component.telegram.model.Update;
 import org.apache.camel.component.telegram.service.TelegramServiceRestBotAPIAdapter;
 import org.apache.camel.component.webhook.WebhookCapableEndpoint;
 import org.apache.camel.component.webhook.WebhookConfiguration;
+import org.apache.camel.spi.RestConfiguration;
 import org.apache.camel.spi.UriEndpoint;
 import org.apache.camel.spi.UriParam;
 import org.apache.camel.support.ScheduledPollEndpoint;
@@ -58,6 +59,7 @@ public class TelegramEndpoint extends ScheduledPollEndpoint implements WebhookCa
     private int bufferSize = 4 * 1024;
 
     private WebhookConfiguration webhookConfiguration;
+    private RestConfiguration restConfiguration;
 
     private AsyncHttpClient client;
     private TelegramService telegramService;
@@ -136,7 +138,7 @@ public class TelegramEndpoint extends ScheduledPollEndpoint implements WebhookCa
 
     @Override
     public void registerWebhook() throws Exception {
-        if (!telegramService.setWebhook(webhookConfiguration.computeFullExternalUrl())) {
+        if (!telegramService.setWebhook(webhookConfiguration.computeFullExternalUrl(restConfiguration))) {
             throw new RuntimeCamelException("The Telegram API refused to register a webhook");
         }
     }
@@ -153,8 +155,9 @@ public class TelegramEndpoint extends ScheduledPollEndpoint implements WebhookCa
     }
 
     @Override
-    public void setWebhookConfiguration(WebhookConfiguration webhookConfiguration) {
+    public void setWebhookConfiguration(WebhookConfiguration webhookConfiguration, RestConfiguration restConfiguration) {
         this.webhookConfiguration = webhookConfiguration;
+        this.restConfiguration = restConfiguration;
     }
 
     @Override
diff --git a/components/camel-telegram/src/test/java/org/apache/camel/component/telegram/TelegramWebhookCallTest.java b/components/camel-telegram/src/test/java/org/apache/camel/component/telegram/TelegramWebhookCallTest.java
index 65fd3ef..accc17f 100644
--- a/components/camel-telegram/src/test/java/org/apache/camel/component/telegram/TelegramWebhookCallTest.java
+++ b/components/camel-telegram/src/test/java/org/apache/camel/component/telegram/TelegramWebhookCallTest.java
@@ -25,6 +25,7 @@ import org.apache.camel.component.mock.MockEndpoint;
 import org.apache.camel.component.telegram.util.TelegramTestSupport;
 import org.apache.camel.component.webhook.WebhookConfiguration;
 import org.apache.camel.component.webhook.WebhookEndpoint;
+import org.apache.camel.spi.RestConfiguration;
 import org.apache.camel.test.AvailablePortFinder;
 import org.junit.jupiter.api.Test;
 
@@ -38,7 +39,8 @@ public class TelegramWebhookCallTest extends TelegramTestSupport {
     @Test
     public void testWebhookCall() throws Exception {
         WebhookConfiguration config = ((WebhookEndpoint) context().getRoute("webhook").getConsumer().getEndpoint()).getConfiguration();
-        String url = config.computeFullExternalUrl();
+        RestConfiguration rest = context().getRestConfiguration();
+        String url = config.computeFullExternalUrl(rest);
 
         try (InputStream content = getClass().getClassLoader().getResourceAsStream("messages/webhook-call.json")) {
             MockEndpoint mock = getMockEndpoint("mock:endpoint");
diff --git a/components/camel-webhook/src/main/java/org/apache/camel/component/webhook/WebhookCapableEndpoint.java b/components/camel-webhook/src/main/java/org/apache/camel/component/webhook/WebhookCapableEndpoint.java
index ff61cc0..cc0f1f5 100644
--- a/components/camel-webhook/src/main/java/org/apache/camel/component/webhook/WebhookCapableEndpoint.java
+++ b/components/camel-webhook/src/main/java/org/apache/camel/component/webhook/WebhookCapableEndpoint.java
@@ -20,6 +20,7 @@ import java.util.List;
 
 import org.apache.camel.Endpoint;
 import org.apache.camel.Processor;
+import org.apache.camel.spi.RestConfiguration;
 
 /**
  * A {@code WebhookCapableEndpoint} is a special kind of endpoint that is able to operate in webhook mode if required.
@@ -44,7 +45,7 @@ public interface WebhookCapableEndpoint extends Endpoint {
      * Register the webhook at the remote site using endpoint specific instruction.
      * <p>
      * Additional configuration is injected into the endpoint using the
-     * {@link WebhookCapableEndpoint#setWebhookConfiguration(WebhookConfiguration)} method.
+     * setWebhookConfiguration method.
      *
      * @throws Exception if something goes wrong during the registration.
      */
@@ -54,7 +55,7 @@ public interface WebhookCapableEndpoint extends Endpoint {
      * Unregister the webhook at the remote site using endpoint specific instruction.
      * <p>
      * Additional configuration is injected into the endpoint using the
-     * {@link WebhookCapableEndpoint#setWebhookConfiguration(WebhookConfiguration)} method.
+     * setWebhookConfiguration method.
      *
      * @throws Exception if something goes wrong during the un-registration.
      */
@@ -64,8 +65,9 @@ public interface WebhookCapableEndpoint extends Endpoint {
      * Used by the workflow manager to inject webhook configuration options.
      *
      * @param webhookConfiguration the webhook configuration options.
+     * @param restConfiguration    the webhook rest configuration options.
      */
-    void setWebhookConfiguration(WebhookConfiguration webhookConfiguration);
+    void setWebhookConfiguration(WebhookConfiguration webhookConfiguration, RestConfiguration restConfiguration);
 
     /**
      * Used by the endpoint to enlist the HTTP methods it's able to handle.
diff --git a/components/camel-webhook/src/main/java/org/apache/camel/component/webhook/WebhookConfiguration.java b/components/camel-webhook/src/main/java/org/apache/camel/component/webhook/WebhookConfiguration.java
index 35c1efe..98a312c 100644
--- a/components/camel-webhook/src/main/java/org/apache/camel/component/webhook/WebhookConfiguration.java
+++ b/components/camel-webhook/src/main/java/org/apache/camel/component/webhook/WebhookConfiguration.java
@@ -16,6 +16,8 @@
  */
 package org.apache.camel.component.webhook;
 
+import java.net.UnknownHostException;
+
 import org.apache.camel.RuntimeCamelException;
 import org.apache.camel.spi.Metadata;
 import org.apache.camel.spi.RestConfiguration;
@@ -23,6 +25,8 @@ import org.apache.camel.spi.UriParam;
 import org.apache.camel.spi.UriParams;
 import org.apache.camel.spi.UriPath;
 
+import static org.apache.camel.component.webhook.WebhookComponent.computeServerUriPrefix;
+
 /**
  * Configuration class for the webhook component.
  */
@@ -70,6 +74,21 @@ public class WebhookConfiguration implements Cloneable {
     }
 
     /**
+     * Computes the external URL of the webhook as seen by the remote webhook provider.
+     *
+     * @param restConfiguration rest configuration
+     * @return the webhook external URL
+     */
+    public String computeFullExternalUrl(RestConfiguration restConfiguration) throws UnknownHostException {
+        String externalServerUrl = this.webhookExternalUrl;
+        if (externalServerUrl == null) {
+            externalServerUrl = computeServerUriPrefix(restConfiguration);
+        }
+        String path = computeFullPath(restConfiguration, true);
+        return externalServerUrl + path;
+    }
+
+    /**
      * Computes the path part of the webhook.
      *
      * @param restConfiguration rest configuration
diff --git a/components/camel-webhook/src/main/java/org/apache/camel/component/webhook/WebhookEndpoint.java b/components/camel-webhook/src/main/java/org/apache/camel/component/webhook/WebhookEndpoint.java
index 7f7ba62..e83f0a6 100644
--- a/components/camel-webhook/src/main/java/org/apache/camel/component/webhook/WebhookEndpoint.java
+++ b/components/camel-webhook/src/main/java/org/apache/camel/component/webhook/WebhookEndpoint.java
@@ -79,7 +79,7 @@ public class WebhookEndpoint extends DefaultEndpoint implements DelegateEndpoint
             throw new IllegalArgumentException("The provided endpoint is not capable of being used in webhook mode: " + configuration.getEndpointUri());
         }
         delegateEndpoint = (WebhookCapableEndpoint) delegate;
-        delegateEndpoint.setWebhookConfiguration(configuration);
+        delegateEndpoint.setWebhookConfiguration(configuration, restConfiguration);
     }
 
     @Override
diff --git a/components/camel-webhook/src/test/java/org/apache/camel/component/webhook/support/TestEndpoint.java b/components/camel-webhook/src/test/java/org/apache/camel/component/webhook/support/TestEndpoint.java
index fd53c36..0cd5a5a 100644
--- a/components/camel-webhook/src/test/java/org/apache/camel/component/webhook/support/TestEndpoint.java
+++ b/components/camel-webhook/src/test/java/org/apache/camel/component/webhook/support/TestEndpoint.java
@@ -27,6 +27,7 @@ import org.apache.camel.Processor;
 import org.apache.camel.Producer;
 import org.apache.camel.component.webhook.WebhookCapableEndpoint;
 import org.apache.camel.component.webhook.WebhookConfiguration;
+import org.apache.camel.spi.RestConfiguration;
 import org.apache.camel.support.DefaultEndpoint;
 
 /**
@@ -49,6 +50,7 @@ public class TestEndpoint extends DefaultEndpoint implements WebhookCapableEndpo
     private Function<Processor, Consumer> consumer;
 
     private WebhookConfiguration webhookConfiguration;
+    private RestConfiguration restConfiguration;
 
     private boolean singleton;
 
@@ -89,8 +91,9 @@ public class TestEndpoint extends DefaultEndpoint implements WebhookCapableEndpo
     }
 
     @Override
-    public void setWebhookConfiguration(WebhookConfiguration webhookConfiguration) {
+    public void setWebhookConfiguration(WebhookConfiguration webhookConfiguration, RestConfiguration restConfiguration) {
         this.webhookConfiguration = webhookConfiguration;
+        this.restConfiguration = restConfiguration;
     }
 
     public WebhookConfiguration getWebhookConfiguration() {
diff --git a/docs/components/modules/ROOT/pages/webhook-component.adoc b/docs/components/modules/ROOT/pages/webhook-component.adoc
index d63003f..0dd53ea 100644
--- a/docs/components/modules/ROOT/pages/webhook-component.adoc
+++ b/docs/components/modules/ROOT/pages/webhook-component.adoc
@@ -70,7 +70,7 @@ with the following path and query parameters:
 [width="100%",cols="2,5,^1,2",options="header"]
 |===
 | Name | Description | Default | Type
-| *endpointUri* | The delegate uri. Must belong to a component that supports webhooks. |  | String
+| *endpointUri* | *Required* The delegate uri. Must belong to a component that supports webhooks. |  | String
 |===
 
 
@@ -80,12 +80,12 @@ with the following path and query parameters:
 [width="100%",cols="2,5,^1,2",options="header"]
 |===
 | Name | Description | Default | Type
-| *webhookAutoRegister* (common) | Automatically register the webhook at startup and unregister it on shutdown. | true | boolean
-| *webhookBasePath* (common) | The first (base) path element where the webhook will be exposed. It's a good practice to set it to a random string, so that it cannot be guessed by unauthorized parties. |  | String
-| *webhookComponentName* (common) | The Camel Rest component to use for the REST transport, such as netty-http. |  | String
-| *webhookExternalUrl* (common) | The URL of the current service as seen by the webhook provider |  | String
-| *webhookPath* (common) | The path where the webhook endpoint will be exposed (relative to basePath, if any) |  | String
 | *bridgeErrorHandler* (consumer) | Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions occurred while the consumer is trying to pickup incoming messages, or the likes, will now be processed as a message and handled by the routing Error Handler. By default the consumer will use the org.apache.camel.spi.ExceptionHandler to deal with exceptions, that will be logged at WARN or ERROR level and ignored. | false | boolean
+| *webhookAutoRegister* (consumer) | Automatically register the webhook at startup and unregister it on shutdown. | true | boolean
+| *webhookBasePath* (consumer) | The first (base) path element where the webhook will be exposed. It's a good practice to set it to a random string, so that it cannot be guessed by unauthorized parties. |  | String
+| *webhookComponentName* (consumer) | The Camel Rest component to use for the REST transport, such as netty-http. |  | String
+| *webhookExternalUrl* (consumer) | The URL of the current service as seen by the webhook provider |  | String
+| *webhookPath* (consumer) | The path where the webhook endpoint will be exposed (relative to basePath, if any) |  | String
 | *exceptionHandler* (consumer) | To let the consumer use a custom ExceptionHandler. Notice if the option bridgeErrorHandler is enabled then this option is not in use. By default the consumer will deal with exceptions, that will be logged at WARN or ERROR level and ignored. |  | ExceptionHandler
 | *exchangePattern* (consumer) | Sets the exchange pattern when the consumer creates an exchange. The value can be one of: InOnly, InOut, InOptionalOut |  | ExchangePattern
 | *basicPropertyBinding* (advanced) | Whether the endpoint should use basic property binding (Camel 2.x) or the newer property binding with additional capabilities | false | boolean