* System.Web20.csproj: added ObjectInputStream.cs and ObjectOutputStream.cs
[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                 private CacheDependency dependency;
47                 private string ctrl_id;
48                 private string guid;
49                 private int duration;
50                 private string varyby_params;
51                 private string varyby_controls;
52                 private string varyby_custom;
53
54                 private Control control;
55                 
56                 protected BasePartialCachingControl()
57                 {
58                 }
59
60                 internal string CtrlID {
61                         get { return ctrl_id; }
62                         set { ctrl_id = value; }
63                 }
64
65                 internal string Guid {
66                         get { return guid; }
67                         set { guid = value; }
68                 }
69
70                 internal int Duration {
71                         get { return duration; }
72                         set { duration = value; }
73                 }
74
75                 internal string VaryByParams {
76                         get { return varyby_params; }
77                         set { varyby_params = value; }
78                 }
79
80                 internal string VaryByControls {
81                         get { return varyby_controls; }
82                         set { varyby_controls = value; }
83                 }
84
85                 internal string VaryByCustom {
86                         get { return varyby_custom; }
87                         set { varyby_custom = value; }
88                 }
89
90                 internal abstract Control CreateControl ();
91
92                 public override void Dispose ()
93                 {
94                         if (dependency != null) {
95                                 dependency.Dispose ();
96                                 dependency = null;
97                         }
98                 }
99
100 #if NET_2_0
101                 protected internal
102 #else
103                 protected
104 #endif
105                 override void OnInit (EventArgs e)
106                 {
107                         control = CreateControl ();
108                         Controls.Add (control);
109                 }
110
111 #if NET_2_0
112                 protected internal
113 #else
114                 protected
115 #endif
116                 override void Render (HtmlTextWriter output)
117                 {
118                         Cache cache = HttpRuntime.InternalCache;
119                         string key = CreateKey ();
120                         string data = cache [key] as string;
121
122                         if (data != null) {
123                                 output.Write (data);
124                                 return;
125                         }
126
127                         HttpContext context = HttpContext.Current;
128                         StringWriter writer = new StringWriter ();
129                         TextWriter prev = context.Response.SetTextWriter (writer);
130                         HtmlTextWriter txt_writer = new HtmlTextWriter (writer);
131                         string text;
132                         try {
133                                 control.RenderControl (txt_writer);
134                         } finally {
135                                 text = writer.ToString ();
136                                 context.Response.SetTextWriter (prev);
137                                 output.Write (text);
138                         }
139
140                         context.InternalCache.Insert (key, text, dependency,
141                                                       DateTime.Now.AddSeconds (duration),
142                                                       Cache.NoSlidingExpiration,
143                                                       CacheItemPriority.Normal, null);
144                 }
145
146 #if NET_2_0
147                 public ControlCachePolicy CachePolicy 
148                 {
149                         get {
150                                 throw new NotImplementedException ();
151                         }
152                 }
153 #endif
154
155                 public CacheDependency Dependency {
156                         get {return dependency;}
157                         set {dependency = value;}
158                 }
159
160                 private string CreateKey ()
161                 {
162                         StringBuilder builder = new StringBuilder ();
163                         HttpContext context = HttpContext.Current;
164
165                         builder.Append ("PartialCachingControl\n");
166                         builder.Append ("GUID: " + guid + "\n");
167
168                         if (varyby_params != null && varyby_params.Length > 0) {
169                                 string[] prms = varyby_params.Split (';');
170                                 for (int i=0; i<prms.Length; i++) {
171                                         string val = context.Request.Params [prms [i]];
172                                         builder.Append ("VP:");
173                                         builder.Append (prms [i]);
174                                         builder.Append ('=');
175                                         builder.Append (val != null ? val : "__null__");
176                                         builder.Append ('\n');
177                                 }
178                         }
179
180                         if (varyby_controls != null && varyby_params.Length > 0) {
181                                 string[] prms = varyby_controls.Split (';');
182                                 for (int i=0; i<prms.Length; i++) {
183                                         string val = context.Request.Params [prms [i]];
184                                         builder.Append ("VCN:");
185                                         builder.Append (prms [i]);
186                                         builder.Append ('=');
187                                         builder.Append (val != null ? val : "__null__");
188                                         builder.Append ('\n');
189                                 }
190                         }
191
192                         if (varyby_custom != null) {
193                                 string val = context.ApplicationInstance.GetVaryByCustomString (context,
194                                                 varyby_custom);
195                                 builder.Append ("VC:");
196                                 builder.Append (varyby_custom);
197                                 builder.Append ('=');
198                                 builder.Append (val != null ? val : "__null__");
199                                 builder.Append ('\n');
200                         }
201
202                         return builder.ToString ();
203                 }
204         }
205 }
206