Rest DSL. camel-swagger 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/1a6fe606 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/1a6fe606 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/1a6fe606 Branch: refs/heads/master Commit: 1a6fe606c48c3a33aa2131345e0c9a684ac58b1b Parents: 5595ed6 Author: Claus Ibsen <[email protected]> Authored: Fri Aug 8 17:29:33 2014 +0200 Committer: Claus Ibsen <[email protected]> Committed: Fri Aug 8 17:29:33 2014 +0200 ---------------------------------------------------------------------- components/camel-swagger/pom.xml | 85 ++++++++++++- .../apache/camel/component/swagger/MyDemo.java | 29 ----- .../component/swagger/MyDynamicCompiler.java | 95 -------------- .../RestSwaggerApiDeclarationServlet.scala | 122 ++++++++++++++++++ .../component/swagger/RestSwaggerReader.scala | 125 +++++++++++++++++++ .../swagger/RestSwaggerReaderTest.java | 63 ++++++++++ .../apache/camel/component/swagger/TestMe.java | 47 ------- .../camel-example-servlet-rest-tomcat/pom.xml | 35 +++++- .../camel/example/rest/UserRouteBuilder.java | 5 + .../src/main/resources/camel-config.xml | 6 +- .../src/main/resources/log4j.properties | 1 + .../src/main/webapp/WEB-INF/web.xml | 34 +++++ 12 files changed, 471 insertions(+), 176 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/1a6fe606/components/camel-swagger/pom.xml ---------------------------------------------------------------------- diff --git a/components/camel-swagger/pom.xml b/components/camel-swagger/pom.xml index 9e2710b..fe978c8 100644 --- a/components/camel-swagger/pom.xml +++ b/components/camel-swagger/pom.xml @@ -15,7 +15,8 @@ See the License for the specific language governing permissions and limitations under the License. --> -<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> @@ -30,8 +31,10 @@ <description>Camel Swagger support</description> <properties> - <camel.osgi.export.pkg>org.apache.camel.component.swgger.*</camel.osgi.export.pkg> - <camel.osgi.export.service>org.apache.camel.spi.ComponentResolver;component=swagger</camel.osgi.export.service> + <camel.osgi.import.before.defaults> + scala.*;version="[2.10,2.12)" + </camel.osgi.import.before.defaults> + <camel.osgi.export.pkg>org.apache.camel.component.swagger.*</camel.osgi.export.pkg> </properties> <dependencies> @@ -42,6 +45,11 @@ </dependency> <dependency> + <groupId>org.springframework</groupId> + <artifactId>spring-web</artifactId> + </dependency> + + <dependency> <groupId>com.wordnik</groupId> <artifactId>swagger-annotations</artifactId> <version>1.3.7</version> @@ -51,6 +59,18 @@ <artifactId>swagger-jaxrs_2.10</artifactId> <version>1.3.7</version> </dependency> + <dependency> + <groupId>com.wordnik</groupId> + <artifactId>swagger-servlet_2.10</artifactId> + <version>1.3.7</version> + </dependency> + + <dependency> + <groupId>javax.servlet</groupId> + <artifactId>servlet-api</artifactId> + <version>2.5</version> + <scope>provided</scope> + </dependency> <!-- testing --> <dependency> @@ -75,4 +95,63 @@ </dependency> </dependencies> + <build> + <pluginManagement> + <plugins> + <!-- Eclipse m2e Lifecycle Management --> + <plugin> + <groupId>org.eclipse.m2e</groupId> + <artifactId>lifecycle-mapping</artifactId> + <version>${lifecycle-mapping-version}</version> + <configuration> + <lifecycleMappingMetadata> + <pluginExecutions> + <pluginExecution> + <pluginExecutionFilter> + <groupId>net.alchim31.maven</groupId> + <artifactId>scala-maven-plugin</artifactId> + <versionRange>${scala-maven-plugin-version}</versionRange> + <goals> + <goal>compile</goal> + <goal>testCompile</goal> + </goals> + </pluginExecutionFilter> + <action> + <ignore/> + </action> + </pluginExecution> + </pluginExecutions> + </lifecycleMappingMetadata> + </configuration> + </plugin> + </plugins> + </pluginManagement> + + <sourceDirectory>src/main/scala</sourceDirectory> + + <plugins> + <plugin> + <groupId>net.alchim31.maven</groupId> + <artifactId>scala-maven-plugin</artifactId> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-eclipse-plugin</artifactId> + <configuration> + <projectnatures> + <projectnature>ch.epfl.lamp.sdt.core.scalanature</projectnature> + <projectnature>org.eclipse.jdt.core.javanature</projectnature> + </projectnatures> + <buildcommands> + <buildcommand>ch.epfl.lamp.sdt.core.scalabuilder</buildcommand> + </buildcommands> + <classpathContainers> + <classpathContainer>ch.epfl.lamp.sdt.launching.SCALA_CONTAINER</classpathContainer> + <classpathContainer>org.eclipse.jdt.launching.JRE_CONTAINER</classpathContainer> + </classpathContainers> + </configuration> + </plugin> + </plugins> + </build> + </project> http://git-wip-us.apache.org/repos/asf/camel/blob/1a6fe606/components/camel-swagger/src/main/java/org/apache/camel/component/swagger/MyDemo.java ---------------------------------------------------------------------- diff --git a/components/camel-swagger/src/main/java/org/apache/camel/component/swagger/MyDemo.java b/components/camel-swagger/src/main/java/org/apache/camel/component/swagger/MyDemo.java deleted file mode 100644 index ccbd9f9..0000000 --- a/components/camel-swagger/src/main/java/org/apache/camel/component/swagger/MyDemo.java +++ /dev/null @@ -1,29 +0,0 @@ -/** - * 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; - -import com.wordnik.swagger.annotations.Api; -import com.wordnik.swagger.annotations.ApiOperation; - -@Api("foo") -public class MyDemo { - - @ApiOperation(value = "hi", produces = "text/plain", httpMethod = "POST") - public String doSomething(String hi) { - return "Hello " + hi; - } -} http://git-wip-us.apache.org/repos/asf/camel/blob/1a6fe606/components/camel-swagger/src/main/java/org/apache/camel/component/swagger/MyDynamicCompiler.java ---------------------------------------------------------------------- diff --git a/components/camel-swagger/src/main/java/org/apache/camel/component/swagger/MyDynamicCompiler.java b/components/camel-swagger/src/main/java/org/apache/camel/component/swagger/MyDynamicCompiler.java deleted file mode 100644 index 30e6996..0000000 --- a/components/camel-swagger/src/main/java/org/apache/camel/component/swagger/MyDynamicCompiler.java +++ /dev/null @@ -1,95 +0,0 @@ -/** - * 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; - -import java.io.File; -import java.io.FileWriter; -import java.io.Writer; -import java.net.URI; -import java.net.URL; -import java.net.URLClassLoader; -import java.util.Arrays; -import javax.tools.JavaCompiler; -import javax.tools.JavaFileObject; -import javax.tools.SimpleJavaFileObject; -import javax.tools.StandardJavaFileManager; -import javax.tools.ToolProvider; - -public class MyDynamicCompiler { - - public static void main(String[] args) throws Exception{ - - StringBuilder sb = new StringBuilder(64); - sb.append("package foobar;\n"); - sb.append("public class HelloWorld {\n"); - sb.append(" public void doStuff() {\n"); - sb.append(" System.out.println(\"Hello world\");\n"); - sb.append(" }\n"); - sb.append("}\n"); - - File file = new File("target/foobar/"); - file.mkdirs(); - file = new File("target/foobar/HelloWorld.java"); - Writer writer = new FileWriter("target/foobar/HelloWorld.java"); - writer.write(sb.toString()); - writer.flush(); - - JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); - StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null); - -// List<SimpleJavaFileObject> list = new ArrayList<SimpleJavaFileObject>(); -// list.add(new JavaSourceFromString("foobar.HelloWorld", sb.toString())); - - boolean done = compiler.getTask(null, fileManager, null, null, null, fileManager.getJavaFileObjectsFromFiles(Arrays.asList(file))).call(); - System.out.println(done); - - URLClassLoader classLoader = new URLClassLoader(new URL[]{new File("target").toURI().toURL()}); - Class clazz = classLoader.loadClass("foobar.HelloWorld"); - Object obj = clazz.newInstance(); - - System.out.println(obj); - - - fileManager.close(); - } - - - - public static class JavaSourceFromString extends SimpleJavaFileObject { - - /** - * The source code of this "file". - */ - final String code; - - /** - * Constructs a new JavaSourceFromString. - * @param name the name of the compilation unit represented by this file object - * @param code the source code for the compilation unit represented by this file object - */ - public JavaSourceFromString(String name, String code) { - super(URI.create("string:///" + name.replace('.', '/') + JavaFileObject.Kind.SOURCE.extension), - JavaFileObject.Kind.SOURCE); - this.code = code; - } - - @Override - public CharSequence getCharContent(boolean ignoreEncodingErrors) { - return code; - } - } -} http://git-wip-us.apache.org/repos/asf/camel/blob/1a6fe606/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 new file mode 100644 index 0000000..ca7106b --- /dev/null +++ b/components/camel-swagger/src/main/scala/org/apache/camel/component/swagger/RestSwaggerApiDeclarationServlet.scala @@ -0,0 +1,122 @@ +package org.apache.camel.component.swagger + +import com.wordnik.swagger.servlet.listing.{ApiListingCache, ApiDeclarationServlet} +import javax.servlet.http.{HttpServletResponse, HttpServletRequest} +import com.wordnik.swagger.core.filter.SpecFilter +import com.wordnik.swagger.config.{ConfigFactory, FilterFactory} +import com.wordnik.swagger.model.{ResourceListing, ApiListingReference} +import com.wordnik.swagger.core.util.JsonSerializer +import javax.servlet.ServletConfig +import org.springframework.web.context.support.WebApplicationContextUtils +import org.springframework.web.context.WebApplicationContext +import org.apache.camel.CamelContext +import org.slf4j.LoggerFactory + +/** + * 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. + */ +class RestSwaggerApiDeclarationServlet extends ApiDeclarationServlet { + + private val LOG = LoggerFactory.getLogger(classOf[RestSwaggerApiDeclarationServlet]) + + var spring: WebApplicationContext = null + val reader = new RestSwaggerReader() + + override def init(config: ServletConfig): Unit = { + super.init(config) + LOG.info("init") + spring = WebApplicationContextUtils.getRequiredWebApplicationContext(config.getServletContext) + LOG.info("init found spring {}", spring) + } + + override def renderResourceListing(request: HttpServletRequest, response: HttpServletResponse) = { + LOG.info("renderResourceListing") + + val docRoot = "" + val queryParams = Map[String, List[String]]() + val cookies = Map[String, String]() + val headers = Map[String, List[String]]() + + val camel = spring.getBean(classOf[CamelContext]) + LOG.info("renderResourceListing camel -> {}", camel) + if (camel != null) { + + val config = ConfigFactory.config + val rests = camel.getRestDefinitions + LOG.info("renderResourceListing rests -> {}", rests) + val list = reader.read(rests.get(0), config) + LOG.info("renderResourceListing reader -> {}", list) + val cache = Some(list.map(m => (m.resourcePath, m)).toMap) + LOG.info("renderResourceListing reader -> {}", cache) + + val f = new SpecFilter + // val listings = ApiListingCache.listing(docRoot).map(specs => { + val listings = cache.map(specs => { + (for (spec <- specs.values) + yield f.filter(spec, FilterFactory.filter, queryParams, cookies, headers) + ).filter(m => m.apis.size > 0) + }) + val references = (for (listing <- listings.getOrElse(List())) yield { + ApiListingReference(listing.resourcePath, listing.description) + }).toList + val resourceListing = ResourceListing(config.apiVersion, + config.swaggerVersion, + references + ) + LOG.info("renderResourceListing write response -> {}", resourceListing) + response.getOutputStream.write(JsonSerializer.asJson(resourceListing).getBytes("utf-8")) + } + } + + override def renderApiDeclaration(request: HttpServletRequest, response: HttpServletResponse) = { + val route = request.getPathInfo + val docRoot = request.getPathInfo + val f = new SpecFilter + val queryParams = Map[String, List[String]]() + val cookies = Map[String, String]() + val headers = Map[String, List[String]]() + val pathPart = docRoot + + val camel = spring.getBean(classOf[CamelContext]) + LOG.info("renderApiDeclaration camel -> {}", camel) + if (camel != null) { + + val config = ConfigFactory.config + val rests = camel.getRestDefinitions + LOG.info("renderApiDeclaration rests -> {}", rests) + val list = reader.read(rests.get(0), config) + LOG.info("renderApiDeclaration reader -> {}", list) + val cache = Some(list.map(m => (m.resourcePath, m)).toMap) + LOG.info("renderApiDeclaration reader -> {}", cache) + + // val listings = ApiListingCache.listing(docRoot).map(specs => { + val listings = cache.map(specs => { + (for (spec <- specs.values) yield { + f.filter(spec, FilterFactory.filter, queryParams, cookies, headers) + }).filter(m => m.resourcePath == pathPart) + }).toList.flatten + listings.size match { + case 1 => { + LOG.info("renderResourceListing write response -> {}", listings.head) + response.getOutputStream.write(JsonSerializer.asJson(listings.head).getBytes("utf-8")) + } + case _ => response.setStatus(404) + } + } + response.setStatus(404) + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/1a6fe606/components/camel-swagger/src/main/scala/org/apache/camel/component/swagger/RestSwaggerReader.scala ---------------------------------------------------------------------- diff --git a/components/camel-swagger/src/main/scala/org/apache/camel/component/swagger/RestSwaggerReader.scala b/components/camel-swagger/src/main/scala/org/apache/camel/component/swagger/RestSwaggerReader.scala new file mode 100644 index 0000000..ff05501 --- /dev/null +++ b/components/camel-swagger/src/main/scala/org/apache/camel/component/swagger/RestSwaggerReader.scala @@ -0,0 +1,125 @@ +package org.apache.camel.component.swagger + +import org.apache.camel.model.rest.{VerbDefinition, RestDefinition} +import com.wordnik.swagger.config.SwaggerConfig +import com.wordnik.swagger.model.{ApiDescription, Operation, ApiListing} +import org.slf4j.LoggerFactory +import com.wordnik.swagger.core.util.ModelUtil +import com.wordnik.swagger.core.SwaggerSpec +import scala.collection.mutable.ListBuffer +import java.util.Locale + +// to iterate using for loop +import scala.collection.JavaConverters._ + +/** + * 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. + */ +class RestSwaggerReader { + + private val LOG = LoggerFactory.getLogger(classOf[RestSwaggerReader]) + + def read(rest: RestDefinition, config: SwaggerConfig): Option[ApiListing] = { + + val api = rest.getPath + if (api != null) { + val fullPath = { + if (api.startsWith("/")) api.substring(1) + else api + } + val (resourcePath, subpath) = { + if (fullPath.indexOf("/") > 0) { + val pos = fullPath.indexOf("/") + ("/" + fullPath.substring(0, pos), fullPath.substring(pos)) + } + else ("/", fullPath) + } + + LOG.debug("read routes from classes: %s, %s".format(resourcePath, subpath)) + + val operations = new ListBuffer[Operation] + + val list = rest.getVerbs.asScala + for (verb: VerbDefinition <- list) { + + var method = verb.asVerb().toUpperCase(Locale.US) + + var responseType = verb.getOutType match { + case e: String => e + case _ => "java.lang.Void" + } + + var p = verb.getProduces + if (p == null) { + p = rest.getProduces + } + val produces = p match { + case e: String if e != "" => e.split(",").map(_.trim).toList + case _ => List() + } + + var c = verb.getConsumes + if (c == null) { + c = rest.getConsumes + } + val consumes = c match { + case e: String if e != "" => e.split(",").map(_.trim).toList + case _ => List() + } + + operations += Operation( + method, + "", + "", + responseType, + "", + 0, + produces, + consumes, + List(), + List(), + List(), + List(), + None) + } + + if (operations.size > 0) { + val apis = List( + ApiDescription( + "/" + fullPath, + Some(""), + operations.toList)) + val models = ModelUtil.modelsFromApis(apis) + Some( + ApiListing( + config.apiVersion, + SwaggerSpec.version, + config.basePath, + resourcePath, + List(), // produces + List(), // consumes + List(), // protocols + List(), // authorizations + ModelUtil.stripPackages(apis), + models) + ) + } + else None + } + else None + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/1a6fe606/components/camel-swagger/src/test/java/org/apache/camel/component/swagger/RestSwaggerReaderTest.java ---------------------------------------------------------------------- diff --git a/components/camel-swagger/src/test/java/org/apache/camel/component/swagger/RestSwaggerReaderTest.java b/components/camel-swagger/src/test/java/org/apache/camel/component/swagger/RestSwaggerReaderTest.java new file mode 100644 index 0000000..a8d633d --- /dev/null +++ b/components/camel-swagger/src/test/java/org/apache/camel/component/swagger/RestSwaggerReaderTest.java @@ -0,0 +1,63 @@ +/** + * 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; + +import com.wordnik.swagger.config.SwaggerConfig; +import com.wordnik.swagger.model.ApiListing; +import junit.framework.TestCase; +import org.apache.camel.CamelContext; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.impl.DefaultCamelContext; +import org.apache.camel.model.rest.RestDefinition; +import org.junit.Ignore; +import org.junit.Test; +import scala.Option; +import scala.collection.immutable.Map; + +public class RestSwaggerReaderTest extends TestCase { + + @Test + @Ignore + public void testReaderRead() throws Exception { + CamelContext context = new DefaultCamelContext(); + context.addRoutes(new RouteBuilder() { + @Override + public void configure() throws Exception { + restConfiguration().component("jetty").host("localhost").port(9090); + + rest("/hello") + .get("/hi").to("log:hi"); + } + }); + context.start(); + + RestDefinition rest = context.getRestDefinitions().get(0); + assertNotNull(rest); + + SwaggerConfig config = new SwaggerConfig(); + RestSwaggerReader reader = new RestSwaggerReader(); + Option<ApiListing> option = reader.read(rest, config); + assertNotNull(option); + ApiListing listing = option.get(); + assertNotNull(listing); + + System.out.println(listing); + + context.stop(); + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/1a6fe606/components/camel-swagger/src/test/java/org/apache/camel/component/swagger/TestMe.java ---------------------------------------------------------------------- diff --git a/components/camel-swagger/src/test/java/org/apache/camel/component/swagger/TestMe.java b/components/camel-swagger/src/test/java/org/apache/camel/component/swagger/TestMe.java deleted file mode 100644 index 476811c..0000000 --- a/components/camel-swagger/src/test/java/org/apache/camel/component/swagger/TestMe.java +++ /dev/null @@ -1,47 +0,0 @@ -/** - * 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; - -import com.wordnik.swagger.config.ScannerFactory; -import com.wordnik.swagger.config.SwaggerConfig; -import com.wordnik.swagger.jaxrs.listing.ApiListingResource; -import com.wordnik.swagger.jaxrs.listing.ApiListingResourceJSON; -import com.wordnik.swagger.jaxrs.reader.DefaultJaxrsApiReader; -import com.wordnik.swagger.model.ApiListing; -import scala.Option; - -public class TestMe { - - public static void main(String[] args) throws Exception { - //use the following as another option -// BeanConfig bean = new BeanConfig(); -// bean.setResourcePackage("org.apache.camel.component.swagger"); -// bean.setBasePath("http://localhost:8080/spring"); -// bean.setVersion("1.0"); -// bean.setScan(true); - - SwaggerConfig config = new SwaggerConfig(); - DefaultJaxrsApiReader reader = new DefaultJaxrsApiReader(); - Option<ApiListing> api = reader.read("", MyDemo.class, config); - if (api != null) { - ApiListing list = api.get(); - System.out.println(list); - } - - - } -} http://git-wip-us.apache.org/repos/asf/camel/blob/1a6fe606/examples/camel-example-servlet-rest-tomcat/pom.xml ---------------------------------------------------------------------- diff --git a/examples/camel-example-servlet-rest-tomcat/pom.xml b/examples/camel-example-servlet-rest-tomcat/pom.xml index 3009fc6..5804e59 100755 --- a/examples/camel-example-servlet-rest-tomcat/pom.xml +++ b/examples/camel-example-servlet-rest-tomcat/pom.xml @@ -46,6 +46,11 @@ <groupId>org.apache.camel</groupId> <artifactId>camel-servlet</artifactId> </dependency> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-swagger</artifactId> + <version>${project.version}</version> + </dependency> <!-- use for json binding --> <dependency> @@ -59,6 +64,23 @@ <artifactId>spring-web</artifactId> </dependency> + <!-- swagger --> + <dependency> + <groupId>com.wordnik</groupId> + <artifactId>swagger-annotations</artifactId> + <version>1.3.7</version> + </dependency> + <dependency> + <groupId>com.wordnik</groupId> + <artifactId>swagger-jaxrs_2.10</artifactId> + <version>1.3.7</version> + </dependency> + <dependency> + <groupId>com.wordnik</groupId> + <artifactId>swagger-servlet_2.10</artifactId> + <version>1.3.7</version> + </dependency> + <!-- logging --> <dependency> <groupId>log4j</groupId> @@ -68,7 +90,18 @@ <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </dependency> - + <dependency> + <groupId>ch.qos.logback</groupId> + <artifactId>logback-classic</artifactId> + <version>1.0.13</version> + <scope>compile</scope> + </dependency> + <dependency> + <groupId>ch.qos.logback</groupId> + <artifactId>logback-core</artifactId> + <version>1.0.13</version> + <scope>compile</scope> + </dependency> </dependencies> <build> http://git-wip-us.apache.org/repos/asf/camel/blob/1a6fe606/examples/camel-example-servlet-rest-tomcat/src/main/java/org/apache/camel/example/rest/UserRouteBuilder.java ---------------------------------------------------------------------- diff --git a/examples/camel-example-servlet-rest-tomcat/src/main/java/org/apache/camel/example/rest/UserRouteBuilder.java b/examples/camel-example-servlet-rest-tomcat/src/main/java/org/apache/camel/example/rest/UserRouteBuilder.java index fe4c5ce..807f4b4 100644 --- a/examples/camel-example-servlet-rest-tomcat/src/main/java/org/apache/camel/example/rest/UserRouteBuilder.java +++ b/examples/camel-example-servlet-rest-tomcat/src/main/java/org/apache/camel/example/rest/UserRouteBuilder.java @@ -16,8 +16,13 @@ */ package org.apache.camel.example.rest; +import com.wordnik.swagger.config.SwaggerConfig; +import com.wordnik.swagger.model.ApiListing; import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.swagger.RestSwaggerReader; import org.apache.camel.model.rest.RestBindingMode; +import org.apache.camel.model.rest.RestDefinition; +import scala.Option; /** * Define REST services using the Camel REST DSL http://git-wip-us.apache.org/repos/asf/camel/blob/1a6fe606/examples/camel-example-servlet-rest-tomcat/src/main/resources/camel-config.xml ---------------------------------------------------------------------- diff --git a/examples/camel-example-servlet-rest-tomcat/src/main/resources/camel-config.xml b/examples/camel-example-servlet-rest-tomcat/src/main/resources/camel-config.xml index 2e4b838..e9674d8 100755 --- a/examples/camel-example-servlet-rest-tomcat/src/main/resources/camel-config.xml +++ b/examples/camel-example-servlet-rest-tomcat/src/main/resources/camel-config.xml @@ -23,13 +23,17 @@ http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd"> + <!--<bean id="generator" class="org.apache.camel.example.rest.DeleteMe" init-method="generate">--> + <!--<property name="camelContext" ref="myCamel"/>--> + <!--</bean>--> + <!-- a rest service which uses binding to/from pojos --> <bean id="userRoutes" class="org.apache.camel.example.rest.UserRouteBuilder"/> <!-- a bean for user services --> <bean id="userService" class="org.apache.camel.example.rest.UserService"/> - <camelContext xmlns="http://camel.apache.org/schema/spring"> + <camelContext id="myCamel" xmlns="http://camel.apache.org/schema/spring"> <routeBuilder ref="userRoutes"/> </camelContext> http://git-wip-us.apache.org/repos/asf/camel/blob/1a6fe606/examples/camel-example-servlet-rest-tomcat/src/main/resources/log4j.properties ---------------------------------------------------------------------- diff --git a/examples/camel-example-servlet-rest-tomcat/src/main/resources/log4j.properties b/examples/camel-example-servlet-rest-tomcat/src/main/resources/log4j.properties index b5c95ab..e822239 100755 --- a/examples/camel-example-servlet-rest-tomcat/src/main/resources/log4j.properties +++ b/examples/camel-example-servlet-rest-tomcat/src/main/resources/log4j.properties @@ -19,6 +19,7 @@ log4j.rootLogger=INFO, console # settings for specific packages +log4j.logger.com.wordnik.swagger=DEBUG #log4j.logger.org.apache.camel.component.servlet=DEBUG #log4j.logger.org.apache.camel=DEBUG http://git-wip-us.apache.org/repos/asf/camel/blob/1a6fe606/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 1c48f59..0833518 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 @@ -39,11 +39,39 @@ <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> + + <!-- swagger servlet reader --> + <servlet> + <servlet-name>DefaultServletReaderConfig</servlet-name> + <servlet-class>com.wordnik.swagger.servlet.config.DefaultServletReaderConfig</servlet-class> + <load-on-startup>1</load-on-startup> + <init-param> + <param-name>swagger.resource.package</param-name> + <param-value>org.apache.camel.example.rest</param-value> + </init-param> + <init-param> + <param-name>swagger.api.basepath</param-name> + <param-value>http://localhost:8082</param-value> + </init-param> + <init-param> + <param-name>api.version</param-name> + <param-value>1.0.1</param-value> + </init-param> + </servlet> + <!-- to setup Camel Servlet --> <servlet> <servlet-name>CamelServlet</servlet-name> <display-name>Camel Http Transport Servlet</display-name> <servlet-class>org.apache.camel.component.servlet.CamelHttpTransportServlet</servlet-class> + <load-on-startup>3</load-on-startup> + </servlet> + + <servlet> + <servlet-name>ApiDeclarationServlet</servlet-name> + <!--<servlet-class>com.wordnik.swagger.servlet.listing.ApiDeclarationServlet</servlet-class>--> + <servlet-class>org.apache.camel.component.swagger.RestSwaggerApiDeclarationServlet</servlet-class> + <load-on-startup>2</load-on-startup> </servlet> <!-- define that url path for the Camel Servlet to use --> @@ -52,4 +80,10 @@ <url-pattern>/rest/*</url-pattern> </servlet-mapping> + <!-- swagger api declaration --> + <servlet-mapping> + <servlet-name>ApiDeclarationServlet</servlet-name> + <url-pattern>/api-docs/*</url-pattern> + </servlet-mapping> + </web-app> \ No newline at end of file
