Skip to content

Commit 7ac7cbf

Browse files
committed
Refactor existing middleware
1 parent f665f94 commit 7ac7cbf

File tree

8 files changed

+50
-33
lines changed

8 files changed

+50
-33
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22

33

4+
45
![Java Express Logo](https://image.ibb.co/mCdxtm/java_express.png)
56

67
Small clone of the node-js express framework written in pure Java 8.
@@ -217,21 +218,21 @@ No we can, as we learned above, include it with:
217218
app.use(new PortMiddleware());
218219
```
219220
## Existing Middlewares
220-
There are already some basic middlewares included:
221+
There are already some basic middlewares included, you can access these via static methods provided from `Express`.
221222

222223
#### Static File serving
223224
If you want to allocate some files, like js-librarys or css files you can use the [static](https://github.com/Simonwep/java-express/blob/master/src/express/middleware/Static.java) middleware. But you can also provide other files like mp4 etc.
224225
Example:
225226
```java
226-
app.use(new Static("examplepath\\test_statics"));
227+
app.use(Express.statics("examplepath\\test_statics"));
227228
```
228229
Now you can access every files in the `test_statics` over the root adress `\`.
229230
#### Cookie Session
230231
Java Express also includes an simple cookie-session middleware:
231232
```java
232233
// You should use an meaningless cookie name for serveral security reasons, here f3v4.
233234
// Also you can specify the maximum age of the cookie from the creation date, here 3000.
234-
app.use(new CookieSession("f3v4", 9000));
235+
app.use(Express.cookieSession("f3v4", 9000));
235236
```
236237
To use a session cookie we need to get the data from the middleware which is actually an `SessionCookie`:
237238
```java

src/examples/Examples.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
import express.Express;
44
import express.http.cookie.Cookie;
5-
import express.middleware.CookieSession;
6-
import express.middleware.SessionCookie;
5+
import express.http.cookie.SessionCookie;
76

87
import java.io.IOException;
98

@@ -78,7 +77,7 @@ public static void main(String[] args) throws IOException {
7877
// req.pipe(new OutputStream() {...}); // Pipe the body to an outputstream
7978
});
8079

81-
app.use(new CookieSession("f3v4", 9000));
80+
app.use(Express.cookieSession("f3v4", 9000));
8281

