Skip to content

Commit d5adefd

Browse files
committed
added mule-compatible version
1 parent 3515adf commit d5adefd

File tree

3 files changed

+254
-0
lines changed

3 files changed

+254
-0
lines changed

modules/swagger-mule/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
README.md

modules/swagger-mule/pom.xml

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
<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">
2+
<parent>
3+
<groupId>com.wordnik</groupId>
4+
<artifactId>swagger-project_2.10</artifactId>
5+
<version>1.3.10</version>
6+
<relativePath>../..</relativePath>
7+
</parent>
8+
<modelVersion>4.0.0</modelVersion>
9+
<groupId>com.wordnik</groupId>
10+
<artifactId>swagger-mule_2.10</artifactId>
11+
<packaging>jar</packaging>
12+
<name>swagger-mule</name>
13+
<version>1.3.10</version>
14+
<build>
15+
<sourceDirectory>src/main/scala</sourceDirectory>
16+
<plugins>
17+
<plugin>
18+
<groupId>net.alchim31.maven</groupId>
19+
<artifactId>scala-maven-plugin</artifactId>
20+
<executions>
21+
<execution>
22+
<goals>
23+
<goal>add-source</goal>
24+
<goal>compile</goal>
25+
<goal>testCompile</goal>
26+
</goals>
27+
</execution>
28+
</executions>
29+
<configuration>
30+
<configuration>
31+
<recompileMode>incremental</recompileMode>
32+
</configuration>
33+
<jvmArgs>
34+
<jvmArg>-Xmx384m</jvmArg>
35+
</jvmArgs>
36+
<args>
37+
<arg>-target:jvm-1.6</arg>
38+
<arg>-deprecation</arg>
39+
</args>
40+
<launchers>
41+
<launcher>
42+
<id>run-scalatest</id>
43+
<mainClass>org.scalatest.tools.Runner</mainClass>
44+
<args>
45+
<arg>-p</arg>
46+
<arg>${project.build.testOutputDirectory}</arg>
47+
</args>
48+
<jvmArgs>
49+
<jvmArg>-Xmx512m</jvmArg>
50+
</jvmArgs>
51+
</launcher>
52+
</launchers>
53+
</configuration>
54+
</plugin>
55+
</plugins>
56+
<defaultGoal>install</defaultGoal>
57+
<resources>
58+
<resource>
59+
<directory>src/main/resources</directory>
60+
<excludes>
61+
<exclude>logback.xml</exclude>
62+
</excludes>
63+
</resource>
64+
</resources>
65+
<testResources>
66+
<testResource>
67+
<directory>src/test/resources</directory>
68+
</testResource>
69+
</testResources>
70+
</build>
71+
<dependencies>
72+
<dependency>
73+
<groupId>org.scalatest</groupId>
74+
<artifactId>scalatest_2.10</artifactId>
75+
<scope>test</scope>
76+
<version>${scala-test-version}</version>
77+
</dependency>
78+
<dependency>
79+
<groupId>junit</groupId>
80+
<artifactId>junit</artifactId>
81+
<scope>test</scope>
82+
<version>${junit-version}</version>
83+
</dependency>
84+
<dependency>
85+
<groupId>com.wordnik</groupId>
86+
<artifactId>swagger-jaxrs_2.10</artifactId>
87+
<version>${project.parent.version}</version>
88+
<scope>compile</scope>
89+
</dependency>
90+
<dependency>
91+
<groupId>javax.servlet</groupId>
92+
<artifactId>servlet-api</artifactId>
93+
<version>${servlet-api-version}</version>
94+
</dependency>
95+
<dependency>
96+
<groupId>org.reflections</groupId>
97+
<artifactId>reflections</artifactId>
98+
<version>0.9.8</version>
99+
</dependency>
100+
<dependency>
101+
<groupId>org.reflections</groupId>
102+
<artifactId>reflections</artifactId>
103+
<version>0.9.8</version>
104+
<scope>compile</scope>
105+
</dependency>
106+
<dependency>
107+
<groupId>joda-time</groupId>
108+
<artifactId>joda-time</artifactId>
109+
<version>2.2</version>
110+
<scope>test</scope>
111+
</dependency>
112+
<dependency>
113+
<groupId>org.joda</groupId>
114+
<artifactId>joda-convert</artifactId>
115+
<version>1.2</version>
116+
<scope>test</scope>
117+
</dependency>
118+
<dependency>
119+
<groupId>ch.qos.logback</groupId>
120+
<artifactId>logback-classic</artifactId>
121+
<version>${logback-version}</version>
122+
<scope>provided</scope>
123+
</dependency>
124+
<dependency>
125+
<groupId>ch.qos.logback</groupId>
126+
<artifactId>logback-core</artifactId>
127+
<version>${logback-version}</version>
128+
<scope>provided</scope>
129+
</dependency>
130+
</dependencies>
131+
<properties>
132+
<servlet-api-version>2.5</servlet-api-version>
133+
<junit-version>4.8.1</junit-version>
134+
<scala-test-version>1.9</scala-test-version>
135+
<slf4j-version>1.6.3</slf4j-version>
136+
<logback-version>1.0.1</logback-version>
137+
</properties>
138+
</project>
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
package com.intuit.platform.swagger
2+
3+
import com.wordnik.swagger.core.util.ReaderUtil
4+
import com.wordnik.swagger.annotations._
5+
import com.wordnik.swagger.jaxrs.config.BeanConfig
6+
import com.wordnik.swagger.core.filter._
7+
import com.wordnik.swagger.config._
8+
import com.wordnik.swagger.model._
9+
import com.wordnik.swagger.reader._
10+
import com.wordnik.swagger.core.util.JsonSerializer
11+
12+
import org.slf4j.LoggerFactory
13+
14+
import javax.ws.rs._
15+
import javax.ws.rs.core.{ Response, MediaType }
16+
17+
@Path("/api-docs")
18+
@Produces(Array(MediaType.APPLICATION_JSON))
19+
class ApiListingJSON {
20+
private val LOGGER = LoggerFactory.getLogger(classOf[ApiListingJSON])
21+
22+
@GET
23+
def getResource(): Response = {
24+
val docRoot = ""
25+
val queryParams = Map[String, List[String]]()
26+
val cookies = Map[String, String]()
27+
val headers = Map[String, List[String]]()
28+
29+
val f = new SpecFilter
30+
val listings = ApiListingCache.listing(docRoot).map(specs => {
31+
(for(spec <- specs.values)
32+
yield f.filter(spec, FilterFactory.filter, queryParams, cookies, headers)
33+
).filter(m => m.apis.size > 0)
34+
})
35+
val references = (for(listing <- listings.getOrElse(List())) yield {
36+
ApiListingReference(listing.resourcePath, listing.description)
37+
}).toList
38+
val config = ConfigFactory.config
39+
val resourceListing = ResourceListing(config.apiVersion,
40+
config.swaggerVersion,
41+
references
42+
)
43+
44+
Response.ok(JsonSerializer.asJson(resourceListing)).build();
45+
}
46+
47+
/**
48+
* individual api listing
49+
**/
50+
@GET
51+
@Path("/{route: .+}")
52+
def apiDeclaration (
53+
@PathParam("route") route: String
54+
): Response = {
55+
LOGGER.debug("requested apiDeclaration for " + route)
56+
val docRoot = this.getClass.getAnnotation(classOf[Path]).value
57+
val queryParams = Map[String, List[String]]()
58+
val cookies = Map[String, String]()
59+
val headers = Map[String, List[String]]()
60+
val f = new SpecFilter
61+
val pathPart = cleanRoute(route)
62+
LOGGER.debug("requested route " + pathPart)
63+
val listings = ApiListingCache.listing(docRoot).map(specs => {
64+
(for(spec <- specs.values) yield {
65+
LOGGER.debug("inspecting path " + spec.resourcePath)
66+
f.filter(spec, FilterFactory.filter, queryParams, cookies, headers)
67+
}).filter(m => {
68+
val resourcePath = m.resourcePath match {
69+
case e: String if(e.startsWith("/")) => e
70+
case e: String => "/" + e
71+
}
72+
resourcePath == pathPart
73+
})
74+
}).toList.flatten
75+
76+
listings.size match {
77+
case 1 => Response.ok(listings(0)).build
78+
case _ => Response.status(404).build
79+
}
80+
}
81+
82+
// ensure leading slash, remove trailing
83+
def cleanRoute(route: String) = {
84+
val cleanStart = {
85+
if(route.startsWith("/")) route
86+
else "/" + route
87+
}
88+
if(cleanStart.endsWith("/")) cleanStart.substring(0, cleanStart.length - 1)
89+
else cleanStart
90+
}
91+
}
92+
93+
object ApiListingCache extends ReaderUtil {
94+
var cache: Option[Map[String, com.wordnik.swagger.model.ApiListing]] = None
95+
96+
def listing(docRoot: String): Option[Map[String, com.wordnik.swagger.model.ApiListing]] = {
97+
cache.orElse{
98+
ClassReaders.reader.map{reader =>
99+
ScannerFactory.scanner.map(scanner => {
100+
val classes = scanner match {
101+
case scanner: Scanner => {
102+
val beanConfig = scanner.asInstanceOf[BeanConfig]
103+
beanConfig.classesFromContext(null, null)
104+
}
105+
case _ =>List()
106+
}
107+
val listings = (for(cls <- classes) yield reader.read(docRoot, cls, ConfigFactory.config)).flatten
108+
val mergedListings = groupByResourcePath(listings)
109+
cache = Some(mergedListings.map(m => (m.resourcePath, m)).toMap)
110+
})
111+
}
112+
cache
113+
}
114+
}
115+
}

0 commit comments

Comments
 (0)