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     * @author Les Hazlewood
029     * @see org.apache.shiro.subject.SubjectContext SubjectContext
030     * @see org.apache.shiro.session.mgt.SessionContext SessionContext
031     * @since 1.0
032     */
033    public class MapContext implements Map<String, Object>, Serializable {
034    
035        private static final long serialVersionUID = 5373399119017820322L;
036    
037        private final Map<String, Object> backingMap;
038    
039        public MapContext() {
040            this.backingMap = new HashMap<String, Object>();
041        }
042    
043        public MapContext(Map<String, Object> map) {
044            this();
045            if (!CollectionUtils.isEmpty(map)) {
046                this.backingMap.putAll(map);
047            }
048        }
049    
050        /**
051         * Performs a {@link #get get} operation but additionally ensures that the value returned is of the specified
052         * {@code type}.  If there is no value, {@code null} is returned.
053         *
054         * @param key  the attribute key to look up a value
055         * @param type the expected type of the value
056         * @param <E>  the expected type of the value
057         * @return the typed value or {@code null} if the attribute does not exist.
058         */
059        @SuppressWarnings({"unchecked"})
060        protected <E> E getTypedValue(String key, Class<E> type) {
061            E found = null;
062            Object o = backingMap.get(key);
063            if (o != null) {
064                if (!type.isAssignableFrom(o.getClass())) {
065                    String msg = "Invalid object found in SubjectContext Map under key [" + key + "].  Expected type " +
066                            "was [" + type.getName() + "], but the object under that key is of type " +
067                            "[" + o.getClass().getName() + "].";
068                    throw new IllegalArgumentException(msg);
069                }
070                found = (E) o;
071            }
072            return found;
073        }
074    
075        /**
076         * Places a value in this context map under the given key only if the given {@code value} argument is not null.
077         *
078         * @param key   the attribute key under which the non-null value will be stored
079         * @param value the non-null value to store.  If {@code null}, this method does nothing and returns immediately.
080         */
081        protected void nullSafePut(String key, Object value) {
082            if (value != null) {
083                put(key, value);
084            }
085        }
086    
087        public int size() {
088            return backingMap.size();
089        }
090    
091        public boolean isEmpty() {
092            return backingMap.isEmpty();
093        }
094    
095        public boolean containsKey(Object o) {
096            return backingMap.containsKey(o);
097        }
098    
099        public boolean containsValue(Object o) {
100            return backingMap.containsValue(o);
101        }
102    
103        public Object get(Object o) {
104            return backingMap.get(o);
105        }
106    
107        public Object put(String s, Object o) {
108            return backingMap.put(s, o);
109        }
110    
111        public Object remove(Object o) {
112            return backingMap.remove(o);
113        }
114    
115        public void putAll(Map<? extends String, ?> map) {
116            backingMap.putAll(map);
117        }
118    
119        public void clear() {
120            backingMap.clear();
121        }
122    
123        public Set<String> keySet() {
124            return Collections.unmodifiableSet(backingMap.keySet());
125        }
126    
127        public Collection<Object> values() {
128            return Collections.unmodifiableCollection(backingMap.values());
129        }
130    
131        public Set<Entry<String, Object>> entrySet() {
132            return Collections.unmodifiableSet(backingMap.entrySet());
133        }
134    }