EstateDataRobustConnector.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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 == String.Empty)
  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. UUID regionID = UUID.Zero;
  168. if (UUID.TryParse(region, out regionID))
  169. {
  170. string create = (string)httpRequest.Query["create"];
  171. bool createYN = false;
  172. Boolean.TryParse(create, out createYN);
  173. estate = m_EstateService.LoadEstateSettings(regionID, createYN);
  174. }
  175. }
  176. else if (!string.IsNullOrEmpty(eid))
  177. {
  178. int id = 0;
  179. if (Int32.TryParse(eid, out id))
  180. estate = m_EstateService.LoadEstateSettings(id);
  181. }
  182. if (estate != null)
  183. {
  184. httpResponse.StatusCode = (int)HttpStatusCode.OK;
  185. httpResponse.ContentType = "text/xml";
  186. data = estate.ToMap();
  187. }
  188. else
  189. httpResponse.StatusCode = (int)HttpStatusCode.NotFound;
  190. return data;
  191. }
  192. private Dictionary<string, object> GetRegions(IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
  193. {
  194. // /estates/regions/?eid=int
  195. Dictionary<string, object> data = null;
  196. string eid = (string)httpRequest.Query["eid"];
  197. httpResponse.StatusCode = (int)HttpStatusCode.NotFound;
  198. if (!string.IsNullOrEmpty(eid))
  199. {
  200. int id = 0;
  201. if (Int32.TryParse(eid, out id))
  202. {
  203. List<UUID> regions = m_EstateService.GetRegions(id);
  204. if (regions != null && regions.Count > 0)
  205. {
  206. data = new Dictionary<string, object>();
  207. int i = 0;
  208. foreach (UUID uuid in regions)
  209. data["region" + i++] = uuid.ToString();
  210. httpResponse.StatusCode = (int)HttpStatusCode.OK;
  211. httpResponse.ContentType = "text/xml";
  212. }
  213. }
  214. }
  215. return data;
  216. }
  217. }
  218. public class EstateServerPostHandler : BaseStreamHandler
  219. {
  220. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  221. IEstateDataService m_EstateService;
  222. // Possibilities
  223. // /estates/estate/ (post an estate)
  224. // /estates/estate/?eid=int&region=uuid (link a region to an estate)
  225. public EstateServerPostHandler(IEstateDataService service, IServiceAuth auth) :
  226. base("POST", "/estates", auth)
  227. {
  228. m_EstateService = service;
  229. }
  230. protected override byte[] ProcessRequest(string path, Stream request,
  231. IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
  232. {
  233. Dictionary<string, object> data = null;
  234. string[] p = SplitParams(path);
  235. if (p.Length > 0)
  236. {
  237. string resource = p[0];
  238. // /estates/estate/
  239. // /estates/estate/?eid=int&region=uuid
  240. if ("estate".Equals(resource))
  241. {
  242. string body;
  243. using(StreamReader sr = new StreamReader(request))
  244. body = sr.ReadToEnd();
  245. body = body.Trim();
  246. Dictionary<string, object> requestData = ServerUtils.ParseQueryString(body);
  247. data = UpdateEstate(requestData, httpRequest, httpResponse);
  248. }
  249. }
  250. if (data == null)
  251. data = new Dictionary<string, object>();
  252. string xmlString = ServerUtils.BuildXmlResponse(data);
  253. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  254. }
  255. private Dictionary<string, object> UpdateEstate(Dictionary<string, object> requestData, IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
  256. {
  257. // /estates/estate/
  258. // /estates/estate/?eid=int&region=uuid
  259. Dictionary<string, object> result = new Dictionary<string, object>();
  260. string eid = (string)httpRequest.Query["eid"];
  261. string region = (string)httpRequest.Query["region"];
  262. httpResponse.StatusCode = (int)HttpStatusCode.NotFound;
  263. if (string.IsNullOrEmpty(eid) && string.IsNullOrEmpty(region) &&
  264. requestData.ContainsKey("OP") && requestData["OP"] != null && "STORE".Equals(requestData["OP"]))
  265. {
  266. // /estates/estate/
  267. EstateSettings es = new EstateSettings(requestData);
  268. m_EstateService.StoreEstateSettings(es);
  269. //m_log.DebugFormat("[EstateServerPostHandler]: Store estate {0}", es.ToString());
  270. httpResponse.StatusCode = (int)HttpStatusCode.OK;
  271. result["Result"] = true;
  272. }
  273. else if (!string.IsNullOrEmpty(region) && !string.IsNullOrEmpty(eid) &&
  274. requestData.ContainsKey("OP") && requestData["OP"] != null && "LINK".Equals(requestData["OP"]))
  275. {
  276. int id = 0;
  277. UUID regionID = UUID.Zero;
  278. if (UUID.TryParse(region, out regionID) && Int32.TryParse(eid, out id))
  279. {
  280. m_log.DebugFormat("[EstateServerPostHandler]: Link region {0} to estate {1}", regionID, id);
  281. httpResponse.StatusCode = (int)HttpStatusCode.OK;
  282. result["Result"] = m_EstateService.LinkRegion(regionID, id);
  283. }
  284. }
  285. else
  286. m_log.WarnFormat("[EstateServerPostHandler]: something wrong with POST request {0}", httpRequest.RawUrl);
  287. return result;
  288. }
  289. }
  290. }