Repository navigation
Expand file tree
/
Copy pathParser.java
More file actions
481 lines (440 loc) · 13.6 KB
/
Copy pathParser.java
File metadata and controls
481 lines (440 loc) · 13.6 KB
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jooby;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.function.Function;
import org.jooby.internal.parser.BeanParser;
import com.google.inject.Key;
import com.google.inject.TypeLiteral;
/**
* Parse a request param (path, query, form) or body to something else.
*
* <h1>Registering a parser</h1>
* <p>
* There are two ways of registering a parser:
* </p>
*
* <ol>
* <li>Using the {@link Jooby#parser(Parser)} method</li>
* <li>From a Guice module:
*
* <pre>
* Multibinder<Parser> pcb = Multibinder
.newSetBinder(binder, Parser.class);
pcb.addBinding().to(MyParser.class);
* </pre>
* </li>
* </ol>
* Parsers are executed in the order they were registered. The first converter that resolved the
* type: wins!.
*
* <h2>Built-in parsers</h2>
* <p>
* These are the built-in parsers:
* </p>
* <ol>
* <li>Primitives and String: convert to int, double, char, string, etc...</li>
* <li>Enums (case-sensitive)</li>
* <li>{@link java.util.Date}: It parses a date using the <code>application.dateFormat</code>
* property.</li>
* <li>{@link java.time.LocalDate}: It parses a date using the <code>application.dateFormat</code>
* property.</li>
* <li>{@link java.util.Locale}</li>
* <li>Classes with a static method: <code>valueOf</code></li>
* <li>Classes with a static method: <code>fromName</code></li>
* <li>Classes with a static method: <code>fromString</code></li>
* <li>Classes with a public constructor with one <code>String</code> argument</li>
* <li>It is an Optional<T>, List<T>, Set<T> or SortedSet<T> where T
* satisfies one of previous rules</li>
* </ol>
*
* @author edgar
* @see Jooby#parser(Parser)
* @since 0.6.0
*/
public interface Parser {
/**
* A parser callback.
*
* @author edgar
*
* @param <T> Type of data to parse.
* @since 0.6.0
*/
interface Callback<T> {
/**
* Parse a raw value to something else.
*
* @param data Data to parse.
* @return A parsed value
* @throws Exception If something goes wrong.
*/
Object invoke(T data) throws Throwable;
}
/**
* Expose HTTP params from path, query, form url encoded or multipart request as a raw string.
*
* @author edgar
* @since 0.6.0
*/
interface ParamReference<T> extends Iterable<T> {
/**
* @return Descriptive type: parameter, header, cookie, etc...
*/
String type();
/**
* @return Parameter name.
*/
String name();
/**
* @return Return the first param or throw {@link Err} with a bad request code when missing.
*/
T first();
/**
* @return Return the last param or throw {@link Err} with a bad request code when missing.
*/
T last();
/**
* Get the param at the given index or throw {@link Err} with a bad request code when missing.
*
* @param index Param index.
* @return Param at the given index or throw {@link Err} with a bad request code when missing.
*/
T get(int index);
@Override
Iterator<T> iterator();
/**
* @return Number of values for this parameter.
*/
int size();
}
/**
* Expose the HTTP body as a series of bytes or text.
*
* @author edgar
* @since 0.6.0
*/
interface BodyReference {
/**
* Returns the HTTP body as a byte array.
*
* @return HTTP body as byte array.
* @throws IOException If reading fails.
*/
byte[] bytes() throws IOException;
/**
* Returns the HTTP body as text.
*
* @return HTTP body as text.
* @throws IOException If reading fails.
*/
String text() throws IOException;
/**
* @return Body length.
*/
long length();
/**
* Write the content to the given output stream. This method won't close the
* {@link OutputStream}.
*
* @param output An output stream.
* @throws Exception If write fails.
*/
void writeTo(final OutputStream output) throws Exception;
}
/**
* A parser can be executed against a simply HTTP param, a set of HTTP params, an file
* {@link Upload} or HTTP {@link BodyReference}.
*
* This class provides utility methods for selecting one of the previous source. It is possible to
* write a parser and apply it against multiple sources, like HTTP param and HTTP body.
*
* Here is an example that will parse text to an int, provided as a HTTP param or body:
*
* <pre>
* {
* parser((type, ctx) {@literal ->} {
* if (type.getRawType() == int.class) {
* return ctx
* .param(values {@literal ->} Integer.parseInt(values.get(0))
* .body(body {@literal ->} Integer.parseInt(body.text()));
* }
* return ctx.next();
* });
*
* get("/", req {@literal ->} {
* // use the param strategy
* return req.param("p").intValue();
* });
*
* post("/", req {@literal ->} {
* // use the body strategy
* return req.body().intValue();
* });
* }
* </pre>
*
* @author edgar
* @since 0.6.0
*/
interface Builder {
/**
* Add a HTTP body callback. The Callback will be executed when current context is bound to the
* HTTP body via {@link Request#body()}.
*
* If current {@link Context} isn't a HTTP body a call to {@link Context#next()} is made.
*
* @param callback A body parser callback.
* @return This builder.
*/
Builder body(Callback<BodyReference> callback);
/**
* Like {@link #body(Callback)} but it skip the callback if the requested type is an
* {@link Optional}.
*
* @param callback A body parser callback.
* @return This builder.
*/
Builder ifbody(Callback<BodyReference> callback);
/**
* Add a HTTP param callback. The Callback will be executed when current context is bound to a
* HTTP param via {@link Request#param(String)}.
*
* If current {@link Context} isn't a HTTP param a call to {@link Context#next()} is made.
*
* @param callback A param parser callback.
* @return This builder.
*/
Builder param(Callback<ParamReference<String>> callback);
/**
* Like {@link #param(Callback)} but it skip the callback if the requested type is an
* {@link Optional}.
*
* @param callback A param parser callback.
* @return This builder.
*/
Builder ifparam(Callback<ParamReference<String>> callback);
/**
* Add a HTTP params callback. The Callback will be executed when current context is bound to a
* HTTP params via {@link Request#params()}.
*
* If current {@link Context} isn't a HTTP params a call to {@link Context#next()} is made.
*
* @param callback A params parser callback.
* @return This builder.
*/
Builder params(Callback<Map<String, Mutant>> callback);
/**
* Like {@link #params(Callback)} but it skip the callback if the requested type is an
* {@link Optional}.
*
* @param callback A params parser callback.
* @return This builder.
*/
Builder ifparams(Callback<Map<String, Mutant>> callback);
/**
* Add a HTTP upload callback. The Callback will be executed when current context is bound to a
* HTTP upload via {@link Request#param(String)}.
*
* If current {@link Context} isn't a HTTP upload a call to {@link Context#next()} is made.
*
* @param callback A upload parser callback.
* @return This builder.
*/
Builder upload(Callback<ParamReference<Upload>> callback);
/**
* Like {@link #upload(Callback)} but it skip the callback if the requested type is an
* {@link Optional}.
*
* @param callback A upload parser callback.
* @return This builder.
*/
Builder ifupload(Callback<ParamReference<Upload>> callback);
}
/**
* Allows you to access to parsing strategies, content type view {@link #type()} and invoke next
* parser in the chain via {@link #next()} methods.
*
* @author edgar
* @since 0.6.0
*/
interface Context extends Builder {
/**
* Requires a service with the given type.
*
* @param type Service type.
* @param <T> Service type.
* @return A service.
*/
<T> T require(final Class<T> type);
/**
* Requires a service with the given type.
*
* @param type Service type.
* @param <T> Service type.
* @return A service.
*/
<T> T require(final TypeLiteral<T> type);
/**
* Requires a service with the given type.
*
* @param key Service key.
* @param <T> Service type.
* @return A service.
*/
<T> T require(final Key<T> key);
/**
* Content Type header, if current context was bind to a HTTP body via {@link Request#body()}.
* If current context was bind to a HTTP param, media type is set to <code>text/plain</code>.
*
* @return Current type.
*/
MediaType type();
/**
* Invoke next parser in the chain.
*
* @return A parsed value.
* @throws Exception An err with a 400 status.
*/
Object next() throws Throwable;
/**
* Invoke next parser in the chain and switch/change the target type we are looking for. Useful
* for generic containers classes, like collections or optional values.
*
* @param type A new type to use.
* @return A parsed value.
* @throws Exception An err with a 400 status.
*/
Object next(TypeLiteral<?> type) throws Throwable;
/**
* Invoke next parser in the chain and switch/change the target type we are looking for but also
* the current value. Useful for generic containers classes, like collections or optional
* values.
*
* @param type A new type to use.
* @param data Data to be parsed.
* @return A parsed value.
* @throws Exception An err with a 400 status.
*/
Object next(TypeLiteral<?> type, Object data) throws Throwable;
}
/** Utility function to handle empty values as {@link NoSuchElementException}. */
static Function<String, String> NOT_EMPTY = v -> {
if (v.length() == 0) {
throw new NoSuchElementException();
}
return v;
};
/**
* <p>
* Parse one or more values to the required type. If the parser doesn't support the required type
* a call to {@link Context#next(TypeLiteral, Object)} must be done.
* </p>
*
* Example:
*
* <pre>
* Parser converter = (type, ctx) {@literal ->} {
* if (type.getRawType() == MyType.class) {
* // convert to MyType
* return ctx.param(values {@literal ->} new MyType(values.get(0)));
* }
* // no luck! move next
* return next.next();
* }
* </pre>
*
* It's also possible to create generic/parameterized types too:
*
* <pre>
* public class MyContainerType<T> {}
*
* ParamConverter converter = (type, ctx) {@literal ->} {
* if (type.getRawType() == MyContainerType.class) {
* // Creates a new type from current generic type
* TypeLiterale<?> paramType = TypeLiteral
* .get(((ParameterizedType) toType.getType()).getActualTypeArguments()[0]);
*
* // Ask param converter to resolve the new/next type.
* Object result = next.next(paramType);
* return new MyType(result);
* }
* // no luck! move next
* return ctx.next();
* }
* </pre>
*
* @param type Requested type.
* @param ctx Execution context.
* @return A parsed value.
* @throws Throwable If conversion fails.
*/
Object parse(TypeLiteral<?> type, Context ctx) throws Throwable;
/**
* Overwrite the default bean parser with <code>empty/null</code> supports. The default bean
* parser doesn't allow <code>null</code>, so if a parameter is optional you must declare it as
* {@link Optional} otherwise parsing fails with a <code>404/500</code> status code.
*
* For example:
* <pre>{@code
*
* public class Book {
*
* public String title;
*
* public Date releaseDate;
*
* public String toString() {
* return title + ":" + releaseDate;
* }
* }
*
* {
* parser(Parser.bean(true));
*
* post("/", req -> {
* return req.params(Book.class).toString();
* });
* }
* }</pre>
*
* <p>
* With <code>/?title=Title&releaseDate=</code> prints <code>Title:null</code>.
* </p>
* <p>
* Now, same call with <code>lenient=false</code> results in <code>Bad Request: 400</code>
* because <code>releaseDate</code> if required and isn't present in the HTTP request.
* </p>
*
* <p>
* This feature is useful while submitting forms.
* </p>
*
* @param lenient Enabled null/empty supports while parsing HTTP params as Java Beans.
* @return A new bean parser.
*/
static Parser bean(final boolean lenient) {
return new BeanParser(lenient);
}
}