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 2016/02/09 12:21:33 UTC

[2/2] camel git commit: CAMEL-9582: swagger-api docs not working in only using xml without any java route

CAMEL-9582: swagger-api docs not working in only using xml without any java route


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

Branch: refs/heads/camel-2.16.x
Commit: f4027cd8e60d9b25768e43bb7d4eb8c6861f3063
Parents: 365f670
Author: Claus Ibsen <da...@apache.org>
Authored: Tue Feb 9 12:20:47 2016 +0100
Committer: Claus Ibsen <da...@apache.org>
Committed: Tue Feb 9 12:21:17 2016 +0100

----------------------------------------------------------------------
 .../xml/AbstractCamelContextFactoryBean.java    | 23 +++++++-
 .../src/main/resources/camel-config-xml.xml     | 17 +++++-
 .../src/main/webapp/WEB-INF/web.xml             | 59 --------------------
 .../src/main/webapp/home.html                   |  8 +--
 4 files changed, 42 insertions(+), 65 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/f4027cd8/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelContextFactoryBean.java
----------------------------------------------------------------------
diff --git a/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelContextFactoryBean.java b/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelContextFactoryBean.java
index 0aef50b..eb95409 100644
--- a/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelContextFactoryBean.java
+++ b/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelContextFactoryBean.java
@@ -48,6 +48,7 @@ import org.apache.camel.management.DefaultManagementLifecycleStrategy;
 import org.apache.camel.management.DefaultManagementStrategy;
 import org.apache.camel.management.ManagedManagementStrategy;
 import org.apache.camel.model.ContextScanDefinition;
+import org.apache.camel.model.FromDefinition;
 import org.apache.camel.model.IdentifiedType;
 import org.apache.camel.model.InterceptDefinition;
 import org.apache.camel.model.InterceptFromDefinition;
@@ -90,6 +91,7 @@ import org.apache.camel.spi.NodeIdFactory;
 import org.apache.camel.spi.PackageScanClassResolver;
 import org.apache.camel.spi.PackageScanFilter;
 import org.apache.camel.spi.ProcessorFactory;
+import org.apache.camel.spi.RestConfiguration;
 import org.apache.camel.spi.RoutePolicyFactory;
 import org.apache.camel.spi.RuntimeEndpointRegistry;
 import org.apache.camel.spi.ShutdownStrategy;
@@ -348,7 +350,26 @@ public abstract class AbstractCamelContextFactoryBean<T extends ModelCamelContex
                     getRoutes().add(route);
                 }
             }
-
+            // convert rests api-doc into routes so they are routes for runtime
+            for (RestConfiguration config : getContext().getRestConfigurations()) {
+                if (config.getApiContextPath() != null) {
+                    // avoid adding rest-api multiple times, in case multiple RouteBuilder classes is added
+                    // to the CamelContext, as we only want to setup rest-api once
+                    // so we check all existing routes if they have rest-api route already added
+                    boolean hasRestApi = false;
+                    for (RouteDefinition route : getContext().getRouteDefinitions()) {
+                        FromDefinition from = route.getInputs().get(0);
+                        if (from.getUri() != null && from.getUri().startsWith("rest-api:")) {
+                            hasRestApi = true;
+                        }
+                    }
+                    if (!hasRestApi) {
+                        RouteDefinition route = RestDefinition.asRouteApiDefinition(getContext(), config);
+                        LOG.debug("Adding routeId: {} as rest-api route", route.getId());
+                        getRoutes().add(route);
+                    }
+                }
+            }
 
             // do special preparation for some concepts such as interceptors and policies
             // this is needed as JAXB does not build exactly the same model definition as Spring DSL would do

http://git-wip-us.apache.org/repos/asf/camel/blob/f4027cd8/examples/camel-example-swagger-java/src/main/resources/camel-config-xml.xml
----------------------------------------------------------------------
diff --git a/examples/camel-example-swagger-java/src/main/resources/camel-config-xml.xml b/examples/camel-example-swagger-java/src/main/resources/camel-config-xml.xml
index 56149e5..c4c7bd2 100755
--- a/examples/camel-example-swagger-java/src/main/resources/camel-config-xml.xml
+++ b/examples/camel-example-swagger-java/src/main/resources/camel-config-xml.xml
@@ -35,8 +35,23 @@
          knows the url to the REST services.
          Notice: This is optional, but needed if the RestRegistry should enlist accurate information.
          You can access the RestRegistry from JMX at runtime -->
-    <restConfiguration component="servlet" bindingMode="json" contextPath="camel-example-swagger-java/rest" port="8080">
+    <!-- also enable swagger api, using the apiContextPath
+         and enable CORS so the swagger-ui web console can access the swagger api docs -->
+    <restConfiguration component="servlet" bindingMode="json"
+                       contextPath="camel-example-swagger-java/rest" port="8080"
+                       apiContextPath="api-docs" apiContextListing="true"
+                       enableCORS="true">
+
+      <!-- we want json output in pretty mode -->
       <dataFormatProperty key="prettyPrint" value="true"/>
