Skip to content

Make contextPath configurable #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions k2-core/src/main/java/com/k2/core/WebConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,16 @@ 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.
*
* @return the factory, never null.
*/
@Bean public ConfigurableServletWebServerFactory servletContainer(
@Value("${server.port:8081}") final int port,
@Value("${server.contextPath:}") final String contextPath,
final JettyServerCustomizer serverCustomizer) {

JettyServletWebServerFactory factory;
Expand Down
6 changes: 5 additions & 1 deletion k2-swagger/src/main/java/com/k2/swagger/Swagger.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

39 changes: 35 additions & 4 deletions k2-swagger/src/main/java/com/k2/swagger/SwaggerController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -50,6 +51,10 @@ public class SwaggerController {
* spec, never null. */
private List<SwaggerRegistry> 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
Expand All @@ -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<SwaggerRegistry> theRegistries, final boolean isDebug) {
final List<SwaggerRegistry> 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;
}

Expand Down Expand Up @@ -107,21 +117,42 @@ public HttpEntity<String> 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<String>(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);
}
}

7 changes: 4 additions & 3 deletions k2-swagger/src/main/resources/com/k2/swagger/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@
<title>Swagger UI</title>

<link rel="stylesheet" type="text/css"
href='../../webjars/swagger-ui/3.9.2/swagger-ui.css' >
href='@@webjarsPath@@webjars/swagger-ui/3.9.2/swagger-ui.css' >
</head>

<body>

<div id="swagger-ui"></div>

<script src='../../webjars/swagger-ui/3.9.2/swagger-ui-bundle.js'> </script>
<script src='../../webjars/swagger-ui/3.9.2/swagger-ui-standalone-preset.js'>
<script src='@@webjarsPath@@webjars/swagger-ui/3.9.2/swagger-ui-bundle.js'>
</script>
<script src='@@webjarsPath@@webjars/swagger-ui/3.9.2/swagger-ui-standalone-preset.js'>
</script>

<script type="text/javascript">
Expand Down
39 changes: 32 additions & 7 deletions k2-swagger/src/test/java/com/k2/swagger/SwaggerControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ public class SwaggerControllerTest {
List<SwaggerRegistry> registries = new LinkedList<>();

SwaggerController controller;
controller = new SwaggerController(moduleDefinition, registries, false);
controller = new SwaggerController(moduleDefinition, registries, "",
false);

String body = controller.swaggerUi().getBody();

Expand All @@ -54,27 +55,48 @@ public class SwaggerControllerTest {
List<SwaggerRegistry> registries = Arrays.asList(registry1, registry2);

SwaggerController controller;
controller = new SwaggerController(moduleDefinition, registries, false);
controller = new SwaggerController(moduleDefinition, registries, "",
false);

String body = controller.swaggerUi().getBody();

assertThat(body, containsString("No idl found"));
}

@Test public void swaggerUi_oneRegistry() {
@Test public void swaggerUi_oneRegistry_rootPath() {

when(registry1.getIdl()).thenReturn("/module1/api.yaml");
when(registry1.getRequestorPath()).thenReturn("module1");

List<SwaggerRegistry> registries = Arrays.asList(registry1);

SwaggerController controller;
controller = new SwaggerController(moduleDefinition, registries, false);
controller = new SwaggerController(moduleDefinition, registries, "",
false);

String body = controller.swaggerUi().getBody();

assertThat(body, containsString("/module1/api.yaml"));
assertThat(body, containsString("\"module1\""));
assertThat(body, containsString("../../webjars"));
}

@Test public void swaggerUi_oneRegistry_contextPath() {

when(registry1.getIdl()).thenReturn("/module1/api.yaml");
when(registry1.getRequestorPath()).thenReturn("module1");

List<SwaggerRegistry> registries = Arrays.asList(registry1);

SwaggerController controller;
controller = new SwaggerController(moduleDefinition, registries, "context",
false);

String body = controller.swaggerUi().getBody();

assertThat(body, containsString("/module1/api.yaml"));
assertThat(body, containsString("\"module1\""));
assertThat(body, containsString("../../../webjars"));
}

@Test public void swaggerUi_twoRegistries() {
Expand All @@ -88,7 +110,8 @@ public class SwaggerControllerTest {
List<SwaggerRegistry> registries = Arrays.asList(registry1, registry2);

SwaggerController controller;
controller = new SwaggerController(moduleDefinition, registries, false);
controller = new SwaggerController(moduleDefinition, registries, "",
false);

String body = controller.swaggerUi().getBody();

Expand All @@ -108,7 +131,8 @@ public class SwaggerControllerTest {
List<SwaggerRegistry> registries = Arrays.asList(registry1);

SwaggerController controller;
controller = new SwaggerController(moduleDefinition, registries, true);
controller = new SwaggerController(moduleDefinition, registries, "",
true);

String body = controller.swaggerUi().getBody();

Expand All @@ -124,7 +148,8 @@ public class SwaggerControllerTest {
List<SwaggerRegistry> registries = Arrays.asList(registry1);

SwaggerController controller;
controller = new SwaggerController(moduleDefinition, registries, true);
controller = new SwaggerController(moduleDefinition, registries, "",
true);

String body = controller.swaggerUi().getBody();

Expand Down