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            public Object getAttribute(String name) {
049                    if (name == null) {
050                            throw new IllegalArgumentException();
051                    }
052    
053                    String scopeName = _getPortletScopeName(name);
054    
055                    return _session.getAttribute(scopeName);
056            }
057    
058            public Object getAttribute(String name, int scope) {
059                    if (name == null) {
060                            throw new IllegalArgumentException();
061                    }
062    
063                    if (scope == PortletSession.PORTLET_SCOPE) {
064                            name = _getPortletScopeName(name);
065                    }
066    
067                    return _session.getAttribute(name);
068            }
069    
070            public Map<String, Object> getAttributeMap() {
071                    return getAttributeMap(PortletSession.PORTLET_SCOPE);
072            }
073    
074            public Map<String, Object> getAttributeMap(int scope) {
075                    Map<String, Object> map = new HashMap<String, Object>();
076    
077                    Enumeration<String> enu = _getAttributeNames(scope, false);
078    
079                    int portletScopeLength = _portletScope.length();
080    
081                    while (enu.hasMoreElements()) {
082                            String name = enu.nextElement();
083    
084                            Object value = _session.getAttribute(name);
085    
086                            if (scope == PortletSession.PORTLET_SCOPE) {
087                                    if ((name.length() <= (portletScopeLength + 1)) ||
088                                            !name.startsWith(_portletScope + StringPool.QUESTION)) {
089    
090                                            continue;
091                                    }
092    
093                                    name = name.substring(portletScopeLength + 1);
094                            }
095    
096                            map.put(name, value);
097                    }
098    
099                    return map;
100            }
101    
102            public Enumeration<String> getAttributeNames() {
103                    return _getAttributeNames(PortletSession.PORTLET_SCOPE, true);
104            }
105    
106            public Enumeration<String> getAttributeNames(int scope) {
107                    return _getAttributeNames(scope, true);
108            }
109    
110            public long getCreationTime() {
111                    return _session.getCreationTime();
112            }
113    
114            public HttpSession getHttpSession() {
115                    return _session;
116            }
117    
118            public String getId() {
119                    return _session.getId();
120            }
121    
122            public long getLastAccessedTime() {
123                    return _session.getLastAccessedTime();
124            }
125    
126            public int getMaxInactiveInterval() {
127                    return _session.getMaxInactiveInterval();
128            }
129    
130            public PortletContext getPortletContext() {
131                    return _portletContext;
132            }
133    
134            public void invalidate() {
135                    _session.invalidate();
136            }
137    
138            public boolean isNew() {
139                    return _session.isNew();
140            }
141    
142            public void removeAttribute(String name) {
143                    if (name == null) {
144                            throw new IllegalArgumentException();
145                    }
146    
147                    String scopeName = _getPortletScopeName(name);
148    
149                    _session.removeAttribute(scopeName);
150            }
151    
152            public void removeAttribute(String name, int scope) {
153                    if (name == null) {
154                            throw new IllegalArgumentException();
155                    }
156    
157                    if (scope == PortletSession.PORTLET_SCOPE) {
158                            name = _getPortletScopeName(name);
159                    }
160    
161                    _session.removeAttribute(name);
162            }
163    
164            public void setAttribute(String name, Object value) {
165                    if (name == null) {
166                            throw new IllegalArgumentException();
167                    }
168    
169                    String scopeName = _getPortletScopeName(name);
170    
171                    _session.setAttribute(scopeName, value);
172            }
173    
174            public void setAttribute(String name, Object value, int scope) {
175                    if (name == null) {
176                            throw new IllegalArgumentException();
177                    }
178    
179                    if (scope == PortletSession.PORTLET_SCOPE) {
180                            name = _getPortletScopeName(name);
181                    }
182    
183                    _session.setAttribute(name, value);
184            }
185    
186            public void setHttpSession(HttpSession session) {
187                    _session = session;
188            }
189    
190            public void setMaxInactiveInterval(int interval) {
191                    _session.setMaxInactiveInterval(interval);
192            }
193    
194            private Enumeration<String> _getAttributeNames(
195                    int scope, boolean removePrefix) {
196    
197                    if (scope != PortletSession.PORTLET_SCOPE) {
198                            return _session.getAttributeNames();
199                    }
200    
201                    List<String> attributeNames = new ArrayList<String>();
202    
203                    int portletScopeLength = _portletScope.length();
204    
205                    Enumeration<String> enu = _session.getAttributeNames();
206    
207                    while (enu.hasMoreElements()) {
208                            String name = enu.nextElement();
209    
210                            if (removePrefix) {
211                                    if ((name.length() <= (portletScopeLength + 1)) ||
212                                            !name.startsWith(_portletScope + StringPool.QUESTION)) {
213    
214                                            continue;
215                                    }
216    
217                                    name = name.substring(portletScopeLength + 1);
218                            }
219    
220                            attributeNames.add(name);
221                    }
222    
223                    return Collections.enumeration(attributeNames);
224            }
225    
226            private String _getPortletScope(String portletName, long plid) {
227                    StringBundler sb = new StringBundler(4);
228    
229                    sb.append(PORTLET_SCOPE_NAMESPACE);
230                    sb.append(portletName);
231                    sb.append(LAYOUT_SEPARATOR);
232                    sb.append(plid);
233    
234                    return sb.toString();
235            }
236    
237            private String _getPortletScopeName(String name) {
238                    return _portletScope.concat(StringPool.QUESTION).concat(name);
239            }
240    
241            private PortletContext _portletContext;
242            private String _portletScope;
243            private HttpSession _session;
244    
245    }