22
33import com .sun .istack .internal .NotNull ;
44import com .sun .net .httpserver .HttpServer ;
5+ import com .sun .net .httpserver .HttpsConfigurator ;
6+ import com .sun .net .httpserver .HttpsServer ;
57import express .events .Action ;
68import express .events .HttpRequest ;
79import express .expressfilter .ExpressFilterImpl ;
@@ -34,6 +36,7 @@ public class Express extends ExpressMiddleware {
3436 private Executor executor ;
3537 private String hostname ;
3638 private HttpServer httpServer ;
39+ private HttpsConfigurator httpsConfigurator ;
3740
3841 {
3942 // Initialize
@@ -57,12 +60,40 @@ public Express(@NotNull String hostname) {
5760 this .hostname = hostname ;
5861 }
5962
63+ /**
64+ * Default, will bind the server to "localhost"
65+ *
66+ * @param httpsConfigurator The HttpsConfigurator for https
67+ */
68+ public Express (HttpsConfigurator httpsConfigurator ) {
69+ this .httpsConfigurator = httpsConfigurator ;
70+ }
71+
72+ /**
73+ * Create an express instance and bind the server to an hostname.
74+ * Default is "Localhost"
75+ *
76+ * @param hostname The host name
77+ * @param httpsConfigurator The HttpsConfigurator for https
78+ */
79+ public Express (@ NotNull String hostname , HttpsConfigurator httpsConfigurator ) {
80+ this .hostname = hostname ;
81+ this .httpsConfigurator = httpsConfigurator ;
82+ }
83+
6084 /**
6185 * Default, will bind the server to "localhost"
6286 */
6387 public Express () {
6488 }
6589
90+ /**
91+ * @return True if the server uses https.
92+ */
93+ public boolean isSecure () {
94+ return httpsConfigurator != null ;
95+ }
96+
6697 /**
6798 * Add an listener which will be called when an url with this parameter is called.
6899 *
@@ -163,6 +194,15 @@ public void all(@NotNull String context, @NotNull HttpRequest request) {
163194 HANDLER .add (1 , new ExpressFilterImpl (this , "*" , context , request ));
164195 }
165196
197+ /**
198+ * Add an listener for all request methods and contexts.
199+ *
200+ * @param request Will be fired on all requests.
201+ */
202+ public void all (@ NotNull HttpRequest request ) {
203+ HANDLER .add (1 , new ExpressFilterImpl (this , "*" , "*" , request ));
204+ }
205+
166206 /**
167207 * Add an listener for GET request's.
168208 *
@@ -283,8 +323,19 @@ public void listen(Action onStart, int port) throws IOException {
283323 // Fire worker threads
284324 WORKER .forEach (ExpressFilterWorker ::start );
285325
286- // Create http server
287- httpServer = HttpServer .create (new InetSocketAddress (this .hostname , port ), 0 );
326+ InetSocketAddress socketAddress = new InetSocketAddress (this .hostname , port );
327+
328+ if (httpsConfigurator != null ) {
329+
330+ // Create https server
331+ httpServer = HttpsServer .create (socketAddress , 0 );
332+ ((HttpsServer ) httpServer ).setHttpsConfigurator (httpsConfigurator );
333+ } else {
334+
335+ // Create http server
336+ httpServer = HttpServer .create (socketAddress , 0 );
337+ }
338+
288339 httpServer .setExecutor (executor ); // Set thread executor
289340 httpServer .createContext ("/" , HANDLER ); // Set handler for all contexts
290341 httpServer .start (); // Start server
0 commit comments