Show informative page instead of "Forbidden" when user tries to access API documentation when disabled.

This commit is contained in:
catbref 2020-03-30 09:57:17 +01:00
parent 9156325ffc
commit fa1aa1c8b2
2 changed files with 26 additions and 2 deletions

View File

@ -108,8 +108,19 @@ public class ApiService {
swaggerUIServlet.setInitParameter("pathInfoOnly", "true"); swaggerUIServlet.setInitParameter("pathInfoOnly", "true");
context.addServlet(swaggerUIServlet, "/api-documentation/*"); context.addServlet(swaggerUIServlet, "/api-documentation/*");
rewriteHandler.addRule(new RedirectPatternRule("", "/api-documentation/")); // redirect to Swagger UI start page rewriteHandler.addRule(new RedirectPatternRule("", "/api-documentation/")); // redirect empty path to API docs
rewriteHandler.addRule(new RedirectPatternRule("/api-documentation", "/api-documentation/")); // redirect to Swagger UI start page rewriteHandler.addRule(new RedirectPatternRule("/api-documentation", "/api-documentation/")); // redirect to add trailing slash if missing
} else {
// Simple pages that explains that API documentation is disabled
ClassLoader loader = this.getClass().getClassLoader();
ServletHolder swaggerUIServlet = new ServletHolder("api-docs-disabled", DefaultServlet.class);
swaggerUIServlet.setInitParameter("resourceBase", loader.getResource("api-docs-disabled/").toString());
swaggerUIServlet.setInitParameter("dirAllowed", "true");
swaggerUIServlet.setInitParameter("pathInfoOnly", "true");
context.addServlet(swaggerUIServlet, "/api-documentation/*");
rewriteHandler.addRule(new RedirectPatternRule("", "/api-documentation/")); // redirect empty path to API docs
rewriteHandler.addRule(new RedirectPatternRule("/api-documentation", "/api-documentation/")); // redirect to add trailing slash if missing
} }
// Start server // Start server

View File

@ -0,0 +1,13 @@
<!doctype html>
<html>
<head>
<title>API Documentation disabled</title>
</head>
<body>
API documentation pages are currently disabled.
<p>
To enable, add an entry like this to your <tt>settings.json</tt> file:
<p>
<code>"apiDocumentationEnabled": true,</code>
</body>
</html>