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:33 UTC
[camel] 09/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 62ae98fb0bd1df64a40098b4e3936c921e0ed0a4
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Sun Feb 16 08:59:55 2020 +0100
CAMEL-14565: camel-webhook refactor a bit and add docs for missing option.
---
.../camel/component/telegram/TelegramEndpoint.java | 7 +-
.../telegram/TelegramWebhookCallTest.java | 4 +-
.../component/webhook/WebhookCapableEndpoint.java | 3 +-
.../camel/component/webhook/WebhookComponent.java | 62 +---------------
.../component/webhook/WebhookConfiguration.java | 84 +++++++++++++++++++---
.../camel/component/webhook/WebhookEndpoint.java | 13 ++--
.../component/webhook/WebhookBasePathTest.java | 2 +-
.../component/webhook/WebhookHttpBindingTest.java | 4 +-
.../component/webhook/WebhookMultiRouteTest.java | 4 +-
.../component/webhook/support/TestEndpoint.java | 5 +-
10 files changed, 92 insertions(+), 96 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 ce66c59..59bfe5d 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,7 +29,6 @@ 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;
@@ -59,7 +58,6 @@ public class TelegramEndpoint extends ScheduledPollEndpoint implements WebhookCa
private int bufferSize = 4 * 1024;
private WebhookConfiguration webhookConfiguration;
- private RestConfiguration restConfiguration;
private AsyncHttpClient client;
private TelegramService telegramService;
@@ -138,7 +136,7 @@ public class TelegramEndpoint extends ScheduledPollEndpoint implements WebhookCa
@Override
public void registerWebhook() throws Exception {
- if (!telegramService.setWebhook(webhookConfiguration.computeFullExternalUrl(restConfiguration))) {
+ if (!telegramService.setWebhook(webhookConfiguration.computeFullExternalUrl())) {
throw new RuntimeCamelException("The Telegram API refused to register a webhook");
}
}
@@ -155,9 +153,8 @@ public class TelegramEndpoint extends ScheduledPollEndpoint implements WebhookCa
}
@Override
- public void setWebhookConfiguration(WebhookConfiguration webhookConfiguration, RestConfiguration restConfiguration) {
+ public void setWebhookConfiguration(WebhookConfiguration webhookConfiguration) {
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 accc17f..65fd3ef 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,7 +25,6 @@ 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;
@@ -39,8 +38,7 @@ public class TelegramWebhookCallTest extends TelegramTestSupport {
@Test
public void testWebhookCall() throws Exception {
WebhookConfiguration config = ((WebhookEndpoint) context().getRoute("webhook").getConsumer().getEndpoint()).getConfiguration();
- RestConfiguration rest = context().getRestConfiguration();
- String url = config.computeFullExternalUrl(rest);
+ String url = config.computeFullExternalUrl();
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 cc0f1f5..7fab370 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
@@ -65,9 +65,8 @@ 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, RestConfiguration restConfiguration);
+ void setWebhookConfiguration(WebhookConfiguration webhookConfiguration);
/**
* 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/WebhookComponent.java b/components/camel-webhook/src/main/java/org/apache/camel/component/webhook/WebhookComponent.java
index aff4fd7..2bf7cb3 100644
--- a/components/camel-webhook/src/main/java/org/apache/camel/component/webhook/WebhookComponent.java
+++ b/components/camel-webhook/src/main/java/org/apache/camel/component/webhook/WebhookComponent.java
@@ -17,21 +17,14 @@
package org.apache.camel.component.webhook;
import java.net.URISyntaxException;
-import java.net.UnknownHostException;
-import java.nio.charset.StandardCharsets;
-import java.security.MessageDigest;
-import java.security.NoSuchAlgorithmException;
-import java.util.Base64;
import java.util.Map;
import java.util.stream.Collectors;
import org.apache.camel.Endpoint;
-import org.apache.camel.RuntimeCamelException;
import org.apache.camel.spi.Metadata;
import org.apache.camel.spi.RestConfiguration;
import org.apache.camel.spi.annotations.Component;
import org.apache.camel.support.DefaultComponent;
-import org.apache.camel.util.HostUtils;
import org.apache.camel.util.ObjectHelper;
import org.apache.camel.util.URISupport;
@@ -56,8 +49,9 @@ public class WebhookComponent extends DefaultComponent {
WebhookConfiguration config = configuration != null ? configuration.copy() : new WebhookConfiguration();
RestConfiguration restConfig = getCamelContext().getRestConfiguration(config.getWebhookComponentName(), true);
+ config.storeConfiguration(restConfig);
- WebhookEndpoint endpoint = new WebhookEndpoint(uri, this, config, restConfig);
+ WebhookEndpoint endpoint = new WebhookEndpoint(uri, this, config);
setProperties(endpoint, parameters);
// we need to apply the params here
if (parameters != null && !parameters.isEmpty()) {
@@ -95,56 +89,4 @@ public class WebhookComponent extends DefaultComponent {
this.configuration = configuration;
}
- /**
- * Computes the URL of the webhook that should be used to bind the REST endpoint locally.
- */
- public static String computeServerUriPrefix(RestConfiguration restConfiguration) throws UnknownHostException {
- // if no explicit port/host configured, then use port from rest configuration
- String scheme = "http";
- String host = "";
- int port = 80;
-
- if (restConfiguration.getScheme() != null) {
- scheme = restConfiguration.getScheme();
- }
- if (restConfiguration.getHost() != null) {
- host = restConfiguration.getHost();
- }
- int num = restConfiguration.getPort();
- if (num > 0) {
- port = num;
- }
-
- // if no explicit hostname set then resolve the hostname
- if (ObjectHelper.isEmpty(host)) {
- if (restConfiguration.getHostNameResolver() == RestConfiguration.RestHostNameResolver.allLocalIp) {
- host = "0.0.0.0";
- } else if (restConfiguration.getHostNameResolver() == RestConfiguration.RestHostNameResolver.localHostName) {
- host = HostUtils.getLocalHostName();
- } else if (restConfiguration.getHostNameResolver() == RestConfiguration.RestHostNameResolver.localIp) {
- host = HostUtils.getLocalIp();
- }
- }
-
- return scheme + "://" + host + (port != 80 ? ":" + port : "");
- }
-
- /**
- * A default path is computed for the webhook if not provided by the user.
- * It uses a hash of the delegate endpoint in order for it to be reproducible.
- *
- * This is not random on purpose.
- */
- public static String computeDefaultPath(String uri) {
- try {
- MessageDigest md = MessageDigest.getInstance("SHA-256");
- md.update(uri.getBytes(StandardCharsets.UTF_8));
- byte[] digest = md.digest();
-
- return "/" + Base64.getUrlEncoder().encodeToString(digest);
- } catch (NoSuchAlgorithmException e) {
- throw new RuntimeCamelException("Cannot compute default webhook path", e);
- }
- }
-
}
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 98a312c..4395ea1 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
@@ -17,6 +17,10 @@
package org.apache.camel.component.webhook;
import java.net.UnknownHostException;
+import java.nio.charset.StandardCharsets;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+import java.util.Base64;
import org.apache.camel.RuntimeCamelException;
import org.apache.camel.spi.Metadata;
@@ -24,8 +28,8 @@ import org.apache.camel.spi.RestConfiguration;
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;
+import org.apache.camel.util.HostUtils;
+import org.apache.camel.util.ObjectHelper;
/**
* Configuration class for the webhook component.
@@ -33,6 +37,8 @@ import static org.apache.camel.component.webhook.WebhookComponent.computeServerU
@UriParams
public class WebhookConfiguration implements Cloneable {
+ private transient RestConfiguration restConfiguration;
+
/*
* Note: all properties start with the 'webhook' prefix to avoid collision with the delegate endpoint.
*/
@@ -73,33 +79,41 @@ public class WebhookConfiguration implements Cloneable {
}
}
+ // cannot use getter/setter as its not a regular option
+ public void storeConfiguration(RestConfiguration restConfiguration) {
+ this.restConfiguration = restConfiguration;
+ }
+
+ // cannot use getter/setter as its not a regular option
+ public RestConfiguration retrieveRestConfiguration() {
+ return restConfiguration;
+ }
+
/**
* 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 {
+ public String computeFullExternalUrl() throws UnknownHostException {
String externalServerUrl = this.webhookExternalUrl;
if (externalServerUrl == null) {
- externalServerUrl = computeServerUriPrefix(restConfiguration);
+ externalServerUrl = computeServerUriPrefix();
}
- String path = computeFullPath(restConfiguration, true);
+ String path = computeFullPath(true);
return externalServerUrl + path;
}
/**
* Computes the path part of the webhook.
*
- * @param restConfiguration rest configuration
* @param external indicates if it's the path seen by the external provider or the internal one.
* @return the webhook full path
*/
- public String computeFullPath(RestConfiguration restConfiguration, boolean external) {
+ public String computeFullPath(boolean external) {
// calculate the url to the rest service
String path = webhookPath;
if (path == null) {
- path = WebhookComponent.computeDefaultPath(endpointUri);
+ path = computeDefaultPath(endpointUri);
} else if (!path.startsWith("/")) {
path = "/" + path;
}
@@ -126,6 +140,58 @@ public class WebhookConfiguration implements Cloneable {
return path;
}
+ /**
+ * Computes the URL of the webhook that should be used to bind the REST endpoint locally.
+ */
+ public String computeServerUriPrefix() throws UnknownHostException {
+ // if no explicit port/host configured, then use port from rest configuration
+ String scheme = "http";
+ String host = "";
+ int port = 80;
+
+ if (restConfiguration.getScheme() != null) {
+ scheme = restConfiguration.getScheme();
+ }
+ if (restConfiguration.getHost() != null) {
+ host = restConfiguration.getHost();
+ }
+ int num = restConfiguration.getPort();
+ if (num > 0) {
+ port = num;
+ }
+
+ // if no explicit hostname set then resolve the hostname
+ if (ObjectHelper.isEmpty(host)) {
+ if (restConfiguration.getHostNameResolver() == RestConfiguration.RestHostNameResolver.allLocalIp) {
+ host = "0.0.0.0";
+ } else if (restConfiguration.getHostNameResolver() == RestConfiguration.RestHostNameResolver.localHostName) {
+ host = HostUtils.getLocalHostName();
+ } else if (restConfiguration.getHostNameResolver() == RestConfiguration.RestHostNameResolver.localIp) {
+ host = HostUtils.getLocalIp();
+ }
+ }
+
+ return scheme + "://" + host + (port != 80 ? ":" + port : "");
+ }
+
+ /**
+ * A default path is computed for the webhook if not provided by the user.
+ * It uses a hash of the delegate endpoint in order for it to be reproducible.
+ *
+ * This is not random on purpose.
+ */
+ public static String computeDefaultPath(String uri) {
+ try {
+ MessageDigest md = MessageDigest.getInstance("SHA-256");
+ md.update(uri.getBytes(StandardCharsets.UTF_8));
+ byte[] digest = md.digest();
+
+ return "/" + Base64.getUrlEncoder().encodeToString(digest);
+ } catch (NoSuchAlgorithmException e) {
+ throw new RuntimeCamelException("Cannot compute default webhook path", e);
+ }
+ }
+
public String getEndpointUri() {
return endpointUri;
}
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 e83f0a6..e4c7d6c 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
@@ -23,7 +23,6 @@ import org.apache.camel.DelegateEndpoint;
import org.apache.camel.Endpoint;
import org.apache.camel.Processor;
import org.apache.camel.Producer;
-import org.apache.camel.spi.RestConfiguration;
import org.apache.camel.spi.RestConsumerFactory;
import org.apache.camel.spi.UriEndpoint;
import org.apache.camel.spi.UriParam;
@@ -41,15 +40,13 @@ public class WebhookEndpoint extends DefaultEndpoint implements DelegateEndpoint
private static final Logger LOG = LoggerFactory.getLogger(WebhookEndpoint.class);
private WebhookCapableEndpoint delegateEndpoint;
- private RestConfiguration restConfiguration;
@UriParam(label = "advanced")
private WebhookConfiguration configuration;
- public WebhookEndpoint(String uri, WebhookComponent component, WebhookConfiguration configuration, RestConfiguration restConfiguration) {
+ public WebhookEndpoint(String uri, WebhookComponent component, WebhookConfiguration configuration) {
super(uri, component);
this.configuration = configuration;
- this.restConfiguration = restConfiguration;
}
@Override
@@ -61,14 +58,14 @@ public class WebhookEndpoint extends DefaultEndpoint implements DelegateEndpoint
public Consumer createConsumer(Processor processor) throws Exception {
RestConsumerFactory factory = WebhookUtils.locateRestConsumerFactory(getCamelContext(), configuration);
- String path = configuration.computeFullPath(restConfiguration, false);
- String serverUrl = WebhookComponent.computeServerUriPrefix(restConfiguration);
+ String path = configuration.computeFullPath(false);
+ String serverUrl = configuration.computeServerUriPrefix();
String url = serverUrl + path;
Processor handler = delegateEndpoint.createWebhookHandler(processor);
return new MultiRestConsumer(getCamelContext(), factory, this, handler, delegateEndpoint.getWebhookMethods(), url, path,
- restConfiguration, this::configureConsumer);
+ configuration.retrieveRestConfiguration(), this::configureConsumer);
}
@Override
@@ -79,7 +76,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, restConfiguration);
+ delegateEndpoint.setWebhookConfiguration(configuration);
}
@Override
diff --git a/components/camel-webhook/src/test/java/org/apache/camel/component/webhook/WebhookBasePathTest.java b/components/camel-webhook/src/test/java/org/apache/camel/component/webhook/WebhookBasePathTest.java
index 35c351e..ec7da63 100644
--- a/components/camel-webhook/src/test/java/org/apache/camel/component/webhook/WebhookBasePathTest.java
+++ b/components/camel-webhook/src/test/java/org/apache/camel/component/webhook/WebhookBasePathTest.java
@@ -41,7 +41,7 @@ public class WebhookBasePathTest extends WebhookTestBase {
@Test
public void testAutoPath() {
String result = template.requestBody("netty-http:http://localhost:" + port + "/base"
- + WebhookComponent.computeDefaultPath("wb-delegate://auto"), "", String.class);
+ + WebhookConfiguration.computeDefaultPath("wb-delegate://auto"), "", String.class);
assertEquals("auto: webhook", result);
}
diff --git a/components/camel-webhook/src/test/java/org/apache/camel/component/webhook/WebhookHttpBindingTest.java b/components/camel-webhook/src/test/java/org/apache/camel/component/webhook/WebhookHttpBindingTest.java
index e91b1d8..8e42d21 100644
--- a/components/camel-webhook/src/test/java/org/apache/camel/component/webhook/WebhookHttpBindingTest.java
+++ b/components/camel-webhook/src/test/java/org/apache/camel/component/webhook/WebhookHttpBindingTest.java
@@ -31,11 +31,11 @@ public class WebhookHttpBindingTest extends WebhookTestBase {
@Test
public void testWrapper() {
String result = template.requestBody("netty-http:http://localhost:" + port
- + WebhookComponent.computeDefaultPath("wb-delegate://xx"), "", String.class);
+ + WebhookConfiguration.computeDefaultPath("wb-delegate://xx"), "", String.class);
assertEquals("msg: webhook", result);
result = template.requestBodyAndHeader("netty-http:http://localhost:" + port
- + WebhookComponent.computeDefaultPath("wb-delegate://xx"), "", Exchange.HTTP_METHOD, "PUT", String.class);
+ + WebhookConfiguration.computeDefaultPath("wb-delegate://xx"), "", Exchange.HTTP_METHOD, "PUT", String.class);
assertEquals("msg: webhook", result);
}
diff --git a/components/camel-webhook/src/test/java/org/apache/camel/component/webhook/WebhookMultiRouteTest.java b/components/camel-webhook/src/test/java/org/apache/camel/component/webhook/WebhookMultiRouteTest.java
index 1a43bab..5a0b0c3 100644
--- a/components/camel-webhook/src/test/java/org/apache/camel/component/webhook/WebhookMultiRouteTest.java
+++ b/components/camel-webhook/src/test/java/org/apache/camel/component/webhook/WebhookMultiRouteTest.java
@@ -27,11 +27,11 @@ public class WebhookMultiRouteTest extends WebhookTestBase {
@Test
public void testMultiRoute() {
String result = template.requestBody("netty-http:http://localhost:" + port
- + WebhookComponent.computeDefaultPath("wb-delegate://yy"), "", String.class);
+ + WebhookConfiguration.computeDefaultPath("wb-delegate://yy"), "", String.class);
assertEquals("uri: webhook", result);
result = template.requestBody("netty-http:http://localhost:" + port
- + WebhookComponent.computeDefaultPath("wb-delegate://xx"), "", String.class);
+ + WebhookConfiguration.computeDefaultPath("wb-delegate://xx"), "", String.class);
assertEquals("msg: webhook", result);
}
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 0cd5a5a..fd53c36 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,7 +27,6 @@ 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;
/**
@@ -50,7 +49,6 @@ public class TestEndpoint extends DefaultEndpoint implements WebhookCapableEndpo
private Function<Processor, Consumer> consumer;
private WebhookConfiguration webhookConfiguration;
- private RestConfiguration restConfiguration;
private boolean singleton;
@@ -91,9 +89,8 @@ public class TestEndpoint extends DefaultEndpoint implements WebhookCapableEndpo
}
@Override
- public void setWebhookConfiguration(WebhookConfiguration webhookConfiguration, RestConfiguration restConfiguration) {
+ public void setWebhookConfiguration(WebhookConfiguration webhookConfiguration) {
this.webhookConfiguration = webhookConfiguration;
- this.restConfiguration = restConfiguration;
}
public WebhookConfiguration getWebhookConfiguration() {