8382
app.get("/session", (req, res) -> {
8483

src/express/Express.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import express.expressfilter.ExpressFilterWorker;
1010
import express.http.Request;
1111
import express.http.Response;
12+
import express.middleware.ExpressMiddleware;
1213

1314
import java.io.IOException;
1415
import java.net.InetSocketAddress;
@@ -20,7 +21,7 @@
2021
* <p>
2122
* An NodeJS like clone written in Java.
2223
*/
23-
public class Express {
24+
public class Express extends ExpressMiddleware {
2425

2526
private final ArrayList<ExpressFilterWorker> WORKER = new ArrayList<>();
2627
private final ExpressFilterChain MIDDLEWARE_CHAIN = new ExpressFilterChain();

src/express/http/Request.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public Request(HttpExchange exchange) {
6464
this.AUTH = HEADERS.get("Authorization") == null ? null : new Authorization(HEADERS.get("Authorization").get(0));
6565

6666
// Check if the request contains x-www-form-urlencoded form data
67-
this.FORM_QUERYS = CONTENT_TYPE.equals("application/x-www-form-urlencoded")
67+
this.FORM_QUERYS = CONTENT_TYPE.startsWith("application/x-www-form-urlencoded")
6868
? RequestUtils.parseRawQuery(ExpressUtils.streamToString(BODY))
6969
: new HashMap<>();
7070

src/express/middleware/SessionCookie.java renamed to src/express/http/cookie/SessionCookie.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package express.middleware;
1+
package express.http.cookie;
22

33
/**
44
* @author Simon Reinisch

src/express/middleware/CookieSession.java renamed to src/express/middleware/ExpressCookieSession.java

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,19 @@
77
import express.http.Request;
88
import express.http.Response;
99
import express.http.cookie.Cookie;
10+
import express.http.cookie.SessionCookie;
1011

1112
import java.util.concurrent.ConcurrentHashMap;
1213

13-
public class CookieSession implements HttpRequest, ExpressFilter, ExpressFilterTask {
14+
final class ExpressCookieSession implements HttpRequest, ExpressFilter, ExpressFilterTask {
1415

1516
private final static String MIDDLEWARE_NAME = "SessionCookie";
1617

1718
private final ConcurrentHashMap<String, SessionCookie> COOKIES = new ConcurrentHashMap<>();
1819
private final String COOKIE_NAME;
1920
private final long MAX_AGE;
2021

21-
/**
22-
* Create an new cookie-session middleware.
23-
* You can access and edit to session-cookie data via request.getMiddlewareContent('SessionCookie').
24-
*
25-
* @param cookieName An name for the session-cookie, it's recommed to use NOT SID for security reasons
26-
* @param maxAge An maxage for the cookie
27-
*/
28-
public CookieSession(String cookieName, long maxAge) {
22+
ExpressCookieSession(String cookieName, long maxAge) {
2923
this.COOKIE_NAME = cookieName;
3024
this.MAX_AGE = maxAge;
3125
}
@@ -62,12 +56,12 @@ public void onStart() {
6256

6357
@Override
6458
public void onStop() {
65-
// Nothing to do
59+
COOKIES.clear();
6660
}
6761

6862
@Override
6963
public long getDelay() {
70-
return 15000; // 1min
64+
return 60000;
7165
}
7266

7367
@Override
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package express.middleware;
2+
3+
/**
4+
* @author Simon Reinisch
5+
* <p>
6+
* Class which serves middleware
7+
*/
8+
public abstract class ExpressMiddleware{
9+
10+
/**
11+
* Create an new cookie-session middleware.
12+
* You can access and edit to session-cookie data via request.getMiddlewareContent('SessionCookie').
13+
*
14+
* @param cookieName An name for the session-cookie, it's recommed to use NOT SID for security reasons
15+
* @param maxAge An maxage for the cookie
16+
*/
17+
public static ExpressCookieSession cookieSession(String cookieName, long maxAge) {
18+
return new ExpressCookieSession(cookieName, maxAge);
19+
}
20+
21+
/**
22+
* This class serves an entire folder which can contains static file for your
23+
* web application, it automatically detect the content type and will send it to
24+
* the Client.
25+
* <p>
26+
* To use it simply put it in the <code>app.use()</code> method!
27+
*
28+
* @param directoryPath The root directory
29+
*/
30+
public static ExpressStatic statics(String directoryPath) {
31+
return new ExpressStatic(directoryPath);
32+
}
33+
}

src/express/middleware/Static.java renamed to src/express/middleware/ExpressStatic.java

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package express.middleware;
22

3-
import express.ExpressUtils;
43
import express.events.HttpRequest;
54
import express.http.Request;
65
import express.http.Response;
7-
import express.utils.MIMETypes;
86

97
import java.io.File;
108

@@ -13,20 +11,11 @@
1311
* <p>
1412
* An Express-Middleware to serve static files.
1513
*/
16-
public class Static implements HttpRequest {
14+
final class ExpressStatic implements HttpRequest {
1715

1816
private final String PATH;
1917

20-
/**
21-
* This class serves an entire folder which can contains static file for your
22-
* web application, it automatically detect the content type and will send it to
23-
* the Client.
24-
* <p>
25-
* To use it simply put it in the <code>app.use()</code> method!
26-
*
27-
* @param directoryPath The root directory
28-
*/
29-
public Static(String directoryPath) {
18+
ExpressStatic(String directoryPath) {
3019
this.PATH = directoryPath;
3120
}
3221

0 commit comments

Comments
 (0)