This repository was archived by the owner on Sep 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 176
Expand file tree
/
Copy pathSwaggerPlugin.scala
More file actions
176 lines (143 loc) · 5.59 KB
/
SwaggerPlugin.scala
File metadata and controls
176 lines (143 loc) · 5.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/**
* Copyright 2017 SmartBear Software, Inc.
*
* Licensed 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 play.modules.swagger
import java.io.File
import javax.inject.Inject
import io.swagger.config.{FilterFactory, ScannerFactory}
import play.modules.swagger.util.SwaggerContext
import io.swagger.core.filter.SwaggerSpecFilter
import play.api.inject.ApplicationLifecycle
import play.api.{Logger, Application}
import play.api.routing.Router
import scala.concurrent.Future
import scala.collection.JavaConversions._
import play.routes.compiler.{Route => PlayRoute, Include => PlayInclude, RoutesFileParser, StaticPart}
import scala.io.Source
trait SwaggerPlugin
class SwaggerPluginImpl @Inject()(lifecycle: ApplicationLifecycle, router: Router, app: Application) extends SwaggerPlugin {
val logger = Logger("swagger")
val config = app.configuration
logger.info("Swagger - starting initialisation...")
val apiVersion = config.getOptional[String]("api.version") match {
case None => "beta"
case Some(value) => value
}
val basePath = config.getOptional[String]("swagger.api.basepath")
.filter(path => !path.isEmpty)
.getOrElse("/")
val host = config.getOptional[String]("swagger.api.host")
.filter(host => !host.isEmpty)
.getOrElse("localhost:9000")
val title = config.getOptional[String]("swagger.api.info.title") match {
case None => ""
case Some(value)=> value
}
val description = config.getOptional[String]("swagger.api.info.description") match {
case None => ""
case Some(value)=> value
}
val termsOfServiceUrl = config.getOptional[String]("swagger.api.info.termsOfServiceUrl") match {
case None => ""
case Some(value)=> value
}
val contact = config.getOptional[String]("swagger.api.info.contact") match {
case None => ""
case Some(value)=> value
}
val license = config.getOptional[String]("swagger.api.info.license") match {
case None => ""
case Some(value)=> value
}
val licenseUrl = config.getOptional[String]("swagger.api.info.licenseUrl") match {
// licenceUrl needs to be a valid URL to validate against schema
case None => "http://licenseUrl"
case Some(value)=> value
}
SwaggerContext.registerClassLoader(app.classloader)
var scanner = new PlayApiScanner()
ScannerFactory.setScanner(scanner)
var swaggerConfig = new PlaySwaggerConfig()
swaggerConfig.description = description
swaggerConfig.basePath = basePath
swaggerConfig.contact = contact
swaggerConfig.version = apiVersion
swaggerConfig.title = title
swaggerConfig.host = host
swaggerConfig.termsOfServiceUrl = termsOfServiceUrl
swaggerConfig.license = license
swaggerConfig.licenseUrl = licenseUrl
PlayConfigFactory.setConfig(swaggerConfig)
val routes = parseRoutes
def parseRoutes: List[PlayRoute] = {
def playRoutesClassNameToFileName(className: String) = className.replace(".Routes", ".routes")
val routesFile = config.underlying.hasPath("play.http.router") match {
case false => "routes"
case true => config.getOptional[String]("play.http.router") match {
case None => "routes"
case Some(value)=> playRoutesClassNameToFileName(value)
}
}
//Parses multiple route files recursively
def parseRoutesHelper(routesFile: String, prefix: String): List[PlayRoute] = {
logger.debug(s"Processing route file '$routesFile' with prefix '$prefix'")
val routesContent = Source.fromInputStream(app.classloader.getResourceAsStream(routesFile)).mkString
val parsedRoutes = RoutesFileParser.parseContent(routesContent,new File(routesFile))
val routes = parsedRoutes.right.get.collect {
case (route: PlayRoute) => {
logger.debug(s"Adding route '$route'")
Seq(route.copy(path = route.path.copy(parts = StaticPart(prefix) +: route.path.parts)))
}
case (include: PlayInclude) => {
logger.debug(s"Processing route include $include")
parseRoutesHelper(playRoutesClassNameToFileName(include.router), include.prefix)
}
}.flatten
logger.debug(s"Finished processing route file '$routesFile'")
routes
}
parseRoutesHelper(routesFile, "")
}
val routesRules = Map(routes map
{ route =>
{
val routeName = s"${route.call.packageName}.${route.call.controller}$$.${route.call.method}"
(routeName -> route)
}
} : _*)
val route = new RouteWrapper(routesRules)
RouteFactory.setRoute(route)
app.configuration.getOptional[String]("swagger.filter") match {
case Some(e) if (e != "") => {
try {
FilterFactory setFilter SwaggerContext.loadClass(e).newInstance.asInstanceOf[SwaggerSpecFilter]
logger.debug("Setting swagger.filter to %s".format(e))
}
catch {
case ex: Exception => Logger("swagger").error("Failed to load filter " + e, ex)
}
}
case _ =>
}
val docRoot = ""
ApiListingCache.listing(docRoot, "127.0.0.1")
logger.info("Swagger - initialization done.")
// previous contents of Plugin.onStart
lifecycle.addStopHook { () =>
ApiListingCache.cache = None
logger.info("Swagger - stopped.")
Future.successful(())
}
}