2007-12-13 Marek Habersack <mhabersack@novell.com>
[mono.git] / mcs / class / System.Web / System.Web.UI / Page.jvm.cs
1 //
2 // System.Web.UI.Page.jvm.cs
3 //
4 // Authors:
5 //   Eyal Alaluf (eyala@mainsoft.com)
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
29 using vmw.@internal.j2ee;
30 using javax.servlet.http;
31 using System.Collections.Specialized;
32 using System.Globalization;
33 using System.Web.Hosting;
34 using System.Web.J2EE;
35 using System.ComponentModel;
36
37 namespace System.Web.UI
38 {
39         public partial class Page
40         {
41                 const string PageNamespaceKey = "__PAGENAMESPACE";
42                 const string RenderPageMark = "vmw.render.page=";
43                 const string ActionPageMark = "vmw.action.page=";
44                 static readonly string NextActionPageKey = PortletInternalUtils.NextActionPage;
45                 static readonly string NextRenderPageKey = PortletInternalUtils.NextRenderPage;
46
47                 bool _emptyPortletNamespace = false;
48                 string _PortletNamespace = null;
49                 bool _renderResponseInit = false;
50                 IPortletRenderResponse _renderResponse = null;
51
52                 internal string PortletNamespace
53                 {
54                         get {
55                                 if (_emptyPortletNamespace)
56                                         return null;
57
58                                 if (_PortletNamespace == null) {
59                                         IPortletResponse portletResponse = null;
60                                         if (Context != null) {
61                                                 string usePortletNamespace = J2EEUtils.GetInitParameterByHierarchy (Context.Servlet.getServletConfig (), "mainsoft.use.portlet.namespace");
62                                                 if (usePortletNamespace == null || Boolean.Parse(usePortletNamespace))
63                                                         portletResponse = Context.ServletResponse as IPortletResponse;
64                                         }
65                                         if (portletResponse != null)
66                                                 _PortletNamespace = portletResponse.getNamespace ();
67                                         else if (_requestValueCollection != null && _requestValueCollection [PageNamespaceKey] != null)
68                                                 _PortletNamespace = _requestValueCollection [PageNamespaceKey];
69                                                 
70                                         _emptyPortletNamespace = _PortletNamespace == null;
71                                 }
72                                 return _PortletNamespace;
73                         }
74                 }
75
76                 internal string theForm {
77                         get {
78                                 return "theForm" + PortletNamespace;
79                         }
80                 }
81                 
82                 bool _isMultiForm = false;
83                 bool _isMultiFormInited = false;
84
85                 internal bool IsMultiForm {
86                         get {
87                                 if (!_isMultiFormInited) {
88                                         Mainsoft.Web.Configuration.PagesSection pageSection = (Mainsoft.Web.Configuration.PagesSection) System.Web.Configuration.WebConfigurationManager.GetSection ("mainsoft.web/pages");
89                                         if (pageSection != null)
90                                                 _isMultiForm = pageSection.MultiForm;
91
92                                         _isMultiFormInited = true;
93                                 }
94                                 return _isMultiForm;
95                         }
96                 }
97
98                 internal bool IsPortletRender
99                 {
100                         get {
101                                 return RenderResponse != null;
102                         }
103                 }
104
105                 internal bool IsGetBack {
106                         get {
107                                 return IsPostBack && IsPortletRender &&
108                                         (0 == String.Compare (Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase));
109                         }
110                 }
111
112                 internal IPortletRenderResponse RenderResponse
113                 {
114                         get {
115                                 if (!_renderResponseInit)
116                                         if (Context != null) {
117                                                 _renderResponse = Context.ServletResponse as IPortletRenderResponse;
118                                                 _renderResponseInit = true;
119                                         }
120                                 return _renderResponse;
121                         }
122                 }
123
124                 public string CreateRenderUrl (string url)
125                 {
126                         if (RenderResponse != null)
127                                 return RenderResponse.createRenderURL (url);
128                         if (PortletNamespace == null)
129                                 return url;
130
131                         string internalUrl = RemoveAppPathIfInternal (url);
132                         if (internalUrl == null)
133                                 return url;
134
135                         PostBackOptions options = new PostBackOptions (this);
136                         options.ActionUrl = RenderPageMark + internalUrl;
137                         options.RequiresJavaScriptProtocol = true;
138                         return ClientScript.GetPostBackEventReference (options);
139                 }
140
141                 public string CreateActionUrl (string url)
142                 {
143                         if (url.StartsWith (RenderPageMark, StringComparison.Ordinal) || url.StartsWith (ActionPageMark, StringComparison.Ordinal))
144                                 return url;
145
146                         if (RenderResponse != null)
147                                 return RenderResponse.createActionURL (url);
148                         if (PortletNamespace == null)
149                                 return url;
150
151                         Uri requestUrl = Request.Url;
152                         string internalUrl = RemoveAppPathIfInternal (url);
153                         if (internalUrl == null)
154                                 return url;
155
156                         return ActionPageMark + internalUrl;
157                 }
158
159                 private string RemoveAppPathIfInternal (string url)
160                 {
161                         Uri reqUrl = Request.Url;
162                         string appPath = Request.ApplicationPath;
163                         string currPage = Request.CurrentExecutionFilePath;
164                         if (currPage.StartsWith (appPath, StringComparison.InvariantCultureIgnoreCase))
165                                 currPage = currPage.Substring (appPath.Length);
166                         return PortletInternalUtils.mapPathIfInternal (url, reqUrl.Host, reqUrl.Port, reqUrl.Scheme, appPath, currPage);
167                 }
168
169                 internal bool OnSaveStateCompleteForPortlet ()
170                 {
171                         if (PortletNamespace != null) {
172                                 ClientScript.RegisterHiddenField (PageNamespaceKey, PortletNamespace);
173                                 ClientScript.RegisterHiddenField (NextActionPageKey, "");
174                                 ClientScript.RegisterHiddenField (NextRenderPageKey, "");
175                         }
176
177                         IPortletActionResponse resp = Context.ServletResponse as IPortletActionResponse;
178                         IPortletActionRequest req = Context.ServletRequest as IPortletActionRequest;
179                         if (req == null)
180                                 return false;
181
182                         // When redirecting don't save the page viewstate and hidden fields
183                         if (resp.isRedirected ())
184                                 return true;
185
186                         if (IsPostBack && 0 == String.Compare (Request.HttpMethod, "POST", true, CultureInfo.InvariantCulture)) {
187                                 resp.setRenderParameter ("__VIEWSTATE", GetSavedViewState ());
188                                 if (ClientScript.hiddenFields != null)
189                                         foreach (string key in ClientScript.hiddenFields.Keys)
190                                                 resp.setRenderParameter (key, (string) ClientScript.hiddenFields [key]);
191
192                                 if (is_validated && Validators.Count > 0) {
193                                         string validatorsState = GetValidatorsState ();
194 #if DEBUG
195                                         Console.WriteLine ("__VALIDATORSSTATE: " + validatorsState);
196 #endif
197                                         if (!String.IsNullOrEmpty (validatorsState))
198                                                 resp.setRenderParameter ("__VALIDATORSSTATE", validatorsState);
199                                 }
200                         }
201
202                         // Stop processing only if we are handling processAction. If we
203                         // are handling a postback from render then fall through.
204                         return req.processActionOnly ();
205                 }
206
207                 string GetValidatorsState () {
208                         bool [] validatorsState = new bool [Validators.Count];
209                         bool isValid = true;
210                         for (int i = 0; i < Validators.Count; i++) {
211                                 IValidator val = Validators [i];
212                                 if (!val.IsValid)
213                                         isValid = false;
214                                 else
215                                         validatorsState [i] = true;
216                         }
217                         if (isValid)
218                                 return null;
219
220                         return GetFormatter ().Serialize (validatorsState);
221                 }
222
223                 void RestoreValidatorsState () {
224                         string validatorsStateSerialized = Request.Form ["__VALIDATORSSTATE"];
225                         if (String.IsNullOrEmpty (validatorsStateSerialized))
226                                 return;
227
228                         is_validated = true;
229                         bool [] validatorsState = (bool []) GetFormatter ().Deserialize (validatorsStateSerialized);
230                         for (int i = 0; i < Math.Min (validatorsState.Length, Validators.Count); i++) {
231                                 IValidator val = Validators [i];
232                                 val.IsValid = validatorsState [i];
233                         }
234                 }
235         }
236 }