2006-12-07 Marek Sieradzki <marek.sieradzki@gmail.com>
[mono.git] / mcs / class / System.Web / System.Web.SessionState / HttpSessionState.cs
1 //
2 // System.Web.SessionState.HttpSessionState
3 //
4 // Authors:
5 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //
7 // (C) 2002,2003 Ximian, Inc (http://www.ximian.com)
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using System.Collections;
31 using System.Collections.Specialized;
32 using System.Globalization;
33 using System.Security.Permissions;
34 using System.Text;
35 using System.Threading;
36
37 namespace System.Web.SessionState {
38
39 // CAS - no InheritanceDemand here as the class is sealed
40 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
41 public sealed class HttpSessionState : ICollection, IEnumerable
42 {
43         private string _id;
44         private SessionDictionary _dict;
45         private HttpStaticObjectsCollection _staticObjects;
46         private int _timeout;
47         private bool _newSession;
48         private bool _isCookieless;
49         private SessionStateMode _mode;
50         private bool _isReadonly;
51         internal bool _abandoned;
52
53         internal HttpSessionState (string id,
54                                    SessionDictionary dict,
55                                    HttpStaticObjectsCollection staticObjects,
56                                    int timeout,
57                                    bool newSession,
58                                    bool isCookieless,
59                                    SessionStateMode mode,
60                                    bool isReadonly)
61         {
62                 _id = id;
63                 _dict = dict;
64                 _staticObjects = staticObjects.Clone ();
65                 _timeout = timeout;
66                 _newSession = newSession;
67                 _isCookieless = isCookieless;
68                 _mode = mode;
69                 _isReadonly = isReadonly;
70         }
71
72         internal HttpSessionState Clone ()
73         {
74                 return new HttpSessionState (_id, _dict.Clone (), _staticObjects, _timeout, _newSession,
75                                              _isCookieless, _mode, _isReadonly);
76
77         }
78
79         public int CodePage {
80                 get {
81                         HttpContext current = HttpContext.Current;
82                         if (current == null)
83                                 return Encoding.Default.CodePage;
84
85                         return current.Response.ContentEncoding.CodePage;
86                 }
87                 
88                 set {
89                         HttpContext current = HttpContext.Current;
90                         if (current != null)
91                                 current.Response.ContentEncoding = Encoding.GetEncoding (value);
92                 }
93         }
94
95         public HttpSessionState Contents {
96                 get { return this; }
97         }
98
99         public int Count {
100                 get { return _dict.Count; }
101         }
102
103         internal bool IsAbandoned {
104                 get { return _abandoned; }
105         }
106
107         public bool IsCookieless {
108                 get { return _isCookieless; }
109         }
110
111         public bool IsNewSession {
112                 get { return _newSession; }
113         }
114
115         public bool IsReadOnly {
116                 get { return _isReadonly; }
117         }
118
119         public bool IsSynchronized {
120                 get { return false; }
121         }
122
123         public object this [string key] {
124                 get { return _dict [key]; }
125                 set { _dict [key] = value; }
126         }
127
128         public object this [int index] {
129                 get { return _dict [index]; }
130                 set { _dict [index] = value; }
131         }
132
133         public NameObjectCollectionBase.KeysCollection Keys {
134                 get { return _dict.Keys; }
135         }
136
137         public int LCID {
138                 get { return Thread.CurrentThread.CurrentCulture.LCID; }
139                 set { Thread.CurrentThread.CurrentCulture = new CultureInfo(value); }
140         }
141
142         public SessionStateMode Mode {
143                 get { return _mode; }
144         }
145
146         public string SessionID {
147                 get { return _id; }
148         }
149
150         public HttpStaticObjectsCollection StaticObjects {
151                 get { return _staticObjects; }
152         }
153
154         public object SyncRoot {
155                 get { return this; }
156         }
157
158         public int Timeout {
159                 get { return _timeout; }
160                 set {
161                         if (value < 1)
162                                 throw new ArgumentException ("The argument to SetTimeout must be greater than 0.");
163                         _timeout = value;
164                 }
165         }
166
167         internal SessionDictionary SessionDictionary {
168                 get { return _dict; }
169         }
170
171         internal void SetNewSession (bool value)
172         {
173                 _newSession = value;
174         }
175
176         public void Abandon ()
177         {
178                 _abandoned = true;
179         }
180
181         public void Add (string name, object value)
182         {
183                 _dict [name] = value;
184         }
185
186         public void Clear ()
187         {
188                 if (_dict != null)
189                         _dict.Clear ();
190         }
191         
192         public void CopyTo (Array array, int index)
193         {
194                 NameObjectCollectionBase.KeysCollection all = Keys;
195                 for (int i = 0; i < all.Count; i++)
196                         array.SetValue (all.Get(i), i + index);
197         }
198
199         public IEnumerator GetEnumerator ()
200         {
201                 return _dict.GetEnumerator ();
202         }
203         
204         public void Remove (string name)
205         {
206                 _dict.Remove (name);
207         }
208
209         public void RemoveAll ()
210         {
211                 _dict.Clear ();
212         }
213
214         public void RemoveAt (int index)
215         {
216                 _dict.RemoveAt (index);
217         }
218 }
219 }
220