01: /*
02: * Copyright 2004 The Apache Software Foundation.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package org.apache.myfaces.context.servlet;
17:
18: import java.util.Enumeration;
19: import java.util.Map;
20:
21: import javax.servlet.http.HttpServletRequest;
22: import javax.servlet.http.HttpSession;
23:
24: import org.apache.myfaces.shared_impl.util.NullEnumeration;
25: import org.apache.myfaces.util.AbstractAttributeMap;
26:
27: /**
28: * HttpSession attibutes as Map.
29: *
30: * @author Anton Koinov (latest modification by $Author: mbr $)
31: * @version $Revision: 512417 $ $Date: 2007-02-27 22:22:36 +0100 (Di, 27 Feb 2007) $
32: */
33: public class SessionMap extends AbstractAttributeMap<Object> {
34: private final HttpServletRequest _httpRequest;
35:
36: SessionMap(HttpServletRequest httpRequest) {
37: _httpRequest = httpRequest;
38: }
39:
40: @Override
41: protected Object getAttribute(String key) {
42: HttpSession httpSession = getSession();
43: return (httpSession == null) ? null : httpSession
44: .getAttribute(key.toString());
45: }
46:
47: @Override
48: protected void setAttribute(String key, Object value) {
49: _httpRequest.getSession(true).setAttribute(key, value);
50: }
51:
52: @Override
53: protected void removeAttribute(String key) {
54: HttpSession httpSession = getSession();
55: if (httpSession != null) {
56: httpSession.removeAttribute(key);
57: }
58: }
59:
60: @Override
61: @SuppressWarnings("unchecked")
62: protected Enumeration<String> getAttributeNames() {
63: HttpSession httpSession = getSession();
64: return (httpSession == null) ? NullEnumeration.instance()
65: : httpSession.getAttributeNames();
66: }
67:
68: private HttpSession getSession() {
69: return _httpRequest.getSession(false);
70: }
71:
72: @Override
73: public void putAll(Map t) {
74: throw new UnsupportedOperationException();
75: }
76:
77: /**
78: * This will clear the session without invalidation. If no session has been created, it will simply return.
79: */
80: @Override
81: public void clear() {
82: HttpSession session = getSession();
83: if (session == null)
84: return;
85: for (Enumeration attributeNames = session.getAttributeNames(); attributeNames
86: .hasMoreElements();) {
87: String attributeName = (String) attributeNames
88: .nextElement();
89: session.removeAttribute(attributeName);
90: }
91: }
92: }
|