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.servlet;
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.Iterator;
024: import java.util.Locale;
025: import java.util.Map;
026: import java.util.Set;
027:
028: import javax.faces.FacesException;
029: import javax.faces.context.ExternalContext;
030: import javax.faces.context.FacesContext;
031: import javax.servlet.RequestDispatcher;
032: import javax.servlet.ServletContext;
033: import javax.servlet.ServletException;
034: import javax.servlet.ServletRequest;
035: import javax.servlet.ServletResponse;
036: import javax.servlet.http.HttpServletRequest;
037: import javax.servlet.http.HttpServletResponse;
038:
039: import org.apache.myfaces.context.ReleaseableExternalContext;
040: import org.apache.myfaces.util.EnumerationIterator;
041:
042: /**
043: * Implements the external context for servlet request. JSF 1.2, 6.1.3
044: *
045: * @author Manfred Geiler (latest modification by $Author: mbr $)
046: * @author Anton Koinov
047: * @version $Revision: 512416 $ $Date: 2007-02-27 22:20:38 +0100 (Di, 27 Feb 2007) $
048: */
049: public class ServletExternalContextImpl extends ExternalContext
050: implements ReleaseableExternalContext {
051: private static final String INIT_PARAMETER_MAP_ATTRIBUTE = InitParameterMap.class
052: .getName();
053:
054: private ServletContext _servletContext;
055: private ServletRequest _servletRequest;
056: private ServletResponse _servletResponse;
057: private Map<String, Object> _applicationMap;
058: private Map<String, Object> _sessionMap;
059: private Map<String, Object> _requestMap;
060: private Map<String, String> _requestParameterMap;
061: private Map<String, String[]> _requestParameterValuesMap;
062: private Map<String, String> _requestHeaderMap;
063: private Map<String, String[]> _requestHeaderValuesMap;
064: private Map _requestCookieMap;
065: private Map<String, String> _initParameterMap;
066: private HttpServletRequest _httpServletRequest;
067: private String _requestServletPath;
068: private String _requestPathInfo;
069:
070: public ServletExternalContextImpl(ServletContext servletContext,
071: ServletRequest servletRequest,
072: ServletResponse servletResponse) {
073: _servletContext = servletContext;
074: _servletRequest = servletRequest;
075: _servletResponse = servletResponse;
076: _applicationMap = null;
077: _sessionMap = null;
078: _requestMap = null;
079: _requestParameterMap = null;
080: _requestParameterValuesMap = null;
081: _requestHeaderMap = null;
082: _requestHeaderValuesMap = null;
083: _requestCookieMap = null;
084: _initParameterMap = null;
085: _httpServletRequest = isHttpServletRequest(servletRequest) ? (HttpServletRequest) servletRequest
086: : null;
087:
088: if (_httpServletRequest != null) {
089: // HACK: MultipartWrapper scrambles the servletPath for some reason in Tomcat 4.1.29 embedded in JBoss
090: // 3.2.3!?
091: // (this was reported by frederic.auge [frederic.auge@laposte.net])
092: _requestServletPath = _httpServletRequest.getServletPath();
093: _requestPathInfo = _httpServletRequest.getPathInfo();
094: }
095: }
096:
097: public void release() {
098: _servletContext = null;
099: _servletRequest = null;
100: _servletResponse = null;
101: _applicationMap = null;
102: _sessionMap = null;
103: _requestMap = null;
104: _requestParameterMap = null;
105: _requestParameterValuesMap = null;
106: _requestHeaderMap = null;
107: _requestHeaderValuesMap = null;
108: _requestCookieMap = null;
109: _initParameterMap = null;
110: _httpServletRequest = null;
111: }
112:
113: @Override
114: public Object getSession(boolean create) {
115: checkHttpServletRequest();
116: return ((HttpServletRequest) _servletRequest)
117: .getSession(create);
118: }
119:
120: @Override
121: public Object getContext() {
122: return _servletContext;
123: }
124:
125: @Override
126: public Object getRequest() {
127: return _servletRequest;
128: }
129:
130: @Override
131: public Object getResponse() {
132: return _servletResponse;
133: }
134:
135: @Override
136: public String getResponseContentType() {
137: return _servletResponse.getContentType();
138: }
139:
140: @Override
141: public Map<String, Object> getApplicationMap() {
142: if (_applicationMap == null) {
143: _applicationMap = new ApplicationMap(_servletContext);
144: }
145: return _applicationMap;
146: }
147:
148: @Override
149: public Map<String, Object> getSessionMap() {
150: if (_sessionMap == null) {
151: checkHttpServletRequest();
152: _sessionMap = new SessionMap(_httpServletRequest);
153: }
154: return _sessionMap;
155: }
156:
157: @Override
158: public Map<String, Object> getRequestMap() {
159: if (_requestMap == null) {
160: _requestMap = new RequestMap(_servletRequest);
161: }
162: return _requestMap;
163: }
164:
165: @Override
166: public Map<String, String> getRequestParameterMap() {
167: if (_requestParameterMap == null) {
168: _requestParameterMap = new RequestParameterMap(
169: _servletRequest);
170: }
171: return _requestParameterMap;
172: }
173:
174: @Override
175: public Map<String, String[]> getRequestParameterValuesMap() {
176: if (_requestParameterValuesMap == null) {
177: _requestParameterValuesMap = new RequestParameterValuesMap(
178: _servletRequest);
179: }
180: return _requestParameterValuesMap;
181: }
182:
183: @Override
184: @SuppressWarnings("unchecked")
185: public Iterator<String> getRequestParameterNames() {
186: return new EnumerationIterator(_servletRequest
187: .getParameterNames());
188: }
189:
190: @Override
191: public Map<String, String> getRequestHeaderMap() {
192: if (_requestHeaderMap == null) {
193: checkHttpServletRequest();
194: _requestHeaderMap = new RequestHeaderMap(
195: _httpServletRequest);
196: }
197: return _requestHeaderMap;
198: }
199:
200: @Override
201: public Map<String, String[]> getRequestHeaderValuesMap() {
202: if (_requestHeaderValuesMap == null) {
203: checkHttpServletRequest();
204: _requestHeaderValuesMap = new RequestHeaderValuesMap(
205: _httpServletRequest);
206: }
207: return _requestHeaderValuesMap;
208: }
209:
210: @Override
211: @SuppressWarnings("unchecked")
212: public Map<String, Object> getRequestCookieMap() {
213: if (_requestCookieMap == null) {
214: checkHttpServletRequest();
215: _requestCookieMap = new CookieMap(_httpServletRequest);
216: }
217: return _requestCookieMap;
218: }
219:
220: @Override
221: public Locale getRequestLocale() {
222: return _servletRequest.getLocale();
223: }
224:
225: @Override
226: public String getRequestPathInfo() {
227: checkHttpServletRequest();
228: // return (_httpServletRequest).getPathInfo();
229: // HACK: see constructor
230: return _requestPathInfo;
231: }
232:
233: @Override
234: public String getRequestContentType() {
235: return _servletRequest.getContentType();
236: }
237:
238: @Override
239: public String getRequestContextPath() {
240: checkHttpServletRequest();
241: return _httpServletRequest.getContextPath();
242: }
243:
244: @Override
245: public String getInitParameter(String s) {
246: return _servletContext.getInitParameter(s);
247: }
248:
249: @Override
250: @SuppressWarnings("unchecked")
251: public Map<String, String> getInitParameterMap() {
252: if (_initParameterMap == null) {
253: // We cache it as an attribute in ServletContext itself (is this circular reference a problem?)
254: if ((_initParameterMap = (Map<String, String>) _servletContext
255: .getAttribute(INIT_PARAMETER_MAP_ATTRIBUTE)) == null) {
256: _initParameterMap = new InitParameterMap(
257: _servletContext);
258: _servletContext
259: .setAttribute(INIT_PARAMETER_MAP_ATTRIBUTE,
260: _initParameterMap);
261: }
262: }
263: return _initParameterMap;
264: }
265:
266: @Override
267: @SuppressWarnings("unchecked")
268: public Set<String> getResourcePaths(String s) {
269: return _servletContext.getResourcePaths(s);
270: }
271:
272: @Override
273: public InputStream getResourceAsStream(String s) {
274: return _servletContext.getResourceAsStream(s);
275: }
276:
277: @Override
278: public String encodeActionURL(String s) {
279: checkHttpServletRequest();
280: return ((HttpServletResponse) _servletResponse).encodeURL(s);
281: }
282:
283: @Override
284: public String encodeResourceURL(String s) {
285: checkHttpServletRequest();
286: return ((HttpServletResponse) _servletResponse).encodeURL(s);
287: }
288:
289: @Override
290: public String encodeNamespace(String s) {
291: return s;
292: }
293:
294: @Override
295: public void dispatch(String requestURI) throws IOException,
296: FacesException {
297: RequestDispatcher requestDispatcher = _servletRequest
298: .getRequestDispatcher(requestURI);
299:
300: // If there is no dispatcher, send NOT_FOUND
301: if (requestDispatcher == null) {
302: ((HttpServletResponse) _servletResponse)
303: .sendError(HttpServletResponse.SC_NOT_FOUND);
304:
305: return;
306: }
307:
308: try {
309: requestDispatcher
310: .forward(_servletRequest, _servletResponse);
311: } catch (ServletException e) {
312: if (e.getMessage() != null) {
313: throw new FacesException(e.getMessage(), e);
314: }
315:
316: throw new FacesException(e);
317:
318: }
319: }
320:
321: @Override
322: public String getRequestServletPath() {
323: checkHttpServletRequest();
324: // return (_httpServletRequest).getServletPath();
325: // HACK: see constructor
326: return _requestServletPath;
327: }
328:
329: @Override
330: public String getAuthType() {
331: checkHttpServletRequest();
332: return _httpServletRequest.getAuthType();
333: }
334:
335: @Override
336: public String getRemoteUser() {
337: checkHttpServletRequest();
338: return _httpServletRequest.getRemoteUser();
339: }
340:
341: @Override
342: public boolean isUserInRole(String role) {
343: checkHttpServletRequest();
344: return _httpServletRequest.isUserInRole(role);
345: }
346:
347: @Override
348: public Principal getUserPrincipal() {
349: checkHttpServletRequest();
350: return _httpServletRequest.getUserPrincipal();
351: }
352:
353: @Override
354: public void log(String message) {
355: _servletContext.log(message);
356: }
357:
358: @Override
359: public void log(String message, Throwable t) {
360: _servletContext.log(message, t);
361: }
362:
363: @Override
364: public void redirect(String url) throws IOException {
365: if (_servletResponse instanceof HttpServletResponse) {
366: ((HttpServletResponse) _servletResponse).sendRedirect(url);
367: FacesContext.getCurrentInstance().responseComplete();
368: } else {
369: throw new IllegalArgumentException(
370: "Only HttpServletResponse supported");
371: }
372: }
373:
374: @SuppressWarnings("unchecked")
375: public Iterator<Locale> getRequestLocales() {
376: checkHttpServletRequest();
377: return new EnumerationIterator(_httpServletRequest.getLocales());
378: }
379:
380: @Override
381: public URL getResource(String s) throws MalformedURLException {
382: return _servletContext.getResource(s);
383: }
384:
385: /**
386: * @since JSF 1.2
387: * @param request
388: */
389: @Override
390: public void setRequest(java.lang.Object request) {
391: this ._servletRequest = (ServletRequest) request;
392: this ._httpServletRequest = isHttpServletRequest(_servletRequest) ? (HttpServletRequest) _servletRequest
393: : null;
394: this ._httpServletRequest = isHttpServletRequest(_servletRequest) ? (HttpServletRequest) _servletRequest
395: : null;
396: }
397:
398: /**
399: * @since JSF 1.2
400: * @param encoding
401: * @throws java.io.UnsupportedEncodingException
402: */
403: @Override
404: public void setRequestCharacterEncoding(java.lang.String encoding)
405: throws java.io.UnsupportedEncodingException {
406:
407: this ._servletRequest.setCharacterEncoding(encoding);
408:
409: }
410:
411: /**
412: * @since JSF 1.2
413: */
414: @Override
415: public String getRequestCharacterEncoding() {
416: return _servletRequest.getCharacterEncoding();
417: }
418:
419: /**
420: * @since JSF 1.2
421: */
422: @Override
423: public String getResponseCharacterEncoding() {
424: return _servletResponse.getCharacterEncoding();
425: }
426:
427: /**
428: * @since JSF 1.2
429: * @param response
430: */
431: @Override
432: public void setResponse(java.lang.Object response) {
433: this ._servletResponse = (ServletResponse) response;
434: }
435:
436: /**
437: * @since JSF 1.2
438: * @param encoding
439: */
440: @Override
441: public void setResponseCharacterEncoding(java.lang.String encoding) {
442: this ._servletResponse.setCharacterEncoding(encoding);
443: }
444:
445: private void checkHttpServletRequest() {
446: if (_httpServletRequest == null) {
447: throw new UnsupportedOperationException(
448: "Only HttpServletRequest supported");
449: }
450: }
451:
452: private boolean isHttpServletRequest(ServletRequest servletRequest) {
453: return servletRequest instanceof HttpServletRequest;
454: }
455:
456: }
|