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