EstateDataRobustConnector.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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.Reflection;
  31. using System.Net;
  32. using Nini.Config;
  33. using log4net;
  34. using OpenMetaverse;
  35. using OpenSim.Server.Base;
  36. using OpenSim.Services.Interfaces;
  37. using OpenSim.Framework;
  38. using OpenSim.Framework.ServiceAuth;
  39. using OpenSim.Framework.Servers.HttpServer;
  40. using OpenSim.Server.Handlers.Base;
  41. namespace OpenSim.Server.Handlers
  42. {
  43. public class EstateDataRobustConnector : ServiceConnector
  44. {
  45. private string m_ConfigName = "EstateService";
  46. public EstateDataRobustConnector(IConfigSource config, IHttpServer server, string configName) :
  47. base(config, server, configName)
  48. {
  49. IConfig serverConfig = config.Configs[m_ConfigName];
  50. if (serverConfig == null)
  51. throw new Exception(String.Format("No section {0} in config file", m_ConfigName));
  52. string service = serverConfig.GetString("LocalServiceModule",
  53. String.Empty);
  54. if (service.Length == 0)
  55. throw new Exception("No LocalServiceModule in config file");
  56. Object[] args = new Object[] { config };
  57. IEstateDataService e_service = ServerUtils.LoadPlugin<IEstateDataService>(service, args);
  58. IServiceAuth auth = ServiceAuth.Create(config, m_ConfigName);
  59. server.AddStreamHandler(new EstateServerGetHandler(e_service, auth));
  60. server.AddStreamHandler(new EstateServerPostHandler(e_service, auth));
  61. }
  62. }
  63. public class EstateServerGetHandler : BaseStreamHandler
  64. {
  65. // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  66. IEstateDataService m_EstateService;
  67. // Possibilities
  68. // /estates/estate/?region=uuid&create=[t|f]
  69. // /estates/estate/?eid=int
  70. // /estates/?name=string
  71. // /estates/?owner=uuid
  72. // /estates/ (all)
  73. // /estates/regions/?eid=int
  74. public EstateServerGetHandler(IEstateDataService service, IServiceAuth auth) :
  75. base("GET", "/estates", auth)
  76. {
  77. m_EstateService = service;
  78. }
  79. protected override byte[] ProcessRequest(string path, Stream request,
  80. IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
  81. {
  82. Dictionary<string, object> data = null;
  83. string[] p = SplitParams(path);
  84. // /estates/ (all)
  85. // /estates/?name=string
  86. // /estates/?owner=uuid
  87. if (p.Length == 0)
  88. data = GetEstates(httpRequest, httpResponse);
  89. else
  90. {
  91. string resource = p[0];
  92. // /estates/estate/?region=uuid&create=[t|f]
  93. // /estates/estate/?eid=int
  94. if ("estate".Equals(resource))
  95. data = GetEstate(httpRequest, httpResponse);
  96. // /estates/regions/?eid=int
  97. else if ("regions".Equals(resource))
  98. data = GetRegions(httpRequest, httpResponse);
  99. }
  100. if (data == null)
  101. data = new Dictionary<string, object>();
  102. string xmlString = ServerUtils.BuildXmlResponse(data);
  103. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  104. }
  105. private Dictionary<string, object> GetEstates(IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
  106. {
  107. // /estates/ (all)
  108. // /estates/?name=string
  109. // /estates/?owner=uuid
  110. Dictionary<string, object> data = null;
  111. string name = (string)httpRequest.Query["name"];
  112. string owner = (string)httpRequest.Query["owner"];
  113. if (!string.IsNullOrEmpty(name) || !string.IsNullOrEmpty(owner))
  114. {
  115. List<int> estateIDs = null;
  116. if (!string.IsNullOrEmpty(name))
  117. {
  118. estateIDs = m_EstateService.GetEstates(name);
  119. }
  120. else if (!string.IsNullOrEmpty(owner))
  121. {
  122. UUID ownerID = UUID.Zero;
  123. if (UUID.TryParse(owner, out ownerID))
  124. estateIDs = m_EstateService.GetEstatesByOwner(ownerID);
  125. }
  126. if (estateIDs == null || (estateIDs != null && estateIDs.Count == 0))
  127. httpResponse.StatusCode = (int)HttpStatusCode.NotFound;
  128. else
  129. {
  130. httpResponse.StatusCode = (int)HttpStatusCode.OK;
  131. httpResponse.ContentType = "text/xml";
  132. data = new Dictionary<string, object>();
  133. int i = 0;
  134. foreach (int id in estateIDs)
  135. data["estate" + i++] = id;
  136. }
  137. }
  138. else
  139. {
  140. List<EstateSettings> estates = m_EstateService.LoadEstateSettingsAll();
  141. if (estates == null || estates.Count == 0)
  142. {
  143. httpResponse.StatusCode = (int)HttpStatusCode.NotFound;
  144. }
  145. else
  146. {
  147. httpResponse.StatusCode = (int)HttpStatusCode.OK;
  148. httpResponse.ContentType = "text/xml";
  149. data = new Dictionary<string, object>();
  150. int i = 0;
  151. foreach (EstateSettings es in estates)
  152. data["estate" + i++] = es.ToMap();
  153. }
  154. }
  155. return data;
  156. }
  157. private Dictionary<string, object> GetEstate(IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
  158. {
  159. // /estates/estate/?region=uuid&create=[t|f]
  160. // /estates/estate/?eid=int
  161. Dictionary<string, object> data = null;
  162. string region = (string)httpRequest.Query["region"];
  163. string eid = (string)httpRequest.Query["eid"];
  164. EstateSettings estate = null;
  165. if (!string.IsNullOrEmpty(region))
  166. {
  167. if (UUID.TryParse(region, out UUID regionID))
  168. {
  169. string create = (string)httpRequest.Query["create"];
  170. bool.TryParse(create, out bool createYN);
  171. estate = m_EstateService.LoadEstateSettings(regionID, createYN);
  172. }
  173. }
  174. else if (!string.IsNullOrEmpty(eid))
  175. {
  176. if (int.TryParse(eid, out int id))
  177. estate = m_EstateService.LoadEstateSettings(id);
  178. }
  179. if (estate != null)
  180. {
  181. httpResponse.StatusCode = (int)HttpStatusCode.OK;
  182. httpResponse.ContentType = "text/xml";
  183. data = estate.ToMap();
  184. }
  185. else
  186. httpResponse.StatusCode = (int)HttpStatusCode.NotFound;
  187. return data;
  188. }
  189. private Dictionary<string, object> GetRegions(IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
  190. {
  191. // /estates/regions/?eid=int
  192. Dictionary<string, object> data = null;
  193. string eid = (string)httpRequest.Query["eid"];
  194. httpResponse.StatusCode = (int)HttpStatusCode.NotFound;
  195. if (!string.IsNullOrEmpty(eid))
  196. {
  197. int id = 0;
  198. if (Int32.TryParse(eid, out id))
  199. {
  200. List<UUID> regions = m_EstateService.GetRegions(id);
  201. if (regions != null && regions.Count > 0)
  202. {
  203. data = new Dictionary<string, object>();
  204. int i = 0;
  205. foreach (UUID uuid in regions)
  206. data["region" + i++] = uuid.ToString();
  207. httpResponse.StatusCode = (int)HttpStatusCode.OK;
  208. httpResponse.ContentType = "text/xml";
  209. }
  210. }
  211. }
  212. return data;
  213. }
  214. }
  215. public class EstateServerPostHandler : BaseStreamHandler
  216. {
  217. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  218. IEstateDataService m_EstateService;
  219. // Possibilities
  220. // /estates/estate/ (post an estate)
  221. // /estates/estate/?eid=int&region=uuid (link a region to an estate)
  222. public EstateServerPostHandler(IEstateDataService service, IServiceAuth auth) :
  223. base("POST", "/estates", auth)
  224. {
  225. m_EstateService = service;
  226. }
  227. protected override byte[] ProcessRequest(string path, Stream request,
  228. IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
  229. {
  230. Dictionary<string, object> data = null;
  231. string[] p = SplitParams(path);
  232. if (p.Length > 0)
  233. {
  234. string resource = p[0];
  235. // /estates/estate/
  236. // /estates/estate/?eid=int&region=uuid
  237. if ("estate".Equals(resource))
  238. {
  239. string body;
  240. using(StreamReader sr = new StreamReader(request))
  241. body = sr.ReadToEnd();
  242. body = body.Trim();
  243. Dictionary<string, object> requestData = ServerUtils.ParseQueryString(body);
  244. data = UpdateEstate(requestData, httpRequest, httpResponse);
  245. }
  246. }
  247. if (data == null)
  248. data = new Dictionary<string, object>();
  249. string xmlString = ServerUtils.BuildXmlResponse(data);
  250. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  251. }
  252. private Dictionary<string, object> UpdateEstate(Dictionary<string, object> requestData, IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
  253. {
  254. // /estates/estate/
  255. // /estates/estate/?eid=int&region=uuid
  256. Dictionary<string, object> result = new Dictionary<string, object>();
  257. string eid = (string)httpRequest.Query["eid"];
  258. string region = (string)httpRequest.Query["region"];
  259. httpResponse.StatusCode = (int)HttpStatusCode.NotFound;
  260. if (string.IsNullOrEmpty(eid) && string.IsNullOrEmpty(region) &&
  261. requestData.ContainsKey("OP") && requestData["OP"] != null && "STORE".Equals(requestData["OP"]))
  262. {
  263. // /estates/estate/
  264. EstateSettings es = new EstateSettings(requestData);
  265. m_EstateService.StoreEstateSettings(es);
  266. //m_log.DebugFormat("[EstateServerPostHandler]: Store estate {0}", es.ToString());
  267. httpResponse.StatusCode = (int)HttpStatusCode.OK;
  268. result["Result"] = true;
  269. }
  270. else if (!string.IsNullOrEmpty(region) && !string.IsNullOrEmpty(eid) &&
  271. requestData.ContainsKey("OP") && requestData["OP"] != null && "LINK".Equals(requestData["OP"]))
  272. {
  273. int id = 0;
  274. UUID regionID = UUID.Zero;
  275. if (UUID.TryParse(region, out regionID) && Int32.TryParse(eid, out id))
  276. {
  277. m_log.DebugFormat("[EstateServerPostHandler]: Link region {0} to estate {1}", regionID, id);
  278. httpResponse.StatusCode = (int)HttpStatusCode.OK;
  279. result["Result"] = m_EstateService.LinkRegion(regionID, id);
  280. }
  281. }
  282. else
  283. m_log.WarnFormat("[EstateServerPostHandler]: something wrong with POST request {0}", httpRequest.RawUrl);
  284. return result;
  285. }
  286. }
  287. }