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