public static void main( String[] args )throws Exception{ //if there is more than 1 argument the second is the path to use //default is fhis-test String path = "/"+ ((args.length >1)?args[1]:"fhir-test"); System.out.println("path "+path); Server server = new Server(8080); MBeanContainer mbContainer = new MBeanContainer( ManagementFactory.getPlatformMBeanServer()); server.addBean(mbContainer); server.setAttribute("org.eclipse.jetty.server.Request.maxFormContentSize", -1); WebAppContext webapp = new WebAppContext(); webapp.setContextPath(path); //first argument is which war file to use File warFile = new File(args[0]); webapp.setWar(warFile.getAbsolutePath()); webapp.addAliasCheck(new AllowSymLinkAliasChecker()); server.setHandler(webapp); server.start(); }
/** * Initialization of the context path for the web application "/console" occurs in this method * and the handler for the web application is set. This only occurs once. * @return HttpServer: the singleton instance of this class * @throws IOException */ public static HttpServer getInstance() throws Exception { if (!HttpServerHolder.INITIALIZED) { HttpServerHolder.WEBAPP.setContextPath("/console"); ServletContextHandler contextJobs = new ServletContextHandler(ServletContextHandler.SESSIONS); contextJobs.setContextPath("/jobs"); ServletContextHandler contextMetrics = new ServletContextHandler(ServletContextHandler.SESSIONS); contextMetrics.setContextPath("/metrics"); ServerUtil sUtil = new ServerUtil(); String commandWarFilePath = sUtil.getAbsoluteWarFilePath("console.war"); if (commandWarFilePath.equals("")){ // check if we are on Eclipse, if Eclipse can't find it, it probably does not exist // running on Eclipse, look for the eclipse war file path ProtectionDomain protectionDomain = HttpServer.class.getProtectionDomain(); String eclipseWarFilePath = sUtil.getEclipseWarFilePath(protectionDomain, "console.war"); if (!eclipseWarFilePath.equals("")) { HttpServerHolder.WEBAPP.setWar(eclipseWarFilePath); } else { throw new Exception(HttpServerHolder.consoleWarNotFoundMessage); } } else { HttpServerHolder.WEBAPP.setWar(commandWarFilePath); } HttpServerHolder.WEBAPP.addAliasCheck(new AllowSymLinkAliasChecker()); ContextHandlerCollection contexts = new ContextHandlerCollection(); contexts.setHandlers(new Handler[] { contextJobs, contextMetrics, HttpServerHolder.WEBAPP }); HttpServerHolder.JETTYSERVER.setHandler(contexts); HttpServerHolder.INITIALIZED = true; } return HttpServerHolder.INSTANCE; }
public static void runWebServer(String[] args) throws Exception { disableJettyLogging(); new File(System.getProperty("java.io.tmpdir")).mkdirs(); System.out.println(IOUtils.toString(StartWebServer.class.getResource("/Metl.asciiart"))); Server server = new Server(); Connector[] connectors = getConnectors(args, server); server.setConnectors(connectors); ClassList classlist = Configuration.ClassList.setServerDefault(server); classlist.addBefore("org.eclipse.jetty.webapp.JettyWebXmlConfiguration", "org.eclipse.jetty.annotations.AnnotationConfiguration"); MBeanContainer mbContainer = new MBeanContainer(ManagementFactory.getPlatformMBeanServer()); server.addBean(mbContainer); ProtectionDomain protectionDomain = StartWebServer.class.getProtectionDomain(); URL location = protectionDomain.getCodeSource().getLocation(); String allowDirListing = System.getProperty(SERVER_ALLOW_DIR_LISTING, "false"); String allowedMethods = System.getProperty(SERVER_ALLOW_HTTP_METHODS, ""); String disallowedMethods = System.getProperty(SERVER_DISALLOW_HTTP_METHODS, "OPTIONS"); WebAppContext webapp = new WebAppContext(); webapp.setContextPath("/metl"); webapp.setWar(location.toExternalForm()); webapp.addAliasCheck(new AllowSymLinkAliasChecker()); webapp.setInitParameter("org.eclipse.jetty.servlet.Default.dirAllowed", allowDirListing); FilterHolder filterHolder = new FilterHolder(HttpMethodFilter.class); filterHolder.setInitParameter("server.allow.http.methods", allowedMethods); filterHolder.setInitParameter("server.disallow.http.methods", disallowedMethods); webapp.addFilter(filterHolder, "/*", EnumSet.of(DispatcherType.REQUEST)); String extraClasspath = getPluginClasspath(new File(Wrapper.getConfigDir(null, false))); webapp.setExtraClasspath(extraClasspath); if (extraClasspath.length() > 0) { getLogger().info("Adding extra classpath of: " + extraClasspath.toString()); } server.setHandler(webapp); ServerContainer webSocketServer = WebSocketServerContainerInitializer.configureContext(webapp); webSocketServer.setDefaultMaxSessionIdleTimeout(10000000); server.start(); server.join(); }
public void start(String resourceBase, int port, String protocol) throws Exception { if (DirectoryLayout.getInstance().getConfiguration().getBoolean("filebroker", "jetty-debug")) { System.setProperty("org.eclipse.jetty.LEVEL", "DEBUG"); } jettyInstance = new Server(); ServerConnector connector; SslContextFactory contextFactory = null; switch (protocol) { case "http": connector = new ServerConnector(jettyInstance); break; case "https": Configuration configuration = DirectoryLayout.getInstance().getConfiguration(); String[] protocols = configuration.getString("filebroker", "ssl-protocol-version").split(","); contextFactory = KeyAndTrustManager.createSslContextFactory( configuration.getString("security", "filebroker-keystore"), configuration.getString("security", "storepass"), protocols ); connector = new ServerConnector(jettyInstance, contextFactory); break; default: throw new IllegalArgumentException("unsupported protocol: " + protocol + " (supported are http and https)"); } connector.setPort(port); jettyInstance.setConnectors(new Connector[]{ connector }); ServletContextHandler root = new ServletContextHandler(jettyInstance, "/", false, false); // file-root and some public files are symlinks root.addAliasCheck(new AllowSymLinkAliasChecker()); root.setResourceBase(resourceBase); root.addServlet(new ServletHolder(new RestServlet(urlRepository.getRootUrl(), urlRepository, metadataServer, cacheCleanUp)), "/*"); jettyInstance.start(); }