1
0

RestClient.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. s.BeginRead(_readbuf, 0, BufferSize, new AsyncCallback(StreamIsReadyDelegate), s);
  222. // TODO! Implement timeout, without killing the server
  223. //ThreadPool.RegisterWaitForSingleObject(asynchronousResult.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallback), _request, DefaultTimeout, true);
  224. }
  225. else
  226. {
  227. s.Close();
  228. _allDone.Set();
  229. }
  230. }
  231. catch (Exception e)
  232. {
  233. _allDone.Set();
  234. _asyncException = e;
  235. }
  236. }
  237. #endregion Async communications with server
  238. /// <summary>
  239. /// Perform a synchronous request
  240. /// </summary>
  241. public Stream Request()
  242. {
  243. lock (_lock)
  244. {
  245. _request = (HttpWebRequest) WebRequest.Create(buildUri());
  246. _request.KeepAlive = false;
  247. _request.ContentType = "application/xml";
  248. _request.Timeout = 200000;
  249. _request.Method = RequestMethod;
  250. _asyncException = null;
  251. // IAsyncResult responseAsyncResult = _request.BeginGetResponse(new AsyncCallback(ResponseIsReadyDelegate), _request);
  252. try
  253. {
  254. _response = (HttpWebResponse) _request.GetResponse();
  255. }
  256. catch (WebException e)
  257. {
  258. m_log.ErrorFormat("[ASSET] Error fetching asset from asset server");
  259. m_log.Debug(e.ToString());
  260. return null;
  261. }
  262. Stream src = _response.GetResponseStream();
  263. int length = src.Read(_readbuf, 0, BufferSize);
  264. while (length > 0)
  265. {
  266. _resource.Write(_readbuf, 0, length);
  267. length = src.Read(_readbuf, 0, BufferSize);
  268. }
  269. // TODO! Implement timeout, without killing the server
  270. // this line implements the timeout, if there is a timeout, the callback fires and the request becomes aborted
  271. //ThreadPool.RegisterWaitForSingleObject(responseAsyncResult.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallback), _request, DefaultTimeout, true);
  272. // _allDone.WaitOne();
  273. if (_response != null)
  274. _response.Close();
  275. if (_asyncException != null)
  276. throw _asyncException;
  277. if (_resource != null)
  278. {
  279. _resource.Flush();
  280. _resource.Seek(0, SeekOrigin.Begin);
  281. }
  282. return _resource;
  283. }
  284. }
  285. public Stream Request(Stream src)
  286. {
  287. _request = (HttpWebRequest) WebRequest.Create(buildUri());
  288. _request.KeepAlive = false;
  289. _request.ContentType = "application/xml";
  290. _request.Timeout = 900000;
  291. _request.Method = RequestMethod;
  292. _asyncException = null;
  293. _request.ContentLength = src.Length;
  294. m_log.InfoFormat("[REST]: Request Length {0}", _request.ContentLength);
  295. m_log.InfoFormat("[REST]: Sending Web Request {0}", buildUri());
  296. src.Seek(0, SeekOrigin.Begin);
  297. m_log.Info("[REST]: Seek is ok");
  298. Stream dst = _request.GetRequestStream();
  299. m_log.Info("[REST]: GetRequestStream is ok");
  300. byte[] buf = new byte[1024];
  301. int length = src.Read(buf, 0, 1024);
  302. m_log.Info("[REST]: First Read is ok");
  303. while (length > 0)
  304. {
  305. dst.Write(buf, 0, length);
  306. length = src.Read(buf, 0, 1024);
  307. }
  308. _response = (HttpWebResponse) _request.GetResponse();
  309. // IAsyncResult responseAsyncResult = _request.BeginGetResponse(new AsyncCallback(ResponseIsReadyDelegate), _request);
  310. // TODO! Implement timeout, without killing the server
  311. // this line implements the timeout, if there is a timeout, the callback fires and the request becomes aborted
  312. //ThreadPool.RegisterWaitForSingleObject(responseAsyncResult.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallback), _request, DefaultTimeout, true);
  313. return null;
  314. }
  315. #region Async Invocation
  316. public IAsyncResult BeginRequest(AsyncCallback callback, object state)
  317. {
  318. /// <summary>
  319. /// In case, we are invoked asynchroneously this object will keep track of the state
  320. /// </summary>
  321. AsyncResult<Stream> ar = new AsyncResult<Stream>(callback, state);
  322. ThreadPool.QueueUserWorkItem(RequestHelper, ar);
  323. return ar;
  324. }
  325. public Stream EndRequest(IAsyncResult asyncResult)
  326. {
  327. AsyncResult<Stream> ar = (AsyncResult<Stream>) asyncResult;
  328. // Wait for operation to complete, then return result or
  329. // throw exception
  330. return ar.EndInvoke();
  331. }
  332. private void RequestHelper(Object asyncResult)
  333. {
  334. // We know that it's really an AsyncResult<DateTime> object
  335. AsyncResult<Stream> ar = (AsyncResult<Stream>) asyncResult;
  336. try
  337. {
  338. // Perform the operation; if sucessful set the result
  339. Stream s = Request();
  340. ar.SetAsCompleted(s, false);
  341. }
  342. catch (Exception e)
  343. {
  344. // If operation fails, set the exception
  345. ar.HandleException(e, false);
  346. }
  347. }
  348. #endregion Async Invocation
  349. }
  350. }