added ServletFacesPageHandlerFactory
[mono.git] / mcs / class / Mainsoft.Web / Mainsoft.Web.Hosting / ServletFacesViewHandler.cs
1 using System;\r
2 using System.Collections.Generic;\r
3 using System.Text;\r
4 using javax.faces.application;\r
5 using javax.servlet.http;\r
6 using java.util;\r
7 using javax.faces.context;\r
8 using javax.faces.component;\r
9 using java.lang;\r
10 using javax.faces.render;\r
11 \r
12 namespace Mainsoft.Web.Hosting\r
13 {\r
14         public class ServletFacesViewHandler : ViewHandler\r
15         {\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
18 \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
31                                         }\r
32                                         else if (supportLocale.Equals (locale)) {\r
33                                                 return supportLocale;\r
34                                         }\r
35                                 }\r
36                         }\r
37 \r
38                         Locale defaultLocale = facesContext.getApplication ().getDefaultLocale ();\r
39                         return defaultLocale != null ? defaultLocale : Locale.getDefault ();\r
40                 }\r
41 \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
46                 }\r
47 \r
48                 /**\r
49                  */\r
50                 public override UIViewRoot createView (FacesContext facesContext, String viewId) {\r
51                         Application application = facesContext.getApplication ();\r
52                         ViewHandler applicationViewHandler = application.getViewHandler ();\r
53 \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
61                         }\r
62 \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
68 \r
69                         //      ok, but the RI does so, so let's do it, too.\r
70                         uiViewRoot.setViewId (viewId);\r
71 \r
72                         if (currentLocale != null) {\r
73                                 //set old locale\r
74                                 uiViewRoot.setLocale (currentLocale);\r
75                         }\r
76                         else {\r
77                                 //calculate locale\r
78                                 uiViewRoot.setLocale (applicationViewHandler.calculateLocale (facesContext));\r
79                         }\r
80 \r
81                         if (currentRenderKitId != null) {\r
82                                 //set old renderKit\r
83                                 uiViewRoot.setRenderKitId (currentRenderKitId);\r
84                         }\r
85                         else {\r
86                                 //calculate renderKit\r
87                                 uiViewRoot.setRenderKitId (applicationViewHandler.calculateRenderKitId (facesContext));\r
88                         }\r
89 \r
90                         return uiViewRoot;\r
91                 }\r
92 \r
93                 public override String getActionURL (FacesContext facesContext, String viewId) {\r
94 \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
100                         //}\r
101 \r
102                         String path = viewId;//                 getViewIdPath (facesContext, viewId);\r
103                         if (path.Length > 0 && path [0] == '/') {\r
104                                 return facesContext.getExternalContext ().getRequestContextPath () + path;\r
105                         }\r
106                         else {\r
107                                 return path;\r
108                         }\r
109                 }\r
110 \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
114                         }\r
115                         else {\r
116                                 return path;\r
117                         }\r
118                 }\r
119 \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
123                         }\r
124 \r
125                         AspNetFacesContext aspNetFacesContext = (AspNetFacesContext) facesContext;\r
126                         UIComponent page = aspNetFacesContext.Handler as UIComponent;\r
127                         if (page == null)\r
128                                 return;\r
129 \r
130                         if (viewToRender.getChildCount () == 0) {\r
131                                 // GET\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
136                         }\r
137                         else\r
138                                 page.encodeChildren (facesContext);\r
139                 }\r
140 \r
141 \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
147                                                                                                                                                         viewId,\r
148                                                                                                                                                         renderKitId);\r
149                         return viewRoot;\r
150                 }\r
151 \r
152                 /**\r
153                  * Writes a state marker that is replaced later by one or more hidden form\r
154                  * inputs.\r
155                  *\r
156                  * @param facesContext\r
157                  * @throws IOException\r
158                  */\r
159                 public override void writeState (FacesContext facesContext) {\r
160                         facesContext.getResponseWriter ().write (FORM_STATE_MARKER);\r
161                 }\r
162 \r
163         }\r
164 }\r