diff options
author | Tleiades Hax | 2007-10-25 09:26:47 +0000 |
---|---|---|
committer | Tleiades Hax | 2007-10-25 09:26:47 +0000 |
commit | 60e45418654975d27633a683b57c7013b98a23e3 (patch) | |
tree | 19e41032d405d29f203139f4c2ad62ac429fd1f1 /OpenSim/Framework/Communications/RestClient/RestClient.cs | |
parent | updated libode.so (no exception on assert) (diff) | |
download | opensim-SC-60e45418654975d27633a683b57c7013b98a23e3.zip opensim-SC-60e45418654975d27633a683b57c7013b98a23e3.tar.gz opensim-SC-60e45418654975d27633a683b57c7013b98a23e3.tar.bz2 opensim-SC-60e45418654975d27633a683b57c7013b98a23e3.tar.xz |
Created a generic RESTClient component, which simplifies querying for resources from REST based web-services.
Currently it supports a barebones scheme for specifying the path of the resource and querying asynchroneously. POST method is still wacky and a good solid scheme for handling timeout still remain.
Diffstat (limited to 'OpenSim/Framework/Communications/RestClient/RestClient.cs')
-rw-r--r-- | OpenSim/Framework/Communications/RestClient/RestClient.cs | 328 |
1 files changed, 328 insertions, 0 deletions
diff --git a/OpenSim/Framework/Communications/RestClient/RestClient.cs b/OpenSim/Framework/Communications/RestClient/RestClient.cs new file mode 100644 index 0000000..ccf8376 --- /dev/null +++ b/OpenSim/Framework/Communications/RestClient/RestClient.cs | |||
@@ -0,0 +1,328 @@ | |||
1 | using System; | ||
2 | using System.IO; | ||
3 | using System.Net; | ||
4 | using System.Web; | ||
5 | using System.Text; | ||
6 | using System.Collections.Generic; | ||
7 | using System.Threading; | ||
8 | |||
9 | namespace OpenSim.Framework.RestClient | ||
10 | { | ||
11 | /// <summary> | ||
12 | /// Implementation of a generic REST client | ||
13 | /// </summary> | ||
14 | /// <remarks> | ||
15 | /// This class is a generic implementation of a REST (Representational State Transfer) web service. This | ||
16 | /// class is designed to execute both synchroneously and asynchroneously. | ||
17 | /// | ||
18 | /// Internally the implementation works as a two stage asynchroneous web-client. | ||
19 | /// When the request is initiated, RestClient will query asynchroneously for for a web-response, | ||
20 | /// sleeping until the initial response is returned by the server. Once the initial response is retrieved | ||
21 | /// the second stage of asynchroneous requests will be triggered, in an attempt to read of the response | ||
22 | /// object into a memorystream as a sequence of asynchroneous reads. | ||
23 | /// | ||
24 | /// The asynchronisity of RestClient is designed to move as much processing into the back-ground, allowing | ||
25 | /// other threads to execute, while it waits for a response from the web-service. RestClient it self, can be | ||
26 | /// invoked by the caller in either synchroneous mode or asynchroneous mode. | ||
27 | /// </remarks> | ||
28 | public class RestClient | ||
29 | { | ||
30 | /// <summary> | ||
31 | /// The base Uri of the web-service e.g. http://www.google.com | ||
32 | /// </summary> | ||
33 | private string _url; | ||
34 | |||
35 | /// <summary> | ||
36 | /// Path elements of the query | ||
37 | /// </summary> | ||
38 | private List<string> _pathElements = new List<string>(); | ||
39 | |||
40 | /// <summary> | ||
41 | /// Parameter elements of the query, e.g. min=34 | ||
42 | /// </summary> | ||
43 | private Dictionary<string, string> _parameterElements = new Dictionary<string, string>(); | ||
44 | |||
45 | /// <summary> | ||
46 | /// Request method. E.g. GET, POST, PUT or DELETE | ||
47 | /// </summary> | ||
48 | private string _method; | ||
49 | |||
50 | /// <summary> | ||
51 | /// Temporary buffer used to store bytes temporarily as they come in from the server | ||
52 | /// </summary> | ||
53 | private byte[] _readbuf; | ||
54 | |||
55 | /// <summary> | ||
56 | /// MemoryStream representing the resultiong resource | ||
57 | /// </summary> | ||
58 | MemoryStream _resource; | ||
59 | |||
60 | /// <summary> | ||
61 | /// WebRequest object, held as a member variable | ||
62 | /// </summary> | ||
63 | private HttpWebRequest _request; | ||
64 | |||
65 | /// <summary> | ||
66 | /// WebResponse object, held as a member variable, so we can close it | ||
67 | /// </summary> | ||
68 | private HttpWebResponse _response; | ||
69 | |||
70 | /// <summary> | ||
71 | /// This flag will help block the main synchroneous method, in case we run in synchroneous mode | ||
72 | /// </summary> | ||
73 | public static ManualResetEvent _allDone = new ManualResetEvent(false); | ||
74 | |||
75 | /// <summary> | ||
76 | /// Default time out period | ||
77 | /// </summary> | ||
78 | const int DefaultTimeout = 10 * 1000; // 10 seconds timeout | ||
79 | |||
80 | /// <summary> | ||
81 | /// Default Buffer size of a block requested from the web-server | ||
82 | /// </summary> | ||
83 | const int BufferSize = 4096; // Read blocks of 4 KB. | ||
84 | |||
85 | |||
86 | /// <summary> | ||
87 | /// if an exception occours during async processing, we need to save it, so it can be | ||
88 | /// rethrown on the primary thread; | ||
89 | /// </summary> | ||
90 | private Exception _asyncException; | ||
91 | |||
92 | /// <summary> | ||
93 | /// Instantiate a new RestClient | ||
94 | /// </summary> | ||
95 | /// <param name="url">Web-service to query, e.g. http://osgrid.org:8003</param> | ||
96 | public RestClient(string url) | ||
97 | { | ||
98 | _url = url; | ||
99 | _readbuf = new byte[BufferSize]; | ||
100 | _resource = new MemoryStream(); | ||
101 | _request = null; | ||
102 | _response = null; | ||
103 | } | ||
104 | |||
105 | /// <summary> | ||
106 | /// Add a path element to the query, e.g. assets | ||
107 | /// </summary> | ||
108 | /// <param name="element">path entry</param> | ||
109 | public void AddResourcePath(string element) | ||
110 | { | ||
111 | if(isSlashed(element)) | ||
112 | _pathElements.Add(element.Substring(0, element.Length-1)); | ||
113 | else | ||
114 | _pathElements.Add(element); | ||
115 | } | ||
116 | |||
117 | /// <summary> | ||
118 | /// Add a query parameter to the Url | ||
119 | /// </summary> | ||
120 | /// <param name="name">Name of the parameter, e.g. min</param> | ||
121 | /// <param name="value">Value of the parameter, e.g. 42</param> | ||
122 | public void AddQueryParameter(string name, string value) | ||
123 | { | ||
124 | _parameterElements.Add(HttpUtility.UrlEncode(name), HttpUtility.UrlEncode(value)); | ||
125 | } | ||
126 | |||
127 | /// <summary> | ||
128 | /// Web-Request method, e.g. GET, PUT, POST, DELETE | ||
129 | /// </summary> | ||
130 | public string RequestMethod | ||
131 | { | ||
132 | get { return _method; } | ||
133 | set { _method = value; } | ||
134 | } | ||
135 | |||
136 | /// <summary> | ||
137 | /// True if string contains a trailing slash '/' | ||
138 | /// </summary> | ||
139 | /// <param name="s">string to be examined</param> | ||
140 | /// <returns>true if slash is present</returns> | ||
141 | private bool isSlashed(string s) | ||
142 | { | ||
143 | return s.Substring(s.Length - 1, 1) == "/"; | ||
144 | } | ||
145 | |||
146 | /// <summary> | ||
147 | /// return a slash or blank. A slash will be returned if the string does not contain one | ||
148 | /// </summary> | ||
149 | /// <param name="s">stromg to be examined</param> | ||
150 | /// <returns>slash '/' if not already present</returns> | ||
151 | private string slash(string s) | ||
152 | { | ||
153 | return isSlashed(s) ? "" : "/"; | ||
154 | } | ||
155 | |||
156 | /// <summary> | ||
157 | /// Build a Uri based on the intial Url, path elements and parameters | ||
158 | /// </summary> | ||
159 | /// <returns>fully constructed Uri</returns> | ||
160 | Uri buildUri() | ||
161 | { | ||
162 | StringBuilder sb = new StringBuilder(); | ||
163 | sb.Append(_url); | ||
164 | |||
165 | foreach (string e in _pathElements) | ||
166 | { | ||
167 | sb.Append("/"); | ||
168 | sb.Append(e); | ||
169 | } | ||
170 | |||
171 | bool firstElement = true; | ||
172 | foreach (KeyValuePair<string, string> kv in _parameterElements) | ||
173 | { | ||
174 | if (firstElement) | ||
175 | { | ||
176 | sb.Append("?"); | ||
177 | firstElement = false; | ||
178 | } else | ||
179 | sb.Append("&"); | ||
180 | |||
181 | sb.Append(kv.Key); | ||
182 | if (kv.Value != null && kv.Value.Length != 0) | ||
183 | { | ||
184 | sb.Append("="); | ||
185 | sb.Append(kv.Value); | ||
186 | } | ||
187 | } | ||
188 | return new Uri(sb.ToString()); | ||
189 | } | ||
190 | |||
191 | /// <summary> | ||
192 | /// Async method, invoked when a block of data has been received from the service | ||
193 | /// </summary> | ||
194 | /// <param name="ar"></param> | ||
195 | private void StreamIsReadyDelegate(IAsyncResult ar) | ||
196 | { | ||
197 | try | ||
198 | { | ||
199 | Stream s = (Stream)ar.AsyncState; | ||
200 | int read = s.EndRead(ar); | ||
201 | |||
202 | // Read the HTML page and then print it to the console. | ||
203 | if (read > 0) | ||
204 | { | ||
205 | _resource.Write(_readbuf, 0, read); | ||
206 | IAsyncResult asynchronousResult = s.BeginRead(_readbuf, 0, BufferSize, new AsyncCallback(StreamIsReadyDelegate), s); | ||
207 | |||
208 | // TODO! Implement timeout, without killing the server | ||
209 | //ThreadPool.RegisterWaitForSingleObject(asynchronousResult.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallback), _request, DefaultTimeout, true); | ||
210 | return; | ||
211 | } | ||
212 | else | ||
213 | { | ||
214 | s.Close(); | ||
215 | _allDone.Set(); | ||
216 | } | ||
217 | } | ||
218 | catch (Exception e) | ||
219 | { | ||
220 | _allDone.Set(); | ||
221 | _asyncException = e; | ||
222 | } | ||
223 | } | ||
224 | |||
225 | /// <summary> | ||
226 | /// Async method, invoked when the intial response if received from the server | ||
227 | /// </summary> | ||
228 | /// <param name="ar"></param> | ||
229 | private void ResponseIsReadyDelegate(IAsyncResult ar) | ||
230 | { | ||
231 | try | ||
232 | { | ||
233 | // grab response | ||
234 | WebRequest wr = (WebRequest)ar.AsyncState; | ||
235 | _response = (HttpWebResponse)wr.EndGetResponse(ar); | ||
236 | |||
237 | // get response stream, and setup async reading | ||
238 | Stream s = _response.GetResponseStream(); | ||
239 | IAsyncResult asynchronousResult = s.BeginRead(_readbuf, 0, BufferSize, new AsyncCallback(StreamIsReadyDelegate), s); | ||
240 | |||
241 | // TODO! Implement timeout, without killing the server | ||
242 | // wait until completed, or we timed out | ||
243 | // ThreadPool.RegisterWaitForSingleObject(asynchronousResult.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallback), _request, DefaultTimeout, true); | ||
244 | } | ||
245 | catch (Exception e) | ||
246 | { | ||
247 | _allDone.Set(); | ||
248 | _asyncException = e; | ||
249 | } | ||
250 | } | ||
251 | |||
252 | // Abort the request if the timer fires. | ||
253 | private static void TimeoutCallback(object state, bool timedOut) | ||
254 | { | ||
255 | if (timedOut) | ||
256 | { | ||
257 | HttpWebRequest request = state as HttpWebRequest; | ||
258 | if (request != null) | ||
259 | { | ||
260 | request.Abort(); | ||
261 | } | ||
262 | } | ||
263 | } | ||
264 | |||
265 | /// <summary> | ||
266 | /// Perform synchroneous request | ||
267 | /// </summary> | ||
268 | public Stream Request() | ||
269 | { | ||
270 | _request = (HttpWebRequest)WebRequest.Create(buildUri()); | ||
271 | _request.KeepAlive = false; | ||
272 | _request.ContentType = "text/html"; | ||
273 | _request.Timeout = 200; | ||
274 | _asyncException = null; | ||
275 | |||
276 | IAsyncResult responseAsyncResult = _request.BeginGetResponse(new AsyncCallback(ResponseIsReadyDelegate), _request); | ||
277 | |||
278 | // TODO! Implement timeout, without killing the server | ||
279 | // this line implements the timeout, if there is a timeout, the callback fires and the request becomes aborted | ||
280 | //ThreadPool.RegisterWaitForSingleObject(responseAsyncResult.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallback), _request, DefaultTimeout, true); | ||
281 | |||
282 | _allDone.WaitOne(); | ||
283 | if(_response != null) | ||
284 | _response.Close(); | ||
285 | if (_asyncException != null) | ||
286 | throw _asyncException; | ||
287 | return _resource; | ||
288 | } | ||
289 | |||
290 | #region Async Invocation | ||
291 | public IAsyncResult BeginRequest(AsyncCallback callback, object state) | ||
292 | { | ||
293 | /// <summary> | ||
294 | /// In case, we are invoked asynchroneously this object will keep track of the state | ||
295 | /// </summary> | ||
296 | AsyncResult<Stream> ar = new AsyncResult<Stream>(callback, state); | ||
297 | ThreadPool.QueueUserWorkItem(RequestHelper, ar); | ||
298 | return ar; | ||
299 | } | ||
300 | |||
301 | public Stream EndRequest(IAsyncResult asyncResult) | ||
302 | { | ||
303 | AsyncResult<Stream> ar = (AsyncResult<Stream>)asyncResult; | ||
304 | |||
305 | // Wait for operation to complete, then return result or | ||
306 | // throw exception | ||
307 | return ar.EndInvoke(); | ||
308 | } | ||
309 | |||
310 | private void RequestHelper(Object asyncResult) | ||
311 | { | ||
312 | // We know that it's really an AsyncResult<DateTime> object | ||
313 | AsyncResult<Stream> ar = (AsyncResult<Stream>)asyncResult; | ||
314 | try | ||
315 | { | ||
316 | // Perform the operation; if sucessful set the result | ||
317 | Stream s = Request(); | ||
318 | ar.SetAsCompleted(s, false); | ||
319 | } | ||
320 | catch (Exception e) | ||
321 | { | ||
322 | // If operation fails, set the exception | ||
323 | ar.HandleException(e, false); | ||
324 | } | ||
325 | } | ||
326 | #endregion Async Invocation | ||
327 | } | ||
328 | } | ||