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 2019/12/21 08:29:45 UTC
[camel] branch camel-3.0.x updated: CAMEL-14303: Fix all components
for matchOnUriPrefix. (#3430)
This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch camel-3.0.x
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/camel-3.0.x by this push:
new 7f0c407 CAMEL-14303: Fix all components for matchOnUriPrefix. (#3430)
7f0c407 is described below
commit 7f0c407db58a32cce54efb9522791cb4c9f37cd4
Author: Bob Paulin <bo...@bobpaulin.com>
AuthorDate: Sat Dec 21 02:29:34 2019 -0600
CAMEL-14303: Fix all components for matchOnUriPrefix. (#3430)
---
.../camel/component/jetty/JettyHttpComponent.java | 43 ++----
.../component/netty/http/NettyHttpComponent.java | 36 +----
.../platform/http/PlatformHttpComponent.java | 38 ++---
components/camel-servlet/pom.xml | 5 +
.../camel/component/servlet/ServletComponent.java | 37 +----
.../servlet/rest/RestApiMatchUriServletTest.java | 62 +++++++++
.../camel/component/sparkrest/SparkComponent.java | 18 +--
.../component/undertow/UndertowComponent.java | 48 ++-----
.../apache/camel/support/RestComponentHelper.java | 154 +++++++++++++++++++++
9 files changed, 266 insertions(+), 175 deletions(-)
diff --git a/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java b/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java
index 90faaba..2958953 100644
--- a/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java
+++ b/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java
@@ -28,7 +28,6 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
-import java.util.Locale;
import java.util.Map;
import javax.management.MBeanServer;
@@ -58,10 +57,10 @@ import org.apache.camel.spi.Metadata;
import org.apache.camel.spi.RestApiConsumerFactory;
import org.apache.camel.spi.RestConfiguration;
import org.apache.camel.spi.RestConsumerFactory;
+import org.apache.camel.support.RestComponentHelper;
import org.apache.camel.support.jsse.SSLContextParameters;
import org.apache.camel.support.service.ServiceHelper;
import org.apache.camel.util.FileUtil;
-import org.apache.camel.util.HostUtils;
import org.apache.camel.util.ObjectHelper;
import org.apache.camel.util.PropertiesHelper;
import org.apache.camel.util.StringHelper;
@@ -1071,50 +1070,24 @@ public abstract class JettyHttpComponent extends HttpCommonComponent implements
// if no explicit hostname set then resolve the hostname
if (ObjectHelper.isEmpty(host)) {
- if (config.getHostNameResolver() == RestConfiguration.RestHostNameResolver.allLocalIp) {
- host = "0.0.0.0";
- } else if (config.getHostNameResolver() == RestConfiguration.RestHostNameResolver.localHostName) {
- host = HostUtils.getLocalHostName();
- } else if (config.getHostNameResolver() == RestConfiguration.RestHostNameResolver.localIp) {
- host = HostUtils.getLocalIp();
- }
+ host = RestComponentHelper.resolveRestHostName(host, config);
}
- Map<String, Object> map = new HashMap<>();
- // build query string, and append any endpoint configuration properties
- if (config.getComponent() == null || config.getComponent().equals("jetty")) {
- // setup endpoint options
- if (config.getEndpointProperties() != null && !config.getEndpointProperties().isEmpty()) {
- map.putAll(config.getEndpointProperties());
- }
- }
+ Map<String, Object> map = RestComponentHelper.initRestEndpointProperties("jetty", config);
boolean cors = config.isEnableCORS();
if (cors) {
// allow HTTP Options as we want to handle CORS in rest-dsl
map.put("optionsEnabled", "true");
}
-
- String query = URISupport.createQueryString(map);
-
- String url;
+
if (api) {
- url = "jetty:%s://%s:%s/%s?matchOnUriPrefix=true&httpMethodRestrict=%s";
- } else {
- url = "jetty:%s://%s:%s/%s?httpMethodRestrict=%s";
- }
-
- // must use upper case for restrict
- String restrict = verb.toUpperCase(Locale.US);
- if (cors) {
- restrict += ",OPTIONS";
+ map.put("matchOnUriPrefix", "true");
}
- // get the endpoint
- url = String.format(url, scheme, host, port, path, restrict);
+
+ RestComponentHelper.addHttpRestrictParam(map, verb, cors);
- if (!query.isEmpty()) {
- url = url + "&" + query;
- }
+ String url = RestComponentHelper.createRestConsumerUrl("jetty", scheme, host, port, path, map);
JettyHttpEndpoint endpoint = camelContext.getEndpoint(url, JettyHttpEndpoint.class);
setProperties(camelContext, endpoint, parameters);
diff --git a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpComponent.java b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpComponent.java
index cbe6fe0..b15bddd 100644
--- a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpComponent.java
+++ b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpComponent.java
@@ -18,7 +18,6 @@ package org.apache.camel.component.netty.http;
import java.net.URI;
import java.util.HashMap;
-import java.util.Locale;
import java.util.Map;
import org.apache.camel.CamelContext;
@@ -42,10 +41,10 @@ import org.apache.camel.spi.RestConsumerFactory;
import org.apache.camel.spi.RestProducerFactory;
import org.apache.camel.spi.annotations.Component;
import org.apache.camel.support.PropertyBindingSupport;
+import org.apache.camel.support.RestComponentHelper;
import org.apache.camel.support.RestProducerFactoryHelper;
import org.apache.camel.support.service.ServiceHelper;
import org.apache.camel.util.FileUtil;
-import org.apache.camel.util.HostUtils;
import org.apache.camel.util.ObjectHelper;
import org.apache.camel.util.PropertiesHelper;
import org.apache.camel.util.URISupport;
@@ -373,23 +372,10 @@ public class NettyHttpComponent extends NettyComponent implements HeaderFilterSt
// if no explicit hostname set then resolve the hostname
if (ObjectHelper.isEmpty(host)) {
- if (config.getHostNameResolver() == RestConfiguration.RestHostNameResolver.allLocalIp) {
- host = "0.0.0.0";
- } else if (config.getHostNameResolver() == RestConfiguration.RestHostNameResolver.localHostName) {
- host = HostUtils.getLocalHostName();
- } else if (config.getHostNameResolver() == RestConfiguration.RestHostNameResolver.localIp) {
- host = HostUtils.getLocalIp();
- }
+ host = RestComponentHelper.resolveRestHostName(host, config);
}
- Map<String, Object> map = new HashMap<>();
- // build query string, and append any endpoint configuration properties
- if (config.getComponent() == null || config.getComponent().equals("netty-http")) {
- // setup endpoint options
- if (config.getEndpointProperties() != null && !config.getEndpointProperties().isEmpty()) {
- map.putAll(config.getEndpointProperties());
- }
- }
+ Map<String, Object> map = RestComponentHelper.initRestEndpointProperties("netty-http", config);
// allow HTTP Options as we want to handle CORS in rest-dsl
boolean cors = config.isEnableCORS();
@@ -397,22 +383,10 @@ public class NettyHttpComponent extends NettyComponent implements HeaderFilterSt
if (api) {
map.put("matchOnUriPrefix", "true");
}
-
- String query = URISupport.createQueryString(map);
-
- String url = "netty-http:%s://%s:%s/%s?httpMethodRestrict=%s";
- // must use upper case for restrict
- String restrict = verb.toUpperCase(Locale.US);
- if (cors) {
- restrict += ",OPTIONS";
- }
- // get the endpoint
- url = String.format(url, scheme, host, port, path, restrict);
+ RestComponentHelper.addHttpRestrictParam(map, verb, cors);
- if (!query.isEmpty()) {
- url = url + "&" + query;
- }
+ String url = RestComponentHelper.createRestConsumerUrl("netty-http", scheme, host, port, path, map);
NettyHttpEndpoint endpoint = camelContext.getEndpoint(url, NettyHttpEndpoint.class);
setProperties(camelContext, endpoint, parameters);
diff --git a/components/camel-platform-http/src/main/java/org/apache/camel/component/platform/http/PlatformHttpComponent.java b/components/camel-platform-http/src/main/java/org/apache/camel/component/platform/http/PlatformHttpComponent.java
index 86afc1f..d860d5a 100644
--- a/components/camel-platform-http/src/main/java/org/apache/camel/component/platform/http/PlatformHttpComponent.java
+++ b/components/camel-platform-http/src/main/java/org/apache/camel/component/platform/http/PlatformHttpComponent.java
@@ -16,8 +16,6 @@
*/
package org.apache.camel.component.platform.http;
-import java.util.HashMap;
-import java.util.Locale;
import java.util.Map;
import org.apache.camel.CamelContext;
@@ -31,8 +29,8 @@ import org.apache.camel.spi.RestConfiguration;
import org.apache.camel.spi.RestConsumerFactory;
import org.apache.camel.spi.annotations.Component;
import org.apache.camel.support.DefaultComponent;
+import org.apache.camel.support.RestComponentHelper;
import org.apache.camel.util.FileUtil;
-import org.apache.camel.util.URISupport;
/**
* Exposes HTTP endpoints leveraging the given platform's (SpringBoot, WildFly, Quarkus, ...) HTTP server.
@@ -108,43 +106,23 @@ public class PlatformHttpComponent extends DefaultComponent implements RestConsu
config = camelContext.getRestConfiguration(PlatformHttpConstants.PLATFORM_HTTP_COMPONENT_NAME, true);
}
- Map<String, Object> map = new HashMap<>();
- // build query string, and append any endpoint configuration properties
- if (config.getComponent() == null || config.getComponent().equals(PlatformHttpConstants.PLATFORM_HTTP_COMPONENT_NAME)) {
- // setup endpoint options
- if (config.getEndpointProperties() != null && !config.getEndpointProperties().isEmpty()) {
- map.putAll(config.getEndpointProperties());
- }
- }
+ Map<String, Object> map = RestComponentHelper.initRestEndpointProperties(PlatformHttpConstants.PLATFORM_HTTP_COMPONENT_NAME, config);
boolean cors = config.isEnableCORS();
if (cors) {
// allow HTTP Options as we want to handle CORS in rest-dsl
map.put("optionsEnabled", "true");
}
-
- // do not append with context-path as the servlet path should be without context-path
-
- String query = URISupport.createQueryString(map);
-
- String url;
+
if (api) {
- url = "platform-http:/%s?matchOnUriPrefix=true&httpMethodRestrict=%s";
- } else {
- url = "platform-http:/%s?httpMethodRestrict=%s";
+ map.put("matchOnUriPrefix", "true");
}
+
+ RestComponentHelper.addHttpRestrictParam(map, verb, cors);
- // must use upper case for restrict
- String restrict = verb.toUpperCase(Locale.US);
- if (cors) {
- restrict += ",OPTIONS";
- }
- // get the endpoint
- url = String.format(url, path, restrict);
+ // do not append with context-path as the servlet path should be without context-path
- if (!query.isEmpty()) {
- url = url + "&" + query;
- }
+ String url = RestComponentHelper.createRestConsumerUrl("platform-http", path, map);
PlatformHttpEndpoint endpoint = camelContext.getEndpoint(url, PlatformHttpEndpoint.class);
setProperties(camelContext, endpoint, parameters);
diff --git a/components/camel-servlet/pom.xml b/components/camel-servlet/pom.xml
index d875abd..5c284fa 100644
--- a/components/camel-servlet/pom.xml
+++ b/components/camel-servlet/pom.xml
@@ -145,6 +145,11 @@
<scope>test</scope>
</dependency>
<dependency>
+ <groupId>org.apache.camel</groupId>
+ <artifactId>camel-swagger-java</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>${rest-assured-version}</version>
diff --git a/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/ServletComponent.java b/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/ServletComponent.java
index b62aa8f..f75619b 100644
--- a/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/ServletComponent.java
+++ b/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/ServletComponent.java
@@ -17,8 +17,6 @@
package org.apache.camel.component.servlet;
import java.net.URI;
-import java.util.HashMap;
-import java.util.Locale;
import java.util.Map;
import org.apache.camel.CamelContext;
@@ -35,6 +33,7 @@ import org.apache.camel.spi.RestApiConsumerFactory;
import org.apache.camel.spi.RestConfiguration;
import org.apache.camel.spi.RestConsumerFactory;
import org.apache.camel.spi.annotations.Component;
+import org.apache.camel.support.RestComponentHelper;
import org.apache.camel.util.FileUtil;
import org.apache.camel.util.StringHelper;
import org.apache.camel.util.URISupport;
@@ -279,43 +278,21 @@ public class ServletComponent extends HttpCommonComponent implements RestConsume
config = camelContext.getRestConfiguration("servlet", true);
}
- Map<String, Object> map = new HashMap<>();
- // build query string, and append any endpoint configuration properties
- if (config.getComponent() == null || config.getComponent().equals("servlet")) {
- // setup endpoint options
- if (config.getEndpointProperties() != null && !config.getEndpointProperties().isEmpty()) {
- map.putAll(config.getEndpointProperties());
- }
- }
+ Map<String, Object> map = RestComponentHelper.initRestEndpointProperties("servlet", config);
boolean cors = config.isEnableCORS();
if (cors) {
// allow HTTP Options as we want to handle CORS in rest-dsl
map.put("optionsEnabled", "true");
}
-
- // do not append with context-path as the servlet path should be without context-path
-
- String query = URISupport.createQueryString(map);
-
- String url;
+
if (api) {
- url = "servlet:///%s?matchOnUriPrefix=true&httpMethodRestrict=%s";
- } else {
- url = "servlet:///%s?httpMethodRestrict=%s";
+ map.put("matchOnUriPrefix", "true");
}
-
- // must use upper case for restrict
- String restrict = verb.toUpperCase(Locale.US);
- if (cors) {
- restrict += ",OPTIONS";
- }
- // get the endpoint
- url = String.format(url, path, restrict);
- if (!query.isEmpty()) {
- url = url + "&" + query;
- }
+ RestComponentHelper.addHttpRestrictParam(map, verb, cors);
+
+ String url = RestComponentHelper.createRestConsumerUrl("servlet", path, map);
ServletEndpoint endpoint = camelContext.getEndpoint(url, ServletEndpoint.class);
setProperties(camelContext, endpoint, parameters);
diff --git a/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/rest/RestApiMatchUriServletTest.java b/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/rest/RestApiMatchUriServletTest.java
new file mode 100644
index 0000000..003506f
--- /dev/null
+++ b/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/rest/RestApiMatchUriServletTest.java
@@ -0,0 +1,62 @@
+/*
+ * 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.servlet.rest;
+
+import com.meterware.httpunit.GetMethodWebRequest;
+import com.meterware.httpunit.WebRequest;
+import com.meterware.httpunit.WebResponse;
+import com.meterware.servletunit.ServletUnitClient;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.servlet.ServletCamelRouterTestSupport;
+import org.apache.camel.model.rest.RestBindingMode;
+import org.junit.Test;
+
+public class RestApiMatchUriServletTest extends ServletCamelRouterTestSupport {
+
+ @Test
+ public void testApiDocGet() throws Exception {
+ WebRequest req = new GetMethodWebRequest(CONTEXT_URL + "/services/api-doc");
+ req.setHeaderField("Accept", "application/json");
+ ServletUnitClient client = newClient();
+ client.setExceptionsThrownOnErrorStatus(false);
+ WebResponse response = client.getResponse(req);
+
+ assertEquals(200, response.getResponseCode());
+
+ assertMockEndpointsSatisfied();
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ restConfiguration().component("servlet")
+ .apiContextPath("/api-doc")
+ .endpointProperty("matchOnUriPrefix", "true")
+ .apiProperty("cors", "true").apiProperty("api.title", "The hello rest thing").apiProperty("api.version", "1.2.3")
+ .bindingMode(RestBindingMode.auto);
+
+ // use the rest DSL to define the rest services
+ rest("/users/")
+ .post("new").consumes("application/json").type(UserPojo.class)
+ .to("mock:input");
+ }
+ };
+ }
+
+}
diff --git a/components/camel-spark-rest/src/main/java/org/apache/camel/component/sparkrest/SparkComponent.java b/components/camel-spark-rest/src/main/java/org/apache/camel/component/sparkrest/SparkComponent.java
index 49e983f..610196c 100644
--- a/components/camel-spark-rest/src/main/java/org/apache/camel/component/sparkrest/SparkComponent.java
+++ b/components/camel-spark-rest/src/main/java/org/apache/camel/component/sparkrest/SparkComponent.java
@@ -31,11 +31,11 @@ import org.apache.camel.spi.RestConfiguration;
import org.apache.camel.spi.RestConsumerFactory;
import org.apache.camel.spi.annotations.Component;
import org.apache.camel.support.DefaultComponent;
+import org.apache.camel.support.RestComponentHelper;
import org.apache.camel.util.FileUtil;
import org.apache.camel.util.HostUtils;
import org.apache.camel.util.ObjectHelper;
import org.apache.camel.util.StringHelper;
-import org.apache.camel.util.URISupport;
import spark.Service;
@Component("spark-rest")
@@ -336,20 +336,12 @@ public class SparkComponent extends DefaultComponent implements RestConsumerFact
path = contextPath + "/" + path;
}
}
-
- String url;
+
if (api) {
- url = "spark-rest:%s:%s?matchOnUriPrefix=true";
- } else {
- url = "spark-rest:%s:%s";
- }
-
- url = String.format(url, verb, path);
+ map.put("matchOnUriPrefix", "true");
+ }
- String query = URISupport.createQueryString(map);
- if (!query.isEmpty()) {
- url = url + "?" + query;
- }
+ String url = RestComponentHelper.createRestConsumerUrl("spark-rest", verb, path, map);
// get the endpoint
SparkEndpoint endpoint = camelContext.getEndpoint(url, SparkEndpoint.class);
diff --git a/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowComponent.java b/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowComponent.java
index 3e7a4cb..5291f58 100644
--- a/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowComponent.java
+++ b/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowComponent.java
@@ -42,11 +42,11 @@ import org.apache.camel.spi.RestConsumerFactory;
import org.apache.camel.spi.RestProducerFactory;
import org.apache.camel.spi.annotations.Component;
import org.apache.camel.support.DefaultComponent;
+import org.apache.camel.support.RestComponentHelper;
import org.apache.camel.support.RestProducerFactoryHelper;
import org.apache.camel.support.jsse.SSLContextParameters;
import org.apache.camel.support.service.ServiceHelper;
import org.apache.camel.util.FileUtil;
-import org.apache.camel.util.HostUtils;
import org.apache.camel.util.ObjectHelper;
import org.apache.camel.util.PropertiesHelper;
import org.apache.camel.util.URISupport;
@@ -191,35 +191,19 @@ public class UndertowComponent extends DefaultComponent implements RestConsumerF
// if no explicit hostname set then resolve the hostname
if (ObjectHelper.isEmpty(host)) {
- if (config.getHostNameResolver() == RestConfiguration.RestHostNameResolver.allLocalIp) {
- host = "0.0.0.0";
- } else if (config.getHostNameResolver() == RestConfiguration.RestHostNameResolver.localHostName) {
- host = HostUtils.getLocalHostName();
- } else if (config.getHostNameResolver() == RestConfiguration.RestHostNameResolver.localIp) {
- host = HostUtils.getLocalIp();
- }
+ host = RestComponentHelper.resolveRestHostName(host, config);
}
- Map<String, Object> map = new HashMap<>();
+ Map<String, Object> map = RestComponentHelper.initRestEndpointProperties(getComponentName(), config);
// build query string, and append any endpoint configuration properties
- if (config.getComponent() == null || config.getComponent().equals(getComponentName())) {
- // setup endpoint options
- if (config.getEndpointProperties() != null && !config.getEndpointProperties().isEmpty()) {
- map.putAll(config.getEndpointProperties());
- }
- }
- boolean explicitOptions = true;
+
// must use upper case for restrict
String restrict = verb.toUpperCase(Locale.US);
- // allow OPTIONS in rest-dsl to allow clients to call the API and have responses with ALLOW headers
- if (!restrict.contains("OPTIONS")) {
- restrict += ",OPTIONS";
- // this is not an explicit OPTIONS path in the rest-dsl
- explicitOptions = false;
- }
-
+
+ boolean explicitOptions = restrict.contains("OPTIONS");
boolean cors = config.isEnableCORS();
+
if (cors) {
// allow HTTP Options as we want to handle CORS in rest-dsl
map.put("optionsEnabled", "true");
@@ -227,22 +211,14 @@ public class UndertowComponent extends DefaultComponent implements RestConsumerF
// the rest-dsl is using OPTIONS
map.put("optionsEnabled", "true");
}
-
- String query = URISupport.createQueryString(map);
-
- String url;
+
if (api) {
- url = getComponentName() + ":%s://%s:%s/%s?matchOnUriPrefix=true&httpMethodRestrict=%s";
- } else {
- url = getComponentName() + ":%s://%s:%s/%s?matchOnUriPrefix=false&httpMethodRestrict=%s";
+ map.put("matchOnUriPrefix", "true");
}
+
+ RestComponentHelper.addHttpRestrictParam(map, verb, !explicitOptions);
- // get the endpoint
- url = String.format(url, scheme, host, port, path, restrict);
-
- if (!query.isEmpty()) {
- url = url + "&" + query;
- }
+ String url = RestComponentHelper.createRestConsumerUrl(getComponentName(), scheme, host, port, path, map);
UndertowEndpoint endpoint = camelContext.getEndpoint(url, UndertowEndpoint.class);
setProperties(camelContext, endpoint, parameters);
diff --git a/core/camel-support/src/main/java/org/apache/camel/support/RestComponentHelper.java b/core/camel-support/src/main/java/org/apache/camel/support/RestComponentHelper.java
new file mode 100644
index 0000000..8262b8c
--- /dev/null
+++ b/core/camel-support/src/main/java/org/apache/camel/support/RestComponentHelper.java
@@ -0,0 +1,154 @@
+/*
+ * 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.support;
+
+import java.net.URISyntaxException;
+import java.net.UnknownHostException;
+import java.util.HashMap;
+import java.util.Locale;
+import java.util.Map;
+
+import org.apache.camel.spi.RestConfiguration;
+import org.apache.camel.util.HostUtils;
+import org.apache.camel.util.URISupport;
+
+/**
+ * Helper class for rest-dsl components.
+ */
+public final class RestComponentHelper {
+
+ private RestComponentHelper() {
+ }
+
+ /**
+ *
+ * @param queryMap the map of Endpoint options to apply the HTTP restrict settings to
+ * @param verb the HTTP verb for the route
+ * @param addOptions should OPTIONS verb be added.
+ * @return the map of Endpoint Properties with HTTP Restrict Options set
+ */
+ public static Map<String, Object> addHttpRestrictParam(Map<String, Object> queryMap, String verb, boolean addOptions) {
+ String restrict = verb.toUpperCase(Locale.US);
+ if (addOptions) {
+ restrict += ",OPTIONS";
+ }
+ queryMap.put("httpMethodRestrict", restrict);
+ return queryMap;
+ }
+
+ /**
+ *
+ * Creates an Endpoint Property Map based on properies set in the component's RestConfiguration.
+ *
+ * @param componentName the Rest Component name
+ * @param config the RestConfiguration
+ * @return the map of Endpoint Properties set in the RestConfiguration
+ */
+ public static Map<String, Object> initRestEndpointProperties(String componentName, RestConfiguration config) {
+ Map<String, Object> map = new HashMap<>();
+ // build query string, and append any endpoint configuration properties
+ if (config.getComponent() == null || config.getComponent().equals(componentName)) {
+ // setup endpoint options
+ if (config.getEndpointProperties() != null && !config.getEndpointProperties().isEmpty()) {
+ map.putAll(config.getEndpointProperties());
+ }
+ }
+ return map;
+ }
+
+ /**
+ *
+ * Sets the Rest consumer host based on RestConfiguration
+ *
+ * @param host the existing host configuration
+ * @param config the RestConfiguration
+ * @return the host based on RestConfiguration
+ * @throws UnknownHostException thrown when local host or local ip can't be resolved via network interfaces.
+ */
+ public static String resolveRestHostName(String host, RestConfiguration config) throws UnknownHostException {
+ if (config.getHostNameResolver() == RestConfiguration.RestHostNameResolver.allLocalIp) {
+ host = "0.0.0.0";
+ } else if (config.getHostNameResolver() == RestConfiguration.RestHostNameResolver.localHostName) {
+ host = HostUtils.getLocalHostName();
+ } else if (config.getHostNameResolver() == RestConfiguration.RestHostNameResolver.localIp) {
+ host = HostUtils.getLocalIp();
+ }
+ return host;
+ }
+
+ /**
+ *
+ * Creates the Rest consumers url based on component and url options.
+ *
+ * @param componentName the name of the rest component
+ * @param verb the HTTP verb
+ * @param path the HTTP path of the route
+ * @param queryMap the endpoint query options
+ * @return a string of the component route url
+ * @throws URISyntaxException - is thrown if uri has invalid syntax.
+ */
+ public static String createRestConsumerUrl(String componentName, String verb, String path, Map<String, Object> queryMap) throws URISyntaxException {
+ String query = URISupport.createQueryString(queryMap);
+ return applyFormatAndQuery("%s:%s:%s", query, componentName, verb, path);
+ }
+
+ /**
+ *
+ * Creates the Rest consumers url based on component and url options.
+ *
+ * @param componentName the name of the rest component
+ * @param path the HTTP path of the route
+ * @param queryMap the endpoint query options
+ * @return a string of the component route url
+ * @throws URISyntaxException - is thrown if uri has invalid syntax.
+ */
+ public static String createRestConsumerUrl(String componentName, String path, Map<String, Object> queryMap) throws URISyntaxException {
+ String query = URISupport.createQueryString(queryMap);
+ return applyFormatAndQuery("%s:/%s", query, componentName, path);
+ }
+
+ /**
+ *
+ * Creates the Rest consumers url based on component and url options.
+ *
+ * @param componentName the name of the rest component
+ * @param scheme the scheme of the HTTP route http/https
+ * @param host the host of the HTTP route
+ * @param port the port the route will be exposed through
+ * @param path the HTTP path of the route
+ * @param queryMap the endpoint query options
+ * @return a string of the component route url
+ * @throws URISyntaxException - is thrown if uri has invalid syntax.
+ */
+ public static String createRestConsumerUrl(String componentName, String scheme, String host, int port, String path, Map<String, Object> queryMap) throws URISyntaxException {
+
+ String query = URISupport.createQueryString(queryMap);
+
+ return applyFormatAndQuery("%s:%s://%s:%s/%s", query, componentName, scheme, host, port, path);
+ }
+
+ private static String applyFormatAndQuery(String format, String query, Object... formatOptions) {
+ // get the endpoint
+ StringBuilder urlBuilder = new StringBuilder(String.format(format, formatOptions));
+
+ if (!query.isEmpty()) {
+ urlBuilder.append("?");
+ urlBuilder.append(query);
+ }
+ return urlBuilder.toString();
+ }
+}