001: /*
002: * Copyright 2004-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package org.springframework.webflow.context.servlet;
017:
018: import java.util.Iterator;
019:
020: import javax.servlet.http.HttpServletRequest;
021: import javax.servlet.http.HttpSession;
022:
023: import org.springframework.binding.collection.SharedMap;
024: import org.springframework.binding.collection.StringKeyedMapAdapter;
025: import org.springframework.web.util.WebUtils;
026: import org.springframework.webflow.core.collection.AttributeMapBindingListener;
027: import org.springframework.webflow.core.collection.CollectionUtils;
028:
029: /**
030: * A Shared Map backed by the Servlet HTTP session, for accessing session scoped
031: * attributes.
032: *
033: * @author Keith Donald
034: */
035: public class HttpSessionMap extends StringKeyedMapAdapter implements
036: SharedMap {
037:
038: /**
039: * The wrapped HTTP request, providing access to the session.
040: */
041: private HttpServletRequest request;
042:
043: /**
044: * Create a map wrapping the session of given request.
045: */
046: public HttpSessionMap(HttpServletRequest request) {
047: this .request = request;
048: }
049:
050: /**
051: * Internal helper to get the HTTP session associated with the wrapped
052: * request, or null if there is no such session.
053: * <p>
054: * Note that this method will not force session creation.
055: */
056: private HttpSession getSession() {
057: return request.getSession(false);
058: }
059:
060: protected Object getAttribute(String key) {
061: HttpSession session = getSession();
062: if (session == null) {
063: return null;
064: }
065: Object value = session.getAttribute(key);
066: if (value instanceof HttpSessionMapBindingListener) {
067: // unwrap
068: return ((HttpSessionMapBindingListener) value)
069: .getListener();
070: } else {
071: return value;
072: }
073: }
074:
075: protected void setAttribute(String key, Object value) {
076: // force session creation
077: HttpSession session = request.getSession(true);
078: if (value instanceof AttributeMapBindingListener) {
079: // wrap
080: session.setAttribute(key,
081: new HttpSessionMapBindingListener(
082: (AttributeMapBindingListener) value, this ));
083: } else {
084: session.setAttribute(key, value);
085: }
086: }
087:
088: protected void removeAttribute(String key) {
089: HttpSession session = getSession();
090: if (session != null) {
091: session.removeAttribute(key);
092: }
093: }
094:
095: protected Iterator getAttributeNames() {
096: HttpSession session = getSession();
097: return session == null ? CollectionUtils.EMPTY_ITERATOR
098: : CollectionUtils.toIterator(session
099: .getAttributeNames());
100: }
101:
102: public Object getMutex() {
103: // force session creation
104: HttpSession session = request.getSession(true);
105: Object mutex = session
106: .getAttribute(WebUtils.SESSION_MUTEX_ATTRIBUTE);
107: return mutex != null ? mutex : session;
108: }
109: }
|