2005-06-14 Lluis Sanchez Gual <lluis@novell.com>
[mono.git] / mcs / class / System.Web.Services / System.Web.Services.Protocols / WebClientProtocol.cs
1 // \r
2 // System.Web.Services.Protocols.WebClientProtocol.cs\r
3 //\r
4 // Author:\r
5 //   Tim Coleman (tim@timcoleman.com)\r
6 //\r
7 // Copyright (C) Tim Coleman, 2002\r
8 //\r
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 \r
31 using System.Collections.Specialized;\r
32 using System.ComponentModel;\r
33 using System.Net;\r
34 using System.Text;\r
35 using System.Threading;\r
36 using System.Web.Services;\r
37 \r
38 namespace System.Web.Services.Protocols {\r
39         public abstract class WebClientProtocol : Component {\r
40 \r
41                 #region Fields\r
42 \r
43                 string connectionGroupName;\r
44                 ICredentials credentials;\r
45                 bool preAuthenticate;\r
46                 Encoding requestEncoding;\r
47                 int timeout;\r
48                 string url;\r
49                 bool abort;\r
50 \r
51                 //\r
52                 // Used by SoapHttpClientProtocol, use this to avoid creating a new Uri on each invocation.\r
53                 //\r
54                 internal Uri uri;\r
55                         \r
56                 //\r
57                 // Points to the current request, so we can call Abort() on it\r
58                 //\r
59                 WebRequest current_request;\r
60                 \r
61 #if !TARGET_JVM
62                 static HybridDictionary cache;\r
63 #else
64                 static HybridDictionary cache {
65                         get {
66                                 return (HybridDictionary)AppDomain.CurrentDomain.GetData("WebClientProtocol.cache");
67                         }
68                         set {
69                                 AppDomain.CurrentDomain.SetData("WebClientProtocol.cache", value);
70                         }
71                 }
72 #endif
73                 #endregion\r
74 \r
75                 #region Constructors\r
76 \r
77                 static WebClientProtocol ()\r
78                 {\r
79                         cache = new HybridDictionary ();\r
80                 }\r
81 \r
82                 protected WebClientProtocol () \r
83                 {\r
84                         connectionGroupName = String.Empty;\r
85                         credentials = null;\r
86                         preAuthenticate = false;\r
87                         requestEncoding = null;\r
88                         timeout = 100000;\r
89                         url = String.Empty;\r
90                         abort = false;\r
91                 }\r
92                 \r
93                 #endregion // Constructors\r
94 \r
95                 #region Properties\r
96 \r
97                 [DefaultValue ("")]\r
98                 public string ConnectionGroupName {\r
99                         get { return connectionGroupName; }\r
100                         set { connectionGroupName = value; }\r
101                 }\r
102 \r
103                 public ICredentials Credentials {\r
104                         get { return credentials; }\r
105                         set { credentials = value; }\r
106                 }\r
107 \r
108                 [DefaultValue (false)]\r
109                 [WebServicesDescription ("Enables pre authentication of the request.")]\r
110                 public bool PreAuthenticate {\r
111                         get { return preAuthenticate; }\r
112                         set { preAuthenticate = value; }\r
113                 }\r
114 \r
115                 [DefaultValue ("")]\r
116                 [RecommendedAsConfigurable (true)]\r
117                 [WebServicesDescription ("The encoding to use for requests.")]\r
118                 public Encoding RequestEncoding {\r
119                         get { return requestEncoding; }\r
120                         set { requestEncoding = value; }\r
121                 }\r
122 \r
123                 [DefaultValue (100000)]\r
124                 [RecommendedAsConfigurable (true)]\r
125                 [WebServicesDescription ("Sets the timeout in milliseconds to be used for synchronous calls.  The default of -1 means infinite.")]\r
126                 public int Timeout {\r
127                         get { return timeout; }\r
128                         set { timeout = value; }\r
129                 }\r
130 \r
131                 [DefaultValue ("")]\r
132                 [RecommendedAsConfigurable (true)]\r
133                 [WebServicesDescription ("The base URL to the server to use for requests.")]\r
134                 public string Url {\r
135                         get { return url; }\r
136                         set {\r
137                                 url = value;\r
138                                 uri = new Uri (url);\r
139                         }\r
140                 }\r
141 \r
142                 #endregion // Properties\r
143 \r
144                 #region Methods\r
145 \r
146                 public virtual void Abort ()\r
147                 {\r
148                         if (current_request != null){\r
149                                 current_request.Abort ();\r
150                                 current_request = null;\r
151                         }\r
152                         abort = true;\r
153                 }\r
154 \r
155                 protected static void AddToCache (Type type, object value)\r
156                 {\r
157                         cache [type] = value;\r
158                 }\r
159 \r
160                 protected static object GetFromCache (Type type)\r
161                 {\r
162                         return cache [type];\r
163                 }\r
164 \r
165                 protected virtual WebRequest GetWebRequest (Uri uri)\r
166                 {\r
167                         if (uri == null)\r
168                                 throw new InvalidOperationException ("uri is null");\r
169 \r
170                         current_request = WebRequest.Create (uri);\r
171                         current_request.Timeout = timeout;\r
172                         current_request.PreAuthenticate = preAuthenticate;\r
173                         current_request.ConnectionGroupName = connectionGroupName;\r
174 \r
175                         if (credentials != null)\r
176                                 current_request.Credentials = credentials;\r
177 \r
178                         return current_request;\r
179                 }\r
180 \r
181                 protected virtual WebResponse GetWebResponse (WebRequest request)\r
182                 {\r
183                         if (abort)\r
184                                 throw new WebException ("The operation has been aborted.", WebExceptionStatus.RequestCanceled);\r
185 \r
186                         WebResponse response = null;\r
187                         try {\r
188                                 request.Timeout = timeout;\r
189                                 response = request.GetResponse ();\r
190                         } catch (WebException e) {\r
191                                 response = e.Response;\r
192                                 if (response == null)\r
193                                         throw;\r
194                         }\r
195 \r
196                         return response;\r
197                 }\r
198 \r
199                 protected virtual WebResponse GetWebResponse (WebRequest request, IAsyncResult result)\r
200                 {\r
201                         if (abort)\r
202                                 throw new WebException ("The operation has been aborted.", WebExceptionStatus.RequestCanceled);\r
203 \r
204                         return request.EndGetResponse (result);\r
205                 }\r
206 \r
207                 #endregion // Methods\r
208         }\r
209 }\r