diff --git a/k2-core/src/main/java/com/k2/core/WebConfiguration.java b/k2-core/src/main/java/com/k2/core/WebConfiguration.java index a03a8e0..563fd4f 100644 --- a/k2-core/src/main/java/com/k2/core/WebConfiguration.java +++ b/k2-core/src/main/java/com/k2/core/WebConfiguration.java @@ -33,6 +33,8 @@ public class WebConfiguration { * * @param port the port that jetty will listen on. * + * @param contextPath the application base path, by default is empty + * * @param serverCustomizer a the server customizer that configures specific * options in jetty. See serverCustomizer(...). It cannot be null. * @@ -40,6 +42,7 @@ public class WebConfiguration { */ @Bean public ConfigurableServletWebServerFactory servletContainer( @Value("${server.port:8081}") final int port, + @Value("${server.contextPath:}") final String contextPath, final JettyServerCustomizer serverCustomizer) { JettyServletWebServerFactory factory; diff --git a/k2-swagger/src/main/java/com/k2/swagger/Swagger.java b/k2-swagger/src/main/java/com/k2/swagger/Swagger.java index c6c8eda..1523add 100755 --- a/k2-swagger/src/main/java/com/k2/swagger/Swagger.java +++ b/k2-swagger/src/main/java/com/k2/swagger/Swagger.java @@ -47,14 +47,18 @@ public SwaggerRegistry getRegistry(final ModuleDefinition requestor) { * * @param moduleDefinition the module definition. It cannot be null. * + * @param contextPath the application base path, by default is empty + * * @param debug true if in debug mode. * * @return the swagger main controller, never returns null. */ @Bean SwaggerController controller(final ModuleDefinition moduleDefinition, + @Value("${server.contextPath:}") final String contextPath, @Value("${debug:#{false}}") final boolean debug) { Validate.notNull(registries, "The registries cannot be null."); - return new SwaggerController(moduleDefinition, registries, debug); + return new SwaggerController(moduleDefinition, registries, contextPath, + debug); } } diff --git a/k2-swagger/src/main/java/com/k2/swagger/SwaggerController.java b/k2-swagger/src/main/java/com/k2/swagger/SwaggerController.java index 0c04b1c..0b5448b 100755 --- a/k2-swagger/src/main/java/com/k2/swagger/SwaggerController.java +++ b/k2-swagger/src/main/java/com/k2/swagger/SwaggerController.java @@ -12,6 +12,7 @@ import java.util.List; import java.util.Scanner; +import org.apache.commons.lang3.StringUtils; import org.slf4j.LoggerFactory; import org.slf4j.Logger; @@ -50,6 +51,10 @@ public class SwaggerController { * spec, never null. */ private List registries; + /** The controller url context path, never null or empty. + */ + private String contextPath; + /** Constructor, creates a SwaggerController. * * @param theModuleDefinition the module definition of this module. It cannot @@ -58,14 +63,19 @@ public class SwaggerController { * @param theRegistries the registries with the swagger specs. It cannot be * null. * + * @param theContextPath the application context path. It cannot be null but + * is empty by default when not configured. + * * @param isDebug true if this controller is operating en debug mode. */ public SwaggerController(final ModuleDefinition theModuleDefinition, - final List theRegistries, final boolean isDebug) { + final List theRegistries, final String theContextPath, + final boolean isDebug) { Validate.notNull(theRegistries, "The registries cannot be null."); Validate.notNull(theModuleDefinition, "The definition cannot be null."); moduleDefinition = theModuleDefinition; registries = theRegistries; + contextPath = theContextPath; debug = isDebug; } @@ -107,21 +117,42 @@ public HttpEntity swaggerUi() { } String urls = ""; - for (SwaggerRegistry registry: registries) { + for (SwaggerRegistry registry : registries) { String idl = registry.getIdl(); String path = registry.getRequestorPath(); // Only non-null idls. if (idl != null) { - urls += "{name: \"" + path + "\", url:\"" + idl + "\"},\n"; + urls += "{name: \"" + path + "\", url:\"" + contextPath + idl + "\"}" + + ",\n"; } } String html; if (!urls.isEmpty()) { html = template.replaceAll("@@urls@@", "[" + urls + "]"); + html = setWebjarsPathToRoot(html); } else { html = "No idl found - better remove the swagger module."; } - return new HttpEntity(html); + + return new HttpEntity<>(html); + } + + /** Calculates the relative path where to look for webjars static content. + * + * @param template the index.html template with the @@webjarPath@@ to + * replace. It cannot be null nor empty. + * + * @return a String with the @@webjarPath@@ replaced, never null nor empty. + */ + private String setWebjarsPathToRoot(final String template) { + String path = StringUtils.isBlank(contextPath) ? "/" : contextPath; + int count = Paths.get(path).getNameCount(); + String webjarsPath = "../../"; + for (int i = 0; i < count; i++) { + webjarsPath += "../"; + } + + return template.replaceAll("@@webjarsPath@@", webjarsPath); } } diff --git a/k2-swagger/src/main/resources/com/k2/swagger/index.html b/k2-swagger/src/main/resources/com/k2/swagger/index.html index 884755e..8161aab 100755 --- a/k2-swagger/src/main/resources/com/k2/swagger/index.html +++ b/k2-swagger/src/main/resources/com/k2/swagger/index.html @@ -5,15 +5,16 @@ Swagger UI + href='@@webjarsPath@@webjars/swagger-ui/3.9.2/swagger-ui.css' >
- - +