001: /*
002: * The Apache Software License, Version 1.1
003: *
004: * Copyright (c) 2001-2004 Caucho Technology, Inc. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * 3. The end-user documentation included with the redistribution, if
019: * any, must include the following acknowlegement:
020: * "This product includes software developed by the
021: * Caucho Technology (http://www.caucho.com/)."
022: * Alternately, this acknowlegement may appear in the software itself,
023: * if and wherever such third-party acknowlegements normally appear.
024: *
025: * 4. The names "Hessian", "Resin", and "Caucho" must not be used to
026: * endorse or promote products derived from this software without prior
027: * written permission. For written permission, please contact
028: * info@caucho.com.
029: *
030: * 5. Products derived from this software may not be called "Resin"
031: * nor may "Resin" appear in their names without prior written
032: * permission of Caucho Technology.
033: *
034: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
035: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
036: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
037: * DISCLAIMED. IN NO EVENT SHALL CAUCHO TECHNOLOGY OR ITS CONTRIBUTORS
038: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
039: * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
040: * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
041: * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
042: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
043: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
044: * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
045: *
046: * @author Sam
047: */
048:
049: package com.caucho.portal.generic;
050:
051: import javax.portlet.PortletContext;
052: import javax.portlet.PortletSession;
053: import javax.servlet.http.HttpSession;
054: import java.util.Enumeration;
055: import java.util.NoSuchElementException;
056: import java.util.logging.Logger;
057:
058: /**
059: * An adapter to a {@link javax.servlet.HttpSession}.
060: */
061: public class HttpPortletSession implements PortletSession {
062: static protected final Logger log = Logger
063: .getLogger(HttpPortletSession.class.getName());
064:
065: public final String PORTLET_SCOPE_PREFIX = "javax.portlet.p.";
066: public final String PORTLET_SCOPE_RESERVED_PREFIX = "javax.portlet.p.javax.portlet.";
067:
068: private PortletContext _portletContext;
069: private HttpSession _httpSession;
070:
071: public HttpPortletSession() {
072: }
073:
074: public void start(PortletContext portletContext,
075: HttpSession httpSession) {
076: _portletContext = portletContext;
077: _httpSession = httpSession;
078:
079: if (_httpSession != null) {
080: for (Enumeration en = _httpSession.getAttributeNames(); en
081: .hasMoreElements();) {
082: String name = (String) en.nextElement();
083: Object value = _httpSession.getAttribute(name);
084: }
085: }
086:
087: }
088:
089: /**
090: * Finish with the HttpSession object. After calling this method,
091: * this object can be placed into a pool and reused for subsequent requests.
092: */
093: public void finish() {
094: _httpSession = null;
095: _portletContext = null;
096: }
097:
098: public PortletContext getPortletContext() {
099: return _portletContext;
100: }
101:
102: public HttpSession getHttpSession() {
103: return _httpSession;
104: }
105:
106: private String scopedName(String name, int scope) {
107: if (name == null)
108: throw new IllegalArgumentException("name is null");
109:
110: if (false) // XXX: isInvalid() might be handled by HttpSession
111: throw new IllegalStateException("session is invalid");
112:
113: if (scope == APPLICATION_SCOPE) {
114: } else if (scope == PORTLET_SCOPE) {
115: StringBuffer sb = new StringBuffer(PORTLET_SCOPE_PREFIX);
116: sb.append(name);
117: name = sb.toString();
118: } else {
119: throw new IllegalArgumentException("invalid scope `"
120: + scope + "'");
121: }
122:
123: return name;
124: }
125:
126: public Object getAttribute(String name) {
127: return getAttribute(name, PORTLET_SCOPE);
128: }
129:
130: public Object getAttribute(String name, int scope) {
131: return _httpSession.getAttribute(scopedName(name, scope));
132: }
133:
134: public Enumeration getAttributeNames() {
135: return getAttributeNames(PORTLET_SCOPE);
136: }
137:
138: public Enumeration getAttributeNames(int scope) {
139: if (scope == APPLICATION_SCOPE) {
140: return _httpSession.getAttributeNames();
141: } else if (scope == PORTLET_SCOPE) {
142: return new Enumeration() {
143: private Enumeration _e = _httpSession
144: .getAttributeNames();
145: private Object _next;
146:
147: public boolean hasMoreElements() {
148: nextIfNeeded();
149: return _next != null ? true : false;
150: }
151:
152: public Object nextElement() {
153: nextIfNeeded();
154: if (_next == null)
155: throw new NoSuchElementException();
156: String result = (String) _next;
157: result = result.substring(PORTLET_SCOPE_PREFIX
158: .length());
159: _next = null;
160: return result;
161: }
162:
163: private void nextIfNeeded() {
164: if (_next == null) {
165: while (_e.hasMoreElements()) {
166: String e = (String) _e.nextElement();
167: if (e.startsWith(PORTLET_SCOPE_PREFIX)) {
168: if (!e
169: .startsWith(PORTLET_SCOPE_RESERVED_PREFIX)) {
170: _next = e;
171: break;
172: }
173: }
174: }
175: }
176: }
177: };
178: } else {
179: throw new IllegalArgumentException("invalid scope `"
180: + scope + "'");
181: }
182: }
183:
184: public long getCreationTime() {
185: return _httpSession.getCreationTime();
186: }
187:
188: public String getId() {
189: return _httpSession.getId();
190: }
191:
192: public long getLastAccessedTime() {
193: return _httpSession.getLastAccessedTime();
194: }
195:
196: public int getMaxInactiveInterval() {
197: return _httpSession.getMaxInactiveInterval();
198: }
199:
200: public void invalidate() {
201: _httpSession.invalidate();
202: }
203:
204: public boolean isNew() {
205: return _httpSession.isNew();
206: }
207:
208: public void removeAttribute(String name) {
209: removeAttribute(name, PORTLET_SCOPE);
210: }
211:
212: public void removeAttribute(String name, int scope) {
213: _httpSession.removeAttribute(scopedName(name, scope));
214: }
215:
216: public void setAttribute(String name, Object value) {
217: setAttribute(name, value, PORTLET_SCOPE);
218: }
219:
220: public void setAttribute(String name, Object value, int scope) {
221: _httpSession.setAttribute(scopedName(name, scope), value);
222: }
223:
224: public void setMaxInactiveInterval(int interval) {
225: _httpSession.setMaxInactiveInterval(interval);
226: }
227:
228: }
|