001
014
015 package com.liferay.portlet;
016
017 import com.liferay.portal.kernel.portlet.LiferayPortletSession;
018 import com.liferay.portal.kernel.util.CharPool;
019 import com.liferay.portal.kernel.util.StringBundler;
020 import com.liferay.portal.kernel.util.StringPool;
021
022 import java.util.ArrayList;
023 import java.util.Collections;
024 import java.util.Enumeration;
025 import java.util.HashMap;
026 import java.util.List;
027 import java.util.Map;
028
029 import javax.portlet.PortletContext;
030 import javax.portlet.PortletSession;
031
032 import javax.servlet.http.HttpServletRequest;
033 import javax.servlet.http.HttpSession;
034
035
039 public class PortletSessionImpl implements LiferayPortletSession {
040
041 public static String getPortletScope(String portletName, long plid) {
042 StringBundler sb = new StringBundler(4);
043
044 sb.append(PORTLET_SCOPE_NAMESPACE);
045 sb.append(portletName);
046 sb.append(LAYOUT_SEPARATOR);
047 sb.append(plid);
048
049 return sb.toString();
050 }
051
052 public static String getPortletScopeName(
053 String portletName, long plid, String name) {
054
055 StringBundler sb = new StringBundler(6);
056
057 sb.append(PORTLET_SCOPE_NAMESPACE);
058 sb.append(portletName);
059 sb.append(LAYOUT_SEPARATOR);
060 sb.append(plid);
061 sb.append(StringPool.QUESTION);
062 sb.append(name);
063
064 return sb.toString();
065 }
066
067 public PortletSessionImpl(
068 HttpServletRequest request, String portletName,
069 PortletContext portletContext, String portalSessionId, long plid) {
070
071 _session = request.getSession();
072 _portletName = portletName;
073 _portletContext = portletContext;
074 _creationTime = System.currentTimeMillis();
075 _lastAccessedTime = _creationTime;
076 _interval = _session.getMaxInactiveInterval();
077 _new = true;
078 _invalid = false;
079 _portalSessionId = portalSessionId;
080 _plid = plid;
081 }
082
083 public Object getAttribute(String name) {
084 if (name == null) {
085 throw new IllegalArgumentException();
086 }
087
088 if (_invalid) {
089 throw new IllegalStateException();
090 }
091
092 return _session.getAttribute(_getPortletScopeName(name));
093 }
094
095 public Object getAttribute(String name, int scope) {
096 if (name == null) {
097 throw new IllegalArgumentException();
098 }
099
100 if (_invalid) {
101 throw new IllegalStateException();
102 }
103
104 if (scope == PortletSession.PORTLET_SCOPE) {
105 name = _getPortletScopeName(name);
106 }
107
108 return _session.getAttribute(name);
109 }
110
111 public Map<String, Object> getAttributeMap() {
112 return getAttributeMap(PortletSession.PORTLET_SCOPE);
113 }
114
115 public Map<String, Object> getAttributeMap(int scope) {
116 Map<String, Object> map = new HashMap<String, Object>();
117
118 Enumeration<String> enu = _getAttributeNames(scope, false);
119
120 String portletScope = getPortletScope(_portletName, _plid);
121
122 int portletScopeLength = portletScope.length();
123
124 while (enu.hasMoreElements()) {
125 String name = enu.nextElement();
126
127 Object value = _session.getAttribute(name);
128
129 map.put(name.substring(portletScopeLength + 1), value);
130 }
131
132 return map;
133 }
134
135 public Enumeration<String> getAttributeNames() {
136 return _getAttributeNames(PortletSession.PORTLET_SCOPE, true);
137 }
138
139 public Enumeration<String> getAttributeNames(int scope) {
140 return _getAttributeNames(scope, true);
141 }
142
143 public long getCreationTime() {
144 if (_invalid) {
145 throw new IllegalStateException();
146 }
147
148 return _creationTime;
149 }
150
151 public HttpSession getHttpSession() {
152 return _session;
153 }
154
155 public String getId() {
156 return _session.getId();
157 }
158
159 public long getLastAccessedTime() {
160 return _lastAccessedTime;
161 }
162
163 public int getMaxInactiveInterval() {
164 return _interval;
165 }
166
167 public String getPortalSessionId() {
168 return _portalSessionId;
169 }
170
171 public PortletContext getPortletContext() {
172 return _portletContext;
173 }
174
175 public void invalidate() {
176 if (_invalid) {
177 throw new IllegalStateException();
178 }
179
180 _session.invalidate();
181
182 _invalid = true;
183 }
184
185 public boolean isNew() {
186 if (_invalid) {
187 throw new IllegalStateException();
188 }
189
190 return _new;
191 }
192
193 public boolean isValid() {
194 return !_invalid;
195 }
196
197 public void removeAttribute(String name) {
198 if (name == null) {
199 throw new IllegalArgumentException();
200 }
201
202 if (_invalid) {
203 throw new IllegalStateException();
204 }
205
206 _session.removeAttribute(_getPortletScopeName(name));
207 }
208
209 public void removeAttribute(String name, int scope) {
210 if (name == null) {
211 throw new IllegalArgumentException();
212 }
213
214 if (_invalid) {
215 throw new IllegalStateException();
216 }
217
218 if (scope == PortletSession.PORTLET_SCOPE) {
219 name = _getPortletScopeName(name);
220 }
221
222 _session.removeAttribute(name);
223 }
224
225 public void setAttribute(String name, Object value) {
226 if (name == null) {
227 throw new IllegalArgumentException();
228 }
229
230 if (_invalid) {
231 throw new IllegalStateException();
232 }
233
234 _session.setAttribute(_getPortletScopeName(name), value);
235 }
236
237 public void setAttribute(String name, Object value, int scope) {
238 if (name == null) {
239 throw new IllegalArgumentException();
240 }
241
242 if (_invalid) {
243 throw new IllegalStateException();
244 }
245
246 if (scope == PortletSession.PORTLET_SCOPE) {
247 name = _getPortletScopeName(name);
248 }
249
250 _session.setAttribute(name, value);
251 }
252
253 public void setHttpSession(HttpSession session) {
254 _session = session;
255 }
256
257 public void setLastAccessedTime(long lastAccessedTime) {
258 _lastAccessedTime = lastAccessedTime;
259 _new = false;
260 }
261
262 public void setMaxInactiveInterval(int interval) {
263 _interval = interval;
264 }
265
266 private Enumeration<String> _getAttributeNames(
267 int scope, boolean removePrefix) {
268
269 if (_invalid) {
270 throw new IllegalStateException();
271 }
272
273 if (scope == PortletSession.PORTLET_SCOPE) {
274 List<String> attributeNames = new ArrayList<String>();
275
276 String portletScope = getPortletScope(_portletName, _plid);
277
278 int portletScopeLength = portletScope.length();
279
280 Enumeration<String> enu = _session.getAttributeNames();
281
282 while (enu.hasMoreElements()) {
283 String name = enu.nextElement();
284
285 if ((name.length() > (portletScopeLength + 1)) &&
286 (name.charAt(portletScopeLength) == CharPool.QUESTION) &&
287 name.startsWith(portletScope)) {
288
289 if (removePrefix) {
290 name = name.substring(portletScopeLength + 1);
291 }
292
293 attributeNames.add(name);
294 }
295 }
296
297 return Collections.enumeration(attributeNames);
298 }
299 else {
300 return _session.getAttributeNames();
301 }
302 }
303
304 private String _getPortletScopeName(String name) {
305 return getPortletScopeName(_portletName, _plid, name);
306 }
307
308 private long _creationTime;
309 private int _interval;
310 private boolean _invalid;
311 private long _lastAccessedTime;
312 private boolean _new;
313 private long _plid;
314 private String _portalSessionId;
315 private PortletContext _portletContext;
316 private String _portletName;
317 private HttpSession _session;
318
319 }