KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > webflow > context > servlet > HttpSessionMap


1 /*
2  * Copyright 2004-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package org.springframework.webflow.context.servlet;
17
18 import java.util.Iterator JavaDoc;
19
20 import javax.servlet.http.HttpServletRequest JavaDoc;
21 import javax.servlet.http.HttpSession JavaDoc;
22
23 import org.springframework.binding.collection.SharedMap;
24 import org.springframework.binding.collection.StringKeyedMapAdapter;
25 import org.springframework.web.util.WebUtils;
26 import org.springframework.webflow.core.collection.AttributeMapBindingListener;
27 import org.springframework.webflow.core.collection.CollectionUtils;
28
29 /**
30  * A Shared Map backed by the Servlet HTTP session, for accessing session scoped
31  * attributes.
32  *
33  * @author Keith Donald
34  */

35 public class HttpSessionMap extends StringKeyedMapAdapter implements SharedMap {
36
37     /**
38      * The wrapped HTTP request, providing access to the session.
39      */

40     private HttpServletRequest JavaDoc request;
41
42     /**
43      * Create a map wrapping the session of given request.
44      */

45     public HttpSessionMap(HttpServletRequest JavaDoc request) {
46         this.request = request;
47     }
48
49     /**
50      * Internal helper to get the HTTP session associated with the wrapped
51      * request, or null if there is no such session.
52      * <p>
53      * Note that this method will not force session creation.
54      */

55     private HttpSession JavaDoc getSession() {
56         return request.getSession(false);
57     }
58
59     protected Object JavaDoc getAttribute(String JavaDoc key) {
60         HttpSession JavaDoc session = getSession();
61         if (session == null) {
62             return null;
63         }
64         Object JavaDoc value = session.getAttribute(key);
65         if (value instanceof HttpSessionMapBindingListener) {
66             // unwrap
67
return ((HttpSessionMapBindingListener)value).getListener();
68         } else {
69             return value;
70         }
71     }
72
73     protected void setAttribute(String JavaDoc key, Object JavaDoc value) {
74         // force session creation
75
HttpSession JavaDoc session = request.getSession(true);
76         if (value instanceof AttributeMapBindingListener) {
77             // wrap
78
session.setAttribute(key,
79                     new HttpSessionMapBindingListener((AttributeMapBindingListener)value, this));
80         }
81         else {
82             session.setAttribute(key, value);
83         }
84     }
85
86     protected void removeAttribute(String JavaDoc key) {
87         HttpSession JavaDoc session = getSession();
88         if (session != null) {
89             session.removeAttribute(key);
90         }
91     }
92
93     protected Iterator JavaDoc getAttributeNames() {
94         HttpSession JavaDoc session = getSession();
95         return session == null ? CollectionUtils.EMPTY_ITERATOR : CollectionUtils.toIterator(session
96                 .getAttributeNames());
97     }
98
99     public Object JavaDoc getMutex() {
100         // force session creation
101
HttpSession JavaDoc session = request.getSession(true);
102         Object JavaDoc mutex = session.getAttribute(WebUtils.SESSION_MUTEX_ATTRIBUTE);
103         return mutex != null ? mutex : session;
104     }
105 }
Popular Tags