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