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
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 int portletScopeLength = portletScope.length();
142
143 Enumeration<String> enu = getHttpSession().getAttributeNames();
144
145 while (enu.hasMoreElements()) {
146 String name = enu.nextElement();
147
148 if ((name.length() > (portletScopeLength + 1)) &&
149 (name.charAt(portletScopeLength) == CharPool.QUESTION) &&
150 name.startsWith(portletScope)) {
151
152 String attributeName = name.substring(
153 portletScopeLength + 1);
154
155 attributeNames.add(attributeName);
156 }
157 }
158
159 return Collections.enumeration(attributeNames);
160 }
161 else {
162 return getHttpSession().getAttributeNames();
163 }
164 }
165
166 public long getCreationTime() {
167 if (_invalid) {
168 throw new IllegalStateException();
169 }
170
171 return _creationTime;
172 }
173
174 public HttpSession getHttpSession() {
175 if (_session == null) {
176 return _request.getSession();
177 }
178 else {
179 return _session;
180 }
181 }
182
183 public String getId() {
184 return getHttpSession().getId();
185 }
186
187 public long getLastAccessedTime() {
188 return _lastAccessedTime;
189 }
190
191 public int getMaxInactiveInterval() {
192 return _interval;
193 }
194
195 public String getPortalSessionId() {
196 return _portalSessionId;
197 }
198
199 public PortletContext getPortletContext() {
200 return _portletContext;
201 }
202
203 public void invalidate() {
204 if (_invalid) {
205 throw new IllegalStateException();
206 }
207
208 getHttpSession().invalidate();
209
210 _invalid = true;
211 }
212
213 public boolean isNew() {
214 if (_invalid) {
215 throw new IllegalStateException();
216 }
217
218 return _new;
219 }
220
221 public boolean isValid() {
222 return !_invalid;
223 }
224
225 public void removeAttribute(String name) {
226 if (name == null) {
227 throw new IllegalArgumentException();
228 }
229
230 if (_invalid) {
231 throw new IllegalStateException();
232 }
233
234 removeAttribute(name, PortletSession.PORTLET_SCOPE);
235 }
236
237 public void removeAttribute(String name, 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 getHttpSession().removeAttribute(_getPortletScopeName(name));
248 }
249 else {
250 getHttpSession().removeAttribute(name);
251 }
252 }
253
254 public void setAttribute(String name, Object value) {
255 if (name == null) {
256 throw new IllegalArgumentException();
257 }
258
259 if (_invalid) {
260 throw new IllegalStateException();
261 }
262
263 setAttribute(name, value, PortletSession.PORTLET_SCOPE);
264 }
265
266 public void setAttribute(String name, Object value, int scope) {
267 if (name == null) {
268 throw new IllegalArgumentException();
269 }
270
271 if (_invalid) {
272 throw new IllegalStateException();
273 }
274
275 if (scope == PortletSession.PORTLET_SCOPE) {
276 getHttpSession().setAttribute(_getPortletScopeName(name), value);
277 }
278 else {
279 getHttpSession().setAttribute(name, value);
280 }
281 }
282
283 public void setHttpSession(HttpSession session) {
284 _session = session;
285 }
286
287 public void setLastAccessedTime(long lastAccessedTime) {
288 _lastAccessedTime = lastAccessedTime;
289 _new = false;
290 }
291
292 public void setMaxInactiveInterval(int interval) {
293 _interval = interval;
294 }
295
296 private String _getPortletScopeName(String name) {
297 return getPortletScopeName(_portletName, _plid, name);
298 }
299
300 private HttpServletRequest _request;
301 private HttpSession _session;
302 private String _portletName;
303 private PortletContext _portletContext;
304 private long _creationTime;
305 private long _lastAccessedTime;
306 private int _interval;
307 private boolean _new;
308 private boolean _invalid;
309 private String _portalSessionId;
310 private long _plid;
311
312 }