001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     */
019    package org.apache.shiro.util;
020    
021    import java.io.Serializable;
022    import java.util.*;
023    
024    /**
025     * A {@code MapContext} provides a common base for context-based data storage in a {@link Map}.  Type-safe attribute
026     * retrieval is provided for subclasses with the {@link #getTypedValue(String, Class)} method.
027     *
028     * @see org.apache.shiro.subject.SubjectContext SubjectContext
029     * @see org.apache.shiro.session.mgt.SessionContext SessionContext
030     * @since 1.0
031     */
032    public class MapContext implements Map<String, Object>, Serializable {
033    
034        private static final long serialVersionUID = 5373399119017820322L;
035    
036        private final Map<String, Object> backingMap;
037    
038        public MapContext() {
039            this.backingMap = new HashMap<String, Object>();
040        }
041    
042        public MapContext(Map<String, Object> map) {
043            this();
044            if (!CollectionUtils.isEmpty(map)) {
045                this.backingMap.putAll(map);
046            }
047        }
048    
049        /**
050         * Performs a {@link #get get} operation but additionally ensures that the value returned is of the specified
051         * {@code type}.  If there is no value, {@code null} is returned.
052         *
053         * @param key  the attribute key to look up a value
054         * @param type the expected type of the value
055         * @param <E>  the expected type of the value
056         * @return the typed value or {@code null} if the attribute does not exist.
057         */
058        @SuppressWarnings({"unchecked"})
059        protected <E> E getTypedValue(String key, Class<E> type) {
060            E found = null;
061            Object o = backingMap.get(key);
062            if (o != null) {
063                if (!type.isAssignableFrom(o.getClass())) {
064                    String msg = "Invalid object found in SubjectContext Map under key [" + key + "].  Expected type " +
065                            "was [" + type.getName() + "], but the object under that key is of type " +
066                            "[" + o.getClass().getName() + "].";
067                    throw new IllegalArgumentException(msg);
068                }
069                found = (E) o;
070            }
071            return found;
072        }
073    
074        /**
075         * Places a value in this context map under the given key only if the given {@code value} argument is not null.
076         *
077         * @param key   the attribute key under which the non-null value will be stored
078         * @param value the non-null value to store.  If {@code null}, this method does nothing and returns immediately.
079         */
080        protected void nullSafePut(String key, Object value) {
081            if (value != null) {
082                put(key, value);
083            }
084        }
085    
086        public int size() {
087            return backingMap.size();
088        }
089    
090        public boolean isEmpty() {
091            return backingMap.isEmpty();
092        }
093    
094        public boolean containsKey(Object o) {
095            return backingMap.containsKey(o);
096        }
097    
098        public boolean containsValue(Object o) {
099            return backingMap.containsValue(o);
100        }
101    
102        public Object get(Object o) {
103            return backingMap.get(o);
104        }
105    
106        public Object put(String s, Object o) {
107            return backingMap.put(s, o);
108        }
109    
110        public Object remove(Object o) {
111            return backingMap.remove(o);
112        }
113    
114        public void putAll(Map<? extends String, ?> map) {
115            backingMap.putAll(map);
116        }
117    
118        public void clear() {
119            backingMap.clear();
120        }
121    
122        public Set<String> keySet() {
123            return Collections.unmodifiableSet(backingMap.keySet());
124        }
125    
126        public Collection<Object> values() {
127            return Collections.unmodifiableCollection(backingMap.values());
128        }
129    
130        public Set<Entry<String, Object>> entrySet() {
131            return Collections.unmodifiableSet(backingMap.entrySet());
132        }
133    }