1
14
15 package com.liferay.portlet;
16
17 import com.liferay.portal.kernel.portlet.LiferayPortletSession;
18 import com.liferay.portal.kernel.util.StringBundler;
19 import com.liferay.portal.kernel.util.StringPool;
20
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.Enumeration;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.StringTokenizer;
28
29 import javax.portlet.PortletContext;
30 import javax.portlet.PortletSession;
31
32 import javax.servlet.http.HttpServletRequest;
33 import javax.servlet.http.HttpSession;
34
35
40 public class PortletSessionImpl implements LiferayPortletSession {
41
42 public static final String PORTLET_SCOPE_NAMESPACE = "javax.portlet.p.";
43
44 public static final String LAYOUT_SEPARATOR = "_LAYOUT_";
45
46 public static final String getPortletScope(String portletName, long plid) {
47 StringBundler sb = new StringBundler(4);
48
49 sb.append(PORTLET_SCOPE_NAMESPACE);
50 sb.append(portletName);
51 sb.append(LAYOUT_SEPARATOR);
52 sb.append(plid);
53
54 return sb.toString();
55 }
56
57 public static final String getPortletScopeName(
58 String portletName, long plid, String name) {
59
60 return getPortletScope(portletName, plid).concat(
61 StringPool.QUESTION).concat(name);
62 }
63
64 public PortletSessionImpl(
65 HttpServletRequest request, String portletName,
66 PortletContext portletContext, String portalSessionId, long plid) {
67
68 _request = request;
69 _portletName = portletName;
70 _portletContext = portletContext;
71 _creationTime = System.currentTimeMillis();
72 _lastAccessedTime = _creationTime;
73 _interval = getHttpSession().getMaxInactiveInterval();
74 _new = true;
75 _invalid = false;
76 _portalSessionId = portalSessionId;
77 _plid = plid;
78 }
79
80 public Object getAttribute(String name) {
81 if (name == null) {
82 throw new IllegalArgumentException();
83 }
84
85 if (_invalid) {
86 throw new IllegalStateException();
87 }
88
89 return getAttribute(name, PortletSession.PORTLET_SCOPE);
90 }
91
92 public Object getAttribute(String name, int scope) {
93 if (name == null) {
94 throw new IllegalArgumentException();
95 }
96
97 if (_invalid) {
98 throw new IllegalStateException();
99 }
100
101 if (scope == PortletSession.PORTLET_SCOPE) {
102 return getHttpSession().getAttribute(_getPortletScopeName(name));
103 }
104 else {
105 return getHttpSession().getAttribute(name);
106 }
107 }
108
109 public Map<String, Object> getAttributeMap() {
110 return getAttributeMap(PortletSession.PORTLET_SCOPE);
111 }
112
113 public Map<String, Object> getAttributeMap(int scope) {
114 Map<String, Object> map = new HashMap<String, Object>();
115
116 Enumeration<String> enu = getAttributeNames(scope);
117
118 while (enu.hasMoreElements()) {
119 String name = enu.nextElement();
120
121 Object value = getAttribute(name);
122
123 map.put(name, value);
124 }
125
126 return map;
127 }
128
129 public Enumeration<String> getAttributeNames() {
130 if (_invalid) {
131 throw new IllegalStateException();
132 }
133
134 return getAttributeNames(PortletSession.PORTLET_SCOPE);
135 }
136
137 public Enumeration<String> getAttributeNames(int scope) {
138 if (_invalid) {
139 throw new IllegalStateException();
140 }
141
142 if (scope == PortletSession.PORTLET_SCOPE) {
143 List<String> attributeNames = new ArrayList<String>();
144
145 String portletScope = getPortletScope(_portletName, _plid);
146
147 Enumeration<String> enu = getHttpSession().getAttributeNames();
148
149 while (enu.hasMoreElements()) {
150 String name = enu.nextElement();
151
152 StringTokenizer st = new StringTokenizer(
153 name, StringPool.QUESTION);
154
155 if (st.countTokens() == 2) {
156 if (st.nextToken().equals(portletScope)) {
157 attributeNames.add(st.nextToken());
158 }
159 }
160 }
161
162 return Collections.enumeration(attributeNames);
163 }
164 else {
165 return getHttpSession().getAttributeNames();
166 }
167 }
168
169 public long getCreationTime() {
170 if (_invalid) {
171 throw new IllegalStateException();
172 }
173
174 return _creationTime;
175 }
176
177 public HttpSession getHttpSession() {
178 if (_session == null) {
179 return _request.getSession();
180 }
181 else {
182 return _session;
183 }
184 }
185
186 public String getId() {
187 return getHttpSession().getId();
188 }
189
190 public long getLastAccessedTime() {
191 return _lastAccessedTime;
192 }
193
194 public int getMaxInactiveInterval() {
195 return _interval;
196 }
197
198 public String getPortalSessionId() {
199 return _portalSessionId;
200 }
201
202 public PortletContext getPortletContext() {
203 return _portletContext;
204 }
205
206 public void invalidate() {
207 if (_invalid) {
208 throw new IllegalStateException();
209 }
210
211 getHttpSession().invalidate();
212
213 _invalid = true;
214 }
215
216 public boolean isNew() {
217 if (_invalid) {
218 throw new IllegalStateException();
219 }
220
221 return _new;
222 }
223
224 public boolean isValid() {
225 return !_invalid;
226 }
227
228 public void removeAttribute(String name) {
229 if (name == null) {
230 throw new IllegalArgumentException();
231 }
232
233 if (_invalid) {
234 throw new IllegalStateException();
235 }
236
237 removeAttribute(name, PortletSession.PORTLET_SCOPE);
238 }
239
240 public void removeAttribute(String name, int scope) {
241 if (name == null) {
242 throw new IllegalArgumentException();
243 }
244
245 if (_invalid) {
246 throw new IllegalStateException();
247 }
248
249 if (scope == PortletSession.PORTLET_SCOPE) {
250 getHttpSession().removeAttribute(_getPortletScopeName(name));
251 }
252 else {
253 getHttpSession().removeAttribute(name);
254 }
255 }
256
257 public void setAttribute(String name, Object value) {
258 if (name == null) {
259 throw new IllegalArgumentException();
260 }
261
262 if (_invalid) {
263 throw new IllegalStateException();
264 }
265
266 setAttribute(name, value, PortletSession.PORTLET_SCOPE);
267 }
268
269 public void setAttribute(String name, Object value, int scope) {
270 if (name == null) {
271 throw new IllegalArgumentException();
272 }
273
274 if (_invalid) {
275 throw new IllegalStateException();
276 }
277
278 if (scope == PortletSession.PORTLET_SCOPE) {
279 getHttpSession().setAttribute(_getPortletScopeName(name), value);
280 }
281 else {
282 getHttpSession().setAttribute(name, value);
283 }
284 }
285
286 public void setHttpSession(HttpSession session) {
287 _session = session;
288 }
289
290 public void setLastAccessedTime(long lastAccessedTime) {
291 _lastAccessedTime = lastAccessedTime;
292 _new = false;
293 }
294
295 public void setMaxInactiveInterval(int interval) {
296 _interval = interval;
297 }
298
299 private String _getPortletScopeName(String name) {
300 return getPortletScopeName(_portletName, _plid, name);
301 }
302
303 private HttpServletRequest _request;
304 private HttpSession _session;
305 private String _portletName;
306 private PortletContext _portletContext;
307 private long _creationTime;
308 private long _lastAccessedTime;
309 private int _interval;
310 private boolean _new;
311 private boolean _invalid;
312 private String _portalSessionId;
313 private long _plid;
314
315 }