Page.jvm.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. //
  2. // System.Web.UI.Page.jvm.cs
  3. //
  4. // Authors:
  5. // Eyal Alaluf ([email protected])
  6. //
  7. // (C) 2006 Mainsoft Co. (http://www.mainsoft.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using [email protected];
  29. using javax.servlet.http;
  30. using System.Collections.Specialized;
  31. using System.Globalization;
  32. using System.Web.Hosting;
  33. using System.Web.J2EE;
  34. using System.ComponentModel;
  35. using System.IO;
  36. using javax.faces.context;
  37. using javax.faces.render;
  38. using javax.servlet;
  39. using javax.faces;
  40. using javax.faces.application;
  41. namespace System.Web.UI
  42. {
  43. public partial class Page
  44. {
  45. const string PageNamespaceKey = "__PAGENAMESPACE";
  46. const string RenderPageMark = "vmw.render.page=";
  47. const string ActionPageMark = "vmw.action.page=";
  48. static readonly string NextActionPageKey = PortletInternalUtils.NextActionPage;
  49. static readonly string NextRenderPageKey = PortletInternalUtils.NextRenderPage;
  50. bool _emptyPortletNamespace = false;
  51. string _PortletNamespace = null;
  52. bool _renderResponseInit = false;
  53. IPortletRenderResponse _renderResponse = null;
  54. StateManager.SerializedView _facesSerializedView;
  55. internal string PortletNamespace
  56. {
  57. get {
  58. return Context.PortletNamespace;
  59. }
  60. }
  61. internal string theForm {
  62. get {
  63. return "theForm" + PortletNamespace;
  64. }
  65. }
  66. bool _isMultiForm = false;
  67. bool _isMultiFormInited = false;
  68. internal bool IsMultiForm {
  69. get {
  70. if (!_isMultiFormInited) {
  71. Mainsoft.Web.Configuration.PagesSection pageSection = (Mainsoft.Web.Configuration.PagesSection) System.Web.Configuration.WebConfigurationManager.GetSection ("mainsoft.web/pages");
  72. if (pageSection != null)
  73. _isMultiForm = pageSection.MultiForm;
  74. _isMultiFormInited = true;
  75. }
  76. return _isMultiForm;
  77. }
  78. }
  79. internal bool IsPortletRender
  80. {
  81. get {
  82. return RenderResponse != null;
  83. }
  84. }
  85. internal bool IsGetBack {
  86. get {
  87. return IsPostBack && Context.IsPortletRequest && !Context.IsActionRequest;
  88. }
  89. }
  90. internal IPortletRenderResponse RenderResponse
  91. {
  92. get {
  93. if (!_renderResponseInit)
  94. if (Context != null) {
  95. _renderResponse = Context.ServletResponse as IPortletRenderResponse;
  96. _renderResponseInit = true;
  97. }
  98. return _renderResponse;
  99. }
  100. }
  101. public string CreateRenderUrl (string url)
  102. {
  103. FacesContext faces = getFacesContext ();
  104. return faces != null ? faces.getExternalContext ().encodeResourceURL (url) : url;
  105. }
  106. public string CreateActionUrl (string url)
  107. {
  108. FacesContext faces = getFacesContext ();
  109. if (faces == null)
  110. return url;
  111. //kostat: handle QueryString!
  112. Application application = faces.getApplication ();
  113. ViewHandler viewHandler = application.getViewHandler ();
  114. String viewId = faces.getViewRoot ().getViewId ();
  115. return viewHandler.getActionURL (faces, viewId);
  116. }
  117. private string RemoveAppPathIfInternal (string url)
  118. {
  119. Uri reqUrl = Request.Url;
  120. string appPath = Request.ApplicationPath;
  121. string currPage = Request.CurrentExecutionFilePath;
  122. if (currPage.StartsWith (appPath, StringComparison.InvariantCultureIgnoreCase))
  123. currPage = currPage.Substring (appPath.Length);
  124. return PortletInternalUtils.mapPathIfInternal (url, reqUrl.Host, reqUrl.Port, reqUrl.Scheme, appPath, currPage);
  125. }
  126. internal bool OnSaveStateCompleteForPortlet ()
  127. {
  128. if (PortletNamespace != null) {
  129. ClientScript.RegisterHiddenField (PageNamespaceKey, PortletNamespace);
  130. ClientScript.RegisterHiddenField (NextActionPageKey, "");
  131. ClientScript.RegisterHiddenField (NextRenderPageKey, "");
  132. }
  133. IPortletActionResponse resp = Context.ServletResponse as IPortletActionResponse;
  134. IPortletActionRequest req = Context.ServletRequest as IPortletActionRequest;
  135. if (req == null)
  136. return false;
  137. // When redirecting don't save the page viewstate and hidden fields
  138. if (resp.isRedirected ())
  139. return true;
  140. if (IsPostBack && 0 == String.Compare (Request.HttpMethod, "POST", true, CultureInfo.InvariantCulture)) {
  141. resp.setRenderParameter ("__VIEWSTATE", GetSavedViewState ());
  142. if (ClientScript.hiddenFields != null)
  143. foreach (string key in ClientScript.hiddenFields.Keys)
  144. resp.setRenderParameter (key, (string) ClientScript.hiddenFields [key]);
  145. if (is_validated && Validators.Count > 0) {
  146. string validatorsState = GetValidatorsState ();
  147. #if DEBUG
  148. Console.WriteLine ("__VALIDATORSSTATE: " + validatorsState);
  149. #endif
  150. if (!String.IsNullOrEmpty (validatorsState))
  151. resp.setRenderParameter ("__VALIDATORSSTATE", validatorsState);
  152. }
  153. }
  154. // Stop processing only if we are handling processAction. If we
  155. // are handling a postback from render then fall through.
  156. return req.processActionOnly ();
  157. }
  158. string GetValidatorsState () {
  159. bool [] validatorsState = new bool [Validators.Count];
  160. bool isValid = true;
  161. for (int i = 0; i < Validators.Count; i++) {
  162. IValidator val = Validators [i];
  163. if (!val.IsValid)
  164. isValid = false;
  165. else
  166. validatorsState [i] = true;
  167. }
  168. if (isValid)
  169. return null;
  170. return GetFormatter ().Serialize (validatorsState);
  171. }
  172. void RestoreValidatorsState () {
  173. string validatorsStateSerialized = Request.Form ["__VALIDATORSSTATE"];
  174. if (String.IsNullOrEmpty (validatorsStateSerialized))
  175. return;
  176. is_validated = true;
  177. bool [] validatorsState = (bool []) GetFormatter ().Deserialize (validatorsStateSerialized);
  178. for (int i = 0; i < Math.Min (validatorsState.Length, Validators.Count); i++) {
  179. IValidator val = Validators [i];
  180. val.IsValid = validatorsState [i];
  181. }
  182. }
  183. void SetupResponseWriter (TextWriter httpWriter) {
  184. FacesContext facesContext = getFacesContext ();
  185. if (facesContext == null)
  186. return;
  187. ResponseWriter writer = facesContext.getResponseWriter ();
  188. if (writer == null) {
  189. RenderKitFactory renderFactory = (RenderKitFactory) FactoryFinder.getFactory (FactoryFinder.RENDER_KIT_FACTORY);
  190. RenderKit renderKit = renderFactory.getRenderKit (facesContext,
  191. facesContext.getViewRoot ().getRenderKitId ());
  192. ServletResponse response = (ServletResponse) facesContext.getExternalContext ().getResponse ();
  193. writer = renderKit.createResponseWriter (new AspNetResponseWriter (httpWriter),
  194. response.getContentType (), //TODO: is this the correct content type?
  195. response.getCharacterEncoding ());
  196. facesContext.setResponseWriter (writer);
  197. }
  198. }
  199. internal string EncodeURL (string raw) {
  200. //kostat: BUGBUG: complete
  201. return raw;
  202. }
  203. }
  204. }