InventoryServiceConnector.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  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.Servers.HttpServer;
  35. using OpenSim.Services.Interfaces;
  36. using OpenMetaverse;
  37. namespace OpenSim.Services.Connectors
  38. {
  39. public class InventoryServicesConnector : ISessionAuthInventoryService
  40. {
  41. private static readonly ILog m_log =
  42. LogManager.GetLogger(
  43. MethodBase.GetCurrentMethod().DeclaringType);
  44. private string m_ServerURI = String.Empty;
  45. private Dictionary<UUID, InventoryReceiptCallback> m_RequestingInventory = new Dictionary<UUID, InventoryReceiptCallback>();
  46. private Dictionary<UUID, DateTime> m_RequestTime = new Dictionary<UUID, DateTime>();
  47. public InventoryServicesConnector()
  48. {
  49. }
  50. public InventoryServicesConnector(string serverURI)
  51. {
  52. m_ServerURI = serverURI.TrimEnd('/');
  53. }
  54. public InventoryServicesConnector(IConfigSource source)
  55. {
  56. Initialise(source);
  57. }
  58. public virtual void Initialise(IConfigSource source)
  59. {
  60. IConfig inventoryConfig = source.Configs["InventoryService"];
  61. if (inventoryConfig == null)
  62. {
  63. m_log.Error("[INVENTORY CONNECTOR]: InventoryService missing from OpenSim.ini");
  64. throw new Exception("InventoryService missing from OpenSim.ini");
  65. }
  66. string serviceURI = inventoryConfig.GetString("InventoryServerURI",
  67. String.Empty);
  68. if (serviceURI == String.Empty)
  69. {
  70. m_log.Error("[INVENTORY CONNECTOR]: No Server URI named in section InventoryService");
  71. throw new Exception("Unable to proceed. Please make sure your ini files in config-include are updated according to .example's");
  72. }
  73. m_ServerURI = serviceURI.TrimEnd('/');
  74. }
  75. #region ISessionAuthInventoryService
  76. public string Host
  77. {
  78. get { return m_ServerURI; }
  79. }
  80. /// <summary>
  81. /// Caller must catch eventual Exceptions.
  82. /// </summary>
  83. /// <param name="userID"></param>
  84. /// <param name="sessionID"></param>
  85. /// <param name="callback"></param>
  86. public void GetUserInventory(string userIDStr, UUID sessionID, InventoryReceiptCallback callback)
  87. {
  88. UUID userID = UUID.Zero;
  89. if (UUID.TryParse(userIDStr, out userID))
  90. {
  91. lock (m_RequestingInventory)
  92. {
  93. // *HACK ALERT*
  94. // If an inventory request times out, it blocks any further requests from the
  95. // same user, even after a relog. This is bad, and makes me sad.
  96. // Really, we should detect a timeout and report a failure to the callback,
  97. // BUT in my testing i found that it's hard to detect a timeout.. sometimes,
  98. // a partial response is recieved, and sometimes a null response.
  99. // So, for now, add a timer of ten seconds (which is the request timeout).
  100. // This should basically have the same effect.
  101. lock (m_RequestTime)
  102. {
  103. if (m_RequestTime.ContainsKey(userID))
  104. {
  105. TimeSpan interval = DateTime.Now - m_RequestTime[userID];
  106. if (interval.TotalSeconds > 10)
  107. {
  108. m_RequestTime.Remove(userID);
  109. if (m_RequestingInventory.ContainsKey(userID))
  110. {
  111. m_RequestingInventory.Remove(userID);
  112. }
  113. }
  114. }
  115. if (!m_RequestingInventory.ContainsKey(userID))
  116. {
  117. m_RequestTime.Add(userID, DateTime.Now);
  118. m_RequestingInventory.Add(userID, callback);
  119. }
  120. else
  121. {
  122. m_log.ErrorFormat("[INVENTORY CONNECTOR]: GetUserInventory - ignoring repeated request for user {0}", userID);
  123. return;
  124. }
  125. }
  126. }
  127. m_log.InfoFormat(
  128. "[INVENTORY CONNECTOR]: Requesting inventory from {0}/GetInventory/ for user {1}",
  129. m_ServerURI, userID);
  130. RestSessionObjectPosterResponse<Guid, InventoryCollection> requester
  131. = new RestSessionObjectPosterResponse<Guid, InventoryCollection>();
  132. requester.ResponseCallback = InventoryResponse;
  133. requester.BeginPostObject(m_ServerURI + "/GetInventory/", userID.Guid, sessionID.ToString(), userID.ToString());
  134. }
  135. }
  136. /// <summary>
  137. /// Gets the user folder for the given folder-type
  138. /// </summary>
  139. /// <param name="userID"></param>
  140. /// <param name="type"></param>
  141. /// <returns></returns>
  142. public Dictionary<AssetType, InventoryFolderBase> GetSystemFolders(string userID, UUID sessionID)
  143. {
  144. List<InventoryFolderBase> folders = null;
  145. Dictionary<AssetType, InventoryFolderBase> dFolders = new Dictionary<AssetType, InventoryFolderBase>();
  146. try
  147. {
  148. folders = SynchronousRestSessionObjectPoster<Guid, List<InventoryFolderBase>>.BeginPostObject(
  149. "POST", m_ServerURI + "/SystemFolders/", new Guid(userID), sessionID.ToString(), userID.ToString());
  150. foreach (InventoryFolderBase f in folders)
  151. dFolders[(AssetType)f.Type] = f;
  152. return dFolders;
  153. }
  154. catch (Exception e)
  155. {
  156. // Maybe we're talking to an old inventory server. Try this other thing.
  157. m_log.ErrorFormat("[INVENTORY CONNECTOR]: GetSystemFolders operation failed, {0} {1} (old sever?). Trying GetInventory.",
  158. e.Source, e.Message);
  159. try
  160. {
  161. InventoryCollection inventory = SynchronousRestSessionObjectPoster<Guid, InventoryCollection>.BeginPostObject(
  162. "POST", m_ServerURI + "/GetInventory/", new Guid(userID), sessionID.ToString(), userID.ToString());
  163. folders = inventory.Folders;
  164. }
  165. catch (Exception ex)
  166. {
  167. m_log.ErrorFormat("[INVENTORY CONNECTOR]: GetInventory operation also failed, {0} {1}. Giving up.",
  168. e.Source, ex.Message);
  169. }
  170. if ((folders != null) && (folders.Count > 0))
  171. {
  172. m_log.DebugFormat("[INVENTORY CONNECTOR]: Received entire inventory ({0} folders) for user {1}",
  173. folders.Count, userID);
  174. foreach (InventoryFolderBase f in folders)
  175. {
  176. if ((f.Type != (short)AssetType.Folder) && (f.Type != (short)AssetType.Unknown))
  177. dFolders[(AssetType)f.Type] = f;
  178. }
  179. UUID rootFolderID = dFolders[AssetType.Animation].ParentID;
  180. InventoryFolderBase rootFolder = new InventoryFolderBase(rootFolderID, new UUID(userID));
  181. rootFolder = QueryFolder(userID, rootFolder, sessionID);
  182. dFolders[AssetType.Folder] = rootFolder;
  183. m_log.DebugFormat("[INVENTORY CONNECTOR]: {0} system folders for user {1}", dFolders.Count, userID);
  184. return dFolders;
  185. }
  186. }
  187. return new Dictionary<AssetType, InventoryFolderBase>();
  188. }
  189. /// <summary>
  190. /// Gets everything (folders and items) inside a folder
  191. /// </summary>
  192. /// <param name="userId"></param>
  193. /// <param name="folderID"></param>
  194. /// <returns></returns>
  195. public InventoryCollection GetFolderContent(string userID, UUID folderID, UUID sessionID)
  196. {
  197. try
  198. {
  199. // normal case
  200. return SynchronousRestSessionObjectPoster<Guid, InventoryCollection>.BeginPostObject(
  201. "POST", m_ServerURI + "/GetFolderContent/", folderID.Guid, sessionID.ToString(), userID.ToString());
  202. }
  203. catch (Exception e)
  204. {
  205. // Maybe we're talking to an old inventory server. Try this other thing.
  206. m_log.ErrorFormat("[INVENTORY CONNECTOR]: GetFolderContent operation failed, {0} {1} (old server?). Trying GetInventory.",
  207. e.Source, e.Message);
  208. InventoryCollection inventory;
  209. List<InventoryFolderBase> folders = null;
  210. try
  211. {
  212. inventory = SynchronousRestSessionObjectPoster<Guid, InventoryCollection>.BeginPostObject(
  213. "POST", m_ServerURI + "/GetInventory/", new Guid(userID), sessionID.ToString(), userID.ToString());
  214. if (inventory != null)
  215. folders = inventory.Folders;
  216. }
  217. catch (Exception ex)
  218. {
  219. m_log.ErrorFormat("[INVENTORY CONNECTOR]: GetInventory operation also failed, {0} {1}. Giving up.",
  220. e.Source, ex.Message);
  221. return new InventoryCollection();
  222. }
  223. if ((folders != null) && (folders.Count > 0))
  224. {
  225. m_log.DebugFormat("[INVENTORY CONNECTOR]: Received entire inventory ({0} folders) for user {1}",
  226. folders.Count, userID);
  227. folders = folders.FindAll(delegate(InventoryFolderBase f) { return f.ParentID == folderID; });
  228. List<InventoryItemBase> items = inventory.Items;
  229. if (items != null)
  230. {
  231. items = items.FindAll(delegate(InventoryItemBase i) { return i.Folder == folderID; });
  232. }
  233. inventory.Items = items;
  234. inventory.Folders = folders;
  235. return inventory;
  236. }
  237. }
  238. InventoryCollection nullCollection = new InventoryCollection();
  239. nullCollection.Folders = new List<InventoryFolderBase>();
  240. nullCollection.Items = new List<InventoryItemBase>();
  241. nullCollection.UserID = new UUID(userID);
  242. return nullCollection;
  243. }
  244. public bool AddFolder(string userID, InventoryFolderBase folder, UUID sessionID)
  245. {
  246. try
  247. {
  248. return SynchronousRestSessionObjectPoster<InventoryFolderBase, bool>.BeginPostObject(
  249. "POST", m_ServerURI + "/NewFolder/", folder, sessionID.ToString(), folder.Owner.ToString());
  250. }
  251. catch (Exception e)
  252. {
  253. m_log.ErrorFormat("[INVENTORY CONNECTOR]: Add new inventory folder operation failed, {0} {1}",
  254. e.Source, e.Message);
  255. }
  256. return false;
  257. }
  258. public bool UpdateFolder(string userID, InventoryFolderBase folder, UUID sessionID)
  259. {
  260. try
  261. {
  262. return SynchronousRestSessionObjectPoster<InventoryFolderBase, bool>.BeginPostObject(
  263. "POST", m_ServerURI + "/UpdateFolder/", folder, sessionID.ToString(), folder.Owner.ToString());
  264. }
  265. catch (Exception e)
  266. {
  267. m_log.ErrorFormat("[INVENTORY CONNECTOR]: Update inventory folder operation failed, {0} {1}",
  268. e.Source, e.Message);
  269. }
  270. return false;
  271. }
  272. public bool MoveFolder(string userID, InventoryFolderBase folder, UUID sessionID)
  273. {
  274. try
  275. {
  276. return SynchronousRestSessionObjectPoster<InventoryFolderBase, bool>.BeginPostObject(
  277. "POST", m_ServerURI + "/MoveFolder/", folder, sessionID.ToString(), folder.Owner.ToString());
  278. }
  279. catch (Exception e)
  280. {
  281. m_log.ErrorFormat("[INVENTORY CONNECTOR]: Move inventory folder operation failed, {0} {1}",
  282. e.Source, e.Message);
  283. }
  284. return false;
  285. }
  286. public bool PurgeFolder(string userID, InventoryFolderBase folder, UUID sessionID)
  287. {
  288. try
  289. {
  290. return SynchronousRestSessionObjectPoster<InventoryFolderBase, bool>.BeginPostObject(
  291. "POST", m_ServerURI + "/PurgeFolder/", folder, sessionID.ToString(), folder.Owner.ToString());
  292. }
  293. catch (Exception e)
  294. {
  295. m_log.ErrorFormat("[INVENTORY CONNECTOR]: Move inventory folder operation failed, {0} {1}",
  296. e.Source, e.Message);
  297. }
  298. return false;
  299. }
  300. public List<InventoryItemBase> GetFolderItems(string userID, UUID folderID, UUID sessionID)
  301. {
  302. try
  303. {
  304. InventoryFolderBase folder = new InventoryFolderBase(folderID, new UUID(userID));
  305. return SynchronousRestSessionObjectPoster<InventoryFolderBase, List<InventoryItemBase>>.BeginPostObject(
  306. "POST", m_ServerURI + "/GetItems/", folder, sessionID.ToString(), userID);
  307. }
  308. catch (Exception e)
  309. {
  310. m_log.ErrorFormat("[INVENTORY CONNECTOR]: Get folder items operation failed, {0} {1}",
  311. e.Source, e.Message);
  312. }
  313. return null;
  314. }
  315. public bool AddItem(string userID, InventoryItemBase item, UUID sessionID)
  316. {
  317. try
  318. {
  319. return SynchronousRestSessionObjectPoster<InventoryItemBase, bool>.BeginPostObject(
  320. "POST", m_ServerURI + "/NewItem/", item, sessionID.ToString(), item.Owner.ToString());
  321. }
  322. catch (Exception e)
  323. {
  324. m_log.ErrorFormat("[INVENTORY CONNECTOR]: Add new inventory item operation failed, {0} {1}",
  325. e.Source, e.Message);
  326. }
  327. return false;
  328. }
  329. public bool UpdateItem(string userID, InventoryItemBase item, UUID sessionID)
  330. {
  331. try
  332. {
  333. return SynchronousRestSessionObjectPoster<InventoryItemBase, bool>.BeginPostObject(
  334. "POST", m_ServerURI + "/NewItem/", item, sessionID.ToString(), item.Owner.ToString());
  335. }
  336. catch (Exception e)
  337. {
  338. m_log.ErrorFormat("[INVENTORY CONNECTOR]: Update new inventory item operation failed, {0} {1}",
  339. e.Source, e.Message);
  340. }
  341. return false;
  342. }
  343. /**
  344. * MoveItems Async group
  345. */
  346. delegate void MoveItemsDelegate(string userID, List<InventoryItemBase> items, UUID sessionID);
  347. private void MoveItemsAsync(string userID, List<InventoryItemBase> items, UUID sessionID)
  348. {
  349. try
  350. {
  351. SynchronousRestSessionObjectPoster<List<InventoryItemBase>, bool>.BeginPostObject(
  352. "POST", m_ServerURI + "/MoveItems/", items, sessionID.ToString(), userID.ToString());
  353. // Success
  354. return;
  355. }
  356. catch (Exception e)
  357. {
  358. m_log.ErrorFormat("[INVENTORY CONNECTOR]: Move inventory items operation failed, {0} {1} (old server?). Trying slow way.",
  359. e.Source, e.Message);
  360. }
  361. foreach (InventoryItemBase item in items)
  362. {
  363. InventoryItemBase itm = this.QueryItem(userID, item, sessionID);
  364. itm.Name = item.Name;
  365. itm.Folder = item.Folder;
  366. this.UpdateItem(userID, itm, sessionID);
  367. }
  368. }
  369. private void MoveItemsCompleted(IAsyncResult iar)
  370. {
  371. }
  372. public bool MoveItems(string userID, List<InventoryItemBase> items, UUID sessionID)
  373. {
  374. MoveItemsDelegate d = MoveItemsAsync;
  375. d.BeginInvoke(userID, items, sessionID, MoveItemsCompleted, d);
  376. return true;
  377. }
  378. public bool DeleteItems(string userID, List<UUID> items, UUID sessionID)
  379. {
  380. try
  381. {
  382. List<Guid> guids = new List<Guid>();
  383. foreach (UUID u in items)
  384. guids.Add(u.Guid);
  385. return SynchronousRestSessionObjectPoster<List<Guid>, bool>.BeginPostObject(
  386. "POST", m_ServerURI + "/DeleteItem/", guids, sessionID.ToString(), userID);
  387. }
  388. catch (Exception e)
  389. {
  390. m_log.ErrorFormat("[INVENTORY CONNECTOR]: Delete inventory item operation failed, {0} {1}",
  391. e.Source, e.Message);
  392. }
  393. return false;
  394. }
  395. public InventoryItemBase QueryItem(string userID, InventoryItemBase item, UUID sessionID)
  396. {
  397. try
  398. {
  399. return SynchronousRestSessionObjectPoster<InventoryItemBase, InventoryItemBase>.BeginPostObject(
  400. "POST", m_ServerURI + "/QueryItem/", item, sessionID.ToString(), item.Owner.ToString());
  401. }
  402. catch (Exception e)
  403. {
  404. m_log.ErrorFormat("[INVENTORY CONNECTOR]: Query inventory item operation failed, {0} {1}",
  405. e.Source, e.Message);
  406. }
  407. return null;
  408. }
  409. public InventoryFolderBase QueryFolder(string userID, InventoryFolderBase item, UUID sessionID)
  410. {
  411. try
  412. {
  413. return SynchronousRestSessionObjectPoster<InventoryFolderBase, InventoryFolderBase>.BeginPostObject(
  414. "POST", m_ServerURI + "/QueryFolder/", item, sessionID.ToString(), item.Owner.ToString());
  415. }
  416. catch (Exception e)
  417. {
  418. m_log.ErrorFormat("[INVENTORY CONNECTOR]: Query inventory item operation failed, {0} {1}",
  419. e.Source, e.Message);
  420. }
  421. return null;
  422. }
  423. public int GetAssetPermissions(string userID, UUID assetID, UUID sessionID)
  424. {
  425. try
  426. {
  427. InventoryItemBase item = new InventoryItemBase();
  428. item.Owner = new UUID(userID);
  429. item.AssetID = assetID;
  430. return SynchronousRestSessionObjectPoster<InventoryItemBase, int>.BeginPostObject(
  431. "POST", m_ServerURI + "/AssetPermissions/", item, sessionID.ToString(), userID);
  432. }
  433. catch (Exception e)
  434. {
  435. m_log.ErrorFormat("[INVENTORY CONNECTOR]: AssetPermissions operation failed, {0} {1}",
  436. e.Source, e.Message);
  437. }
  438. return 0;
  439. }
  440. #endregion
  441. /// <summary>
  442. /// Callback used by the inventory server GetInventory request
  443. /// </summary>
  444. /// <param name="userID"></param>
  445. private void InventoryResponse(InventoryCollection response)
  446. {
  447. UUID userID = response.UserID;
  448. InventoryReceiptCallback callback = null;
  449. lock (m_RequestingInventory)
  450. {
  451. if (m_RequestingInventory.ContainsKey(userID))
  452. {
  453. callback = m_RequestingInventory[userID];
  454. m_RequestingInventory.Remove(userID);
  455. lock (m_RequestTime)
  456. {
  457. if (m_RequestTime.ContainsKey(userID))
  458. {
  459. m_RequestTime.Remove(userID);
  460. }
  461. }
  462. }
  463. else
  464. {
  465. m_log.WarnFormat(
  466. "[INVENTORY CONNECTOR]: " +
  467. "Received inventory response for {0} for which we do not have a record of requesting!",
  468. userID);
  469. return;
  470. }
  471. }
  472. m_log.InfoFormat("[INVENTORY CONNECTOR]: " +
  473. "Received inventory response for user {0} containing {1} folders and {2} items",
  474. userID, response.Folders.Count, response.Items.Count);
  475. InventoryFolderImpl rootFolder = null;
  476. ICollection<InventoryFolderImpl> folders = new List<InventoryFolderImpl>();
  477. ICollection<InventoryItemBase> items = new List<InventoryItemBase>();
  478. foreach (InventoryFolderBase folder in response.Folders)
  479. {
  480. if (folder.ParentID == UUID.Zero)
  481. {
  482. rootFolder = new InventoryFolderImpl(folder);
  483. folders.Add(rootFolder);
  484. break;
  485. }
  486. }
  487. if (rootFolder != null)
  488. {
  489. foreach (InventoryFolderBase folder in response.Folders)
  490. {
  491. if (folder.ID != rootFolder.ID)
  492. {
  493. folders.Add(new InventoryFolderImpl(folder));
  494. }
  495. }
  496. foreach (InventoryItemBase item in response.Items)
  497. {
  498. items.Add(item);
  499. }
  500. }
  501. else
  502. {
  503. m_log.ErrorFormat("[INVENTORY CONNECTOR]: Did not get back an inventory containing a root folder for user {0}", userID);
  504. }
  505. callback(folders, items);
  506. }
  507. }
  508. }