/
RestSwaggerApiDeclarationServlet.scala
233 lines (208 loc) · 8.34 KB
/
RestSwaggerApiDeclarationServlet.scala
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/**
* 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.net.URL
import javax.servlet.http.{HttpServlet, HttpServletResponse, HttpServletRequest}
import javax.servlet.ServletConfig
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.apache.camel.model.rest.RestDefinition
import org.slf4j.LoggerFactory
import scala.collection.mutable
/**
* A Http Servlet to expose the REST services as Swagger APIs.
*/
abstract class RestSwaggerApiDeclarationServlet extends HttpServlet {
private val LOG = LoggerFactory.getLogger(classOf[RestSwaggerApiDeclarationServlet])
val reader = new RestSwaggerReader()
var camelId: String = null
val swaggerConfig: SwaggerConfig = ConfigFactory.config
var cors: Boolean = false
var initDone: Boolean = false
override def init(config: ServletConfig): Unit = {
super.init(config)
// configure swagger options
var s = config.getInitParameter("api.version")
if (s != null) {
swaggerConfig.setApiVersion(s)
}
s = config.getInitParameter("swagger.version")
if (s != null) {
swaggerConfig.setSwaggerVersion(s)
}
s = config.getInitParameter("base.path")
if (s != null) {
swaggerConfig.setBasePath(s)
}
s = config.getInitParameter("api.path")
if (s != null) {
swaggerConfig.setApiPath(s)
}
s = config.getInitParameter("cors")
if (s != null) {
cors = "true".equalsIgnoreCase(s)
}
s = config.getInitParameter("camelId")
if (s != null) {
camelId = s
}
val title = config.getInitParameter("api.title")
val description = config.getInitParameter("api.description")
val termsOfServiceUrl = config.getInitParameter("api.termsOfServiceUrl")
val contact = config.getInitParameter("api.contact")
val license = config.getInitParameter("api.license")
val licenseUrl = config.getInitParameter("api.licenseUrl")
val apiInfo = new ApiInfo(title, description, termsOfServiceUrl, contact, license, licenseUrl)
swaggerConfig.setApiInfo(apiInfo)
}
def getRestDefinitions(camelId: String) : mutable.Buffer[RestDefinition]
override protected def doGet(request: HttpServletRequest, response: HttpServletResponse) = {
if (!initDone) {
initBaseAndApiPaths(request)
}
val route = request.getPathInfo
// render overview if the route is empty or is the root path
if (route != null && route != "" && route != "/") {
renderApiDeclaration(request, response)
} else {
renderResourceListing(request, response)
}
}
def initBaseAndApiPaths(request: HttpServletRequest) = {
var base = swaggerConfig.getBasePath
if (base == null || !base.startsWith("http")) {
// base path is configured using relative, so lets calculate the absolute url now we have the http request
val url = new URL(request.getRequestURL.toString)
if (base == null) {
base = ""
}
val path = translateContextPath(request)
if (url.getPort != 80 && url.getPort != -1) {
base = url.getProtocol + "://" + url.getHost + ":" + url.getPort + path + "/" + base
} else {
base = url.getProtocol + "://" + url.getHost + request.getContextPath + "/" + base
}
swaggerConfig.setBasePath(base)
}
base = swaggerConfig.getApiPath
if (base == null || !base.startsWith("http")) {
// api path is configured using relative, so lets calculate the absolute url now we have the http request
val url = new URL(request.getRequestURL.toString)
if (base == null) {
base = ""
}
val path = translateContextPath(request)
if (url.getPort != 80 && url.getPort != -1) {
base = url.getProtocol + "://" + url.getHost + ":" + url.getPort + path + "/" + base
} else {
base = url.getProtocol + "://" + url.getHost + request.getContextPath + "/" + base
}
swaggerConfig.setApiPath(base)
}
initDone = true
}
/**
* We do only want the base context-path and not sub paths
*/
def translateContextPath(request: HttpServletRequest): String = {
var path = request.getContextPath
if (path.isEmpty || path.equals("/")) {
return ""
} else {
val idx = path.lastIndexOf("/")
if (idx > 0) {
return path.substring(0, idx)
}
}
path
}
/**
* Renders the resource listing which is the overview of all the apis
*/
def renderResourceListing(request: HttpServletRequest, response: HttpServletResponse) = {
LOG.trace("renderResourceListing")
val queryParams = Map[String, List[String]]()
val cookies = Map[String, String]()
val headers = Map[String, List[String]]()
if (cors) {
response.setHeader("Access-Control-Allow-Headers", "Origin, Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers")
response.setHeader("Access-Control-Allow-Methods", "GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS, CONNECT, PATCH")
response.setHeader("Access-Control-Allow-Origin", "*")
}
val rests = getRestDefinitions(camelId)
if (rests != null) {
val f = new SpecFilter
val listings = RestApiListingCache.listing(rests, swaggerConfig).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(
swaggerConfig.apiVersion,
swaggerConfig.swaggerVersion,
references,
List(),
swaggerConfig.info
)
LOG.debug("renderResourceListing write response -> {}", resourceListing)
response.getOutputStream.write(JsonSerializer.asJson(resourceListing).getBytes("utf-8"))
} else {
response.setStatus(204)
}
}
/**
* Renders the api listing of a single resource
*/
def renderApiDeclaration(request: HttpServletRequest, response: HttpServletResponse) = {
LOG.trace("renderApiDeclaration")
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
if (cors) {
response.setHeader("Access-Control-Allow-Headers", "Origin, Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers")
response.setHeader("Access-Control-Allow-Methods", "GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS, CONNECT, PATCH")
response.setHeader("Access-Control-Allow-Origin", "*")
}
val rests = getRestDefinitions(camelId)
if (rests != null) {
val listings = RestApiListingCache.listing(rests, swaggerConfig).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.debug("renderResourceListing write response -> {}", listings.head)
response.getOutputStream.write(JsonSerializer.asJson(listings.head).getBytes("utf-8"))
}
case _ => response.setStatus(404)
}
} else {
// no data
response.setStatus(204)
}
}
}