Page.jvm.cs 12 KB

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