public static void main(String[] args) throws IOException { if (args.length > 3 || usageRequested(args)) { System.out.println( "Usage: " + HTTPServer.class.getSimpleName() + " [--port|-p port] [--public]"); printCommonOptions(); System.exit(1); } final boolean runInternal = false; final HTTPServerConfig config = new HTTPServerConfig(args); try { final HTTPServer server; System.out.println( "WARNING: running in HTTP mode, consider using SSL by running " + HTTPSServer.class.getName() + " instead"); if (config.isPublicAccess()) { System.out.println( "WARNING: running in public mode, LanguageTool API can be accessed without restrictions!"); server = new HTTPServer(config, runInternal, null, null); } else { server = new HTTPServer(config, runInternal, DEFAULT_HOST, DEFAULT_ALLOWED_IPS); } server.run(); } catch (Exception e) { throw new RuntimeException( "Could not start LanguageTool HTTP server on " + DEFAULT_HOST + ", port " + config.getPort(), e); } }
/** * Prepare a server on the given host and port - use run() to start it. * * @param runInternally if true, then the server was started from the GUI. * @param host the host to bind to, e.g. <code>"localhost"</code> or <code>null</code> to bind to * any host * @param allowedIps the IP addresses from which connections are allowed or <code>null</code> to * allow any host * @throws PortBindingException if we cannot bind to the given port, e.g. because something else * is running there * @since 1.7 */ public HTTPServer( HTTPServerConfig config, boolean runInternally, String host, Set<String> allowedIps) { this.port = config.getPort(); this.host = host; try { if (host == null) { server = HttpServer.create(new InetSocketAddress(port), 0); } else { server = HttpServer.create(new InetSocketAddress(host, port), 0); } final LanguageToolHttpHandler httpHandler = new LanguageToolHttpHandler(config.isVerbose(), allowedIps, runInternally, null); httpHandler.setAllowOriginUrl(config.getAllowOriginUrl()); server.createContext("/", httpHandler); } catch (Exception e) { final ResourceBundle messages = JLanguageTool.getMessageBundle(); final String message = Tools.makeTexti18n(messages, "http_server_start_failed", host, Integer.toString(port)); throw new PortBindingException(message, e); } }