2007-08-21 Igor Zelmanovich <igorz@mainsoft.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                 internal bool IsMultiForm {
83                         get { return IsPortletRender; }
84                 }
85
86                 internal bool IsPortletRender
87                 {
88                         get {
89                                 return RenderResponse != null;
90                         }
91                 }
92
93                 internal bool IsGetBack {
94                         get {
95                                 return IsPostBack && IsPortletRender &&
96                                         (0 == String.Compare (Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase));
97                         }
98                 }
99
100                 internal IPortletRenderResponse RenderResponse
101                 {
102                         get {
103                                 if (!_renderResponseInit)
104                                         if (Context != null) {
105                                                 _renderResponse = Context.ServletResponse as IPortletRenderResponse;
106                                                 _renderResponseInit = true;
107                                         }
108                                 return _renderResponse;
109                         }
110                 }
111
112                 public string CreateRenderUrl (string url)
113                 {
114                         if (RenderResponse != null)
115                                 return RenderResponse.createRenderURL (url);
116                         if (PortletNamespace == null)
117                                 return url;
118
119                         string internalUrl = RemoveAppPathIfInternal (url);
120                         if (internalUrl == null)
121                                 return url;
122
123                         PostBackOptions options = new PostBackOptions (this);
124                         options.ActionUrl = RenderPageMark + internalUrl;
125                         options.RequiresJavaScriptProtocol = true;
126                         return ClientScript.GetPostBackEventReference (options);
127                 }
128
129                 public string CreateActionUrl (string url)
130                 {
131                         if (url.StartsWith (RenderPageMark, StringComparison.Ordinal) || url.StartsWith (ActionPageMark, StringComparison.Ordinal))
132                                 return url;
133
134                         if (RenderResponse != null)
135                                 return RenderResponse.createActionURL (url);
136                         if (PortletNamespace == null)
137                                 return url;
138
139                         Uri requestUrl = Request.Url;
140                         string internalUrl = RemoveAppPathIfInternal (url);
141                         if (internalUrl == null)
142                                 return url;
143
144                         return ActionPageMark + internalUrl;
145                 }
146
147                 private string RemoveAppPathIfInternal (string url)
148                 {
149                         Uri reqUrl = Request.Url;
150                         string appPath = Request.ApplicationPath;
151                         string currPage = Request.CurrentExecutionFilePath;
152                         if (currPage.StartsWith (appPath, StringComparison.InvariantCultureIgnoreCase))
153                                 currPage = currPage.Substring (appPath.Length);
154                         return PortletInternalUtils.mapPathIfInternal (url, reqUrl.Host, reqUrl.Port, reqUrl.Scheme, appPath, currPage);
155                 }
156
157                 internal bool OnSaveStateCompleteForPortlet ()
158                 {
159                         if (PortletNamespace != null) {
160                                 ClientScript.RegisterHiddenField (PageNamespaceKey, PortletNamespace);
161                                 ClientScript.RegisterHiddenField (NextActionPageKey, "");
162                                 ClientScript.RegisterHiddenField (NextRenderPageKey, "");
163                         }
164
165                         IPortletActionResponse resp = Context.ServletResponse as IPortletActionResponse;
166                         IPortletActionRequest req = Context.ServletRequest as IPortletActionRequest;
167                         if (req == null)
168                                 return false;
169
170                         // When redirecting don't save the page viewstate and hidden fields
171                         if (resp.isRedirected ())
172                                 return true;
173
174                         if (IsPostBack && 0 == String.Compare (Request.HttpMethod, "POST", true, CultureInfo.InvariantCulture)) {
175                                 resp.setRenderParameter ("__VIEWSTATE", GetSavedViewState ());
176                                 if (ClientScript.hiddenFields != null)
177                                         foreach (string key in ClientScript.hiddenFields.Keys)
178                                                 resp.setRenderParameter (key, (string) ClientScript.hiddenFields [key]);
179                         }
180
181                         // Stop processing only if we are handling processAction. If we
182                         // are handling a postback from render then fall through.
183                         return req.processActionOnly ();
184                 }
185         }
186 }