EstateDataConnector.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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.Net;
  30. using System.Reflection;
  31. using log4net;
  32. using OpenMetaverse;
  33. using Nini.Config;
  34. using OpenSim.Framework;
  35. using OpenSim.Framework.ServiceAuth;
  36. using OpenSim.Services.Connectors;
  37. using OpenSim.Services.Interfaces;
  38. using OpenSim.Server.Base;
  39. namespace OpenSim.Services.Connectors
  40. {
  41. public class EstateDataRemoteConnector : BaseServiceConnector, IEstateDataService
  42. {
  43. private static readonly ILog m_log =
  44. LogManager.GetLogger(
  45. MethodBase.GetCurrentMethod().DeclaringType);
  46. private string m_ServerURI = String.Empty;
  47. private ExpiringCache<string, List<EstateSettings>> m_EstateCache = new ExpiringCache<string, List<EstateSettings>>();
  48. private const int EXPIRATION = 5 * 60; // 5 minutes in secs
  49. public EstateDataRemoteConnector(IConfigSource source)
  50. {
  51. Initialise(source);
  52. }
  53. public virtual void Initialise(IConfigSource source)
  54. {
  55. IConfig gridConfig = source.Configs["EstateService"];
  56. if (gridConfig == null)
  57. {
  58. m_log.Error("[ESTATE CONNECTOR]: EstateService missing from OpenSim.ini");
  59. throw new Exception("Estate connector init error");
  60. }
  61. string serviceURI = gridConfig.GetString("EstateServerURI",
  62. String.Empty);
  63. if (serviceURI == String.Empty)
  64. {
  65. m_log.Error("[ESTATE CONNECTOR]: No Server URI named in section EstateService");
  66. throw new Exception("Estate connector init error");
  67. }
  68. m_ServerURI = serviceURI;
  69. base.Initialise(source, "EstateService");
  70. }
  71. #region IEstateDataService
  72. public List<EstateSettings> LoadEstateSettingsAll()
  73. {
  74. string reply = string.Empty;
  75. string uri = m_ServerURI + "/estates";
  76. reply = MakeRequest("GET", uri, string.Empty);
  77. if (String.IsNullOrEmpty(reply))
  78. return new List<EstateSettings>();
  79. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  80. List<EstateSettings> estates = new List<EstateSettings>();
  81. if (replyData != null && replyData.Count > 0)
  82. {
  83. m_log.DebugFormat("[ESTATE CONNECTOR]: LoadEstateSettingsAll returned {0} elements", replyData.Count);
  84. Dictionary<string, object>.ValueCollection estateData = replyData.Values;
  85. foreach (object r in estateData)
  86. {
  87. if (r is Dictionary<string, object>)
  88. {
  89. EstateSettings es = new EstateSettings((Dictionary<string, object>)r);
  90. estates.Add(es);
  91. }
  92. }
  93. m_EstateCache.AddOrUpdate("estates", estates, EXPIRATION);
  94. }
  95. else
  96. m_log.DebugFormat("[ESTATE CONNECTOR]: LoadEstateSettingsAll from {0} received null or zero response", uri);
  97. return estates;
  98. }
  99. public List<int> GetEstatesAll()
  100. {
  101. List<int> eids = new List<int>();
  102. // If we don't have them, load them from the server
  103. List<EstateSettings> estates = null;
  104. if (!m_EstateCache.TryGetValue("estates", out estates))
  105. estates = LoadEstateSettingsAll();
  106. foreach (EstateSettings es in estates)
  107. eids.Add((int)es.EstateID);
  108. return eids;
  109. }
  110. public List<int> GetEstates(string search)
  111. {
  112. // If we don't have them, load them from the server
  113. List<EstateSettings> estates = null;
  114. if (!m_EstateCache.TryGetValue("estates", out estates))
  115. estates = LoadEstateSettingsAll();
  116. List<int> eids = new List<int>();
  117. foreach (EstateSettings es in estates)
  118. if (es.EstateName == search)
  119. eids.Add((int)es.EstateID);
  120. return eids;
  121. }
  122. public List<int> GetEstatesByOwner(UUID ownerID)
  123. {
  124. // If we don't have them, load them from the server
  125. List<EstateSettings> estates = null;
  126. if (!m_EstateCache.TryGetValue("estates", out estates))
  127. estates = LoadEstateSettingsAll();
  128. List<int> eids = new List<int>();
  129. foreach (EstateSettings es in estates)
  130. if (es.EstateOwner == ownerID)
  131. eids.Add((int)es.EstateID);
  132. return eids;
  133. }
  134. public List<UUID> GetRegions(int estateID)
  135. {
  136. string reply = string.Empty;
  137. // /estates/regions/?eid=int
  138. string uri = m_ServerURI + "/estates/regions/?eid=" + estateID.ToString();
  139. reply = MakeRequest("GET", uri, string.Empty);
  140. if (String.IsNullOrEmpty(reply))
  141. return new List<UUID>();
  142. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  143. List<UUID> regions = new List<UUID>();
  144. if (replyData != null && replyData.Count > 0)
  145. {
  146. m_log.DebugFormat("[ESTATE CONNECTOR]: GetRegions for estate {0} returned {1} elements", estateID, replyData.Count);
  147. Dictionary<string, object>.ValueCollection data = replyData.Values;
  148. foreach (object r in data)
  149. {
  150. UUID uuid = UUID.Zero;
  151. if (UUID.TryParse(r.ToString(), out uuid))
  152. regions.Add(uuid);
  153. }
  154. }
  155. else
  156. m_log.DebugFormat("[ESTATE CONNECTOR]: GetRegions from {0} received null or zero response", uri);
  157. return regions;
  158. }
  159. public EstateSettings LoadEstateSettings(UUID regionID, bool create)
  160. {
  161. string reply = string.Empty;
  162. // /estates/estate/?region=uuid&create=[t|f]
  163. string uri = m_ServerURI + string.Format("/estates/estate/?region={0}&create={1}", regionID, create);
  164. reply = MakeRequest("GET", uri, string.Empty);
  165. if (String.IsNullOrEmpty(reply))
  166. return null;
  167. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  168. if (replyData != null && replyData.Count > 0)
  169. {
  170. m_log.DebugFormat("[ESTATE CONNECTOR]: LoadEstateSettings({0}) returned {1} elements", regionID, replyData.Count);
  171. EstateSettings es = new EstateSettings(replyData);
  172. return es;
  173. }
  174. else
  175. m_log.DebugFormat("[ESTATE CONNECTOR]: LoadEstateSettings(regionID) from {0} received null or zero response", uri);
  176. return null;
  177. }
  178. public EstateSettings LoadEstateSettings(int estateID)
  179. {
  180. string reply = string.Empty;
  181. // /estates/estate/?eid=int
  182. string uri = m_ServerURI + string.Format("/estates/estate/?eid={0}", estateID);
  183. reply = MakeRequest("GET", uri, string.Empty);
  184. if (String.IsNullOrEmpty(reply))
  185. return null;
  186. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  187. if (replyData != null && replyData.Count > 0)
  188. {
  189. m_log.DebugFormat("[ESTATE CONNECTOR]: LoadEstateSettings({0}) returned {1} elements", estateID, replyData.Count);
  190. EstateSettings es = new EstateSettings(replyData);
  191. return es;
  192. }
  193. else
  194. m_log.DebugFormat("[ESTATE CONNECTOR]: LoadEstateSettings(estateID) from {0} received null or zero response", uri);
  195. return null;
  196. }
  197. /// <summary>
  198. /// Forbidden operation
  199. /// </summary>
  200. /// <returns></returns>
  201. public EstateSettings CreateNewEstate()
  202. {
  203. // No can do
  204. return null;
  205. }
  206. public void StoreEstateSettings(EstateSettings es)
  207. {
  208. // /estates/estate/
  209. string uri = m_ServerURI + ("/estates/estate");
  210. Dictionary<string, object> formdata = es.ToMap();
  211. formdata["OP"] = "STORE";
  212. PostRequest(uri, formdata);
  213. }
  214. public bool LinkRegion(UUID regionID, int estateID)
  215. {
  216. // /estates/estate/?eid=int&region=uuid
  217. string uri = m_ServerURI + String.Format("/estates/estate/?eid={0}&region={1}", estateID, regionID);
  218. Dictionary<string, object> formdata = new Dictionary<string, object>();
  219. formdata["OP"] = "LINK";
  220. return PostRequest(uri, formdata);
  221. }
  222. private bool PostRequest(string uri, Dictionary<string, object> sendData)
  223. {
  224. string reqString = ServerUtils.BuildQueryString(sendData);
  225. string reply = MakeRequest("POST", uri, reqString);
  226. if (String.IsNullOrEmpty(reply))
  227. return false;
  228. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  229. bool result = false;
  230. if (replyData != null && replyData.Count > 0)
  231. {
  232. if (replyData.ContainsKey("Result"))
  233. {
  234. if (Boolean.TryParse(replyData["Result"].ToString(), out result))
  235. m_log.DebugFormat("[ESTATE CONNECTOR]: PostRequest {0} returned {1}", uri, result);
  236. }
  237. }
  238. else
  239. m_log.DebugFormat("[ESTATE CONNECTOR]: PostRequest {0} received null or zero response", uri);
  240. return result;
  241. }
  242. /// <summary>
  243. /// Forbidden operation
  244. /// </summary>
  245. /// <returns></returns>
  246. public bool DeleteEstate(int estateID)
  247. {
  248. return false;
  249. }
  250. #endregion
  251. private string MakeRequest(string verb, string uri, string formdata)
  252. {
  253. string reply = string.Empty;
  254. try
  255. {
  256. reply = SynchronousRestFormsRequester.MakeRequest(verb, uri, formdata, m_Auth);
  257. }
  258. catch (WebException e)
  259. {
  260. using (HttpWebResponse hwr = (HttpWebResponse)e.Response)
  261. {
  262. if (hwr != null)
  263. {
  264. if (hwr.StatusCode == HttpStatusCode.NotFound)
  265. m_log.Error(string.Format("[ESTATE CONNECTOR]: Resource {0} not found ", uri));
  266. if (hwr.StatusCode == HttpStatusCode.Unauthorized)
  267. m_log.Error(string.Format("[ESTATE CONNECTOR]: Web request {0} requires authentication ", uri));
  268. }
  269. else
  270. m_log.Error(string.Format(
  271. "[ESTATE CONNECTOR]: WebException for {0} {1} {2} {3}",
  272. verb, uri, formdata, e));
  273. }
  274. }
  275. catch (Exception e)
  276. {
  277. m_log.DebugFormat("[ESTATE CONNECTOR]: Exception when contacting estate server at {0}: {1}", uri, e.Message);
  278. }
  279. return reply;
  280. }
  281. }
  282. }