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 2015/09/24 08:40:37 UTC

[3/3] camel git commit: CAMEL-8545: camel-swagger-java to run outside servlet - work in progress

CAMEL-8545: camel-swagger-java to run outside servlet - work in progress


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

Branch: refs/heads/master
Commit: 55627269cebce448d5703c6ab300d230bc070184
Parents: 356bf4a
Author: Claus Ibsen <da...@apache.org>
Authored: Thu Sep 24 08:42:02 2015 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Thu Sep 24 08:42:02 2015 +0200

----------------------------------------------------------------------
 .../swagger/servlet/RestSwaggerServlet.java     | 44 ++++++++++++--------
 .../src/main/webapp/WEB-INF/web.xml             | 10 ++++-
 2 files changed, 35 insertions(+), 19 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/55627269/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 35f53c8..48b7ee5 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
@@ -54,11 +54,11 @@ public class RestSwaggerServlet extends HttpServlet {
     private final ClassResolver classResolver = new DefaultClassResolver();
     private volatile boolean initDone;
 
-    private String contextIdPattern;
-    private boolean contextIdListing;
+    private String apiContextIdPattern;
+    private boolean apiContextIdListing;
 
-    public String getContextIdPattern() {
-        return contextIdPattern;
+    public String getApiContextIdPattern() {
+        return apiContextIdPattern;
     }
 
     /**
@@ -66,22 +66,22 @@ public class RestSwaggerServlet extends HttpServlet {
      * <p/>
      * The pattern uses the rules from {@link org.apache.camel.util.EndpointHelper#matchPattern(String, String)}
      *
-     * @param contextIdPattern  the pattern
+     * @param apiContextIdPattern  the pattern
      */
-    public void setContextIdPattern(String contextIdPattern) {
-        this.contextIdPattern = contextIdPattern;
+    public void setApiContextIdPattern(String apiContextIdPattern) {
+        this.apiContextIdPattern = apiContextIdPattern;
     }
 
-    public boolean isContextIdListing() {
-        return contextIdListing;
+    public boolean isApiContextIdListing() {
+        return apiContextIdListing;
     }
 
     /**
      * Sets whether listing of all available CamelContext's with REST services in the JVM is enabled. If enabled it allows to discover
      * these contexts, if <tt>false</tt> then only if there is exactly one CamelContext then its used.
      */
-    public void setContextIdListing(boolean contextIdListing) {
-        this.contextIdListing = contextIdListing;
+    public void setApiContextIdListing(boolean apiContextIdListing) {
+        this.apiContextIdListing = apiContextIdListing;
     }
 
     @Override
@@ -95,6 +95,16 @@ public class RestSwaggerServlet extends HttpServlet {
             parameters.put(name, value);
         }
         support.initSwagger(swaggerConfig, parameters);
+
+        // allow to configure these options from the servlet config as well
+        Object pattern = parameters.remove("apiContextIdPattern");
+        if (pattern != null) {
+            apiContextIdPattern = pattern.toString();
+        }
+        Object listing = parameters.remove("apiContextIdListing");
+        if (listing != null) {
+            apiContextIdListing = Boolean.valueOf(listing.toString());
+        }
     }
 
     @Override
@@ -111,8 +121,8 @@ public class RestSwaggerServlet extends HttpServlet {
 
         try {
             // render list of camel contexts as root
-            if (contextIdListing && (ObjectHelper.isEmpty(route) || route.equals("/"))) {
-                support.renderCamelContexts(adapter, contextId, contextIdPattern);
+            if (apiContextIdListing && (ObjectHelper.isEmpty(route) || route.equals("/"))) {
+                support.renderCamelContexts(adapter, contextId, apiContextIdPattern);
             } else {
                 String name = null;
                 if (ObjectHelper.isNotEmpty(route)) {
@@ -136,15 +146,15 @@ public class RestSwaggerServlet extends HttpServlet {
                 boolean match = false;
                 if (name != null) {
                     match = true;
-                    if (contextIdPattern != null) {
-                        if ("#name#".equals(contextIdPattern)) {
+                    if (apiContextIdPattern != null) {
+                        if ("#name#".equals(apiContextIdPattern)) {
                             // always match as we do not know what is the current CamelContext in a plain servlet
                             match = true;
                         } else {
-                            match = EndpointHelper.matchPattern(name, contextIdPattern);
+                            match = EndpointHelper.matchPattern(name, apiContextIdPattern);
                         }
                         if (LOG.isDebugEnabled()) {
-                            LOG.debug("Match contextId: {} with pattern: {} -> {}", new Object[]{name, contextIdPattern, match});
+                            LOG.debug("Match contextId: {} with pattern: {} -> {}", new Object[]{name, apiContextIdPattern, match});
                         }
                     }
                 }

http://git-wip-us.apache.org/repos/asf/camel/blob/55627269/examples/camel-example-swagger-java/src/main/webapp/WEB-INF/web.xml
----------------------------------------------------------------------
diff --git a/examples/camel-example-swagger-java/src/main/webapp/WEB-INF/web.xml b/examples/camel-example-swagger-java/src/main/webapp/WEB-INF/web.xml
index cfde29f..ad45c0d 100755
--- a/examples/camel-example-swagger-java/src/main/webapp/WEB-INF/web.xml
+++ b/examples/camel-example-swagger-java/src/main/webapp/WEB-INF/web.xml
@@ -43,9 +43,15 @@
 
   <!-- to setup Camel Swagger servlet -->
   <servlet>
-    <servlet-name>ApiDeclarationServlet</servlet-name>
+    <servlet-name>SwaggerServlet</servlet-name>
     <servlet-class>org.apache.camel.swagger.servlet.RestSwaggerServlet</servlet-class>
     <init-param>
+      <!-- enable context id listing so we can list all the CamelContexts in the JVM that has REST services
+           which we then will be able to show -->
+      <param-name>apiContextIdListing</param-name>
+      <param-value>true</param-value>
+    </init-param>
+    <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>
@@ -78,7 +84,7 @@
 
   <!-- swagger api declaration -->
   <servlet-mapping>
-    <servlet-name>ApiDeclarationServlet</servlet-name>
+    <servlet-name>SwaggerServlet</servlet-name>
     <url-pattern>/api-docs/*</url-pattern>
   </servlet-mapping>