1515import java .util .Collections ;
1616import java .util .List ;
1717
18+ /**
19+ * @author Simon Reinisch
20+ * @implNote Core modul, don't change anything.
21+ *
22+ * An NodeJS like clone written in Java, see README for more information.
23+ */
1824public class Express {
1925
2026 private final List <Filter > MIDDLEWARE = Collections .synchronizedList (new ArrayList <>());
2127 private final List <Filter > FILTER = Collections .synchronizedList (new ArrayList <>());
28+
29+ // Index of last middleware to keep it sorted
2230 int middlewareIndex = 0 ;
2331
2432 private HttpServer httpServer ;
2533 private HttpContext httpContext ;
2634 private HttpRequest request404 ;
2735
36+ /**
37+ * Add an middleware which will be firea BEFORE EACH request-type listener will be fired.
38+ *
39+ * @param request An listener which will be fired on every equestmethod- and path.
40+ */
2841 public void use (HttpRequest request ) {
2942 addFilter (true , "*" , "*" , request );
3043 }
3144
45+ /**
46+ * Add an middleware which will be firea BEFORE EACH request-type listener will be fired.
47+ *
48+ * @param context The context where the middleware should listen, see README for information about placeholder.
49+ * @param request An listener which will be fired if the context matches the requestpath.
50+ */
3251 public void use (String context , HttpRequest request ) {
3352 addFilter (true , "*" , context , request );
3453 }
3554
55+ /**
56+ * Add an middleware which will be firea BEFORE EACH request-type listener will be fired.
57+ *
58+ * @param context The context where the middleware should listen, see README for information about placeholder.
59+ * @param requestMethod And type of request-method eg. GET, POST etc.
60+ * @param request An listener which will be fired if the context matches the requestmethod- and path.
61+ */
3662 public void use (String context , String requestMethod , HttpRequest request ) {
3763 addFilter (true , requestMethod .toUpperCase (), context , request );
3864 }
3965
66+ /**
67+ * Add an listener for request types.
68+ *
69+ * @param context The context, see README for information about placeholder.
70+ * @param request An listener.
71+ */
4072 public void all (String context , HttpRequest request ) {
4173 addFilter (false , "*" , context , request );
4274 }
4375
76+ /**
77+ * Add an listener for GET request's.
78+ *
79+ * @param context The context, see README for information about placeholder.
80+ * @param request An listener which will be fired if the context matches the requestpath.
81+ */
4482 public void get (String context , HttpRequest request ) {
4583 addFilter (false , "GET" , context , request );
4684 }
4785
86+ /**
87+ * Add an listener for POST request's.
88+ *
89+ * @param context The context, see README for information about placeholder.
90+ * @param request An listener which will be fired if the context matches the requestpath.
91+ */
4892 public void post (String context , HttpRequest request ) {
4993 addFilter (false , "POST" , context , request );
5094 }
5195
96+ /**
97+ * Add an listener for PUT request's.
98+ *
99+ * @param context The context, see README for information about placeholder.
100+ * @param request An listener which will be fired if the context matches the requestpath.
101+ */
52102 public void put (String context , HttpRequest request ) {
53103 addFilter (false , "PUT" , context , request );
54104 }
55105
106+ /**
107+ * Add an listener for DELETE request's.
108+ *
109+ * @param context The context, see README for information about placeholder.
110+ * @param request An listener which will be fired if the context matches the requestpath.
111+ */
56112 public void delete (String context , HttpRequest request ) {
57113 addFilter (false , "DELETE" , context , request );
58114 }
59115
116+ /**
117+ * Add an listener for PATCH request's.
118+ *
119+ * @param context The context, see README for information about placeholder.
120+ * @param request An listener which will be fired if the context matches the requestpath.
121+ */
60122 public void patch (String context , HttpRequest request ) {
61123 addFilter (false , "PATCH" , context , request );
62124 }
63125
126+ /**
127+ * Internal method to add an filter
128+ *
129+ * @param middleware If the filter is an middleware
130+ * @param requestMethod The request-method
131+ * @param context The url-path
132+ * @param request An listener which will be fired if the given context is matching
133+ */
64134 private void addFilter (boolean middleware , String requestMethod , String context , HttpRequest request ) {
65135 ExpressFilter handler = new ExpressFilter (requestMethod , context , request );
136+
137+ // Check if the server is already active
66138 if (httpContext == null ) {
139+
140+ // Middleware needs an seperated list because it will ALWAYS fired before each request handler
67141 if (middleware ) {
68142 MIDDLEWARE .add (handler );
69143 } else {
@@ -73,46 +147,70 @@ private void addFilter(boolean middleware, String requestMethod, String context,
73147 List <Filter > filters = httpContext .getFilters ();
74148
75149 if (middleware ) {
150+
151+ // Insert middleware after the last middleware
76152 filters .add (middlewareIndex , handler );
77153 middlewareIndex ++;
78154 } else filters .add (handler );
79155 }
80156 }
81157
82- private void addFirst (String requestMethod , String context , HttpRequest request ) {
83- ExpressFilter handler = new ExpressFilter (requestMethod , context , request );
84- if (httpContext == null ) {
85- MIDDLEWARE .add (handler );
86- } else {
87- httpContext .getFilters ().add (handler );
88- }
158+ /**
159+ * Set an extra lisener for 404 (not found) requests, also request where no context
160+ * match the given request path (or method).
161+ *
162+ * @param request An listener.
163+ */
164+ public void set404 (HttpRequest request ) {
165+ this .request404 = request ;
89166 }
90167
168+ /**
169+ * Start the HTTP-Server on port 80.
170+ * This method is asyncronous so be sure to add an listener or keep it in mind!
171+ *
172+ * @throws IOException - If an IO-Error occurs, eg. the port is already in use.
173+ */
91174 public void listen () throws IOException {
92- launch (null , 80 );
175+ listen (null , 80 );
93176 }
94177
178+ /**
179+ * Start the HTTP-Server on a specific port
180+ * This method is asyncronous so be sure to add an listener or keep it in mind!
181+ *
182+ * @param port The port.
183+ * @throws IOException - If an IO-Error occurs, eg. the port is already in use.
184+ */
95185 public void listen (int port ) throws IOException {
96- launch (null , port );
97- }
98-
99- public void listen (Action action ) throws IOException {
100- launch (action , 80 );
186+ listen (null , port );
101187 }
102188
103- public void listen (int port , Action action ) throws IOException {
104- launch (action , port );
189+ /**
190+ * Start the HTTP-Server on port 80.
191+ * This method is asyncronous so be sure to add an listener or keep it in mind!
192+ *
193+ * @param onStart An listener which will be fired after the server is stardet.
194+ * @throws IOException - If an IO-Error occurs, eg. the port is already in use.
195+ */
196+ public void listen (Action onStart ) throws IOException {
197+ listen (onStart , 80 );
105198 }
106199
107- public void set404 (HttpRequest request ) {
108- this .request404 = request ;
109- }
110-
111- private void launch (Action action , int port ) throws IOException {
200+ /**
201+ * Start the HTTP-Server on a specific port.
202+ * This method is asyncronous so be sure to add an listener or keep it in mind!
203+ *
204+ * @param onStart An listener which will be fired after the server is stardet.
205+ * @param port The port.
206+ * @throws IOException - If an IO-Error occurs, eg. the port is already in use.
207+ */
208+ public void listen (Action onStart , int port ) throws IOException {
112209 new Thread (() -> {
113210 try {
114211 httpServer = HttpServer .create (new InetSocketAddress ("localhost" , port ), 0 );
115212 httpServer .setExecutor (null );
213+
116214 httpContext = httpServer .createContext ("/" , httpExchange -> {
117215 if (request404 != null ) {
118216 Request request = new Request (httpExchange );
@@ -127,14 +225,26 @@ private void launch(Action action, int port) throws IOException {
127225 httpContext .getFilters ().addAll (FILTER );
128226
129227 httpServer .start ();
130- action .action ();
228+
229+ // Fire listener
230+ onStart .action ();
131231 } catch (IOException e ) {
132232 // TODO: Handle errors
133233 e .printStackTrace ();
134234 }
135235 }).start ();
136236 }
137237
238+ /**
239+ * This method serves an entire folder which can contains static file for your
240+ * web application, it automatically detect the content type and will send it to
241+ * the Client.
242+ * <p>
243+ * To use it simply put it in the <code>app.use()</code> method!
244+ *
245+ * @param path The root directory
246+ * @return An HttpRequest interface with the service.
247+ */
138248 public static HttpRequest statics (String path ) {
139249 return (req , res ) -> {
140250 File reqFile = new File (path + req .getURI ().getPath ());
0 commit comments