1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.seasar.teeda.core.context.portlet; |
17 | |
|
18 | |
import java.io.IOException; |
19 | |
import java.io.InputStream; |
20 | |
import java.io.UnsupportedEncodingException; |
21 | |
import java.net.MalformedURLException; |
22 | |
import java.net.URL; |
23 | |
import java.security.Principal; |
24 | |
import java.util.Collections; |
25 | |
import java.util.HashMap; |
26 | |
import java.util.Iterator; |
27 | |
import java.util.Locale; |
28 | |
import java.util.Map; |
29 | |
import java.util.Set; |
30 | |
|
31 | |
import javax.faces.FacesException; |
32 | |
import javax.faces.application.ViewHandler; |
33 | |
import javax.faces.context.ExternalContext; |
34 | |
import javax.portlet.ActionRequest; |
35 | |
import javax.portlet.ActionResponse; |
36 | |
import javax.portlet.PortletContext; |
37 | |
import javax.portlet.PortletException; |
38 | |
import javax.portlet.PortletRequest; |
39 | |
import javax.portlet.PortletRequestDispatcher; |
40 | |
import javax.portlet.PortletResponse; |
41 | |
import javax.portlet.PortletSession; |
42 | |
import javax.portlet.RenderRequest; |
43 | |
import javax.portlet.RenderResponse; |
44 | |
|
45 | |
import org.seasar.framework.container.external.portlet.PortletApplicationMap; |
46 | |
import org.seasar.framework.container.external.portlet.PortletInitParameterMap; |
47 | |
import org.seasar.framework.container.external.portlet.PortletRequestHeaderMap; |
48 | |
import org.seasar.framework.container.external.portlet.PortletRequestHeaderValuesMap; |
49 | |
import org.seasar.framework.container.external.portlet.PortletRequestMap; |
50 | |
import org.seasar.framework.container.external.portlet.PortletRequestParameterMap; |
51 | |
import org.seasar.framework.container.external.portlet.PortletRequestParameterValuesMap; |
52 | |
import org.seasar.framework.container.external.portlet.PortletSessionMap; |
53 | |
import org.seasar.framework.log.Logger; |
54 | |
import org.seasar.framework.util.AssertionUtil; |
55 | |
import org.seasar.framework.util.EnumerationIterator; |
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
public class PortletExternalContextImpl extends ExternalContext { |
65 | |
|
66 | 1 | private static Logger logger = Logger |
67 | 2 | .getLogger(PortletExternalContextImpl.class); |
68 | |
|
69 | 1 | private static final String INIT_PARAMETER_MAP_ATTRIBUTE = PortletInitParameterMap.class |
70 | |
.getName(); |
71 | |
|
72 | 1 | private static final String SESSION_NAMESPACE = PortletExternalContextImpl.class |
73 | |
.getName() |
74 | |
+ ".Namespace"; |
75 | |
|
76 | 1 | private static final Map EMPTY_UNMODIFIABLE_MAP = Collections |
77 | |
.unmodifiableMap(new HashMap(0)); |
78 | |
|
79 | |
private PortletContext portletContext; |
80 | |
|
81 | |
private PortletRequest portletRequest; |
82 | |
|
83 | |
private PortletResponse portletResponse; |
84 | |
|
85 | |
private Map applicationMap; |
86 | |
|
87 | |
private Map sessionMap; |
88 | |
|
89 | |
private Map requestMap; |
90 | |
|
91 | |
private Map requestParameterMap; |
92 | |
|
93 | |
private Map requestParameterValuesMap; |
94 | |
|
95 | |
private Map requestHeaderMap; |
96 | |
|
97 | |
private Map requestHeaderValuesMap; |
98 | |
|
99 | |
private Map initParameterMap; |
100 | |
|
101 | |
private boolean isActionRequest; |
102 | |
|
103 | |
public PortletExternalContextImpl(PortletContext context, |
104 | 4 | PortletRequest request, PortletResponse response) { |
105 | 4 | this.portletContext = context; |
106 | 4 | this.portletRequest = request; |
107 | 4 | this.portletResponse = response; |
108 | 4 | this.isActionRequest = (portletRequest != null && portletRequest instanceof ActionRequest); |
109 | |
|
110 | 4 | if (isActionRequest) { |
111 | |
|
112 | |
|
113 | 0 | ActionRequest actionRequest = (ActionRequest) portletRequest; |
114 | |
|
115 | |
|
116 | 0 | String contentType = portletRequest.getProperty("Content-Type"); |
117 | |
|
118 | 0 | String characterEncoding = null; |
119 | 0 | if (contentType != null) { |
120 | 0 | int charIndex = contentType.indexOf("charset="); |
121 | 0 | if (charIndex != -1) { |
122 | 0 | characterEncoding = contentType.substring(charIndex + 8); |
123 | |
} |
124 | |
} |
125 | |
|
126 | 0 | if (characterEncoding == null) { |
127 | 0 | PortletSession session = portletRequest |
128 | |
.getPortletSession(false); |
129 | |
|
130 | 0 | if (session != null) { |
131 | 0 | characterEncoding = (String) session.getAttribute( |
132 | |
ViewHandler.CHARACTER_ENCODING_KEY, |
133 | |
PortletSession.PORTLET_SCOPE); |
134 | |
} |
135 | |
} |
136 | |
|
137 | |
|
138 | 0 | if (characterEncoding != null) { |
139 | |
try { |
140 | 0 | actionRequest.setCharacterEncoding(characterEncoding); |
141 | 0 | } catch (UnsupportedEncodingException e) { |
142 | 0 | logger.warn("The specified encoding is wrong: " |
143 | |
+ characterEncoding, e); |
144 | 0 | } catch (IllegalStateException e) { |
145 | 0 | logger |
146 | |
.warn( |
147 | |
"setCharacterEncoding(String) must not be called " |
148 | |
+ "after reading request parameters or reading input using getReader()", |
149 | |
e); |
150 | 0 | } |
151 | |
} |
152 | |
|
153 | |
} else { |
154 | |
|
155 | |
|
156 | 4 | Map sessionMap = getSessionMap(); |
157 | 4 | sessionMap.put(SESSION_NAMESPACE, |
158 | |
((RenderResponse) portletResponse).getNamespace()); |
159 | |
} |
160 | 4 | } |
161 | |
|
162 | |
public void dispatch(String path) throws IOException { |
163 | 0 | if (isActionRequest) { |
164 | 0 | throw new IllegalStateException( |
165 | |
"Cannot call dispatch(String) if the reqeust is ActionRequest."); |
166 | |
} |
167 | |
|
168 | 0 | PortletRequestDispatcher requestDispatcher = portletContext |
169 | |
.getRequestDispatcher(path); |
170 | |
|
171 | |
try { |
172 | 0 | requestDispatcher.include((RenderRequest) portletRequest, |
173 | |
(RenderResponse) portletResponse); |
174 | 0 | } catch (PortletException e) { |
175 | 0 | throw new FacesException( |
176 | |
"Failed to include the content of a resource in the response.", |
177 | |
e); |
178 | 0 | } |
179 | 0 | } |
180 | |
|
181 | |
public String encodeActionURL(String url) { |
182 | 0 | AssertionUtil.assertNotNull("url is null.", url); |
183 | 0 | return portletResponse.encodeURL(url); |
184 | |
} |
185 | |
|
186 | |
public String encodeNamespace(String name) { |
187 | 0 | return name + getNamespace(); |
188 | |
} |
189 | |
|
190 | |
public String encodeResourceURL(String url) { |
191 | 2 | AssertionUtil.assertNotNull("url is null.", url); |
192 | 2 | if (url.indexOf("://") == -1 && !url.startsWith("/")) { |
193 | 1 | return url; |
194 | |
} |
195 | 1 | return portletResponse.encodeURL(url); |
196 | |
} |
197 | |
|
198 | |
public Map getApplicationMap() { |
199 | 0 | if (applicationMap == null) { |
200 | 0 | applicationMap = new PortletApplicationMap(portletContext); |
201 | |
} |
202 | 0 | return applicationMap; |
203 | |
} |
204 | |
|
205 | |
public String getAuthType() { |
206 | 0 | return portletRequest.getAuthType(); |
207 | |
} |
208 | |
|
209 | |
public Object getContext() { |
210 | 0 | return portletContext; |
211 | |
} |
212 | |
|
213 | |
public String getInitParameter(String name) { |
214 | 0 | return portletContext.getInitParameter(name); |
215 | |
} |
216 | |
|
217 | |
public Map getInitParameterMap() { |
218 | 0 | if (initParameterMap == null) { |
219 | 0 | if ((initParameterMap = (Map) portletContext |
220 | |
.getAttribute(INIT_PARAMETER_MAP_ATTRIBUTE)) == null) { |
221 | 0 | initParameterMap = new PortletInitParameterMap(portletContext); |
222 | 0 | portletContext.setAttribute(INIT_PARAMETER_MAP_ATTRIBUTE, |
223 | |
initParameterMap); |
224 | |
} |
225 | |
} |
226 | 0 | return initParameterMap; |
227 | |
} |
228 | |
|
229 | |
public String getRemoteUser() { |
230 | 0 | return portletRequest.getRemoteUser(); |
231 | |
} |
232 | |
|
233 | |
public Object getRequest() { |
234 | 0 | return portletRequest; |
235 | |
} |
236 | |
|
237 | |
public String getRequestContextPath() { |
238 | 0 | return portletRequest.getContextPath(); |
239 | |
} |
240 | |
|
241 | |
public Map getRequestCookieMap() { |
242 | 0 | return EMPTY_UNMODIFIABLE_MAP; |
243 | |
} |
244 | |
|
245 | |
public Map getRequestHeaderMap() { |
246 | 0 | if (requestHeaderMap == null) { |
247 | 0 | requestHeaderMap = new PortletRequestHeaderMap(portletRequest); |
248 | |
} |
249 | 0 | return requestHeaderMap; |
250 | |
} |
251 | |
|
252 | |
public Map getRequestHeaderValuesMap() { |
253 | 0 | if (requestHeaderValuesMap == null) { |
254 | 0 | requestHeaderValuesMap = new PortletRequestHeaderValuesMap( |
255 | |
portletRequest); |
256 | |
} |
257 | 0 | return requestHeaderValuesMap; |
258 | |
} |
259 | |
|
260 | |
public Locale getRequestLocale() { |
261 | 0 | return portletRequest.getLocale(); |
262 | |
} |
263 | |
|
264 | |
public Iterator getRequestLocales() { |
265 | 0 | return new EnumerationIterator(portletRequest.getLocales()); |
266 | |
} |
267 | |
|
268 | |
public Map getRequestMap() { |
269 | 0 | if (requestMap == null) { |
270 | 0 | requestMap = new PortletRequestMap(portletRequest); |
271 | |
} |
272 | 0 | return requestMap; |
273 | |
} |
274 | |
|
275 | |
public Map getRequestParameterMap() { |
276 | 0 | if (requestParameterMap == null) { |
277 | 0 | requestParameterMap = new PortletRequestParameterMap(portletRequest); |
278 | |
} |
279 | 0 | return requestParameterMap; |
280 | |
} |
281 | |
|
282 | |
public Iterator getRequestParameterNames() { |
283 | 0 | return new EnumerationIterator(portletRequest.getParameterNames()); |
284 | |
} |
285 | |
|
286 | |
public Map getRequestParameterValuesMap() { |
287 | 0 | if (requestParameterValuesMap == null) { |
288 | 0 | requestParameterValuesMap = new PortletRequestParameterValuesMap( |
289 | |
portletRequest); |
290 | |
} |
291 | 0 | return requestParameterValuesMap; |
292 | |
} |
293 | |
|
294 | |
public String getRequestPathInfo() { |
295 | |
|
296 | 0 | return null; |
297 | |
} |
298 | |
|
299 | |
public String getRequestServletPath() { |
300 | |
|
301 | 0 | return null; |
302 | |
} |
303 | |
|
304 | |
public URL getResource(String path) throws MalformedURLException { |
305 | 0 | AssertionUtil.assertNotNull("path is null.", path); |
306 | 0 | return portletContext.getResource(path); |
307 | |
} |
308 | |
|
309 | |
public InputStream getResourceAsStream(String path) { |
310 | 0 | AssertionUtil.assertNotNull("path is null.", path); |
311 | 0 | return portletContext.getResourceAsStream(path); |
312 | |
} |
313 | |
|
314 | |
public Set getResourcePaths(String path) { |
315 | 0 | AssertionUtil.assertNotNull("path is null.", path); |
316 | 0 | return portletContext.getResourcePaths(path); |
317 | |
} |
318 | |
|
319 | |
public Object getResponse() { |
320 | 0 | return portletResponse; |
321 | |
} |
322 | |
|
323 | |
public Object getSession(boolean create) { |
324 | 0 | return portletRequest.getPortletSession(create); |
325 | |
} |
326 | |
|
327 | |
public Map getSessionMap() { |
328 | 4 | if (sessionMap == null) { |
329 | 4 | sessionMap = new PortletSessionMap(portletRequest); |
330 | |
} |
331 | 4 | return sessionMap; |
332 | |
} |
333 | |
|
334 | |
public Principal getUserPrincipal() { |
335 | 0 | return portletRequest.getUserPrincipal(); |
336 | |
} |
337 | |
|
338 | |
public boolean isUserInRole(String role) { |
339 | 0 | AssertionUtil.assertNotNull("role is null.", role); |
340 | 0 | return portletRequest.isUserInRole(role); |
341 | |
} |
342 | |
|
343 | |
public void log(String message) { |
344 | 0 | AssertionUtil.assertNotNull("message is null.", message); |
345 | 0 | portletContext.log(message); |
346 | 0 | } |
347 | |
|
348 | |
public void log(String message, Throwable exception) { |
349 | 0 | AssertionUtil.assertNotNull("message", message); |
350 | 0 | AssertionUtil.assertNotNull("exception", exception); |
351 | 0 | portletContext.log(message, exception); |
352 | 0 | } |
353 | |
|
354 | |
public void redirect(String url) throws IOException { |
355 | 0 | if (portletResponse instanceof ActionResponse) { |
356 | 0 | ((ActionResponse) portletResponse).sendRedirect(url); |
357 | |
} else { |
358 | 0 | throw new IllegalArgumentException( |
359 | |
"RenderResponse does not support redirect(String)."); |
360 | |
} |
361 | 0 | } |
362 | |
|
363 | |
protected String getNamespace() { |
364 | 0 | if (isActionRequest) { |
365 | 0 | Map sessionMap = getSessionMap(); |
366 | 0 | String namespace = (String) sessionMap.get(SESSION_NAMESPACE); |
367 | 0 | if (namespace != null) { |
368 | 0 | return namespace; |
369 | |
} |
370 | 0 | throw new IllegalStateException( |
371 | |
"Cannot call encodeNamespace(String) if the request is ActionRequest."); |
372 | |
} |
373 | 0 | return ((RenderResponse) portletResponse).getNamespace(); |
374 | |
} |
375 | |
} |