View Javadoc

1   /*
2    * $Id: PortletHttpSession.java 651946 2008-04-27 13:41:38Z apetrelli $
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  
22  package org.apache.struts2.portlet.servlet;
23  
24  import java.util.ArrayList;
25  import java.util.Enumeration;
26  import java.util.List;
27  
28  import javax.portlet.PortletSession;
29  import javax.servlet.ServletContext;
30  import javax.servlet.http.HttpSession;
31  import javax.servlet.http.HttpSessionContext;
32  
33  /***
34   * Wrapper object exposing a {@link PortletSession} as a {@link HttpSession} instance.
35   * Clients accessing this session object will in fact operate on the
36   * {@link PortletSession} object wrapped by this session object.
37   */
38  public class PortletHttpSession implements HttpSession {
39  
40  	private PortletSession portletSession;
41  
42  	public PortletHttpSession(PortletSession portletSession) {
43  		this.portletSession = portletSession;
44  	}
45  
46  	/*
47  	 * (non-Javadoc)
48  	 * 
49  	 * @see javax.servlet.http.HttpSession#getAttribute(java.lang.String)
50  	 */
51  	public Object getAttribute(String name) {
52  		return portletSession.getAttribute(name);
53  	}
54  
55  	/*
56  	 * (non-Javadoc)
57  	 * 
58  	 * @see javax.servlet.http.HttpSession#getAttributeNames()
59  	 */
60  	public Enumeration getAttributeNames() {
61  		return portletSession.getAttributeNames();
62  	}
63  
64  	/*
65  	 * (non-Javadoc)
66  	 * 
67  	 * @see javax.servlet.http.HttpSession#getCreationTime()
68  	 */
69  	public long getCreationTime() {
70  		return portletSession.getCreationTime();
71  	}
72  
73  	/*
74  	 * (non-Javadoc)
75  	 * 
76  	 * @see javax.servlet.http.HttpSession#getId()
77  	 */
78  	public String getId() {
79  		return portletSession.getId();
80  	}
81  
82  	/*
83  	 * (non-Javadoc)
84  	 * 
85  	 * @see javax.servlet.http.HttpSession#getLastAccessedTime()
86  	 */
87  	public long getLastAccessedTime() {
88  		return portletSession.getLastAccessedTime();
89  	}
90  
91  	/*
92  	 * (non-Javadoc)
93  	 * 
94  	 * @see javax.servlet.http.HttpSession#getMaxInactiveInterval()
95  	 */
96  	public int getMaxInactiveInterval() {
97  		return portletSession.getMaxInactiveInterval();
98  	}
99  
100 	/*
101 	 * (non-Javadoc)
102 	 * 
103 	 * @see javax.servlet.http.HttpSession#getServletContext()
104 	 */
105 	public ServletContext getServletContext() {
106 		return new PortletServletContext(portletSession.getPortletContext());
107 	}
108 
109 	/***
110 	 * @see javax.servlet.http.HttpSession#getSessionContext()
111 	 * @throws IllegalStateException
112 	 *             Not supported in a portlet.
113 	 */
114 	public HttpSessionContext getSessionContext() {
115 		throw new IllegalStateException("Not supported in a portlet");
116 	}
117 
118 	/*
119 	 * (non-Javadoc)
120 	 * 
121 	 * @see javax.servlet.http.HttpSession#getValue(java.lang.String)
122 	 */
123 	public Object getValue(String name) {
124 		return getAttribute(name);
125 	}
126 
127 	/*
128 	 * (non-Javadoc)
129 	 * 
130 	 * @see javax.servlet.http.HttpSession#getValueNames()
131 	 */
132 	public String[] getValueNames() {
133 		List<String> names = new ArrayList<String>();
134 		Enumeration attrNames = getAttributeNames();
135 		while (attrNames.hasMoreElements()) {
136 			names.add((String) attrNames.nextElement());
137 		}
138 		return names.toArray(new String[0]);
139 	}
140 
141 	/*
142 	 * (non-Javadoc)
143 	 * 
144 	 * @see javax.servlet.http.HttpSession#invalidate()
145 	 */
146 	public void invalidate() {
147 		portletSession.invalidate();
148 	}
149 
150 	/*
151 	 * (non-Javadoc)
152 	 * 
153 	 * @see javax.servlet.http.HttpSession#isNew()
154 	 */
155 	public boolean isNew() {
156 		return portletSession.isNew();
157 	}
158 
159 	/*
160 	 * (non-Javadoc)
161 	 * 
162 	 * @see javax.servlet.http.HttpSession#putValue(java.lang.String,
163 	 *      java.lang.Object)
164 	 */
165 	public void putValue(String name, Object value) {
166 		setAttribute(name, value);
167 	}
168 
169 	/*
170 	 * (non-Javadoc)
171 	 * 
172 	 * @see javax.servlet.http.HttpSession#removeAttribute(java.lang.String)
173 	 */
174 	public void removeAttribute(String name) {
175 		portletSession.removeAttribute(name);
176 	}
177 
178 	/*
179 	 * (non-Javadoc)
180 	 * 
181 	 * @see javax.servlet.http.HttpSession#removeValue(java.lang.String)
182 	 */
183 	public void removeValue(String name) {
184 		removeAttribute(name);
185 	}
186 
187 	/*
188 	 * (non-Javadoc)
189 	 * 
190 	 * @see javax.servlet.http.HttpSession#setAttribute(java.lang.String,
191 	 *      java.lang.Object)
192 	 */
193 	public void setAttribute(String name, Object value) {
194 		portletSession.setAttribute(name, value);
195 	}
196 
197 	/*
198 	 * (non-Javadoc)
199 	 * 
200 	 * @see javax.servlet.http.HttpSession#setMaxInactiveInterval(int)
201 	 */
202 	public void setMaxInactiveInterval(int interval) {
203 		portletSession.setMaxInactiveInterval(interval);
204 	}
205 
206 	/***
207 	 * Get the wrapped portlet session.
208 	 * 
209 	 * @return The wrapped portlet session.
210 	 */
211 	public PortletSession getPortletSession() {
212 		return portletSession;
213 	}
214 
215 }