11/**
2- * Copyright 2014 Reverb Technologies, Inc.
3- *
4- * Licensed under the Apache License, Version 2.0 (the "License");
5- * you may not use this file except in compliance with the License.
6- * You may obtain a copy of the License at
7- *
8- * http://www.apache.org/licenses/LICENSE-2.0
9- *
10- * Unless required by applicable law or agreed to in writing, software
11- * distributed under the License is distributed on an "AS IS" BASIS,
12- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13- * See the License for the specific language governing permissions and
14- * limitations under the License.
15- */
2+ * Copyright 2014 Reverb Technologies, Inc.
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
1616
1717package play .modules .swagger
1818
1919import java .io .File
2020import javax .inject .Inject
21+
2122import io .swagger .config .{FilterFactory , ScannerFactory }
22- import play .modules .swagger .util .SwaggerContext
2323import io .swagger .core .filter .SwaggerSpecFilter
2424import play .api .inject .ApplicationLifecycle
25- import play .api .{Logger , Application }
2625import play .api .routing .Router
27- import scala . concurrent . Future
28- import scala . collection . JavaConversions . _
29- import play .routes .compiler .{Route => PlayRoute , Include => PlayInclude , RoutesFileParser , StaticPart }
26+ import play . api .{ Application , Logger }
27+ import play . modules . swagger . util . SwaggerContext
28+ import play .routes .compiler .{RoutesFileParser , StaticPart , Include => PlayInclude , Route => PlayRoute }
3029
30+ import scala .collection .JavaConversions ._
31+ import scala .concurrent .Future
3132import scala .io .Source
3233
3334trait SwaggerPlugin
@@ -54,33 +55,33 @@ class SwaggerPluginImpl @Inject()(lifecycle: ApplicationLifecycle, router: Route
5455
5556 val title = config.getString(" swagger.api.info.title" ) match {
5657 case None => " "
57- case Some (value)=> value
58+ case Some (value) => value
5859 }
5960
6061 val description = config.getString(" swagger.api.info.description" ) match {
6162 case None => " "
62- case Some (value)=> value
63+ case Some (value) => value
6364 }
6465
6566 val termsOfServiceUrl = config.getString(" swagger.api.info.termsOfServiceUrl" ) match {
6667 case None => " "
67- case Some (value)=> value
68+ case Some (value) => value
6869 }
6970
7071 val contact = config.getString(" swagger.api.info.contact" ) match {
7172 case None => " "
72- case Some (value)=> value
73+ case Some (value) => value
7374 }
7475
7576 val license = config.getString(" swagger.api.info.license" ) match {
7677 case None => " "
77- case Some (value)=> value
78+ case Some (value) => value
7879 }
7980
8081 val licenseUrl = config.getString(" swagger.api.info.licenseUrl" ) match {
8182 // licenceUrl needs to be a valid URL to validate against schema
8283 case None => " http://licenseUrl"
83- case Some (value)=> value
84+ case Some (value) => value
8485 }
8586
8687 SwaggerContext .registerClassLoader(app.classloader)
@@ -106,44 +107,23 @@ class SwaggerPluginImpl @Inject()(lifecycle: ApplicationLifecycle, router: Route
106107 val routes = parseRoutes
107108
108109 def parseRoutes : List [PlayRoute ] = {
109- def playRoutesClassNameToFileName (className : String ) = className.replace(" .Routes" , " .routes" )
110110
111111 val routesFile = config.underlying.hasPath(" play.http.router" ) match {
112112 case false => " routes"
113113 case true => config.getString(" play.http.router" ) match {
114114 case None => " routes"
115- case Some (value)=> playRoutesClassNameToFileName(value)
115+ case Some (value) => SwaggerPluginHelper . playRoutesClassNameToFileName(value)
116116 }
117117 }
118- // Parses multiple route files recursively
119- def parseRoutesHelper (routesFile : String , prefix : String ): List [PlayRoute ] = {
120- logger.debug(s " Processing route file ' $routesFile' with prefix ' $prefix' " )
121-
122- val routesContent = Source .fromInputStream(app.classloader.getResourceAsStream(routesFile)).mkString
123- val parsedRoutes = RoutesFileParser .parseContent(routesContent,new File (routesFile))
124- val routes = parsedRoutes.right.get.collect {
125- case (route : PlayRoute ) => {
126- logger.debug(s " Adding route ' $route' " )
127- Seq (route.copy(path = route.path.copy(parts = StaticPart (prefix) +: route.path.parts)))
128- }
129- case (include : PlayInclude ) => {
130- logger.debug(s " Processing route include $include" )
131- parseRoutesHelper(playRoutesClassNameToFileName(include.router), include.prefix)
132- }
133- }.flatten
134- logger.debug(s " Finished processing route file ' $routesFile' " )
135- routes
136- }
137- parseRoutesHelper(routesFile, " " )
118+
119+ SwaggerPluginHelper .parseRoutes(routesFile, " " , logger.debug(_), app.classloader)
138120 }
139121
140- val routesRules = Map (routes map
141- { route =>
142- {
143- val routeName = s " ${route.call.packageName}. ${route.call.controller}$$ . ${route.call.method}"
144- (routeName -> route)
145- }
146- } : _* )
122+ val routesRules = Map (routes map { route => {
123+ val routeName = s " ${route.call.packageName}. ${route.call.controller}$$ . ${route.call.method}"
124+ (routeName -> route)
125+ }
126+ }: _* )
147127
148128 val route = new RouteWrapper (routesRules)
149129 RouteFactory .setRoute(route)
@@ -174,3 +154,36 @@ class SwaggerPluginImpl @Inject()(lifecycle: ApplicationLifecycle, router: Route
174154 }
175155
176156}
157+
158+ object SwaggerPluginHelper {
159+ def playRoutesClassNameToFileName (className : String ): String = className.replace(" .Routes" , " .routes" )
160+
161+ // Parses multiple route files recursively
162+ def parseRoutes (routesFile : String , prefix : String , debug : String => Unit , classLoader : ClassLoader ): List [PlayRoute ] = {
163+ debug(s " Processing route file ' $routesFile' with prefix ' $prefix' " )
164+
165+ val routesContent = Source .fromInputStream(classLoader.getResourceAsStream(routesFile)).mkString
166+ val parsedRoutes = RoutesFileParser .parseContent(routesContent, new File (routesFile))
167+ val routes = parsedRoutes.right.get.collect {
168+ case (route : PlayRoute ) =>
169+ debug(s " Adding route ' $route' " )
170+ (prefix, route.path.parts) match {
171+ case (" " , _) => Seq (route)
172+ case (_, Seq ()) => Seq (route.copy(path = route.path.copy(parts = StaticPart (prefix) +: route.path.parts)))
173+ case (_, Seq (StaticPart (" " ))) => Seq (route.copy(path = route.path.copy(parts = StaticPart (prefix) +: route.path.parts)))
174+ case (_, Seq (StaticPart (" /" ))) => Seq (route.copy(path = route.path.copy(parts = StaticPart (prefix) +: route.path.parts)))
175+ case (_, _) => Seq (route.copy(path = route.path.copy(parts = StaticPart (prefix) +: StaticPart (" /" ) +: route.path.parts)))
176+ }
177+ case (include : PlayInclude ) =>
178+ debug(s " Processing route include $include" )
179+ val newPrefix = if (prefix == " " ) {
180+ include.prefix
181+ } else {
182+ s " $prefix/ ${include.prefix}"
183+ }
184+ parseRoutes(playRoutesClassNameToFileName(include.router), newPrefix, debug, classLoader)
185+ }.flatten
186+ debug(s " Finished processing route file ' $routesFile' " )
187+ routes
188+ }
189+ }
0 commit comments