+
+      <!-- setup swagger api descriptions -->
+      <apiProperty key="base.path" value="rest"/>
+      <apiProperty key="api.version" value="1.2.3"/>
+      <apiProperty key="api.title" value="User Services"/>
+      <apiProperty key="api.description" value="Camel Rest Example with Swagger that provides an User REST service"/>
+      <apiProperty key="api.contact.name" value="The Apache Camel team"/>
+
     </restConfiguration>
 
     <!-- defines the rest services using the context-path /user -->

http://git-wip-us.apache.org/repos/asf/camel/blob/f4027cd8/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 ad45c0d..8ec650c 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
@@ -41,71 +41,12 @@
     <load-on-startup>1</load-on-startup>
   </servlet>
 
-  <!-- to setup Camel Swagger servlet -->
-  <servlet>
-    <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>
-      <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>api-docs</param-value>
-    </init-param>
-    <init-param>
-      <param-name>api.version</param-name>
-      <param-value>1.2.3</param-value>
-    </init-param>
-    <init-param>
-      <param-name>api.title</param-name>
-      <param-value>User Services</param-value>
-    </init-param>
-    <init-param>
-      <param-name>api.description</param-name>
-      <param-value>Camel Rest Example with Swagger that provides an User REST service</param-value>
-    </init-param>
-    <init-param>
-      <param-name>api.contact.name</param-name>
-      <param-value>The Apache Camel team</param-value>
-    </init-param>
-    <load-on-startup>2</load-on-startup>
-  </servlet>
-
-  <!-- swagger api declaration -->
-  <servlet-mapping>
-    <servlet-name>SwaggerServlet</servlet-name>
-    <url-pattern>/api-docs/*</url-pattern>
-  </servlet-mapping>
-
   <!-- define that url path for the Camel Servlet to use -->
   <servlet-mapping>
     <servlet-name>CamelServlet</servlet-name>
     <url-pattern>/rest/*</url-pattern>
   </servlet-mapping>
 
-  <!-- enable CORS filter so people can use swagger ui to browse and test the apis -->
-  <filter>
-    <filter-name>RestSwaggerCorsFilter</filter-name>
-    <filter-class>org.apache.camel.swagger.servlet.RestSwaggerCorsFilter</filter-class>
-  </filter>
-
-  <filter-mapping>
-    <filter-name>RestSwaggerCorsFilter</filter-name>
-    <url-pattern>/api-docs/*</url-pattern>
-    <url-pattern>/rest/*</url-pattern>
-  </filter-mapping>
-
   <welcome-file-list>
     <welcome-file>home.html</welcome-file>
   </welcome-file-list>

http://git-wip-us.apache.org/repos/asf/camel/blob/f4027cd8/examples/camel-example-swagger-java/src/main/webapp/home.html
----------------------------------------------------------------------
diff --git a/examples/camel-example-swagger-java/src/main/webapp/home.html b/examples/camel-example-swagger-java/src/main/webapp/home.html
index dbd3700..cbb04f2 100644
--- a/examples/camel-example-swagger-java/src/main/webapp/home.html
+++ b/examples/camel-example-swagger-java/src/main/webapp/home.html
@@ -72,13 +72,13 @@
         <p/>
         This example offers an API browser using Swagger which is accessible from the following link
         <ul>
-            <li><a href="api-docs">api-docs</a> - list all CamelContexts in the JVM that may have REST services</li>
-            <li><a href="api-docs/myCamel">api-docs/myCamel</a> - myCamel - overview of the apis from the REST services</li>
-            <li><a href="api-docs/myCamel/user">api-docs/myCamel/user</a> - myCamel - api of the user REST service</li>
+            <li><a href="rest/api-docs">api-docs</a> - list all CamelContexts in the JVM that may have REST services</li>
+            <li><a href="rest/api-docs/myCamel">api-docs/myCamel</a> - myCamel - overview of the apis from the REST services</li>
+            <li><a href="rest/api-docs/myCamel/user">api-docs/myCamel/user</a> - myCamel - api of the user REST service</li>
         </ul>
 
         <p/>
-        To use the swagger ui, follow this link. Replace the URL value with this one to access the REST local resources http://localhost:8080/camel-example-swagger-java/api-docs/myCamel
+        To use the swagger ui, follow this link. Replace the URL value with this one to access the REST local resources http://localhost:8080/camel-example-swagger-java/rest/api-docs/myCamel
         <ul>
             <li><a href="index.html">swagger</a> - swagger ui page</li>
         </ul>