001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.environment.portlet;
018:
019: import org.apache.cocoon.environment.Session;
020:
021: import java.util.Enumeration;
022: import java.util.NoSuchElementException;
023:
024: /**
025: * Provides access to the JSR-168 (Portlet) environment session.
026: *
027: * <p>Portlet scope and application scope session attributes are differentiated
028: * using attribute name prefix, {@link PortletEnvironment#SESSION_APPLICATION_SCOPE}.
029: *
030: * @see javax.portlet.PortletSession
031: * @author <a href="mailto:alex.rudnev@dc.gov">Alex Rudnev</a>
032: * @author <a href="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
033: * @version CVS $Id: PortletSession.java 433543 2006-08-22 06:22:54Z crossley $
034: */
035: public final class PortletSession implements Session {
036:
037: private static final String APP_SCOPE = PortletEnvironment.SESSION_APPLICATION_SCOPE;
038: private static final String PORTLET_SCOPE = PortletEnvironment.SESSION_PORTLET_SCOPE;
039:
040: javax.portlet.PortletSession session;
041:
042: /**
043: * Default session scope. One of
044: * {@link javax.portlet.PortletSession#APPLICATION_SCOPE},
045: * {@link javax.portlet.PortletSession#PORTLET_SCOPE}.
046: */
047: private int scope;
048:
049: /**
050: * Construct a new session from an PortletSession
051: */
052: public PortletSession(javax.portlet.PortletSession session,
053: int scope) {
054: this .scope = scope;
055: this .session = session;
056: }
057:
058: /**
059: * Returns the time when this session was created, measured
060: * in milliseconds since midnight January 1, 1970 GMT.
061: *
062: * @return a <code>long</code> specifying
063: * when this session was created,
064: * expressed in
065: * milliseconds since 1/1/1970 GMT
066: *
067: * @exception IllegalStateException if this method is called on an
068: * invalidated session
069: */
070: public long getCreationTime() {
071: return this .session.getCreationTime();
072: }
073:
074: /**
075: * Returns a string containing the unique identifier assigned
076: * to this session. The identifier is assigned
077: * by the context container and is implementation dependent.
078: *
079: * @return a string specifying the identifier
080: * assigned to this session
081: *
082: * @exception IllegalStateException if this method is called on an
083: * invalidated session
084: */
085: public String getId() {
086: return this .session.getId();
087: }
088:
089: /**
090: * Returns the last time the client sent a request associated with
091: * this session, as the number of milliseconds since midnight
092: * January 1, 1970 GMT.
093: *
094: * <p>Actions that your application takes, such as getting or setting
095: * a value associated with the session, do not affect the access
096: * time.
097: *
098: * @return a <code>long</code>
099: * representing the last time
100: * the client sent a request associated
101: * with this session, expressed in
102: * milliseconds since 1/1/1970 GMT
103: *
104: * @exception IllegalStateException if this method is called on an
105: * invalidated session
106: */
107: public long getLastAccessedTime() {
108: return this .session.getLastAccessedTime();
109: }
110:
111: /**
112: * Specifies the time, in seconds, between client requests before the
113: * contextcontainer will invalidate this session. A negative time
114: * indicates the session should never timeout.
115: *
116: * @param interval An integer specifying the number
117: * of seconds
118: */
119: public void setMaxInactiveInterval(int interval) {
120: this .session.setMaxInactiveInterval(interval);
121: }
122:
123: /**
124: * Returns the maximum time interval, in seconds, that
125: * the context container will keep this session open between
126: * client accesses. After this interval, the context container
127: * will invalidate the session. The maximum time interval can be set
128: * with the <code>setMaxInactiveInterval</code> method.
129: * A negative time indicates the session should never timeout.
130: *
131: *
132: * @return an integer specifying the number of
133: * seconds this session remains open
134: * between client requests
135: *
136: * @see #setMaxInactiveInterval(int)
137: */
138: public int getMaxInactiveInterval() {
139: return this .session.getMaxInactiveInterval();
140: }
141:
142: /**
143: * Returns the object bound with the specified name in this session, or
144: * <code>null</code> if no object is bound under the name.
145: *
146: * @param name a string specifying the name of the object
147: *
148: * @return the object with the specified name
149: *
150: * @exception IllegalStateException if this method is called on an
151: * invalidated session
152: */
153: public Object getAttribute(String name) {
154: if (name.startsWith(APP_SCOPE)) {
155: return this .session.getAttribute(name.substring(APP_SCOPE
156: .length()),
157: javax.portlet.PortletSession.APPLICATION_SCOPE);
158: } else if (name.startsWith(PORTLET_SCOPE)) {
159: return this .session.getAttribute(name
160: .substring(PORTLET_SCOPE.length()),
161: javax.portlet.PortletSession.PORTLET_SCOPE);
162: } else {
163: return this .session.getAttribute(name, this .scope);
164: }
165: }
166:
167: /**
168: * Returns an <code>Enumeration</code> of <code>String</code> objects
169: * containing the names of all the objects bound to this session.
170: *
171: * <p>Objects' names in portlet session scope will be prefixed with
172: * {@link PortletEnvironment#SESSION_PORTLET_SCOPE}, and names in
173: * application scope will be prefixed with
174: * {@link PortletEnvironment#SESSION_APPLICATION_SCOPE}.</p>
175: *
176: * @return an <code>Enumeration</code> of
177: * <code>String</code> objects specifying the
178: * names of all the objects bound to
179: * this session
180: *
181: * @exception IllegalStateException if this method is called on an
182: * invalidated session
183: */
184: public Enumeration getAttributeNames() {
185: final Enumeration names1 = this .session
186: .getAttributeNames(javax.portlet.PortletSession.PORTLET_SCOPE);
187: final Enumeration names2 = this .session
188: .getAttributeNames(javax.portlet.PortletSession.APPLICATION_SCOPE);
189:
190: return new Enumeration() {
191: public boolean hasMoreElements() {
192: return names1.hasMoreElements()
193: || names2.hasMoreElements();
194: }
195:
196: public Object nextElement() throws NoSuchElementException {
197: if (names1.hasMoreElements()) {
198: return PORTLET_SCOPE + names1.nextElement();
199: } else if (names2.hasMoreElements()) {
200: return APP_SCOPE + names2.nextElement();
201: }
202:
203: throw new NoSuchElementException();
204: }
205: };
206: }
207:
208: /**
209: * Binds an object to this session, using the name specified.
210: * If an object of the same name is already bound to the session,
211: * the object is replaced.
212: *
213: *
214: * @param name the name to which the object is bound;
215: * cannot be null
216: *
217: * @param value the object to be bound; cannot be null
218: *
219: * @exception IllegalStateException if this method is called on an
220: * invalidated session
221: */
222: public void setAttribute(String name, Object value) {
223: if (name.startsWith(APP_SCOPE)) {
224: this .session.setAttribute(name
225: .substring(APP_SCOPE.length()), value,
226: javax.portlet.PortletSession.APPLICATION_SCOPE);
227: } else if (name.startsWith(PORTLET_SCOPE)) {
228: this .session.setAttribute(name.substring(PORTLET_SCOPE
229: .length()), value,
230: javax.portlet.PortletSession.PORTLET_SCOPE);
231: } else {
232: this .session.setAttribute(name, value, this .scope);
233: }
234: }
235:
236: /**
237: * Removes the object bound with the specified name from
238: * this session. If the session does not have an object
239: * bound with the specified name, this method does nothing.
240: *
241: *
242: * @param name the name of the object to
243: * remove from this session
244: *
245: * @exception IllegalStateException if this method is called on an
246: * invalidated session
247: */
248: public void removeAttribute(String name) {
249: if (name.startsWith(APP_SCOPE)) {
250: this .session.removeAttribute(name.substring(APP_SCOPE
251: .length()),
252: javax.portlet.PortletSession.APPLICATION_SCOPE);
253: } else if (name.startsWith(PORTLET_SCOPE)) {
254: this .session.removeAttribute(name.substring(PORTLET_SCOPE
255: .length()),
256: javax.portlet.PortletSession.PORTLET_SCOPE);
257: } else {
258: this .session.removeAttribute(name, this .scope);
259: }
260: }
261:
262: /**
263: * Invalidates this session to it.
264: *
265: * @exception IllegalStateException if this method is called on an
266: * already invalidated session
267: */
268: public void invalidate() {
269: this .session.invalidate();
270: }
271:
272: /**
273: * Returns <code>true</code> if the client does not yet know about the
274: * session or if the client chooses not to join the session. For
275: * example, if the server used only cookie-based sessions, and
276: * the client had disabled the use of cookies, then a session would
277: * be new on each request.
278: *
279: * @return <code>true</code> if the
280: * server has created a session,
281: * but the client has not yet joined
282: *
283: * @exception IllegalStateException if this method is called on an
284: * already invalidated session
285: */
286: public boolean isNew() {
287: return this.session.isNew();
288: }
289: }
|