public static void main(String[] args) throws Exception { HttpServer s1 = null; HttpsServer s2 = null; ExecutorService executor = null; try { String root = System.getProperty("test.src") + "/docs"; System.out.print("Test12: "); InetSocketAddress addr = new InetSocketAddress(0); s1 = HttpServer.create(addr, 0); s2 = HttpsServer.create(addr, 0); HttpHandler h = new FileServerHandler(root); HttpContext c1 = s1.createContext("/test1", h); HttpContext c2 = s2.createContext("/test1", h); executor = Executors.newCachedThreadPool(); s1.setExecutor(executor); s2.setExecutor(executor); ctx = new SimpleSSLContext(System.getProperty("test.src")).get(); s2.setHttpsConfigurator(new HttpsConfigurator(ctx)); s1.start(); s2.start(); int port = s1.getAddress().getPort(); int httpsport = s2.getAddress().getPort(); Runner r[] = new Runner[8]; r[0] = new Runner(true, "http", root + "/test1", port, "smallfile.txt", 23); r[1] = new Runner(true, "http", root + "/test1", port, "largefile.txt", 2730088); r[2] = new Runner(true, "https", root + "/test1", httpsport, "smallfile.txt", 23); r[3] = new Runner(true, "https", root + "/test1", httpsport, "largefile.txt", 2730088); r[4] = new Runner(false, "http", root + "/test1", port, "smallfile.txt", 23); r[5] = new Runner(false, "http", root + "/test1", port, "largefile.txt", 2730088); r[6] = new Runner(false, "https", root + "/test1", httpsport, "smallfile.txt", 23); r[7] = new Runner(false, "https", root + "/test1", httpsport, "largefile.txt", 2730088); start(r); join(r); System.out.println("OK"); } finally { delay(); if (s1 != null) s1.stop(2); if (s2 != null) s2.stop(2); if (executor != null) executor.shutdown(); } }
public static void main(String[] args) throws Exception { HttpServer s1 = null; HttpsServer s2 = null; ExecutorService executor = null; try { String root = System.getProperty("test.src") + "/docs"; System.out.print("Test1: "); InetSocketAddress addr = new InetSocketAddress(0); s1 = HttpServer.create(addr, 0); if (s1 instanceof HttpsServer) { throw new RuntimeException("should not be httpsserver"); } s2 = HttpsServer.create(addr, 0); HttpHandler h = new FileServerHandler(root); HttpContext c1 = s1.createContext("/test1", h); HttpContext c2 = s2.createContext("/test1", h); executor = Executors.newCachedThreadPool(); s1.setExecutor(executor); s2.setExecutor(executor); ctx = new SimpleSSLContext(System.getProperty("test.src")).get(); s2.setHttpsConfigurator(new HttpsConfigurator(ctx)); s1.start(); s2.start(); int port = s1.getAddress().getPort(); int httpsport = s2.getAddress().getPort(); test(true, "http", root + "/test1", port, "smallfile.txt", 23); test(true, "http", root + "/test1", port, "largefile.txt", 2730088); test(true, "https", root + "/test1", httpsport, "smallfile.txt", 23); test(true, "https", root + "/test1", httpsport, "largefile.txt", 2730088); test(false, "http", root + "/test1", port, "smallfile.txt", 23); test(false, "http", root + "/test1", port, "largefile.txt", 2730088); test(false, "https", root + "/test1", httpsport, "smallfile.txt", 23); test(false, "https", root + "/test1", httpsport, "largefile.txt", 2730088); System.out.println("OK"); } finally { delay(); s1.stop(2); s2.stop(2); executor.shutdown(); } }
/** Process initial method */ public static void main(final String[] args) { final Configuration config = new Configuration(); LOG.trace("Starting server..."); // log server version LOG.trace("Preparing to run " + config.VERSION + "."); LOG.trace("Resuming DB from folder: " + config.DATABASE_FOLDER); // create listener LOG.trace("Creating listener."); final HttpServer http; try { http = HttpServer.create(config.HOST, config.BACKLOG); } catch (IOException e) { LOG.fatal("Could not create listener."); return; } // Process continues even if Https Listener failed to initialize HttpsServer https = null; if (config.HOST_HTTPS != null) { try { https = createHttps(config); } catch (IOException | KeyStoreException | NoSuchAlgorithmException | CertificateException | UnrecoverableKeyException | KeyManagementException e) { LOG.error("Could not create HTTPS listener.", e); } } // Single Threaded Executor final Executor exec = new Executor() { @Override public void execute(Runnable task) { task.run(); } }; http.setExecutor(exec); if (https != null) { https.setExecutor(exec); } LOG.trace("Starting virtual file system."); BufferedWriter accessLog = null; try { // start access log try { final FileWriter file = new FileWriter(config.ACCESS_LOG, true); accessLog = new BufferedWriter(file); } catch (IOException e) { System.out.println("Access log redirected to console."); } LOG.trace("Appending to access log."); // RequestHandler /* * final BasicAuthenticator ba = new * BasicAuthenticator("Statistics") { * * @Override public boolean checkCredentials(final String user, * final String pass) { * * LOG.info("login: [" + user + "] | [" + pass + "]"); * * LOG.info("required: [" + config.STATISTICS_USERNAME + "] | [" + * config.STATISTICS_PASSWORD + "]"); * * return user.equals(config.STATISTICS_USERNAME) && * pass.equals(config.STATISTICS_PASSWORD.trim()); } }; */ final List<Class<?>> pages = new ArrayList<>(); // scan packages PageClassLoader.load(config.PAGES_PACKAGES, pages); final List<Object> pageObjs = new ArrayList<>(); // instantiate objects and inject dependencies PageClassLoader.injectDependencies(pages, pageObjs); // create contexts final HttpHandler enforcer = new HttpsEnforcer(config); final boolean usingHttps = https != null && !"no".equals(config.HTTPS_ENABLED); for (Class<?> pageClass : pages) { LOG.info("Creating context for: " + pageClass.getName()); final Page page = pageClass.getAnnotation(Page.class); if (page == null) { LOG.info("Missing required Annotations. Skipping"); continue; } final HttpHandler handler; try { handler = (HttpHandler) pageClass.newInstance(); } catch (InstantiationException | IllegalAccessException e) { LOG.info("Class is not a handler."); continue; } if (usingHttps) { https.createContext(page.path(), handler); http.createContext(page.path(), page.requireLogin() ? enforcer : handler); } else { https.createContext(page.path(), handler); } } // start server http.start(); if (https != null) { http.start(); } LOG.trace("Listener is Started."); System.out.println("Server Running. Press [k] to kill listener."); boolean running = true; do { try { running = System.in.read() == 'k'; } catch (IOException e) { } } while (running); LOG.trace("Server stopping."); http.stop(1); if (https != null) { http.stop(1); } } finally { try { if (accessLog != null) { accessLog.close(); } } catch (IOException e) { } LOG.trace("Server stopped."); } }