forked from jooby-project/jooby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParamLookup.java
More file actions
146 lines (132 loc) · 3.2 KB
/
ParamLookup.java
File metadata and controls
146 lines (132 loc) · 3.2 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
/**
* Jooby https://jooby.io
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
* Copyright 2014 Edgar Espina
*/
package io.jooby;
import java.util.Optional;
/**
* Fluent interface allowing to conveniently search context parameters
* in multiple sources.
*
* <pre>{@code
* Value foo = ctx.lookup()
* .inQuery()
* .inPath()
* .get("foo");
* }</pre>
*
* @see Context#lookup()
* @see Context#lookup(String, ParamSource...)
*/
public interface ParamLookup {
/**
* Adds the specified source to the search locations.
*
* @param source The source to add.
* @return This instance.
*/
Stage in(ParamSource source);
/**
* Adds the path parameters to the search locations.
*
* @return This instance.
*/
default Stage inPath() {
return in(ParamSource.PATH);
}
/**
* Adds the header parameters to the search locations.
*
* @return This instance.
*/
default Stage inHeader() {
return in(ParamSource.HEADER);
}
/**
* Adds the cookie parameters to the search locations.
*
* @return This instance.
*/
default Stage inCookie() {
return in(ParamSource.COOKIE);
}
/**
* Adds the flash parameters to the search locations.
*
* @return This instance.
*/
default Stage inFlash() {
return in(ParamSource.FLASH);
}
/**
* Adds the session parameters to the search locations.
*
* @return This instance.
*/
default Stage inSession() {
return in(ParamSource.SESSION);
}
/**
* Adds the query parameters to the search locations.
*
* @return This instance.
*/
default Stage inQuery() {
return in(ParamSource.QUERY);
}
/**
* Adds the form parameters to the search locations.
*
* @return This instance.
*/
default Stage inForm() {
return in(ParamSource.FORM);
}
/**
* Adds the multipart parameters to the search locations.
*
* @return This instance.
*/
default Stage inMultipart() {
return in(ParamSource.MULTIPART);
}
/**
* Fluent interface allowing to conveniently search context parameters
* in multiple sources.
*
* <pre>{@code
* Value foo = ctx.lookup()
* .inQuery()
* .inPath()
* .get("foo");
* }</pre>
*
* @see Context#lookup()
* @see Context#lookup(String, ParamSource...)
*/
interface Stage extends ParamLookup {
/**
* Searches for a parameter in the specified sources, in the specified
* order, returning the first non-missing {@link Value}, or a 'missing'
* {@link Value} if none found.
*
* @param name The name of the parameter.
* @return The first non-missing {@link Value} or a {@link Value} representing
* a missing value if none found.
*/
Value get(String name);
/**
* Wraps the result of {@link #get(String)} in an {@link Optional} if the
* value is a {@link ValueNode} or returns an empty {@link Optional}
* otherwise.
*
* @param name The name of the parameter.
* @return An {@link Optional} wrapping the result of {@link #get(String)}
*/
default Optional<ValueNode> getNode(String name) {
return Optional.of(get(name))
.map(v -> v instanceof ValueNode ? (ValueNode) v : null);
}
}
}