36
36
import java .util .LinkedHashMap ;
37
37
import java .util .List ;
38
38
import java .util .Map ;
39
+ import java .util .Objects ;
39
40
import org .restheart .exchange .ExchangeKeys .METHOD ;
40
41
41
42
/**
@@ -53,6 +54,8 @@ public abstract class Request<T> extends Exchange<T> {
53
54
public static final String PATCH = "PATCH" ;
54
55
public static final String UNDERSCORE = "_" ;
55
56
57
+ private static final AttachmentKey <Map <String , Object >> ATTACHED_PARAMS_KEY = AttachmentKey .create (Map .class );
58
+
56
59
public static final AttachmentKey <PipelineInfo > PIPELINE_INFO_KEY = AttachmentKey .create (PipelineInfo .class );
57
60
58
61
private static final AttachmentKey <Long > START_TIME_KEY = AttachmentKey .create (Long .class );
@@ -63,6 +66,10 @@ public abstract class Request<T> extends Exchange<T> {
63
66
64
67
protected Request (HttpServerExchange exchange ) {
65
68
super (exchange );
69
+ // init attached params
70
+ if (exchange .getAttachment (ATTACHED_PARAMS_KEY ) == null ) {
71
+ exchange .putAttachment (ATTACHED_PARAMS_KEY , new HashMap <>());
72
+ }
66
73
}
67
74
68
75
@ SuppressWarnings ("rawtypes" )
@@ -465,4 +472,55 @@ public boolean isBlockForTooManyRequests() {
465
472
466
473
return block == null ? false : block ;
467
474
}
475
+
476
+ /**
477
+ * Retrieves the key-value entries attached to the current request.
478
+ * <p>
479
+ * This method always returns a non-null map. If no parameters were previously attached,
480
+ * an empty map is returned.
481
+ *
482
+ * @return a non-null map containing the attached parameters.
483
+ */
484
+ public Map <String , Object > attachedParams () {
485
+ return getWrappedExchange ().getAttachment (ATTACHED_PARAMS_KEY );
486
+ }
487
+
488
+ /**
489
+ * Retrieves the value of a specific attached parameter from the request, cast to the expected type.
490
+ *
491
+ * @param <T> the expected type of the parameter value.
492
+ * @param key the key of the parameter to retrieve; must not be {@code null}.
493
+ * @return the value associated with the given key, cast to type {@code T}, or {@code null} if the key is not present.
494
+ * @throws NullPointerException if {@code key} is {@code null}.
495
+ * @throws ClassCastException if the value cannot be cast to type {@code T}.
496
+ */
497
+ @ SuppressWarnings ("unchecked" )
498
+ public <T > T attachedParam (String key ) {
499
+ Objects .requireNonNull (key , "Key must not be null" );
500
+ var val = getWrappedExchange ().getAttachment (ATTACHED_PARAMS_KEY ).get (key );
501
+
502
+ return val == null ? null : (T ) attachedParams ().get (key );
503
+ }
504
+
505
+ /**
506
+ * Adds a key-value entry to the attached parameters of the current request.
507
+ * <p>
508
+ * If no parameters exist, this method ensures that a new map is created before adding the entry.
509
+ *
510
+ * @param key the key of the parameter to attach; must not be {@code null}.
511
+ * @param value the value of the parameter to attach; can be {@code null}.
512
+ * @throws NullPointerException if {@code key} is {@code null}.
513
+ */
514
+ public void attachParam (String key , Object value ) {
515
+ Objects .requireNonNull (key , "Key must not be null" );
516
+
517
+ Map <String , Object > attachedParams = getWrappedExchange ().getAttachment (ATTACHED_PARAMS_KEY );
518
+
519
+ // Ensure the map is initialized if not already present
520
+ if (attachedParams == null ) {
521
+ throw new IllegalStateException ("Attached parameters map is unexpectedly null" );
522
+ }
523
+
524
+ attachedParams .put (key , value );
525
+ }
468
526
}
0 commit comments