forked from perwendel/spark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME
357 lines (286 loc) · 10.5 KB
/
README
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
Spark - a Sinatra inspired micro web framework
----------------------------------------------
Examples can also be viewed on: http://code.google.com/p/spark-java/
Getting started:
import static spark.Spark.*;
import spark.*;
public class HelloWorld {
public static void main(String[] args) {
get(new Route("/hello") {
@Override
public Object handle(Request request, Response response) {
return "Hello World!";
}
});
}
}
View at: http://localhost:4567/hello
More documentation is on the way!
Check out and try the examples in the source code.
You can also check out the javadoc. After getting the source from
github run:
mvn javadoc:javadoc
The result is put in /target/site/apidocs
Examples:
---------
Simple example showing some basic functionality:
import static spark.Spark.*;
import spark.Request;
import spark.Response;
import spark.Route;
/**
* A simple example just showing some basic functionality
*/
public class SimpleExample {
public static void main(String[] args) {
// setPort(5678); <- Uncomment this if you wan't spark to listen on a port different than 4567.
get(new Route("/hello") {
@Override
public Object handle(Request request, Response response) {
return "Hello World!";
}
});
post(new Route("/hello") {
@Override
public Object handle(Request request, Response response) {
return "Hello World: " + request.body();
}
});
get(new Route("/private") {
@Override
public Object handle(Request request, Response response) {
response.status(401);
return "Go Away!!!";
}
});
get(new Route("/users/:name") {
@Override
public Object handle(Request request, Response response) {
return "Selected user: " + request.params(":name");
}
});
get(new Route("/news/:section") {
@Override
public Object handle(Request request, Response response) {
response.type("text/xml");
return "<?xml version=\"1.0\" encoding=\"UTF-8\"?><news>" + request.params("section") + "</news>";
}
});
get(new Route("/protected") {
@Override
public Object handle(Request request, Response response) {
halt(403, "I don't think so!!!");
return null;
}
});
get(new Route("/redirect") {
@Override
public Object handle(Request request, Response response) {
response.redirect("/news/world");
return null;
}
});
get(new Route("/") {
@Override
public Object handle(Request request, Response response) {
return "root";
}
});
}
}
-------------------------------
A simple RESTful example showing howto create, get, update and delete book resources:
import static spark.Spark.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import spark.Request;
import spark.Response;
import spark.Route;
/**
* A simple RESTful example showing howto create, get, update and delete book resources.
*/
public class Books {
/**
* Map holding the books
*/
private static Map<String, Book> books = new HashMap<String, Book>();
public static void main(String[] args) {
// Creates a new book resource, will return the ID to the created resource
// author and title are sent as query parameters e.g. /books?author=Foo&title=Bar
post(new Route("/books") {
Random random = new Random();
@Override
public Object handle(Request request, Response response) {
String author = request.queryParams("author");
String title = request.queryParams("title");
Book book = new Book(author, title);
int id = random.nextInt(Integer.MAX_VALUE);
books.put(String.valueOf(id), book);
response.status(201); // 201 Created
return id;
}
});
// Gets the book resource for the provided id
get(new Route("/books/:id") {
@Override
public Object handle(Request request, Response response) {
Book book = books.get(request.params(":id"));
if (book != null) {
return "Title: " + book.getTitle() + ", Author: " + book.getAuthor();
} else {
response.status(404); // 404 Not found
return "Book not found";
}
}
});
// Updates the book resource for the provided id with new information
// author and title are sent as query parameters e.g. /books/<id>?author=Foo&title=Bar
put(new Route("/books/:id") {
@Override
public Object handle(Request request, Response response) {
String id = request.params(":id");
Book book = books.get(id);
if (book != null) {
String newAuthor = request.queryParams("author");
String newTitle = request.queryParams("title");
if (newAuthor != null) {
book.setAuthor(newAuthor);
}
if (newTitle != null) {
book.setTitle(newTitle);
}
return "Book with id '" + id + "' updated";
} else {
response.status(404); // 404 Not found
return "Book not found";
}
}
});
// Deletes the book resource for the provided id
delete(new Route("/books/:id") {
@Override
public Object handle(Request request, Response response) {
String id = request.params(":id");
Book book = books.remove(id);
if (book != null) {
return "Book with id '" + id + "' deleted";
} else {
response.status(404); // 404 Not found
return "Book not found";
}
}
});
// Gets all available book resources (id's)
get(new Route("/books") {
@Override
public Object handle(Request request, Response response) {
String ids = "";
for (String id : books.keySet()) {
ids += id + " ";
}
return ids;
}
});
}
}
---------------------------------
Example showing a very simple (and stupid) autentication filter that is executed before all other resources:
import static spark.Spark.*;
import java.util.HashMap;
import java.util.Map;
import spark.Filter;
import spark.Request;
import spark.Response;
import spark.Route;
/**
* Example showing a very simple (and stupid) autentication filter that is
* executed before all other resources.
*
* When requesting the resource with e.g.
* http://localhost:4567/hello?user=some&password=guy
* the filter will stop the execution and the client will get a 401 UNAUTHORIZED with the content 'You are not welcome here'
*
* When requesting the resource with e.g.
* http://localhost:4567/hello?user=foo&password=bar
* the filter will accept the request and the request will continue to the /hello route.
*
* Note: There is a second "before filter" that adds a header to the response
* Note: There is also an "after filter" that adds a header to the response
*/
public class FilterExample {
private static Map<String, String> usernamePasswords = new HashMap<String, String>();
public static void main(String[] args) {
usernamePasswords.put("foo", "bar");
usernamePasswords.put("admin", "admin");
before(new Filter() {
@Override
public void handle(Request request, Response response) {
String user = request.queryParams("user");
String password = request.queryParams("password");
String dbPassword = usernamePasswords.get(user);
if (!(password != null && password.equals(dbPassword))) {
halt(401, "You are not welcome here!!!");
}
}
});
before(new Filter("/hello") {
@Override
public void handle(Request request, Response response) {
response.header("Foo", "Set by second before filter");
}
});
get(new Route("/hello") {
@Override
public Object handle(Request request, Response response) {
return "Hello World!";
}
});
after(new Filter("/hello") {
@Override
public void handle(Request request, Response response) {
response.header("spark", "added by after-filter");
}
});
}
}
---------------------------------
Example showing how to use attributes:
import static spark.Spark.after;
import static spark.Spark.get;
import spark.Filter;
import spark.Request;
import spark.Response;
import spark.Route;
/**
* Example showing the use of attributes
*/
public class FilterExampleAttributes {
public static void main(String[] args) {
get(new Route("/hi") {
@Override
public Object handle(Request request, Response response) {
request.attribute("foo", "bar");
return null;
}
});
after(new Filter("/hi") {
@Override
public void handle(Request request, Response response) {
for (String attr : request.attributes()) {
System.out.println("attr: " + attr);
}
}
});
after(new Filter("/hi") {
@Override
public void handle(Request request, Response response) {
Object foo = request.attribute("foo");
response.body(asXml("foo", foo));
}
});
}
private static String asXml(String name, Object value) {
return "<?xml version=\"1.0\" encoding=\"UTF-8\"?><" + name +">" + value + "</"+ name + ">";
}
}