1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet;
24  
25  import com.liferay.portal.kernel.portlet.LiferayPortletSession;
26  import com.liferay.portal.kernel.util.StringPool;
27  
28  import java.util.ArrayList;
29  import java.util.Collections;
30  import java.util.Enumeration;
31  import java.util.List;
32  import java.util.StringTokenizer;
33  
34  import javax.portlet.PortletContext;
35  import javax.portlet.PortletSession;
36  
37  import javax.servlet.http.HttpServletRequest;
38  import javax.servlet.http.HttpSession;
39  
40  /**
41   * <a href="PortletSessionImpl.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Brian Wing Shun Chan
44   *
45   */
46  public class PortletSessionImpl implements LiferayPortletSession {
47  
48      public static final String getPortletScope(String portletName, long plid) {
49          String portletScope =
50              _PORTLET_SCOPE_NAMESPACE + portletName + _LAYOUT_SEPARATOR +
51                  plid;
52  
53          return portletScope;
54      }
55  
56      public static final String getPortletScopeName(
57          String portletName, long plid, String name) {
58  
59          String portletScopeName =
60              getPortletScope(portletName, plid) + StringPool.QUESTION + name;
61  
62          return portletScopeName;
63      }
64  
65      public PortletSessionImpl(HttpServletRequest req, String portletName,
66                                PortletContext ctx, String portalSessionId,
67                                long plid) {
68  
69          _req = req;
70          _portletName = portletName;
71          _ctx = ctx;
72          _creationTime = System.currentTimeMillis();
73          _lastAccessedTime = _creationTime;
74          _interval = getHttpSession().getMaxInactiveInterval();
75          _new = true;
76          _invalid = false;
77          _portalSessionId = portalSessionId;
78          _plid = plid;
79      }
80  
81      public PortletContext getPortletContext() {
82          return _ctx;
83      }
84  
85      public String getId() {
86          return getHttpSession().getId();
87      }
88  
89      public long getCreationTime() {
90          if (_invalid) {
91              throw new IllegalStateException();
92          }
93  
94          return _creationTime;
95      }
96  
97      public long getLastAccessedTime() {
98          return _lastAccessedTime;
99      }
100 
101     public void setLastAccessedTime(long lastAccessedTime) {
102         _lastAccessedTime = lastAccessedTime;
103         _new = false;
104     }
105 
106     public int getMaxInactiveInterval() {
107         return _interval;
108     }
109 
110     public void setMaxInactiveInterval(int interval) {
111         _interval = interval;
112     }
113 
114     public boolean isNew() {
115         if (_invalid) {
116             throw new IllegalStateException();
117         }
118 
119         return _new;
120     }
121 
122     public Object getAttribute(String name) {
123         if (name == null) {
124             throw new IllegalArgumentException();
125         }
126 
127         if (_invalid) {
128             throw new IllegalStateException();
129         }
130 
131         return getAttribute(name, PortletSession.PORTLET_SCOPE);
132     }
133 
134     public Object getAttribute(String name, int scope) {
135         if (name == null) {
136             throw new IllegalArgumentException();
137         }
138 
139         if (_invalid) {
140             throw new IllegalStateException();
141         }
142 
143         if (scope == PortletSession.PORTLET_SCOPE) {
144             return getHttpSession().getAttribute(_getPortletScopeName(name));
145         }
146         else {
147             return getHttpSession().getAttribute(name);
148         }
149     }
150 
151     public Enumeration getAttributeNames() {
152         if (_invalid) {
153             throw new IllegalStateException();
154         }
155 
156         return getAttributeNames(PortletSession.PORTLET_SCOPE);
157     }
158 
159     public Enumeration getAttributeNames(int scope) {
160         if (_invalid) {
161             throw new IllegalStateException();
162         }
163 
164         if (scope == PortletSession.PORTLET_SCOPE) {
165             List attributeNames = new ArrayList();
166 
167             String portletScope = getPortletScope(_portletName, _plid);
168 
169             Enumeration enu = getHttpSession().getAttributeNames();
170 
171             while (enu.hasMoreElements()) {
172                 String name = (String)enu.nextElement();
173 
174                 StringTokenizer st = new StringTokenizer(
175                     name, StringPool.QUESTION);
176 
177                 if (st.countTokens() == 2) {
178                     if (st.nextToken().equals(portletScope)) {
179                         attributeNames.add(st.nextToken());
180                     }
181                 }
182             }
183 
184             return Collections.enumeration(attributeNames);
185         }
186         else {
187             return getHttpSession().getAttributeNames();
188         }
189     }
190 
191     public void removeAttribute(String name) {
192         if (name == null) {
193             throw new IllegalArgumentException();
194         }
195 
196         if (_invalid) {
197             throw new IllegalStateException();
198         }
199 
200         removeAttribute(name, PortletSession.PORTLET_SCOPE);
201     }
202 
203     public void removeAttribute(String name, int scope) {
204         if (name == null) {
205             throw new IllegalArgumentException();
206         }
207 
208         if (_invalid) {
209             throw new IllegalStateException();
210         }
211 
212         if (scope == PortletSession.PORTLET_SCOPE) {
213             getHttpSession().removeAttribute(_getPortletScopeName(name));
214         }
215         else {
216             getHttpSession().removeAttribute(name);
217         }
218     }
219 
220     public void setAttribute(String name, Object value) {
221         if (name == null) {
222             throw new IllegalArgumentException();
223         }
224 
225         if (_invalid) {
226             throw new IllegalStateException();
227         }
228 
229         setAttribute(name, value, PortletSession.PORTLET_SCOPE);
230     }
231 
232     public void setAttribute(String name, Object value, int scope) {
233         if (name == null) {
234             throw new IllegalArgumentException();
235         }
236 
237         if (_invalid) {
238             throw new IllegalStateException();
239         }
240 
241         if (scope == PortletSession.PORTLET_SCOPE) {
242             getHttpSession().setAttribute(_getPortletScopeName(name), value);
243         }
244         else {
245             getHttpSession().setAttribute(name, value);
246         }
247     }
248 
249     public void invalidate() {
250         if (_invalid) {
251             throw new IllegalStateException();
252         }
253 
254         getHttpSession().invalidate();
255 
256         _invalid = true;
257     }
258 
259     public boolean isValid() {
260         return !_invalid;
261     }
262 
263     public String getPortalSessionId() {
264         return _portalSessionId;
265     }
266 
267     public HttpSession getHttpSession() {
268         if (_ses == null) {
269             return _req.getSession();
270         }
271         else {
272             return _ses;
273         }
274     }
275 
276     public void setHttpSession(HttpSession ses) {
277         _ses = ses;
278     }
279 
280     private String _getPortletScopeName(String name) {
281         return getPortletScopeName(_portletName, _plid, name);
282     }
283 
284     private static final String _PORTLET_SCOPE_NAMESPACE = "javax.portlet.p.";
285 
286     private static final String _LAYOUT_SEPARATOR = "_LAYOUT_";
287 
288     private HttpServletRequest _req;
289     private HttpSession _ses;
290     private String _portletName;
291     private PortletContext _ctx;
292     private long _creationTime;
293     private long _lastAccessedTime;
294     private int _interval;
295     private boolean _new;
296     private boolean _invalid;
297     private String _portalSessionId;
298     private long _plid;
299 
300 }