001: /*
002: * Copyright 2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.myfaces.context.portlet;
017:
018: import java.io.IOException;
019: import java.io.InputStream;
020: import java.net.MalformedURLException;
021: import java.net.URL;
022: import java.security.Principal;
023: import java.util.Collections;
024: import java.util.Iterator;
025: import java.util.Locale;
026: import java.util.Map;
027: import java.util.Set;
028:
029: import javax.faces.FacesException;
030: import javax.faces.context.ExternalContext;
031: import javax.portlet.ActionRequest;
032: import javax.portlet.ActionResponse;
033: import javax.portlet.PortletContext;
034: import javax.portlet.PortletException;
035: import javax.portlet.PortletRequest;
036: import javax.portlet.PortletRequestDispatcher;
037: import javax.portlet.PortletResponse;
038: import javax.portlet.RenderRequest;
039: import javax.portlet.RenderResponse;
040:
041: import org.apache.myfaces.context.ReleaseableExternalContext;
042: import org.apache.myfaces.util.EnumerationIterator;
043:
044: /**
045: * An ExternalContext implementation for JSF applications that run inside a Portlet.
046: *
047: * @author Stan Silvert (latest modification by $Author: mbr $)
048: * @version $Revision: 512415 $ $Date: 2007-02-27 22:20:10 +0100 (Di, 27 Feb 2007) $
049: */
050: public class PortletExternalContextImpl extends ExternalContext
051: implements ReleaseableExternalContext {
052:
053: private static final String INIT_PARAMETER_MAP_ATTRIBUTE = InitParameterMap.class
054: .getName();
055:
056: PortletContext _portletContext;
057: PortletRequest _portletRequest;
058: PortletResponse _portletResponse;
059:
060: private Map<String, Object> _applicationMap;
061: private Map<String, Object> _sessionMap;
062: private Map<String, Object> _requestMap;
063: private Map<String, String> _requestParameterMap;
064: private Map<String, String[]> _requestParameterValuesMap;
065: private Map<String, String> _requestHeaderMap;
066: private Map<String, String[]> _requestHeaderValuesMap;
067: private Map<String, String> _initParameterMap;
068:
069: private ActionRequest _actionRequest;
070:
071: /** Creates a new instance of PortletFacesContextImpl */
072: public PortletExternalContextImpl(PortletContext portletContext,
073: PortletRequest portletRequest,
074: PortletResponse portletResponse) {
075: _portletContext = portletContext;
076: _portletRequest = portletRequest;
077: _portletResponse = portletResponse;
078: _actionRequest = isActionRequest(portletRequest) ? (ActionRequest) portletRequest
079: : null;
080: }
081:
082: public void dispatch(String path) throws IOException {
083: if (_actionRequest != null) { // dispatch only allowed for RenderRequest
084: String msg = "Can not call dispatch() during a portlet ActionRequest";
085: throw new UnsupportedOperationException(msg);
086: }
087:
088: PortletRequestDispatcher requestDispatcher = _portletContext
089: .getRequestDispatcher(path); //TODO: figure out why I need named dispatcher
090: try {
091: requestDispatcher.include((RenderRequest) _portletRequest,
092: (RenderResponse) _portletResponse);
093: } catch (PortletException e) {
094: if (e.getMessage() != null) {
095: throw new FacesException(e.getMessage(), e);
096: }
097: throw new FacesException(e);
098: }
099: }
100:
101: public String encodeActionURL(String url) {
102: checkNull(url, "url");
103: return _portletResponse.encodeURL(url);
104: }
105:
106: public String encodeNamespace(String name) {
107: if (_actionRequest != null) {
108: // encodeNamespace only allowed for RenderRequest
109: throw new UnsupportedOperationException(
110: "Can not call encodeNamespace() during a portlet ActionRequest");
111: }
112:
113: // we render out the name and then the namespace as
114: // e.g. for JSF-ids, it is important to keep the _id prefix
115: // to know that id creation has happened automatically
116: return name
117: + ((RenderResponse) _portletResponse).getNamespace();
118: }
119:
120: @Override
121: public String encodeResourceURL(String url) {
122: checkNull(url, "url");
123: return _portletResponse.encodeURL(url);
124: }
125:
126: @Override
127: public Map<String, Object> getApplicationMap() {
128: if (_applicationMap == null) {
129: _applicationMap = new ApplicationMap(_portletContext);
130: }
131: return _applicationMap;
132: }
133:
134: @Override
135: public String getAuthType() {
136: return _portletRequest.getAuthType();
137: }
138:
139: @Override
140: public Object getContext() {
141: return _portletContext;
142: }
143:
144: @Override
145: public String getInitParameter(String name) {
146: return _portletContext.getInitParameter(name);
147: }
148:
149: @Override
150: @SuppressWarnings("unchecked")
151: public Map<String, String> getInitParameterMap() {
152: if (_initParameterMap == null) {
153: // We cache it as an attribute in PortletContext itself (is this circular reference a problem?)
154: if ((_initParameterMap = (Map<String, String>) _portletContext
155: .getAttribute(INIT_PARAMETER_MAP_ATTRIBUTE)) == null) {
156: _initParameterMap = new InitParameterMap(
157: _portletContext);
158: _portletContext
159: .setAttribute(INIT_PARAMETER_MAP_ATTRIBUTE,
160: _initParameterMap);
161: }
162: }
163: return _initParameterMap;
164: }
165:
166: @Override
167: public String getRemoteUser() {
168: return _portletRequest.getRemoteUser();
169: }
170:
171: @Override
172: public Object getRequest() {
173: return _portletRequest;
174: }
175:
176: @Override
177: public String getRequestContentType() {
178: return null;
179: }
180:
181: @Override
182: public String getRequestContextPath() {
183: return _portletRequest.getContextPath();
184: }
185:
186: @Override
187: @SuppressWarnings("unchecked")
188: public Map<String, Object> getRequestCookieMap() {
189: return Collections.EMPTY_MAP;
190: }
191:
192: @Override
193: public Map<String, String> getRequestHeaderMap() {
194: if (_requestHeaderMap == null) {
195: _requestHeaderMap = new RequestHeaderMap(_portletRequest);
196: }
197: return _requestHeaderMap;
198: }
199:
200: @Override
201: public Map<String, String[]> getRequestHeaderValuesMap() {
202: if (_requestHeaderValuesMap == null) {
203: _requestHeaderValuesMap = new RequestHeaderValuesMap(
204: _portletRequest);
205: }
206: return _requestHeaderValuesMap;
207: }
208:
209: @Override
210: public Locale getRequestLocale() {
211: return _portletRequest.getLocale();
212: }
213:
214: @SuppressWarnings("unchecked")
215: @Override
216: public Iterator<Locale> getRequestLocales() {
217: return new EnumerationIterator(_portletRequest.getLocales());
218: }
219:
220: @Override
221: public Map<String, Object> getRequestMap() {
222: if (_requestMap == null) {
223: _requestMap = new RequestMap(_portletRequest);
224: }
225: return _requestMap;
226: }
227:
228: @Override
229: public Map<String, String> getRequestParameterMap() {
230: if (_requestParameterMap == null) {
231: _requestParameterMap = new RequestParameterMap(
232: _portletRequest);
233: }
234: return _requestParameterMap;
235: }
236:
237: @SuppressWarnings("unchecked")
238: @Override
239: public Iterator<String> getRequestParameterNames() {
240: return new EnumerationIterator(_portletRequest
241: .getParameterNames());
242: }
243:
244: @Override
245: public Map<String, String[]> getRequestParameterValuesMap() {
246: if (_requestParameterValuesMap == null) {
247: _requestParameterValuesMap = new RequestParameterValuesMap(
248: _portletRequest);
249: }
250: return _requestParameterValuesMap;
251: }
252:
253: @Override
254: public String getRequestPathInfo() {
255: return null; // must return null
256: }
257:
258: @Override
259: public String getRequestServletPath() {
260: return null; // must return null
261: }
262:
263: @Override
264: public URL getResource(String path) throws MalformedURLException {
265: checkNull(path, "path");
266:
267: return _portletContext.getResource(path);
268: }
269:
270: @Override
271: public InputStream getResourceAsStream(String path) {
272: checkNull(path, "path");
273:
274: return _portletContext.getResourceAsStream(path);
275: }
276:
277: @Override
278: @SuppressWarnings("unchecked")
279: public Set<String> getResourcePaths(String path) {
280: checkNull(path, "path");
281: return _portletContext.getResourcePaths(path);
282: }
283:
284: @Override
285: public Object getResponse() {
286: return _portletResponse;
287: }
288:
289: @Override
290: public String getResponseContentType() {
291: return null;
292: }
293:
294: @Override
295: public Object getSession(boolean create) {
296: return _portletRequest.getPortletSession(create);
297: }
298:
299: @Override
300: public Map<String, Object> getSessionMap() {
301: if (_sessionMap == null) {
302: _sessionMap = new SessionMap(_portletRequest);
303: }
304: return _sessionMap;
305: }
306:
307: @Override
308: public Principal getUserPrincipal() {
309: return _portletRequest.getUserPrincipal();
310: }
311:
312: @Override
313: public boolean isUserInRole(String role) {
314: checkNull(role, "role");
315:
316: return _portletRequest.isUserInRole(role);
317: }
318:
319: @Override
320: public void log(String message) {
321: checkNull(message, "message");
322:
323: _portletContext.log(message);
324: }
325:
326: @Override
327: public void log(String message, Throwable exception) {
328: checkNull(message, "message");
329: checkNull(exception, "exception");
330:
331: _portletContext.log(message, exception);
332: }
333:
334: @Override
335: public void redirect(String url) throws IOException {
336: if (_actionRequest instanceof ActionResponse) {
337: ((ActionResponse) _portletResponse).sendRedirect(url);
338: } else {
339: throw new IllegalArgumentException(
340: "Only ActionResponse supported");
341: }
342: }
343:
344: public void release() {
345: _portletContext = null;
346: _portletRequest = null;
347: _portletResponse = null;
348: _applicationMap = null;
349: _sessionMap = null;
350: _requestMap = null;
351: _requestParameterMap = null;
352: _requestParameterValuesMap = null;
353: _requestHeaderMap = null;
354: _requestHeaderValuesMap = null;
355: _initParameterMap = null;
356: _actionRequest = null;
357: }
358:
359: /**
360: * @since JSF 1.2
361: * @param request
362: */
363: @Override
364: public void setRequest(java.lang.Object request) {
365: this ._portletRequest = (PortletRequest) request;
366: this ._actionRequest = isActionRequest(_portletRequest) ? (ActionRequest) request
367: : null;
368: }
369:
370: /**
371: * @since JSF 1.2
372: * @param encoding
373: * @throws java.io.UnsupportedEncodingException
374: */
375: public void setRequestCharacterEncoding(java.lang.String encoding)
376: throws java.io.UnsupportedEncodingException {
377:
378: if (_actionRequest != null)
379: _actionRequest.setCharacterEncoding(encoding);
380: else
381: throw new UnsupportedOperationException(
382: "Can not set request character encoding to value '"
383: + encoding
384: + "'. Request is not an action request");
385: }
386:
387: @Override
388: public String getRequestCharacterEncoding() {
389: if (_actionRequest != null)
390: return _actionRequest.getCharacterEncoding();
391: throw new UnsupportedOperationException(
392: "Can not get request character encoding. Request is not an action request");
393: }
394:
395: @Override
396: public String getResponseCharacterEncoding() {
397: return null;
398: }
399:
400: /**
401: * @since JSF 1.2
402: * @param response
403: */
404: @Override
405: public void setResponse(java.lang.Object response) {
406: this ._portletResponse = (PortletResponse) response;
407: }
408:
409: /**
410: * @since JSF 1.2
411: * @param encoding
412: */
413: @Override
414: public void setResponseCharacterEncoding(java.lang.String encoding) {
415: // nope!
416: }
417:
418: private void checkNull(Object o, String param) {
419: if (o == null) {
420: throw new NullPointerException(param + " can not be null.");
421: }
422: }
423:
424: private boolean isActionRequest(PortletRequest portletRequest) {
425: return portletRequest instanceof ActionRequest;
426: }
427:
428: }
|