Starts a stand-alone HTTP server, serving files from disk. @param args the command line arguments
(String[] args)
| 3048 | * @param args the command line arguments |
| 3049 | */ |
| 3050 | public static void main(String[] args) { |
| 3051 | try { |
| 3052 | if (args.length == 0) { |
| 3053 | System.err.printf("Usage: java [-options] %s <directory> [port]%n" + |
| 3054 | "To enable SSL: specify options -Djavax.net.ssl.keyStore, " + |
| 3055 | "-Djavax.net.ssl.keyStorePassword, etc.%n", HTTPServer.class.getName()); |
| 3056 | return; |
| 3057 | } |
| 3058 | File dir = new File(args[0]); |
| 3059 | if (!dir.canRead()) |
| 3060 | throw new FileNotFoundException(dir.getAbsolutePath()); |
| 3061 | int port = args.length < 2 ? 80 : (int)parseULong(args[1], 10); |
| 3062 | // set up server |
| 3063 | for (File f : Arrays.asList(new File("/etc/mime.types"), new File(dir, ".mime.types"))) |
| 3064 | if (f.exists()) |
| 3065 | addContentTypes(new FileInputStream(f)); |
| 3066 | HTTPServer server = new HTTPServer(port); |
| 3067 | if (System.getProperty("javax.net.ssl.keyStore") != null) // enable SSL if configured |
| 3068 | server.setServerSocketFactory(SSLServerSocketFactory.getDefault()); |
| 3069 | VirtualHost host = server.getVirtualHost(null); // default host |
| 3070 | host.setAllowGeneratedIndex(true); // with directory index pages |
| 3071 | host.addContext("/", new FileContextHandler(dir)); |
| 3072 | host.addContext("/api/time", new ContextHandler() { |
| 3073 | public int serve(Request req, Response resp) throws IOException { |
| 3074 | long now = System.currentTimeMillis(); |
| 3075 | resp.getHeaders().add("Content-Type", "text/plain"); |
| 3076 | resp.send(200, String.format("%tF %<tT", now)); |
| 3077 | return 0; |
| 3078 | } |
| 3079 | }); |
| 3080 | server.start(); |
| 3081 | System.out.println("HTTPServer is listening on port " + port); |
| 3082 | } catch (Exception e) { |
| 3083 | System.err.println("error: " + e); |
| 3084 | } |
| 3085 | } |
| 3086 | } |
nothing calls this directly
no test coverage detected