001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.wicket.protocol.ws.api;
018
019import java.util.Collections;
020import java.util.Enumeration;
021import java.util.concurrent.ConcurrentHashMap;
022
023import javax.servlet.ServletContext;
024import javax.servlet.http.HttpSession;
025import javax.servlet.http.HttpSessionContext;
026
027/**
028 * A copy of the HttpSession used at the WebSocket connection creation time
029 *
030 * @since 6.0
031 */
032public class HttpSessionCopy implements HttpSession
033{
034        private final long creationTime;
035        private final ConcurrentHashMap<String, Object> attributes;
036        private final String sessionId;
037        private final ServletContext servletContext;
038        private int maxInactiveInterval;
039
040        public HttpSessionCopy(final HttpSession originalSession)
041        {
042                this.sessionId = originalSession.getId();
043                this.servletContext = originalSession.getServletContext();
044                this.creationTime = originalSession.getCreationTime();
045                this.attributes = new ConcurrentHashMap<>();
046
047                Enumeration<String> attributeNames = originalSession.getAttributeNames();
048                while (attributeNames.hasMoreElements())
049                {
050                        String attributeName = attributeNames.nextElement();
051                        Object attributeValue = originalSession.getAttribute(attributeName);
052                        attributes.put(attributeName, attributeValue);
053                }
054
055        }
056
057        @Override
058        public long getCreationTime()
059        {
060                return creationTime;
061        }
062
063        @Override
064        public String getId()
065        {
066                return sessionId;
067        }
068
069        @Override
070        public long getLastAccessedTime()
071        {
072                return 0;
073        }
074
075        @Override
076        public ServletContext getServletContext()
077        {
078                return servletContext;
079        }
080
081        @Override
082        public void setMaxInactiveInterval(int interval)
083        {
084                this.maxInactiveInterval = interval;
085        }
086
087        @Override
088        public int getMaxInactiveInterval()
089        {
090                return maxInactiveInterval;
091        }
092
093        @Override
094        public HttpSessionContext getSessionContext()
095        {
096                return null;
097        }
098
099        @Override
100        public Object getAttribute(String name)
101        {
102                return attributes.get(name);
103        }
104
105        @Override
106        public Object getValue(String name)
107        {
108                return attributes.get(name);
109        }
110
111        @Override
112        public Enumeration<String> getAttributeNames()
113        {
114                return attributes.keys();
115        }
116
117        @Override
118        public String[] getValueNames()
119        {
120                return Collections.list(attributes.keys()).toArray(new String[0]);
121        }
122
123        @Override
124        public void setAttribute(String name, Object value)
125        {
126                attributes.put(name, value);
127        }
128
129        @Override
130        public void putValue(String name, Object value)
131        {
132                attributes.put(name, value);
133        }
134
135        @Override
136        public void removeAttribute(String name)
137        {
138                attributes.remove(name);
139        }
140
141        @Override
142        public void removeValue(String name)
143        {
144                attributes.remove(name);
145        }
146
147        @Override
148        public void invalidate()
149        {
150                attributes.clear();
151        }
152
153        @Override
154        public boolean isNew()
155        {
156                return false;
157        }
158
159}