1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
50
51
52 public Object getAttribute(String name) {
53 return portletContext.getAttribute(name);
54 }
55
56
57
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
72
73
74 public String getInitParameter(String name) {
75 return portletContext.getInitParameter(name);
76 }
77
78
79
80
81 public Enumeration getInitParameterNames() {
82 return portletContext.getInitParameterNames();
83 }
84
85
86
87
88 public int getMajorVersion() {
89 return portletContext.getMajorVersion();
90 }
91
92
93
94
95 public String getMimeType(String file) {
96 return portletContext.getMimeType(file);
97 }
98
99
100
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
117
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
134
135
136 public URL getResource(String path) throws MalformedURLException {
137 return portletContext.getResource(path);
138 }
139
140
141
142
143 public InputStream getResourceAsStream(String path) {
144 return portletContext.getResourceAsStream(path);
145 }
146
147
148
149
150 public Set getResourcePaths(String path) {
151 return portletContext.getResourcePaths(path);
152 }
153
154
155
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
170
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
193
194
195 public void log(String msg) {
196 portletContext.log(msg);
197 }
198
199
200
201
202 public void log(Exception exception, String msg) {
203 log(msg, exception);
204 }
205
206
207
208
209 public void log(String message, Throwable throwable) {
210 portletContext.log(message, throwable);
211 }
212
213
214
215
216 public void removeAttribute(String name) {
217 portletContext.removeAttribute(name);
218 }
219
220
221
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 }