Skip to content

Commit 072ead4

Browse files
committed
Change line indent to 4 spaces
1 parent b833498 commit 072ead4

32 files changed

+3249
-3261
lines changed

src/main/java/examples/AuthorizationExamples.java

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,19 @@
44
import express.http.request.Authorization;
55
import express.utils.Status;
66

7-
public class AuthorizationExamples
8-
{
9-
public static void main(String[] args)
10-
{
11-
Express app = new Express();
12-
13-
app.get("/", (req, res) -> {
14-
if(Authorization.validate(req, Authorization.validator("Basic", "123456789"))) {
15-
res.send("You are authorized!");
16-
} else {
17-
res.setStatus(Status._401);
18-
res.send();
19-
}
20-
});
21-
22-
app.listen(() -> System.out.println("Listening!"));
23-
}
7+
public class AuthorizationExamples {
8+
public static void main(String[] args) {
9+
Express app = new Express();
10+
11+
app.get("/", (req, res) -> {
12+
if (Authorization.validate(req, Authorization.validator("Basic", "123456789"))) {
13+
res.send("You are authorized!");
14+
} else {
15+
res.setStatus(Status._401);
16+
res.send();
17+
}
18+
});
19+
20+
app.listen(() -> System.out.println("Listening!"));
21+
}
2422
}

src/main/java/examples/Examples.java

Lines changed: 95 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -9,100 +9,100 @@
99

1010
public class Examples {
1111

12-
public static void main(String[] args) throws IOException {
13-
Express app = new Express();
14-
15-
app.get("/", (req, res) -> res.send("Hello World"))
16-
17-
.get("/posts", (req, res) -> {
18-
String page = req.getQuery("page"); // Contains '12'
19-
String from = req.getQuery("from"); // Contains 'John'
20-
res.send("Page: " + page + ", from: " + from); // Send: "Page: 12, from: John"
21-
})
22-
23-
.get("/posts/:user/:type", (req, res) -> {
24-
String user = req.getParam("user"); // Contains 'john'
25-
String type = req.getParam("type"); // Contains 'all'
26-
res.send("User: " + user + ", type: " + type); // Send: "User: john, type: all"
27-
})
28-
29-
.get("/setcookie", (req, res) -> {
30-
Cookie cookie = new Cookie("username", "john");
31-
res.setCookie(cookie);
32-
res.send("Cookie has been set!");
33-
})
34-
35-
.get("/showcookie", (req, res) -> {
36-
Cookie cookie = req.getCookie("username");
37-
String username = cookie.getValue();
38-
res.send("The username is: " + username); // Prints "The username is: john"
39-
})
40-
41-
.post("/register", (req, res) -> {
42-
String email = req.getFormQuery("email");
43-
String username = req.getFormQuery("username");
44-
// Process data
45-
46-
// Prints "E-Mail: [email protected], Username: john"
47-
res.send("E-Mail: " + email + ", Username: " + username);
48-
})
49-
50-
.get("/res", (req, res) -> {
51-
// res.send(); // Send empty response
52-
// res.send("Hello World"); // Send an string
53-
// res.send("chart.pdf"); // Send an file
54-
// res.setStatus(200); // Set the response status
55-
// res.getStatus(); // Returns the current response status
56-
// res.setCookie(new Cookie(...)); // Send an cookie
57-
// res.isClosed(); // Check if already something has been send to the client
58-
})
59-
60-
.get("/req/", (req, res) -> {
61-
// req.getURI(); // Request URI
62-
// req.getHost(); // Request host (mostly localhost)
63-
// req.getMethod(); // Request method (here GET)
64-
// req.getContentType(); // Request content type, is here null because it's an GET request
65-
// req.getBody(); // Request body inputstream
66-
// req.getUserAgent(); // Request user-agent
67-
// req.getParam("parameter"); // Returns an url parameter
68-
// req.getQuery("queryname"); // Returns an url query by key
69-
// req.getFormQuery("formqueryname"); // Returns an form input value
70-
// req.getFormQuerys(); // Returns all form querys
71-
// req.getCookie("user"); // Returns an cookie by name
72-
// req.getCookies(); // Returns all cookies
73-
// req.hasAuthorization(); // Check if the request contains an authorization header
74-
// req.getAuthorization(); // Returns the authorization header
75-
// req.getMiddlewareContent("name"); // Returns data from middleware
76-
// req.pipe(new OutputStream() {...}); // Pipe the body to an outputstream
77-
})
78-
79-
.use(Middleware.cookieSession("f3v4", 9000))
80-
81-
.get("/session", (req, res) -> {
82-
83-
/*
84-
* CookieSession named its data "SessionCookie" which is
85-
* an SessionCookie so we can Cast it.
86-
*/
87-
SessionCookie sessionCookie = (SessionCookie) req.getMiddlewareContent("SessionCookie");
88-
int count;
89-
90-
// Check if the data is null, we want to implement an simple counter
91-
if (sessionCookie.getData() == null) {
92-
93-
// Set the default data to 1 (first request with this session cookie)
94-
count = (int) sessionCookie.setData(1);
95-
} else {
96-
97-
// Now we know that the cookie has an integer as data property, increase it
98-
count = (int) sessionCookie.setData((int) sessionCookie.getData() + 1);
99-
}
100-
101-
// Send an info message
102-
res.send("You take use of your session cookie " + count + " times.");
103-
})
104-
105-
.listen(() -> System.out.println("Express is listening!"));
106-
}
12+
public static void main(String[] args) throws IOException {
13+
Express app = new Express();
14+
15+
app.get("/", (req, res) -> res.send("Hello World"))
16+
17+
.get("/posts", (req, res) -> {
18+
String page = req.getQuery("page"); // Contains '12'
19+
String from = req.getQuery("from"); // Contains 'John'
20+
res.send("Page: " + page + ", from: " + from); // Send: "Page: 12, from: John"
21+
})
22+
23+
.get("/posts/:user/:type", (req, res) -> {
24+
String user = req.getParam("user"); // Contains 'john'
25+
String type = req.getParam("type"); // Contains 'all'
26+
res.send("User: " + user + ", type: " + type); // Send: "User: john, type: all"
27+
})
28+
29+
.get("/setcookie", (req, res) -> {
30+
Cookie cookie = new Cookie("username", "john");
31+
res.setCookie(cookie);
32+
res.send("Cookie has been set!");
33+
})
34+
35+
.get("/showcookie", (req, res) -> {
36+
Cookie cookie = req.getCookie("username");
37+
String username = cookie.getValue();
38+
res.send("The username is: " + username); // Prints "The username is: john"
39+
})
40+
41+
.post("/register", (req, res) -> {
42+
String email = req.getFormQuery("email");
43+
String username = req.getFormQuery("username");
44+
// Process data
45+
46+
// Prints "E-Mail: [email protected], Username: john"
47+
res.send("E-Mail: " + email + ", Username: " + username);
48+
})
49+
50+
.get("/res", (req, res) -> {
51+
// res.send(); // Send empty response
52+
// res.send("Hello World"); // Send an string
53+
// res.send("chart.pdf"); // Send an file
54+
// res.setStatus(200); // Set the response status
55+
// res.getStatus(); // Returns the current response status
56+
// res.setCookie(new Cookie(...)); // Send an cookie
57+
// res.isClosed(); // Check if already something has been send to the client
58+
})
59+
60+
.get("/req/", (req, res) -> {
61+
// req.getURI(); // Request URI
62+
// req.getHost(); // Request host (mostly localhost)
63+
// req.getMethod(); // Request method (here GET)
64+
// req.getContentType(); // Request content type, is here null because it's an GET request
65+
// req.getBody(); // Request body inputstream
66+
// req.getUserAgent(); // Request user-agent
67+
// req.getParam("parameter"); // Returns an url parameter
68+
// req.getQuery("queryname"); // Returns an url query by key
69+
// req.getFormQuery("formqueryname"); // Returns an form input value
70+
// req.getFormQuerys(); // Returns all form querys
71+
// req.getCookie("user"); // Returns an cookie by name
72+
// req.getCookies(); // Returns all cookies
73+
// req.hasAuthorization(); // Check if the request contains an authorization header
74+
// req.getAuthorization(); // Returns the authorization header
75+
// req.getMiddlewareContent("name"); // Returns data from middleware
76+
// req.pipe(new OutputStream() {...}); // Pipe the body to an outputstream
77+
})
78+
79+
.use(Middleware.cookieSession("f3v4", 9000))
80+
81+
.get("/session", (req, res) -> {
82+
83+
/*
84+
* CookieSession named its data "SessionCookie" which is
85+
* an SessionCookie so we can Cast it.
86+
*/
87+
SessionCookie sessionCookie = (SessionCookie) req.getMiddlewareContent("SessionCookie");
88+
int count;
89+
90+
// Check if the data is null, we want to implement an simple counter
91+
if (sessionCookie.getData() == null) {
92+
93+
// Set the default data to 1 (first request with this session cookie)
94+
count = (int) sessionCookie.setData(1);
95+
} else {
96+
97+
// Now we know that the cookie has an integer as data property, increase it
98+
count = (int) sessionCookie.setData((int) sessionCookie.getData() + 1);
99+
}
100+
101+
// Send an info message
102+
res.send("You take use of your session cookie " + count + " times.");
103+
})
104+
105+
.listen(() -> System.out.println("Express is listening!"));
106+
}
107107

108108
}

src/main/java/examples/PortMiddleware.java

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,37 +8,37 @@
88
public class PortMiddleware implements HttpRequestHandler, Filter {
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());
11+
/**
12+
* From interface HttpRequest, to handle the request.
2913
*
30-
* Than you can get the port with:
31-
* int port = (Integer) app.getMiddlewareContent("PortParser");
14+
* @param req - The request object
15+
* @param res - The response object
3216
*/
33-
}
34-
35-
/**
36-
* Defines the middleware.
37-
*
38-
* @return The middleware name.
39-
*/
40-
@Override
41-
public String getName() {
42-
return "PortParser";
43-
}
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/main/java/examples/Routing.java

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,28 @@
55

