2 // System.Web.UI.Page.jvm.cs
5 // Eyal Alaluf (eyala@mainsoft.com)
7 // (C) 2006 Mainsoft Co. (http://www.mainsoft.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
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;
36 using javax.faces.context;
37 using javax.faces.render;
40 using javax.faces.application;
41 using javax.faces.@event;
43 using javax.faces.component;
44 using System.Threading;
45 using System.Web.Configuration;
46 using Mainsoft.Web.Hosting;
48 namespace System.Web.UI
50 public partial class Page
52 string _namespace = null;
53 StateManager.SerializedView _facesSerializedView;
54 MethodBinding _action;
55 MethodBinding _actionListener;
57 bool [] _validatorsState;
58 ICallbackEventHandler _callbackTarget;
59 string _callbackEventError = String.Empty;
60 static readonly object CrossPagePostBack = new object ();
61 FacesContext _facesContext;
62 const string RenderBodyContentOnlyKey = "mainsoft.render.body.content.only";
64 static readonly java.util.List emptyList = java.util.Collections.unmodifiableList (new java.util.ArrayList ());
66 bool _isMultiForm = false;
67 bool _isMultiFormInited = false;
69 internal string Namespace
72 if (_namespace == null) {
74 if (getFacesContext () != null) {
75 _namespace = getFacesContext ().getExternalContext ().encodeNamespace (String.Empty);
78 _namespace = _namespace ?? String.Empty;
84 internal Pair PageState { get; set; }
86 internal string theForm {
88 return "theForm" + Namespace;
92 internal bool IsMultiForm {
94 if (!_isMultiFormInited) {
95 string isMultiForm = WebConfigurationManager.AppSettings ["mainsoft.use.portlet.namespace"];
96 _isMultiForm = isMultiForm != null ? Boolean.Parse(isMultiForm) : false;
98 _isMultiFormInited = true;
104 IHttpHandler EnterThread () {
106 IHttpHandler jsfHandler = _context.CurrentHandler;
107 _context.PopHandler ();
108 _context.PushHandler (this);
109 if (jsfHandler == _context.Handler)
110 _context.Handler = this;
112 _context.CurrentHandlerInternal = this;
117 void ExitThread (IHttpHandler jsfHandler) {
119 //if (context.getResponseComplete ())
122 _context.PopHandler ();
123 _context.PushHandler (jsfHandler);
124 if (this == _context.Handler)
125 _context.Handler = jsfHandler;
129 public override void encodeBegin (FacesContext context) {
133 public override void encodeChildren (FacesContext context) {
134 System.Diagnostics.Trace.WriteLine ("encodeChildren");
136 // reset _facesContext if changed between action and render phases (such portal).
137 _facesContext = null;
139 IHttpHandler jsfHandler = EnterThread ();
140 bool wasException = false;
142 if (!context.getResponseComplete ()) {
145 string result = ProcessGetCallbackResult (_callbackTarget, _callbackEventError);
146 HtmlTextWriter callbackOutput = new HtmlTextWriter (Response.Output);
147 callbackOutput.Write (result);
148 callbackOutput.Flush ();
152 // ensure lifecycle complete.
155 RestoreValidatorsState (_validatorsState);
158 ProcessLoadComplete ();
163 catch (Exception ex) {
165 HandleException (ex);
173 ExitThread (jsfHandler);
178 public override void encodeEnd (FacesContext context) {
182 // BUGBUG: must return correct value. Currently returns 0 as performance optimization.
183 public override int getChildCount ()
188 // BUGBUG: must return correct value. Currently returns empty list as performance optimization.
189 public override java.util.List getChildren ()
194 public override UIComponent getParent () {
198 public override void setParent (UIComponent parent) {
199 //ignore: parent is root
202 // TODO: consider validators state
203 public override object processSaveState (FacesContext context) {
204 System.Diagnostics.Trace.WriteLine ("processSaveState");
206 object state = new Pair (PageState, GetValidatorsState ());
207 return new StateSerializer (state);
210 public override void processRestoreState (FacesContext context, object state) {
211 System.Diagnostics.Trace.WriteLine ("processRestoreState");
214 Console.WriteLine ("WARNING: processRestoreState was called with null state.");
215 return; //throw new ArgumentNullException ("state");
217 IHttpHandler jsfHandler = EnterThread ();
219 state = ((StateSerializer) state).State;
220 PageState = (Pair) ((Pair) state).First;
221 _validatorsState = (bool []) ((Pair) state).Second;
224 catch (Exception ex) {
225 HandleException (ex);
228 ExitThread (jsfHandler);
232 public override void processDecodes (FacesContext context) {
233 System.Diagnostics.Trace.WriteLine ("processDecodes");
235 IHttpHandler jsfHandler = EnterThread ();
239 EventRaiserFacesEvent facesEvent = new EventRaiserFacesEvent (this);
240 facesEvent.setPhaseId (PhaseId.INVOKE_APPLICATION);
241 context.getViewRoot ().queueEvent (facesEvent);
243 base.processDecodes (context);
245 catch (Exception ex) {
246 HandleException (ex);
249 ExitThread (jsfHandler);
253 public override void processValidators (FacesContext context) {
254 System.Diagnostics.Trace.WriteLine ("processValidators");
256 IHttpHandler jsfHandler = EnterThread ();
258 base.processValidators (context);
260 catch (Exception ex) {
261 HandleException (ex);
264 ExitThread (jsfHandler);
268 public override void processUpdates (FacesContext context) {
269 System.Diagnostics.Trace.WriteLine ("processUpdates");
271 IHttpHandler jsfHandler = EnterThread ();
273 base.processUpdates (context);
275 catch (Exception ex) {
276 HandleException (ex);
279 ExitThread (jsfHandler);
283 public override void broadcast (FacesEvent e) {
284 System.Diagnostics.Trace.WriteLine ("broadcast");
286 if (!(e is EventRaiserFacesEvent))
287 throw new NotSupportedException ("FacesEvent of class " + e.GetType ().Name + " not supported by Page");
289 IHttpHandler jsfHandler = EnterThread ();
290 bool doUnload = false;
292 ProcessRaiseEvents ();
293 doUnload = (ProcessLoadComplete () && IsCrossPagePostBack);
295 catch (Exception ex) {
297 HandleException (ex);
302 getFacesContext ().responseComplete ();
307 ExitThread (jsfHandler);
312 void HandleException (Exception ex) {
314 if (ex is ThreadAbortException) {
315 if (FlagEnd.Value == ((ThreadAbortException) ex).ExceptionState) {
316 Thread.ResetAbort ();
319 vmw.common.TypeUtils.Throw (ex);
322 ProcessException (ex);
325 if (getFacesContext () != null)
326 getFacesContext ().responseComplete ();
331 bool [] GetValidatorsState () {
332 if (is_validated && Validators.Count > 0) {
333 bool [] validatorsState = new bool [Validators.Count];
335 for (int i = 0; i < Validators.Count; i++) {
336 IValidator val = Validators [i];
340 validatorsState [i] = true;
342 return validatorsState;
347 void RestoreValidatorsState (bool [] validatorsState) {
348 if (validatorsState == null)
352 for (int i = 0; i < Math.Min (validatorsState.Length, Validators.Count); i++) {
353 IValidator val = Validators [i];
354 val.IsValid = validatorsState [i];
358 ResponseWriter SetupResponseWriter (TextWriter httpWriter) { //TODO
359 FacesContext facesContext = getFacesContext ();
361 ResponseWriter oldWriter = facesContext.getResponseWriter ();
362 if (oldWriter == null)
363 throw new InvalidOperationException ();
365 ResponseWriter writer = oldWriter.cloneWithWriter (new AspNetResponseWriter (httpWriter));
367 facesContext.setResponseWriter (writer);
371 string DecodeNamespace (string id) {
372 if (Namespace.Length > 0 && id.Length > Namespace.Length && id.StartsWith (Namespace, StringComparison.Ordinal))
373 id = id.Substring (Namespace.Length);
377 protected override FacesContext getFacesContext () {
378 return _facesContext ?? (_facesContext = FacesContext.getCurrentInstance ());
381 internal FacesContext FacesContext {
382 get { return getFacesContext (); }
385 #region EventRaiserFacesEvent
386 sealed class EventRaiserFacesEvent : FacesEvent
388 public EventRaiserFacesEvent (Page page)
392 public override bool isAppropriateListener (FacesListener __p1) {
393 throw new NotSupportedException ();
396 public override void processListener (FacesListener __p1) {
397 throw new NotSupportedException ();
402 #region AspNetResponseWriter
403 sealed class AspNetResponseWriter : java.io.Writer
405 readonly TextWriter _writer;
406 public AspNetResponseWriter (TextWriter writer) {
409 public override void close () {
413 public override void flush () {
417 public override void write (char [] __p1, int __p2, int __p3) {
418 _writer.Write (__p1, __p2, __p3);
421 public override void write (int __p1) {
422 _writer.Write ((char) __p1);
425 public override void write (char [] __p1) {
426 _writer.Write (__p1);
429 public override void write (string __p1) {
430 _writer.Write (__p1);
433 public override void write (string __p1, int __p2, int __p3) {
434 _writer.Write (__p1, __p2, __p3);
439 #region StateSerializer
440 public sealed class StateSerializer : java.io.Externalizable
444 public StateSerializer ()
448 public StateSerializer (object state)
455 get { return _state; }
458 public void readExternal (java.io.ObjectInput __p1)
460 Page page = CurrentPage;
461 ObjectStateFormatter osf = new ObjectStateFormatter (page);
462 ObjectInputStream inputStream = new ObjectInputStream (__p1);
464 if (page.NeedViewStateEncryption || page.EnableViewStateMac)
465 _state = osf.Deserialize ((string) inputStream.readObject ());
467 _state = osf.Deserialize (inputStream);
470 public void writeExternal (java.io.ObjectOutput __p1)
472 Page page = CurrentPage;
473 ObjectStateFormatter osf = new ObjectStateFormatter (page);
474 ObjectOutputStream outputStream = new ObjectOutputStream (__p1);
476 if (page.NeedViewStateEncryption || page.EnableViewStateMac)
477 outputStream.writeObject (osf.Serialize (_state));
479 osf.Serialize (outputStream, _state);
486 HttpContext context = HttpContext.Current;
487 if (context.CurrentHandler is Page)
488 return (Page) context.CurrentHandler;
490 return context.CurrentHandlerInternal;