XInventoryServicesConnector.cs 24 KB

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