InventoryServiceConnector.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  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 (TimeoutException e)
  204. {
  205. m_log.ErrorFormat(
  206. "[INVENTORY CONNECTOR]: GetFolderContent operation to {0} for {1} timed out {2} {3}.",
  207. m_ServerURI, folderID, e.Source, e.Message);
  208. }
  209. catch (Exception e)
  210. {
  211. m_log.ErrorFormat("[INVENTORY CONNECTOR]: GetFolderContent operation failed for {0}, {1} {2} (old server?).",
  212. folderID, e.Source, e.Message);
  213. }
  214. InventoryCollection nullCollection = new InventoryCollection();
  215. nullCollection.Folders = new List<InventoryFolderBase>();
  216. nullCollection.Items = new List<InventoryItemBase>();
  217. nullCollection.UserID = new UUID(userID);
  218. return nullCollection;
  219. }
  220. public bool AddFolder(string userID, InventoryFolderBase folder, UUID sessionID)
  221. {
  222. try
  223. {
  224. return SynchronousRestSessionObjectPoster<InventoryFolderBase, bool>.BeginPostObject(
  225. "POST", m_ServerURI + "/NewFolder/", folder, sessionID.ToString(), folder.Owner.ToString());
  226. }
  227. catch (Exception e)
  228. {
  229. m_log.ErrorFormat("[INVENTORY CONNECTOR]: Add new inventory folder operation failed for {0} {1}, {2} {3}",
  230. folder.Name, folder.ID, e.Source, e.Message);
  231. }
  232. return false;
  233. }
  234. public bool UpdateFolder(string userID, InventoryFolderBase folder, UUID sessionID)
  235. {
  236. try
  237. {
  238. return SynchronousRestSessionObjectPoster<InventoryFolderBase, bool>.BeginPostObject(
  239. "POST", m_ServerURI + "/UpdateFolder/", folder, sessionID.ToString(), folder.Owner.ToString());
  240. }
  241. catch (Exception e)
  242. {
  243. m_log.ErrorFormat("[INVENTORY CONNECTOR]: Update inventory folder operation failed for {0} {1}, {2} {3}",
  244. folder.Name, folder.ID, e.Source, e.Message);
  245. }
  246. return false;
  247. }
  248. public bool DeleteFolders(string userID, List<UUID> folderIDs, UUID sessionID)
  249. {
  250. try
  251. {
  252. List<Guid> guids = new List<Guid>();
  253. foreach (UUID u in folderIDs)
  254. guids.Add(u.Guid);
  255. return SynchronousRestSessionObjectPoster<List<Guid>, bool>.BeginPostObject(
  256. "POST", m_ServerURI + "/DeleteFolders/", guids, sessionID.ToString(), userID);
  257. }
  258. catch (Exception e)
  259. {
  260. m_log.ErrorFormat("[INVENTORY CONNECTOR]: Delete inventory folders operation failed, {0} {1}",
  261. e.Source, e.Message);
  262. }
  263. return false;
  264. }
  265. public bool MoveFolder(string userID, InventoryFolderBase folder, UUID sessionID)
  266. {
  267. try
  268. {
  269. return SynchronousRestSessionObjectPoster<InventoryFolderBase, bool>.BeginPostObject(
  270. "POST", m_ServerURI + "/MoveFolder/", folder, sessionID.ToString(), folder.Owner.ToString());
  271. }
  272. catch (Exception e)
  273. {
  274. m_log.ErrorFormat("[INVENTORY CONNECTOR]: Move inventory folder operation failed for {0} {1}, {2} {3}",
  275. folder.Name, folder.ID, e.Source, e.Message);
  276. }
  277. return false;
  278. }
  279. public bool PurgeFolder(string userID, InventoryFolderBase folder, UUID sessionID)
  280. {
  281. try
  282. {
  283. return SynchronousRestSessionObjectPoster<InventoryFolderBase, bool>.BeginPostObject(
  284. "POST", m_ServerURI + "/PurgeFolder/", folder, sessionID.ToString(), folder.Owner.ToString());
  285. }
  286. catch (Exception e)
  287. {
  288. m_log.ErrorFormat("[INVENTORY CONNECTOR]: Purge inventory folder operation failed for {0} {1}, {2} {3}",
  289. folder.Name, folder.ID, e.Source, e.Message);
  290. }
  291. return false;
  292. }
  293. public List<InventoryItemBase> GetFolderItems(string userID, UUID folderID, UUID sessionID)
  294. {
  295. try
  296. {
  297. InventoryFolderBase folder = new InventoryFolderBase(folderID, new UUID(userID));
  298. return SynchronousRestSessionObjectPoster<InventoryFolderBase, List<InventoryItemBase>>.BeginPostObject(
  299. "POST", m_ServerURI + "/GetItems/", folder, sessionID.ToString(), userID);
  300. }
  301. catch (Exception e)
  302. {
  303. m_log.ErrorFormat("[INVENTORY CONNECTOR]: Get folder items operation failed for folder {0}, {1} {2}",
  304. folderID, e.Source, e.Message);
  305. }
  306. return null;
  307. }
  308. public bool AddItem(string userID, InventoryItemBase item, UUID sessionID)
  309. {
  310. try
  311. {
  312. return SynchronousRestSessionObjectPoster<InventoryItemBase, bool>.BeginPostObject(
  313. "POST", m_ServerURI + "/NewItem/", item, sessionID.ToString(), item.Owner.ToString());
  314. }
  315. catch (Exception e)
  316. {
  317. m_log.ErrorFormat("[INVENTORY CONNECTOR]: Add new inventory item operation failed for {0} {1}, {2} {3}",
  318. item.Name, item.ID, e.Source, e.Message);
  319. }
  320. return false;
  321. }
  322. public bool UpdateItem(string userID, InventoryItemBase item, UUID sessionID)
  323. {
  324. try
  325. {
  326. return SynchronousRestSessionObjectPoster<InventoryItemBase, bool>.BeginPostObject(
  327. "POST", m_ServerURI + "/NewItem/", item, sessionID.ToString(), item.Owner.ToString());
  328. }
  329. catch (Exception e)
  330. {
  331. m_log.ErrorFormat("[INVENTORY CONNECTOR]: Update new inventory item operation failed for {0} {1}, {2} {3}",
  332. item.Name, item.ID, e.Source, e.Message);
  333. }
  334. return false;
  335. }
  336. /**
  337. * MoveItems Async group
  338. */
  339. delegate void MoveItemsDelegate(string userID, List<InventoryItemBase> items, UUID sessionID);
  340. private void MoveItemsAsync(string userID, List<InventoryItemBase> items, UUID sessionID)
  341. {
  342. if (items == null)
  343. {
  344. m_log.WarnFormat("[INVENTORY CONNECTOR]: request to move items got a null list.");
  345. return;
  346. }
  347. try
  348. {
  349. //SynchronousRestSessionObjectPoster<List<InventoryItemBase>, bool>.BeginPostObject(
  350. // "POST", m_ServerURI + "/MoveItems/", items, sessionID.ToString(), userID.ToString());
  351. //// Success
  352. //return;
  353. string uri = m_ServerURI + "/inventory/" + userID;
  354. if (SynchronousRestObjectRequester.
  355. MakeRequest<List<InventoryItemBase>, bool>("PUT", uri, items))
  356. m_log.DebugFormat("[INVENTORY CONNECTOR]: move {0} items poster succeeded {1}", items.Count, uri);
  357. else
  358. m_log.DebugFormat("[INVENTORY CONNECTOR]: move {0} items poster failed {1}", items.Count, uri); ;
  359. return;
  360. }
  361. catch (Exception e)
  362. {
  363. m_log.ErrorFormat("[INVENTORY CONNECTOR]: Move inventory items operation failed, {0} {1} (old server?). Trying slow way.",
  364. e.Source, e.Message);
  365. }
  366. }
  367. private void MoveItemsCompleted(IAsyncResult iar)
  368. {
  369. MoveItemsDelegate d = (MoveItemsDelegate)iar.AsyncState;
  370. d.EndInvoke(iar);
  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 items 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 folder, UUID sessionID)
  410. {
  411. try
  412. {
  413. return SynchronousRestSessionObjectPoster<InventoryFolderBase, InventoryFolderBase>.BeginPostObject(
  414. "POST", m_ServerURI + "/QueryFolder/", folder, sessionID.ToString(), userID);
  415. }
  416. catch (Exception e)
  417. {
  418. m_log.ErrorFormat("[INVENTORY CONNECTOR]: Query inventory folder 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. }