1
0

EstateDataConnector.cs 12 KB

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