RestClient.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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 OpenSimulator 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. try
  145. {
  146. _parameterElements.Add(HttpUtility.UrlEncode(name), HttpUtility.UrlEncode(value));
  147. }
  148. catch (ArgumentException)
  149. {
  150. m_log.Error("[REST]: Query parameter " + name + " is already added.");
  151. }
  152. catch (Exception e)
  153. {
  154. m_log.Error("[REST]: An exception was raised adding query parameter to dictionary. Exception: {0}",e);
  155. }
  156. }
  157. /// <summary>
  158. /// Add a query parameter to the Url
  159. /// </summary>
  160. /// <param name="name">Name of the parameter, e.g. min</param>
  161. public void AddQueryParameter(string name)
  162. {
  163. try
  164. {
  165. _parameterElements.Add(HttpUtility.UrlEncode(name), null);
  166. }
  167. catch (ArgumentException)
  168. {
  169. m_log.Error("[REST]: Query parameter " + name + " is already added.");
  170. }
  171. catch (Exception e)
  172. {
  173. m_log.Error("[REST]: An exception was raised adding query parameter to dictionary. Exception: {0}",e);
  174. }
  175. }
  176. /// <summary>
  177. /// Web-Request method, e.g. GET, PUT, POST, DELETE
  178. /// </summary>
  179. public string RequestMethod
  180. {
  181. get { return _method; }
  182. set { _method = value; }
  183. }
  184. /// <summary>
  185. /// True if string contains a trailing slash '/'
  186. /// </summary>
  187. /// <param name="s">string to be examined</param>
  188. /// <returns>true if slash is present</returns>
  189. private static bool isSlashed(string s)
  190. {
  191. return s.Substring(s.Length - 1, 1) == "/";
  192. }
  193. /// <summary>
  194. /// Build a Uri based on the initial Url, path elements and parameters
  195. /// </summary>
  196. /// <returns>fully constructed Uri</returns>
  197. private Uri buildUri()
  198. {
  199. StringBuilder sb = new StringBuilder();
  200. sb.Append(_url);
  201. foreach (string e in _pathElements)
  202. {
  203. sb.Append("/");
  204. sb.Append(e);
  205. }
  206. bool firstElement = true;
  207. foreach (KeyValuePair<string, string> kv in _parameterElements)
  208. {
  209. if (firstElement)
  210. {
  211. sb.Append("?");
  212. firstElement = false;
  213. }
  214. else
  215. sb.Append("&");
  216. sb.Append(kv.Key);
  217. if (!string.IsNullOrEmpty(kv.Value))
  218. {
  219. sb.Append("=");
  220. sb.Append(kv.Value);
  221. }
  222. }
  223. // realuri = sb.ToString();
  224. //m_log.InfoFormat("[REST CLIENT]: RestURL: {0}", realuri);
  225. return new Uri(sb.ToString());
  226. }
  227. #region Async communications with server
  228. /// <summary>
  229. /// Async method, invoked when a block of data has been received from the service
  230. /// </summary>
  231. /// <param name="ar"></param>
  232. private void StreamIsReadyDelegate(IAsyncResult ar)
  233. {
  234. try
  235. {
  236. Stream s = (Stream) ar.AsyncState;
  237. int read = s.EndRead(ar);
  238. if (read > 0)
  239. {
  240. _resource.Write(_readbuf, 0, read);
  241. // IAsyncResult asynchronousResult =
  242. // s.BeginRead(_readbuf, 0, BufferSize, new AsyncCallback(StreamIsReadyDelegate), s);
  243. s.BeginRead(_readbuf, 0, BufferSize, new AsyncCallback(StreamIsReadyDelegate), s);
  244. // TODO! Implement timeout, without killing the server
  245. //ThreadPool.RegisterWaitForSingleObject(asynchronousResult.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallback), _request, DefaultTimeout, true);
  246. }
  247. else
  248. {
  249. s.Close();
  250. _allDone.Set();
  251. }
  252. }
  253. catch (Exception e)
  254. {
  255. _allDone.Set();
  256. _asyncException = e;
  257. }
  258. }
  259. #endregion Async communications with server
  260. /// <summary>
  261. /// Perform a synchronous request
  262. /// </summary>
  263. public Stream Request()
  264. {
  265. lock (_lock)
  266. {
  267. _request = (HttpWebRequest) WebRequest.Create(buildUri());
  268. _request.KeepAlive = false;
  269. _request.ContentType = "application/xml";
  270. _request.Timeout = 200000;
  271. _request.Method = RequestMethod;
  272. _asyncException = null;
  273. // IAsyncResult responseAsyncResult = _request.BeginGetResponse(new AsyncCallback(ResponseIsReadyDelegate), _request);
  274. try
  275. {
  276. _response = (HttpWebResponse) _request.GetResponse();
  277. }
  278. catch (WebException e)
  279. {
  280. HttpWebResponse errorResponse = e.Response as HttpWebResponse;
  281. if (null != errorResponse && HttpStatusCode.NotFound == errorResponse.StatusCode)
  282. {
  283. m_log.Warn("[ASSET] Asset not found (404)");
  284. }
  285. else
  286. {
  287. m_log.Error("[ASSET] Error fetching asset from asset server");
  288. m_log.Debug(e.ToString());
  289. }
  290. return null;
  291. }
  292. Stream src = _response.GetResponseStream();
  293. int length = src.Read(_readbuf, 0, BufferSize);
  294. while (length > 0)
  295. {
  296. _resource.Write(_readbuf, 0, length);
  297. length = src.Read(_readbuf, 0, BufferSize);
  298. }
  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. // _allDone.WaitOne();
  303. if (_response != null)
  304. _response.Close();
  305. if (_asyncException != null)
  306. throw _asyncException;
  307. if (_resource != null)
  308. {
  309. _resource.Flush();
  310. _resource.Seek(0, SeekOrigin.Begin);
  311. }
  312. return _resource;
  313. }
  314. }
  315. public Stream Request(Stream src)
  316. {
  317. _request = (HttpWebRequest) WebRequest.Create(buildUri());
  318. _request.KeepAlive = false;
  319. _request.ContentType = "application/xml";
  320. _request.Timeout = 900000;
  321. _request.Method = RequestMethod;
  322. _asyncException = null;
  323. _request.ContentLength = src.Length;
  324. m_log.InfoFormat("[REST]: Request Length {0}", _request.ContentLength);
  325. m_log.InfoFormat("[REST]: Sending Web Request {0}", buildUri());
  326. src.Seek(0, SeekOrigin.Begin);
  327. m_log.Info("[REST]: Seek is ok");
  328. Stream dst = _request.GetRequestStream();
  329. m_log.Info("[REST]: GetRequestStream is ok");
  330. byte[] buf = new byte[1024];
  331. int length = src.Read(buf, 0, 1024);
  332. m_log.Info("[REST]: First Read is ok");
  333. while (length > 0)
  334. {
  335. dst.Write(buf, 0, length);
  336. length = src.Read(buf, 0, 1024);
  337. }
  338. _response = (HttpWebResponse) _request.GetResponse();
  339. // IAsyncResult responseAsyncResult = _request.BeginGetResponse(new AsyncCallback(ResponseIsReadyDelegate), _request);
  340. // TODO! Implement timeout, without killing the server
  341. // this line implements the timeout, if there is a timeout, the callback fires and the request becomes aborted
  342. //ThreadPool.RegisterWaitForSingleObject(responseAsyncResult.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallback), _request, DefaultTimeout, true);
  343. return null;
  344. }
  345. #region Async Invocation
  346. public IAsyncResult BeginRequest(AsyncCallback callback, object state)
  347. {
  348. /// <summary>
  349. /// In case, we are invoked asynchroneously this object will keep track of the state
  350. /// </summary>
  351. AsyncResult<Stream> ar = new AsyncResult<Stream>(callback, state);
  352. ThreadPool.QueueUserWorkItem(RequestHelper, ar);
  353. return ar;
  354. }
  355. public Stream EndRequest(IAsyncResult asyncResult)
  356. {
  357. AsyncResult<Stream> ar = (AsyncResult<Stream>) asyncResult;
  358. // Wait for operation to complete, then return result or
  359. // throw exception
  360. return ar.EndInvoke();
  361. }
  362. private void RequestHelper(Object asyncResult)
  363. {
  364. // We know that it's really an AsyncResult<DateTime> object
  365. AsyncResult<Stream> ar = (AsyncResult<Stream>) asyncResult;
  366. try
  367. {
  368. // Perform the operation; if sucessful set the result
  369. Stream s = Request();
  370. ar.SetAsCompleted(s, false);
  371. }
  372. catch (Exception e)
  373. {
  374. // If operation fails, set the exception
  375. ar.HandleException(e, false);
  376. }
  377. }
  378. #endregion Async Invocation
  379. }
  380. }