Skip to content

Commit a8886dd

Browse files
committed
Added javadocs for RequestContext
1 parent 9f563a9 commit a8886dd

File tree

1 file changed

+127
-1
lines changed

1 file changed

+127
-1
lines changed

services-api/src/main/java/io/scalecube/services/RequestContext.java

Lines changed: 127 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,30 @@
1919
import reactor.core.publisher.Mono;
2020
import reactor.util.context.Context;
2121

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+
*/
2228
public class RequestContext implements Context {
2329

2430
private static final Logger LOGGER = LoggerFactory.getLogger(RequestContext.class);
2531

2632
private final Context source;
2733

34+
/** Creates new {@code RequestContext} with an empty base context. */
2835
public RequestContext() {
2936
this(Context.empty());
3037
}
3138

39+
/**
40+
* Creates new {@code RequestContext} based on an existing context.
41+
*
42+
* @param source the source context to wrap
43+
*/
3244
public RequestContext(Context source) {
33-
this.source = source;
45+
this.source = Objects.requireNonNull(source, "source");
3446
}
3547

3648
@Override
@@ -80,69 +92,159 @@ public Context delete(Object key) {
8092
return key == RequestContext.class ? source : source.delete(key);
8193
}
8294

95+
/**
96+
* Returns request headers.
97+
*
98+
* @return headers, or {@code null} if not set
99+
*/
83100
public Map<String, String> headers() {
84101
return source.getOrDefault("headers", null);
85102
}
86103

104+
/**
105+
* Puts request headers to the context.
106+
*
107+
* @param headers headers
108+
* @return new {@code RequestContext} instance with updated headers
109+
*/
87110
public RequestContext headers(Map<String, String> headers) {
88111
return put("headers", headers);
89112
}
90113

114+
/**
115+
* Returns request.
116+
*
117+
* @return request, or {@code null} if not set
118+
*/
91119
public Object request() {
92120
return source.getOrDefault("request", null);
93121
}
94122

123+
/**
124+
* Puts request to the context.
125+
*
126+
* @param request request
127+
* @return new {@code RequestContext} instance with updated request
128+
*/
95129
public RequestContext request(Object request) {
96130
return put("request", request);
97131
}
98132

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+
*/
99139
public String header(String name) {
100140
final var headers = headers();
101141
return headers != null ? headers.get(name) : null;
102142
}
103143

144+
/**
145+
* Returns request method from headers.
146+
*
147+
* @return request method, or {@code null} if not found
148+
*/
104149
public String requestMethod() {
105150
return header(HEADER_REQUEST_METHOD);
106151
}
107152

153+
/**
154+
* Returns request qualifier from headers.
155+
*
156+
* @return request qualifier, or {@code null} if not found
157+
*/
108158
public String requestQualifier() {
109159
return header(HEADER_QUALIFIER);
110160
}
111161

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+
*/
112167
public Principal principal() {
113168
return getOrDefault("principal", null);
114169
}
115170

171+
/**
172+
* Puts principal to the context.
173+
*
174+
* @param principal principal
175+
* @return new {@code RequestContext} instance with the updated principal
176+
*/
116177
public RequestContext principal(Principal principal) {
117178
return put("principal", principal);
118179
}
119180

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+
*/
120187
public boolean hasPrincipal() {
121188
final var principal = principal();
122189
return principal != null && principal != NULL_PRINCIPAL;
123190
}
124191

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+
*/
125197
public MethodInfo methodInfo() {
126198
return getOrDefault("methodInfo", null);
127199
}
128200

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+
*/
129207
public RequestContext methodInfo(MethodInfo methodInfo) {
130208
return put("methodInfo", methodInfo);
131209
}
132210

211+
/**
212+
* Returns path variables associated with the request.
213+
*
214+
* @return path variables, or {@code null} if not set
215+
*/
133216
public Map<String, String> pathVars() {
134217
return get("pathVars");
135218
}
136219

220+
/**
221+
* Puts path variables associated with the request.
222+
*
223+
* @return path variables, or {@code null} if not set
224+
*/
137225
public RequestContext pathVars(Map<String, String> pathVars) {
138226
return put("pathVars", pathVars);
139227
}
140228

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+
*/
141235
public String pathVar(String name) {
142236
final var pathVars = pathVars();
143237
return pathVars != null ? pathVars.get(name) : null;
144238
}
145239

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+
*/
146248
public <T> T pathVar(String name, Class<T> type) {
147249
final var s = pathVar(name);
148250
if (s == null) {
@@ -173,10 +275,34 @@ public <T> T pathVar(String name, Class<T> type) {
173275
throw new IllegalArgumentException("Wrong pathVar type: " + type);
174276
}
175277

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+
*/
176283
public static Mono<RequestContext> deferContextual() {
177284
return Mono.deferContextual(context -> Mono.just(context.get(RequestContext.class)));
178285
}
179286

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+
*/
180306
public static Mono<RequestContext> deferSecured() {
181307
return Mono.deferContextual(context -> Mono.just(context.get(RequestContext.class)))
182308
.doOnNext(

0 commit comments

Comments
 (0)