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