001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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    
028    import javax.portlet.PortletContext;
029    import javax.portlet.PortletSession;
030    
031    import javax.servlet.http.HttpSession;
032    
033    /**
034     * @author Brian Wing Shun Chan
035     * @author Shuyang Zhou
036     */
037    public class PortletSessionImpl implements LiferayPortletSession {
038    
039            public PortletSessionImpl(
040                    HttpSession session, PortletContext portletContext, String portletName,
041                    long plid) {
042    
043                    _session = session;
044                    _portletContext = portletContext;
045                    _portletScope = _getPortletScope(portletName, plid);
046            }
047    
048            @Override
049            public Object getAttribute(String name) {
050                    if (name == null) {
051                            throw new IllegalArgumentException();
052                    }
053    
054                    String scopeName = _getPortletScopeName(name);
055    
056                    return _session.getAttribute(scopeName);
057            }
058    
059            @Override
060            public Object getAttribute(String name, int scope) {
061                    if (name == null) {
062                            throw new IllegalArgumentException();
063                    }
064    
065                    if (scope == PortletSession.PORTLET_SCOPE) {
066                            name = _getPortletScopeName(name);
067                    }
068    
069                    return _session.getAttribute(name);
070            }
071    
072            @Override
073            public Map<String, Object> getAttributeMap() {
074                    return getAttributeMap(PortletSession.PORTLET_SCOPE);
075            }
076    
077            @Override
078            public Map<String, Object> getAttributeMap(int scope) {
079                    Map<String, Object> map = new HashMap<String, Object>();
080    
081                    Enumeration<String> enu = _getAttributeNames(scope, false);
082    
083                    int portletScopeLength = _portletScope.length();
084    
085                    while (enu.hasMoreElements()) {
086                            String name = enu.nextElement();
087    
088                            Object value = _session.getAttribute(name);
089    
090                            if (scope == PortletSession.PORTLET_SCOPE) {
091                                    if ((name.length() <= (portletScopeLength + 1)) ||
092                                            !name.startsWith(_portletScope + StringPool.QUESTION)) {
093    
094                                            continue;
095                                    }
096    
097                                    name = name.substring(portletScopeLength + 1);
098                            }
099    
100                            map.put(name, value);
101                    }
102    
103                    return map;
104            }
105    
106            @Override
107            public Enumeration<String> getAttributeNames() {
108                    return _getAttributeNames(PortletSession.PORTLET_SCOPE, true);
109            }
110    
111            @Override
112            public Enumeration<String> getAttributeNames(int scope) {
113                    return _getAttributeNames(scope, true);
114            }
115    
116            @Override
117            public long getCreationTime() {
118                    return _session.getCreationTime();
119            }
120    
121            public HttpSession getHttpSession() {
122                    return _session;
123            }
124    
125            @Override
126            public String getId() {
127                    return _session.getId();
128            }
129    
130            @Override
131            public long getLastAccessedTime() {
132                    return _session.getLastAccessedTime();
133            }
134    
135            @Override
136            public int getMaxInactiveInterval() {
137                    return _session.getMaxInactiveInterval();
138            }
139    
140            @Override
141            public PortletContext getPortletContext() {
142                    return _portletContext;
143            }
144    
145            @Override
146            public void invalidate() {
147                    _session.invalidate();
148            }
149    
150            @Override
151            public boolean isNew() {
152                    return _session.isNew();
153            }
154    
155            @Override
156            public void removeAttribute(String name) {
157                    if (name == null) {
158                            throw new IllegalArgumentException();
159                    }
160    
161                    String scopeName = _getPortletScopeName(name);
162    
163                    _session.removeAttribute(scopeName);
164            }
165    
166            @Override
167            public void removeAttribute(String name, int scope) {
168                    if (name == null) {
169                            throw new IllegalArgumentException();
170                    }
171    
172                    if (scope == PortletSession.PORTLET_SCOPE) {
173                            name = _getPortletScopeName(name);
174                    }
175    
176                    _session.removeAttribute(name);
177            }
178    
179            @Override
180            public void setAttribute(String name, Object value) {
181                    if (name == null) {
182                            throw new IllegalArgumentException();
183                    }
184    
185                    String scopeName = _getPortletScopeName(name);
186    
187                    _session.setAttribute(scopeName, value);
188            }
189    
190            @Override
191            public void setAttribute(String name, Object value, int scope) {
192                    if (name == null) {
193                            throw new IllegalArgumentException();
194                    }
195    
196                    if (scope == PortletSession.PORTLET_SCOPE) {
197                            name = _getPortletScopeName(name);
198                    }
199    
200                    _session.setAttribute(name, value);
201            }
202    
203            @Override
204            public void setHttpSession(HttpSession session) {
205                    _session = session;
206            }
207    
208            @Override
209            public void setMaxInactiveInterval(int interval) {
210                    _session.setMaxInactiveInterval(interval);
211            }
212    
213            private Enumeration<String> _getAttributeNames(
214                    int scope, boolean removePrefix) {
215    
216                    if (scope != PortletSession.PORTLET_SCOPE) {
217                            return _session.getAttributeNames();
218                    }
219    
220                    List<String> attributeNames = new ArrayList<String>();
221    
222                    int portletScopeLength = _portletScope.length();
223    
224                    Enumeration<String> enu = _session.getAttributeNames();
225    
226                    while (enu.hasMoreElements()) {
227                            String name = enu.nextElement();
228    
229                            if (removePrefix) {
230                                    if ((name.length() <= (portletScopeLength + 1)) ||
231                                            !name.startsWith(_portletScope + StringPool.QUESTION)) {
232    
233                                            continue;
234                                    }
235    
236                                    name = name.substring(portletScopeLength + 1);
237                            }
238    
239                            attributeNames.add(name);
240                    }
241    
242                    return Collections.enumeration(attributeNames);
243            }
244    
245            private String _getPortletScope(String portletName, long plid) {
246                    StringBundler sb = new StringBundler(4);
247    
248                    sb.append(PORTLET_SCOPE_NAMESPACE);
249                    sb.append(portletName);
250                    sb.append(LAYOUT_SEPARATOR);
251                    sb.append(plid);
252    
253                    return sb.toString();
254            }
255    
256            private String _getPortletScopeName(String name) {
257                    return _portletScope.concat(StringPool.QUESTION).concat(name);
258            }
259    
260            private PortletContext _portletContext;
261            private String _portletScope;
262            private HttpSession _session;
263    
264    }