Java Code Examples for org.eclipse.jetty.webapp.WebAppContext#setDefaultsDescriptor()
The following examples show how to use
org.eclipse.jetty.webapp.WebAppContext#setDefaultsDescriptor() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Jetty9Server.java From gocd with Apache License 2.0 | 6 votes |
private WebAppContext createWebAppContext() { webAppContext = new WebAppContext(); webAppContext.setDefaultsDescriptor(GoWebXmlConfiguration.configuration(getWarFile())); webAppContext.setConfigurationClasses(new String[]{ WebInfConfiguration.class.getCanonicalName(), WebXmlConfiguration.class.getCanonicalName(), JettyWebXmlConfiguration.class.getCanonicalName() }); webAppContext.setContextPath(systemEnvironment.getWebappContextPath()); // delegate all logging to parent classloader to avoid initialization of loggers in multiple classloaders webAppContext.getSystemClasspathPattern().add("org.apache.log4j."); webAppContext.getSystemClasspathPattern().add("org.slf4j."); webAppContext.getSystemClasspathPattern().add("org.apache.commons.logging."); webAppContext.setWar(getWarFile()); webAppContext.setParentLoaderPriority(systemEnvironment.getParentLoaderPriority()); return webAppContext; }
Example 2
Source File: JettyCustomServer.java From nano-framework with Apache License 2.0 | 6 votes |
private void applyHandle(final String contextPath, final String warPath) { final ContextHandlerCollection handler = new ContextHandlerCollection(); final WebAppContext webapp = new WebAppContext(); webapp.setContextPath(contextPath); webapp.setDefaultsDescriptor(WEB_DEFAULT); if (StringUtils.isEmpty(warPath)) { webapp.setResourceBase(DEFAULT_RESOURCE_BASE); webapp.setDescriptor(DEFAULT_WEB_XML_PATH); } else { webapp.setWar(warPath); } applySessionHandler(webapp); handler.addHandler(webapp); super.setHandler(handler); }
Example 3
Source File: JettyFactory.java From java-tutorial with Creative Commons Attribution Share Alike 4.0 International | 6 votes |
public static void initWebAppContext(Server server, int type) { System.out.println("[INFO] Application loading"); WebAppContext webAppContext = (WebAppContext) server.getHandler(); webAppContext.setContextPath(CONTEXT); webAppContext.setResourceBase(getAbsolutePath() + RESOURCE_BASE_PATH); webAppContext.setDescriptor(getAbsolutePath() + RESOURCE_BASE_PATH + WEB_XML_PATH); if (IDE_INTELLIJ == type) { webAppContext.setDefaultsDescriptor(WINDOWS_WEBDEFAULT_PATH); supportJspAndSetTldJarNames(server, TLD_JAR_NAMES); } else { webAppContext.setParentLoaderPriority(true); } System.out.println("[INFO] Application loaded"); }
Example 4
Source File: JettyFactory.java From java-tutorial with Creative Commons Attribution Share Alike 4.0 International | 6 votes |
public static void initWebAppContext(Server server, int type) throws Exception { System.out.println("[INFO] Application loading"); WebAppContext webAppContext = (WebAppContext) server.getHandler(); webAppContext.setContextPath(CONTEXT); webAppContext.setResourceBase(getAbsolutePath() + RESOURCE_BASE_PATH); webAppContext.setDescriptor(getAbsolutePath() + RESOURCE_BASE_PATH + WEB_XML_PATH); if (IDE_INTELLIJ == type) { webAppContext.setDefaultsDescriptor(WINDOWS_WEBDEFAULT_PATH); supportJspAndSetTldJarNames(server, TLD_JAR_NAMES); } else { webAppContext.setParentLoaderPriority(true); } System.out.println("[INFO] Application loaded"); }
Example 5
Source File: JettyFactory.java From java-tutorial with Creative Commons Attribution Share Alike 4.0 International | 6 votes |
public static void initWebAppContext(Server server, int type) { System.out.println("[INFO] Application loading"); WebAppContext webAppContext = (WebAppContext) server.getHandler(); webAppContext.setContextPath(CONTEXT); webAppContext.setResourceBase(getAbsolutePath() + RESOURCE_BASE_PATH); webAppContext.setDescriptor(getAbsolutePath() + RESOURCE_BASE_PATH + WEB_XML_PATH); if (IDE_INTELLIJ == type) { webAppContext.setDefaultsDescriptor(WINDOWS_WEBDEFAULT_PATH); supportJspAndSetTldJarNames(server, TLD_JAR_NAMES); } else { webAppContext.setParentLoaderPriority(true); } System.out.println("[INFO] Application loaded"); }
Example 6
Source File: JettyFactory.java From java-tutorial with Creative Commons Attribution Share Alike 4.0 International | 6 votes |
public static void initWebAppContext(Server server, int type) throws Exception { System.out.println("[INFO] Application loading"); WebAppContext webAppContext = (WebAppContext) server.getHandler(); webAppContext.setContextPath(CONTEXT); webAppContext.setResourceBase(getAbsolutePath() + RESOURCE_BASE_PATH); webAppContext.setDescriptor(getAbsolutePath() + RESOURCE_BASE_PATH + WEB_XML_PATH); if (IDE_INTELLIJ == type) { webAppContext.setDefaultsDescriptor(WINDOWS_WEBDEFAULT_PATH); supportJspAndSetTldJarNames(server, TLD_JAR_NAMES); } else { webAppContext.setParentLoaderPriority(true); } System.out.println("[INFO] Application loaded"); }
Example 7
Source File: JettyFactory.java From java-tutorial with Creative Commons Attribution Share Alike 4.0 International | 6 votes |
public static void initWebAppContext(Server server, int type) throws Exception { System.out.println("[INFO] Application loading"); WebAppContext webAppContext = (WebAppContext) server.getHandler(); webAppContext.setContextPath(CONTEXT); webAppContext.setResourceBase(getAbsolutePath() + RESOURCE_BASE_PATH); webAppContext.setDescriptor(getAbsolutePath() + RESOURCE_BASE_PATH + WEB_XML_PATH); if (IDE_INTELLIJ == type) { webAppContext.setDefaultsDescriptor(WINDOWS_WEBDEFAULT_PATH); supportJspAndSetTldJarNames(server, TLD_JAR_NAMES); } else { webAppContext.setParentLoaderPriority(true); } System.out.println("[INFO] Application loaded"); }
Example 8
Source File: JettyFactory.java From java-tutorial with Creative Commons Attribution Share Alike 4.0 International | 6 votes |
public static void initWebAppContext(Server server, int type) throws Exception { System.out.println("[INFO] Application loading"); WebAppContext webAppContext = (WebAppContext) server.getHandler(); webAppContext.setContextPath(CONTEXT); webAppContext.setResourceBase(getAbsolutePath() + RESOURCE_BASE_PATH); webAppContext.setDescriptor(getAbsolutePath() + RESOURCE_BASE_PATH + WEB_XML_PATH); if (IDE_INTELLIJ == type) { webAppContext.setDefaultsDescriptor(WINDOWS_WEBDEFAULT_PATH); supportJspAndSetTldJarNames(server, TLD_JAR_NAMES); } else { webAppContext.setParentLoaderPriority(true); } System.out.println("[INFO] Application loaded"); }
Example 9
Source File: JettyFactory.java From java-tutorial with Creative Commons Attribution Share Alike 4.0 International | 6 votes |
public static void initWebAppContext(Server server, int type) throws Exception { System.out.println("[INFO] Application loading"); WebAppContext webAppContext = (WebAppContext) server.getHandler(); webAppContext.setContextPath(CONTEXT); webAppContext.setResourceBase(getAbsolutePath() + RESOURCE_BASE_PATH); webAppContext.setDescriptor(getAbsolutePath() + RESOURCE_BASE_PATH + WEB_XML_PATH); if (IDE_INTELLIJ == type) { webAppContext.setDefaultsDescriptor(WINDOWS_WEBDEFAULT_PATH); supportJspAndSetTldJarNames(server, TLD_JAR_NAMES); } else { webAppContext.setParentLoaderPriority(true); } System.out.println("[INFO] Application loaded"); }
Example 10
Source File: HttpServer2.java From hadoop-ozone with Apache License 2.0 | 5 votes |
private static WebAppContext createWebAppContext(Builder b, AccessControlList adminsAcl, final String appDir) { WebAppContext ctx = new WebAppContext(); ctx.setDefaultsDescriptor(null); ServletHolder holder = new ServletHolder(new DefaultServlet()); Map<String, String> params = ImmutableMap.<String, String>builder() .put("acceptRanges", "true") .put("dirAllowed", "false") .put("gzip", "true") .put("useFileMappedBuffer", "true") .build(); holder.setInitParameters(params); ctx.setWelcomeFiles(new String[] {"index.html"}); ctx.addServlet(holder, "/"); ctx.setDisplayName(b.name); ctx.setContextPath("/"); ctx.setWar(appDir + "/" + b.name); String tempDirectory = b.conf.get(HTTP_TEMP_DIR_KEY); if (tempDirectory != null && !tempDirectory.isEmpty()) { ctx.setTempDirectory(new File(tempDirectory)); ctx.setAttribute("javax.servlet.context.tempdir", tempDirectory); } ctx.getServletContext().setAttribute(CONF_CONTEXT_ATTRIBUTE, b.conf); ctx.getServletContext().setAttribute(ADMINS_ACL, adminsAcl); addNoCacheFilter(ctx); return ctx; }
Example 11
Source File: HttpServer2.java From knox with Apache License 2.0 | 5 votes |
private static WebAppContext createWebAppContext(Builder b, AccessControlList adminsAcl, final String appDir) { WebAppContext ctx = new WebAppContext(); ctx.setDefaultsDescriptor(null); ServletHolder holder = new ServletHolder(new DefaultServlet()); Map<String, String> params = ImmutableMap. <String, String> builder() .put("acceptRanges", "true") .put("dirAllowed", "false") .put("gzip", "true") .put("useFileMappedBuffer", "true") .build(); holder.setInitParameters(params); ctx.setWelcomeFiles(new String[] {"index.html"}); ctx.addServlet(holder, "/"); ctx.setDisplayName(b.name); ctx.setContextPath("/"); ctx.setWar(appDir + "/" + b.name); String tempDirectory = b.conf.get(HTTP_TEMP_DIR_KEY); if (tempDirectory != null && !tempDirectory.isEmpty()) { ctx.setTempDirectory(new File(tempDirectory)); ctx.setAttribute("javax.servlet.context.tempdir", tempDirectory); } ctx.getServletContext().setAttribute(CONF_CONTEXT_ATTRIBUTE, b.conf); ctx.getServletContext().setAttribute(ADMINS_ACL, adminsAcl); addNoCacheFilter(ctx); return ctx; }
Example 12
Source File: JettyServer.java From nifi-minifi with Apache License 2.0 | 5 votes |
private static WebAppContext loadWar(final File warFile, final String contextPath, final ClassLoader parentClassLoader) throws IOException { final WebAppContext webappContext = new WebAppContext(warFile.getPath(), contextPath); webappContext.setContextPath(contextPath); webappContext.setDisplayName(contextPath); // instruction jetty to examine these jars for tlds, web-fragments, etc webappContext.setAttribute("org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern", ".*/[^/]*servlet-api-[^/]*\\.jar$|.*/javax.servlet.jsp.jstl-.*\\\\.jar$|.*/[^/]*taglibs.*\\.jar$" ); // remove slf4j server class to allow WAR files to have slf4j dependencies in WEB-INF/lib List<String> serverClasses = new ArrayList<>(Arrays.asList(webappContext.getServerClasses())); serverClasses.remove("org.slf4j."); webappContext.setServerClasses(serverClasses.toArray(new String[0])); webappContext.setDefaultsDescriptor(WEB_DEFAULTS_XML); // get the temp directory for this webapp File tempDir = Paths.get(C2_SERVER_HOME, "tmp", warFile.getName()).toFile(); if (tempDir.exists() && !tempDir.isDirectory()) { throw new RuntimeException(tempDir.getAbsolutePath() + " is not a directory"); } else if (!tempDir.exists()) { final boolean made = tempDir.mkdirs(); if (!made) { throw new RuntimeException(tempDir.getAbsolutePath() + " could not be created"); } } if (!(tempDir.canRead() && tempDir.canWrite())) { throw new RuntimeException(tempDir.getAbsolutePath() + " directory does not have read/write privilege"); } // configure the temp dir webappContext.setTempDirectory(tempDir); // configure the max form size (3x the default) webappContext.setMaxFormContentSize(600000); webappContext.setClassLoader(new WebAppClassLoader(parentClassLoader, webappContext)); logger.info("Loading WAR: " + warFile.getAbsolutePath() + " with context path set to " + contextPath); return webappContext; }
Example 13
Source File: HttpServer2.java From lucene-solr with Apache License 2.0 | 5 votes |
private static WebAppContext createWebAppContext(Builder b, AccessControlList adminsAcl, final String appDir) { WebAppContext ctx = new WebAppContext(); ctx.setDefaultsDescriptor(null); ServletHolder holder = new ServletHolder(new DefaultServlet()); Map<String, String> params = ImmutableMap. <String, String> builder() .put("acceptRanges", "true") .put("dirAllowed", "false") .put("gzip", "true") .put("useFileMappedBuffer", "true") .build(); holder.setInitParameters(params); ctx.setWelcomeFiles(new String[] {"index.html"}); ctx.addServlet(holder, "/"); ctx.setDisplayName(b.name); ctx.setContextPath("/"); ctx.setWar(appDir + "/" + b.name); String tempDirectory = b.conf.get(HTTP_TEMP_DIR_KEY); if (tempDirectory != null && !tempDirectory.isEmpty()) { ctx.setTempDirectory(new File(tempDirectory)); ctx.setAttribute("javax.servlet.context.tempdir", tempDirectory); } ctx.getServletContext().setAttribute(CONF_CONTEXT_ATTRIBUTE, b.conf); ctx.getServletContext().setAttribute(ADMINS_ACL, adminsAcl); addNoCacheFilter(ctx); return ctx; }
Example 14
Source File: QAServer.java From Shour with MIT License | 5 votes |
private WebAppContext buildContext() { WebAppContext context = new WebAppContext(); context.setDefaultsDescriptor("webapp/WEB-INF/webdefault.xml"); context.setDescriptor("webapp/WEB-INF/web.xml"); context.setResourceBase("webapp/"); context.setContextPath("/"); return context; }
Example 15
Source File: HttpServer2.java From knox with Apache License 2.0 | 5 votes |
private static WebAppContext createWebAppContext(Builder b, AccessControlList adminsAcl, final String appDir) { WebAppContext ctx = new WebAppContext(); ctx.setDefaultsDescriptor(null); ServletHolder holder = new ServletHolder(new DefaultServlet()); Map<String, String> params = ImmutableMap. <String, String> builder() .put("acceptRanges", "true") .put("dirAllowed", "false") .put("gzip", "true") .put("useFileMappedBuffer", "true") .build(); holder.setInitParameters(params); ctx.setWelcomeFiles(new String[] {"index.html"}); ctx.addServlet(holder, "/"); ctx.setDisplayName(b.name); ctx.setContextPath("/"); ctx.setWar(appDir + "/" + b.name); String tempDirectory = b.conf.get(HTTP_TEMP_DIR_KEY); if (tempDirectory != null && !tempDirectory.isEmpty()) { ctx.setTempDirectory(new File(tempDirectory)); ctx.setAttribute("javax.servlet.context.tempdir", tempDirectory); } ctx.getServletContext().setAttribute(CONF_CONTEXT_ATTRIBUTE, b.conf); ctx.getServletContext().setAttribute(ADMINS_ACL, adminsAcl); addNoCacheFilter(ctx); return ctx; }
Example 16
Source File: JettyServer.java From nifi with Apache License 2.0 | 4 votes |
private WebAppContext loadWar(final File warFile, final String contextPath, final ClassLoader parentClassLoader) { final WebAppContext webappContext = new WebAppContext(warFile.getPath(), contextPath); webappContext.setContextPath(contextPath); webappContext.setDisplayName(contextPath); // instruction jetty to examine these jars for tlds, web-fragments, etc webappContext.setAttribute(CONTAINER_INCLUDE_PATTERN_KEY, CONTAINER_INCLUDE_PATTERN_VALUE); // remove slf4j server class to allow WAR files to have slf4j dependencies in WEB-INF/lib List<String> serverClasses = new ArrayList<>(Arrays.asList(webappContext.getServerClasses())); serverClasses.remove("org.slf4j."); webappContext.setServerClasses(serverClasses.toArray(new String[0])); webappContext.setDefaultsDescriptor(WEB_DEFAULTS_XML); webappContext.getMimeTypes().addMimeMapping("ttf", "font/ttf"); // get the temp directory for this webapp File tempDir = new File(props.getWebWorkingDirectory(), warFile.getName()); if (tempDir.exists() && !tempDir.isDirectory()) { throw new RuntimeException(tempDir.getAbsolutePath() + " is not a directory"); } else if (!tempDir.exists()) { final boolean made = tempDir.mkdirs(); if (!made) { throw new RuntimeException(tempDir.getAbsolutePath() + " could not be created"); } } if (!(tempDir.canRead() && tempDir.canWrite())) { throw new RuntimeException(tempDir.getAbsolutePath() + " directory does not have read/write privilege"); } // configure the temp dir webappContext.setTempDirectory(tempDir); // configure the max form size (3x the default) webappContext.setMaxFormContentSize(600000); // add HTTP security headers to all responses final String ALL_PATHS = "/*"; ArrayList<Class<? extends Filter>> filters = new ArrayList<>(Arrays.asList( XFrameOptionsFilter.class, ContentSecurityPolicyFilter.class, XSSProtectionFilter.class, XContentTypeOptionsFilter.class)); if(props.isHTTPSConfigured()) { filters.add(StrictTransportSecurityFilter.class); } filters.forEach( (filter) -> addFilters(filter, ALL_PATHS, webappContext)); addFiltersWithProps(ALL_PATHS, webappContext); try { // configure the class loader - webappClassLoader -> jetty nar -> web app's nar -> ... webappContext.setClassLoader(new WebAppClassLoader(parentClassLoader, webappContext)); } catch (final IOException ioe) { startUpFailure(ioe); } logger.info("Loading WAR: " + warFile.getAbsolutePath() + " with context path set to " + contextPath); return webappContext; }
Example 17
Source File: DevServer.java From java-tutorial with Creative Commons Attribution Share Alike 4.0 International | 4 votes |
public static void main(String[] args) { // 初始化 WebAppContext System.out.println("[INFO] Application loading"); WebAppContext webAppContext = new WebAppContext(); webAppContext.setContextPath(CONTEXT); webAppContext.setResourceBase(getAbsolutePath() + RESOURCE_BASE_PATH); webAppContext.setDescriptor(getAbsolutePath() + RESOURCE_BASE_PATH + WEB_XML_PATH); webAppContext.setDefaultsDescriptor(WINDOWS_WEBDEFAULT_PATH); // 初始化 server Server server = new Server(PORT); server.setHandler(webAppContext); supportJspAndSetTldJarNames(server, TLD_JAR_NAMES); System.out.println("[INFO] Application loaded"); try { // Initialize javax.websocket layer ServerContainer wscontainer = WebSocketServerContainerInitializer.configureContext(webAppContext); // Add WebSocket endpoint to javax.websocket layer wscontainer.addEndpoint(WebSocketServer.class); System.out.println("[HINT] Don't forget to set -XX:MaxPermSize=128m"); System.out.println("Server running at http://localhost:" + PORT + CONTEXT); System.out.println("[HINT] Hit Enter to reload the application quickly"); server.start(); // server.join(); // 等待用户输入回车重载应用 while (true) { char c = (char) System.in.read(); if (c == '\n') { DevServer.reloadWebAppContext(server); } } } catch (Exception e) { e.printStackTrace(); System.exit(-1); } }
Example 18
Source File: JettyServer.java From nifi-registry with Apache License 2.0 | 4 votes |
private WebAppContext loadWar(final File warFile, final String contextPath, final URL[] additionalResources) throws IOException { final WebAppContext webappContext = new WebAppContext(warFile.getPath(), contextPath); webappContext.setContextPath(contextPath); webappContext.setDisplayName(contextPath); // remove slf4j server class to allow WAR files to have slf4j dependencies in WEB-INF/lib List<String> serverClasses = new ArrayList<>(Arrays.asList(webappContext.getServerClasses())); serverClasses.remove("org.slf4j."); webappContext.setServerClasses(serverClasses.toArray(new String[0])); webappContext.setDefaultsDescriptor(WEB_DEFAULTS_XML); // get the temp directory for this webapp final File webWorkingDirectory = properties.getWebWorkingDirectory(); final File tempDir = new File(webWorkingDirectory, warFile.getName()); if (tempDir.exists() && !tempDir.isDirectory()) { throw new RuntimeException(tempDir.getAbsolutePath() + " is not a directory"); } else if (!tempDir.exists()) { final boolean made = tempDir.mkdirs(); if (!made) { throw new RuntimeException(tempDir.getAbsolutePath() + " could not be created"); } } if (!(tempDir.canRead() && tempDir.canWrite())) { throw new RuntimeException(tempDir.getAbsolutePath() + " directory does not have read/write privilege"); } // configure the temp dir webappContext.setTempDirectory(tempDir); // configure the max form size (3x the default) webappContext.setMaxFormContentSize(600000); // add HTTP security headers to all responses final String ALL_PATHS = "/*"; ArrayList<Class<? extends Filter>> filters = new ArrayList<>(Arrays.asList(XFrameOptionsFilter.class, ContentSecurityPolicyFilter.class, XSSProtectionFilter.class)); if(properties.isHTTPSConfigured()) { filters.add(StrictTransportSecurityFilter.class); } filters.forEach( (filter) -> addFilters(filter, ALL_PATHS, webappContext)); // start out assuming the system ClassLoader will be the parent, but if additional resources were specified then // inject a new ClassLoader in between the system and webapp ClassLoaders that contains the additional resources ClassLoader parentClassLoader = ClassLoader.getSystemClassLoader(); if (additionalResources != null && additionalResources.length > 0) { URLClassLoader additionalClassLoader = new URLClassLoader(additionalResources, ClassLoader.getSystemClassLoader()); parentClassLoader = additionalClassLoader; } webappContext.setClassLoader(new WebAppClassLoader(parentClassLoader, webappContext)); logger.info("Loading WAR: " + warFile.getAbsolutePath() + " with context path set to " + contextPath); return webappContext; }
Example 19
Source File: JettyServer.java From localization_nifi with Apache License 2.0 | 4 votes |
private WebAppContext loadWar(final File warFile, final String contextPath, final ClassLoader parentClassLoader) { final WebAppContext webappContext = new WebAppContext(warFile.getPath(), contextPath); webappContext.setContextPath(contextPath); webappContext.setDisplayName(contextPath); // instruction jetty to examine these jars for tlds, web-fragments, etc webappContext.setAttribute("org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern", ".*/[^/]*servlet-api-[^/]*\\.jar$|.*/javax.servlet.jsp.jstl-.*\\\\.jar$|.*/[^/]*taglibs.*\\.jar$" ); // remove slf4j server class to allow WAR files to have slf4j dependencies in WEB-INF/lib List<String> serverClasses = new ArrayList<>(Arrays.asList(webappContext.getServerClasses())); serverClasses.remove("org.slf4j."); webappContext.setServerClasses(serverClasses.toArray(new String[0])); webappContext.setDefaultsDescriptor(WEB_DEFAULTS_XML); // get the temp directory for this webapp File tempDir = new File(props.getWebWorkingDirectory(), warFile.getName()); if (tempDir.exists() && !tempDir.isDirectory()) { throw new RuntimeException(tempDir.getAbsolutePath() + " is not a directory"); } else if (!tempDir.exists()) { final boolean made = tempDir.mkdirs(); if (!made) { throw new RuntimeException(tempDir.getAbsolutePath() + " could not be created"); } } if (!(tempDir.canRead() && tempDir.canWrite())) { throw new RuntimeException(tempDir.getAbsolutePath() + " directory does not have read/write privilege"); } // configure the temp dir webappContext.setTempDirectory(tempDir); // configure the max form size (3x the default) webappContext.setMaxFormContentSize(600000); try { // configure the class loader - webappClassLoader -> jetty nar -> web app's nar -> ... webappContext.setClassLoader(new WebAppClassLoader(parentClassLoader, webappContext)); } catch (final IOException ioe) { startUpFailure(ioe); } logger.info("Loading WAR: " + warFile.getAbsolutePath() + " with context path set to " + contextPath); return webappContext; }
Example 20
Source File: WebServerTestCase.java From htmlunit with Apache License 2.0 | 4 votes |
/** * This is usually needed if you want to have a running server during many tests invocation. * * Creates and starts a web server on the default {@link #PORT}. * The given resourceBase is used to be the ROOT directory that serves the default context. * <p><b>Don't forget to stop the returned Server after the test</b> * * @param port the port to which the server is bound * @param resourceBase the base of resources for the default context * @param classpath additional classpath entries to add (may be null) * @param servlets map of {String, Class} pairs: String is the path spec, while class is the class * @param handler wrapper for handler (can be null) * @return the newly created server * @throws Exception if an error occurs */ public static Server createWebServer(final int port, final String resourceBase, final String[] classpath, final Map<String, Class<? extends Servlet>> servlets, final HandlerWrapper handler) throws Exception { final Server server = buildServer(port); final WebAppContext context = new WebAppContext(); context.setContextPath("/"); context.setResourceBase(resourceBase); if (servlets != null) { for (final Map.Entry<String, Class<? extends Servlet>> entry : servlets.entrySet()) { final String pathSpec = entry.getKey(); final Class<? extends Servlet> servlet = entry.getValue(); context.addServlet(servlet, pathSpec); // disable defaults if someone likes to register his own root servlet if ("/".equals(pathSpec)) { context.setDefaultsDescriptor(null); context.addServlet(DefaultServlet.class, "/favicon.ico"); } } } final WebAppClassLoader loader = new WebAppClassLoader(context); if (classpath != null) { for (final String path : classpath) { loader.addClassPath(path); } } context.setClassLoader(loader); if (handler != null) { handler.setHandler(context); server.setHandler(handler); } else { server.setHandler(context); } tryStart(port, server); return server; }