Skip to content

Commit 2a9193b

Browse files
committed
Implement layer-schema
1 parent 19cf67e commit 2a9193b

File tree

6 files changed

+132
-77
lines changed

6 files changed

+132
-77
lines changed

src/examples/PortMiddleware.java

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,37 +8,37 @@
88
public class PortMiddleware implements HttpRequest, ExpressFilter {
99

1010

11-
/**
12-
* From interface HttpRequest, to handle the request.
13-
*
14-
* @param req - The request object
15-
* @param res - The response object
16-
*/
17-
@Override
18-
public void handle(Request req, Response res) {
19-
20-
// Get the port
21-
int port = req.getURI().getPort();
22-
23-
// Add the port to the request middleware map
24-
req.addMiddlewareContent(this, port);
25-
26-
/**
27-
* After that you can use this middleware by call:
28-
* app.use(new PortMiddleware());
29-
*
30-
* Than you can get the port with:
31-
* int port = (Integer) app.getMiddlewareContent("PortParser");
32-
*/
33-
}
34-
35-
/**
36-
* Defines the middleware.
37-
*
38-
* @return The middleware name.
39-
*/
40-
@Override
41-
public String getName() {
42-
return "PortParser";
43-
}
11+
/**
12+
* From interface HttpRequest, to handle the request.
13+
*
14+
* @param req - The request object
15+
* @param res - The response object
16+
*/
17+
@Override
18+
public void handle(Request req, Response res) {
19+
20+
// Get the port
21+
int port = req.getURI().getPort();
22+
23+
// Add the port to the request middleware map
24+
req.addMiddlewareContent(this, port);
25+
26+
/**
27+
* After that you can use this middleware by call:
28+
* app.use(new PortMiddleware());
29+
*
30+
* Than you can get the port with:
31+
* int port = (Integer) app.getMiddlewareContent("PortParser");
32+
*/
33+
}
34+
35+
/**
36+
* Defines the middleware.
37+
*
38+
* @return The middleware name.
39+
*/
40+
@Override
41+
public String getName() {
42+
return "PortParser";
43+
}
4444
}

src/express/Express.java

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@
44
import com.sun.net.httpserver.HttpServer;
55
import express.events.Action;
66
import express.events.HttpRequest;
7-
import express.expressfilter.ExpressFilterChain;
87
import express.expressfilter.ExpressFilterImpl;
98
import express.expressfilter.ExpressFilterTask;
109
import express.expressfilter.ExpressFilterWorker;
11-
import express.http.request.Request;
12-
import express.http.response.Response;
10+
import express.expressfilter.FilterLayerHandler;
1311
import express.middleware.ExpressMiddleware;
1412

1513
import java.io.IOException;
@@ -27,17 +25,28 @@
2725
*/
2826
public 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();

src/express/expressfilter/ExpressFilterImpl.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import express.http.response.Response;
77

88
import java.util.HashMap;
9-
import java.util.concurrent.ConcurrentHashMap;
109

1110
/**
1211
* @author Simon Reinisch
@@ -55,7 +54,7 @@ public void handle(Request req, Response res) {
5554

5655
// Save parameter to request object
5756
req.setParams(params);
58-
57+
5958
// Check parameter lsitener
6059
params.forEach((s, s2) -> {
6160
HttpRequest hreq = APP.getParameterListener().get(s);
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package express.expressfilter;
2+
3+
import com.sun.net.httpserver.HttpExchange;
4+
import com.sun.net.httpserver.HttpHandler;
5+
import express.events.HttpRequest;
6+
import express.http.request.Request;
7+
import express.http.response.Response;
8+
9+
import java.io.IOException;
10+
11+
public class FilterLayerHandler implements HttpHandler {
12+
13+
private ExpressFilterChain[] LEVELS;
14+
15+
{
16+
// Initialize layers
17+
LEVELS = new ExpressFilterChain[2];
18+
19+
for (int i = 0; i < LEVELS.length; i++) {
20+
LEVELS[i] = new ExpressFilterChain();
21+
}
22+
}
23+
24+
@Override
25+
public void handle(HttpExchange httpExchange) throws IOException {
26+
Request request = new Request(httpExchange);
27+
Response response = new Response(httpExchange);
28+
29+
// First fire all middlewares, then the normal request filter
30+
for (ExpressFilterChain chain : LEVELS) {
31+
chain.filter(request, response);
32+
33+
if (response.isClosed())
34+
return;
35+
}
36+
}
37+
38+
/**
39+
* Add an new handler for an specific handler layer.
40+
*
41+
* @param level The layer.
42+
* @param handler The handler, will be append to the top of the layer.
43+
*/
44+
public void add(int level, HttpRequest handler) {
45+
46+
if (level >= LEVELS.length)
47+
throw new IndexOutOfBoundsException("Out of bounds: " + level + " > " + LEVELS.length);
48+
if (level < 0)
49+
throw new IndexOutOfBoundsException("Cannot be under zero: " + level + " < 0");
50+
51+
LEVELS[level].add(handler);
52+
}
53+
54+
}

src/express/http/request/RequestUtils.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import com.sun.net.httpserver.Headers;
44
import express.http.Cookie;
55

6-
import java.io.IOException;
76
import java.io.UnsupportedEncodingException;
87
import java.net.URLDecoder;
98
import java.util.HashMap;
@@ -44,7 +43,7 @@ protected static HashMap<String, Cookie> parseCookies(Headers headers) {
4443
* @param rawQuery The raw query
4544
* @return An list with key-values which are encoded in UTF8.
4645
*/
47-
protected static HashMap<String, String> parseRawQuery(String rawQuery) {
46+
protected static HashMap<String, String> parseRawQuery(String rawQuery) {
4847
HashMap<String, String> querys = new HashMap<>();
4948

5049
if (rawQuery == null)

src/express/utils/Status.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,6 @@ public enum Status {
9797
this.description = description;
9898
}
9999

100-
public String getDescription() {
101-
return description;
102-
}
103-
104-
public int getCode() {
105-
return code;
106-
}
107-
108100
public static Status valueOf(int code) {
109101
switch (code) {
110102
case 100:
@@ -236,4 +228,12 @@ public static Status valueOf(int code) {
236228
}
237229
return null;
238230
}
231+
232+
public String getDescription() {
233+
return description;
234+
}
235+
236+
public int getCode() {
237+
return code;
238+
}
239239
}

0 commit comments

Comments
 (0)