66
public class Routing {
77

8-
public static void main(String[] args) throws Throwable {
9-
Express app = new Express();
10-
11-
// Define router for index sites
12-
ExpressRouter indexRouter = new ExpressRouter();
13-
indexRouter.get("/", (req, res) -> res.send("Hello World!"));
14-
indexRouter.get("/index", (req, res) -> res.send("Index"));
15-
indexRouter.get("/about", (req, res) -> res.send("About"));
16-
17-
// Define router for user pages
18-
ExpressRouter userRouter = new ExpressRouter();
19-
userRouter.get("/", (req, res) -> res.send("User Page"));
20-
userRouter.get("/login", (req, res) -> res.send("User Login"));
21-
userRouter.get("/register", (req, res) -> res.send("User Register"));
22-
userRouter.get("/:username", (req, res) -> res.send("You want to see: " + req.getParam("username")));
23-
24-
// Add router and set root pathsl
25-
app.use("/", indexRouter);
26-
app.use("/user", userRouter);
27-
28-
// Start server
29-
app.listen();
30-
}
8+
public static void main(String[] args) throws Throwable {
9+
Express app = new Express();
10+
11+
// Define router for index sites
12+
ExpressRouter indexRouter = new ExpressRouter();
13+
indexRouter.get("/", (req, res) -> res.send("Hello World!"));
14+
indexRouter.get("/index", (req, res) -> res.send("Index"));
15+
indexRouter.get("/about", (req, res) -> res.send("About"));
16+
17+
// Define router for user pages
18+
ExpressRouter userRouter = new ExpressRouter();
19+
userRouter.get("/", (req, res) -> res.send("User Page"));
20+
userRouter.get("/login", (req, res) -> res.send("User Login"));
21+
userRouter.get("/register", (req, res) -> res.send("User Register"));
22+
userRouter.get("/:username", (req, res) -> res.send("You want to see: " + req.getParam("username")));
23+
24+
// Add router and set root pathsl
25+
app.use("/", indexRouter);
26+
app.use("/user", userRouter);
27+
28+
// Start server
29+
app.listen();
30+
}
3131

3232
}

0 commit comments

Comments
 (0)