View Javadoc

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