Constructor. @param args command-line arguments @throws Exception exception
(final String... args)
| 68 | * @throws Exception exception |
| 69 | */ |
| 70 | public BaseXHTTP(final String... args) throws Exception { |
| 71 | super(null, args); |
| 72 | |
| 73 | // context must be initialized after parsing of arguments |
| 74 | soptions = new StaticOptions(true); |
| 75 | |
| 76 | if(!quiet) Util.println(header()); |
| 77 | |
| 78 | // initialize configuration files and initialize HTTP context |
| 79 | final String webapp = soptions.get(StaticOptions.WEBPATH); |
| 80 | final WebAppContext wac = new WebAppContext(webapp, "/"); |
| 81 | final IOFile webXml = locate(WEBCONF, webapp), jettyXml = locate(JETTYCONF, webapp); |
| 82 | |
| 83 | hc = HTTPContext.get(); |
| 84 | hc.init(soptions, webXml); |
| 85 | |
| 86 | // create jetty instance |
| 87 | final URI jettyUri = Paths.get(jettyXml.toString()).toUri(); |
| 88 | final Resource resource = new PathResourceFactory().newResource(jettyUri); |
| 89 | jetty = (Server) new XmlConfiguration(resource).configure(); |
| 90 | |
| 91 | // try to use GZIP compression (changed with Jetty 12.1) |
| 92 | Supplier<Handler> supplier = wac; |
| 93 | if(soptions.get(StaticOptions.GZIP)) { |
| 94 | final String clzz = "org.eclipse.jetty.compression.server.CompressionHandler"; |
| 95 | if(Reflect.available(clzz)) { |
| 96 | // create anonymous class, as a lambda expression would yield a ClassNotFoundException |
| 97 | // if the compression handler is not included in the classpath |
| 98 | supplier = gzip(wac); |
| 99 | } else if(Reflect.available("org.eclipse.jetty.server.handler.gzip.GzipHandler")) { |
| 100 | Util.errln("Please add " + clzz + " to the classpath to enable GZIP compression"); |
| 101 | } |
| 102 | } |
| 103 | jetty.setHandler(supplier); |
| 104 | JettyWebSocketServletContainerInitializer.configure(wac, null); |
| 105 | |
| 106 | ServerConnector sc = null; |
| 107 | for(final Connector conn : jetty.getConnectors()) { |
| 108 | if(conn instanceof final ServerConnector s) sc = s; |
| 109 | } |
| 110 | if(sc == null) throw new BaseXException("No Jetty connector defined in " + JETTYCONF + '.'); |
| 111 | if(port != 0) sc.setPort(port); |
| 112 | else port = sc.getPort(); |
| 113 | |
| 114 | // info strings |
| 115 | final Function<Boolean, String> msg1 = start -> start ? SRV_STARTED_PORT_X : SRV_STOPPED_PORT_X; |
| 116 | final Function<Boolean, String> msg2 = start -> Util.info(HTTP + ' ' + msg1.apply(start), port); |
| 117 | // output user info, keep message visible for a while |
| 118 | final Consumer<Boolean> info = start -> { |
| 119 | Util.println(msg2.apply(start)); |
| 120 | if(!soptions.get(StaticOptions.HTTPLOCAL)) { |
| 121 | final int serverPort = soptions.get(StaticOptions.SERVERPORT); |
| 122 | Util.println(msg1.apply(start), serverPort); |
| 123 | } |
| 124 | Performance.sleep(1000); |
| 125 | }; |
| 126 | |
| 127 | // stop web server |