77import express .filter .FilterLayerHandler ;
88import express .filter .FilterTask ;
99import express .filter .FilterWorker ;
10- import express .http .HttpRequest ;
10+ import express .http .HttpRequestHandler ;
1111
1212import java .io .IOException ;
1313import java .net .InetSocketAddress ;
2222 */
2323public class Express implements Router {
2424
25- private final ConcurrentHashMap <String , HttpRequest > PARAMETER_LISTENER ;
26- private final ConcurrentHashMap <Object , Object > LOCALS ;
25+ private final ConcurrentHashMap <String , HttpRequestHandler > parameterListener ;
26+ private final ConcurrentHashMap <Object , Object > locals ;
2727
28- private final ArrayList <FilterWorker > WORKER ;
29- private final FilterLayerHandler HANDLER ;
28+ private final ArrayList <FilterWorker > worker ;
29+ private final FilterLayerHandler handler ;
3030
3131 private Executor executor ;
3232 private String hostname ;
@@ -35,11 +35,11 @@ public class Express implements Router {
3535
3636 {
3737 // Initialize
38- PARAMETER_LISTENER = new ConcurrentHashMap <>();
39- LOCALS = new ConcurrentHashMap <>();
38+ parameterListener = new ConcurrentHashMap <>();
39+ locals = new ConcurrentHashMap <>();
4040
41- WORKER = new ArrayList <>();
42- HANDLER = new FilterLayerHandler (2 );
41+ worker = new ArrayList <>();
42+ handler = new FilterLayerHandler (2 );
4343
4444 executor = Executors .newCachedThreadPool ();
4545 }
@@ -89,17 +89,18 @@ public boolean isSecure() {
8989 }
9090
9191 /**
92- * Add an listener which will be called when an url with this parameter is called.
92+ * Add a listener which will be called when an url with this parameter is called.
9393 *
9494 * @param param The parameter name.
9595 * @param request An request handler.
9696 */
97- public void onParam (String param , HttpRequest request ) {
98- PARAMETER_LISTENER .put (param , request );
97+ public Express onParam (String param , HttpRequestHandler request ) {
98+ parameterListener .put (param , request );
99+ return this ;
99100 }
100101
101- public ConcurrentHashMap <String , HttpRequest > getParameterListener () {
102- return PARAMETER_LISTENER ;
102+ public ConcurrentHashMap <String , HttpRequestHandler > getParameterListener () {
103+ return parameterListener ;
103104 }
104105
105106 /**
@@ -111,7 +112,7 @@ public ConcurrentHashMap<String, HttpRequest> getParameterListener() {
111112 * @return The last value which was attached by this key, can be null.
112113 */
113114 public Object set (String key , String val ) {
114- return LOCALS .put (key , val );
115+ return locals .put (key , val );
115116 }
116117
117118 /**
@@ -121,7 +122,7 @@ public Object set(String key, String val) {
121122 * @return The value.
122123 */
123124 public Object get (String key ) {
124- return LOCALS .get (key );
125+ return locals .get (key );
125126 }
126127
127128 /**
@@ -144,9 +145,10 @@ public void setExecutor(Executor executor) throws IOException {
144145 *
145146 * @param router The router.
146147 */
147- public void use (ExpressRouter router ) {
148- this .HANDLER .combine (router .getHandler ());
149- this .WORKER .addAll (router .getWorker ());
148+ public Express use (ExpressRouter router ) {
149+ this .handler .combine (router .getHandler ());
150+ this .worker .addAll (router .getWorker ());
151+ return this ;
150152 }
151153
152154 /**
@@ -156,67 +158,80 @@ public void use(ExpressRouter router) {
156158 * @param router The router.
157159 */
158160 @ SuppressWarnings ("unchecked" )
159- public void use (String root , ExpressRouter router ) {
161+ public Express use (String root , ExpressRouter router ) {
160162
161163 router .getHandler ().forEach (fl -> fl .getFilter ().forEach (layer -> {
162164 ((FilterImpl ) layer ).setRoot (root );
163165 }));
164166
165- this .HANDLER .combine (router .getHandler ());
166- this .WORKER .addAll (router .getWorker ());
167+ this .handler .combine (router .getHandler ());
168+ this .worker .addAll (router .getWorker ());
169+
170+ return this ;
167171 }
168172
169- public void use (HttpRequest middleware ) {
173+ public Express use (HttpRequestHandler middleware ) {
170174 addMiddleware ("*" , "*" , middleware );
175+ return this ;
171176 }
172177
173- public void use (String context , HttpRequest middleware ) {
178+ public Express use (String context , HttpRequestHandler middleware ) {
174179 addMiddleware ("*" , context , middleware );
180+ return this ;
175181 }
176182
177- public void use (String context , String requestMethod , HttpRequest middleware ) {
183+ public Express use (String context , String requestMethod , HttpRequestHandler middleware ) {
178184 addMiddleware (requestMethod .toUpperCase (), context , middleware );
185+ return this ;
179186 }
180187
181188 // Internal service to handle middleware
182- private void addMiddleware (String requestMethod , String context , HttpRequest middleware ) {
189+ private void addMiddleware (String requestMethod , String context , HttpRequestHandler middleware ) {
183190 if (middleware instanceof FilterTask ) {
184- WORKER .add (new FilterWorker ((FilterTask ) middleware ));
191+ worker .add (new FilterWorker ((FilterTask ) middleware ));
185192 }
186193
187- HANDLER .add (0 , new FilterImpl (requestMethod , context , middleware ));
194+ handler .add (0 , new FilterImpl (requestMethod , context , middleware ));
188195 }
189196
190- public void all (HttpRequest request ) {
191- HANDLER .add (1 , new FilterImpl ("*" , "*" , request ));
197+ public Express all (HttpRequestHandler request ) {
198+ handler .add (1 , new FilterImpl ("*" , "*" , request ));
199+ return this ;
192200 }
193201
194- public void all (String context , HttpRequest request ) {
195- HANDLER .add (1 , new FilterImpl ("*" , context , request ));
202+ public Express all (String context , HttpRequestHandler request ) {
203+ handler .add (1 , new FilterImpl ("*" , context , request ));
204+ return this ;
196205 }
197206
198- public void all (String context , String requestMethod , HttpRequest request ) {
199- HANDLER .add (1 , new FilterImpl (requestMethod , context , request ));
207+ public Express all (String context , String requestMethod , HttpRequestHandler request ) {
208+ handler .add (1 , new FilterImpl (requestMethod , context , request ));
209+ return this ;
200210 }
201211
202- public void get (String context , HttpRequest request ) {
203- HANDLER .add (1 , new FilterImpl ("GET" , context , request ));
212+ public Express get (String context , HttpRequestHandler request ) {
213+ handler .add (1 , new FilterImpl ("GET" , context , request ));
214+ return this ;
204215 }
205216
206- public void post (String context , HttpRequest request ) {
207- HANDLER .add (1 , new FilterImpl ("POST" , context , request ));
217+ public Express post (String context , HttpRequestHandler request ) {
218+ handler .add (1 , new FilterImpl ("POST" , context , request ));
219+ return this ;
208220 }
209221
210- public void put (String context , HttpRequest request ) {
211- HANDLER .add (1 , new FilterImpl ("PUT" , context , request ));
222+ public Express put (String context , HttpRequestHandler request ) {
223+ handler .add (1 , new FilterImpl ("PUT" , context , request ));
224+ return this ;
212225 }
213226
214- public void delete (String context , HttpRequest request ) {
215- HANDLER .add (1 , new FilterImpl ("DELETE" , context , request ));
227+ public Express delete (String context , HttpRequestHandler request ) {
228+ handler .add (1 , new FilterImpl ("DELETE" , context , request ));
229+ return this ;
216230 }
217231
218- public void patch (String context , HttpRequest request ) {
219- HANDLER .add (1 , new FilterImpl ("PATCH" , context , request ));
232+ public Express patch (String context , HttpRequestHandler request ) {
233+ handler .add (1 , new FilterImpl ("PATCH" , context , request ));
234+ return this ;
220235 }
221236
222237 /**
@@ -259,7 +274,7 @@ public void listen(ExpressListener onStart, int port) {
259274 try {
260275
261276 // Fire worker threads
262- WORKER .forEach (FilterWorker ::start );
277+ worker .forEach (FilterWorker ::start );
263278
264279 InetSocketAddress socketAddress = this .hostname == null ? new InetSocketAddress (port ) : new InetSocketAddress (this .hostname , port );
265280 if (httpsConfigurator != null ) {
@@ -277,7 +292,7 @@ public void listen(ExpressListener onStart, int port) {
277292 httpServer .setExecutor (executor );
278293
279294 // Create handler for all contexts
280- httpServer .createContext ("/" , exchange -> HANDLER .handle (exchange , this ));
295+ httpServer .createContext ("/" , exchange -> handler .handle (exchange , this ));
281296
282297 // Start server
283298 httpServer .start ();
@@ -302,7 +317,7 @@ public void stop() {
302317 httpServer .stop (0 );
303318
304319 // Stop worker threads
305- WORKER .forEach (FilterWorker ::stop );
320+ worker .forEach (FilterWorker ::stop );
306321 }
307322 }
308323}
0 commit comments