1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet;
21  
22  import com.liferay.portal.kernel.portlet.LiferayPortletSession;
23  import com.liferay.portal.kernel.util.StringPool;
24  
25  import java.util.ArrayList;
26  import java.util.Collections;
27  import java.util.Enumeration;
28  import java.util.HashMap;
29  import java.util.List;
30  import java.util.Map;
31  import java.util.StringTokenizer;
32  
33  import javax.portlet.PortletContext;
34  import javax.portlet.PortletSession;
35  
36  import javax.servlet.http.HttpServletRequest;
37  import javax.servlet.http.HttpSession;
38  
39  /**
40   * <a href="PortletSessionImpl.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Brian Wing Shun Chan
43   *
44   */
45  public class PortletSessionImpl implements LiferayPortletSession {
46  
47      public static final String PORTLET_SCOPE_NAMESPACE = "javax.portlet.p.";
48  
49      public static final String LAYOUT_SEPARATOR = "_LAYOUT_";
50  
51      public static final String getPortletScope(String portletName, long plid) {
52          StringBuilder sb = new StringBuilder();
53  
54          sb.append(PORTLET_SCOPE_NAMESPACE);
55          sb.append(portletName);
56          sb.append(LAYOUT_SEPARATOR);
57          sb.append(plid);
58  
59          return sb.toString();
60      }
61  
62      public static final String getPortletScopeName(
63          String portletName, long plid, String name) {
64  
65          StringBuilder sb = new StringBuilder();
66  
67          sb.append(getPortletScope(portletName, plid));
68          sb.append(StringPool.QUESTION);
69          sb.append(name);
70  
71          return sb.toString();
72      }
73  
74      public PortletSessionImpl(
75          HttpServletRequest request, String portletName,
76          PortletContext portletContext, String portalSessionId, long plid) {
77  
78          _request = request;
79          _portletName = portletName;
80          _portletContext = portletContext;
81          _creationTime = System.currentTimeMillis();
82          _lastAccessedTime = _creationTime;
83          _interval = getHttpSession().getMaxInactiveInterval();
84          _new = true;
85          _invalid = false;
86          _portalSessionId = portalSessionId;
87          _plid = plid;
88      }
89  
90      public Object getAttribute(String name) {
91          if (name == null) {
92              throw new IllegalArgumentException();
93          }
94  
95          if (_invalid) {
96              throw new IllegalStateException();
97          }
98  
99          return getAttribute(name, PortletSession.PORTLET_SCOPE);
100     }
101 
102     public Object getAttribute(String name, int scope) {
103         if (name == null) {
104             throw new IllegalArgumentException();
105         }
106 
107         if (_invalid) {
108             throw new IllegalStateException();
109         }
110 
111         if (scope == PortletSession.PORTLET_SCOPE) {
112             return getHttpSession().getAttribute(_getPortletScopeName(name));
113         }
114         else {
115             return getHttpSession().getAttribute(name);
116         }
117     }
118 
119     public Map<String, Object> getAttributeMap() {
120         return getAttributeMap(PortletSession.PORTLET_SCOPE);
121     }
122 
123     public Map<String, Object> getAttributeMap(int scope) {
124         Map<String, Object> map = new HashMap<String, Object>();
125 
126         Enumeration<String> enu = getAttributeNames(scope);
127 
128         while (enu.hasMoreElements()) {
129             String name = enu.nextElement();
130 
131             Object value = getAttribute(name);
132 
133             map.put(name, value);
134         }
135 
136         return map;
137     }
138 
139     public Enumeration<String> getAttributeNames() {
140         if (_invalid) {
141             throw new IllegalStateException();
142         }
143 
144         return getAttributeNames(PortletSession.PORTLET_SCOPE);
145     }
146 
147     public Enumeration<String> getAttributeNames(int scope) {
148         if (_invalid) {
149             throw new IllegalStateException();
150         }
151 
152         if (scope == PortletSession.PORTLET_SCOPE) {
153             List<String> attributeNames = new ArrayList<String>();
154 
155             String portletScope = getPortletScope(_portletName, _plid);
156 
157             Enumeration<String> enu = getHttpSession().getAttributeNames();
158 
159             while (enu.hasMoreElements()) {
160                 String name = enu.nextElement();
161 
162                 StringTokenizer st = new StringTokenizer(
163                     name, StringPool.QUESTION);
164 
165                 if (st.countTokens() == 2) {
166                     if (st.nextToken().equals(portletScope)) {
167                         attributeNames.add(st.nextToken());
168                     }
169                 }
170             }
171 
172             return Collections.enumeration(attributeNames);
173         }
174         else {
175             return getHttpSession().getAttributeNames();
176         }
177     }
178 
179     public long getCreationTime() {
180         if (_invalid) {
181             throw new IllegalStateException();
182         }
183 
184         return _creationTime;
185     }
186 
187     public HttpSession getHttpSession() {
188         if (_session == null) {
189             return _request.getSession();
190         }
191         else {
192             return _session;
193         }
194     }
195 
196     public String getId() {
197         return getHttpSession().getId();
198     }
199 
200     public long getLastAccessedTime() {
201         return _lastAccessedTime;
202     }
203 
204     public int getMaxInactiveInterval() {
205         return _interval;
206     }
207 
208     public String getPortalSessionId() {
209         return _portalSessionId;
210     }
211 
212     public PortletContext getPortletContext() {
213         return _portletContext;
214     }
215 
216     public void invalidate() {
217         if (_invalid) {
218             throw new IllegalStateException();
219         }
220 
221         getHttpSession().invalidate();
222 
223         _invalid = true;
224     }
225 
226     public boolean isNew() {
227         if (_invalid) {
228             throw new IllegalStateException();
229         }
230 
231         return _new;
232     }
233 
234     public boolean isValid() {
235         return !_invalid;
236     }
237 
238     public void removeAttribute(String name) {
239         if (name == null) {
240             throw new IllegalArgumentException();
241         }
242 
243         if (_invalid) {
244             throw new IllegalStateException();
245         }
246 
247         removeAttribute(name, PortletSession.PORTLET_SCOPE);
248     }
249 
250     public void removeAttribute(String name, int scope) {
251         if (name == null) {
252             throw new IllegalArgumentException();
253         }
254 
255         if (_invalid) {
256             throw new IllegalStateException();
257         }
258 
259         if (scope == PortletSession.PORTLET_SCOPE) {
260             getHttpSession().removeAttribute(_getPortletScopeName(name));
261         }
262         else {
263             getHttpSession().removeAttribute(name);
264         }
265     }
266 
267     public void setAttribute(String name, Object value) {
268         if (name == null) {
269             throw new IllegalArgumentException();
270         }
271 
272         if (_invalid) {
273             throw new IllegalStateException();
274         }
275 
276         setAttribute(name, value, PortletSession.PORTLET_SCOPE);
277     }
278 
279     public void setAttribute(String name, Object value, int scope) {
280         if (name == null) {
281             throw new IllegalArgumentException();
282         }
283 
284         if (_invalid) {
285             throw new IllegalStateException();
286         }
287 
288         if (scope == PortletSession.PORTLET_SCOPE) {
289             getHttpSession().setAttribute(_getPortletScopeName(name), value);
290         }
291         else {
292             getHttpSession().setAttribute(name, value);
293         }
294     }
295 
296     public void setHttpSession(HttpSession session) {
297         _session = session;
298     }
299 
300     public void setLastAccessedTime(long lastAccessedTime) {
301         _lastAccessedTime = lastAccessedTime;
302         _new = false;
303     }
304 
305     public void setMaxInactiveInterval(int interval) {
306         _interval = interval;
307     }
308 
309     private String _getPortletScopeName(String name) {
310         return getPortletScopeName(_portletName, _plid, name);
311     }
312 
313     private HttpServletRequest _request;
314     private HttpSession _session;
315     private String _portletName;
316     private PortletContext _portletContext;
317     private long _creationTime;
318     private long _lastAccessedTime;
319     private int _interval;
320     private boolean _new;
321     private boolean _invalid;
322     private String _portalSessionId;
323     private long _plid;
324 
325 }