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