001
014
015 package com.liferay.portlet;
016
017 import com.liferay.portal.kernel.util.MappingEnumeration;
018 import com.liferay.portal.kernel.util.MappingEnumeration.Mapper;
019 import com.liferay.portal.kernel.util.SetUtil;
020
021 import java.util.AbstractMap;
022 import java.util.ArrayList;
023 import java.util.Collection;
024 import java.util.Collections;
025 import java.util.Enumeration;
026 import java.util.HashMap;
027 import java.util.List;
028 import java.util.Map;
029 import java.util.Set;
030
031 import javax.servlet.http.HttpSession;
032
033
037 public class PortletSessionAttributeMap extends AbstractMap<String, Object> {
038
039 public PortletSessionAttributeMap(HttpSession session) {
040 this(session, null);
041 }
042
043 public PortletSessionAttributeMap(HttpSession session, String scopePrefix) {
044 this.session = session;
045 this.scopePrefix = scopePrefix;
046 }
047
048 @Override
049 public void clear() {
050 throw new UnsupportedOperationException();
051 }
052
053 @Override
054 public boolean containsKey(Object key) {
055 if (key == null) {
056 return false;
057 }
058
059 key = encodeKey(String.valueOf(key));
060
061 Enumeration<String> enumeration = getAttributeNames(false);
062
063 while (enumeration.hasMoreElements()) {
064 String attributeName = enumeration.nextElement();
065
066 if (attributeName.equals(key)) {
067 return true;
068 }
069 }
070
071 return false;
072 }
073
074 @Override
075 public boolean containsValue(Object value) {
076 Enumeration<String> enumeration = getAttributeNames(false);
077
078 while (enumeration.hasMoreElements()) {
079 Object attributeValue = session.getAttribute(
080 enumeration.nextElement());
081
082 if (attributeValue.equals(value)) {
083 return true;
084 }
085 }
086
087 return false;
088 }
089
090 @Override
091 public Set<Entry<String, Object>> entrySet() {
092 Map<String, Object> map = new HashMap<String, Object>();
093
094 Enumeration<String> enumeration = getAttributeNames(true);
095
096 while (enumeration.hasMoreElements()) {
097 String attributeName = enumeration.nextElement();
098
099 map.put(attributeName, get(attributeName));
100 }
101
102 return Collections.unmodifiableSet(map.entrySet());
103 }
104
105 @Override
106 public Object get(Object key) {
107 if (key == null) {
108 return null;
109 }
110
111 return session.getAttribute(encodeKey(String.valueOf(key)));
112 }
113
114 @Override
115 public boolean isEmpty() {
116 Enumeration<String> enumeration = getAttributeNames(false);
117
118 return !enumeration.hasMoreElements();
119 }
120
121 @Override
122 public Set<String> keySet() {
123 return Collections.unmodifiableSet(
124 SetUtil.fromEnumeration(getAttributeNames(true)));
125 }
126
127 @Override
128 public Object put(String key, Object value) {
129 throw new UnsupportedOperationException();
130 }
131
132 @Override
133 public void putAll(Map<? extends String, ?> map) {
134 throw new UnsupportedOperationException();
135 }
136
137 @Override
138 public Object remove(Object key) {
139 throw new UnsupportedOperationException();
140 }
141
142 @Override
143 public int size() {
144 int size = 0;
145
146 Enumeration<String> enumeration = getAttributeNames(false);
147
148 while (enumeration.hasMoreElements()) {
149 enumeration.nextElement();
150
151 size++;
152 }
153
154 return size;
155 }
156
157 @Override
158 public Collection<Object> values() {
159 List<Object> attributeValues = new ArrayList<Object>();
160
161 Enumeration<String> enumeration = getAttributeNames(false);
162
163 while (enumeration.hasMoreElements()) {
164 attributeValues.add(
165 session.getAttribute(enumeration.nextElement()));
166 }
167
168 return attributeValues;
169 }
170
171 protected String encodeKey(String key) {
172 if (scopePrefix == null) {
173 return key;
174 }
175
176 return scopePrefix.concat(key);
177 }
178
179 protected Enumeration<String> getAttributeNames(boolean removePrefix) {
180 Enumeration<String> enumeration = session.getAttributeNames();
181
182 if (scopePrefix == null) {
183 return enumeration;
184 }
185
186 return new MappingEnumeration<String, String>(
187 enumeration,
188 new AttributeNameMapper(scopePrefix, removePrefix));
189 }
190
191 protected final String scopePrefix;
192 protected final HttpSession session;
193
194 protected static class AttributeNameMapper
195 implements Mapper<String, String> {
196
197 @Override
198 public String map(String attributeName) {
199 if (attributeName.startsWith(_attributeNamespace)) {
200 if (_removePrefix) {
201 return attributeName.substring(
202 _attributeNamespace.length());
203 }
204
205 return attributeName;
206 }
207
208 return null;
209 }
210
211 protected AttributeNameMapper(
212 String attributeNamespace, boolean removePrefix) {
213
214 _attributeNamespace = attributeNamespace;
215 _removePrefix = removePrefix;
216 }
217
218 private final String _attributeNamespace;
219 private final boolean _removePrefix;
220
221 }
222
223 }