svn path=/branches/mono-1-1-9/mono/; revision=51217
[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 //
9
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 using System;
31 using System.Collections;
32 using System.Collections.Specialized;
33 using System.Globalization;
34 using System.Text;
35 using System.Threading;
36 using System.Web;
37
38 namespace System.Web.SessionState {
39 public sealed class HttpSessionState : ICollection, IEnumerable
40 {
41         private string _id;
42         private SessionDictionary _dict;
43         private HttpStaticObjectsCollection _staticObjects;
44         private int _timeout;
45         private bool _newSession;
46         private bool _isCookieless;
47         private SessionStateMode _mode;
48         private bool _isReadonly;
49         internal bool _abandoned;
50
51         internal HttpSessionState (string id,
52                                    SessionDictionary dict,
53                                    HttpStaticObjectsCollection staticObjects,
54                                    int timeout,
55                                    bool newSession,
56                                    bool isCookieless,
57                                    SessionStateMode mode,
58                                    bool isReadonly)
59         {
60                 _id = id;
61                 _dict = dict;
62                 _staticObjects = staticObjects.Clone ();
63                 _timeout = timeout;
64                 _newSession = newSession;
65                 _isCookieless = isCookieless;
66                 _mode = mode;
67                 _isReadonly = isReadonly;
68         }
69
70         internal HttpSessionState Clone ()
71         {
72                 return new HttpSessionState (_id, _dict.Clone (), _staticObjects, _timeout, _newSession,
73                                              _isCookieless, _mode, _isReadonly);
74
75         }
76
77         public int CodePage {
78                 get {
79                         HttpContext current = HttpContext.Current;
80                         if (current == null)
81                                 return Encoding.Default.CodePage;
82
83                         return current.Response.ContentEncoding.CodePage;
84                 }
85                 
86                 set {
87                         HttpContext current = HttpContext.Current;
88                         if (current != null)
89                                 current.Response.ContentEncoding = Encoding.GetEncoding (value);
90                 }
91         }
92
93         public HttpSessionState Contents {
94                 get { return this; }
95         }
96
97         public int Count {
98                 get { return _dict.Count; }
99         }
100
101         internal bool IsAbandoned {
102                 get { return _abandoned; }
103         }
104
105         public bool IsCookieless {
106                 get { return _isCookieless; }
107         }
108
109         public bool IsNewSession {
110                 get { return _newSession; }
111         }
112
113         public bool IsReadOnly {
114                 get { return _isReadonly; }
115         }
116
117         public bool IsSynchronized {
118                 get { return false; }
119         }
120
121         public object this [string key] {
122                 get { return _dict [key]; }
123                 set { _dict [key] = value; }
124         }
125
126         public object this [int index] {
127                 get { return _dict [index]; }
128                 set { _dict [index] = value; }
129         }
130
131         public NameObjectCollectionBase.KeysCollection Keys {
132                 get { return _dict.Keys; }
133         }
134
135         public int LCID {
136                 get { return Thread.CurrentThread.CurrentCulture.LCID; }
137                 set { Thread.CurrentThread.CurrentCulture = new CultureInfo(value); }
138         }
139
140         public SessionStateMode Mode {
141                 get { return _mode; }
142         }
143
144         public string SessionID {
145                 get { return _id; }
146         }
147
148         public HttpStaticObjectsCollection StaticObjects {
149                 get { return _staticObjects; }
150         }
151
152         public object SyncRoot {
153                 get { return this; }
154         }
155
156         public int Timeout {
157                 get { return _timeout; }
158                 set {
159                         if (value < 1)
160                                 throw new ArgumentException ("The argument to SetTimeout must be greater than 0.");
161                         _timeout = value;
162                 }
163         }
164
165         internal SessionDictionary SessionDictionary {
166                 get { return _dict; }
167         }
168
169         internal void SetNewSession (bool value)
170         {
171                 _newSession = value;
172         }
173
174         public void Abandon ()
175         {
176                 _abandoned = true;
177         }
178
179         public void Add (string name, object value)
180         {
181                 _dict [name] = value;
182         }
183
184         public void Clear ()
185         {
186                 if (_dict != null)
187                         _dict.Clear ();
188         }
189         
190         public void CopyTo (Array array, int index)
191         {
192                 NameObjectCollectionBase.KeysCollection all = Keys;
193                 for (int i = 0; i < all.Count; i++)
194                         array.SetValue (all.Get(i), i + index);
195         }
196
197         public IEnumerator GetEnumerator ()
198         {
199                 return _dict.GetEnumerator ();
200         }
201         
202         public void Remove (string name)
203         {
204                 _dict.Remove (name);
205         }
206
207         public void RemoveAll ()
208         {
209                 _dict.Clear ();
210         }
211
212         public void RemoveAt (int index)
213         {
214                 _dict.RemoveAt (index);
215         }
216 }
217 }
218