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/08/26 11:56:19 UTC
[2/2] git commit: CAMEL-7689: camel-swagger has a spring servlet when
using spring to lookup CamelContext.
CAMEL-7689: camel-swagger has a spring servlet when using spring to lookup CamelContext.
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/374dbd91
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/374dbd91
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/374dbd91
Branch: refs/heads/master
Commit: 374dbd913a44cfce2ee1173113dcea6f6951328a
Parents: 2854e18
Author: Claus Ibsen <da...@apache.org>
Authored: Tue Aug 26 11:41:46 2014 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Tue Aug 26 11:56:08 2014 +0200
----------------------------------------------------------------------
components/camel-swagger/pom.xml | 5 +--
.../RestSwaggerApiDeclarationServlet.scala | 28 +++++-------
...SpringRestSwaggerApiDeclarationServlet.scala | 47 ++++++++++++++++++++
.../src/main/webapp/WEB-INF/web.xml | 3 +-
4 files changed, 63 insertions(+), 20 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/374dbd91/components/camel-swagger/pom.xml
----------------------------------------------------------------------
diff --git a/components/camel-swagger/pom.xml b/components/camel-swagger/pom.xml
index 29c945a..f78defa 100644
--- a/components/camel-swagger/pom.xml
+++ b/components/camel-swagger/pom.xml
@@ -44,12 +44,11 @@
<artifactId>camel-core</artifactId>
</dependency>
- <!--
- TODO: we should avoid spring dependency and have some plugin/spi
- -->
+ <!-- if using spring then the spring-web is assumed to be provided -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
+ <scope>provided</scope>
</dependency>
<dependency>
http://git-wip-us.apache.org/repos/asf/camel/blob/374dbd91/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 4fa6297..925042f 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
@@ -19,27 +19,21 @@ package org.apache.camel.component.swagger
import javax.servlet.http.{HttpServlet, HttpServletResponse, HttpServletRequest}
import javax.servlet.ServletConfig
-import com.wordnik.swagger.core.SwaggerContext
import com.wordnik.swagger.core.filter.SpecFilter
import com.wordnik.swagger.core.util.JsonSerializer
import com.wordnik.swagger.config.{SwaggerConfig, ConfigFactory, FilterFactory}
import com.wordnik.swagger.model.{ApiInfo, ResourceListing, ApiListingReference}
-import org.springframework.web.context.support.WebApplicationContextUtils
-import org.springframework.web.context.WebApplicationContext
import org.apache.camel.CamelContext
import org.slf4j.LoggerFactory
/**
* A Http Servlet to expose the REST services as Swagger APIs.
*/
-class RestSwaggerApiDeclarationServlet extends HttpServlet {
-
- // TODO: this has spring dependency, find a way to make it work with blueprint/spring/cdi/servlet-listener etc
+abstract class RestSwaggerApiDeclarationServlet extends HttpServlet {
private val LOG = LoggerFactory.getLogger(classOf[RestSwaggerApiDeclarationServlet])
- var spring: WebApplicationContext = null
val reader = new RestSwaggerReader()
var camel: CamelContext = null
val swaggerConfig: SwaggerConfig = ConfigFactory.config
@@ -80,18 +74,20 @@ class RestSwaggerApiDeclarationServlet extends HttpServlet {
val apiInfo = new ApiInfo(title, description, termsOfServiceUrl, contact, license, licenseUrl)
swaggerConfig.setApiInfo(apiInfo)
- spring = WebApplicationContextUtils.getRequiredWebApplicationContext(config.getServletContext)
- if (spring != null) {
- camel = spring.getBean(classOf[CamelContext])
- if (camel != null) {
- // TODO: if this is not sufficient we need to use Camel's resolveClass API instead
- SwaggerContext.registerClassLoader(camel.getApplicationContextClassLoader)
- }
+ camel = lookupCamelContext(config)
+ if (camel == null) {
+ LOG.warn("Cannot find CamelContext to be used.")
}
-
- LOG.info("init found spring {}", spring)
}
+ /**
+ * Used for implementations to lookup the CamelContext to be used.
+ *
+ * @param config the servlet config
+ * @return the CamelContext to use, or <tt>null</tt> if no CamelContext was found
+ */
+ def lookupCamelContext(config: ServletConfig) : CamelContext
+
override protected def doGet(request: HttpServletRequest, response: HttpServletResponse) = {
val route = request.getPathInfo
// render overview if the route is empty or is the root path
http://git-wip-us.apache.org/repos/asf/camel/blob/374dbd91/components/camel-swagger/src/main/scala/org/apache/camel/component/swagger/spring/SpringRestSwaggerApiDeclarationServlet.scala
----------------------------------------------------------------------
diff --git a/components/camel-swagger/src/main/scala/org/apache/camel/component/swagger/spring/SpringRestSwaggerApiDeclarationServlet.scala b/components/camel-swagger/src/main/scala/org/apache/camel/component/swagger/spring/SpringRestSwaggerApiDeclarationServlet.scala
new file mode 100644
index 0000000..0b028da
--- /dev/null
+++ b/components/camel-swagger/src/main/scala/org/apache/camel/component/swagger/spring/SpringRestSwaggerApiDeclarationServlet.scala
@@ -0,0 +1,47 @@
+/**
+ * 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.swagger.spring
+
+import javax.servlet.ServletConfig
+
+import com.wordnik.swagger.core.SwaggerContext
+
+import org.apache.camel.component.swagger.RestSwaggerApiDeclarationServlet
+import org.apache.camel.CamelContext
+
+import org.springframework.web.context.support.WebApplicationContextUtils
+import org.springframework.web.context.WebApplicationContext
+
+/**
+ * To lookup CamelContext from a Spring application.
+ */
+class SpringRestSwaggerApiDeclarationServlet extends RestSwaggerApiDeclarationServlet {
+
+ var spring: WebApplicationContext = null
+
+ override def lookupCamelContext(config: ServletConfig): CamelContext = {
+ spring = WebApplicationContextUtils.getRequiredWebApplicationContext(config.getServletContext)
+ if (spring != null) {
+ camel = spring.getBean(classOf[CamelContext])
+ if (camel != null) {
+ SwaggerContext.registerClassLoader(camel.getApplicationContextClassLoader)
+ }
+ }
+ camel
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/374dbd91/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 cd3e527..3d53e85 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
@@ -50,8 +50,9 @@
<!-- START SNIPPET: e1 -->
<!-- to setup Camel Swagger api servlet -->
<servlet>
+ <!-- we are using spring so use the spring swagger servlet -->
<servlet-name>ApiDeclarationServlet</servlet-name>
- <servlet-class>org.apache.camel.component.swagger.RestSwaggerApiDeclarationServlet</servlet-class>
+ <servlet-class>org.apache.camel.component.swagger.spring.SpringRestSwaggerApiDeclarationServlet</servlet-class>
<init-param>
<param-name>base.path</param-name>
<param-value>http://localhost:8080/rest</param-value>