-
Notifications
You must be signed in to change notification settings - Fork 10
/
FlashExample.java
77 lines (67 loc) · 2.96 KB
/
FlashExample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package examples.flash;
import com.vtence.molecule.Response;
import com.vtence.molecule.WebServer;
import com.vtence.molecule.lib.FlashHash;
import com.vtence.molecule.middlewares.CookieSessionTracker;
import com.vtence.molecule.middlewares.Cookies;
import com.vtence.molecule.middlewares.Flash;
import com.vtence.molecule.routing.Routes;
import com.vtence.molecule.session.SessionPool;
import java.io.IOException;
/**
* <p>
* In this example we demonstrate how to use the {@link FlashHash}.
* <p>
* The flash is a special part of the session which is cleared with each request.
* This means that values stored in the flash will only be available in the next request.
* </p>
* <p>
* When a new user posts to /accounts to create an account, we add a message to the flash
* and redirect to /account. A get to /account reads the message from the flash and displays
* it to our user.
* </p>
*/
public class FlashExample {
public void run(WebServer server) throws IOException {
server.add(new Cookies())
// We'll use an in memory session pool in this example
.add(new CookieSessionTracker(SessionPool.secure()))
// We need the Flash middleware
.add(new Flash())
.route(new Routes() {{
// a post to /accounts creates a new account if email is not already taken
post("/accounts").to(request -> {
FlashHash flash = FlashHash.get(request);
String email = request.parameter("email");
if (email != null) {
// Add a flash notice if the account was successfully created
flash.notice("Account '" + email + "' successfully created");
} else {
// Add a flash alert if creation failed
flash.alert("An email is required");
}
return Response.redirect("/account").done();
});
// a get /account displays the flash message
get("/account").to(request -> {
FlashHash flash = FlashHash.get(request);
// Display either the notice or alert to the user ...
if (flash.notice() != null) {
return Response.ok().done(flash.notice());
}
if (flash.alert() != null) {
return Response.ok().done(flash.alert());
}
// ... or nothing
return Response.ok().done();
});
}});
}
public static void main(String[] args) throws IOException {
FlashExample example = new FlashExample();
// Run the default web server
WebServer webServer = WebServer.create();
example.run(webServer);
System.out.println("Access at " + webServer.uri());
}
}