Page.jvm.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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. 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. IHttpHandler EnterThread () {
  90. IHttpHandler jsfHandler = _context.CurrentHandler;
  91. _context.PopHandler ();
  92. _context.PushHandler (this);
  93. if (jsfHandler == _context.Handler)
  94. _context.Handler = this;
  95. return jsfHandler;
  96. }
  97. void ExitThread (IHttpHandler jsfHandler) {
  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. // reset _facesContext if changed between action and render phases (such portal).
  112. _facesContext = null;
  113. IHttpHandler jsfHandler = EnterThread ();
  114. bool wasException = false;
  115. try {
  116. if (!context.getResponseComplete ()) {
  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. try {
  140. if (!wasException)
  141. ProcessUnload ();
  142. }
  143. finally {
  144. ExitThread (jsfHandler);
  145. }
  146. }
  147. }
  148. public override void encodeEnd (FacesContext context) {
  149. // do nothing
  150. }
  151. public override UIComponent getParent () {
  152. return null;
  153. }
  154. public override void setParent (UIComponent parent) {
  155. //ignore: parent is root
  156. }
  157. // TODO: consider validators state
  158. public override object processSaveState (FacesContext context) {
  159. System.Diagnostics.Trace.WriteLine ("processSaveState");
  160. object state = new Pair (_state, GetValidatorsState ());
  161. if (getFacesContext ().getApplication ().getStateManager ().isSavingStateInClient (getFacesContext ())) {
  162. int length;
  163. byte [] buffer = new ObjectStateFormatter (this).SerializeInternal (state, out length);
  164. if (buffer.Length != length) {
  165. byte [] trimmedBuffer = new byte [length];
  166. Array.Copy (buffer, trimmedBuffer, length);
  167. buffer = trimmedBuffer;
  168. }
  169. state = vmw.common.TypeUtils.ToSByteArray (buffer);
  170. }
  171. return state;
  172. }
  173. public override void processRestoreState (FacesContext context, object state) {
  174. System.Diagnostics.Trace.WriteLine ("processRestoreState");
  175. if (state == null)
  176. throw new ArgumentNullException ("state");
  177. IHttpHandler jsfHandler = EnterThread ();
  178. try {
  179. if (getFacesContext ().getApplication ().getStateManager ().isSavingStateInClient (getFacesContext ())) {
  180. byte [] buffer = (byte []) vmw.common.TypeUtils.ToByteArray ((sbyte []) state);
  181. state = new ObjectStateFormatter (this).DeserializeInternal (buffer);
  182. }
  183. _state = (Pair) ((Pair) state).First;
  184. _validatorsState = (bool []) ((Pair) state).Second;
  185. RestorePageState ();
  186. }
  187. catch (Exception ex) {
  188. HandleException (ex);
  189. }
  190. finally {
  191. ExitThread (jsfHandler);
  192. }
  193. }
  194. public override void processDecodes (FacesContext context) {
  195. System.Diagnostics.Trace.WriteLine ("processDecodes");
  196. IHttpHandler jsfHandler = EnterThread ();
  197. try {
  198. ProcessPostData ();
  199. EventRaiserFacesEvent facesEvent = new EventRaiserFacesEvent (this);
  200. facesEvent.setPhaseId (PhaseId.INVOKE_APPLICATION);
  201. context.getViewRoot ().queueEvent (facesEvent);
  202. base.processDecodes (context);
  203. }
  204. catch (Exception ex) {
  205. HandleException (ex);
  206. }
  207. finally {
  208. ExitThread (jsfHandler);
  209. }
  210. }
  211. public override void processValidators (FacesContext context) {
  212. System.Diagnostics.Trace.WriteLine ("processValidators");
  213. IHttpHandler jsfHandler = EnterThread ();
  214. try {
  215. base.processValidators (context);
  216. }
  217. catch (Exception ex) {
  218. HandleException (ex);
  219. }
  220. finally {
  221. ExitThread (jsfHandler);
  222. }
  223. }
  224. public override void processUpdates (FacesContext context) {
  225. System.Diagnostics.Trace.WriteLine ("processUpdates");
  226. IHttpHandler jsfHandler = EnterThread ();
  227. try {
  228. base.processUpdates (context);
  229. }
  230. catch (Exception ex) {
  231. HandleException (ex);
  232. }
  233. finally {
  234. ExitThread (jsfHandler);
  235. }
  236. }
  237. public override void broadcast (FacesEvent e) {
  238. System.Diagnostics.Trace.WriteLine ("broadcast");
  239. if (!(e is EventRaiserFacesEvent))
  240. throw new NotSupportedException ("FacesEvent of class " + e.GetType ().Name + " not supported by Page");
  241. IHttpHandler jsfHandler = EnterThread ();
  242. bool doUnload = false;
  243. try {
  244. ProcessRaiseEvents ();
  245. doUnload = (ProcessLoadComplete () && IsCrossPagePostBack);
  246. }
  247. catch (Exception ex) {
  248. doUnload = false;
  249. HandleException (ex);
  250. }
  251. finally {
  252. try {
  253. if (doUnload) {
  254. getFacesContext ().responseComplete ();
  255. ProcessUnload ();
  256. }
  257. }
  258. finally {
  259. ExitThread (jsfHandler);
  260. }
  261. }
  262. }
  263. void HandleException (Exception ex) {
  264. try {
  265. if (ex is ThreadAbortException) {
  266. if (_context.Response.FlagEnd == ((ThreadAbortException) ex).ExceptionState) {
  267. Thread.ResetAbort ();
  268. return;
  269. }
  270. vmw.common.TypeUtils.Throw (ex);
  271. }
  272. else
  273. ProcessException (ex);
  274. }
  275. finally {
  276. if (getFacesContext () != null)
  277. getFacesContext ().responseComplete ();
  278. ProcessUnload ();
  279. }
  280. }
  281. bool [] GetValidatorsState () {
  282. if (is_validated && Validators.Count > 0) {
  283. bool [] validatorsState = new bool [Validators.Count];
  284. bool isValid = true;
  285. for (int i = 0; i < Validators.Count; i++) {
  286. IValidator val = Validators [i];
  287. if (!val.IsValid)
  288. isValid = false;
  289. else
  290. validatorsState [i] = true;
  291. }
  292. return validatorsState;
  293. }
  294. return null;
  295. }
  296. void RestoreValidatorsState (bool [] validatorsState) {
  297. if (validatorsState == null)
  298. return;
  299. is_validated = true;
  300. for (int i = 0; i < Math.Min (validatorsState.Length, Validators.Count); i++) {
  301. IValidator val = Validators [i];
  302. val.IsValid = validatorsState [i];
  303. }
  304. }
  305. ResponseWriter SetupResponseWriter (TextWriter httpWriter) { //TODO
  306. FacesContext facesContext = getFacesContext ();
  307. ResponseWriter oldWriter = facesContext.getResponseWriter ();
  308. RenderKitFactory renderFactory = (RenderKitFactory) FactoryFinder.getFactory (FactoryFinder.RENDER_KIT_FACTORY);
  309. RenderKit renderKit = renderFactory.getRenderKit (facesContext,
  310. facesContext.getViewRoot ().getRenderKitId ());
  311. ServletResponse response = (ServletResponse) facesContext.getExternalContext ().getResponse ();
  312. ResponseWriter writer = renderKit.createResponseWriter (new AspNetResponseWriter (httpWriter),
  313. response.getContentType (), //TODO: is this the correct content type?
  314. response.getCharacterEncoding ());
  315. facesContext.setResponseWriter (writer);
  316. return oldWriter;
  317. }
  318. string DecodeNamespace (string id) {
  319. if (Namespace.Length > 0 && id.Length > Namespace.Length && id.StartsWith (Namespace, StringComparison.Ordinal))
  320. id = id.Substring (Namespace.Length);
  321. return id;
  322. }
  323. protected override FacesContext getFacesContext () {
  324. return _facesContext ?? (_facesContext = FacesContext.getCurrentInstance ());
  325. }
  326. #region FacesPageStatePersister
  327. sealed class FacesPageStatePersister : PageStatePersister
  328. {
  329. public FacesPageStatePersister (Page page)
  330. : base (page) {
  331. }
  332. public override void Load () {
  333. if (Page._state != null) {
  334. ViewState = Page._state.First;
  335. ControlState = Page._state.Second;
  336. }
  337. }
  338. public override void Save () {
  339. if (ViewState != null || ControlState != null)
  340. Page._state = new Pair (ViewState, ControlState);
  341. }
  342. }
  343. #endregion
  344. #region EventRaiserFacesEvent
  345. sealed class EventRaiserFacesEvent : FacesEvent
  346. {
  347. public EventRaiserFacesEvent (Page page)
  348. : base (page) {
  349. }
  350. public override bool isAppropriateListener (FacesListener __p1) {
  351. throw new NotSupportedException ();
  352. }
  353. public override void processListener (FacesListener __p1) {
  354. throw new NotSupportedException ();
  355. }
  356. }
  357. #endregion
  358. #region AspNetResponseWriter
  359. private sealed class AspNetResponseWriter : java.io.Writer
  360. {
  361. readonly TextWriter _writer;
  362. public AspNetResponseWriter (TextWriter writer) {
  363. _writer = writer;
  364. }
  365. public override void close () {
  366. _writer.Close ();
  367. }
  368. public override void flush () {
  369. _writer.Flush ();
  370. }
  371. public override void write (char [] __p1, int __p2, int __p3) {
  372. _writer.Write (__p1, __p2, __p3);
  373. }
  374. public override void write (int __p1) {
  375. _writer.Write ((char) __p1);
  376. }
  377. public override void write (char [] __p1) {
  378. _writer.Write (__p1);
  379. }
  380. public override void write (string __p1) {
  381. _writer.Write (__p1);
  382. }
  383. public override void write (string __p1, int __p2, int __p3) {
  384. _writer.Write (__p1, __p2, __p3);
  385. }
  386. }
  387. #endregion
  388. }
  389. }