NIFI-5258 - Changed the way the servlets are created for the documentation webapp.

Removed some unnecessary code.
Fixed imports.

This closes #2812.

Signed-off-by: Andy LoPresto <alopresto@apache.org>
This commit is contained in:
thenatog 2018-06-25 13:23:28 -04:00 committed by Andy LoPresto
parent 9af61d3a2d
commit a274918dc5
No known key found for this signature in database
GPG Key ID: 6EC293152D90B61D
1 changed files with 30 additions and 27 deletions

View File

@ -49,15 +49,12 @@ import org.eclipse.jetty.server.SecureRequestCustomizer;
import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector; import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.SslConnectionFactory; import org.eclipse.jetty.server.SslConnectionFactory;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.server.handler.ContextHandlerCollection;
import org.eclipse.jetty.server.handler.HandlerCollection; import org.eclipse.jetty.server.handler.HandlerCollection;
import org.eclipse.jetty.server.handler.HandlerList; import org.eclipse.jetty.server.handler.HandlerList;
import org.eclipse.jetty.server.handler.ResourceHandler;
import org.eclipse.jetty.server.handler.gzip.GzipHandler; import org.eclipse.jetty.server.handler.gzip.GzipHandler;
import org.eclipse.jetty.servlet.DefaultServlet;
import org.eclipse.jetty.servlet.FilterHolder; import org.eclipse.jetty.servlet.FilterHolder;
import org.eclipse.jetty.util.resource.Resource; import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.util.resource.ResourceCollection;
import org.eclipse.jetty.util.ssl.SslContextFactory; import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.eclipse.jetty.util.thread.QueuedThreadPool; import org.eclipse.jetty.util.thread.QueuedThreadPool;
import org.eclipse.jetty.webapp.Configuration; import org.eclipse.jetty.webapp.Configuration;
@ -337,11 +334,10 @@ public class JettyServer implements NiFiServer {
// load the documentation war // load the documentation war
webDocsContext = loadWar(webDocsWar, docsContextPath, frameworkClassLoader); webDocsContext = loadWar(webDocsWar, docsContextPath, frameworkClassLoader);
// overlay the actual documentation // add the servlets which serve the HTML documentation within the documentation web app
final ContextHandlerCollection documentationHandlers = new ContextHandlerCollection(); addDocsServlets(webDocsContext);
documentationHandlers.addHandler(createDocsWebApp(docsContextPath));
documentationHandlers.addHandler(webDocsContext); handlers.addHandler(webDocsContext);
handlers.addHandler(documentationHandlers);
// load the web error app // load the web error app
final WebAppContext webErrorContext = loadWar(webErrorWar, "/", frameworkClassLoader); final WebAppContext webErrorContext = loadWar(webErrorWar, "/", frameworkClassLoader);
@ -517,39 +513,46 @@ public class JettyServer implements NiFiServer {
return webappContext; return webappContext;
} }
private ContextHandler createDocsWebApp(final String contextPath) { private void addDocsServlets(WebAppContext docsContext) {
try { try {
final ResourceHandler resourceHandler = new ResourceHandler(); // Load the nifi/docs directory
resourceHandler.setDirectoriesListed(false);
final File docsDir = getDocsDir("docs"); final File docsDir = getDocsDir("docs");
final Resource docsResource = Resource.newResource(docsDir);
// load the component documentation working directory // load the component documentation working directory
final File componentDocsDirPath = props.getComponentDocumentationWorkingDirectory(); final File componentDocsDirPath = props.getComponentDocumentationWorkingDirectory();
final File workingDocsDirectory = getWorkingDocsDirectory(componentDocsDirPath); final File workingDocsDirectory = getWorkingDocsDirectory(componentDocsDirPath);
final Resource workingDocsResource = Resource.newResource(workingDocsDirectory);
// Load the API docs
final File webApiDocsDir = getWebApiDocsDir(); final File webApiDocsDir = getWebApiDocsDir();
final Resource webApiDocsResource = Resource.newResource(webApiDocsDir);
// create resources for both docs locations // Create the servlet which will serve the static resources
final ResourceCollection resources = new ResourceCollection(docsResource, workingDocsResource, webApiDocsResource); ServletHolder defaultHolder = new ServletHolder("default", DefaultServlet.class);
resourceHandler.setBaseResource(resources); defaultHolder.setInitParameter("dirAllowed", "false");
// create the context handler ServletHolder docs = new ServletHolder("docs", DefaultServlet.class);
final ContextHandler handler = new ContextHandler(contextPath); docs.setInitParameter("resourceBase", docsDir.getPath());
handler.setHandler(resourceHandler);
ServletHolder components = new ServletHolder("components", DefaultServlet.class);
components.setInitParameter("resourceBase", workingDocsDirectory.getPath());
ServletHolder restApi = new ServletHolder("rest-api", DefaultServlet.class);
restApi.setInitParameter("resourceBase", webApiDocsDir.getPath());
docsContext.addServlet(docs, "/html/*");
docsContext.addServlet(components, "/components/*");
docsContext.addServlet(restApi, "/rest-api/*");
docsContext.addServlet(defaultHolder, "/");
logger.info("Loading documents web app with context path set to " + docsContext.getContextPath());
logger.info("Loading documents web app with context path set to " + contextPath);
return handler;
} catch (Exception ex) { } catch (Exception ex) {
logger.error("Unhandled Exception in createDocsWebApp: " + ex.getMessage()); logger.error("Unhandled Exception in createDocsWebApp: " + ex.getMessage());
startUpFailure(ex); startUpFailure(ex);
return null; // required by compiler, though never be executed.
} }
} }
/** /**
* Returns a File object for the directory containing NIFI documentation. * Returns a File object for the directory containing NIFI documentation.
* <p> * <p>
@ -1052,4 +1055,4 @@ public class JettyServer implements NiFiServer {
@FunctionalInterface @FunctionalInterface
interface ServerConnectorCreator<Server, HttpConfiguration, ServerConnector> { interface ServerConnectorCreator<Server, HttpConfiguration, ServerConnector> {
ServerConnector create(Server server, HttpConfiguration httpConfiguration); ServerConnector create(Server server, HttpConfiguration httpConfiguration);
} }