001: /*
002: * This file is part of the WfMOpen project.
003: * Copyright (C) 2001-2006 Danet GmbH (www.danet.de), BU BTS.
004: * All rights reserved.
005: *
006: * This program is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU General Public License as published by
008: * the Free Software Foundation; either version 2 of the License, or
009: * (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * $Id: AjaxExternalContext.java,v 1.3 2007/03/27 21:59:44 mlipp Exp $
021: *
022: * $Log: AjaxExternalContext.java,v $
023: * Revision 1.3 2007/03/27 21:59:44 mlipp
024: * Fixed lots of checkstyle warnings.
025: *
026: * Revision 1.2 2006/12/12 09:53:20 drmlipp
027: * Added Ajax support.
028: *
029: * Revision 1.1.2.3 2006/11/27 14:28:33 drmlipp
030: * Continuing.
031: *
032: * Revision 1.1.2.2 2006/11/25 22:45:56 mlipp
033: * Got request through to JSF.
034: *
035: * Revision 1.1.2.1 2006/11/24 16:15:31 drmlipp
036: * Continuing Ajax support.
037: *
038: */
039: package de.danet.an.util.jsf;
040:
041: import java.io.IOException;
042: import java.io.InputStream;
043: import java.net.MalformedURLException;
044: import java.net.URL;
045: import java.security.Principal;
046: import java.util.AbstractMap;
047: import java.util.Collections;
048: import java.util.Enumeration;
049: import java.util.HashMap;
050: import java.util.Iterator;
051: import java.util.Locale;
052: import java.util.Map;
053: import java.util.Set;
054:
055: import javax.faces.context.ExternalContext;
056: import javax.portlet.PortalContext;
057: import javax.portlet.PortletMode;
058: import javax.portlet.PortletPreferences;
059: import javax.portlet.PortletRequest;
060: import javax.portlet.PortletSession;
061: import javax.portlet.WindowState;
062: import javax.servlet.RequestDispatcher;
063: import javax.servlet.ServletContext;
064: import javax.servlet.ServletException;
065: import javax.servlet.http.HttpServletRequest;
066: import javax.servlet.http.HttpServletResponse;
067: import javax.servlet.http.HttpSession;
068:
069: import org.apache.myfaces.context.ReleaseableExternalContext;
070: import org.apache.myfaces.context.servlet.ApplicationMap;
071:
072: /**
073: * This class provides an ExternalContext for use with Ajax requests.
074: *
075: * @author Michael Lipp
076: */
077: public final class AjaxExternalContext extends ExternalContext
078: implements ReleaseableExternalContext {
079:
080: private static final org.apache.commons.logging.Log logger = org.apache.commons.logging.LogFactory
081: .getLog(AjaxExternalContext.class);
082:
083: private HttpServletRequest request;
084: private HttpServletResponse response;
085: private String sessionPrefix = null;
086:
087: private PortletRequest wrappedRequest = null;
088: private Map applicationMap = null;
089: private Map initParamMap = null;
090: private Map requestHeaderMap = null;
091: private Map requestHeaderValuesMap = null;
092: private Map requestMap = null;
093: private Map requestParameterMap = null;
094: private Map sessionMap = null;
095:
096: /**
097: * Create a new instance with all attributes initialized
098: * to defaults or the given values.
099: *
100: * @param request
101: * @param response
102: */
103: private AjaxExternalContext(HttpServletRequest request,
104: HttpServletResponse response, String sessionPrefix) {
105: this .request = request;
106: this .response = response;
107: this .sessionPrefix = sessionPrefix;
108: }
109:
110: public static AjaxExternalContext makeContext(
111: HttpServletRequest request, HttpServletResponse response)
112: throws ServletException, IOException {
113: String namespace = request
114: .getParameter(MyFacesAjaxServlet.RENDER_NAMESPACE);
115: if (namespace == null) {
116: response.sendError(HttpServletResponse.SC_BAD_REQUEST);
117: throw new ServletException("Invalid request: "
118: + request.getRequestURL());
119: }
120: HttpSession session = request.getSession();
121: String sessionPrefix = null;
122: for (Enumeration e = session.getAttributeNames(); e
123: .hasMoreElements();) {
124: String key = (String) e.nextElement();
125: if (key.endsWith("?" + MyFacesAjaxServlet.RENDER_NAMESPACE)
126: && ((String) session.getAttribute(key))
127: .equals(namespace)) {
128: sessionPrefix = key.substring(0, key
129: .indexOf(MyFacesAjaxServlet.RENDER_NAMESPACE));
130: if (logger.isDebugEnabled()) {
131: logger.debug("Using session prefix "
132: + sessionPrefix);
133: }
134: break;
135: }
136: }
137: if (sessionPrefix == null) {
138: response.sendError(HttpServletResponse.SC_NOT_FOUND);
139: throw new ServletException(
140: "No portlet session with namespace " + namespace);
141: }
142: return new AjaxExternalContext(request, response, sessionPrefix);
143: }
144:
145: /* (non-Javadoc)
146: * @see javax.faces.context.ExternalContext#dispatch(java.lang.String)
147: */
148: public void dispatch(String path) throws IOException {
149: RequestDispatcher disp = request.getSession()
150: .getServletContext().getRequestDispatcher(path);
151: if (disp == null) {
152: throw new IllegalArgumentException("Not found: " + path);
153: }
154: try {
155: disp.forward(request, response);
156: } catch (ServletException e) {
157: throw (IOException) (new IOException(e.getMessage()))
158: .initCause(e);
159: }
160: }
161:
162: /* (non-Javadoc)
163: * @see javax.faces.context.ExternalContext#encodeActionURL(java.lang.String)
164: */
165: public String encodeActionURL(String url) {
166: return response.encodeURL(url);
167: }
168:
169: /* (non-Javadoc)
170: * @see javax.faces.context.ExternalContext#encodeNamespace(java.lang.String)
171: */
172: public String encodeNamespace(String name) {
173: String prefix = (String) getSessionMap().get(
174: MyFacesAdaptedPortlet.NAMESPACE_PREFIX);
175: return prefix + name;
176: }
177:
178: /* (non-Javadoc)
179: * @see javax.faces.context.ExternalContext#encodeResourceURL(java.lang.String)
180: */
181: public String encodeResourceURL(String url) {
182: return response.encodeURL(url);
183: }
184:
185: /* (non-Javadoc)
186: * @see javax.faces.context.ExternalContext#getApplicationMap()
187: */
188: public Map getApplicationMap() {
189: if (applicationMap == null) {
190: applicationMap = new ApplicationMap();
191: }
192: return applicationMap;
193: }
194:
195: /* (non-Javadoc)
196: * @see javax.faces.context.ExternalContext#getAuthType()
197: */
198: public String getAuthType() {
199: return request.getAuthType();
200: }
201:
202: /* (non-Javadoc)
203: * @see javax.faces.context.ExternalContext#getContext()
204: */
205: public Object getContext() {
206: return request.getSession().getServletContext();
207: }
208:
209: /* (non-Javadoc)
210: * @see javax.faces.context.ExternalContext#getInitParameter
211: */
212: public String getInitParameter(String param) {
213: return request.getSession().getServletContext()
214: .getInitParameter(param);
215: }
216:
217: /* (non-Javadoc)
218: * @see javax.faces.context.ExternalContext#getInitParameterMap()
219: */
220: public Map getInitParameterMap() {
221: if (initParamMap == null) {
222: initParamMap = new InitParametersMap();
223: }
224: return initParamMap;
225: }
226:
227: /* (non-Javadoc)
228: * @see javax.faces.context.ExternalContext#getRemoteUser()
229: */
230: public String getRemoteUser() {
231: return request.getRemoteUser();
232: }
233:
234: /* (non-Javadoc)
235: * @see javax.faces.context.ExternalContext#getRequest()
236: */
237: public Object getRequest() {
238: if (wrappedRequest == null) {
239: wrappedRequest = new RequestWrapper();
240: }
241: return wrappedRequest;
242: }
243:
244: /* (non-Javadoc)
245: * @see javax.faces.context.ExternalContext#getRequestContextPath()
246: */
247: public String getRequestContextPath() {
248: return request.getContextPath();
249: }
250:
251: /* (non-Javadoc)
252: * @see javax.faces.context.ExternalContext#getRequestCookieMap()
253: */
254: public Map getRequestCookieMap() {
255: return Collections.EMPTY_MAP;
256: }
257:
258: /* (non-Javadoc)
259: * @see javax.faces.context.ExternalContext#getRequestHeaderMap()
260: */
261: public Map getRequestHeaderMap() {
262: if (requestHeaderMap == null) {
263: requestHeaderMap = new RequestHeaderMap();
264: }
265: return requestHeaderMap;
266: }
267:
268: /* (non-Javadoc)
269: * @see javax.faces.context.ExternalContext#getRequestHeaderValuesMap()
270: */
271: public Map getRequestHeaderValuesMap() {
272: if (requestHeaderValuesMap == null) {
273: requestHeaderValuesMap = new RequestHeaderValuesMap();
274: }
275: return requestHeaderValuesMap;
276: }
277:
278: /* (non-Javadoc)
279: * @see javax.faces.context.ExternalContext#getRequestLocale()
280: */
281: public Locale getRequestLocale() {
282: return request.getLocale();
283: }
284:
285: /* (non-Javadoc)
286: * @see javax.faces.context.ExternalContext#getRequestLocales()
287: */
288: public Iterator getRequestLocales() {
289: return Collections.list(request.getLocales()).iterator();
290: }
291:
292: /* (non-Javadoc)
293: * @see javax.faces.context.ExternalContext#getRequestMap()
294: */
295: public Map getRequestMap() {
296: if (requestMap == null) {
297: requestMap = new RequestMap();
298: }
299: return requestMap;
300: }
301:
302: /* (non-Javadoc)
303: * @see javax.faces.context.ExternalContext#getRequestParameterMap()
304: */
305: public Map getRequestParameterMap() {
306: if (requestParameterMap == null) {
307: requestParameterMap = new RequestParameterMap();
308: }
309: return requestParameterMap;
310: }
311:
312: /* (non-Javadoc)
313: * @see javax.faces.context.ExternalContext#getRequestParameterNames()
314: */
315: public Iterator getRequestParameterNames() {
316: return Collections.list(request.getParameterNames()).iterator();
317: }
318:
319: /* (non-Javadoc)
320: * @see javax.faces.context.ExternalContext#getRequestParameterValuesMap()
321: */
322: public Map getRequestParameterValuesMap() {
323: return request.getParameterMap();
324: }
325:
326: /* (non-Javadoc)
327: * @see javax.faces.context.ExternalContext#getRequestPathInfo()
328: */
329: public String getRequestPathInfo() {
330: return null;
331: }
332:
333: /* (non-Javadoc)
334: * @see javax.faces.context.ExternalContext#getRequestServletPath()
335: */
336: public String getRequestServletPath() {
337: return null;
338: }
339:
340: /* (non-Javadoc)
341: * @see javax.faces.context.ExternalContext#getResource(java.lang.String)
342: */
343: public URL getResource(String path) throws MalformedURLException {
344: return request.getSession().getServletContext().getResource(
345: path);
346: }
347:
348: /* (non-Javadoc)
349: * @see javax.faces.context.ExternalContext#getResourceAsStream(java.lang.String)
350: */
351: public InputStream getResourceAsStream(String path) {
352: return request.getSession().getServletContext()
353: .getResourceAsStream(path);
354: }
355:
356: /* (non-Javadoc)
357: * @see javax.faces.context.ExternalContext#getResourcePaths(java.lang.String)
358: */
359: public Set getResourcePaths(String path) {
360: return request.getSession().getServletContext()
361: .getResourcePaths(path);
362: }
363:
364: /* (non-Javadoc)
365: * @see javax.faces.context.ExternalContext#getResponse()
366: */
367: public Object getResponse() {
368: return response;
369: }
370:
371: /* (non-Javadoc)
372: * @see javax.faces.context.ExternalContext#getSession(boolean)
373: */
374: public Object getSession(boolean arg0) {
375: return request.getSession();
376: }
377:
378: /* (non-Javadoc)
379: * @see javax.faces.context.ExternalContext#getSessionMap()
380: */
381: public Map getSessionMap() {
382: if (sessionMap == null) {
383: sessionMap = new SessionMap();
384: }
385: return sessionMap;
386: }
387:
388: /* (non-Javadoc)
389: * @see javax.faces.context.ExternalContext#getUserPrincipal()
390: */
391: public Principal getUserPrincipal() {
392: return request.getUserPrincipal();
393: }
394:
395: /* (non-Javadoc)
396: * @see javax.faces.context.ExternalContext#isUserInRole(java.lang.String)
397: */
398: public boolean isUserInRole(String role) {
399: return request.isUserInRole(role);
400: }
401:
402: /* (non-Javadoc)
403: * @see javax.faces.context.ExternalContext#log(java.lang.String)
404: */
405: public void log(String arg0) {
406: request.getSession().getServletContext().log(arg0);
407: }
408:
409: /* (non-Javadoc)
410: * @see javax.faces.context.ExternalContext#log(java.lang.String, java.lang.Throwable)
411: */
412: public void log(String arg0, Throwable arg1) {
413: request.getSession().getServletContext().log(arg0, arg1);
414: }
415:
416: /* (non-Javadoc)
417: * @see javax.faces.context.ExternalContext#redirect(java.lang.String)
418: */
419: public void redirect(String arg0) throws IOException {
420: response.sendRedirect(arg0);
421: }
422:
423: public class ApplicationMap extends AbstractMap {
424:
425: private Map backupCache = null;
426:
427: private Map backup() {
428: if (backupCache == null) {
429: backupCache = new HashMap();
430: ServletContext context = request.getSession()
431: .getServletContext();
432: for (Enumeration e = context.getAttributeNames(); e
433: .hasMoreElements();) {
434: String key = (String) e.nextElement();
435: backupCache.put(key, context.getAttribute(key));
436: }
437: }
438: return backupCache;
439: }
440:
441: /* (non-Javadoc)
442: * @see java.util.AbstractMap#entrySet()
443: */
444: public Set entrySet() {
445: return backup().entrySet();
446: }
447:
448: /* (non-Javadoc)
449: * @see java.util.AbstractMap#put(java.lang.Object, java.lang.Object)
450: */
451: public Object put(Object key, Object value) {
452: ServletContext context = request.getSession()
453: .getServletContext();
454: context.setAttribute((String) key, value);
455: return backup().put(key, value);
456: }
457:
458: /* (non-Javadoc)
459: * @see java.util.AbstractMap#remove(java.lang.Object)
460: */
461: public Object remove(Object key) {
462: ServletContext context = request.getSession()
463: .getServletContext();
464: context.removeAttribute((String) key);
465: return backup().remove(key);
466: }
467: }
468:
469: public class InitParametersMap extends AbstractMap {
470:
471: private Map backup = null;
472:
473: /* (non-Javadoc)
474: * @see java.util.AbstractMap#entrySet()
475: */
476: public Set entrySet() {
477: if (backup == null) {
478: backup = new HashMap();
479: ServletContext context = request.getSession()
480: .getServletContext();
481: for (Enumeration e = context.getInitParameterNames(); e
482: .hasMoreElements();) {
483: String key = (String) e.nextElement();
484: backup.put(key, context.getInitParameter(key));
485: }
486: }
487: return backup.entrySet();
488: }
489: }
490:
491: public class RequestHeaderMap extends AbstractMap {
492:
493: private Map backup = null;
494:
495: /* (non-Javadoc)
496: * @see java.util.AbstractMap#entrySet()
497: */
498: public Set entrySet() {
499: if (backup == null) {
500: backup = new HashMap();
501: for (Enumeration e = request.getHeaderNames(); e
502: .hasMoreElements();) {
503: String key = (String) e.nextElement();
504: backup.put(key, request.getHeader(key));
505: }
506: }
507: return backup.entrySet();
508: }
509: }
510:
511: public class RequestHeaderValuesMap extends AbstractMap {
512:
513: private Map backup = null;
514:
515: /* (non-Javadoc)
516: * @see java.util.AbstractMap#entrySet()
517: */
518: public Set entrySet() {
519: if (backup == null) {
520: backup = new HashMap();
521: for (Enumeration e = request.getHeaderNames(); e
522: .hasMoreElements();) {
523: String key = (String) e.nextElement();
524: backup.put(key, Collections.list(
525: request.getHeaders(key)).toArray(
526: new String[0]));
527: }
528: }
529: return backup.entrySet();
530: }
531: }
532:
533: public class RequestMap extends AbstractMap {
534:
535: private Map backupCache = null;
536:
537: private Map backup() {
538: if (backupCache == null) {
539: backupCache = new HashMap();
540: for (Enumeration e = request.getAttributeNames(); e
541: .hasMoreElements();) {
542: String key = (String) e.nextElement();
543: backupCache.put(key, request.getAttribute(key));
544: }
545: }
546: return backupCache;
547: }
548:
549: /* (non-Javadoc)
550: * @see java.util.AbstractMap#entrySet()
551: */
552: public Set entrySet() {
553: return backup().entrySet();
554: }
555:
556: /* (non-Javadoc)
557: * @see java.util.AbstractMap#put(java.lang.Object, java.lang.Object)
558: */
559: public Object put(Object key, Object value) {
560: request.setAttribute((String) key, value);
561: return backup().put(key, value);
562: }
563:
564: /* (non-Javadoc)
565: * @see java.util.AbstractMap#remove(java.lang.Object)
566: */
567: public Object remove(Object key) {
568: request.removeAttribute((String) key);
569: return backup().remove(key);
570: }
571: }
572:
573: public class RequestParameterMap extends AbstractMap {
574:
575: private Map backup = null;
576:
577: /* (non-Javadoc)
578: * @see java.util.AbstractMap#entrySet()
579: */
580: public Set entrySet() {
581: if (backup == null) {
582: backup = new HashMap();
583: for (Enumeration e = request.getParameterNames(); e
584: .hasMoreElements();) {
585: String key = (String) e.nextElement();
586: backup.put(key, request.getParameter(key));
587: }
588: }
589: return backup.entrySet();
590: }
591: }
592:
593: public class SessionMap extends AbstractMap {
594:
595: private Map backupCache = null;
596:
597: private Map backup() {
598: if (backupCache == null) {
599: backupCache = new HashMap();
600: HttpSession session = request.getSession();
601: for (Enumeration e = session.getAttributeNames(); e
602: .hasMoreElements();) {
603: String key = (String) e.nextElement();
604: if (key.startsWith(sessionPrefix)) {
605: backupCache.put(key.substring(sessionPrefix
606: .length()), session.getAttribute(key));
607: }
608: }
609: }
610: return backupCache;
611: }
612:
613: /* (non-Javadoc)
614: * @see java.util.AbstractMap#entrySet()
615: */
616: public Set entrySet() {
617: return backup().entrySet();
618: }
619:
620: /* (non-Javadoc)
621: * @see java.util.AbstractMap#put(java.lang.Object, java.lang.Object)
622: */
623: public Object put(Object key, Object value) {
624: HttpSession session = request.getSession();
625: session.setAttribute(sessionPrefix + (String) key, value);
626: return backup().put(key, value);
627: }
628:
629: /* (non-Javadoc)
630: * @see java.util.AbstractMap#remove(java.lang.Object)
631: */
632: public Object remove(Object key) {
633: HttpSession session = request.getSession();
634: session.removeAttribute(sessionPrefix + (String) key);
635: return backup().remove(key);
636: }
637: }
638:
639: /**
640: * Make the servlet request look like a portlet request as much as
641: * possible.
642: *
643: * @author Michael Lipp
644: */
645: public class RequestWrapper implements PortletRequest {
646:
647: public Object getAttribute(String arg0) {
648: return request.getAttribute(arg0);
649: }
650:
651: public Enumeration getAttributeNames() {
652: return request.getAttributeNames();
653: }
654:
655: public String getAuthType() {
656: return request.getAuthType();
657: }
658:
659: public String getContextPath() {
660: return request.getContextPath();
661: }
662:
663: public Locale getLocale() {
664: return request.getLocale();
665: }
666:
667: public Enumeration getLocales() {
668: return request.getLocales();
669: }
670:
671: public String getParameter(String arg0) {
672: return request.getParameter(arg0);
673: }
674:
675: public Map getParameterMap() {
676: return request.getParameterMap();
677: }
678:
679: public Enumeration getParameterNames() {
680: return request.getParameterNames();
681: }
682:
683: public String[] getParameterValues(String arg0) {
684: return request.getParameterValues(arg0);
685: }
686:
687: public PortalContext getPortalContext() {
688: throw new UnsupportedOperationException();
689: }
690:
691: public PortletMode getPortletMode() {
692: throw new UnsupportedOperationException();
693: }
694:
695: public PortletSession getPortletSession() {
696: throw new UnsupportedOperationException();
697: }
698:
699: public PortletSession getPortletSession(boolean arg0) {
700: throw new UnsupportedOperationException();
701: }
702:
703: public PortletPreferences getPreferences() {
704: throw new UnsupportedOperationException();
705: }
706:
707: public Enumeration getProperties(String arg0) {
708: throw new UnsupportedOperationException();
709: }
710:
711: public String getProperty(String arg0) {
712: throw new UnsupportedOperationException();
713: }
714:
715: public Enumeration getPropertyNames() {
716: throw new UnsupportedOperationException();
717: }
718:
719: public String getRemoteUser() {
720: return request.getRemoteUser();
721: }
722:
723: public String getRequestedSessionId() {
724: return request.getRequestedSessionId();
725: }
726:
727: public String getResponseContentType() {
728: throw new UnsupportedOperationException();
729: }
730:
731: public Enumeration getResponseContentTypes() {
732: throw new UnsupportedOperationException();
733: }
734:
735: public String getScheme() {
736: return request.getScheme();
737: }
738:
739: public String getServerName() {
740: return request.getServerName();
741: }
742:
743: public int getServerPort() {
744: return request.getServerPort();
745: }
746:
747: public Principal getUserPrincipal() {
748: return request.getUserPrincipal();
749: }
750:
751: public WindowState getWindowState() {
752: throw new UnsupportedOperationException();
753: }
754:
755: public boolean isPortletModeAllowed(PortletMode arg0) {
756: throw new UnsupportedOperationException();
757: }
758:
759: public boolean isRequestedSessionIdValid() {
760: return request.isRequestedSessionIdValid();
761: }
762:
763: public boolean isSecure() {
764: return request.isSecure();
765: }
766:
767: public boolean isUserInRole(String arg0) {
768: return request.isUserInRole(arg0);
769: }
770:
771: public boolean isWindowStateAllowed(WindowState arg0) {
772: throw new UnsupportedOperationException();
773: }
774:
775: public void removeAttribute(String arg0) {
776: request.removeAttribute(arg0);
777: }
778:
779: public void setAttribute(String arg0, Object arg1) {
780: request.setAttribute(arg0, arg1);
781: }
782:
783: }
784:
785: /* (non-Javadoc)
786: * @see org.apache.myfaces.context.ReleaseableExternalContext#release()
787: */
788: public void release() {
789: }
790: }
|