001/*
002 * Copyright 2002-2013 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.mock.web.portlet;
018
019import java.util.Collections;
020import java.util.Enumeration;
021import java.util.Iterator;
022import java.util.LinkedHashMap;
023import java.util.LinkedHashSet;
024import java.util.Map;
025import javax.portlet.PortletContext;
026import javax.portlet.PortletSession;
027import javax.servlet.http.HttpSessionBindingEvent;
028import javax.servlet.http.HttpSessionBindingListener;
029
030import org.springframework.mock.web.MockHttpSession;
031
032/**
033 * Mock implementation of the {@link javax.portlet.PortletSession} interface.
034 *
035 * @author John A. Lewis
036 * @author Juergen Hoeller
037 * @since 2.0
038 */
039public class MockPortletSession implements PortletSession {
040
041        private static int nextId = 1;
042
043
044        private final String id = Integer.toString(nextId++);
045
046        private final long creationTime = System.currentTimeMillis();
047
048        private int maxInactiveInterval;
049
050        private long lastAccessedTime = System.currentTimeMillis();
051
052        private final PortletContext portletContext;
053
054        private final Map<String, Object> portletAttributes = new LinkedHashMap<String, Object>();
055
056        private final Map<String, Object> applicationAttributes = new LinkedHashMap<String, Object>();
057
058        private boolean invalid = false;
059
060        private boolean isNew = true;
061
062
063        /**
064         * Create a new MockPortletSession with a default {@link MockPortletContext}.
065         * @see MockPortletContext
066         */
067        public MockPortletSession() {
068                this(null);
069        }
070
071        /**
072         * Create a new MockPortletSession.
073         * @param portletContext the PortletContext that the session runs in
074         */
075        public MockPortletSession(PortletContext portletContext) {
076                this.portletContext = (portletContext != null ? portletContext : new MockPortletContext());
077        }
078
079
080        @Override
081        public Object getAttribute(String name) {
082                return this.portletAttributes.get(name);
083        }
084
085        @Override
086        public Object getAttribute(String name, int scope) {
087                if (scope == PortletSession.PORTLET_SCOPE) {
088                        return this.portletAttributes.get(name);
089                }
090                else if (scope == PortletSession.APPLICATION_SCOPE) {
091                        return this.applicationAttributes.get(name);
092                }
093                return null;
094        }
095
096        @Override
097        public Enumeration<String> getAttributeNames() {
098                return Collections.enumeration(new LinkedHashSet<String>(this.portletAttributes.keySet()));
099        }
100
101        @Override
102        public Enumeration<String> getAttributeNames(int scope) {
103                if (scope == PortletSession.PORTLET_SCOPE) {
104                        return Collections.enumeration(new LinkedHashSet<String>(this.portletAttributes.keySet()));
105                }
106                else if (scope == PortletSession.APPLICATION_SCOPE) {
107                        return Collections.enumeration(new LinkedHashSet<String>(this.applicationAttributes.keySet()));
108                }
109                return null;
110        }
111
112        @Override
113        public long getCreationTime() {
114                return this.creationTime;
115        }
116
117        @Override
118        public String getId() {
119                return this.id;
120        }
121
122        public void access() {
123                this.lastAccessedTime = System.currentTimeMillis();
124                setNew(false);
125        }
126
127        @Override
128        public long getLastAccessedTime() {
129                return this.lastAccessedTime;
130        }
131
132        @Override
133        public int getMaxInactiveInterval() {
134                return this.maxInactiveInterval;
135        }
136
137        /**
138         * Clear all of this session's attributes.
139         */
140        public void clearAttributes() {
141                doClearAttributes(this.portletAttributes);
142                doClearAttributes(this.applicationAttributes);
143        }
144
145        protected void doClearAttributes(Map<String, Object> attributes) {
146                for (Iterator<Map.Entry<String, Object>> it = attributes.entrySet().iterator(); it.hasNext();) {
147                        Map.Entry<String, Object> entry = it.next();
148                        String name = entry.getKey();
149                        Object value = entry.getValue();
150                        it.remove();
151                        if (value instanceof HttpSessionBindingListener) {
152                                ((HttpSessionBindingListener) value).valueUnbound(
153                                                new HttpSessionBindingEvent(new MockHttpSession(), name, value));
154                        }
155                }
156        }
157
158        @Override
159        public void invalidate() {
160                this.invalid = true;
161                clearAttributes();
162        }
163
164        public boolean isInvalid() {
165                return this.invalid;
166        }
167
168        public void setNew(boolean value) {
169                this.isNew = value;
170        }
171
172        @Override
173        public boolean isNew() {
174                return this.isNew;
175        }
176
177        @Override
178        public void removeAttribute(String name) {
179                this.portletAttributes.remove(name);
180        }
181
182        @Override
183        public void removeAttribute(String name, int scope) {
184                if (scope == PortletSession.PORTLET_SCOPE) {
185                        this.portletAttributes.remove(name);
186                }
187                else if (scope == PortletSession.APPLICATION_SCOPE) {
188                        this.applicationAttributes.remove(name);
189                }
190        }
191
192        @Override
193        public void setAttribute(String name, Object value) {
194                if (value != null) {
195                        this.portletAttributes.put(name, value);
196                }
197                else {
198                        this.portletAttributes.remove(name);
199                }
200        }
201
202        @Override
203        public void setAttribute(String name, Object value, int scope) {
204                if (scope == PortletSession.PORTLET_SCOPE) {
205                        if (value != null) {
206                                this.portletAttributes.put(name, value);
207                        }
208                        else {
209                                this.portletAttributes.remove(name);
210                        }
211                }
212                else if (scope == PortletSession.APPLICATION_SCOPE) {
213                        if (value != null) {
214                                this.applicationAttributes.put(name, value);
215                        }
216                        else {
217                                this.applicationAttributes.remove(name);
218                        }
219                }
220        }
221
222        @Override
223        public void setMaxInactiveInterval(int interval) {
224                this.maxInactiveInterval = interval;
225        }
226
227        @Override
228        public PortletContext getPortletContext() {
229                return this.portletContext;
230        }
231
232        @Override
233        public Map<String, Object> getAttributeMap() {
234                return Collections.unmodifiableMap(this.portletAttributes);
235        }
236
237        @Override
238        public Map<String, Object> getAttributeMap(int scope) {
239                if (scope == PortletSession.PORTLET_SCOPE) {
240                        return Collections.unmodifiableMap(this.portletAttributes);
241                }
242                else if (scope == PortletSession.APPLICATION_SCOPE) {
243                        return Collections.unmodifiableMap(this.applicationAttributes);
244                }
245                else {
246                        return Collections.emptyMap();
247                }
248        }
249
250}