Wed Feb 24 15:47:16 CET 2010 Paolo Molaro <lupus@ximian.com>
[mono.git] / mcs / class / System.Web / System.Web.UI / BasePartialCachingControl.cs
1 //
2 // System.Web.UI.BasePartialCachingControl.cs
3 //
4 // Author:
5 //   Andreas Nahr (ClassDevelopment@A-SoftTech.com)
6 //   Jackson Harper (jackson@ximian.com)
7 //
8 // (C) 2003 Andreas Nahr
9 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System.IO;
32 using System.Text;
33 using System.ComponentModel;
34 using System.Security.Permissions;
35 using System.Web.Caching;
36
37 namespace System.Web.UI
38 {
39         // CAS
40         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
41         [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
42         // attributes
43         [ToolboxItem (false)]
44         public abstract class BasePartialCachingControl : Control
45         {
46                 CacheDependency dependency;
47                 string ctrl_id;
48                 string guid;
49                 int duration;
50                 string varyby_params;
51                 string varyby_controls;
52                 string varyby_custom;
53                 DateTime expirationTime;
54                 bool slidingExpiration;
55                 
56                 Control control;
57                 ControlCachePolicy cachePolicy;
58                 
59                 protected BasePartialCachingControl()
60                 {
61                 }
62
63                 internal string CtrlID {
64                         get { return ctrl_id; }
65                         set { ctrl_id = value; }
66                 }
67
68                 internal string Guid {
69                         get { return guid; }
70                         set { guid = value; }
71                 }
72
73                 internal int Duration {
74                         get { return duration; }
75                         set { duration = value; }
76                 }
77
78                 internal string VaryByParams {
79                         get { return varyby_params; }
80                         set { varyby_params = value; }
81                 }
82
83                 internal string VaryByControls {
84                         get { return varyby_controls; }
85                         set { varyby_controls = value; }
86                 }
87
88                 internal string VaryByCustom {
89                         get { return varyby_custom; }
90                         set { varyby_custom = value; }
91                 }
92
93                 internal DateTime ExpirationTime {
94                         get { return expirationTime; }
95                         set { expirationTime = value; }
96                 }
97
98                 internal bool SlidingExpiration {
99                         get { return slidingExpiration; }
100                         set { slidingExpiration = value; }
101                 }
102                 
103                 internal abstract Control CreateControl ();
104
105                 public override void Dispose ()
106                 {
107                         if (dependency != null) {
108                                 dependency.Dispose ();
109                                 dependency = null;
110                         }
111                 }
112
113                 protected internal override void OnInit (EventArgs e)
114                 {
115                         control = CreateControl ();
116                         Controls.Add (control);
117                 }
118
119                 protected internal override void Render (HtmlTextWriter output)
120                 {
121                         Cache cache = HttpRuntime.InternalCache;
122                         string key = CreateKey ();
123                         string data = cache [key] as string;
124
125                         if (data != null) {
126                                 output.Write (data);
127                                 return;
128                         }
129
130                         HttpContext context = HttpContext.Current;
131                         StringWriter writer = new StringWriter ();
132                         TextWriter prev = context.Response.SetTextWriter (writer);
133                         HtmlTextWriter txt_writer = new HtmlTextWriter (writer);
134                         string text;
135                         try {
136                                 control.RenderControl (txt_writer);
137                         } finally {
138                                 text = writer.ToString ();
139                                 context.Response.SetTextWriter (prev);
140                                 output.Write (text);
141                         }
142
143                         context.InternalCache.Insert (key, text, dependency,
144                                                       DateTime.Now.AddSeconds (duration),
145                                                       Cache.NoSlidingExpiration,
146                                                       CacheItemPriority.Normal, null);
147                 }
148
149                 public ControlCachePolicy CachePolicy 
150                 {
151                         get {
152                                 if (cachePolicy == null)
153                                         cachePolicy = new ControlCachePolicy (this);
154
155                                 return cachePolicy;
156                         }
157                 }
158
159                 public CacheDependency Dependency {
160                         get {return dependency;}
161                         set {dependency = value;}
162                 }
163
164                 string CreateKey ()
165                 {
166                         StringBuilder builder = new StringBuilder ();
167                         HttpContext context = HttpContext.Current;
168
169                         builder.Append ("PartialCachingControl\n");
170                         builder.Append ("GUID: " + guid + "\n");
171
172                         if (varyby_params != null && varyby_params.Length > 0) {
173                                 string[] prms = varyby_params.Split (';');
174                                 for (int i=0; i<prms.Length; i++) {
175                                         string val = context.Request.Params [prms [i]];
176                                         builder.Append ("VP:");
177                                         builder.Append (prms [i]);
178                                         builder.Append ('=');
179                                         builder.Append (val != null ? val : "__null__");
180                                         builder.Append ('\n');
181                                 }
182                         }
183
184                         if (varyby_controls != null && varyby_params.Length > 0) {
185                                 string[] prms = varyby_controls.Split (';');
186                                 for (int i=0; i<prms.Length; i++) {
187                                         string val = context.Request.Params [prms [i]];
188                                         builder.Append ("VCN:");
189                                         builder.Append (prms [i]);
190                                         builder.Append ('=');
191                                         builder.Append (val != null ? val : "__null__");
192                                         builder.Append ('\n');
193                                 }
194                         }
195
196                         if (varyby_custom != null) {
197                                 string val = context.ApplicationInstance.GetVaryByCustomString (context,
198                                                 varyby_custom);
199                                 builder.Append ("VC:");
200                                 builder.Append (varyby_custom);
201                                 builder.Append ('=');
202                                 builder.Append (val != null ? val : "__null__");
203                                 builder.Append ('\n');
204                         }
205
206                         return builder.ToString ();
207                 }
208         }
209 }
210