From bf758bc8d2c5cf2f2808617ae2aebfac10ea0954 Mon Sep 17 00:00:00 2001 From: Hylke van der Schaaf Date: Mon, 18 Sep 2023 09:30:53 +0200 Subject: [PATCH] Added a key/value context store to PrincipalExtended --- .../util/user/PrincipalExtended.java | 83 +++++++++++++++++++ 1 file changed, 83 insertions(+) diff --git a/FROST-Server.Util/src/main/java/de/fraunhofer/iosb/ilt/frostserver/util/user/PrincipalExtended.java b/FROST-Server.Util/src/main/java/de/fraunhofer/iosb/ilt/frostserver/util/user/PrincipalExtended.java index ff0f9ed09..589c46dfe 100644 --- a/FROST-Server.Util/src/main/java/de/fraunhofer/iosb/ilt/frostserver/util/user/PrincipalExtended.java +++ b/FROST-Server.Util/src/main/java/de/fraunhofer/iosb/ilt/frostserver/util/user/PrincipalExtended.java @@ -19,6 +19,8 @@ import java.security.Principal; import java.util.Collections; +import java.util.HashMap; +import java.util.Map; import java.util.Set; /** @@ -40,6 +42,7 @@ public class PrincipalExtended implements Principal { private final String name; private final boolean admin; private final Set roles; + private Map context; public PrincipalExtended(String name, boolean admin, Set roles) { this.name = name; @@ -65,6 +68,86 @@ public String toString() { return name + " (" + admin + ")"; } + /** + * Get the current context map. + * + * @return the current context map. + */ + public Map getContext() { + return context; + } + + /** + * Add an item to the context, or replace an existing item if an item with + * the given key already exists. + * + * @param key The key to store the item under. + * @param value The value to associate with the given key. + * @return this. + */ + public PrincipalExtended addContextItem(String key, Object value) { + if (context == null) { + context = new HashMap<>(); + } + context.put(key, value); + return this; + } + + /** + * Check if the context has an item for the given key. + * + * @param key the key to check. + * @return true if there is a context item for the given key, false + * otherwise. + */ + public boolean hasContextItem(String key) { + if (context == null) { + return false; + } + return context.containsKey(key); + } + + /** + * Get the context item with the given key, or dflt if there is no such + * item. + * + * @param key The key to get the item for. + * @param dflt The value to return if there is no value for the given key. + * @return The value for the given key, or dflt if there is no such item. + */ + public Object getContextItem(String key, Object dflt) { + if (context == null) { + return dflt; + } + Object value = context.get(key); + if (value == null) { + return dflt; + } + return value; + } + + /** + * Get the context item with the given key, or null if there is no such + * item. + * + * @param key The key to get the item for. + * @return The value for the given key, or null if there is no such item. + */ + public Object getContextItem(String key) { + return getContextItem(key, null); + } + + /** + * Replace the current context with the given value. + * + * @param context The context map to replace the current context with. + * @return this + */ + public PrincipalExtended setContext(Map context) { + this.context = context; + return this; + } + /** * Turns the given principal into a PrincipalExtended. A null value will * turn into an anonymous principal extended.