XInventoryInConnector.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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.Reflection;
  29. using System.Text;
  30. using System.Xml;
  31. using System.Collections.Generic;
  32. using System.IO;
  33. using Nini.Config;
  34. using OpenSim.Framework;
  35. using OpenSim.Server.Base;
  36. using OpenSim.Services.Interfaces;
  37. using OpenSim.Framework.Servers.HttpServer;
  38. using OpenSim.Server.Handlers.Base;
  39. using log4net;
  40. using OpenMetaverse;
  41. namespace OpenSim.Server.Handlers.Asset
  42. {
  43. public class XInventoryInConnector : ServiceConnector
  44. {
  45. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  46. private IInventoryService m_InventoryService;
  47. private string m_ConfigName = "InventoryService";
  48. public XInventoryInConnector(IConfigSource config, IHttpServer server, string configName) :
  49. base(config, server, configName)
  50. {
  51. if (configName != String.Empty)
  52. m_ConfigName = configName;
  53. IConfig serverConfig = config.Configs[m_ConfigName];
  54. if (serverConfig == null)
  55. throw new Exception(String.Format("No section '{0}' in config file", m_ConfigName));
  56. string inventoryService = serverConfig.GetString("LocalServiceModule",
  57. String.Empty);
  58. if (inventoryService == String.Empty)
  59. throw new Exception("No InventoryService in config file");
  60. Object[] args = new Object[] { config };
  61. m_InventoryService =
  62. ServerUtils.LoadPlugin<IInventoryService>(inventoryService, args);
  63. server.AddStreamHandler(new XInventoryConnectorPostHandler(m_InventoryService));
  64. }
  65. }
  66. public class XInventoryConnectorPostHandler : BaseStreamHandler
  67. {
  68. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  69. private IInventoryService m_InventoryService;
  70. public XInventoryConnectorPostHandler(IInventoryService service) :
  71. base("POST", "/xinventory")
  72. {
  73. m_InventoryService = service;
  74. }
  75. public override byte[] Handle(string path, Stream requestData,
  76. OSHttpRequest httpRequest, OSHttpResponse httpResponse)
  77. {
  78. StreamReader sr = new StreamReader(requestData);
  79. string body = sr.ReadToEnd();
  80. sr.Close();
  81. body = body.Trim();
  82. m_log.DebugFormat("[XXX]: query String: {0}", body);
  83. try
  84. {
  85. Dictionary<string, object> request =
  86. ServerUtils.ParseQueryString(body);
  87. if (!request.ContainsKey("METHOD"))
  88. return FailureResult();
  89. string method = request["METHOD"].ToString();
  90. request.Remove("METHOD");
  91. switch (method)
  92. {
  93. case "CREATEUSERINVENTORY":
  94. return HandleCreateUserInventory(request);
  95. case "GETINVENTORYSKELETON":
  96. return HandleGetInventorySkeleton(request);
  97. case "GETROOTFOLDER":
  98. return HandleGetRootFolder(request);
  99. case "GETFOLDERFORTYPE":
  100. return HandleGetFolderForType(request);
  101. case "GETFOLDERCONTENT":
  102. return HandleGetFolderContent(request);
  103. case "GETFOLDERITEMS":
  104. return HandleGetFolderItems(request);
  105. case "ADDFOLDER":
  106. return HandleAddFolder(request);
  107. case "UPDATEFOLDER":
  108. return HandleUpdateFolder(request);
  109. case "MOVEFOLDER":
  110. return HandleMoveFolder(request);
  111. case "DELETEFOLDERS":
  112. return HandleDeleteFolders(request);
  113. case "PURGEFOLDER":
  114. return HandlePurgeFolder(request);
  115. case "ADDITEM":
  116. return HandleAddItem(request);
  117. case "UPDATEITEM":
  118. return HandleUpdateItem(request);
  119. case "MOVEITEMS":
  120. return HandleMoveItems(request);
  121. case "DELETEITEMS":
  122. return HandleDeleteItems(request);
  123. case "GETITEM":
  124. return HandleGetItem(request);
  125. case "GETFOLDER":
  126. return HandleGetFolder(request);
  127. case "GETACTIVEGESTURES":
  128. return HandleGetActiveGestures(request);
  129. case "GETASSETPERMISSIONS":
  130. return HandleGetAssetPermissions(request);
  131. }
  132. m_log.DebugFormat("[XINVENTORY HANDLER]: unknown method request: {0}", method);
  133. }
  134. catch (Exception e)
  135. {
  136. m_log.Debug("[XINVENTORY HANDLER]: Exception {0}" + e);
  137. }
  138. return FailureResult();
  139. }
  140. private byte[] FailureResult()
  141. {
  142. XmlDocument doc = new XmlDocument();
  143. XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
  144. "", "");
  145. doc.AppendChild(xmlnode);
  146. XmlElement rootElement = doc.CreateElement("", "ServerResponse",
  147. "");
  148. doc.AppendChild(rootElement);
  149. XmlElement result = doc.CreateElement("", "RESULT", "");
  150. result.AppendChild(doc.CreateTextNode("False"));
  151. rootElement.AppendChild(result);
  152. return DocToBytes(doc);
  153. }
  154. private byte[] DocToBytes(XmlDocument doc)
  155. {
  156. MemoryStream ms = new MemoryStream();
  157. XmlTextWriter xw = new XmlTextWriter(ms, null);
  158. xw.Formatting = Formatting.Indented;
  159. doc.WriteTo(xw);
  160. xw.Flush();
  161. return ms.ToArray();
  162. }
  163. byte[] HandleCreateUserInventory(Dictionary<string,object> request)
  164. {
  165. Dictionary<string,object> result = new Dictionary<string,object>();
  166. if (!request.ContainsKey("PRINCIPAL"))
  167. return FailureResult();
  168. if(m_InventoryService.CreateUserInventory(new UUID(request["PRINCIPAL"].ToString())))
  169. result["RESULT"] = "True";
  170. else
  171. result["RESULT"] = "False";
  172. string xmlString = ServerUtils.BuildXmlResponse(result);
  173. m_log.DebugFormat("[XXX]: resp string: {0}", xmlString);
  174. UTF8Encoding encoding = new UTF8Encoding();
  175. return encoding.GetBytes(xmlString);
  176. }
  177. byte[] HandleGetInventorySkeleton(Dictionary<string,object> request)
  178. {
  179. Dictionary<string,object> result = new Dictionary<string,object>();
  180. if (!request.ContainsKey("PRINCIPAL"))
  181. return FailureResult();
  182. List<InventoryFolderBase> folders = m_InventoryService.GetInventorySkeleton(new UUID(request["PRINCIPAL"].ToString()));
  183. foreach (InventoryFolderBase f in folders)
  184. result[f.ID.ToString()] = EncodeFolder(f);
  185. string xmlString = ServerUtils.BuildXmlResponse(result);
  186. m_log.DebugFormat("[XXX]: resp string: {0}", xmlString);
  187. UTF8Encoding encoding = new UTF8Encoding();
  188. return encoding.GetBytes(xmlString);
  189. }
  190. byte[] HandleGetRootFolder(Dictionary<string,object> request)
  191. {
  192. Dictionary<string,object> result = new Dictionary<string,object>();
  193. string xmlString = ServerUtils.BuildXmlResponse(result);
  194. m_log.DebugFormat("[XXX]: resp string: {0}", xmlString);
  195. UTF8Encoding encoding = new UTF8Encoding();
  196. return encoding.GetBytes(xmlString);
  197. }
  198. byte[] HandleGetFolderForType(Dictionary<string,object> request)
  199. {
  200. Dictionary<string,object> result = new Dictionary<string,object>();
  201. string xmlString = ServerUtils.BuildXmlResponse(result);
  202. m_log.DebugFormat("[XXX]: resp string: {0}", xmlString);
  203. UTF8Encoding encoding = new UTF8Encoding();
  204. return encoding.GetBytes(xmlString);
  205. }
  206. byte[] HandleGetFolderContent(Dictionary<string,object> request)
  207. {
  208. Dictionary<string,object> result = new Dictionary<string,object>();
  209. string xmlString = ServerUtils.BuildXmlResponse(result);
  210. m_log.DebugFormat("[XXX]: resp string: {0}", xmlString);
  211. UTF8Encoding encoding = new UTF8Encoding();
  212. return encoding.GetBytes(xmlString);
  213. }
  214. byte[] HandleGetFolderItems(Dictionary<string,object> request)
  215. {
  216. Dictionary<string,object> result = new Dictionary<string,object>();
  217. string xmlString = ServerUtils.BuildXmlResponse(result);
  218. m_log.DebugFormat("[XXX]: resp string: {0}", xmlString);
  219. UTF8Encoding encoding = new UTF8Encoding();
  220. return encoding.GetBytes(xmlString);
  221. }
  222. byte[] HandleAddFolder(Dictionary<string,object> request)
  223. {
  224. Dictionary<string,object> result = new Dictionary<string,object>();
  225. string xmlString = ServerUtils.BuildXmlResponse(result);
  226. m_log.DebugFormat("[XXX]: resp string: {0}", xmlString);
  227. UTF8Encoding encoding = new UTF8Encoding();
  228. return encoding.GetBytes(xmlString);
  229. }
  230. byte[] HandleUpdateFolder(Dictionary<string,object> request)
  231. {
  232. Dictionary<string,object> result = new Dictionary<string,object>();
  233. string xmlString = ServerUtils.BuildXmlResponse(result);
  234. m_log.DebugFormat("[XXX]: resp string: {0}", xmlString);
  235. UTF8Encoding encoding = new UTF8Encoding();
  236. return encoding.GetBytes(xmlString);
  237. }
  238. byte[] HandleMoveFolder(Dictionary<string,object> request)
  239. {
  240. Dictionary<string,object> result = new Dictionary<string,object>();
  241. string xmlString = ServerUtils.BuildXmlResponse(result);
  242. m_log.DebugFormat("[XXX]: resp string: {0}", xmlString);
  243. UTF8Encoding encoding = new UTF8Encoding();
  244. return encoding.GetBytes(xmlString);
  245. }
  246. byte[] HandleDeleteFolders(Dictionary<string,object> request)
  247. {
  248. Dictionary<string,object> result = new Dictionary<string,object>();
  249. string xmlString = ServerUtils.BuildXmlResponse(result);
  250. m_log.DebugFormat("[XXX]: resp string: {0}", xmlString);
  251. UTF8Encoding encoding = new UTF8Encoding();
  252. return encoding.GetBytes(xmlString);
  253. }
  254. byte[] HandlePurgeFolder(Dictionary<string,object> request)
  255. {
  256. Dictionary<string,object> result = new Dictionary<string,object>();
  257. string xmlString = ServerUtils.BuildXmlResponse(result);
  258. m_log.DebugFormat("[XXX]: resp string: {0}", xmlString);
  259. UTF8Encoding encoding = new UTF8Encoding();
  260. return encoding.GetBytes(xmlString);
  261. }
  262. byte[] HandleAddItem(Dictionary<string,object> request)
  263. {
  264. Dictionary<string,object> result = new Dictionary<string,object>();
  265. string xmlString = ServerUtils.BuildXmlResponse(result);
  266. m_log.DebugFormat("[XXX]: resp string: {0}", xmlString);
  267. UTF8Encoding encoding = new UTF8Encoding();
  268. return encoding.GetBytes(xmlString);
  269. }
  270. byte[] HandleUpdateItem(Dictionary<string,object> request)
  271. {
  272. Dictionary<string,object> result = new Dictionary<string,object>();
  273. string xmlString = ServerUtils.BuildXmlResponse(result);
  274. m_log.DebugFormat("[XXX]: resp string: {0}", xmlString);
  275. UTF8Encoding encoding = new UTF8Encoding();
  276. return encoding.GetBytes(xmlString);
  277. }
  278. byte[] HandleMoveItems(Dictionary<string,object> request)
  279. {
  280. Dictionary<string,object> result = new Dictionary<string,object>();
  281. string xmlString = ServerUtils.BuildXmlResponse(result);
  282. m_log.DebugFormat("[XXX]: resp string: {0}", xmlString);
  283. UTF8Encoding encoding = new UTF8Encoding();
  284. return encoding.GetBytes(xmlString);
  285. }
  286. byte[] HandleDeleteItems(Dictionary<string,object> request)
  287. {
  288. Dictionary<string,object> result = new Dictionary<string,object>();
  289. string xmlString = ServerUtils.BuildXmlResponse(result);
  290. m_log.DebugFormat("[XXX]: resp string: {0}", xmlString);
  291. UTF8Encoding encoding = new UTF8Encoding();
  292. return encoding.GetBytes(xmlString);
  293. }
  294. byte[] HandleGetItem(Dictionary<string,object> request)
  295. {
  296. Dictionary<string,object> result = new Dictionary<string,object>();
  297. string xmlString = ServerUtils.BuildXmlResponse(result);
  298. m_log.DebugFormat("[XXX]: resp string: {0}", xmlString);
  299. UTF8Encoding encoding = new UTF8Encoding();
  300. return encoding.GetBytes(xmlString);
  301. }
  302. byte[] HandleGetFolder(Dictionary<string,object> request)
  303. {
  304. Dictionary<string,object> result = new Dictionary<string,object>();
  305. string xmlString = ServerUtils.BuildXmlResponse(result);
  306. m_log.DebugFormat("[XXX]: resp string: {0}", xmlString);
  307. UTF8Encoding encoding = new UTF8Encoding();
  308. return encoding.GetBytes(xmlString);
  309. }
  310. byte[] HandleGetActiveGestures(Dictionary<string,object> request)
  311. {
  312. Dictionary<string,object> result = new Dictionary<string,object>();
  313. string xmlString = ServerUtils.BuildXmlResponse(result);
  314. m_log.DebugFormat("[XXX]: resp string: {0}", xmlString);
  315. UTF8Encoding encoding = new UTF8Encoding();
  316. return encoding.GetBytes(xmlString);
  317. }
  318. byte[] HandleGetAssetPermissions(Dictionary<string,object> request)
  319. {
  320. Dictionary<string,object> result = new Dictionary<string,object>();
  321. string xmlString = ServerUtils.BuildXmlResponse(result);
  322. m_log.DebugFormat("[XXX]: resp string: {0}", xmlString);
  323. UTF8Encoding encoding = new UTF8Encoding();
  324. return encoding.GetBytes(xmlString);
  325. }
  326. private Dictionary<string, object> EncodeFolder(InventoryFolderBase f)
  327. {
  328. Dictionary<string, object> ret = new Dictionary<string, object>();
  329. ret["ParentID"] = f.ParentID.ToString();
  330. ret["Type"] = f.Type.ToString();
  331. ret["Version"] = f.Version.ToString();
  332. ret["Name"] = f.Name;
  333. ret["Owner"] = f.Owner.ToString();
  334. ret["ID"] = f.ID.ToString();
  335. return ret;
  336. }
  337. private InventoryFolderBase BuildFolder(Dictionary<string,object> data)
  338. {
  339. InventoryFolderBase folder = new InventoryFolderBase();
  340. folder.ParentID = new UUID(data["ParentID"].ToString());
  341. folder.Type = short.Parse(data["Type"].ToString());
  342. folder.Version = ushort.Parse(data["Version"].ToString());
  343. folder.Name = data["Name"].ToString();
  344. folder.Owner = new UUID(data["Owner"].ToString());
  345. folder.ID = new UUID(data["ID"].ToString());
  346. return folder;
  347. }
  348. private InventoryItemBase BuildItem(Dictionary<string,object> data)
  349. {
  350. InventoryItemBase item = new InventoryItemBase();
  351. item.AssetID = new UUID(data["AssetID"].ToString());
  352. item.AssetType = int.Parse(data["AssetType"].ToString());
  353. item.Name = data["Name"].ToString();
  354. item.Owner = new UUID(data["Owner"].ToString());
  355. item.ID = new UUID(data["ID"].ToString());
  356. item.InvType = int.Parse(data["InvType"].ToString());
  357. item.Folder = new UUID(data["Folder"].ToString());
  358. item.CreatorId = data["CreatorId"].ToString();
  359. item.Description = data["Description"].ToString();
  360. item.NextPermissions = uint.Parse(data["NextPermissions"].ToString());
  361. item.CurrentPermissions = uint.Parse(data["CurrentPermissions"].ToString());
  362. item.BasePermissions = uint.Parse(data["BasePermissions"].ToString());
  363. item.EveryOnePermissions = uint.Parse(data["EveryOnePermissions"].ToString());
  364. item.GroupPermissions = uint.Parse(data["GroupPermissions"].ToString());
  365. item.GroupID = new UUID(data["GroupID"].ToString());
  366. item.GroupOwned = bool.Parse(data["GroupOwned"].ToString());
  367. item.SalePrice = int.Parse(data["SalePrice"].ToString());
  368. item.SaleType = byte.Parse(data["SaleType"].ToString());
  369. item.Flags = uint.Parse(data["Flags"].ToString());
  370. item.CreationDate = int.Parse(data["CreationDate"].ToString());
  371. return item;
  372. }
  373. }
  374. }