|
| 1 | +/** |
| 2 | + * Jooby https://jooby.io |
| 3 | + * Apache License Version 2.0 https://jooby.io/LICENSE.txt |
| 4 | + * Copyright 2014 Edgar Espina |
| 5 | + */ |
| 6 | +package io.jooby; |
| 7 | + |
| 8 | +import java.util.Optional; |
| 9 | + |
| 10 | +/** |
| 11 | + * Fluent interface allowing to conveniently search context parameters |
| 12 | + * in multiple sources. |
| 13 | + * |
| 14 | + * <pre>{@code |
| 15 | + * Value foo = ctx.lookup() |
| 16 | + * .inQuery() |
| 17 | + * .inPath() |
| 18 | + * .get("foo"); |
| 19 | + * }</pre> |
| 20 | + * |
| 21 | + * @see Context#lookup() |
| 22 | + * @see Context#lookup(String, ParamSource...) |
| 23 | + */ |
| 24 | +public interface ParamLookup { |
| 25 | + |
| 26 | + /** |
| 27 | + * Adds the specified source to the search locations. |
| 28 | + * |
| 29 | + * @param source The source to add. |
| 30 | + * @return This instance. |
| 31 | + */ |
| 32 | + Stage in(ParamSource source); |
| 33 | + |
| 34 | + /** |
| 35 | + * Adds the path parameters to the search locations. |
| 36 | + * |
| 37 | + * @return This instance. |
| 38 | + */ |
| 39 | + default Stage inPath() { |
| 40 | + return in(ParamSource.PATH); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Adds the header parameters to the search locations. |
| 45 | + * |
| 46 | + * @return This instance. |
| 47 | + */ |
| 48 | + default Stage inHeader() { |
| 49 | + return in(ParamSource.HEADER); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Adds the cookie parameters to the search locations. |
| 54 | + * |
| 55 | + * @return This instance. |
| 56 | + */ |
| 57 | + default Stage inCookie() { |
| 58 | + return in(ParamSource.COOKIE); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Adds the flash parameters to the search locations. |
| 63 | + * |
| 64 | + * @return This instance. |
| 65 | + */ |
| 66 | + default Stage inFlash() { |
| 67 | + return in(ParamSource.FLASH); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Adds the session parameters to the search locations. |
| 72 | + * |
| 73 | + * @return This instance. |
| 74 | + */ |
| 75 | + default Stage inSession() { |
| 76 | + return in(ParamSource.SESSION); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Adds the query parameters to the search locations. |
| 81 | + * |
| 82 | + * @return This instance. |
| 83 | + */ |
| 84 | + default Stage inQuery() { |
| 85 | + return in(ParamSource.QUERY); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Adds the form parameters to the search locations. |
| 90 | + * |
| 91 | + * @return This instance. |
| 92 | + */ |
| 93 | + default Stage inForm() { |
| 94 | + return in(ParamSource.FORM); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Adds the multipart parameters to the search locations. |
| 99 | + * |
| 100 | + * @return This instance. |
| 101 | + */ |
| 102 | + default Stage inMultipart() { |
| 103 | + return in(ParamSource.MULTIPART); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Fluent interface allowing to conveniently search context parameters |
| 108 | + * in multiple sources. |
| 109 | + * |
| 110 | + * <pre>{@code |
| 111 | + * Value foo = ctx.lookup() |
| 112 | + * .inQuery() |
| 113 | + * .inPath() |
| 114 | + * .get("foo"); |
| 115 | + * }</pre> |
| 116 | + * |
| 117 | + * @see Context#lookup() |
| 118 | + * @see Context#lookup(String, ParamSource...) |
| 119 | + */ |
| 120 | + interface Stage extends ParamLookup { |
| 121 | + |
| 122 | + /** |
| 123 | + * Searches for a parameter in the specified sources, in the specified |
| 124 | + * order, returning the first non-missing {@link Value}, or a 'missing' |
| 125 | + * {@link Value} if none found. |
| 126 | + * |
| 127 | + * @param name The name of the parameter. |
| 128 | + * @return The first non-missing {@link Value} or a {@link Value} representing |
| 129 | + * a missing value if none found. |
| 130 | + */ |
| 131 | + Value get(String name); |
| 132 | + |
| 133 | + /** |
| 134 | + * Wraps the result of {@link #get(String)} in an {@link Optional} if the |
| 135 | + * value is a {@link ValueNode} or returns an empty {@link Optional} |
| 136 | + * otherwise. |
| 137 | + * |
| 138 | + * @param name The name of the parameter. |
| 139 | + * @return An {@link Optional} wrapping the result of {@link #get(String)} |
| 140 | + */ |
| 141 | + default Optional<ValueNode> getNode(String name) { |
| 142 | + return Optional.of(get(name)) |
| 143 | + .map(v -> v instanceof ValueNode ? (ValueNode) v : null); |
| 144 | + } |
| 145 | + } |
| 146 | +} |
0 commit comments