public void run() { Properties jettyProperties = new Properties(); String pFileName = PropertiesUtil.getJettyPropertiesFile(); try { jettyProperties.load(new FileInputStream(pFileName)); } catch (IOException e) { log.error("load properties from {} error.", pFileName); return; } // crate server server = new Server(Integer.parseInt(jettyProperties.getProperty(PropList.HTTP_PORT))); // Create the ResourceHandler. It is the object that will actually handle the request for a // given file. It is // a Jetty Handler object so it is suitable for chaining with other handlers as you will see in // other examples. ResourceHandler resource_handler = new ResourceHandler(); // Configure the ResourceHandler. Setting the resource base indicates where the files should be // served out of. // In this example it is the current directory but it can be configured to anything that the jvm // has access to. resource_handler.setDirectoriesListed(true); resource_handler.setWelcomeFiles(new String[] {"index.html"}); resource_handler.setResourceBase(PropertiesUtil.getWebBaseDir()); log.debug(PropertiesUtil.getWebBaseDir()); // Add a single handler on context "/hello" ContextHandler context = new ContextHandler(); context.setContextPath("/LoadT"); context.setHandler(new LoadTHandler()); // Add the ResourceHandler to the server. GzipHandler gzip = new GzipHandler(); server.setHandler(gzip); // make handler chain HandlerList handlers = new HandlerList(); handlers.setHandlers(new Handler[] {resource_handler, context, new DefaultHandler()}); gzip.setHandler(handlers); try { server.start(); } catch (Exception e) { log.error("embedded jetty server start error.", e); } server.dumpStdErr(); /* try { server.join(); } catch (InterruptedException e) { log.info("jetty server interrupted", e); }*/ }
@BeforeClass public static void startServer() throws Exception { server = new Server(); ServerConnector connector = new ServerConnector(server); connector.setPort(0); server.addConnector(connector); ContextHandlerCollection contexts = new ContextHandlerCollection(); File dir = MavenTestingUtils.getTargetTestingDir(ResourceHandlerRangeTest.class.getSimpleName()); FS.ensureEmpty(dir); File rangeFile = new File(dir, "range.txt"); try (FileWriter writer = new FileWriter(rangeFile)) { writer.append("0123456789"); writer.flush(); } ContextHandler contextHandler = new ContextHandler(); ResourceHandler contentResourceHandler = new ResourceHandler(); contextHandler.setBaseResource(Resource.newResource(dir.getAbsolutePath())); contextHandler.setHandler(contentResourceHandler); contextHandler.setContextPath("/"); contexts.addHandler(contextHandler); server.setHandler(contexts); server.start(); String host = connector.getHost(); if (host == null) { host = "localhost"; } int port = connector.getLocalPort(); serverUri = new URI(String.format("http://%s:%d/", host, port)); }