2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[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                 static HybridDictionary cache;\r
62                 #endregion\r
63 \r
64                 #region Constructors\r
65 \r
66                 static WebClientProtocol ()\r
67                 {\r
68                         cache = new HybridDictionary ();\r
69                 }\r
70 \r
71                 protected WebClientProtocol () \r
72                 {\r
73                         connectionGroupName = String.Empty;\r
74                         credentials = null;\r
75                         preAuthenticate = false;\r
76                         requestEncoding = null;\r
77                         timeout = 100000;\r
78                         url = String.Empty;\r
79                         abort = false;\r
80                 }\r
81                 \r
82                 #endregion // Constructors\r
83 \r
84                 #region Properties\r
85 \r
86                 [DefaultValue ("")]\r
87                 public string ConnectionGroupName {\r
88                         get { return connectionGroupName; }\r
89                         set { connectionGroupName = value; }\r
90                 }\r
91 \r
92                 public ICredentials Credentials {\r
93                         get { return credentials; }\r
94                         set { credentials = value; }\r
95                 }\r
96 \r
97                 [DefaultValue (false)]\r
98                 [WebServicesDescription ("Enables pre authentication of the request.")]\r
99                 public bool PreAuthenticate {\r
100                         get { return preAuthenticate; }\r
101                         set { preAuthenticate = value; }\r
102                 }\r
103 \r
104                 [DefaultValue ("")]\r
105                 [RecommendedAsConfigurable (true)]\r
106                 [WebServicesDescription ("The encoding to use for requests.")]\r
107                 public Encoding RequestEncoding {\r
108                         get { return requestEncoding; }\r
109                         set { requestEncoding = value; }\r
110                 }\r
111 \r
112                 [DefaultValue (100000)]\r
113                 [RecommendedAsConfigurable (true)]\r
114                 [WebServicesDescription ("Sets the timeout in milliseconds to be used for synchronous calls.  The default of -1 means infinite.")]\r
115                 public int Timeout {\r
116                         get { return timeout; }\r
117                         set { timeout = value; }\r
118                 }\r
119 \r
120                 [DefaultValue ("")]\r
121                 [RecommendedAsConfigurable (true)]\r
122                 [WebServicesDescription ("The base URL to the server to use for requests.")]\r
123                 public string Url {\r
124                         get { return url; }\r
125                         set {\r
126                                 url = value;\r
127                                 uri = new Uri (url);\r
128                         }\r
129                 }\r
130 \r
131                 #endregion // Properties\r
132 \r
133                 #region Methods\r
134 \r
135                 public virtual void Abort ()\r
136                 {\r
137                         if (current_request != null){\r
138                                 current_request.Abort ();\r
139                                 current_request = null;\r
140                         }\r
141                         abort = true;\r
142                 }\r
143 \r
144                 protected static void AddToCache (Type type, object value)\r
145                 {\r
146                         cache [type] = value;\r
147                 }\r
148 \r
149                 protected static object GetFromCache (Type type)\r
150                 {\r
151                         return cache [type];\r
152                 }\r
153 \r
154                 protected virtual WebRequest GetWebRequest (Uri uri)\r
155                 {\r
156                         if (uri == null)\r
157                                 throw new InvalidOperationException ("uri is null");\r
158 \r
159                         current_request = WebRequest.Create (uri);\r
160                         current_request.Timeout = timeout;\r
161                         current_request.PreAuthenticate = preAuthenticate;\r
162                         current_request.ConnectionGroupName = connectionGroupName;\r
163 \r
164                         if (credentials != null)\r
165                                 current_request.Credentials = credentials;\r
166 \r
167                         return current_request;\r
168                 }\r
169 \r
170                 protected virtual WebResponse GetWebResponse (WebRequest request)\r
171                 {\r
172                         if (abort)\r
173                                 throw new WebException ("The operation has been aborted.", WebExceptionStatus.RequestCanceled);\r
174 \r
175                         WebResponse response = null;\r
176                         try {\r
177                                 request.Timeout = timeout;\r
178                                 response = request.GetResponse ();\r
179                         } catch (WebException e) {\r
180                                 response = e.Response;\r
181                                 if (response == null)\r
182                                         throw;\r
183                         }\r
184 \r
185                         return response;\r
186                 }\r
187 \r
188                 protected virtual WebResponse GetWebResponse (WebRequest request, IAsyncResult result)\r
189                 {\r
190                         if (abort)\r
191                                 throw new WebException ("The operation has been aborted.", WebExceptionStatus.RequestCanceled);\r
192 \r
193                         return request.EndGetResponse (result);\r
194                 }\r
195 \r
196                 #endregion // Methods\r
197         }\r
198 }\r