forked from federecio/dropwizard-swagger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SwaggerBundleConfiguration.java
339 lines (281 loc) · 8.87 KB
/
SwaggerBundleConfiguration.java
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
// Copyright (C) 2014 Federico Recio
/**
* 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 io.federecio.dropwizard.swagger;
import javax.annotation.Nullable;
import org.hibernate.validator.constraints.NotEmpty;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Strings;
import io.swagger.jaxrs.config.BeanConfig;
import io.swagger.models.Contact;
/**
* For the meaning of all these properties please refer to Swagger documentation
* or {@link io.swagger.jaxrs.config.BeanConfig}
*/
@JsonIgnoreProperties(ignoreUnknown = true)
public class SwaggerBundleConfiguration {
/**
* This is the only property that is required for Swagger to work correctly.
* <p/>
* It is a comma separated list of the all the packages that contain the
* {@link io.swagger.annotations.Api} annotated resources
*/
@NotEmpty
private String resourcePackage = "";
@Nullable
private String title;
@Nullable
private String version;
@Nullable
private String description;
@Nullable
private String termsOfServiceUrl;
@Nullable
private String contact;
@Nullable
private String contactEmail;
@Nullable
private String contactUrl;
@Nullable
private String license;
@Nullable
private String licenseUrl;
private SwaggerViewConfiguration swaggerViewConfiguration = new SwaggerViewConfiguration();
private SwaggerOAuth2Configuration swaggerOAuth2Configuration = new SwaggerOAuth2Configuration();
private boolean prettyPrint = true;
@Nullable
private String host;
private String contextRoot = "/";
private String[] schemes = new String[] { "http" };
private boolean enabled = true;
private boolean includeSwaggerResource = true;
/**
* For most of the scenarios this property is not needed.
* <p/>
* This is not a property for Swagger but for bundle to set up Swagger UI
* correctly. It only needs to be used of the root path or the context path
* is set programmatically and therefore cannot be derived correctly. The
* problem arises in that if you set the root path or context path in the
* run() method in your Application subclass the bundle has already been
* initialized by that time and so does not know you set the path
* programmatically.
*/
@Nullable
private String uriPrefix;
@JsonProperty
public String getResourcePackage() {
return resourcePackage;
}
@JsonProperty
public void setResourcePackage(String resourcePackage) {
this.resourcePackage = resourcePackage;
}
@Nullable
@JsonProperty
public String getTitle() {
return title;
}
@JsonProperty
public void setTitle(@Nullable String title) {
this.title = title;
}
@Nullable
@JsonProperty
public String getVersion() {
return version;
}
@JsonProperty
public void setVersion(@Nullable String version) {
this.version = version;
}
@Nullable
@JsonProperty
public String getDescription() {
return description;
}
@JsonProperty
public void setDescription(@Nullable String description) {
this.description = description;
}
@Nullable
@JsonProperty
public String getTermsOfServiceUrl() {
return termsOfServiceUrl;
}
@JsonProperty
public void setTermsOfServiceUrl(@Nullable String termsOfServiceUrl) {
this.termsOfServiceUrl = termsOfServiceUrl;
}
@Nullable
@JsonProperty
public String getContact() {
return contact;
}
@JsonProperty
public void setContact(@Nullable String contact) {
this.contact = contact;
}
@Nullable
@JsonProperty
public String getContactEmail() {
return contactEmail;
}
@JsonProperty
public void setContactEmail(@Nullable String contactEmail) {
this.contactEmail = contactEmail;
}
@Nullable
@JsonProperty
public String getContactUrl() {
return contactUrl;
}
@JsonProperty
public void setContactUrl(@Nullable String contactUrl) {
this.contactUrl = contactUrl;
}
@Nullable
@JsonProperty
public String getLicense() {
return license;
}
@JsonProperty
public void setLicense(@Nullable String license) {
this.license = license;
}
@Nullable
@JsonProperty
public String getLicenseUrl() {
return licenseUrl;
}
@JsonProperty
public void setLicenseUrl(@Nullable String licenseUrl) {
this.licenseUrl = licenseUrl;
}
@Nullable
@JsonProperty
public String getUriPrefix() {
return uriPrefix;
}
@JsonProperty
public void setUriPrefix(@Nullable String uriPrefix) {
this.uriPrefix = uriPrefix;
}
@JsonProperty
public SwaggerViewConfiguration getSwaggerViewConfiguration() {
return swaggerViewConfiguration;
}
@JsonProperty
public void setSwaggerViewConfiguration(
final SwaggerViewConfiguration swaggerViewConfiguration) {
this.swaggerViewConfiguration = swaggerViewConfiguration;
}
@JsonProperty
public SwaggerOAuth2Configuration getSwaggerOAuth2Configuration() {
return swaggerOAuth2Configuration;
}
@JsonProperty("oauth2")
public void setSwaggerOAuth2Configuration(
final SwaggerOAuth2Configuration swaggerOAuth2Configuration) {
this.swaggerOAuth2Configuration = swaggerOAuth2Configuration;
}
@JsonProperty
public boolean isPrettyPrint() {
return prettyPrint;
}
@JsonProperty
public void setIsPrettyPrint(final boolean isPrettyPrint) {
this.prettyPrint = isPrettyPrint;
}
@Nullable
@JsonProperty
public String getHost() {
return host;
}
@JsonProperty
public void setHost(@Nullable String host) {
this.host = host;
}
@JsonProperty
public String getContextRoot() {
return contextRoot;
}
@JsonProperty
public void setContextRoot(String contextRoot) {
this.contextRoot = contextRoot;
}
@JsonProperty
public String[] getSchemes() {
return schemes;
}
@JsonProperty
public void setSchemes(String[] schemes) {
this.schemes = schemes;
}
@JsonProperty
public boolean isEnabled() {
return enabled;
}
@JsonProperty
public void setIsEnabled(final boolean isEnabled) {
this.enabled = isEnabled;
}
@JsonProperty
public boolean isIncludeSwaggerResource() {
return includeSwaggerResource;
}
@JsonProperty
public void setIncludeSwaggerResource(final boolean include) {
this.includeSwaggerResource = include;
}
@JsonIgnore
public BeanConfig build(String urlPattern) {
if (Strings.isNullOrEmpty(resourcePackage)) {
throw new IllegalStateException(
"Resource package needs to be specified"
+ " for Swagger to correctly detect annotated resources");
}
final BeanConfig config = new BeanConfig();
config.setTitle(title);
config.setVersion(version);
config.setDescription(description);
config.setContact(contact);
config.setLicense(license);
config.setLicenseUrl(licenseUrl);
config.setTermsOfServiceUrl(termsOfServiceUrl);
config.setPrettyPrint(prettyPrint);
config.setBasePath(
("/".equals(contextRoot) ? "" : contextRoot) + urlPattern);
config.setResourcePackage(resourcePackage);
config.setSchemes(schemes);
config.setHost(host);
config.setScan(true);
// Assign contact email/url after scan, since BeanConfig.scan will
// create a new info.Contact instance, thus overriding any info.Contact
// settings prior to scan.
if (contactEmail != null || contactUrl != null) {
if (config.getInfo().getContact() == null) {
config.getInfo().setContact(new Contact());
}
if (contactEmail != null) {
config.getInfo().getContact().setEmail(contactEmail);
}
if (contactUrl != null) {
config.getInfo().getContact().setUrl(contactUrl);
}
}
return config;
}
}