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 2017/07/29 12:07:10 UTC
camel git commit: CAMEL-11614: rest-dsl - Allow to configure api
hostname
Repository: camel
Updated Branches:
refs/heads/master 05a0d5168 -> 57a3a3d01
CAMEL-11614: rest-dsl - Allow to configure api hostname
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/57a3a3d0
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/57a3a3d0
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/57a3a3d0
Branch: refs/heads/master
Commit: 57a3a3d019e5de55e8e0fc595211516a22dced61
Parents: 05a0d51
Author: Claus Ibsen <da...@apache.org>
Authored: Sat Jul 29 13:56:01 2017 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Sat Jul 29 13:56:01 2017 +0200
----------------------------------------------------------------------
.../camel/component/rest/RestApiEndpoint.java | 4 +-
.../model/rest/RestConfigurationDefinition.java | 28 ++++++++
.../org/apache/camel/spi/RestConfiguration.java | 14 ++++
.../component/jetty/rest/RestApiJettyTest.java | 2 +
.../rest/RestApiOverrideHostJettyTest.java | 68 ++++++++++++++++++++
.../swagger/SwaggerRestApiProcessorFactory.java | 22 +++++--
.../swagger/servlet/RestSwaggerServlet.java | 11 ++--
...estSwaggerReaderOverrideHostApiDocsTest.java | 59 +++++++++++++++++
.../RestConfigurationDefinitionProperties.java | 13 ++++
9 files changed, 209 insertions(+), 12 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/57a3a3d0/camel-core/src/main/java/org/apache/camel/component/rest/RestApiEndpoint.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/component/rest/RestApiEndpoint.java b/camel-core/src/main/java/org/apache/camel/component/rest/RestApiEndpoint.java
index eb948b0..1fe7299 100644
--- a/camel-core/src/main/java/org/apache/camel/component/rest/RestApiEndpoint.java
+++ b/camel-core/src/main/java/org/apache/camel/component/rest/RestApiEndpoint.java
@@ -163,7 +163,9 @@ public class RestApiEndpoint extends DefaultEndpoint {
String host = "";
int port = 80;
- if (config.getHost() != null) {
+ if (config.getApiHost() != null) {
+ host = config.getApiHost();
+ } else if (config.getHost() != null) {
host = config.getHost();
}
int num = config.getPort();
http://git-wip-us.apache.org/repos/asf/camel/blob/57a3a3d0/camel-core/src/main/java/org/apache/camel/model/rest/RestConfigurationDefinition.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/model/rest/RestConfigurationDefinition.java b/camel-core/src/main/java/org/apache/camel/model/rest/RestConfigurationDefinition.java
index 6dfd527..8acb077 100644
--- a/camel-core/src/main/java/org/apache/camel/model/rest/RestConfigurationDefinition.java
+++ b/camel-core/src/main/java/org/apache/camel/model/rest/RestConfigurationDefinition.java
@@ -55,6 +55,9 @@ public class RestConfigurationDefinition {
private String host;
@XmlAttribute
+ private String apiHost;
+
+ @XmlAttribute
private String port;
@XmlAttribute @Metadata(label = "producer")
@@ -171,6 +174,19 @@ public class RestConfigurationDefinition {
this.host = host;
}
+ public String getApiHost() {
+ return apiHost;
+ }
+
+ /**
+ * To use an specific hostname for the API documentation (eg swagger)
+ * <p/>
+ * This can be used to override the generated host with this configured hostname
+ */
+ public void setApiHost(String apiHost) {
+ this.apiHost = apiHost;
+ }
+
public String getPort() {
return port;
}
@@ -476,6 +492,15 @@ public class RestConfigurationDefinition {
}
/**
+ * To define a specific host to use for API documentation (eg swagger) instead
+ * of using a generated API hostname that is relative to the REST service host.
+ */
+ public RestConfigurationDefinition apiHost(String host) {
+ setApiHost(host);
+ return this;
+ }
+
+ /**
* To specify the port number to use for the REST service
*/
public RestConfigurationDefinition port(int port) {
@@ -720,6 +745,9 @@ public class RestConfigurationDefinition {
if (host != null) {
answer.setHost(CamelContextHelper.parseText(context, host));
}
+ if (apiHost != null) {
+ answer.setApiHost(CamelContextHelper.parseText(context, apiHost));
+ }
if (port != null) {
answer.setPort(CamelContextHelper.parseInteger(context, port));
}
http://git-wip-us.apache.org/repos/asf/camel/blob/57a3a3d0/camel-core/src/main/java/org/apache/camel/spi/RestConfiguration.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/spi/RestConfiguration.java b/camel-core/src/main/java/org/apache/camel/spi/RestConfiguration.java
index c90ba8d..ce9854a 100644
--- a/camel-core/src/main/java/org/apache/camel/spi/RestConfiguration.java
+++ b/camel-core/src/main/java/org/apache/camel/spi/RestConfiguration.java
@@ -43,6 +43,7 @@ public class RestConfiguration {
private String producerApiDoc;
private String scheme;
private String host;
+ private String apiHost;
private int port;
private String contextPath;
private String apiContextPath;
@@ -155,6 +156,19 @@ public class RestConfiguration {
this.host = host;
}
+ public String getApiHost() {
+ return apiHost;
+ }
+
+ /**
+ * To use an specific hostname for the API documentation (eg swagger)
+ * <p/>
+ * This can be used to override the generated host with this configured hostname
+ */
+ public void setApiHost(String apiHost) {
+ this.apiHost = apiHost;
+ }
+
/**
* Gets the scheme to use by the REST consumer
*
http://git-wip-us.apache.org/repos/asf/camel/blob/57a3a3d0/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/RestApiJettyTest.java
----------------------------------------------------------------------
diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/RestApiJettyTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/RestApiJettyTest.java
index a7c7a1a..2f5de93 100644
--- a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/RestApiJettyTest.java
+++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/RestApiJettyTest.java
@@ -32,8 +32,10 @@ public class RestApiJettyTest extends BaseJettyTest {
public void testApi() throws Exception {
String out = template.requestBody("jetty:http://localhost:{{port}}/api-doc", null, String.class);
assertNotNull(out);
+
assertTrue(out.contains("\"version\" : \"1.2.3\""));
assertTrue(out.contains("\"title\" : \"The hello rest thing\""));
+ assertTrue(out.contains("\"host\" : \"localhost:" + getPort() + "\""));
assertTrue(out.contains("\"/hello/bye/{name}\""));
assertTrue(out.contains("\"/hello/hi/{name}\""));
assertTrue(out.contains("\"summary\" : \"To update the greeting message\""));
http://git-wip-us.apache.org/repos/asf/camel/blob/57a3a3d0/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/RestApiOverrideHostJettyTest.java
----------------------------------------------------------------------
diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/RestApiOverrideHostJettyTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/RestApiOverrideHostJettyTest.java
new file mode 100644
index 0000000..7fae3d0
--- /dev/null
+++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/RestApiOverrideHostJettyTest.java
@@ -0,0 +1,68 @@
+/**
+ * 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.jetty.rest;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.jetty.BaseJettyTest;
+import org.apache.camel.model.rest.RestParamType;
+import org.junit.Test;
+
+public class RestApiOverrideHostJettyTest extends BaseJettyTest {
+
+ @Override
+ protected boolean useJmx() {
+ return true;
+ }
+
+ @Test
+ public void testApi() throws Exception {
+ String out = template.requestBody("jetty:http://localhost:{{port}}/api-doc", null, String.class);
+ assertNotNull(out);
+ System.out.println(out);
+
+ assertTrue(out.contains("\"version\" : \"1.2.3\""));
+ assertTrue(out.contains("\"title\" : \"The hello rest thing\""));
+ assertTrue(out.contains("\"host\" : \"mycoolserver/myapi\""));
+ assertTrue(out.contains("\"/hello/bye/{name}\""));
+ assertTrue(out.contains("\"/hello/hi/{name}\""));
+ assertTrue(out.contains("\"summary\" : \"To update the greeting message\""));
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ restConfiguration().component("jetty").host("localhost").port(getPort()).apiContextPath("/api-doc").apiHost("mycoolserver/myapi")
+ .apiProperty("cors", "true").apiProperty("api.title", "The hello rest thing").apiProperty("api.version", "1.2.3");
+
+ rest("/hello").consumes("application/json").produces("application/json")
+ .get("/hi/{name}").description("Saying hi")
+ .param().name("name").type(RestParamType.path).dataType("string").description("Who is it").endParam()
+ .to("log:hi")
+ .get("/bye/{name}").description("Saying bye")
+ .param().name("name").type(RestParamType.path).dataType("string").description("Who is it").endParam()
+ .responseMessage().code(200).message("A reply message").endResponseMessage()
+ .to("log:bye")
+ .post("/bye").description("To update the greeting message").consumes("application/xml").produces("application/xml")
+ .param().name("greeting").type(RestParamType.body).dataType("string").description("Message to use as greeting").endParam()
+ .to("log:bye");
+ }
+ };
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/57a3a3d0/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/SwaggerRestApiProcessorFactory.java
----------------------------------------------------------------------
diff --git a/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/SwaggerRestApiProcessorFactory.java b/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/SwaggerRestApiProcessorFactory.java
index c815001..5da5c6b 100644
--- a/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/SwaggerRestApiProcessorFactory.java
+++ b/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/SwaggerRestApiProcessorFactory.java
@@ -37,15 +37,23 @@ public class SwaggerRestApiProcessorFactory implements RestApiProcessorFactory {
// need to include host in options
String host = (String) options.get("host");
- if (host == null) {
- host = configuration.getHost();
- int port = configuration.getPort();
- if (host != null && port > 0) {
- options.put("host", host + ":" + port);
- } else if (host != null) {
+ if (host != null) {
+ options.put("host", host);
+ } else {
+ // favor using explicit configured host for the api
+ host = configuration.getApiHost();
+ if (host != null) {
options.put("host", host);
} else {
- options.put("host", "localhost");
+ host = configuration.getHost();
+ int port = configuration.getPort();
+ if (host != null && port > 0) {
+ options.put("host", host + ":" + port);
+ } else if (host != null) {
+ options.put("host", host);
+ } else {
+ options.put("host", "localhost");
+ }
}
}
// and context path is the base.path
http://git-wip-us.apache.org/repos/asf/camel/blob/57a3a3d0/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/servlet/RestSwaggerServlet.java
----------------------------------------------------------------------
diff --git a/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/servlet/RestSwaggerServlet.java b/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/servlet/RestSwaggerServlet.java
index 6d47826..bc441a8 100644
--- a/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/servlet/RestSwaggerServlet.java
+++ b/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/servlet/RestSwaggerServlet.java
@@ -206,12 +206,15 @@ public class RestSwaggerServlet extends HttpServlet {
base = "";
}
String path = translateContextPath(request);
- swaggerConfig.setHost(url.getHost());
- if (url.getPort() != 80 && url.getPort() != -1) {
- swaggerConfig.setHost(url.getHost() + ":" + url.getPort());
- } else {
+ // setup host if not configured
+ if (swaggerConfig.getHost() == null) {
swaggerConfig.setHost(url.getHost());
+ if (url.getPort() != 80 && url.getPort() != -1) {
+ swaggerConfig.setHost(url.getHost() + ":" + url.getPort());
+ } else {
+ swaggerConfig.setHost(url.getHost());
+ }
}
swaggerConfig.setBasePath(buildUrl(path, base));
}
http://git-wip-us.apache.org/repos/asf/camel/blob/57a3a3d0/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/RestSwaggerReaderOverrideHostApiDocsTest.java
----------------------------------------------------------------------
diff --git a/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/RestSwaggerReaderOverrideHostApiDocsTest.java b/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/RestSwaggerReaderOverrideHostApiDocsTest.java
new file mode 100644
index 0000000..5333483
--- /dev/null
+++ b/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/RestSwaggerReaderOverrideHostApiDocsTest.java
@@ -0,0 +1,59 @@
+/**
+ * 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.swagger;
+
+import com.fasterxml.jackson.annotation.JsonInclude;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.SerializationFeature;
+import io.swagger.jaxrs.config.BeanConfig;
+import io.swagger.models.Swagger;
+import org.apache.camel.impl.DefaultClassResolver;
+import org.junit.Test;
+
+public class RestSwaggerReaderOverrideHostApiDocsTest extends RestSwaggerReaderApiDocsTest {
+
+ @Test
+ public void testReaderRead() throws Exception {
+ BeanConfig config = new BeanConfig();
+ config.setHost("localhost:8080");
+ config.setSchemes(new String[]{"http"});
+ config.setBasePath("/api");
+ config.setHost("http:mycoolserver:8888/myapi");
+ RestSwaggerReader reader = new RestSwaggerReader();
+
+ Swagger swagger = reader.read(context.getRestDefinitions(), null, config, context.getName(), new DefaultClassResolver());
+ assertNotNull(swagger);
+
+ ObjectMapper mapper = new ObjectMapper();
+ mapper.enable(SerializationFeature.INDENT_OUTPUT);
+ mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
+ String json = mapper.writeValueAsString(swagger);
+
+ log.info(json);
+
+ assertTrue(json.contains("\"host\" : \"http:mycoolserver:8888/myapi\""));
+ assertTrue(json.contains("\"basePath\" : \"/api\""));
+
+ assertFalse(json.contains("\"/hello/bye\""));
+ assertFalse(json.contains("\"summary\" : \"To update the greeting message\""));
+ assertFalse(json.contains("\"/hello/bye/{name}\""));
+ assertTrue(json.contains("\"/hello/hi/{name}\""));
+
+ context.stop();
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/57a3a3d0/platforms/spring-boot/components-starter/camel-core-starter/src/main/java/org/apache/camel/model/rest/springboot/RestConfigurationDefinitionProperties.java
----------------------------------------------------------------------
diff --git a/platforms/spring-boot/components-starter/camel-core-starter/src/main/java/org/apache/camel/model/rest/springboot/RestConfigurationDefinitionProperties.java b/platforms/spring-boot/components-starter/camel-core-starter/src/main/java/org/apache/camel/model/rest/springboot/RestConfigurationDefinitionProperties.java
index 9cf3ebb..f490daa 100644
--- a/platforms/spring-boot/components-starter/camel-core-starter/src/main/java/org/apache/camel/model/rest/springboot/RestConfigurationDefinitionProperties.java
+++ b/platforms/spring-boot/components-starter/camel-core-starter/src/main/java/org/apache/camel/model/rest/springboot/RestConfigurationDefinitionProperties.java
@@ -57,6 +57,11 @@ public class RestConfigurationDefinitionProperties {
*/
private String host;
/**
+ * To use an specific hostname for the API documentation (eg swagger) This
+ * can be used to override the generated host with this configured hostname
+ */
+ private String apiHost;
+ /**
* The port number to use for exposing the REST service. Notice if you use
* servlet component then the port number configured here does not apply as
* the port number in use is the actual port number the servlet component is
@@ -217,6 +222,14 @@ public class RestConfigurationDefinitionProperties {
this.host = host;
}
+ public String getApiHost() {
+ return apiHost;
+ }
+
+ public void setApiHost(String apiHost) {
+ this.apiHost = apiHost;
+ }
+
public String getPort() {
return port;
}