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.io.InputStream;
24  import java.net.MalformedURLException;
25  import java.net.URL;
26  import java.util.Enumeration;
27  import java.util.Set;
28  
29  import javax.portlet.PortletContext;
30  import javax.portlet.PortletRequestDispatcher;
31  import javax.servlet.RequestDispatcher;
32  import javax.servlet.Servlet;
33  import javax.servlet.ServletContext;
34  import javax.servlet.ServletException;
35  
36  /***
37   * Wrapper object exposing a {@link PortletContext} as a {@link ServletContext} instance.
38   * Clients accessing this context object will in fact operate on the
39   * {@link PortletContext} object wrapped by this context object.
40   */
41  public class PortletServletContext implements ServletContext {
42  
43  	private PortletContext portletContext;
44  	
45  	public PortletServletContext(PortletContext portletContext) {
46  		this.portletContext = portletContext;
47  	}
48  
49  	/* (non-Javadoc)
50  	 * @see javax.servlet.ServletContext#getAttribute(java.lang.String)
51  	 */
52  	public Object getAttribute(String name) {
53  		return portletContext.getAttribute(name);
54  	}
55  
56  	/* (non-Javadoc)
57  	 * @see javax.servlet.ServletContext#getAttributeNames()
58  	 */
59  	public Enumeration getAttributeNames() {
60  		return portletContext.getAttributeNames();
61  	}
62  
63  	/***
64  	 * @see javax.servlet.ServletContext#getContext(java.lang.String)
65  	 * @throws IllegalStateException Not supported in a portlet.
66  	 */
67  	public ServletContext getContext(String uripath) {
68  		throw new IllegalStateException("Not supported in a portlet");
69  	}
70  
71  	/* (non-Javadoc)
72  	 * @see javax.servlet.ServletContext#getInitParameter(java.lang.String)
73  	 */
74  	public String getInitParameter(String name) {
75  		return portletContext.getInitParameter(name);
76  	}
77  
78  	/* (non-Javadoc)
79  	 * @see javax.servlet.ServletContext#getInitParameterNames()
80  	 */
81  	public Enumeration getInitParameterNames() {
82  		return portletContext.getInitParameterNames();
83  	}
84  
85  	/* (non-Javadoc)
86  	 * @see javax.servlet.ServletContext#getMajorVersion()
87  	 */
88  	public int getMajorVersion() {
89  		return portletContext.getMajorVersion();
90  	}
91  
92  	/* (non-Javadoc)
93  	 * @see javax.servlet.ServletContext#getMimeType(java.lang.String)
94  	 */
95  	public String getMimeType(String file) {
96  		return portletContext.getMimeType(file);
97  	}
98  
99  	/* (non-Javadoc)
100 	 * @see javax.servlet.ServletContext#getMinorVersion()
101 	 */
102 	public int getMinorVersion() {
103 		return portletContext.getMinorVersion();
104 	}
105 
106 	/***
107 	 * Returns a {@link PortletServletRequestDispatcher} wrapping the {@link PortletRequestDispatcher}
108 	 * as a {@link RequestDispatcher} instance.
109 	 * @see javax.servlet.ServletContext#getNamedDispatcher(java.lang.String)
110 	 * @return PortletServletRequestDispatcher
111 	 */
112 	public RequestDispatcher getNamedDispatcher(String name) {
113 		return new PortletServletRequestDispatcher(portletContext.getNamedDispatcher(name));
114 	}
115 
116 	/* (non-Javadoc)
117 	 * @see javax.servlet.ServletContext#getRealPath(java.lang.String)
118 	 */
119 	public String getRealPath(String path) {
120 		return portletContext.getRealPath(path);
121 	}
122 
123 	/***
124 	 * Returns a {@link PortletServletRequestDispatcher} wrapping the {@link PortletRequestDispatcher}
125 	 * as a {@link RequestDispatcher} instance.
126 	 * @see javax.servlet.ServletContext#getNamedDispatcher(java.lang.String)
127 	 * @return PortletServletRequestDispatcher
128 	 */
129 	public RequestDispatcher getRequestDispatcher(String path) {
130 		return new PortletServletRequestDispatcher(portletContext.getRequestDispatcher(path));
131 	}
132 
133 	/* (non-Javadoc)
134 	 * @see javax.servlet.ServletContext#getResource(java.lang.String)
135 	 */
136 	public URL getResource(String path) throws MalformedURLException {
137 		return portletContext.getResource(path);
138 	}
139 
140 	/* (non-Javadoc)
141 	 * @see javax.servlet.ServletContext#getResourceAsStream(java.lang.String)
142 	 */
143 	public InputStream getResourceAsStream(String path) {
144 		return portletContext.getResourceAsStream(path);
145 	}
146 
147 	/* (non-Javadoc)
148 	 * @see javax.servlet.ServletContext#getResourcePaths(java.lang.String)
149 	 */
150 	public Set getResourcePaths(String path) {
151 		return portletContext.getResourcePaths(path);
152 	}
153 
154 	/* (non-Javadoc)
155 	 * @see javax.servlet.ServletContext#getServerInfo()
156 	 */
157 	public String getServerInfo() {
158 		return portletContext.getServerInfo();
159 	}
160 
161 	/***
162 	 * @see javax.servlet.ServletContext#getServlet(java.lang.String)
163 	 * @throws IllegalStateException Not supported in a portlet.
164 	 */
165 	public Servlet getServlet(String name) throws ServletException {
166 		throw new IllegalStateException("Not allowed in a portlet");
167 	}
168 
169 	/* (non-Javadoc)
170 	 * @see javax.servlet.ServletContext#getServletContextName()
171 	 */
172 	public String getServletContextName() {
173 		return portletContext.getPortletContextName();
174 	}
175 
176 	/***
177 	 * @see javax.servlet.ServletContext#getServletNames()
178  	 * @throws IllegalStateException Not supported in a portlet.
179 	 */
180 	public Enumeration getServletNames() {
181 		throw new IllegalStateException("Not allowed in a portlet");
182 	}
183 
184 	/***
185 	 * @see javax.servlet.ServletContext#getServlets()
186 	 * @throws IllegalStateException Not supported in a portlet.
187 	 */
188 	public Enumeration getServlets() {
189 		throw new IllegalStateException("Not allowed in a portlet");
190 	}
191 
192 	/* (non-Javadoc)
193 	 * @see javax.servlet.ServletContext#log(java.lang.String)
194 	 */
195 	public void log(String msg) {
196 		portletContext.log(msg);
197 	}
198 
199 	/* (non-Javadoc)
200 	 * @see javax.servlet.ServletContext#log(java.lang.Exception, java.lang.String)
201 	 */
202 	public void log(Exception exception, String msg) {
203 		log(msg, exception);
204 	}
205 
206 	/* (non-Javadoc)
207 	 * @see javax.servlet.ServletContext#log(java.lang.String, java.lang.Throwable)
208 	 */
209 	public void log(String message, Throwable throwable) {
210 		portletContext.log(message, throwable);
211 	}
212 
213 	/* (non-Javadoc)
214 	 * @see javax.servlet.ServletContext#removeAttribute(java.lang.String)
215 	 */
216 	public void removeAttribute(String name) {
217 		portletContext.removeAttribute(name);
218 	}
219 
220 	/* (non-Javadoc)
221 	 * @see javax.servlet.ServletContext#setAttribute(java.lang.String, java.lang.Object)
222 	 */
223 	public void setAttribute(String name, Object object) {
224 		portletContext.setAttribute(name, object);
225 	}
226 	
227 	/***
228 	 * Get the wrapped {@link PortletContext} instance.
229 	 * @return The wrapped {@link PortletContext} instance.
230 	 */
231 	public PortletContext getPortletContext() {
232 		return portletContext;
233 	}
234 
235 }