2003-10-25 Sebastien Pouliot <spouliot@videotron.ca>
[mono.git] / mcs / class / Microsoft.Web.Services / Microsoft.Web.Services / SoapContext.cs
1 //
2 // SoapContext.cs: SOAP Context
3 //
4 // Author:
5 //      Sebastien Pouliot (spouliot@motus.com)
6 //
7 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
8 //
9
10 using Microsoft.Web.Services.Dime;
11 using Microsoft.Web.Services.Referral;
12 using Microsoft.Web.Services.Routing;
13 using Microsoft.Web.Services.Security;
14 using Microsoft.Web.Services.Timestamp;
15 #if !WSE1
16 using Microsoft.Web.Services.Addressing;
17 using Microsoft.Web.Services.Messaging;
18 #endif
19
20 using System;
21 using System.Collections;
22
23 namespace Microsoft.Web.Services {
24
25         public sealed class SoapContext {
26
27                 private SoapEnvelope envelope;
28 #if WSE1
29                 private Uri actor;
30                 private Microsoft.Web.Services.Timestamp.Timestamp timestamp;
31 #else
32                 private Uri actor = new Uri ("http://" + System.Net.Dns.GetHostName ());
33                 private Microsoft.Web.Services.Timestamp.Timestamp timestamp = new Microsoft.Web.Services.Timestamp.Timestamp ();
34 #endif
35                 private Microsoft.Web.Services.Security.Security security;
36                 private Hashtable table;
37                 private DimeAttachmentCollection attachments;
38                 private string contentType;
39                 private SecurityCollection extendedSecurity;
40                 private ReferralCollection referrals;
41 #if !WSE1
42                 private AddressingHeaders addressingHeaders;
43                 private SoapChannel _channel;
44                 private bool _processed = false;
45                 private bool _isInbound = false;
46 #endif
47                 internal SoapContext () : this (null) 
48                 {
49                 }
50
51                 internal SoapContext (SoapEnvelope env) 
52                 {
53                         timestamp = new Microsoft.Web.Services.Timestamp.Timestamp ();
54 #if WSE1
55                         table = new Hashtable ();
56
57                         envelope = env;
58 #else //WSE2
59                         addressingHeaders = new AddressingHeaders (env);
60
61                         envelope = env;
62 #endif
63                 }
64 #if !WSE1
65                 public Action Action {
66                         get { return addressingHeaders.Action; }
67                         set { addressingHeaders.Action = value; }
68                 }
69
70                 public ReplyTo ReplyTo {
71                         get { return addressingHeaders.ReplyTo; }
72                         set { addressingHeaders.ReplyTo = value; }
73                 }
74          
75                 public To To {
76                         get { return addressingHeaders.To; }
77                 }
78
79                 public AddressingHeaders Addressing {
80                         get { return addressingHeaders; }
81                         set { addressingHeaders = value; }
82                 }
83
84                 public FaultTo FaultTo {
85                         get { return addressingHeaders.FaultTo; }
86                         set { addressingHeaders.FaultTo = value; }
87                 }
88
89                 public From From {
90                         get { return addressingHeaders.From; }
91                         set { addressingHeaders.From = value; }
92                 }
93
94                 public MessageID MessageID {
95                         get { return addressingHeaders.MessageID; }
96                         set { addressingHeaders.MessageID = value; }
97                 }
98
99                 public Recipient Recipient {
100                         get { return addressingHeaders.Recipient; }
101                         set { addressingHeaders.Recipient = value; }
102                 }
103
104                 public RelatesTo RelatesTo {
105                         get { return addressingHeaders.RelatesTo; }
106                         set { addressingHeaders.RelatesTo = value; }
107                 }
108
109                 public SoapChannel Channel {
110                         get { return _channel; }
111                         set { _channel = value; }
112                 }
113
114                 public bool Processed {
115                         get { return _processed; }
116                 }
117
118                 public void SetProcessed (bool to) {
119                         _processed = to;
120                 }
121
122                 public void SetTo (Uri uri) {
123                         addressingHeaders.To = uri;
124                 }
125
126                 public void SetTo (To to) {
127                         addressingHeaders.To = to;
128                 }
129
130                 public void SetActor (Uri act)
131                 {
132                         actor = act;
133                 }
134
135                 public void SetIsInbound (bool to)
136                 {
137                         _isInbound = to;
138                 }
139 #endif
140                 public Uri Actor { 
141                         get { return actor; }
142                 }
143
144                 public DimeAttachmentCollection Attachments { 
145                         get { 
146                                 if (attachments == null)
147                                         attachments = new DimeAttachmentCollection ();
148                                 return attachments; 
149                         }
150                 }
151
152                 public string ContentType { 
153                         get { return contentType; }
154                 }
155
156                 public SoapEnvelope Envelope { 
157                         get { return envelope; }
158                 }
159
160                 public SecurityCollection ExtendedSecurity {
161                         get { 
162                                 if (extendedSecurity == null)
163                                         extendedSecurity = new SecurityCollection ();
164                                 return extendedSecurity; 
165                         }
166                 }
167
168                 public object this [string key] { 
169                         get { return table [key]; }
170                         set { 
171                                 if (key == null)
172                                         throw new ArgumentNullException ("key");
173                                 table [key] = value;
174                         } 
175                 }
176
177                 public Path Path { 
178                         get { return null; }
179                         set {;} 
180                 }
181
182                 public ReferralCollection Referrals { 
183                         get { return referrals; }
184                 }
185
186                 public Microsoft.Web.Services.Security.Security Security { 
187                         get { 
188                                 if (security == null) {
189                                         if (actor != null)
190                                                 security = new Microsoft.Web.Services.Security.Security (actor.ToString ());
191                                         else
192                                                 security = new Microsoft.Web.Services.Security.Security ();
193                                 }
194                                 return security; 
195                         }
196                 }
197
198                 public Microsoft.Web.Services.Timestamp.Timestamp Timestamp { 
199                         get { return timestamp; }
200                 }
201
202                 internal bool IsReserved (string key) 
203                 {
204                         switch (key) {
205                                 case "Actor":
206                                 case "Attachments":
207                                 case "ContentType":
208                                 case "Envelope":
209                                 case "ExtendedSecurity":
210                                 case "IsInbound":
211                                 case "IsIntermediary":
212                                 case "Referrals":
213                                 case "Path":
214                                 case "Security":
215                                 case "Timestamp":
216                                 case "WebRequest":
217                                 case "WebResponse":
218                                         return true;
219                                 default:
220                                         return false;
221                         }
222                 }
223
224                 public void Add (string key, object value) 
225                 {
226                         if (key == null)
227                                 throw new ArgumentNullException ("key");
228                         if (IsReserved (key))
229                                 throw new ArgumentException ("reserved key");
230                         table.Add (key, value);
231                 }
232
233                 public void Clear () 
234                 {
235                         foreach (DictionaryEntry entry in table) {
236                                 string key = (string) entry.Key;
237                                 // remove all except reserved names
238                                 if (!IsReserved (key))
239                                         table.Remove (key);
240                         }
241                 }
242
243                 public bool Contains (string key) 
244                 {
245                         if (key == null)
246                                 throw new ArgumentNullException ("key");
247                         return table.Contains (key);
248                 }
249
250                 public void CopyTo (SoapContext context) 
251                 {
252                         if (context == null)
253                                 throw new ArgumentNullException ("context");
254                         context.actor = this.actor;
255                         foreach (DimeAttachment da in Attachments) {
256                                 context.Attachments.Add (da);
257                         }
258                         context.contentType = contentType;
259                         context.envelope = envelope;
260                         context.extendedSecurity = ExtendedSecurity;
261                         context.Path = Path;
262                         context.referrals = Referrals;
263                         context.security = security;
264                         context.timestamp = timestamp;
265                         foreach (DictionaryEntry de in table) {
266                                 context.table.Add (de.Key, de.Value);
267                         }
268                 }
269
270                 public IDictionaryEnumerator GetEnumerator () 
271                 {
272                         return table.GetEnumerator ();
273                 }
274
275                 public void Remove (string key) 
276                 {
277                         if (key == null)
278                                 throw new ArgumentNullException ("key");
279                         if (IsReserved (key))
280                                 throw new ArgumentException ("reserved key");
281                         table.Remove (key);
282                 }
283         }
284 }