Skip to content

Commit 15efcac

Browse files
eciucajplock
authored andcommitted
Added the posibility to configure the contextRoot (federecio#56)
* Added the posibility to configure the contextRoot * Fixed license header * Compute SwaggerAssetsPath depending on contextRootPrefix * Updated swagger version and disabled hanging test * Enabled DefaultServerWithAuthenticationSeleniumTest * Add ViewBundle diamond reference type * Clean unused import
1 parent f8f8046 commit 15efcac

4 files changed

Lines changed: 35 additions & 15 deletions

File tree

src/main/java/io/federecio/dropwizard/swagger/SwaggerBundle.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public abstract class SwaggerBundle<T extends Configuration>
3535

3636
@Override
3737
public void initialize(Bootstrap<?> bootstrap) {
38-
bootstrap.addBundle(new ViewBundle<>());
38+
bootstrap.addBundle(new ViewBundle<Configuration>());
3939
ModelConverters.getInstance()
4040
.addConverter(new ModelResolver(bootstrap.getObjectMapper()));
4141
}
@@ -67,7 +67,8 @@ public void run(T configuration, Environment environment) throws Exception {
6767
environment.jersey().register(new SwaggerSerializers());
6868
environment.jersey().register(new SwaggerResource(
6969
configurationHelper.getUrlPattern(),
70-
swaggerBundleConfiguration.getSwaggerViewConfiguration()));
70+
swaggerBundleConfiguration.getSwaggerViewConfiguration(),
71+
swaggerBundleConfiguration.getContextRoot()));
7172
}
7273

7374
protected abstract SwaggerBundleConfiguration getSwaggerBundleConfiguration(

src/main/java/io/federecio/dropwizard/swagger/SwaggerBundleConfiguration.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public class SwaggerBundleConfiguration {
5050
private SwaggerViewConfiguration swaggerViewConfiguration = new SwaggerViewConfiguration();
5151
private Boolean prettyPrint = true;
5252
private String host;
53+
private String contextRoot = "/";
5354
private String[] schemes = new String[] { "http" };
5455
private Boolean enabled = true;
5556

@@ -208,6 +209,16 @@ public void setHost(String host) {
208209
this.host = host;
209210
}
210211

212+
@JsonProperty
213+
public String getContextRoot() {
214+
return contextRoot;
215+
}
216+
217+
@JsonProperty
218+
public void setContextRoot(String contextRoot) {
219+
this.contextRoot = contextRoot;
220+
}
221+
211222
@JsonProperty
212223
public String[] getSchemes() {
213224
return schemes;
@@ -245,7 +256,7 @@ public BeanConfig build(String urlPattern) {
245256
config.setLicenseUrl(licenseUrl);
246257
config.setTermsOfServiceUrl(termsOfServiceUrl);
247258
config.setPrettyPrint(prettyPrint);
248-
config.setBasePath(urlPattern);
259+
config.setBasePath(("/".equals(contextRoot) ? "" : contextRoot) + urlPattern);
249260
config.setResourcePackage(resourcePackage);
250261
config.setSchemes(schemes);
251262
config.setHost(host);

src/main/java/io/federecio/dropwizard/swagger/SwaggerResource.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,23 @@
2323
@Produces(MediaType.TEXT_HTML)
2424
public class SwaggerResource {
2525
private final SwaggerViewConfiguration config;
26+
private final String contextRoot;
2627
private final String urlPattern;
2728

2829
public SwaggerResource(String urlPattern, SwaggerViewConfiguration config) {
2930
this.urlPattern = urlPattern;
3031
this.config = config;
32+
this.contextRoot = "/";
33+
}
34+
35+
public SwaggerResource(String urlPattern, SwaggerViewConfiguration config, String contextRoot) {
36+
this.config = config;
37+
this.urlPattern = urlPattern;
38+
this.contextRoot = contextRoot;
3139
}
3240

3341
@GET
3442
public SwaggerView get() {
35-
return new SwaggerView(urlPattern, config);
43+
return new SwaggerView(contextRoot, urlPattern, config);
3644
}
3745
}

src/main/java/io/federecio/dropwizard/swagger/SwaggerView.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,22 @@ public class SwaggerView extends View {
3333

3434
private final SwaggerViewConfiguration viewConfiguration;
3535

36-
public SwaggerView(@Nonnull final String urlPattern,
37-
@Nonnull SwaggerViewConfiguration config) {
36+
public SwaggerView(@Nonnull final String contextRoot,
37+
@Nonnull final String urlPattern,
38+
@Nonnull SwaggerViewConfiguration config) {
3839
super(config.getTemplateUrl(), StandardCharsets.UTF_8);
3940

40-
if ("/".equals(urlPattern)) {
41-
swaggerAssetsPath = SWAGGER_URI_PATH;
42-
} else {
43-
swaggerAssetsPath = urlPattern + SWAGGER_URI_PATH;
44-
}
41+
String contextRootPrefix = "/".equals(contextRoot) ? "" : contextRoot;
4542

46-
if ("/".equals(urlPattern)) {
47-
contextPath = "";
48-
} else {
49-
contextPath = urlPattern;
43+
if (!contextRootPrefix.isEmpty()) { //swagger-static should be found on the root context
44+
swaggerAssetsPath = contextRootPrefix + SWAGGER_URI_PATH;
45+
}
46+
else {
47+
swaggerAssetsPath = (urlPattern.equals("/") ? SWAGGER_URI_PATH : (urlPattern + SWAGGER_URI_PATH));
5048
}
5149

50+
contextPath = urlPattern.equals("/") ? contextRootPrefix : (contextRootPrefix + urlPattern);
51+
5252
this.viewConfiguration = config;
5353
}
5454

0 commit comments

Comments
 (0)