2
0

Page.jvm.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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 javax.servlet.http;
  29. using System.Collections.Specialized;
  30. using System.Globalization;
  31. using System.Web.Hosting;
  32. using System.Web.J2EE;
  33. using System.ComponentModel;
  34. using System.IO;
  35. using javax.faces.context;
  36. using javax.faces.render;
  37. using javax.servlet;
  38. using javax.faces;
  39. using javax.faces.application;
  40. using javax.faces.@event;
  41. using javax.faces.el;
  42. using javax.faces.component;
  43. using System.Threading;
  44. namespace System.Web.UI
  45. {
  46. public partial class Page
  47. {
  48. string _namespace = null;
  49. StateManager.SerializedView _facesSerializedView;
  50. MethodBinding _action;
  51. MethodBinding _actionListener;
  52. bool _immediate;
  53. Pair _state;
  54. bool [] _validatorsState;
  55. ICallbackEventHandler _callbackTarget;
  56. string _callbackEventError = String.Empty;
  57. IHttpHandler _jsfHandler;
  58. static readonly object CrossPagePostBack = new object ();
  59. bool _isMultiForm = false;
  60. bool _isMultiFormInited = false;
  61. internal string Namespace
  62. {
  63. get {
  64. if (_namespace == null) {
  65. if (getFacesContext () != null) {
  66. _namespace = getFacesContext ().getExternalContext ().encodeNamespace (String.Empty);
  67. }
  68. _namespace = _namespace ?? String.Empty;
  69. }
  70. return _namespace;
  71. }
  72. }
  73. internal string theForm {
  74. get {
  75. return "theForm" + Namespace;
  76. }
  77. }
  78. internal bool IsMultiForm {
  79. get {
  80. if (!_isMultiFormInited) {
  81. Mainsoft.Web.Configuration.PagesSection pageSection = (Mainsoft.Web.Configuration.PagesSection) System.Web.Configuration.WebConfigurationManager.GetSection ("mainsoft.web/pages");
  82. if (pageSection != null)
  83. _isMultiForm = pageSection.MultiForm;
  84. _isMultiFormInited = true;
  85. }
  86. return _isMultiForm;
  87. }
  88. }
  89. void EnterThread (HttpContext context) {
  90. _jsfHandler = context.CurrentHandler;
  91. context.PopHandler ();
  92. context.PushHandler (this);
  93. if (_jsfHandler == context.Handler)
  94. context.Handler = this;
  95. SetContext (context);
  96. }
  97. void ExitThread () {
  98. // TODO
  99. //if (context.getResponseComplete ())
  100. // Response.End ();
  101. _context.PopHandler ();
  102. _context.PushHandler (_jsfHandler);
  103. if (this == _context.Handler)
  104. _context.Handler = _jsfHandler;
  105. }
  106. public override void encodeBegin (FacesContext context) {
  107. // do nothing
  108. }
  109. public override void encodeChildren (FacesContext context) {
  110. System.Diagnostics.Trace.WriteLine ("encodeChildren");
  111. EnterThread (HttpContext.Current);
  112. try {
  113. if (!context.getResponseComplete ()) {
  114. if (IsCrossPagePostBack)
  115. return;
  116. if (IsCallback) {
  117. string result = ProcessGetCallbackResult (_callbackTarget, _callbackEventError);
  118. HtmlTextWriter callbackOutput = new HtmlTextWriter (Response.Output);
  119. callbackOutput.Write (result);
  120. callbackOutput.Flush ();
  121. return;
  122. }
  123. // ensure lifecycle complete.
  124. if (!IsLoaded) {
  125. ProcessLoad ();
  126. RestoreValidatorsState (_validatorsState);
  127. }
  128. if (!IsPrerendered)
  129. ProcessLoadComplete ();
  130. RenderPage ();
  131. }
  132. }
  133. catch (Exception ex) {
  134. if (!(ex is ThreadAbortException))
  135. ProcessException (ex);
  136. throw;
  137. }
  138. finally {
  139. ProcessUnload ();
  140. ExitThread ();
  141. }
  142. }
  143. public override void encodeEnd (FacesContext context) {
  144. // do nothing
  145. }
  146. public override UIComponent getParent () {
  147. return null;
  148. }
  149. public override void setParent (UIComponent parent) {
  150. //ignore: parent is root
  151. }
  152. // TODO: consider validators state
  153. public override object processSaveState (FacesContext context) {
  154. System.Diagnostics.Trace.WriteLine ("processSaveState");
  155. object state = new Pair (_state, GetValidatorsState ());
  156. if (getFacesContext ().getApplication ().getStateManager ().isSavingStateInClient (getFacesContext ())) {
  157. int length;
  158. byte [] buffer = new ObjectStateFormatter (this).SerializeInternal (state, out length);
  159. if (buffer.Length != length) {
  160. byte [] trimmedBuffer = new byte [length];
  161. Array.Copy (buffer, trimmedBuffer, length);
  162. buffer = trimmedBuffer;
  163. }
  164. state = vmw.common.TypeUtils.ToSByteArray (buffer);
  165. }
  166. return state;
  167. }
  168. public override void processRestoreState (FacesContext context, object state) {
  169. System.Diagnostics.Trace.WriteLine ("processRestoreState");
  170. if (state == null)
  171. throw new ArgumentNullException ("state");
  172. EnterThread (HttpContext.Current);
  173. try {
  174. if (getFacesContext ().getApplication ().getStateManager ().isSavingStateInClient (getFacesContext ())) {
  175. byte [] buffer = (byte []) vmw.common.TypeUtils.ToByteArray ((sbyte []) state);
  176. state = new ObjectStateFormatter (this).DeserializeInternal (buffer);
  177. }
  178. _state = (Pair) ((Pair) state).First;
  179. _validatorsState = (bool []) ((Pair) state).Second;
  180. RestorePageState ();
  181. }
  182. catch (Exception ex) {
  183. HandleException (ex);
  184. throw;
  185. }
  186. finally {
  187. ExitThread ();
  188. }
  189. }
  190. public override void processDecodes (FacesContext context) {
  191. System.Diagnostics.Trace.WriteLine ("processDecodes");
  192. EnterThread (HttpContext.Current);
  193. try {
  194. ProcessPostData ();
  195. EventRaiserFacesEvent facesEvent = new EventRaiserFacesEvent (this);
  196. facesEvent.setPhaseId (PhaseId.INVOKE_APPLICATION);
  197. context.getViewRoot ().queueEvent (facesEvent);
  198. base.processDecodes (context);
  199. }
  200. catch (Exception ex) {
  201. HandleException (ex);
  202. throw;
  203. }
  204. finally {
  205. ExitThread ();
  206. }
  207. }
  208. public override void processValidators (FacesContext context) {
  209. System.Diagnostics.Trace.WriteLine ("processValidators");
  210. EnterThread (HttpContext.Current);
  211. try {
  212. base.processValidators (context);
  213. }
  214. catch (Exception ex) {
  215. HandleException (ex);
  216. throw;
  217. }
  218. finally {
  219. ExitThread ();
  220. }
  221. }
  222. public override void processUpdates (FacesContext context) {
  223. System.Diagnostics.Trace.WriteLine ("processUpdates");
  224. EnterThread (HttpContext.Current);
  225. try {
  226. base.processUpdates (context);
  227. }
  228. catch (Exception ex) {
  229. HandleException (ex);
  230. throw;
  231. }
  232. finally {
  233. ExitThread ();
  234. }
  235. }
  236. public override void broadcast (FacesEvent e) {
  237. System.Diagnostics.Trace.WriteLine ("broadcast");
  238. if (!(e is EventRaiserFacesEvent))
  239. throw new NotSupportedException ("FacesEvent of class " + e.GetType ().Name + " not supported by Page");
  240. EnterThread (HttpContext.Current);
  241. try {
  242. ProcessRaiseEvents ();
  243. ProcessLoadComplete ();
  244. }
  245. catch (Exception ex) {
  246. HandleException (ex);
  247. throw;
  248. }
  249. finally {
  250. ExitThread ();
  251. }
  252. }
  253. void HandleException (Exception ex) {
  254. try {
  255. if (!(ex is ThreadAbortException))
  256. ProcessException (ex);
  257. }
  258. finally {
  259. ProcessUnload ();
  260. }
  261. }
  262. bool [] GetValidatorsState () {
  263. if (is_validated && Validators.Count > 0) {
  264. bool [] validatorsState = new bool [Validators.Count];
  265. bool isValid = true;
  266. for (int i = 0; i < Validators.Count; i++) {
  267. IValidator val = Validators [i];
  268. if (!val.IsValid)
  269. isValid = false;
  270. else
  271. validatorsState [i] = true;
  272. }
  273. return validatorsState;
  274. }
  275. return null;
  276. }
  277. void RestoreValidatorsState (bool [] validatorsState) {
  278. if (validatorsState == null)
  279. return;
  280. is_validated = true;
  281. for (int i = 0; i < Math.Min (validatorsState.Length, Validators.Count); i++) {
  282. IValidator val = Validators [i];
  283. val.IsValid = validatorsState [i];
  284. }
  285. }
  286. ResponseWriter SetupResponseWriter (TextWriter httpWriter) { //TODO
  287. FacesContext facesContext = getFacesContext ();
  288. ResponseWriter oldWriter = facesContext.getResponseWriter ();
  289. RenderKitFactory renderFactory = (RenderKitFactory) FactoryFinder.getFactory (FactoryFinder.RENDER_KIT_FACTORY);
  290. RenderKit renderKit = renderFactory.getRenderKit (facesContext,
  291. facesContext.getViewRoot ().getRenderKitId ());
  292. ServletResponse response = (ServletResponse) facesContext.getExternalContext ().getResponse ();
  293. ResponseWriter writer = renderKit.createResponseWriter (new AspNetResponseWriter (httpWriter),
  294. response.getContentType (), //TODO: is this the correct content type?
  295. response.getCharacterEncoding ());
  296. facesContext.setResponseWriter (writer);
  297. return oldWriter;
  298. }
  299. string DecodeNamespace (string id) {
  300. if (Namespace.Length > 0 && id.Length > Namespace.Length && id.StartsWith (Namespace, StringComparison.Ordinal))
  301. id = id.Substring (Namespace.Length);
  302. return id;
  303. }
  304. #region FacesPageStatePersister
  305. sealed class FacesPageStatePersister : PageStatePersister
  306. {
  307. public FacesPageStatePersister (Page page)
  308. : base (page) {
  309. }
  310. public override void Load () {
  311. if (Page._state != null) {
  312. ViewState = Page._state.First;
  313. ControlState = Page._state.Second;
  314. }
  315. }
  316. public override void Save () {
  317. if (ViewState != null || ControlState != null)
  318. Page._state = new Pair (ViewState, ControlState);
  319. }
  320. }
  321. #endregion
  322. #region EventRaiserFacesEvent
  323. sealed class EventRaiserFacesEvent : FacesEvent
  324. {
  325. public EventRaiserFacesEvent (Page page)
  326. : base (page) {
  327. }
  328. public override bool isAppropriateListener (FacesListener __p1) {
  329. throw new NotSupportedException ();
  330. }
  331. public override void processListener (FacesListener __p1) {
  332. throw new NotSupportedException ();
  333. }
  334. }
  335. #endregion
  336. #region AspNetResponseWriter
  337. private sealed class AspNetResponseWriter : java.io.Writer
  338. {
  339. readonly TextWriter _writer;
  340. public AspNetResponseWriter (TextWriter writer) {
  341. _writer = writer;
  342. }
  343. public override void close () {
  344. _writer.Close ();
  345. }
  346. public override void flush () {
  347. _writer.Flush ();
  348. }
  349. public override void write (char [] __p1, int __p2, int __p3) {
  350. _writer.Write (__p1, __p2, __p3);
  351. }
  352. public override void write (int __p1) {
  353. _writer.Write ((char) __p1);
  354. }
  355. public override void write (char [] __p1) {
  356. _writer.Write (__p1);
  357. }
  358. public override void write (string __p1) {
  359. _writer.Write (__p1);
  360. }
  361. public override void write (string __p1, int __p2, int __p3) {
  362. _writer.Write (__p1, __p2, __p3);
  363. }
  364. }
  365. #endregion
  366. }
  367. }