1919
2020import java .security .Principal ;
2121import java .util .Collections ;
22+ import java .util .HashMap ;
23+ import java .util .Map ;
2224import java .util .Set ;
2325
2426/**
@@ -40,6 +42,7 @@ public class PrincipalExtended implements Principal {
4042 private final String name ;
4143 private final boolean admin ;
4244 private final Set <String > roles ;
45+ private Map <String , Object > context ;
4346
4447 public PrincipalExtended (String name , boolean admin , Set <String > roles ) {
4548 this .name = name ;
@@ -65,6 +68,86 @@ public String toString() {
6568 return name + " (" + admin + ")" ;
6669 }
6770
71+ /**
72+ * Get the current context map.
73+ *
74+ * @return the current context map.
75+ */
76+ public Map <String , Object > getContext () {
77+ return context ;
78+ }
79+
80+ /**
81+ * Add an item to the context, or replace an existing item if an item with
82+ * the given key already exists.
83+ *
84+ * @param key The key to store the item under.
85+ * @param value The value to associate with the given key.
86+ * @return this.
87+ */
88+ public PrincipalExtended addContextItem (String key , Object value ) {
89+ if (context == null ) {
90+ context = new HashMap <>();
91+ }
92+ context .put (key , value );
93+ return this ;
94+ }
95+
96+ /**
97+ * Check if the context has an item for the given key.
98+ *
99+ * @param key the key to check.
100+ * @return true if there is a context item for the given key, false
101+ * otherwise.
102+ */
103+ public boolean hasContextItem (String key ) {
104+ if (context == null ) {
105+ return false ;
106+ }
107+ return context .containsKey (key );
108+ }
109+
110+ /**
111+ * Get the context item with the given key, or dflt if there is no such
112+ * item.
113+ *
114+ * @param key The key to get the item for.
115+ * @param dflt The value to return if there is no value for the given key.
116+ * @return The value for the given key, or dflt if there is no such item.
117+ */
118+ public Object getContextItem (String key , Object dflt ) {
119+ if (context == null ) {
120+ return dflt ;
121+ }
122+ Object value = context .get (key );
123+ if (value == null ) {
124+ return dflt ;
125+ }
126+ return value ;
127+ }
128+
129+ /**
130+ * Get the context item with the given key, or null if there is no such
131+ * item.
132+ *
133+ * @param key The key to get the item for.
134+ * @return The value for the given key, or null if there is no such item.
135+ */
136+ public Object getContextItem (String key ) {
137+ return getContextItem (key , null );
138+ }
139+
140+ /**
141+ * Replace the current context with the given value.
142+ *
143+ * @param context The context map to replace the current context with.
144+ * @return this
145+ */
146+ public PrincipalExtended setContext (Map <String , Object > context ) {
147+ this .context = context ;
148+ return this ;
149+ }
150+
68151 /**
69152 * Turns the given principal into a PrincipalExtended. A null value will
70153 * turn into an anonymous principal extended.
0 commit comments