XInventoryServicesConnector.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  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.Services.Interfaces;
  37. using OpenSim.Server.Base;
  38. using OpenMetaverse;
  39. namespace OpenSim.Services.Connectors
  40. {
  41. public class XInventoryServicesConnector : IInventoryService
  42. {
  43. private static readonly ILog m_log =
  44. LogManager.GetLogger(
  45. MethodBase.GetCurrentMethod().DeclaringType);
  46. private string m_ServerURI = String.Empty;
  47. private object m_Lock = new object();
  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 OpenSim.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. private bool CheckReturn(Dictionary<string, object> ret)
  77. {
  78. if (ret == null)
  79. return false;
  80. if (ret.Count == 0)
  81. return false;
  82. if (ret.ContainsKey("RESULT"))
  83. {
  84. if (ret["RESULT"] is string)
  85. {
  86. bool result;
  87. if (bool.TryParse((string)ret["RESULT"], out result))
  88. return result;
  89. return false;
  90. }
  91. }
  92. return true;
  93. }
  94. public bool CreateUserInventory(UUID principalID)
  95. {
  96. Dictionary<string,object> ret = MakeRequest("CREATEUSERINVENTORY",
  97. new Dictionary<string,object> {
  98. { "PRINCIPAL", principalID.ToString() }
  99. });
  100. return CheckReturn(ret);
  101. }
  102. public List<InventoryFolderBase> GetInventorySkeleton(UUID principalID)
  103. {
  104. Dictionary<string,object> ret = MakeRequest("GETINVENTORYSKELETON",
  105. new Dictionary<string,object> {
  106. { "PRINCIPAL", principalID.ToString() }
  107. });
  108. if (!CheckReturn(ret))
  109. return null;
  110. Dictionary<string, object> folders = (Dictionary<string, object>)ret["FOLDERS"];
  111. List<InventoryFolderBase> fldrs = new List<InventoryFolderBase>();
  112. try
  113. {
  114. foreach (Object o in folders.Values)
  115. fldrs.Add(BuildFolder((Dictionary<string, object>)o));
  116. }
  117. catch (Exception e)
  118. {
  119. m_log.Error("[XINVENTORY SERVICES CONNECTOR]: Exception unwrapping folder list: ", e);
  120. }
  121. return fldrs;
  122. }
  123. public InventoryFolderBase GetRootFolder(UUID principalID)
  124. {
  125. Dictionary<string,object> ret = MakeRequest("GETROOTFOLDER",
  126. new Dictionary<string,object> {
  127. { "PRINCIPAL", principalID.ToString() }
  128. });
  129. if (!CheckReturn(ret))
  130. return null;
  131. return BuildFolder((Dictionary<string, object>)ret["folder"]);
  132. }
  133. public InventoryFolderBase GetFolderForType(UUID principalID, AssetType type)
  134. {
  135. Dictionary<string,object> ret = MakeRequest("GETFOLDERFORTYPE",
  136. new Dictionary<string,object> {
  137. { "PRINCIPAL", principalID.ToString() },
  138. { "TYPE", ((int)type).ToString() }
  139. });
  140. if (!CheckReturn(ret))
  141. return null;
  142. return BuildFolder((Dictionary<string, object>)ret["folder"]);
  143. }
  144. public InventoryCollection GetFolderContent(UUID principalID, UUID folderID)
  145. {
  146. InventoryCollection inventory = new InventoryCollection();
  147. inventory.Folders = new List<InventoryFolderBase>();
  148. inventory.Items = new List<InventoryItemBase>();
  149. inventory.UserID = principalID;
  150. try
  151. {
  152. Dictionary<string,object> ret = MakeRequest("GETFOLDERCONTENT",
  153. new Dictionary<string,object> {
  154. { "PRINCIPAL", principalID.ToString() },
  155. { "FOLDER", folderID.ToString() }
  156. });
  157. if (!CheckReturn(ret))
  158. return null;
  159. Dictionary<string,object> folders =
  160. (Dictionary<string,object>)ret["FOLDERS"];
  161. Dictionary<string,object> items =
  162. (Dictionary<string,object>)ret["ITEMS"];
  163. foreach (Object o in folders.Values) // getting the values directly, we don't care about the keys folder_i
  164. inventory.Folders.Add(BuildFolder((Dictionary<string, object>)o));
  165. foreach (Object o in items.Values) // getting the values directly, we don't care about the keys item_i
  166. inventory.Items.Add(BuildItem((Dictionary<string, object>)o));
  167. }
  168. catch (Exception e)
  169. {
  170. m_log.WarnFormat("[XINVENTORY SERVICES CONNECTOR]: Exception in GetFolderContent: {0}", e.Message);
  171. }
  172. return inventory;
  173. }
  174. public List<InventoryItemBase> GetFolderItems(UUID principalID, UUID folderID)
  175. {
  176. Dictionary<string,object> ret = MakeRequest("GETFOLDERITEMS",
  177. new Dictionary<string,object> {
  178. { "PRINCIPAL", principalID.ToString() },
  179. { "FOLDER", folderID.ToString() }
  180. });
  181. if (!CheckReturn(ret))
  182. return null;
  183. Dictionary<string, object> items = (Dictionary<string, object>)ret["ITEMS"];
  184. List<InventoryItemBase> fitems = new List<InventoryItemBase>();
  185. foreach (Object o in items.Values) // getting the values directly, we don't care about the keys item_i
  186. fitems.Add(BuildItem((Dictionary<string, object>)o));
  187. return fitems;
  188. }
  189. public bool AddFolder(InventoryFolderBase folder)
  190. {
  191. Dictionary<string,object> ret = MakeRequest("ADDFOLDER",
  192. new Dictionary<string,object> {
  193. { "ParentID", folder.ParentID.ToString() },
  194. { "Type", folder.Type.ToString() },
  195. { "Version", folder.Version.ToString() },
  196. { "Name", folder.Name.ToString() },
  197. { "Owner", folder.Owner.ToString() },
  198. { "ID", folder.ID.ToString() }
  199. });
  200. return CheckReturn(ret);
  201. }
  202. public bool UpdateFolder(InventoryFolderBase folder)
  203. {
  204. Dictionary<string,object> ret = MakeRequest("UPDATEFOLDER",
  205. new Dictionary<string,object> {
  206. { "ParentID", folder.ParentID.ToString() },
  207. { "Type", folder.Type.ToString() },
  208. { "Version", folder.Version.ToString() },
  209. { "Name", folder.Name.ToString() },
  210. { "Owner", folder.Owner.ToString() },
  211. { "ID", folder.ID.ToString() }
  212. });
  213. return CheckReturn(ret);
  214. }
  215. public bool MoveFolder(InventoryFolderBase folder)
  216. {
  217. Dictionary<string,object> ret = MakeRequest("MOVEFOLDER",
  218. new Dictionary<string,object> {
  219. { "ParentID", folder.ParentID.ToString() },
  220. { "ID", folder.ID.ToString() },
  221. { "PRINCIPAL", folder.Owner.ToString() }
  222. });
  223. return CheckReturn(ret);
  224. }
  225. public bool DeleteFolders(UUID principalID, List<UUID> folderIDs)
  226. {
  227. List<string> slist = new List<string>();
  228. foreach (UUID f in folderIDs)
  229. slist.Add(f.ToString());
  230. Dictionary<string,object> ret = MakeRequest("DELETEFOLDERS",
  231. new Dictionary<string,object> {
  232. { "PRINCIPAL", principalID.ToString() },
  233. { "FOLDERS", slist }
  234. });
  235. return CheckReturn(ret);
  236. }
  237. public bool PurgeFolder(InventoryFolderBase folder)
  238. {
  239. Dictionary<string,object> ret = MakeRequest("PURGEFOLDER",
  240. new Dictionary<string,object> {
  241. { "ID", folder.ID.ToString() }
  242. });
  243. return CheckReturn(ret);
  244. }
  245. public bool AddItem(InventoryItemBase item)
  246. {
  247. if (item.CreatorData == null)
  248. item.CreatorData = String.Empty;
  249. Dictionary<string,object> ret = MakeRequest("ADDITEM",
  250. new Dictionary<string,object> {
  251. { "AssetID", item.AssetID.ToString() },
  252. { "AssetType", item.AssetType.ToString() },
  253. { "Name", item.Name.ToString() },
  254. { "Owner", item.Owner.ToString() },
  255. { "ID", item.ID.ToString() },
  256. { "InvType", item.InvType.ToString() },
  257. { "Folder", item.Folder.ToString() },
  258. { "CreatorId", item.CreatorId.ToString() },
  259. { "CreatorData", item.CreatorData.ToString() },
  260. { "Description", item.Description.ToString() },
  261. { "NextPermissions", item.NextPermissions.ToString() },
  262. { "CurrentPermissions", item.CurrentPermissions.ToString() },
  263. { "BasePermissions", item.BasePermissions.ToString() },
  264. { "EveryOnePermissions", item.EveryOnePermissions.ToString() },
  265. { "GroupPermissions", item.GroupPermissions.ToString() },
  266. { "GroupID", item.GroupID.ToString() },
  267. { "GroupOwned", item.GroupOwned.ToString() },
  268. { "SalePrice", item.SalePrice.ToString() },
  269. { "SaleType", item.SaleType.ToString() },
  270. { "Flags", item.Flags.ToString() },
  271. { "CreationDate", item.CreationDate.ToString() }
  272. });
  273. return CheckReturn(ret);
  274. }
  275. public bool UpdateItem(InventoryItemBase item)
  276. {
  277. if (item.CreatorData == null)
  278. item.CreatorData = String.Empty;
  279. Dictionary<string,object> ret = MakeRequest("UPDATEITEM",
  280. new Dictionary<string,object> {
  281. { "AssetID", item.AssetID.ToString() },
  282. { "AssetType", item.AssetType.ToString() },
  283. { "Name", item.Name.ToString() },
  284. { "Owner", item.Owner.ToString() },
  285. { "ID", item.ID.ToString() },
  286. { "InvType", item.InvType.ToString() },
  287. { "Folder", item.Folder.ToString() },
  288. { "CreatorId", item.CreatorId.ToString() },
  289. { "CreatorData", item.CreatorData.ToString() },
  290. { "Description", item.Description.ToString() },
  291. { "NextPermissions", item.NextPermissions.ToString() },
  292. { "CurrentPermissions", item.CurrentPermissions.ToString() },
  293. { "BasePermissions", item.BasePermissions.ToString() },
  294. { "EveryOnePermissions", item.EveryOnePermissions.ToString() },
  295. { "GroupPermissions", item.GroupPermissions.ToString() },
  296. { "GroupID", item.GroupID.ToString() },
  297. { "GroupOwned", item.GroupOwned.ToString() },
  298. { "SalePrice", item.SalePrice.ToString() },
  299. { "SaleType", item.SaleType.ToString() },
  300. { "Flags", item.Flags.ToString() },
  301. { "CreationDate", item.CreationDate.ToString() }
  302. });
  303. return CheckReturn(ret);
  304. }
  305. public bool MoveItems(UUID principalID, List<InventoryItemBase> items)
  306. {
  307. List<string> idlist = new List<string>();
  308. List<string> destlist = new List<string>();
  309. foreach (InventoryItemBase item in items)
  310. {
  311. idlist.Add(item.ID.ToString());
  312. destlist.Add(item.Folder.ToString());
  313. }
  314. Dictionary<string,object> ret = MakeRequest("MOVEITEMS",
  315. new Dictionary<string,object> {
  316. { "PRINCIPAL", principalID.ToString() },
  317. { "IDLIST", idlist },
  318. { "DESTLIST", destlist }
  319. });
  320. return CheckReturn(ret);
  321. }
  322. public bool DeleteItems(UUID principalID, List<UUID> itemIDs)
  323. {
  324. List<string> slist = new List<string>();
  325. foreach (UUID f in itemIDs)
  326. slist.Add(f.ToString());
  327. Dictionary<string,object> ret = MakeRequest("DELETEITEMS",
  328. new Dictionary<string,object> {
  329. { "PRINCIPAL", principalID.ToString() },
  330. { "ITEMS", slist }
  331. });
  332. return CheckReturn(ret);
  333. }
  334. public InventoryItemBase GetItem(InventoryItemBase item)
  335. {
  336. try
  337. {
  338. Dictionary<string, object> ret = MakeRequest("GETITEM",
  339. new Dictionary<string, object> {
  340. { "ID", item.ID.ToString() }
  341. });
  342. if (!CheckReturn(ret))
  343. return null;
  344. return BuildItem((Dictionary<string, object>)ret["item"]);
  345. }
  346. catch (Exception e)
  347. {
  348. m_log.Error("[XINVENTORY SERVICES CONNECTOR]: Exception in GetItem: ", e);
  349. }
  350. return null;
  351. }
  352. public InventoryFolderBase GetFolder(InventoryFolderBase folder)
  353. {
  354. try
  355. {
  356. Dictionary<string, object> ret = MakeRequest("GETFOLDER",
  357. new Dictionary<string, object> {
  358. { "ID", folder.ID.ToString() }
  359. });
  360. if (!CheckReturn(ret))
  361. return null;
  362. return BuildFolder((Dictionary<string, object>)ret["folder"]);
  363. }
  364. catch (Exception e)
  365. {
  366. m_log.Error("[XINVENTORY SERVICES CONNECTOR]: Exception in GetFolder: ", e);
  367. }
  368. return null;
  369. }
  370. public List<InventoryItemBase> GetActiveGestures(UUID principalID)
  371. {
  372. Dictionary<string,object> ret = MakeRequest("GETACTIVEGESTURES",
  373. new Dictionary<string,object> {
  374. { "PRINCIPAL", principalID.ToString() }
  375. });
  376. if (!CheckReturn(ret))
  377. return null;
  378. List<InventoryItemBase> items = new List<InventoryItemBase>();
  379. foreach (Object o in ((Dictionary<string,object>)ret["ITEMS"]).Values)
  380. items.Add(BuildItem((Dictionary<string, object>)o));
  381. return items;
  382. }
  383. public int GetAssetPermissions(UUID principalID, UUID assetID)
  384. {
  385. Dictionary<string,object> ret = MakeRequest("GETASSETPERMISSIONS",
  386. new Dictionary<string,object> {
  387. { "PRINCIPAL", principalID.ToString() },
  388. { "ASSET", assetID.ToString() }
  389. });
  390. // We cannot use CheckReturn() here because valid values for RESULT are "false" (in the case of request failure) or an int
  391. if (ret == null)
  392. return 0;
  393. if (ret.ContainsKey("RESULT"))
  394. {
  395. if (ret["RESULT"] is string)
  396. {
  397. int intResult;
  398. if (int.TryParse ((string)ret["RESULT"], out intResult))
  399. return intResult;
  400. }
  401. }
  402. return 0;
  403. }
  404. public InventoryCollection GetUserInventory(UUID principalID)
  405. {
  406. InventoryCollection inventory = new InventoryCollection();
  407. inventory.Folders = new List<InventoryFolderBase>();
  408. inventory.Items = new List<InventoryItemBase>();
  409. inventory.UserID = principalID;
  410. try
  411. {
  412. Dictionary<string, object> ret = MakeRequest("GETUSERINVENTORY",
  413. new Dictionary<string, object> {
  414. { "PRINCIPAL", principalID.ToString() }
  415. });
  416. if (!CheckReturn(ret))
  417. return null;
  418. Dictionary<string, object> folders =
  419. (Dictionary<string, object>)ret["FOLDERS"];
  420. Dictionary<string, object> items =
  421. (Dictionary<string, object>)ret["ITEMS"];
  422. foreach (Object o in folders.Values) // getting the values directly, we don't care about the keys folder_i
  423. inventory.Folders.Add(BuildFolder((Dictionary<string, object>)o));
  424. foreach (Object o in items.Values) // getting the values directly, we don't care about the keys item_i
  425. inventory.Items.Add(BuildItem((Dictionary<string, object>)o));
  426. }
  427. catch (Exception e)
  428. {
  429. m_log.Error("[XINVENTORY SERVICES CONNECTOR]: Exception in GetUserInventory: ", e);
  430. }
  431. return inventory;
  432. }
  433. public void GetUserInventory(UUID principalID, InventoryReceiptCallback callback)
  434. {
  435. }
  436. public bool HasInventoryForUser(UUID principalID)
  437. {
  438. return false;
  439. }
  440. // Helpers
  441. //
  442. private Dictionary<string,object> MakeRequest(string method,
  443. Dictionary<string,object> sendData)
  444. {
  445. sendData["METHOD"] = method;
  446. string reply = string.Empty;
  447. lock (m_Lock)
  448. reply = SynchronousRestFormsRequester.MakeRequest("POST",
  449. m_ServerURI + "/xinventory",
  450. ServerUtils.BuildQueryString(sendData));
  451. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(
  452. reply);
  453. return replyData;
  454. }
  455. private InventoryFolderBase BuildFolder(Dictionary<string,object> data)
  456. {
  457. InventoryFolderBase folder = new InventoryFolderBase();
  458. try
  459. {
  460. folder.ParentID = new UUID(data["ParentID"].ToString());
  461. folder.Type = short.Parse(data["Type"].ToString());
  462. folder.Version = ushort.Parse(data["Version"].ToString());
  463. folder.Name = data["Name"].ToString();
  464. folder.Owner = new UUID(data["Owner"].ToString());
  465. folder.ID = new UUID(data["ID"].ToString());
  466. }
  467. catch (Exception e)
  468. {
  469. m_log.Error("[XINVENTORY SERVICES CONNECTOR]: Exception building folder: ", e);
  470. }
  471. return folder;
  472. }
  473. private InventoryItemBase BuildItem(Dictionary<string,object> data)
  474. {
  475. InventoryItemBase item = new InventoryItemBase();
  476. try
  477. {
  478. item.AssetID = new UUID(data["AssetID"].ToString());
  479. item.AssetType = int.Parse(data["AssetType"].ToString());
  480. item.Name = data["Name"].ToString();
  481. item.Owner = new UUID(data["Owner"].ToString());
  482. item.ID = new UUID(data["ID"].ToString());
  483. item.InvType = int.Parse(data["InvType"].ToString());
  484. item.Folder = new UUID(data["Folder"].ToString());
  485. item.CreatorId = data["CreatorId"].ToString();
  486. if (data.ContainsKey("CreatorData"))
  487. item.CreatorData = data["CreatorData"].ToString();
  488. else
  489. item.CreatorData = String.Empty;
  490. item.Description = data["Description"].ToString();
  491. item.NextPermissions = uint.Parse(data["NextPermissions"].ToString());
  492. item.CurrentPermissions = uint.Parse(data["CurrentPermissions"].ToString());
  493. item.BasePermissions = uint.Parse(data["BasePermissions"].ToString());
  494. item.EveryOnePermissions = uint.Parse(data["EveryOnePermissions"].ToString());
  495. item.GroupPermissions = uint.Parse(data["GroupPermissions"].ToString());
  496. item.GroupID = new UUID(data["GroupID"].ToString());
  497. item.GroupOwned = bool.Parse(data["GroupOwned"].ToString());
  498. item.SalePrice = int.Parse(data["SalePrice"].ToString());
  499. item.SaleType = byte.Parse(data["SaleType"].ToString());
  500. item.Flags = uint.Parse(data["Flags"].ToString());
  501. item.CreationDate = int.Parse(data["CreationDate"].ToString());
  502. }
  503. catch (Exception e)
  504. {
  505. m_log.Error("[XINVENTORY CONNECTOR]: Exception building item: ", e);
  506. }
  507. return item;
  508. }
  509. }
  510. }