XInventoryConnector.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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 log4net;
  28. using System;
  29. using System.Collections.Generic;
  30. using System.IO;
  31. using System.Reflection;
  32. using Nini.Config;
  33. using OpenSim.Framework;
  34. using OpenSim.Framework.Console;
  35. using OpenSim.Framework.Communications;
  36. using OpenSim.Framework.Servers.HttpServer;
  37. using OpenSim.Services.Interfaces;
  38. using OpenSim.Server.Base;
  39. using OpenMetaverse;
  40. namespace OpenSim.Services.Connectors
  41. {
  42. public class XInventoryServicesConnector : IInventoryService
  43. {
  44. private static readonly ILog m_log =
  45. LogManager.GetLogger(
  46. MethodBase.GetCurrentMethod().DeclaringType);
  47. private string m_ServerURI = String.Empty;
  48. public XInventoryServicesConnector()
  49. {
  50. }
  51. public XInventoryServicesConnector(string serverURI)
  52. {
  53. m_ServerURI = serverURI.TrimEnd('/');
  54. }
  55. public XInventoryServicesConnector(IConfigSource source)
  56. {
  57. Initialise(source);
  58. }
  59. public virtual void Initialise(IConfigSource source)
  60. {
  61. IConfig assetConfig = source.Configs["InventoryService"];
  62. if (assetConfig == null)
  63. {
  64. m_log.Error("[INVENTORY CONNECTOR]: InventoryService missing from OpanSim.ini");
  65. throw new Exception("Inventory connector init error");
  66. }
  67. string serviceURI = assetConfig.GetString("InventoryServerURI",
  68. String.Empty);
  69. if (serviceURI == String.Empty)
  70. {
  71. m_log.Error("[INVENTORY CONNECTOR]: No Server URI named in section InventoryService");
  72. throw new Exception("Inventory connector init error");
  73. }
  74. m_ServerURI = serviceURI;
  75. }
  76. public bool CreateUserInventory(UUID principalID)
  77. {
  78. Dictionary<string,object> ret = MakeRequest("CREATEUSERINVENTORY",
  79. new Dictionary<string,object> {
  80. { "PRINCIPAL", principalID.ToString() }
  81. });
  82. if (ret == null)
  83. return false;
  84. return bool.Parse(ret["RESULT"].ToString());
  85. }
  86. public List<InventoryFolderBase> GetInventorySkeleton(UUID principalID)
  87. {
  88. Dictionary<string,object> ret = MakeRequest("GETINVENTORYSKELETON",
  89. new Dictionary<string,object> {
  90. { "PRINCIPAL", principalID.ToString() }
  91. });
  92. if (ret == null)
  93. return null;
  94. List<InventoryFolderBase> folders = new List<InventoryFolderBase>();
  95. foreach (Object o in ret.Values)
  96. folders.Add(BuildFolder((Dictionary<string,object>)o));
  97. return folders;
  98. }
  99. public InventoryFolderBase GetRootFolder(UUID principalID)
  100. {
  101. Dictionary<string,object> ret = MakeRequest("GETROOTFOLDER",
  102. new Dictionary<string,object> {
  103. { "PRINCIPAL", principalID.ToString() }
  104. });
  105. if (ret == null)
  106. return null;
  107. if (ret.Count == 0)
  108. return null;
  109. return BuildFolder(ret);
  110. }
  111. public InventoryFolderBase GetFolderForType(UUID principalID, AssetType type)
  112. {
  113. Dictionary<string,object> ret = MakeRequest("GETFOLDERFORTYPE",
  114. new Dictionary<string,object> {
  115. { "PRINCIPAL", principalID.ToString() },
  116. { "TYPE", ((int)type).ToString() }
  117. });
  118. if (ret == null)
  119. return null;
  120. if (ret.Count == 0)
  121. return null;
  122. return BuildFolder(ret);
  123. }
  124. public InventoryCollection GetFolderContent(UUID principalID, UUID folderID)
  125. {
  126. Dictionary<string,object> ret = MakeRequest("GETFOLDERCONTENT",
  127. new Dictionary<string,object> {
  128. { "PRINCIPAL", principalID.ToString() },
  129. { "FOLDER", folderID.ToString() }
  130. });
  131. if (ret == null)
  132. return null;
  133. if (ret.Count == 0)
  134. return null;
  135. InventoryCollection inventory = new InventoryCollection();
  136. inventory.Folders = new List<InventoryFolderBase>();
  137. inventory.Items = new List<InventoryItemBase>();
  138. inventory.UserID = principalID;
  139. Dictionary<string,object> folders =
  140. (Dictionary<string,object>)ret["FOLDERS"];
  141. Dictionary<string,object> items =
  142. (Dictionary<string,object>)ret["ITEMS"];
  143. foreach (Object o in folders.Values)
  144. inventory.Folders.Add(BuildFolder((Dictionary<string,object>)o));
  145. foreach (Object o in items.Values)
  146. inventory.Items.Add(BuildItem((Dictionary<string,object>)o));
  147. return inventory;
  148. }
  149. public List<InventoryItemBase> GetFolderItems(UUID principalID, UUID folderID)
  150. {
  151. Dictionary<string,object> ret = MakeRequest("GETFOLDERCONTENT",
  152. new Dictionary<string,object> {
  153. { "PRINCIPAL", principalID.ToString() },
  154. { "FOLDER", folderID.ToString() }
  155. });
  156. if (ret == null)
  157. return null;
  158. if (ret.Count == 0)
  159. return null;
  160. List<InventoryItemBase> items = new List<InventoryItemBase>();
  161. foreach (Object o in ret.Values)
  162. items.Add(BuildItem((Dictionary<string,object>)o));
  163. return items;
  164. }
  165. public bool AddFolder(InventoryFolderBase folder)
  166. {
  167. Dictionary<string,object> ret = MakeRequest("ADDFOLDER",
  168. new Dictionary<string,object> {
  169. { "ParentID", folder.ParentID.ToString() },
  170. { "Type", folder.Type.ToString() },
  171. { "Version", folder.Version.ToString() },
  172. { "Name", folder.Name.ToString() },
  173. { "Owner", folder.Owner.ToString() },
  174. { "ID", folder.ID.ToString() }
  175. });
  176. if (ret == null)
  177. return false;
  178. return bool.Parse(ret["RESULT"].ToString());
  179. }
  180. public bool UpdateFolder(InventoryFolderBase folder)
  181. {
  182. Dictionary<string,object> ret = MakeRequest("UPDATEFOLDER",
  183. new Dictionary<string,object> {
  184. { "ParentID", folder.ParentID.ToString() },
  185. { "Type", folder.Type.ToString() },
  186. { "Version", folder.Version.ToString() },
  187. { "Name", folder.Name.ToString() },
  188. { "Owner", folder.Owner.ToString() },
  189. { "ID", folder.ID.ToString() }
  190. });
  191. if (ret == null)
  192. return false;
  193. return bool.Parse(ret["RESULT"].ToString());
  194. }
  195. public bool MoveFolder(InventoryFolderBase folder)
  196. {
  197. Dictionary<string,object> ret = MakeRequest("MOVEFOLDER",
  198. new Dictionary<string,object> {
  199. { "ParentID", folder.ParentID.ToString() },
  200. { "ID", folder.ID.ToString() }
  201. });
  202. if (ret == null)
  203. return false;
  204. return bool.Parse(ret["RESULT"].ToString());
  205. }
  206. public bool DeleteFolders(UUID principalID, List<UUID> folderIDs)
  207. {
  208. List<string> slist = new List<string>();
  209. foreach (UUID f in folderIDs)
  210. slist.Add(f.ToString());
  211. Dictionary<string,object> ret = MakeRequest("DELETEFOLDERS",
  212. new Dictionary<string,object> {
  213. { "PRINCIPAL", principalID.ToString() },
  214. { "FOLDERS", slist }
  215. });
  216. if (ret == null)
  217. return false;
  218. return bool.Parse(ret["RESULT"].ToString());
  219. }
  220. public bool PurgeFolder(InventoryFolderBase folder)
  221. {
  222. Dictionary<string,object> ret = MakeRequest("PURGEFOLDER",
  223. new Dictionary<string,object> {
  224. { "ID", folder.ID.ToString() }
  225. });
  226. if (ret == null)
  227. return false;
  228. return bool.Parse(ret["RESULT"].ToString());
  229. }
  230. public bool AddItem(InventoryItemBase item)
  231. {
  232. Dictionary<string,object> ret = MakeRequest("ADDITEM",
  233. new Dictionary<string,object> {
  234. { "AssetID", item.AssetID.ToString() },
  235. { "AssetType", item.AssetType.ToString() },
  236. { "Name", item.Name.ToString() },
  237. { "Owner", item.Owner.ToString() },
  238. { "ID", item.ID.ToString() },
  239. { "InvType", item.InvType.ToString() },
  240. { "Folder", item.Folder.ToString() },
  241. { "CreatorId", item.CreatorId.ToString() },
  242. { "Description", item.Description.ToString() },
  243. { "NextPermissions", item.NextPermissions.ToString() },
  244. { "CurrentPermissions", item.CurrentPermissions.ToString() },
  245. { "BasePermissions", item.BasePermissions.ToString() },
  246. { "EveryOnePermissions", item.EveryOnePermissions.ToString() },
  247. { "GroupPermissions", item.GroupPermissions.ToString() },
  248. { "GroupID", item.GroupID.ToString() },
  249. { "GroupOwned", item.GroupOwned.ToString() },
  250. { "SalePrice", item.SalePrice.ToString() },
  251. { "SaleType", item.SaleType.ToString() },
  252. { "Flags", item.Flags.ToString() },
  253. { "CreationDate", item.CreationDate.ToString() }
  254. });
  255. if (ret == null)
  256. return false;
  257. return bool.Parse(ret["RESULT"].ToString());
  258. }
  259. public bool UpdateItem(InventoryItemBase item)
  260. {
  261. Dictionary<string,object> ret = MakeRequest("UPDATEITEM",
  262. new Dictionary<string,object> {
  263. { "AssetID", item.AssetID.ToString() },
  264. { "AssetType", item.AssetType.ToString() },
  265. { "Name", item.Name.ToString() },
  266. { "Owner", item.Owner.ToString() },
  267. { "ID", item.ID.ToString() },
  268. { "InvType", item.InvType.ToString() },
  269. { "Folder", item.Folder.ToString() },
  270. { "CreatorId", item.CreatorId.ToString() },
  271. { "Description", item.Description.ToString() },
  272. { "NextPermissions", item.NextPermissions.ToString() },
  273. { "CurrentPermissions", item.CurrentPermissions.ToString() },
  274. { "BasePermissions", item.BasePermissions.ToString() },
  275. { "EveryOnePermissions", item.EveryOnePermissions.ToString() },
  276. { "GroupPermissions", item.GroupPermissions.ToString() },
  277. { "GroupID", item.GroupID.ToString() },
  278. { "GroupOwned", item.GroupOwned.ToString() },
  279. { "SalePrice", item.SalePrice.ToString() },
  280. { "SaleType", item.SaleType.ToString() },
  281. { "Flags", item.Flags.ToString() },
  282. { "CreationDate", item.CreationDate.ToString() }
  283. });
  284. if (ret == null)
  285. return false;
  286. return bool.Parse(ret["RESULT"].ToString());
  287. }
  288. public bool MoveItems(UUID principalID, List<InventoryItemBase> items)
  289. {
  290. List<string> idlist = new List<string>();
  291. List<string> destlist = new List<string>();
  292. foreach (InventoryItemBase item in items)
  293. {
  294. idlist.Add(item.ID.ToString());
  295. destlist.Add(item.Folder.ToString());
  296. }
  297. Dictionary<string,object> ret = MakeRequest("MOVEITEMS",
  298. new Dictionary<string,object> {
  299. { "PrincipalID", principalID.ToString() },
  300. { "IDLIST", idlist },
  301. { "DESTLIST", destlist }
  302. });
  303. if (ret == null)
  304. return false;
  305. return bool.Parse(ret["RESULT"].ToString());
  306. }
  307. public bool DeleteItems(UUID principalID, List<UUID> itemIDs)
  308. {
  309. List<string> slist = new List<string>();
  310. foreach (UUID f in itemIDs)
  311. slist.Add(f.ToString());
  312. Dictionary<string,object> ret = MakeRequest("DELETEITEMS",
  313. new Dictionary<string,object> {
  314. { "PRINCIPAL", principalID.ToString() },
  315. { "ITEMS", slist }
  316. });
  317. if (ret == null)
  318. return false;
  319. return bool.Parse(ret["RESULT"].ToString());
  320. }
  321. public InventoryItemBase GetItem(InventoryItemBase item)
  322. {
  323. Dictionary<string,object> ret = MakeRequest("GETITEM",
  324. new Dictionary<string,object> {
  325. { "ID", item.ID.ToString() }
  326. });
  327. if (ret == null)
  328. return null;
  329. if (ret.Count == 0)
  330. return null;
  331. return BuildItem(ret);
  332. }
  333. public InventoryFolderBase GetFolder(InventoryFolderBase folder)
  334. {
  335. Dictionary<string,object> ret = MakeRequest("GETFOLDER",
  336. new Dictionary<string,object> {
  337. { "ID", folder.ID.ToString() }
  338. });
  339. if (ret == null)
  340. return null;
  341. if (ret.Count == 0)
  342. return null;
  343. return BuildFolder(ret);
  344. }
  345. public List<InventoryItemBase> GetActiveGestures(UUID principalID)
  346. {
  347. Dictionary<string,object> ret = MakeRequest("GETACTIVEGESTURES",
  348. new Dictionary<string,object> {
  349. { "PRINCIPAL", principalID.ToString() }
  350. });
  351. if (ret == null)
  352. return null;
  353. List<InventoryItemBase> items = new List<InventoryItemBase>();
  354. foreach (Object o in ret.Values)
  355. items.Add(BuildItem((Dictionary<string,object>)o));
  356. return items;
  357. }
  358. public int GetAssetPermissions(UUID principalID, UUID assetID)
  359. {
  360. Dictionary<string,object> ret = MakeRequest("GETASSETPERMISSIONS",
  361. new Dictionary<string,object> {
  362. { "PRINCIPAL", principalID.ToString() },
  363. { "ASSET", assetID.ToString() }
  364. });
  365. if (ret == null)
  366. return 0;
  367. return int.Parse(ret["RESULT"].ToString());
  368. }
  369. // These are either obsolete or unused
  370. //
  371. public InventoryCollection GetUserInventory(UUID principalID)
  372. {
  373. return null;
  374. }
  375. public void GetUserInventory(UUID principalID, InventoryReceiptCallback callback)
  376. {
  377. }
  378. public bool HasInventoryForUser(UUID principalID)
  379. {
  380. return false;
  381. }
  382. // Helpers
  383. //
  384. private Dictionary<string,object> MakeRequest(string method,
  385. Dictionary<string,object> sendData)
  386. {
  387. sendData["METHOD"] = method;
  388. string reply = SynchronousRestFormsRequester.MakeRequest("POST",
  389. m_ServerURI + "/xinventory",
  390. ServerUtils.BuildQueryString(sendData));
  391. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(
  392. reply);
  393. return replyData;
  394. }
  395. private InventoryFolderBase BuildFolder(Dictionary<string,object> data)
  396. {
  397. InventoryFolderBase folder = new InventoryFolderBase();
  398. folder.ParentID = new UUID(data["ParentID"].ToString());
  399. folder.Type = short.Parse(data["Type"].ToString());
  400. folder.Version = ushort.Parse(data["Version"].ToString());
  401. folder.Name = data["Name"].ToString();
  402. folder.Owner = new UUID(data["Owner"].ToString());
  403. folder.ID = new UUID(data["ID"].ToString());
  404. return folder;
  405. }
  406. private InventoryItemBase BuildItem(Dictionary<string,object> data)
  407. {
  408. InventoryItemBase item = new InventoryItemBase();
  409. item.AssetID = new UUID(data["AssetID"].ToString());
  410. item.AssetType = int.Parse(data["AssetType"].ToString());
  411. item.Name = data["Name"].ToString();
  412. item.Owner = new UUID(data["Owner"].ToString());
  413. item.ID = new UUID(data["ID"].ToString());
  414. item.InvType = int.Parse(data["InvType"].ToString());
  415. item.Folder = new UUID(data["Folder"].ToString());
  416. item.CreatorId = data["CreatorId"].ToString();
  417. item.Description = data["Description"].ToString();
  418. item.NextPermissions = uint.Parse(data["NextPermissions"].ToString());
  419. item.CurrentPermissions = uint.Parse(data["CurrentPermissions"].ToString());
  420. item.BasePermissions = uint.Parse(data["BasePermissions"].ToString());
  421. item.EveryOnePermissions = uint.Parse(data["EveryOnePermissions"].ToString());
  422. item.GroupPermissions = uint.Parse(data["GroupPermissions"].ToString());
  423. item.GroupID = new UUID(data["GroupID"].ToString());
  424. item.GroupOwned = bool.Parse(data["GroupOwned"].ToString());
  425. item.SalePrice = int.Parse(data["SalePrice"].ToString());
  426. item.SaleType = byte.Parse(data["SaleType"].ToString());
  427. item.Flags = uint.Parse(data["Flags"].ToString());
  428. item.CreationDate = int.Parse(data["CreationDate"].ToString());
  429. return item;
  430. }
  431. }
  432. }