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 2014/12/24 13:50:08 UTC

camel git commit: CAMEL-7878: camel-swagger now supports relative paths for base/api paths which gets calculated at runtime to the actual absolute url.

Repository: camel
Updated Branches:
  refs/heads/master 6f41a86d6 -> 6fdee41be


CAMEL-7878: camel-swagger now supports relative paths for base/api paths which gets calculated at runtime to the actual absolute url.


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/6fdee41b
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/6fdee41b
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/6fdee41b

Branch: refs/heads/master
Commit: 6fdee41be09ce276b7d50ebfceb223c9ef870c87
Parents: 6f41a86
Author: Claus Ibsen <da...@apache.org>
Authored: Wed Dec 24 13:49:56 2014 +0100
Committer: Claus Ibsen <da...@apache.org>
Committed: Wed Dec 24 13:49:56 2014 +0100

----------------------------------------------------------------------
 .../RestSwaggerApiDeclarationServlet.scala      | 56 ++++++++++++++++++++
 .../src/main/webapp/WEB-INF/web.xml             |  8 ++-
 2 files changed, 62 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/6fdee41b/components/camel-swagger/src/main/scala/org/apache/camel/component/swagger/RestSwaggerApiDeclarationServlet.scala
----------------------------------------------------------------------
diff --git a/components/camel-swagger/src/main/scala/org/apache/camel/component/swagger/RestSwaggerApiDeclarationServlet.scala b/components/camel-swagger/src/main/scala/org/apache/camel/component/swagger/RestSwaggerApiDeclarationServlet.scala
index 8a058db..9057863 100644
--- a/components/camel-swagger/src/main/scala/org/apache/camel/component/swagger/RestSwaggerApiDeclarationServlet.scala
+++ b/components/camel-swagger/src/main/scala/org/apache/camel/component/swagger/RestSwaggerApiDeclarationServlet.scala
@@ -16,6 +16,7 @@
  */
 package org.apache.camel.component.swagger
 
+import java.net.URL
 import javax.servlet.http.{HttpServlet, HttpServletResponse, HttpServletRequest}
 import javax.servlet.ServletConfig
 
@@ -40,6 +41,7 @@ abstract class RestSwaggerApiDeclarationServlet extends HttpServlet {
   var camelId: String = null
   val swaggerConfig: SwaggerConfig = ConfigFactory.config
   var cors: Boolean = false
+  var initDone: Boolean = false
 
   override def init(config: ServletConfig): Unit = {
     super.init(config)
@@ -84,6 +86,10 @@ abstract class RestSwaggerApiDeclarationServlet extends HttpServlet {
   def getRestDefinitions(camelId: String) : mutable.Buffer[RestDefinition]
 
   override protected def doGet(request: HttpServletRequest, response: HttpServletResponse) = {
+    if (!initDone) {
+      initBaseAndApiPaths(request)
+    }
+
     val route = request.getPathInfo
     // render overview if the route is empty or is the root path
     if (route != null && route != "" && route != "/") {
@@ -93,6 +99,56 @@ abstract class RestSwaggerApiDeclarationServlet extends HttpServlet {
     }
   }
 
+  def initBaseAndApiPaths(request: HttpServletRequest) = {
+    var base = swaggerConfig.getBasePath
+    if (base == null || !base.startsWith("http")) {
+      // base path is configured using relative, so lets calculate the absolute url now we have the http request
+      val url = new URL(request.getRequestURL.toString)
+      if (base == null) {
+        base = ""
+      }
+      val path = translateContextPath(request)
+      if (url.getPort != 80) {
+        base = url.getProtocol + "://" + url.getHost + ":" + url.getPort + path + "/" + base
+      } else {
+        base = url.getProtocol + "://" + url.getHost + request.getContextPath + "/" + base
+      }
+      swaggerConfig.setBasePath(base)
+    }
+    base = swaggerConfig.getApiPath
+    if (base == null || !base.startsWith("http")) {
+      // api path is configured using relative, so lets calculate the absolute url now we have the http request
+      val url = new URL(request.getRequestURL.toString)
+      if (base == null) {
+        base = ""
+      }
+      val path = translateContextPath(request)
+      if (url.getPort != 80) {
+        base = url.getProtocol + "://" + url.getHost + ":" + url.getPort + path + "/" + base
+      } else {
+        base = url.getProtocol + "://" + url.getHost + request.getContextPath + "/" + base
+      }
+      swaggerConfig.setApiPath(base)
+    }
+    initDone = true
+  }
+
+  /**
+   * We do only want the base context-path and not sub paths
+   */
+  def translateContextPath(request: HttpServletRequest): String = {
+    var path = request.getContextPath
+    if (path.isEmpty || path.equals("/")) {
+      return ""
+    } else {
+      val idx = path.lastIndexOf("/")
+      if (idx > 0) {
+        return path.substring(0, idx)
+      }
+    }
+    path
+  }
+
   /**
    * Renders the resource listing which is the overview of all the apis
    */

http://git-wip-us.apache.org/repos/asf/camel/blob/6fdee41b/examples/camel-example-servlet-rest-tomcat/src/main/webapp/WEB-INF/web.xml
----------------------------------------------------------------------
diff --git a/examples/camel-example-servlet-rest-tomcat/src/main/webapp/WEB-INF/web.xml b/examples/camel-example-servlet-rest-tomcat/src/main/webapp/WEB-INF/web.xml
index 251e5f1..3cd24b4 100755
--- a/examples/camel-example-servlet-rest-tomcat/src/main/webapp/WEB-INF/web.xml
+++ b/examples/camel-example-servlet-rest-tomcat/src/main/webapp/WEB-INF/web.xml
@@ -53,12 +53,16 @@
     <servlet-name>ApiDeclarationServlet</servlet-name>
     <servlet-class>org.apache.camel.component.swagger.DefaultCamelSwaggerServlet</servlet-class>
     <init-param>
+      <!-- we specify the base.path using relative notation, that means the actual path will be calculated at runtime as
+           http://server:port/contextpath/rest -->
       <param-name>base.path</param-name>
-      <param-value>http://localhost:8080/camel-example-servlet-rest-tomcat/rest</param-value>
+      <param-value>rest</param-value>
     </init-param>
     <init-param>
+      <!-- we specify the api.path using relative notation, that means the actual path will be calculated at runtime as
+           http://server:port/contextpath/api-docs -->
       <param-name>api.path</param-name>
-      <param-value>http://localhost:8080/camel-example-servlet-rest-tomcat/api-docs</param-value>
+      <param-value>api-docs</param-value>
     </init-param>
     <init-param>
       <param-name>api.version</param-name>