2 using System.Collections.Generic;
\r
4 using javax.faces.application;
\r
5 using javax.servlet.http;
\r
7 using javax.faces.context;
\r
8 using javax.faces.component;
\r
10 using javax.faces.render;
\r
12 namespace Mainsoft.Web.Hosting
\r
14 public class ServletFacesViewHandler : ViewHandler
\r
16 public static readonly String FORM_STATE_MARKER = "<!--@@JSF_FORM_STATE_MARKER@@-->";
\r
17 public static readonly int FORM_STATE_MARKER_LEN = FORM_STATE_MARKER.Length;
\r
19 public override Locale calculateLocale (FacesContext facesContext) {
\r
20 Iterator locales = facesContext.getExternalContext ().getRequestLocales ();
\r
21 while (locales.hasNext ()) {
\r
22 Locale locale = (Locale) locales.next ();
\r
23 for (Iterator it = facesContext.getApplication ().getSupportedLocales (); it.hasNext (); ) {
\r
24 Locale supportLocale = (Locale) it.next ();
\r
25 // higher priority to a language match over an exact match
\r
26 // that occures further down (see Jstl Reference 1.0 8.3.1)
\r
27 if (locale.getLanguage ().Equals (supportLocale.getLanguage ()) &&
\r
28 (supportLocale.getCountry () == null ||
\r
29 supportLocale.getCountry ().Length == 0)) {
\r
30 return supportLocale;
\r
32 else if (supportLocale.Equals (locale)) {
\r
33 return supportLocale;
\r
38 Locale defaultLocale = facesContext.getApplication ().getDefaultLocale ();
\r
39 return defaultLocale != null ? defaultLocale : Locale.getDefault ();
\r
42 public override String calculateRenderKitId (FacesContext facesContext) {
\r
43 String renderKitId = facesContext.getApplication ().getDefaultRenderKitId ();
\r
44 return (renderKitId != null) ? renderKitId : RenderKitFactory.HTML_BASIC_RENDER_KIT;
\r
45 //TODO: how to calculate from client?
\r
50 public override UIViewRoot createView (FacesContext facesContext, String viewId) {
\r
51 Application application = facesContext.getApplication ();
\r
52 ViewHandler applicationViewHandler = application.getViewHandler ();
\r
54 Locale currentLocale = null;
\r
55 String currentRenderKitId = null;
\r
56 UIViewRoot uiViewRoot = facesContext.getViewRoot ();
\r
57 if (uiViewRoot != null) {
\r
58 //Remember current locale and renderKitId
\r
59 currentLocale = uiViewRoot.getLocale ();
\r
60 currentRenderKitId = uiViewRoot.getRenderKitId ();
\r
63 uiViewRoot = (UIViewRoot) application.createComponent (UIViewRoot.COMPONENT_TYPE);
\r
64 // as of JSF spec page 7-16:
\r
65 // "It is the callers responsibility to ensure that setViewId() is called
\r
66 // on the returned view, passing the same viewId value."
\r
67 // so we do not set the viewId here
\r
69 // ok, but the RI does so, so let's do it, too.
\r
70 uiViewRoot.setViewId (viewId);
\r
72 if (currentLocale != null) {
\r
74 uiViewRoot.setLocale (currentLocale);
\r
78 uiViewRoot.setLocale (applicationViewHandler.calculateLocale (facesContext));
\r
81 if (currentRenderKitId != null) {
\r
83 uiViewRoot.setRenderKitId (currentRenderKitId);
\r
86 //calculate renderKit
\r
87 uiViewRoot.setRenderKitId (applicationViewHandler.calculateRenderKitId (facesContext));
\r
93 public override String getActionURL (FacesContext facesContext, String viewId) {
\r
95 //if (PortletUtil.isRenderResponse (facesContext)) {
\r
96 // RenderResponse response = (RenderResponse) facesContext.getExternalContext ().getResponse ();
\r
97 // PortletURL url = response.createActionURL ();
\r
98 // url.setParameter (MyFacesGenericPortlet.VIEW_ID, viewId);
\r
99 // return url.toString ();
\r
102 String path = viewId;// getViewIdPath (facesContext, viewId);
\r
103 if (path.Length > 0 && path [0] == '/') {
\r
104 return facesContext.getExternalContext ().getRequestContextPath () + path;
\r
111 public override String getResourceURL (FacesContext facesContext, String path) {
\r
112 if (path.Length > 0 && path [0] == '/') {
\r
113 return facesContext.getExternalContext ().getRequestContextPath () + path;
\r
120 public override void renderView (FacesContext facesContext, UIViewRoot viewToRender) {
\r
121 if (viewToRender == null) {
\r
122 throw new ArgumentNullException ("viewToRender", "viewToRender must not be null");
\r
125 AspNetFacesContext aspNetFacesContext = (AspNetFacesContext) facesContext;
\r
126 UIComponent page = aspNetFacesContext.Handler as UIComponent;
\r
130 if (viewToRender.getChildCount () == 0) {
\r
132 // ensure uiViewRoot contains the page
\r
133 viewToRender.getChildren ().add (0, page);
\r
134 // process GET request
\r
135 aspNetFacesContext.Handler.ProcessRequest (aspNetFacesContext.Context);
\r
138 page.encodeChildren (facesContext);
\r
142 public override UIViewRoot restoreView (FacesContext facesContext, String viewId) {
\r
143 Application application = facesContext.getApplication ();
\r
144 ViewHandler applicationViewHandler = application.getViewHandler ();
\r
145 String renderKitId = applicationViewHandler.calculateRenderKitId (facesContext);
\r
146 UIViewRoot viewRoot = application.getStateManager ().restoreView (facesContext,
\r
153 * Writes a state marker that is replaced later by one or more hidden form
\r
156 * @param facesContext
\r
157 * @throws IOException
\r
159 public override void writeState (FacesContext facesContext) {
\r
160 facesContext.getResponseWriter ().write (FORM_STATE_MARKER);
\r