|
19 | 19 | import reactor.core.publisher.Mono;
|
20 | 20 | import reactor.util.context.Context;
|
21 | 21 |
|
| 22 | +/** |
| 23 | + * Represents request-specific context that holds metadata related to the current request. It acts |
| 24 | + * as a wrapper around an existing {@link Context} and provides additional methods for accessing and |
| 25 | + * managing request-specific data such as headers, principal, method information, and path |
| 26 | + * variables. |
| 27 | + */ |
22 | 28 | public class RequestContext implements Context {
|
23 | 29 |
|
24 | 30 | private static final Logger LOGGER = LoggerFactory.getLogger(RequestContext.class);
|
25 | 31 |
|
26 | 32 | private final Context source;
|
27 | 33 |
|
| 34 | + /** Creates new {@code RequestContext} with an empty base context. */ |
28 | 35 | public RequestContext() {
|
29 | 36 | this(Context.empty());
|
30 | 37 | }
|
31 | 38 |
|
| 39 | + /** |
| 40 | + * Creates new {@code RequestContext} based on an existing context. |
| 41 | + * |
| 42 | + * @param source the source context to wrap |
| 43 | + */ |
32 | 44 | public RequestContext(Context source) {
|
33 |
| - this.source = source; |
| 45 | + this.source = Objects.requireNonNull(source, "source"); |
34 | 46 | }
|
35 | 47 |
|
36 | 48 | @Override
|
@@ -80,69 +92,159 @@ public Context delete(Object key) {
|
80 | 92 | return key == RequestContext.class ? source : source.delete(key);
|
81 | 93 | }
|
82 | 94 |
|
| 95 | + /** |
| 96 | + * Returns request headers. |
| 97 | + * |
| 98 | + * @return headers, or {@code null} if not set |
| 99 | + */ |
83 | 100 | public Map<String, String> headers() {
|
84 | 101 | return source.getOrDefault("headers", null);
|
85 | 102 | }
|
86 | 103 |
|
| 104 | + /** |
| 105 | + * Puts request headers to the context. |
| 106 | + * |
| 107 | + * @param headers headers |
| 108 | + * @return new {@code RequestContext} instance with updated headers |
| 109 | + */ |
87 | 110 | public RequestContext headers(Map<String, String> headers) {
|
88 | 111 | return put("headers", headers);
|
89 | 112 | }
|
90 | 113 |
|
| 114 | + /** |
| 115 | + * Returns request. |
| 116 | + * |
| 117 | + * @return request, or {@code null} if not set |
| 118 | + */ |
91 | 119 | public Object request() {
|
92 | 120 | return source.getOrDefault("request", null);
|
93 | 121 | }
|
94 | 122 |
|
| 123 | + /** |
| 124 | + * Puts request to the context. |
| 125 | + * |
| 126 | + * @param request request |
| 127 | + * @return new {@code RequestContext} instance with updated request |
| 128 | + */ |
95 | 129 | public RequestContext request(Object request) {
|
96 | 130 | return put("request", request);
|
97 | 131 | }
|
98 | 132 |
|
| 133 | + /** |
| 134 | + * Returns specific request header by name. |
| 135 | + * |
| 136 | + * @param name header name |
| 137 | + * @return header value, or {@code null} if not found |
| 138 | + */ |
99 | 139 | public String header(String name) {
|
100 | 140 | final var headers = headers();
|
101 | 141 | return headers != null ? headers.get(name) : null;
|
102 | 142 | }
|
103 | 143 |
|
| 144 | + /** |
| 145 | + * Returns request method from headers. |
| 146 | + * |
| 147 | + * @return request method, or {@code null} if not found |
| 148 | + */ |
104 | 149 | public String requestMethod() {
|
105 | 150 | return header(HEADER_REQUEST_METHOD);
|
106 | 151 | }
|
107 | 152 |
|
| 153 | + /** |
| 154 | + * Returns request qualifier from headers. |
| 155 | + * |
| 156 | + * @return request qualifier, or {@code null} if not found |
| 157 | + */ |
108 | 158 | public String requestQualifier() {
|
109 | 159 | return header(HEADER_QUALIFIER);
|
110 | 160 | }
|
111 | 161 |
|
| 162 | + /** |
| 163 | + * Returns principal object (authenticated entity) associated with the request. |
| 164 | + * |
| 165 | + * @return {@link Principal} associated with the request, or {@code null} if not set |
| 166 | + */ |
112 | 167 | public Principal principal() {
|
113 | 168 | return getOrDefault("principal", null);
|
114 | 169 | }
|
115 | 170 |
|
| 171 | + /** |
| 172 | + * Puts principal to the context. |
| 173 | + * |
| 174 | + * @param principal principal |
| 175 | + * @return new {@code RequestContext} instance with the updated principal |
| 176 | + */ |
116 | 177 | public RequestContext principal(Principal principal) {
|
117 | 178 | return put("principal", principal);
|
118 | 179 | }
|
119 | 180 |
|
| 181 | + /** |
| 182 | + * Checks whether request context has principal. |
| 183 | + * |
| 184 | + * @return {@code true} if principal exists and is not {@link Principal#NULL_PRINCIPAL}, {@code |
| 185 | + * false} otherwise |
| 186 | + */ |
120 | 187 | public boolean hasPrincipal() {
|
121 | 188 | final var principal = principal();
|
122 | 189 | return principal != null && principal != NULL_PRINCIPAL;
|
123 | 190 | }
|
124 | 191 |
|
| 192 | + /** |
| 193 | + * Retrieves {@link MethodInfo} associated with the request. |
| 194 | + * |
| 195 | + * @return {@link MethodInfo} associated with the request, or {@code null} if not set |
| 196 | + */ |
125 | 197 | public MethodInfo methodInfo() {
|
126 | 198 | return getOrDefault("methodInfo", null);
|
127 | 199 | }
|
128 | 200 |
|
| 201 | + /** |
| 202 | + * Puts {@link MethodInfo} associated with the request. |
| 203 | + * |
| 204 | + * @param methodInfo methodInfo |
| 205 | + * @return new {@code RequestContext} instance with the updated {@link MethodInfo} |
| 206 | + */ |
129 | 207 | public RequestContext methodInfo(MethodInfo methodInfo) {
|
130 | 208 | return put("methodInfo", methodInfo);
|
131 | 209 | }
|
132 | 210 |
|
| 211 | + /** |
| 212 | + * Returns path variables associated with the request. |
| 213 | + * |
| 214 | + * @return path variables, or {@code null} if not set |
| 215 | + */ |
133 | 216 | public Map<String, String> pathVars() {
|
134 | 217 | return get("pathVars");
|
135 | 218 | }
|
136 | 219 |
|
| 220 | + /** |
| 221 | + * Puts path variables associated with the request. |
| 222 | + * |
| 223 | + * @return path variables, or {@code null} if not set |
| 224 | + */ |
137 | 225 | public RequestContext pathVars(Map<String, String> pathVars) {
|
138 | 226 | return put("pathVars", pathVars);
|
139 | 227 | }
|
140 | 228 |
|
| 229 | + /** |
| 230 | + * Returns specific path variable by name. |
| 231 | + * |
| 232 | + * @param name name of the path variable |
| 233 | + * @return path variable value, or {@code null} if not found |
| 234 | + */ |
141 | 235 | public String pathVar(String name) {
|
142 | 236 | final var pathVars = pathVars();
|
143 | 237 | return pathVars != null ? pathVars.get(name) : null;
|
144 | 238 | }
|
145 | 239 |
|
| 240 | + /** |
| 241 | + * Returns specific path variable by name, and converts it to the specified type. |
| 242 | + * |
| 243 | + * @param name name of the path variable |
| 244 | + * @param type expected type of the variable |
| 245 | + * @param <T> type parameter |
| 246 | + * @return converted path variable, or {@code null} if not found |
| 247 | + */ |
146 | 248 | public <T> T pathVar(String name, Class<T> type) {
|
147 | 249 | final var s = pathVar(name);
|
148 | 250 | if (s == null) {
|
@@ -173,10 +275,34 @@ public <T> T pathVar(String name, Class<T> type) {
|
173 | 275 | throw new IllegalArgumentException("Wrong pathVar type: " + type);
|
174 | 276 | }
|
175 | 277 |
|
| 278 | + /** |
| 279 | + * Retrieves {@link RequestContext} from the reactor context in deferred manner. |
| 280 | + * |
| 281 | + * @return {@link Mono} emitting the {@code RequestContext}, or error - if it is missing |
| 282 | + */ |
176 | 283 | public static Mono<RequestContext> deferContextual() {
|
177 | 284 | return Mono.deferContextual(context -> Mono.just(context.get(RequestContext.class)));
|
178 | 285 | }
|
179 | 286 |
|
| 287 | + /** |
| 288 | + * Retrieves {@link RequestContext} from the reactor context in deferred manner, and ensures that |
| 289 | + * caller has necessary permissions. |
| 290 | + * |
| 291 | + * <p>This method first extracts {@code RequestContext} and then performs access control checks |
| 292 | + * based on the associated {@link Principal}. If request lacks valid principal or does not have |
| 293 | + * the required role and permissions, then {@link ForbiddenException} is thrown. |
| 294 | + * |
| 295 | + * <p>Access control enforcement follows these rules: |
| 296 | + * |
| 297 | + * <ul> |
| 298 | + * <li>If no principal is present, access is denied |
| 299 | + * <li>If principal role is not among the method allowed roles, access is denied |
| 300 | + * <li>If principal lacks any of the required permissions, access is denied |
| 301 | + * </ul> |
| 302 | + * |
| 303 | + * @return {@link Mono} emitting the validated {@code RequestContext}, or error - if {@code |
| 304 | + * RequestContext} is missing, or if access is denied |
| 305 | + */ |
180 | 306 | public static Mono<RequestContext> deferSecured() {
|
181 | 307 | return Mono.deferContextual(context -> Mono.just(context.get(RequestContext.class)))
|
182 | 308 | .doOnNext(
|
|
0 commit comments