View Javadoc

1   /*
2    * $Id: $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  package org.apache.struts2.portlet.servlet;
22  
23  import java.util.ArrayList;
24  import java.util.Enumeration;
25  import java.util.List;
26  
27  import javax.portlet.PortletSession;
28  import javax.servlet.ServletContext;
29  import javax.servlet.http.HttpSession;
30  import javax.servlet.http.HttpSessionContext;
31  
32  /***
33   * Wrapper object exposing a {@link PortletSession} as a {@link HttpSession} instance.
34   * Clients accessing this session object will in fact operate on the
35   * {@link PortletSession} object wrapped by this session object.
36   */
37  public class PortletHttpSession implements HttpSession {
38  
39  	private PortletSession portletSession;
40  
41  	public PortletHttpSession(PortletSession portletSession) {
42  		this.portletSession = portletSession;
43  	}
44  
45  	/*
46  	 * (non-Javadoc)
47  	 * 
48  	 * @see javax.servlet.http.HttpSession#getAttribute(java.lang.String)
49  	 */
50  	public Object getAttribute(String name) {
51  		return portletSession.getAttribute(name);
52  	}
53  
54  	/*
55  	 * (non-Javadoc)
56  	 * 
57  	 * @see javax.servlet.http.HttpSession#getAttributeNames()
58  	 */
59  	public Enumeration getAttributeNames() {
60  		return portletSession.getAttributeNames();
61  	}
62  
63  	/*
64  	 * (non-Javadoc)
65  	 * 
66  	 * @see javax.servlet.http.HttpSession#getCreationTime()
67  	 */
68  	public long getCreationTime() {
69  		return portletSession.getCreationTime();
70  	}
71  
72  	/*
73  	 * (non-Javadoc)
74  	 * 
75  	 * @see javax.servlet.http.HttpSession#getId()
76  	 */
77  	public String getId() {
78  		return portletSession.getId();
79  	}
80  
81  	/*
82  	 * (non-Javadoc)
83  	 * 
84  	 * @see javax.servlet.http.HttpSession#getLastAccessedTime()
85  	 */
86  	public long getLastAccessedTime() {
87  		return portletSession.getLastAccessedTime();
88  	}
89  
90  	/*
91  	 * (non-Javadoc)
92  	 * 
93  	 * @see javax.servlet.http.HttpSession#getMaxInactiveInterval()
94  	 */
95  	public int getMaxInactiveInterval() {
96  		return portletSession.getMaxInactiveInterval();
97  	}
98  
99  	/*
100 	 * (non-Javadoc)
101 	 * 
102 	 * @see javax.servlet.http.HttpSession#getServletContext()
103 	 */
104 	public ServletContext getServletContext() {
105 		return new PortletServletContext(portletSession.getPortletContext());
106 	}
107 
108 	/***
109 	 * @see javax.servlet.http.HttpSession#getSessionContext()
110 	 * @throws IllegalStateException
111 	 *             Not supported in a portlet.
112 	 */
113 	public HttpSessionContext getSessionContext() {
114 		throw new IllegalStateException("Not supported in a portlet");
115 	}
116 
117 	/*
118 	 * (non-Javadoc)
119 	 * 
120 	 * @see javax.servlet.http.HttpSession#getValue(java.lang.String)
121 	 */
122 	public Object getValue(String name) {
123 		return getAttribute(name);
124 	}
125 
126 	/*
127 	 * (non-Javadoc)
128 	 * 
129 	 * @see javax.servlet.http.HttpSession#getValueNames()
130 	 */
131 	public String[] getValueNames() {
132 		List<String> names = new ArrayList<String>();
133 		Enumeration attrNames = getAttributeNames();
134 		while (attrNames.hasMoreElements()) {
135 			names.add((String) attrNames.nextElement());
136 		}
137 		return names.toArray(new String[0]);
138 	}
139 
140 	/*
141 	 * (non-Javadoc)
142 	 * 
143 	 * @see javax.servlet.http.HttpSession#invalidate()
144 	 */
145 	public void invalidate() {
146 		portletSession.invalidate();
147 	}
148 
149 	/*
150 	 * (non-Javadoc)
151 	 * 
152 	 * @see javax.servlet.http.HttpSession#isNew()
153 	 */
154 	public boolean isNew() {
155 		return portletSession.isNew();
156 	}
157 
158 	/*
159 	 * (non-Javadoc)
160 	 * 
161 	 * @see javax.servlet.http.HttpSession#putValue(java.lang.String,
162 	 *      java.lang.Object)
163 	 */
164 	public void putValue(String name, Object value) {
165 		setAttribute(name, value);
166 	}
167 
168 	/*
169 	 * (non-Javadoc)
170 	 * 
171 	 * @see javax.servlet.http.HttpSession#removeAttribute(java.lang.String)
172 	 */
173 	public void removeAttribute(String name) {
174 		portletSession.removeAttribute(name);
175 	}
176 
177 	/*
178 	 * (non-Javadoc)
179 	 * 
180 	 * @see javax.servlet.http.HttpSession#removeValue(java.lang.String)
181 	 */
182 	public void removeValue(String name) {
183 		removeAttribute(name);
184 	}
185 
186 	/*
187 	 * (non-Javadoc)
188 	 * 
189 	 * @see javax.servlet.http.HttpSession#setAttribute(java.lang.String,
190 	 *      java.lang.Object)
191 	 */
192 	public void setAttribute(String name, Object value) {
193 		portletSession.setAttribute(name, value);
194 	}
195 
196 	/*
197 	 * (non-Javadoc)
198 	 * 
199 	 * @see javax.servlet.http.HttpSession#setMaxInactiveInterval(int)
200 	 */
201 	public void setMaxInactiveInterval(int interval) {
202 		portletSession.setMaxInactiveInterval(interval);
203 	}
204 
205 	/***
206 	 * Get the wrapped portlet session.
207 	 * 
208 	 * @return The wrapped portlet session.
209 	 */
210 	public PortletSession getPortletSession() {
211 		return portletSession;
212 	}
213 
214 }