b5d91c268598148f8d2dd34d0e129dea9ccdde1a
[mono.git] / mcs / class / System.Web / System.Web.SessionState / HttpSessionState.jvm.cs
1 //
2 // System.Web.SessionState.HttpSessionState.jvm.cs
3 //
4 // Authors:
5 //  Ilya Kharmatsky (ilyak@mainsoft.com)
6 //  Alon Gazit
7 //  Pavel Sandler
8 //
9 // (C) 2005 Mainsoft Corporation (http://www.mainsoft.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.Collections;
35 using System.Collections.Specialized;
36 using System.Globalization;
37 using System.Text;
38 using System.Threading;
39 using System.Web;
40 using System.Web.J2EE;
41 using System.Web.Hosting;
42
43 namespace System.Web.SessionState 
44 {
45 public sealed class HttpSessionState : ICollection, IEnumerable, java.io.Externalizable
46 {
47         private string _id;
48         private SessionDictionary _dict;
49         private HttpStaticObjectsCollection _staticObjects;
50         private int _timeout;
51         private bool _newSession;
52         private bool _isCookieless;
53         private SessionStateMode _mode;
54         private bool _isReadonly;
55         internal bool _abandoned;
56
57         private bool _needSessionPersistence = false;
58
59         internal HttpSessionState (string id,
60                                    SessionDictionary dict,
61                                    HttpStaticObjectsCollection staticObjects,
62                                    int timeout,
63                                    bool newSession,
64                                    bool isCookieless,
65                                    SessionStateMode mode,
66                                    bool isReadonly)
67         {
68                 _id = id;
69                 _dict = dict;
70                 _staticObjects = staticObjects.Clone ();
71                 _timeout = timeout;
72                 _newSession = newSession;
73                 _isCookieless = isCookieless;
74                 _mode = mode;
75                 _isReadonly = isReadonly;
76
77                 _needSessionPersistence = false;
78                 javax.servlet.ServletConfig config = (javax.servlet.ServletConfig)AppDomain.CurrentDomain.GetData(J2EEConsts.SERVLET_CONFIG);
79                 string sessionPersistance = config.getInitParameter(J2EEConsts.Enable_Session_Persistency);
80                 if (sessionPersistance!= null)
81                 {
82                         try
83                         {
84                                 _needSessionPersistence = Boolean.Parse(sessionPersistance);
85                         }
86                         catch (Exception)
87                         {
88                                 Console.WriteLine("EnableSessionPersistency init param's value is invalid. the value is " + sessionPersistance);
89                         }
90                 }
91         }
92
93         public HttpSessionState ()
94         {
95                 _id = null;
96                 _dict = new SessionDictionary();
97                 _staticObjects = new HttpStaticObjectsCollection();
98                 _timeout = 0;
99                 _newSession = false;
100                 _isCookieless = false;
101                 _mode = SessionStateMode.Off;
102                 _isReadonly = false;
103         }
104
105         public void writeExternal(java.io.ObjectOutput output)
106         {
107                 lock (this)
108                 {
109                         output.writeBoolean(_needSessionPersistence);
110                         if (!_needSessionPersistence)
111                                 //indicates that there is nothing to serialize for this object
112                                 return;
113
114                         System.Web.J2EE.ObjectOutputStream ms = new System.Web.J2EE.ObjectOutputStream(output);
115                         System.IO.BinaryWriter bw = new System.IO.BinaryWriter(ms);
116                         bw.Write(_id);
117                         _dict.Serialize(bw);
118                         _staticObjects.Serialize(bw);
119                         bw.Write(_timeout);
120                         bw.Write(_newSession);
121                         bw.Write(_isCookieless);
122                         if (_mode == SessionStateMode.Off)
123                                 bw.Write(0);
124                         else if (_mode == SessionStateMode.InProc)
125                                 bw.Write(1);
126                         else if (_mode == SessionStateMode.StateServer)
127                                 bw.Write(2);
128                         else 
129                                 bw.Write(3);
130                         bw.Write(_isReadonly);
131                 }
132         }
133
134         public void readExternal(java.io.ObjectInput input)
135         {
136                 lock(this)
137                 {
138                         _needSessionPersistence = input.readBoolean();
139                         if(!_needSessionPersistence) //noting has been written 
140                                 return;
141
142                         System.Web.J2EE.ObjectInputStream ms = new System.Web.J2EE.ObjectInputStream( input );
143                         System.IO.BinaryReader br = new System.IO.BinaryReader(ms);
144                         _id = br.ReadString();
145                         _dict =  SessionDictionary.Deserialize(br);
146                         _staticObjects = HttpStaticObjectsCollection.Deserialize(br);
147                         _timeout = br.ReadInt32();
148                         _newSession = br.ReadBoolean();
149                         _isCookieless = br.ReadBoolean();
150                         int mode = br.ReadInt32();
151                         if (mode == 0)
152                                 _mode = SessionStateMode.Off;
153                         else if (mode == 1)
154                                 _mode = SessionStateMode.InProc;
155                         else if (mode == 2)
156                                 _mode = SessionStateMode.StateServer;
157                         else 
158                                 _mode = SessionStateMode.SQLServer;
159                         _isReadonly = br.ReadBoolean();
160                 }
161         }
162
163         internal HttpSessionState Clone ()
164         {
165                 return new HttpSessionState (_id, _dict.Clone (), _staticObjects, _timeout, _newSession,
166                                              _isCookieless, _mode, _isReadonly);
167
168         }
169
170         public int CodePage {
171                 get {
172                         HttpContext current = HttpContext.Current;
173                         if (current == null)
174                                 return Encoding.Default.CodePage;
175
176                         return current.Response.ContentEncoding.CodePage;
177                 }
178                 
179                 set {
180                         HttpContext current = HttpContext.Current;
181                         if (current != null)
182                                 current.Response.ContentEncoding = Encoding.GetEncoding (value);
183                 }
184         }
185
186         public HttpSessionState Contents {
187                 get { return this; }
188         }
189
190         public int Count {
191                 get { return _dict.Count; }
192         }
193
194         internal bool IsAbandoned {
195                 get { return _abandoned; }
196         }
197
198         public bool IsCookieless {
199                 get { 
200                         ServletWorkerRequest worker = (ServletWorkerRequest)HttpContext.Current.Request.WorkerRequest;
201                         return worker.ServletRequest.isRequestedSessionIdFromURL();     
202                 }
203         }
204
205         public bool IsNewSession {
206                 get { return _newSession; }
207         }
208
209         public bool IsReadOnly {
210                 get { return _isReadonly; }
211         }
212
213         public bool IsSynchronized {
214                 get { return false; }
215         }
216
217         public object this [string key] {
218                 get { return _dict [key]; }
219                 set { 
220                         _dict [key] = value; 
221
222                         _newSession = false;
223                         SetJavaSessionAttribute();
224                 }
225         }
226
227         public object this [int index] {
228                 get { return _dict [index]; }
229                 set { 
230                         _dict [index] = value; 
231
232                         _newSession = false;
233                         SetJavaSessionAttribute();
234                 }
235         }
236
237         public NameObjectCollectionBase.KeysCollection Keys {
238                 get { return _dict.Keys; }
239         }
240
241         public int LCID {
242                 get { return Thread.CurrentThread.CurrentCulture.LCID; }
243                 set { Thread.CurrentThread.CurrentCulture = new CultureInfo(value); }
244         }
245
246         public SessionStateMode Mode {
247                 get { return _mode; }
248         }
249
250         public string SessionID {
251                 get { return _id; }
252         }
253
254         public HttpStaticObjectsCollection StaticObjects {
255                 get { return _staticObjects; }
256         }
257
258         public object SyncRoot {
259                 get { return this; }
260         }
261
262         public int Timeout {
263                 get { 
264                         ServletWorkerRequest worker = (ServletWorkerRequest)HttpContext.Current.Request.WorkerRequest;
265                         javax.servlet.http.HttpSession javaSession = worker.ServletRequest.getSession(false);
266                         if (javaSession != null)
267                          return javaSession.getMaxInactiveInterval()/60;                        
268                         else
269                          throw new NotSupportedException();
270                 }
271                 set {
272             if (value < 1)
273                  throw new ArgumentException ("The argument to SetTimeout must be greater than 0.");
274                         ServletWorkerRequest worker = (ServletWorkerRequest)HttpContext.Current.Request.WorkerRequest;
275                         javax.servlet.http.HttpSession javaSession = worker.ServletRequest.getSession(false);
276                         if (javaSession != null)
277                                 javaSession.setMaxInactiveInterval(value*60);                   
278                         else
279                                 throw new NotSupportedException();
280         }
281         }
282
283         internal SessionDictionary SessionDictionary {
284                 get { return _dict; }
285         }
286
287         internal void SetNewSession (bool value)
288         {
289                 _newSession = value;
290         }
291
292         public void Abandon ()
293         {
294                 _abandoned = true;
295
296                 SessionDictionary.Clear();
297                 ServletWorkerRequest worker = (ServletWorkerRequest)HttpContext.Current.Request.WorkerRequest;
298 //              worker.Servlet.getServletContext().removeAttribute("GH_SESSION_STATE");
299                 javax.servlet.http.HttpSession javaSession = worker.ServletRequest.getSession(false);
300
301                 if (javaSession != null)
302                 {
303                         javaSession.setAttribute("GH_SESSION_STATE",this);      
304                         javaSession.invalidate();
305                 }
306         }
307
308         public void Add (string name, object value)
309         {
310                 _dict [name] = value;
311
312                 _newSession = false;
313                 SetJavaSessionAttribute();
314         }
315
316         public void Clear ()
317         {
318                 if (_dict != null)
319                         _dict.Clear ();
320
321                 SetJavaSessionAttribute();
322         }
323         
324         public void CopyTo (Array array, int index)
325         {
326                 NameObjectCollectionBase.KeysCollection all = Keys;
327                 for (int i = 0; i < all.Count; i++)
328                         array.SetValue (all.Get(i), i + index);
329         }
330
331         public IEnumerator GetEnumerator ()
332         {
333                 return _dict.GetEnumerator ();
334         }
335         
336         public void Remove (string name)
337         {
338                 _dict.Remove (name);
339
340                 SetJavaSessionAttribute();
341         }
342
343         public void RemoveAll ()
344         {
345                 _dict.Clear ();
346
347                 SetJavaSessionAttribute();
348         }
349
350         public void RemoveAt (int index)
351         {
352                 _dict.RemoveAt (index);
353
354                 SetJavaSessionAttribute();
355         }
356         
357         public void SetJavaSessionAttribute ()
358         {
359                 ServletWorkerRequest worker = (ServletWorkerRequest)HttpContext.Current.Request.WorkerRequest;
360                 javax.servlet.http.HttpSession javaSession = worker.ServletRequest.getSession(false);
361                 if (javaSession != null)
362                         javaSession.setAttribute("GH_SESSION_STATE",this);      
363         }       
364 }
365 }
366