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.List;
025    import java.util.Map;
026    
027    import javax.portlet.PortletContext;
028    import javax.portlet.PortletSession;
029    
030    import javax.servlet.http.HttpSession;
031    
032    /**
033     * @author Brian Wing Shun Chan
034     * @author Shuyang Zhou
035     */
036    public class PortletSessionImpl implements LiferayPortletSession {
037    
038            public PortletSessionImpl(
039                    HttpSession session, PortletContext portletContext, String portletName,
040                    long plid) {
041    
042                    this.session = session;
043                    this.portletContext = portletContext;
044    
045                    StringBundler sb = new StringBundler(5);
046    
047                    sb.append(PORTLET_SCOPE_NAMESPACE);
048                    sb.append(portletName);
049                    sb.append(LAYOUT_SEPARATOR);
050                    sb.append(plid);
051                    sb.append(StringPool.QUESTION);
052    
053                    scopePrefix = sb.toString();
054            }
055    
056            @Override
057            public Object getAttribute(String name) {
058                    return getAttribute(name, PORTLET_SCOPE);
059            }
060    
061            @Override
062            public Object getAttribute(String name, int scope) {
063                    if (name == null) {
064                            throw new IllegalArgumentException();
065                    }
066    
067                    if (scope == PORTLET_SCOPE) {
068                            name = scopePrefix.concat(name);
069                    }
070    
071                    return session.getAttribute(name);
072            }
073    
074            @Override
075            public Map<String, Object> getAttributeMap() {
076                    return getAttributeMap(PortletSession.PORTLET_SCOPE);
077            }
078    
079            @Override
080            public Map<String, Object> getAttributeMap(int scope) {
081                    if (scope == PORTLET_SCOPE) {
082                            return new PortletSessionAttributeMap(session, scopePrefix);
083                    }
084    
085                    return new PortletSessionAttributeMap(session);
086            }
087    
088            @Override
089            public Enumeration<String> getAttributeNames() {
090                    return getAttributeNames(PORTLET_SCOPE);
091            }
092    
093            @Override
094            public Enumeration<String> getAttributeNames(int scope) {
095                    if (scope != PORTLET_SCOPE) {
096                            return session.getAttributeNames();
097                    }
098    
099                    List<String> attributeNames = new ArrayList<String>();
100    
101                    Enumeration<String> enu = session.getAttributeNames();
102    
103                    while (enu.hasMoreElements()) {
104                            String name = enu.nextElement();
105    
106                            if (name.startsWith(scopePrefix)) {
107                                    name = name.substring(scopePrefix.length());
108    
109                                    attributeNames.add(name);
110                            }
111                    }
112    
113                    return Collections.enumeration(attributeNames);
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                    removeAttribute(name, PORTLET_SCOPE);
158            }
159    
160            @Override
161            public void removeAttribute(String name, int scope) {
162                    if (name == null) {
163                            throw new IllegalArgumentException();
164                    }
165    
166                    if (scope == PORTLET_SCOPE) {
167                            name = scopePrefix.concat(name);
168                    }
169    
170                    session.removeAttribute(name);
171            }
172    
173            @Override
174            public void setAttribute(String name, Object value) {
175                    setAttribute(name, value, PORTLET_SCOPE);
176            }
177    
178            @Override
179            public void setAttribute(String name, Object value, int scope) {
180                    if (name == null) {
181                            throw new IllegalArgumentException();
182                    }
183    
184                    if (scope == PORTLET_SCOPE) {
185                            name = scopePrefix.concat(name);
186                    }
187    
188                    session.setAttribute(name, value);
189            }
190    
191            @Override
192            public void setHttpSession(HttpSession session) {
193                    this.session = session;
194            }
195    
196            @Override
197            public void setMaxInactiveInterval(int interval) {
198                    session.setMaxInactiveInterval(interval);
199            }
200    
201            protected final PortletContext portletContext;
202            protected final String scopePrefix;
203            protected HttpSession session;
204    
205    }