RestClient.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSim Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using System.IO;
  30. using System.Net;
  31. using System.Reflection;
  32. using System.Text;
  33. using System.Threading;
  34. using System.Web;
  35. using log4net;
  36. namespace OpenSim.Framework.Communications
  37. {
  38. /// <summary>
  39. /// Implementation of a generic REST client
  40. /// </summary>
  41. /// <remarks>
  42. /// This class is a generic implementation of a REST (Representational State Transfer) web service. This
  43. /// class is designed to execute both synchronously and asynchronously.
  44. ///
  45. /// Internally the implementation works as a two stage asynchronous web-client.
  46. /// When the request is initiated, RestClient will query asynchronously for for a web-response,
  47. /// sleeping until the initial response is returned by the server. Once the initial response is retrieved
  48. /// the second stage of asynchronous requests will be triggered, in an attempt to read of the response
  49. /// object into a memorystream as a sequence of asynchronous reads.
  50. ///
  51. /// The asynchronisity of RestClient is designed to move as much processing into the back-ground, allowing
  52. /// other threads to execute, while it waits for a response from the web-service. RestClient itself can be
  53. /// invoked by the caller in either synchronous mode or asynchronous modes.
  54. /// </remarks>
  55. public class RestClient
  56. {
  57. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  58. private string realuri;
  59. #region member variables
  60. /// <summary>
  61. /// The base Uri of the web-service e.g. http://www.google.com
  62. /// </summary>
  63. private string _url;
  64. /// <summary>
  65. /// Path elements of the query
  66. /// </summary>
  67. private List<string> _pathElements = new List<string>();
  68. /// <summary>
  69. /// Parameter elements of the query, e.g. min=34
  70. /// </summary>
  71. private Dictionary<string, string> _parameterElements = new Dictionary<string, string>();
  72. /// <summary>
  73. /// Request method. E.g. GET, POST, PUT or DELETE
  74. /// </summary>
  75. private string _method;
  76. /// <summary>
  77. /// Temporary buffer used to store bytes temporarily as they come in from the server
  78. /// </summary>
  79. private byte[] _readbuf;
  80. /// <summary>
  81. /// MemoryStream representing the resultiong resource
  82. /// </summary>
  83. private Stream _resource;
  84. /// <summary>
  85. /// WebRequest object, held as a member variable
  86. /// </summary>
  87. private HttpWebRequest _request;
  88. /// <summary>
  89. /// WebResponse object, held as a member variable, so we can close it
  90. /// </summary>
  91. private HttpWebResponse _response;
  92. /// <summary>
  93. /// This flag will help block the main synchroneous method, in case we run in synchroneous mode
  94. /// </summary>
  95. public static ManualResetEvent _allDone = new ManualResetEvent(false);
  96. /// <summary>
  97. /// Default time out period
  98. /// </summary>
  99. private const int DefaultTimeout = 10*1000; // 10 seconds timeout
  100. /// <summary>
  101. /// Default Buffer size of a block requested from the web-server
  102. /// </summary>
  103. private const int BufferSize = 4096; // Read blocks of 4 KB.
  104. /// <summary>
  105. /// if an exception occours during async processing, we need to save it, so it can be
  106. /// rethrown on the primary thread;
  107. /// </summary>
  108. private Exception _asyncException;
  109. #endregion member variables
  110. #region constructors
  111. /// <summary>
  112. /// Instantiate a new RestClient
  113. /// </summary>
  114. /// <param name="url">Web-service to query, e.g. http://osgrid.org:8003</param>
  115. public RestClient(string url)
  116. {
  117. _url = url;
  118. _readbuf = new byte[BufferSize];
  119. _resource = new MemoryStream();
  120. _request = null;
  121. _response = null;
  122. _lock = new object();
  123. }
  124. private object _lock;
  125. #endregion constructors
  126. /// <summary>
  127. /// Add a path element to the query, e.g. assets
  128. /// </summary>
  129. /// <param name="element">path entry</param>
  130. public void AddResourcePath(string element)
  131. {
  132. if (isSlashed(element))
  133. _pathElements.Add(element.Substring(0, element.Length - 1));
  134. else
  135. _pathElements.Add(element);
  136. }
  137. /// <summary>
  138. /// Add a query parameter to the Url
  139. /// </summary>
  140. /// <param name="name">Name of the parameter, e.g. min</param>
  141. /// <param name="value">Value of the parameter, e.g. 42</param>
  142. public void AddQueryParameter(string name, string value)
  143. {
  144. _parameterElements.Add(HttpUtility.UrlEncode(name), HttpUtility.UrlEncode(value));
  145. }
  146. /// <summary>
  147. /// Add a query parameter to the Url
  148. /// </summary>
  149. /// <param name="name">Name of the parameter, e.g. min</param>
  150. public void AddQueryParameter(string name)
  151. {
  152. _parameterElements.Add(HttpUtility.UrlEncode(name), null);
  153. }
  154. /// <summary>
  155. /// Web-Request method, e.g. GET, PUT, POST, DELETE
  156. /// </summary>
  157. public string RequestMethod
  158. {
  159. get { return _method; }
  160. set { _method = value; }
  161. }
  162. /// <summary>
  163. /// True if string contains a trailing slash '/'
  164. /// </summary>
  165. /// <param name="s">string to be examined</param>
  166. /// <returns>true if slash is present</returns>
  167. private static bool isSlashed(string s)
  168. {
  169. return s.Substring(s.Length - 1, 1) == "/";
  170. }
  171. /// <summary>
  172. /// Build a Uri based on the initial Url, path elements and parameters
  173. /// </summary>
  174. /// <returns>fully constructed Uri</returns>
  175. private Uri buildUri()
  176. {
  177. StringBuilder sb = new StringBuilder();
  178. sb.Append(_url);
  179. foreach (string e in _pathElements)
  180. {
  181. sb.Append("/");
  182. sb.Append(e);
  183. }
  184. bool firstElement = true;
  185. foreach (KeyValuePair<string, string> kv in _parameterElements)
  186. {
  187. if (firstElement)
  188. {
  189. sb.Append("?");
  190. firstElement = false;
  191. }
  192. else
  193. sb.Append("&");
  194. sb.Append(kv.Key);
  195. if (!string.IsNullOrEmpty(kv.Value))
  196. {
  197. sb.Append("=");
  198. sb.Append(kv.Value);
  199. }
  200. }
  201. realuri = sb.ToString();
  202. //m_log.InfoFormat("[REST CLIENT]: RestURL: {0}", realuri);
  203. return new Uri(sb.ToString());
  204. }
  205. #region Async communications with server
  206. /// <summary>
  207. /// Async method, invoked when a block of data has been received from the service
  208. /// </summary>
  209. /// <param name="ar"></param>
  210. private void StreamIsReadyDelegate(IAsyncResult ar)
  211. {
  212. try
  213. {
  214. Stream s = (Stream) ar.AsyncState;
  215. int read = s.EndRead(ar);
  216. if (read > 0)
  217. {
  218. _resource.Write(_readbuf, 0, read);
  219. IAsyncResult asynchronousResult =
  220. s.BeginRead(_readbuf, 0, BufferSize, new AsyncCallback(StreamIsReadyDelegate), s);
  221. // TODO! Implement timeout, without killing the server
  222. //ThreadPool.RegisterWaitForSingleObject(asynchronousResult.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallback), _request, DefaultTimeout, true);
  223. }
  224. else
  225. {
  226. s.Close();
  227. _allDone.Set();
  228. }
  229. }
  230. catch (Exception e)
  231. {
  232. _allDone.Set();
  233. _asyncException = e;
  234. }
  235. }
  236. #endregion Async communications with server
  237. /// <summary>
  238. /// Perform a synchronous request
  239. /// </summary>
  240. public Stream Request()
  241. {
  242. lock (_lock)
  243. {
  244. _request = (HttpWebRequest) WebRequest.Create(buildUri());
  245. _request.KeepAlive = false;
  246. _request.ContentType = "application/xml";
  247. _request.Timeout = 200000;
  248. _asyncException = null;
  249. // IAsyncResult responseAsyncResult = _request.BeginGetResponse(new AsyncCallback(ResponseIsReadyDelegate), _request);
  250. _response = (HttpWebResponse) _request.GetResponse();
  251. Stream src = _response.GetResponseStream();
  252. int length = src.Read(_readbuf, 0, BufferSize);
  253. while (length > 0)
  254. {
  255. _resource.Write(_readbuf, 0, length);
  256. length = src.Read(_readbuf, 0, BufferSize);
  257. }
  258. // TODO! Implement timeout, without killing the server
  259. // this line implements the timeout, if there is a timeout, the callback fires and the request becomes aborted
  260. //ThreadPool.RegisterWaitForSingleObject(responseAsyncResult.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallback), _request, DefaultTimeout, true);
  261. // _allDone.WaitOne();
  262. if (_response != null)
  263. _response.Close();
  264. if (_asyncException != null)
  265. throw _asyncException;
  266. if (_resource != null)
  267. {
  268. _resource.Flush();
  269. _resource.Seek(0, SeekOrigin.Begin);
  270. }
  271. return _resource;
  272. }
  273. }
  274. public Stream Request(Stream src)
  275. {
  276. _request = (HttpWebRequest) WebRequest.Create(buildUri());
  277. _request.KeepAlive = false;
  278. _request.ContentType = "application/xml";
  279. _request.Timeout = 900000;
  280. _request.Method = RequestMethod;
  281. _asyncException = null;
  282. _request.ContentLength = src.Length;
  283. m_log.InfoFormat("[REST]: Request Length {0}", _request.ContentLength);
  284. m_log.InfoFormat("[REST]: Sending Web Request {0}", buildUri());
  285. src.Seek(0, SeekOrigin.Begin);
  286. m_log.Info("[REST]: Seek is ok");
  287. Stream dst = _request.GetRequestStream();
  288. m_log.Info("[REST]: GetRequestStream is ok");
  289. byte[] buf = new byte[1024];
  290. int length = src.Read(buf, 0, 1024);
  291. m_log.Info("[REST]: First Read is ok");
  292. while (length > 0)
  293. {
  294. dst.Write(buf, 0, length);
  295. length = src.Read(buf, 0, 1024);
  296. }
  297. _response = (HttpWebResponse) _request.GetResponse();
  298. // IAsyncResult responseAsyncResult = _request.BeginGetResponse(new AsyncCallback(ResponseIsReadyDelegate), _request);
  299. // TODO! Implement timeout, without killing the server
  300. // this line implements the timeout, if there is a timeout, the callback fires and the request becomes aborted
  301. //ThreadPool.RegisterWaitForSingleObject(responseAsyncResult.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallback), _request, DefaultTimeout, true);
  302. return null;
  303. }
  304. #region Async Invocation
  305. public IAsyncResult BeginRequest(AsyncCallback callback, object state)
  306. {
  307. /// <summary>
  308. /// In case, we are invoked asynchroneously this object will keep track of the state
  309. /// </summary>
  310. AsyncResult<Stream> ar = new AsyncResult<Stream>(callback, state);
  311. ThreadPool.QueueUserWorkItem(RequestHelper, ar);
  312. return ar;
  313. }
  314. public Stream EndRequest(IAsyncResult asyncResult)
  315. {
  316. AsyncResult<Stream> ar = (AsyncResult<Stream>) asyncResult;
  317. // Wait for operation to complete, then return result or
  318. // throw exception
  319. return ar.EndInvoke();
  320. }
  321. private void RequestHelper(Object asyncResult)
  322. {
  323. // We know that it's really an AsyncResult<DateTime> object
  324. AsyncResult<Stream> ar = (AsyncResult<Stream>) asyncResult;
  325. try
  326. {
  327. // Perform the operation; if sucessful set the result
  328. Stream s = Request();
  329. ar.SetAsCompleted(s, false);
  330. }
  331. catch (Exception e)
  332. {
  333. // If operation fails, set the exception
  334. ar.HandleException(e, false);
  335. }
  336. }
  337. #endregion Async Invocation
  338. }
  339. }