|
20 | 20 | import java.nio.charset.StandardCharsets; |
21 | 21 | import java.nio.file.Path; |
22 | 22 | import java.time.Instant; |
23 | | -import java.util.Date; |
24 | | -import java.util.List; |
25 | | -import java.util.Map; |
26 | | -import java.util.Objects; |
27 | | -import java.util.Optional; |
| 23 | +import java.util.*; |
28 | 24 |
|
29 | 25 | import org.slf4j.Logger; |
30 | 26 |
|
31 | 27 | import edu.umd.cs.findbugs.annotations.NonNull; |
32 | 28 | import edu.umd.cs.findbugs.annotations.Nullable; |
| 29 | +import io.jooby.buffer.BufferedOutputFactory; |
33 | 30 | import io.jooby.exception.RegistryException; |
34 | | -import io.jooby.internal.HashValue; |
35 | | -import io.jooby.internal.MissingValue; |
36 | | -import io.jooby.internal.SingleValue; |
37 | | -import io.jooby.internal.UrlParser; |
38 | | -import io.jooby.output.BufferedOutputFactory; |
| 31 | +import io.jooby.internal.*; |
39 | 32 | import io.jooby.value.Value; |
40 | 33 | import io.jooby.value.ValueFactory; |
41 | 34 |
|
@@ -189,6 +182,49 @@ default Value cookie(@NonNull String name) { |
189 | 182 | return value == null ? Value.missing(name) : Value.value(getValueFactory(), name, value); |
190 | 183 | } |
191 | 184 |
|
| 185 | + /** |
| 186 | + * Returns a {@link ParamLookup} instance which is a fluent interface covering the functionality |
| 187 | + * of the {@link #lookup(String, ParamSource...)} method. |
| 188 | + * |
| 189 | + * <pre>{@code |
| 190 | + * Value foo = ctx.lookup() |
| 191 | + * .inQuery() |
| 192 | + * .inPath() |
| 193 | + * .get("foo"); |
| 194 | + * }</pre> |
| 195 | + * |
| 196 | + * @return A {@link ParamLookup} instance. |
| 197 | + * @see ParamLookup |
| 198 | + * @see #lookup(String, ParamSource...) |
| 199 | + */ |
| 200 | + default ParamLookup lookup() { |
| 201 | + return new ParamLookupImpl(this); |
| 202 | + } |
| 203 | + |
| 204 | + /** |
| 205 | + * Searches for a parameter in the specified sources, in the specified order, returning the first |
| 206 | + * non-missing {@link Value}, or a 'missing' {@link Value} if none found. |
| 207 | + * |
| 208 | + * <p>At least one {@link ParamSource} must be specified. |
| 209 | + * |
| 210 | + * @param name The name of the parameter. |
| 211 | + * @param sources Sources to search in. |
| 212 | + * @return The first non-missing {@link Value} or a {@link Value} representing a missing value if |
| 213 | + * none found. |
| 214 | + * @throws IllegalArgumentException If no {@link ParamSource}s are specified. |
| 215 | + */ |
| 216 | + default Value lookup(@NonNull String name, ParamSource... sources) { |
| 217 | + if (sources.length == 0) { |
| 218 | + throw new IllegalArgumentException("No parameter sources were specified."); |
| 219 | + } |
| 220 | + |
| 221 | + return Arrays.stream(sources) |
| 222 | + .map(source -> source.provider.apply(this, name)) |
| 223 | + .filter(value -> !value.isMissing()) |
| 224 | + .findFirst() |
| 225 | + .orElseGet(() -> Value.missing(name)); |
| 226 | + } |
| 227 | + |
192 | 228 | @Override |
193 | 229 | default Value path(@NonNull String name) { |
194 | 230 | String value = pathMap().get(name); |
|
0 commit comments