Skip to content

Commit c0094bb

Browse files
committed
✨ Add support for attached request parameters
1 parent 31c4742 commit c0094bb

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

commons/src/main/java/org/restheart/exchange/Request.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import java.util.LinkedHashMap;
3737
import java.util.List;
3838
import java.util.Map;
39+
import java.util.Objects;
3940
import org.restheart.exchange.ExchangeKeys.METHOD;
4041

4142
/**
@@ -53,6 +54,8 @@ public abstract class Request<T> extends Exchange<T> {
5354
public static final String PATCH = "PATCH";
5455
public static final String UNDERSCORE = "_";
5556

57+
private static final AttachmentKey<Map<String, Object>> ATTACHED_PARAMS_KEY = AttachmentKey.create(Map.class);
58+
5659
public static final AttachmentKey<PipelineInfo> PIPELINE_INFO_KEY = AttachmentKey.create(PipelineInfo.class);
5760

5861
private static final AttachmentKey<Long> START_TIME_KEY = AttachmentKey.create(Long.class);
@@ -63,6 +66,10 @@ public abstract class Request<T> extends Exchange<T> {
6366

6467
protected Request(HttpServerExchange exchange) {
6568
super(exchange);
69+
// init attached params
70+
if (exchange.getAttachment(ATTACHED_PARAMS_KEY) == null) {
71+
exchange.putAttachment(ATTACHED_PARAMS_KEY, new HashMap<>());
72+
}
6673
}
6774

6875
@SuppressWarnings("rawtypes")
@@ -465,4 +472,55 @@ public boolean isBlockForTooManyRequests() {
465472

466473
return block == null ? false : block;
467474
}
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+
}
468526
}

0 commit comments

Comments
 (0)