44import com .sun .net .httpserver .HttpServer ;
55import express .events .Action ;
66import express .events .HttpRequest ;
7- import express .expressfilter .ExpressFilterChain ;
87import express .expressfilter .ExpressFilterImpl ;
98import express .expressfilter .ExpressFilterTask ;
109import express .expressfilter .ExpressFilterWorker ;
11- import express .http .request .Request ;
12- import express .http .response .Response ;
10+ import express .expressfilter .FilterLayerHandler ;
1311import express .middleware .ExpressMiddleware ;
1412
1513import java .io .IOException ;
2725 */
2826public class Express extends ExpressMiddleware {
2927
30- private final ConcurrentHashMap <String , HttpRequest > PARAMETER_LISTENER = new ConcurrentHashMap <>() ;
31- private final ConcurrentHashMap <Object , Object > LOCALS = new ConcurrentHashMap <>() ;
28+ private final ConcurrentHashMap <String , HttpRequest > PARAMETER_LISTENER ;
29+ private final ConcurrentHashMap <Object , Object > LOCALS ;
3230
33- private final ArrayList <ExpressFilterWorker > WORKER = new ArrayList <>();
34- private final ExpressFilterChain MIDDLEWARE_CHAIN = new ExpressFilterChain ();
35- private final ExpressFilterChain FILTER_CHAIN = new ExpressFilterChain ();
31+ private final ArrayList <ExpressFilterWorker > WORKER ;
32+ private final FilterLayerHandler HANDLER ;
3633
37- private Executor executor = Executors . newCachedThreadPool () ;
38- private String hostname = "localhost" ;
34+ private Executor executor ;
35+ private String hostname ;
3936 private HttpServer httpServer ;
4037
38+ {
39+ // Initialize
40+ PARAMETER_LISTENER = new ConcurrentHashMap <>();
41+ LOCALS = new ConcurrentHashMap <>();
42+
43+ WORKER = new ArrayList <>();
44+ HANDLER = new FilterLayerHandler ();
45+
46+ executor = Executors .newCachedThreadPool ();
47+ hostname = "localhost" ;
48+ }
49+
4150 /**
4251 * Create an express instance and bind the server to an hostname.
4352 * Default is "Localhost"
@@ -135,12 +144,13 @@ public void use(@NotNull String context, @NotNull String requestMethod, @NotNull
135144 addMiddleware (requestMethod .toUpperCase (), context , middleware );
136145 }
137146
147+ // Internal service to handle middleware
138148 private void addMiddleware (@ NotNull String requestMethod , @ NotNull String context , HttpRequest middleware ) {
139149 if (middleware instanceof ExpressFilterTask ) {
140150 WORKER .add (new ExpressFilterWorker ((ExpressFilterTask ) middleware ));
141151 }
142152
143- MIDDLEWARE_CHAIN .add (new ExpressFilterImpl (this , requestMethod , context , middleware ));
153+ HANDLER .add (0 , new ExpressFilterImpl (this , requestMethod , context , middleware ));
144154 }
145155
146156 /**
@@ -150,7 +160,7 @@ private void addMiddleware(@NotNull String requestMethod, @NotNull String contex
150160 * @param request An listener which will be fired if the context matches the requestpath.
151161 */
152162 public void all (@ NotNull String context , @ NotNull HttpRequest request ) {
153- FILTER_CHAIN .add (new ExpressFilterImpl (this , "*" , context , request ));
163+ HANDLER .add (1 , new ExpressFilterImpl (this , "*" , context , request ));
154164 }
155165
156166 /**
@@ -160,7 +170,7 @@ public void all(@NotNull String context, @NotNull HttpRequest request) {
160170 * @param request An listener which will be fired if the context matches the requestpath.
161171 */
162172 public void get (@ NotNull String context , @ NotNull HttpRequest request ) {
163- FILTER_CHAIN .add (new ExpressFilterImpl (this , "GET" , context , request ));
173+ HANDLER .add (1 , new ExpressFilterImpl (this , "GET" , context , request ));
164174 }
165175
166176 /**
@@ -170,7 +180,7 @@ public void get(@NotNull String context, @NotNull HttpRequest request) {
170180 * @param request An listener which will be fired if the context matches the requestpath.
171181 */
172182 public void post (@ NotNull String context , @ NotNull HttpRequest request ) {
173- FILTER_CHAIN .add (new ExpressFilterImpl (this , "POST" , context , request ));
183+ HANDLER .add (1 , new ExpressFilterImpl (this , "POST" , context , request ));
174184 }
175185
176186 /**
@@ -180,7 +190,7 @@ public void post(@NotNull String context, @NotNull HttpRequest request) {
180190 * @param request An listener which will be fired if the context matches the requestpath.
181191 */
182192 public void put (@ NotNull String context , @ NotNull HttpRequest request ) {
183- FILTER_CHAIN .add (new ExpressFilterImpl (this , "PUT" , context , request ));
193+ HANDLER .add (1 , new ExpressFilterImpl (this , "PUT" , context , request ));
184194 }
185195
186196 /**
@@ -190,7 +200,7 @@ public void put(@NotNull String context, @NotNull HttpRequest request) {
190200 * @param request An listener which will be fired if the context matches the requestpath.
191201 */
192202 public void delete (@ NotNull String context , @ NotNull HttpRequest request ) {
193- FILTER_CHAIN .add (new ExpressFilterImpl (this , "DELETE" , context , request ));
203+ HANDLER .add (1 , new ExpressFilterImpl (this , "DELETE" , context , request ));
194204 }
195205
196206 /**
@@ -200,7 +210,7 @@ public void delete(@NotNull String context, @NotNull HttpRequest request) {
200210 * @param request An listener which will be fired if the context matches the requestpath.
201211 */
202212 public void patch (@ NotNull String context , @ NotNull HttpRequest request ) {
203- FILTER_CHAIN .add (new ExpressFilterImpl (this , "PATCH" , context , request ));
213+ HANDLER .add (1 , new ExpressFilterImpl (this , "PATCH" , context , request ));
204214 }
205215
206216 /**
@@ -211,7 +221,7 @@ public void patch(@NotNull String context, @NotNull HttpRequest request) {
211221 * @param request An listener which will be fired if the context matches the requestpath.
212222 */
213223 public void on (@ NotNull String requestMethod , @ NotNull String context , @ NotNull HttpRequest request ) {
214- FILTER_CHAIN .add (new ExpressFilterImpl (this , requestMethod , context , request ));
224+ HANDLER .add (1 , new ExpressFilterImpl (this , requestMethod , context , request ));
215225 }
216226
217227 /**
@@ -224,7 +234,7 @@ public void on(@NotNull String requestMethod, @NotNull String context, @NotNull
224234 */
225235 public void on (@ NotNull String requestMethod , @ NotNull HttpRequest request , String ... contexts ) {
226236 for (String c : contexts )
227- FILTER_CHAIN .add (new ExpressFilterImpl (this , requestMethod , c , request ));
237+ HANDLER .add (1 , new ExpressFilterImpl (this , requestMethod , c , request ));
228238 }
229239
230240 /**
@@ -276,21 +286,14 @@ public void listen(Action onStart, int port) throws IOException {
276286
277287 // Create http server
278288 httpServer = HttpServer .create (new InetSocketAddress (this .hostname , port ), 0 );
279- httpServer .setExecutor (executor );
280-
281- httpServer .createContext ("/" , httpExchange -> {
282- Request request = new Request (httpExchange );
283- Response response = new Response (httpExchange );
284-
285- // First fire all middlewares, then the normal request filter
286- MIDDLEWARE_CHAIN .filter (request , response );
287- FILTER_CHAIN .filter (request , response );
288- });
289-
290- httpServer .start ();
289+ httpServer .setExecutor (executor ); // Set thread executor
290+ httpServer .createContext ("/" , HANDLER ); // Set handler for all contexts
291+ httpServer .start (); // Start server
291292
292293 // Fire listener
293- if (onStart != null ) onStart .action ();
294+ if (onStart != null )
295+ onStart .action ();
296+
294297 } catch (IOException e ) {
295298 // TODO: Handle errors
296299 e .printStackTrace ();
0 commit comments