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