Skip to content

Commit 95001cc

Browse files
committed
Add comments
1 parent b037ec6 commit 95001cc

File tree

10 files changed

+296
-34
lines changed

10 files changed

+296
-34
lines changed

src/express/Express.java

Lines changed: 131 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,55 +15,129 @@
1515
import java.util.Collections;
1616
import 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+
*/
1824
public 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());

src/express/ExpressFilter.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111
import java.util.regex.Matcher;
1212
import java.util.regex.Pattern;
1313

14+
/**
15+
* @author Simon Reinisch
16+
* @implNote Core modul of express, don't change anything.
17+
*
18+
* Filter to handle request handling & parsing.
19+
*/
1420
public class ExpressFilter extends Filter {
1521

1622
private final HttpRequest REQUEST;
@@ -54,9 +60,11 @@ public void doFilter(HttpExchange httpExchange, Chain chain) throws IOException
5460
return;
5561
}
5662

63+
// Parse params, see README
5764
HashMap<String, String> params = new HashMap<>();
5865
Matcher matcher = CONTEXT_PATTERN.matcher(requestPath);
5966

67+
// Match all params
6068
if (matcher.find()) {
6169

6270
for (int i = 1; i <= matcher.groupCount() && i < CONTEXT_PARAMS.length; i++) {

src/express/ExpressUtils.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,27 @@
33
import com.sun.net.httpserver.Headers;
44
import express.cookie.Cookie;
55

6-
import java.io.*;
6+
import java.io.UnsupportedEncodingException;
77
import java.net.URI;
88
import java.net.URLDecoder;
99
import java.util.HashMap;
1010
import java.util.List;
1111
import java.util.regex.Matcher;
1212
import java.util.regex.Pattern;
1313

14+
/**
15+
* @author Simon Reinisch
16+
* <p>
17+
* A few utils which are used by express.
18+
*/
1419
public class ExpressUtils {
1520

21+
/**
22+
* Extract the cookies from the 'Cookie' header.
23+
*
24+
* @param headers The Headers
25+
* @return An hashmap with the cookie name as key and the complete cookie as value.
26+
*/
1627
public static HashMap<String, Cookie> parseCookies(Headers headers) {
1728
HashMap<String, Cookie> cookieList = new HashMap<>();
1829
List<String> headerCookies = headers.get("Cookie");
@@ -34,6 +45,12 @@ public static HashMap<String, Cookie> parseCookies(Headers headers) {
3445
return cookieList;
3546
}
3647

48+
/**
49+
* Method to extract the querys from an url.
50+
*
51+
* @param uri The uri.
52+
* @return An list with key-values which are encoded in UTF8.
53+
*/
3754
public static HashMap<String, String> parseRawQuery(URI uri) {
3855
HashMap<String, String> querys = new HashMap<>();
3956
String rawQuery = uri.getRawQuery();
@@ -55,6 +72,12 @@ public static HashMap<String, String> parseRawQuery(URI uri) {
5572
return querys;
5673
}
5774

75+
/**
76+
* Returns the MIME-Type of an filename.
77+
*
78+
* @param fileExtension The file extension.
79+
* @return The MIME-Type.
80+
*/
5881
public static String getContentType(String fileExtension) {
5982
String ct = MIMETypes.get().get(fileExtension);
6083

src/express/MIMETypes.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
import java.util.HashMap;
44

5+
/**
6+
* @author Simon Reinisch
7+
*
8+
* Full list of mime-types to handle static served files.
9+
* List-link: https://www.freeformatter.com/mime-types-list.html
10+
*/
511
public class MIMETypes {
612

713
private final static HashMap<String, String> MIME = new HashMap<>();

src/express/cookie/Cookie.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33

44
import java.time.Instant;
55

6+
/**
7+
* @author Simon Reinisch
8+
* @implNote Can be reused
9+
*/
610
public class Cookie {
711

812
private String name, value;

src/express/cookie/SameSite.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package express.cookie;
22

3+
/**
4+
* @author Simon Reinisch
5+
*/
36
public enum SameSite {
47
STRICT, LAX
58
}

src/express/events/Action.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package express.events;
22

3+
/**
4+
* @author Simon Reinisch
5+
*/
36
public interface Action {
47
void action();
58
}

src/express/events/HttpRequest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
import express.http.Request;
44
import express.http.Response;
55

6+
/**
7+
* @author Simon Reinisch
8+
*/
69
public interface HttpRequest {
710

811
/**

0 commit comments

Comments
 (0)