New test.
[mono.git] / mcs / class / System.Web.Services / System.Web.Services.Protocols / SoapMessage.cs
1 // \r
2 // System.Web.Services.Protocols.SoapMessage.cs\r
3 //\r
4 // Author:\r
5 //   Tim Coleman (tim@timcoleman.com)\r
6 //   Lluis Sanchez Gual (lluis@ximian.com)\r
7 //\r
8 // Copyright (C) Tim Coleman, 2002\r
9 //\r
10
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31 \r
32 using System.IO;\r
33 using System.Web.Services;\r
34 \r
35 namespace System.Web.Services.Protocols {\r
36         public abstract class SoapMessage {\r
37 \r
38                 #region Fields\r
39 \r
40                 string content_type = "text/xml";\r
41                 string content_encoding;\r
42                 SoapException exception = null;\r
43                 SoapHeaderCollection headers;\r
44                 SoapMessageStage stage;\r
45                 Stream stream;\r
46                 object[] inParameters;\r
47                 object[] outParameters;\r
48                 \r
49 #if NET_2_0\r
50                 SoapProtocolVersion soapVersion;\r
51 #endif\r
52 \r
53                 #endregion // Fields\r
54 \r
55                 #region Constructors\r
56 \r
57                 internal SoapMessage ()\r
58                 {\r
59                         headers = new SoapHeaderCollection ();\r
60                 }\r
61 \r
62                 internal SoapMessage (Stream stream, SoapException exception)\r
63                 {\r
64                         this.exception = exception;\r
65                         this.stream = stream;\r
66                         headers = new SoapHeaderCollection ();\r
67                 }\r
68 \r
69                 #endregion\r
70 \r
71                 #region Properties\r
72 \r
73                 internal object[] InParameters \r
74                 {\r
75                         get { return inParameters; }\r
76                         set { inParameters = value; }\r
77                 }\r
78 \r
79                 internal object[] OutParameters \r
80                 {\r
81                         get { return outParameters; }\r
82                         set { outParameters = value; }\r
83                 }\r
84 \r
85                 public abstract string Action \r
86                 {\r
87                         get;\r
88                 }\r
89 \r
90                 public string ContentType {\r
91                         get { return content_type; }\r
92                         set { content_type = value; }\r
93                 }\r
94 \r
95                 public SoapException Exception {\r
96                         get { return exception; }\r
97 #if NET_2_0\r
98                         set { exception = value; }\r
99 #endif\r
100                 }\r
101 \r
102                 public SoapHeaderCollection Headers {\r
103                         get { return headers; }\r
104                 }\r
105 \r
106                 public abstract LogicalMethodInfo MethodInfo {\r
107                         get;\r
108                 }\r
109 \r
110                 public abstract bool OneWay {\r
111                         get;\r
112                 }\r
113 \r
114                 public SoapMessageStage Stage {\r
115                         get { return stage; }\r
116                 }\r
117 \r
118                 internal void SetStage (SoapMessageStage stage)\r
119                 {\r
120                         this.stage = stage;\r
121                 }\r
122                 \r
123                 public Stream Stream {\r
124                         get {\r
125                                 return stream;\r
126                         }\r
127                 }\r
128 \r
129                 public abstract string Url {\r
130                         get;\r
131                 }\r
132                 \r
133 #if NET_1_1\r
134                 public string ContentEncoding\r
135                 {\r
136                         get { return content_encoding; }\r
137                         set { content_encoding = value; }\r
138                 }\r
139 #else\r
140                 internal string ContentEncoding\r
141                 {\r
142                         get { return content_encoding; }\r
143                         set { content_encoding = value; }\r
144                 }\r
145 #endif\r
146 \r
147 #if NET_2_0\r
148                 [System.Runtime.InteropServices.ComVisible(false)]\r
149                 public virtual SoapProtocolVersion SoapVersion {\r
150                         get { return soapVersion; }\r
151                 }\r
152 #endif\r
153  \r
154                 internal Stream InternalStream \r
155                 { \r
156                         // for getter use public stream property\r
157                         set {\r
158                                 stream = value;\r
159                         }\r
160                 }\r
161 \r
162                 #endregion Properties\r
163 \r
164                 #region Methods\r
165 \r
166                 protected abstract void EnsureInStage ();\r
167                 protected abstract void EnsureOutStage ();\r
168 \r
169                 protected void EnsureStage (SoapMessageStage stage) \r
170                 {\r
171                         if ((((int) stage) & ((int) Stage)) == 0)\r
172                                 throw new InvalidOperationException ("The current SoapMessageStage is not the asserted stage or stages.");\r
173                 }\r
174 \r
175                 public object GetInParameterValue (int index) \r
176                 {\r
177                         return inParameters [index];\r
178                 }\r
179 \r
180                 public object GetOutParameterValue (int index) \r
181                 {\r
182                         if (MethodInfo.IsVoid) return outParameters [index];\r
183                         else return outParameters [index + 1];\r
184                 }\r
185 \r
186                 public object GetReturnValue ()\r
187                 {\r
188                         if (!MethodInfo.IsVoid && exception == null) return outParameters [0];\r
189                         else return null;\r
190                 }\r
191 \r
192                 internal void SetHeaders (SoapHeaderCollection headers)\r
193                 {\r
194                         this.headers = headers;\r
195                 }\r
196 \r
197                 internal void SetException (SoapException ex)\r
198                 {\r
199                         exception = ex;\r
200                 }\r
201 \r
202                 internal void CollectHeaders (object target, HeaderInfo[] headers, SoapHeaderDirection direction)\r
203                 {\r
204                         Headers.Clear ();\r
205                         foreach (HeaderInfo hi in headers) \r
206                         {\r
207                                 if ((hi.Direction & direction) != 0 && !hi.IsUnknownHeader) \r
208                                 {\r
209                                         SoapHeader headerVal = hi.GetHeaderValue (target) as SoapHeader;\r
210                                         if (headerVal != null)\r
211                                                 Headers.Add (headerVal);\r
212                                 }\r
213                         }\r
214                 }\r
215 \r
216                 internal void UpdateHeaderValues (object target, HeaderInfo[] headersInfo)\r
217                 {\r
218                         foreach (SoapHeader header in Headers)\r
219                         {\r
220                                 HeaderInfo hinfo = FindHeader (headersInfo, header.GetType ());\r
221                                 if (hinfo != null) {\r
222                                         hinfo.SetHeaderValue (target, header);
223                                         header.DidUnderstand = !hinfo.IsUnknownHeader;
224                                 }\r
225                         }\r
226                 }\r
227 \r
228                 HeaderInfo FindHeader (HeaderInfo[] headersInfo, Type headerType)\r
229                 {
230                         HeaderInfo unknownHeaderInfo = null;
231                 \r
232                         foreach (HeaderInfo headerInfo in headersInfo) {
233                                 if (headerInfo.HeaderType == headerType)
234                                         return headerInfo;
235                                 else if (headerInfo.IsUnknownHeader) 
236                                         unknownHeaderInfo = headerInfo;
237                         }
238                         return unknownHeaderInfo;
239                 }\r
240 \r
241                 #endregion // Methods\r
242         }\r
243 }