Page.jvm.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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. IHttpHandler _jsfHandler;
  59. static readonly object CrossPagePostBack = new object ();
  60. bool _isMultiForm = false;
  61. bool _isMultiFormInited = false;
  62. internal string Namespace
  63. {
  64. get {
  65. if (_namespace == null) {
  66. if (getFacesContext () != null) {
  67. _namespace = getFacesContext ().getExternalContext ().encodeNamespace (String.Empty);
  68. }
  69. _namespace = _namespace ?? String.Empty;
  70. }
  71. return _namespace;
  72. }
  73. }
  74. internal string theForm {
  75. get {
  76. return "theForm" + Namespace;
  77. }
  78. }
  79. internal bool IsMultiForm {
  80. get {
  81. if (!_isMultiFormInited) {
  82. string isMultiForm = WebConfigurationManager.AppSettings ["mainsoft.use.portlet.namespace"];
  83. _isMultiForm = isMultiForm != null ? Boolean.Parse(isMultiForm) : false;
  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. bool wasException = false;
  113. try {
  114. if (!context.getResponseComplete ()) {
  115. if (IsCrossPagePostBack)
  116. return;
  117. if (IsCallback) {
  118. string result = ProcessGetCallbackResult (_callbackTarget, _callbackEventError);
  119. HtmlTextWriter callbackOutput = new HtmlTextWriter (Response.Output);
  120. callbackOutput.Write (result);
  121. callbackOutput.Flush ();
  122. return;
  123. }
  124. // ensure lifecycle complete.
  125. if (!IsLoaded) {
  126. ProcessLoad ();
  127. RestoreValidatorsState (_validatorsState);
  128. }
  129. if (!IsPrerendered)
  130. ProcessLoadComplete ();
  131. RenderPage ();
  132. }
  133. }
  134. catch (Exception ex) {
  135. wasException = true;
  136. HandleException (ex);
  137. }
  138. finally {
  139. if (!wasException)
  140. ProcessUnload ();
  141. ExitThread ();
  142. }
  143. }
  144. public override void encodeEnd (FacesContext context) {
  145. // do nothing
  146. }
  147. public override UIComponent getParent () {
  148. return null;
  149. }
  150. public override void setParent (UIComponent parent) {
  151. //ignore: parent is root
  152. }
  153. // TODO: consider validators state
  154. public override object processSaveState (FacesContext context) {
  155. System.Diagnostics.Trace.WriteLine ("processSaveState");
  156. object state = new Pair (_state, GetValidatorsState ());
  157. if (getFacesContext ().getApplication ().getStateManager ().isSavingStateInClient (getFacesContext ())) {
  158. int length;
  159. byte [] buffer = new ObjectStateFormatter (this).SerializeInternal (state, out length);
  160. if (buffer.Length != length) {
  161. byte [] trimmedBuffer = new byte [length];
  162. Array.Copy (buffer, trimmedBuffer, length);
  163. buffer = trimmedBuffer;
  164. }
  165. state = vmw.common.TypeUtils.ToSByteArray (buffer);
  166. }
  167. return state;
  168. }
  169. public override void processRestoreState (FacesContext context, object state) {
  170. System.Diagnostics.Trace.WriteLine ("processRestoreState");
  171. if (state == null)
  172. throw new ArgumentNullException ("state");
  173. EnterThread (HttpContext.Current);
  174. try {
  175. if (getFacesContext ().getApplication ().getStateManager ().isSavingStateInClient (getFacesContext ())) {
  176. byte [] buffer = (byte []) vmw.common.TypeUtils.ToByteArray ((sbyte []) state);
  177. state = new ObjectStateFormatter (this).DeserializeInternal (buffer);
  178. }
  179. _state = (Pair) ((Pair) state).First;
  180. _validatorsState = (bool []) ((Pair) state).Second;
  181. RestorePageState ();
  182. }
  183. catch (Exception ex) {
  184. HandleException (ex);
  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. }
  203. finally {
  204. ExitThread ();
  205. }
  206. }
  207. public override void processValidators (FacesContext context) {
  208. System.Diagnostics.Trace.WriteLine ("processValidators");
  209. EnterThread (HttpContext.Current);
  210. try {
  211. base.processValidators (context);
  212. }
  213. catch (Exception ex) {
  214. HandleException (ex);
  215. }
  216. finally {
  217. ExitThread ();
  218. }
  219. }
  220. public override void processUpdates (FacesContext context) {
  221. System.Diagnostics.Trace.WriteLine ("processUpdates");
  222. EnterThread (HttpContext.Current);
  223. try {
  224. base.processUpdates (context);
  225. }
  226. catch (Exception ex) {
  227. HandleException (ex);
  228. }
  229. finally {
  230. ExitThread ();
  231. }
  232. }
  233. public override void broadcast (FacesEvent e) {
  234. System.Diagnostics.Trace.WriteLine ("broadcast");
  235. if (!(e is EventRaiserFacesEvent))
  236. throw new NotSupportedException ("FacesEvent of class " + e.GetType ().Name + " not supported by Page");
  237. EnterThread (HttpContext.Current);
  238. try {
  239. ProcessRaiseEvents ();
  240. ProcessLoadComplete ();
  241. }
  242. catch (Exception ex) {
  243. HandleException (ex);
  244. }
  245. finally {
  246. ExitThread ();
  247. }
  248. }
  249. void HandleException (Exception ex) {
  250. try {
  251. if (ex is ThreadAbortException) {
  252. if (_context.Response.FlagEnd == ((ThreadAbortException) ex).ExceptionState) {
  253. Thread.ResetAbort ();
  254. return;
  255. }
  256. vmw.common.TypeUtils.Throw (ex);
  257. }
  258. else
  259. ProcessException (ex);
  260. }
  261. finally {
  262. ProcessUnload ();
  263. }
  264. }
  265. bool [] GetValidatorsState () {
  266. if (is_validated && Validators.Count > 0) {
  267. bool [] validatorsState = new bool [Validators.Count];
  268. bool isValid = true;
  269. for (int i = 0; i < Validators.Count; i++) {
  270. IValidator val = Validators [i];
  271. if (!val.IsValid)
  272. isValid = false;
  273. else
  274. validatorsState [i] = true;
  275. }
  276. return validatorsState;
  277. }
  278. return null;
  279. }
  280. void RestoreValidatorsState (bool [] validatorsState) {
  281. if (validatorsState == null)
  282. return;
  283. is_validated = true;
  284. for (int i = 0; i < Math.Min (validatorsState.Length, Validators.Count); i++) {
  285. IValidator val = Validators [i];
  286. val.IsValid = validatorsState [i];
  287. }
  288. }
  289. ResponseWriter SetupResponseWriter (TextWriter httpWriter) { //TODO
  290. FacesContext facesContext = getFacesContext ();
  291. ResponseWriter oldWriter = facesContext.getResponseWriter ();
  292. RenderKitFactory renderFactory = (RenderKitFactory) FactoryFinder.getFactory (FactoryFinder.RENDER_KIT_FACTORY);
  293. RenderKit renderKit = renderFactory.getRenderKit (facesContext,
  294. facesContext.getViewRoot ().getRenderKitId ());
  295. ServletResponse response = (ServletResponse) facesContext.getExternalContext ().getResponse ();
  296. ResponseWriter writer = renderKit.createResponseWriter (new AspNetResponseWriter (httpWriter),
  297. response.getContentType (), //TODO: is this the correct content type?
  298. response.getCharacterEncoding ());
  299. facesContext.setResponseWriter (writer);
  300. return oldWriter;
  301. }
  302. string DecodeNamespace (string id) {
  303. if (Namespace.Length > 0 && id.Length > Namespace.Length && id.StartsWith (Namespace, StringComparison.Ordinal))
  304. id = id.Substring (Namespace.Length);
  305. return id;
  306. }
  307. #region FacesPageStatePersister
  308. sealed class FacesPageStatePersister : PageStatePersister
  309. {
  310. public FacesPageStatePersister (Page page)
  311. : base (page) {
  312. }
  313. public override void Load () {
  314. if (Page._state != null) {
  315. ViewState = Page._state.First;
  316. ControlState = Page._state.Second;
  317. }
  318. }
  319. public override void Save () {
  320. if (ViewState != null || ControlState != null)
  321. Page._state = new Pair (ViewState, ControlState);
  322. }
  323. }
  324. #endregion
  325. #region EventRaiserFacesEvent
  326. sealed class EventRaiserFacesEvent : FacesEvent
  327. {
  328. public EventRaiserFacesEvent (Page page)
  329. : base (page) {
  330. }
  331. public override bool isAppropriateListener (FacesListener __p1) {
  332. throw new NotSupportedException ();
  333. }
  334. public override void processListener (FacesListener __p1) {
  335. throw new NotSupportedException ();
  336. }
  337. }
  338. #endregion
  339. #region AspNetResponseWriter
  340. private sealed class AspNetResponseWriter : java.io.Writer
  341. {
  342. readonly TextWriter _writer;
  343. public AspNetResponseWriter (TextWriter writer) {
  344. _writer = writer;
  345. }
  346. public override void close () {
  347. _writer.Close ();
  348. }
  349. public override void flush () {
  350. _writer.Flush ();
  351. }
  352. public override void write (char [] __p1, int __p2, int __p3) {
  353. _writer.Write (__p1, __p2, __p3);
  354. }
  355. public override void write (int __p1) {
  356. _writer.Write ((char) __p1);
  357. }
  358. public override void write (char [] __p1) {
  359. _writer.Write (__p1);
  360. }
  361. public override void write (string __p1) {
  362. _writer.Write (__p1);
  363. }
  364. public override void write (string __p1, int __p2, int __p3) {
  365. _writer.Write (__p1, __p2, __p3);
  366. }
  367. }
  368. #endregion
  369. }
  370. }