-
Notifications
You must be signed in to change notification settings - Fork 10
/
RESTExample.java
143 lines (127 loc) · 5.69 KB
/
RESTExample.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package examples.rest;
import com.vtence.molecule.Response;
import com.vtence.molecule.WebServer;
import com.vtence.molecule.lib.TextBody;
import com.vtence.molecule.middlewares.HttpMethodOverride;
import com.vtence.molecule.routing.Routes;
import java.io.IOException;
import java.util.Map;
import java.util.TreeMap;
import static com.vtence.molecule.http.HttpStatus.CREATED;
import static com.vtence.molecule.http.HttpStatus.NOT_FOUND;
/**
* <p>
* This example demonstrates RESTful routing. It shows how to define routes, map routes to HTTP verbs
* and use route patterns containing named parameters.
* </p>
* <p>
* We model a simple music library, with routes to query albums, create new albums,
* update existing albums and finally delete albums. For this simple example we serve plain text responses.
* </p>
* <p>
* We use the {@link HttpMethodOverride} middleware to support PUT and DELETE methods
* using a simple POST with a special <code>_method</code> request parameter.
* </p>
*/
public class RESTExample {
// Our in-memory music library
private final Map<Integer, Album> albums = new TreeMap<>();
// A simple sequence for generating new albums identifiers
private final Sequence sequence = new Sequence();
public void run(WebServer server) throws IOException {
// Support HTTP method override via the _method request parameter
// PUT and DELETE can be done using a POST providing there is a _method
// parameter that describes the actual verb
server.add(new HttpMethodOverride())
.route((new Routes() {{
// GET to /albums returns the entire list of albums
get("/albums").to(request -> {
// We server a simple plain text response
TextBody body = new TextBody();
for (int id : albums.keySet()) {
Album album = albums.get(id);
body.append(String.format("%d: %s\n", id, album.info()));
}
if (body.text().isEmpty()) {
body.append("Your music library is empty");
}
return Response.ok()
.done(body);
});
// POST to /albums creates a new album
post("/albums").to(request -> {
int id = sequence.next();
Album album = new Album(request.parameter("title"), request.parameter("artist"));
albums.put(id, album);
return Response.of(CREATED)
.done(album.info());
});
// GET to /albums/<id> fetches an existing album
get("/albums/:id").to(request -> {
int id = Integer.parseInt(request.parameter("id"));
if (albums.containsKey(id)) {
Album album = albums.get(id);
return Response.ok()
.done(album.info());
} else {
return Response.of(NOT_FOUND)
.done();
}
});
// PUT to /albums/<id> updates an existing album
// It can be done with either a PUT or a POST with parameter _method=PUT
put("/albums/:id").to(request -> {
int id = Integer.parseInt(request.parameter("id"));
Album album = albums.get(id);
if (album != null) {
String title = request.parameter("title");
if (title != null) album.title = title;
String artist = request.parameter("artist");
if (artist != null) album.artist = artist;
return Response.ok()
.done(album.info());
} else {
return Response.of(NOT_FOUND)
.done();
}
});
// DELETE to /albums/<id> deletes an existing album
// It can be done with either a DELETE or a POST with parameter _method=DELETE
delete("/albums/:id").to(request -> {
int id = Integer.parseInt(request.parameter("id"));
Album album = albums.remove(id);
if (album != null) {
return Response.ok()
.done(album.info());
} else {
return Response.of(NOT_FOUND)
.done();
}
});
}}));
}
public static class Sequence {
private int next = 1;
public int next() {
return next++;
}
}
public static class Album {
public String title;
public String artist;
public Album(String title, String artist) {
this.title = title;
this.artist = artist;
}
public String info() {
return String.format("Title: %s, Artist: %s", title, artist);
}
}
public static void main(String[] args) throws IOException {
RESTExample example = new RESTExample();
// Run the default web server
WebServer webServer = WebServer.create();
example.run(webServer);
System.out.println("Access at " + webServer.uri() + "/albums");
}
}