001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License version 2
011: * as published by the Free Software Foundation.
012: *
013: * Resin Open Source is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
016: * of NON-INFRINGEMENT. See the GNU General Public License for more
017: * details.
018: *
019: * You should have received a copy of the GNU General Public License
020: * along with Resin Open Source; if not, write to the
021: *
022: * Free Software Foundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.jsf.context;
030:
031: import java.io.*;
032: import java.net.URL;
033: import java.net.MalformedURLException;
034: import java.util.*;
035: import java.util.logging.Logger;
036: import java.security.*;
037:
038: import javax.faces.*;
039: import javax.faces.context.*;
040:
041: import javax.servlet.*;
042: import javax.servlet.http.*;
043:
044: import com.caucho.server.connection.*;
045: import com.caucho.util.*;
046:
047: public class ServletExternalContext extends ExternalContext {
048: private static final L10N L = new L10N(ServletExternalContext.class);
049:
050: private static final Logger log = Logger
051: .getLogger(ServletFacesContextImpl.class.getName());
052:
053: private ServletContext _webApp;
054: private HttpServletRequest _request;
055: private HttpServletResponse _response;
056:
057: private Map<String, String> _requestParameterMap;
058: private Map<String, Object> _applicationMap;
059: private Map<String, Object> _requestMap;
060: private Map<String, Object> _sessionMap;
061:
062: ServletExternalContext(ServletContext webApp,
063: HttpServletRequest request, HttpServletResponse response) {
064: _webApp = webApp;
065: _request = request;
066: _response = response;
067: }
068:
069: public void dispatch(String path) throws IOException {
070: try {
071: //_request.getRequestDispatcher(path).include(_request, _response);
072: RequestDispatcher rd = _request.getRequestDispatcher(path);
073:
074: if (rd == null)
075: throw new FacesException(L.l(
076: "'{0}' is an unknown dispatcher.", path));
077:
078: rd.forward(_request, _response);
079: } catch (ServletException e) {
080: throw new FacesException(e);
081: }
082: }
083:
084: public String encodeActionURL(String url) {
085: if (url == null)
086: throw new NullPointerException();
087:
088: return _response.encodeURL(url);
089: }
090:
091: public String encodeNamespace(String name) {
092: if (name == null)
093: throw new NullPointerException();
094:
095: return name;
096: }
097:
098: public String encodeResourceURL(String url) {
099: if (url == null)
100: throw new NullPointerException();
101:
102: return _response.encodeURL(url);
103: }
104:
105: public Map<String, Object> getApplicationMap() {
106: if (_applicationMap == null)
107: _applicationMap = new ApplicationMap();
108:
109: return _applicationMap;
110: }
111:
112: public String getAuthType() {
113: return _request.getAuthType();
114: }
115:
116: public Object getContext() {
117: return _webApp;
118: }
119:
120: public String getInitParameter(String name) {
121: if (name == null)
122: throw new NullPointerException();
123:
124: return _webApp.getInitParameter(name);
125: }
126:
127: public Map getInitParameterMap() {
128: HashMap map = new HashMap();
129:
130: Enumeration e = _webApp.getInitParameterNames();
131: while (e.hasMoreElements()) {
132: String name = (String) e.nextElement();
133:
134: map.put(name, _webApp.getInitParameter(name));
135: }
136:
137: return Collections.unmodifiableMap(map);
138: }
139:
140: public String getRemoteUser() {
141: return _request.getRemoteUser();
142: }
143:
144: public Object getRequest() {
145: return _request;
146: }
147:
148: /**
149: * @Since 1.2
150: */
151: public void setRequest(Object request) {
152: //hack: trinidad passes an instance of ServerRequest
153: // for an internal check that does not seem to affect
154: // further operation
155: if (request instanceof HttpServletRequest)
156: _request = (HttpServletRequest) request;
157: else
158: log.warning("parameter request is not HttpServletRequest");
159: }
160:
161: /**
162: * @Since 1.2
163: */
164: public void setRequestCharacterEncoding(String encoding)
165: throws UnsupportedEncodingException {
166: _request.setCharacterEncoding(encoding);
167: }
168:
169: public String getRequestContextPath() {
170: return _request.getContextPath();
171: }
172:
173: public Map<String, Object> getRequestCookieMap() {
174: HashMap<String, Object> map = new HashMap<String, Object>();
175:
176: Cookie[] cookies = _request.getCookies();
177:
178: if (cookies == null)
179: return map;
180:
181: for (int i = 0; i < cookies.length; i++) {
182: map.put(cookies[i].getName(), cookies[i]);
183: }
184:
185: return Collections.unmodifiableMap(map);
186: }
187:
188: public Map<String, String> getRequestHeaderMap() {
189: HashMap<String, String> map = new HashMap<String, String>();
190:
191: Enumeration e = _request.getHeaderNames();
192: while (e.hasMoreElements()) {
193: String name = (String) e.nextElement();
194:
195: map.put(name, _request.getHeader(name));
196: }
197:
198: return Collections.unmodifiableMap(map);
199: }
200:
201: public Map<String, String[]> getRequestHeaderValuesMap() {
202: HashMap<String, String[]> map = new HashMap<String, String[]>();
203:
204: Enumeration e = _request.getHeaderNames();
205: while (e.hasMoreElements()) {
206: String name = (String) e.nextElement();
207:
208: Enumeration e1 = _request.getHeaders(name);
209: ArrayList<String> values = new ArrayList<String>();
210: while (e1.hasMoreElements()) {
211: values.add((String) e1.nextElement());
212: }
213:
214: map.put(name, values.toArray(new String[values.size()]));
215: }
216:
217: return Collections.unmodifiableMap(map);
218: }
219:
220: public Locale getRequestLocale() {
221: return _request.getLocale();
222: }
223:
224: public Iterator<Locale> getRequestLocales() {
225: ArrayList<Locale> locales = new ArrayList<Locale>();
226:
227: Enumeration e = _request.getLocales();
228: while (e.hasMoreElements())
229: locales.add((Locale) e.nextElement());
230:
231: return locales.iterator();
232: }
233:
234: public Map<String, Object> getRequestMap() {
235: if (_requestMap == null)
236: _requestMap = new RequestMap();
237:
238: return _requestMap;
239: }
240:
241: public Map<String, String> getRequestParameterMap() {
242: if (_requestParameterMap == null) {
243: HashMap<String, String> map = new HashMap<String, String>();
244:
245: Enumeration e = _request.getParameterNames();
246: while (e.hasMoreElements()) {
247: String name = (String) e.nextElement();
248:
249: map.put(name, _request.getParameter(name));
250: }
251:
252: _requestParameterMap = Collections.unmodifiableMap(map);
253: }
254:
255: return _requestParameterMap;
256: }
257:
258: public Iterator<String> getRequestParameterNames() {
259: return getRequestParameterMap().keySet().iterator();
260: }
261:
262: public Map<String, String[]> getRequestParameterValuesMap() {
263: HashMap<String, String[]> map = new HashMap<String, String[]>();
264:
265: Enumeration e = _request.getParameterNames();
266: while (e.hasMoreElements()) {
267: String name = (String) e.nextElement();
268:
269: map.put(name, _request.getParameterValues(name));
270: }
271:
272: return Collections.unmodifiableMap(map);
273: }
274:
275: public String getRequestPathInfo() {
276: if (_request instanceof CauchoRequest) {
277: return ((CauchoRequest) _request).getPagePathInfo();
278: } else {
279: // XXX: include
280:
281: return _request.getPathInfo();
282: }
283: }
284:
285: public String getRequestServletPath() {
286: if (_request instanceof CauchoRequest) {
287: return ((CauchoRequest) _request).getPageServletPath();
288: } else {
289: // XXX: include
290:
291: return _request.getServletPath();
292: }
293: }
294:
295: /**
296: * @Since 1.2
297: */
298: public String getRequestCharacterEncoding() {
299: return _request.getCharacterEncoding();
300: }
301:
302: /**
303: * @Since 1.2
304: */
305: public String getRequestContentType() {
306: return _request.getContentType();
307: }
308:
309: /**
310: * @Since 1.2
311: */
312: public String getResponseCharacterEncoding() {
313: return _response.getCharacterEncoding();
314: }
315:
316: /**
317: * @Since 1.2
318: */
319: public String getResponseContentType() {
320: return _response.getContentType();
321: }
322:
323: public URL getResource(String path) throws MalformedURLException {
324: return _webApp.getResource(path);
325: }
326:
327: public InputStream getResourceAsStream(String path) {
328: return _webApp.getResourceAsStream(path);
329: }
330:
331: public Set<String> getResourcePaths(String path) {
332: return _webApp.getResourcePaths(path);
333: }
334:
335: public Object getResponse() {
336: return _response;
337: }
338:
339: /**
340: * @Since 1.2
341: */
342: public void setResponse(Object response) {
343: _response = (HttpServletResponse) response;
344: }
345:
346: /**
347: * @Since 1.2
348: */
349: public void setResponseCharacterEncoding(String encoding) {
350: _response.setCharacterEncoding(encoding);
351: }
352:
353: public Object getSession(boolean create) {
354: return _request.getSession(create);
355: }
356:
357: public Map<String, Object> getSessionMap() {
358: if (_sessionMap == null)
359: _sessionMap = new SessionMap(_request.getSession(true));
360:
361: return _sessionMap;
362: }
363:
364: public Principal getUserPrincipal() {
365: return _request.getUserPrincipal();
366: }
367:
368: public boolean isUserInRole(String role) {
369: return _request.isUserInRole(role);
370: }
371:
372: public void log(String message) {
373: _webApp.log(message);
374: }
375:
376: public void log(String message, Throwable exn) {
377: _webApp.log(message, exn);
378: }
379:
380: public void redirect(String url) throws IOException {
381: _response.sendRedirect(url);
382:
383: FacesContext.getCurrentInstance().responseComplete();
384: }
385:
386: class ApplicationMap extends AttributeMap {
387: public Object get(String key) {
388: return _webApp.getAttribute(key);
389: }
390:
391: public Object put(String key, Object value) {
392: _webApp.setAttribute(key, value);
393:
394: return null;
395: }
396:
397: public Object remove(String key) {
398: _webApp.removeAttribute(key);
399:
400: return null;
401: }
402:
403: public Enumeration getNames() {
404: return _webApp.getAttributeNames();
405: }
406: }
407:
408: class SessionMap extends AttributeMap {
409: private HttpSession _session;
410:
411: SessionMap(HttpSession session) {
412: _session = session;
413: }
414:
415: public Object get(String key) {
416: return _session.getAttribute(key);
417: }
418:
419: public Object put(String key, Object value) {
420: _session.setAttribute(key, value);
421:
422: return null;
423: }
424:
425: public Object remove(String key) {
426: _session.removeAttribute(key);
427:
428: return null;
429: }
430:
431: public Enumeration getNames() {
432: return _session.getAttributeNames();
433: }
434: }
435:
436: class RequestMap extends AttributeMap {
437: public Object get(String key) {
438: return _request.getAttribute(key);
439: }
440:
441: public Object put(String key, Object value) {
442: _request.setAttribute(key, value);
443:
444: return null;
445: }
446:
447: public Object remove(String key) {
448: _request.removeAttribute(key);
449:
450: return null;
451: }
452:
453: public Enumeration getNames() {
454: return _request.getAttributeNames();
455: }
456: }
457: }
|