InventoryServiceConnector.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  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("[INVENTORY CONNECTOR]: GetFolderContent operation to {0} timed out {0} {1}.", m_ServerURI,
  206. e.Source, e.Message);
  207. }
  208. catch (Exception e)
  209. {
  210. m_log.ErrorFormat("[INVENTORY CONNECTOR]: GetFolderContent operation failed, {0} {1} (old server?).",
  211. e.Source, e.Message);
  212. }
  213. InventoryCollection nullCollection = new InventoryCollection();
  214. nullCollection.Folders = new List<InventoryFolderBase>();
  215. nullCollection.Items = new List<InventoryItemBase>();
  216. nullCollection.UserID = new UUID(userID);
  217. return nullCollection;
  218. }
  219. public bool AddFolder(string userID, InventoryFolderBase folder, UUID sessionID)
  220. {
  221. try
  222. {
  223. return SynchronousRestSessionObjectPoster<InventoryFolderBase, bool>.BeginPostObject(
  224. "POST", m_ServerURI + "/NewFolder/", folder, sessionID.ToString(), folder.Owner.ToString());
  225. }
  226. catch (Exception e)
  227. {
  228. m_log.ErrorFormat("[INVENTORY CONNECTOR]: Add new inventory folder operation failed, {0} {1}",
  229. e.Source, e.Message);
  230. }
  231. return false;
  232. }
  233. public bool UpdateFolder(string userID, InventoryFolderBase folder, UUID sessionID)
  234. {
  235. try
  236. {
  237. return SynchronousRestSessionObjectPoster<InventoryFolderBase, bool>.BeginPostObject(
  238. "POST", m_ServerURI + "/UpdateFolder/", folder, sessionID.ToString(), folder.Owner.ToString());
  239. }
  240. catch (Exception e)
  241. {
  242. m_log.ErrorFormat("[INVENTORY CONNECTOR]: Update inventory folder operation failed, {0} {1}",
  243. e.Source, e.Message);
  244. }
  245. return false;
  246. }
  247. public bool DeleteFolders(string userID, List<UUID> folderIDs, UUID sessionID)
  248. {
  249. try
  250. {
  251. List<Guid> guids = new List<Guid>();
  252. foreach (UUID u in folderIDs)
  253. guids.Add(u.Guid);
  254. return SynchronousRestSessionObjectPoster<List<Guid>, bool>.BeginPostObject(
  255. "POST", m_ServerURI + "/DeleteFolders/", guids, sessionID.ToString(), userID);
  256. }
  257. catch (Exception e)
  258. {
  259. m_log.ErrorFormat("[INVENTORY CONNECTOR]: Delete inventory folders operation failed, {0} {1}",
  260. e.Source, e.Message);
  261. }
  262. return false;
  263. }
  264. public bool MoveFolder(string userID, InventoryFolderBase folder, UUID sessionID)
  265. {
  266. try
  267. {
  268. return SynchronousRestSessionObjectPoster<InventoryFolderBase, bool>.BeginPostObject(
  269. "POST", m_ServerURI + "/MoveFolder/", folder, sessionID.ToString(), folder.Owner.ToString());
  270. }
  271. catch (Exception e)
  272. {
  273. m_log.ErrorFormat("[INVENTORY CONNECTOR]: Move inventory folder operation failed, {0} {1}",
  274. e.Source, e.Message);
  275. }
  276. return false;
  277. }
  278. public bool PurgeFolder(string userID, InventoryFolderBase folder, UUID sessionID)
  279. {
  280. try
  281. {
  282. return SynchronousRestSessionObjectPoster<InventoryFolderBase, bool>.BeginPostObject(
  283. "POST", m_ServerURI + "/PurgeFolder/", folder, sessionID.ToString(), folder.Owner.ToString());
  284. }
  285. catch (Exception e)
  286. {
  287. m_log.ErrorFormat("[INVENTORY CONNECTOR]: Move inventory folder operation failed, {0} {1}",
  288. e.Source, e.Message);
  289. }
  290. return false;
  291. }
  292. public List<InventoryItemBase> GetFolderItems(string userID, UUID folderID, UUID sessionID)
  293. {
  294. try
  295. {
  296. InventoryFolderBase folder = new InventoryFolderBase(folderID, new UUID(userID));
  297. return SynchronousRestSessionObjectPoster<InventoryFolderBase, List<InventoryItemBase>>.BeginPostObject(
  298. "POST", m_ServerURI + "/GetItems/", folder, sessionID.ToString(), userID);
  299. }
  300. catch (Exception e)
  301. {
  302. m_log.ErrorFormat("[INVENTORY CONNECTOR]: Get folder items operation failed, {0} {1}",
  303. e.Source, e.Message);
  304. }
  305. return null;
  306. }
  307. public bool AddItem(string userID, InventoryItemBase item, UUID sessionID)
  308. {
  309. try
  310. {
  311. return SynchronousRestSessionObjectPoster<InventoryItemBase, bool>.BeginPostObject(
  312. "POST", m_ServerURI + "/NewItem/", item, sessionID.ToString(), item.Owner.ToString());
  313. }
  314. catch (Exception e)
  315. {
  316. m_log.ErrorFormat("[INVENTORY CONNECTOR]: Add new inventory item operation failed, {0} {1}",
  317. e.Source, e.Message);
  318. }
  319. return false;
  320. }
  321. public bool UpdateItem(string userID, InventoryItemBase item, UUID sessionID)
  322. {
  323. try
  324. {
  325. return SynchronousRestSessionObjectPoster<InventoryItemBase, bool>.BeginPostObject(
  326. "POST", m_ServerURI + "/NewItem/", item, sessionID.ToString(), item.Owner.ToString());
  327. }
  328. catch (Exception e)
  329. {
  330. m_log.ErrorFormat("[INVENTORY CONNECTOR]: Update new inventory item operation failed, {0} {1}",
  331. e.Source, e.Message);
  332. }
  333. return false;
  334. }
  335. /**
  336. * MoveItems Async group
  337. */
  338. delegate void MoveItemsDelegate(string userID, List<InventoryItemBase> items, UUID sessionID);
  339. private void MoveItemsAsync(string userID, List<InventoryItemBase> items, UUID sessionID)
  340. {
  341. if (items == null)
  342. {
  343. m_log.WarnFormat("[INVENTORY CONNECTOR]: request to move items got a null list.");
  344. return;
  345. }
  346. try
  347. {
  348. //SynchronousRestSessionObjectPoster<List<InventoryItemBase>, bool>.BeginPostObject(
  349. // "POST", m_ServerURI + "/MoveItems/", items, sessionID.ToString(), userID.ToString());
  350. //// Success
  351. //return;
  352. string uri = m_ServerURI + "/inventory/" + userID;
  353. if (SynchronousRestObjectRequester.
  354. MakeRequest<List<InventoryItemBase>, bool>("PUT", uri, items))
  355. m_log.DebugFormat("[INVENTORY CONNECTOR]: move {0} items poster succeeded {1}", items.Count, uri);
  356. else
  357. m_log.DebugFormat("[INVENTORY CONNECTOR]: move {0} items poster failed {1}", items.Count, uri); ;
  358. return;
  359. }
  360. catch (Exception e)
  361. {
  362. m_log.ErrorFormat("[INVENTORY CONNECTOR]: Move inventory items operation failed, {0} {1} (old server?). Trying slow way.",
  363. e.Source, e.Message);
  364. }
  365. foreach (InventoryItemBase item in items)
  366. {
  367. InventoryItemBase itm = this.QueryItem(userID, item, sessionID);
  368. itm.Name = item.Name;
  369. itm.Folder = item.Folder;
  370. this.UpdateItem(userID, itm, sessionID);
  371. }
  372. }
  373. private void MoveItemsCompleted(IAsyncResult iar)
  374. {
  375. MoveItemsDelegate d = (MoveItemsDelegate)iar.AsyncState;
  376. d.EndInvoke(iar);
  377. }
  378. public bool MoveItems(string userID, List<InventoryItemBase> items, UUID sessionID)
  379. {
  380. MoveItemsDelegate d = MoveItemsAsync;
  381. d.BeginInvoke(userID, items, sessionID, MoveItemsCompleted, d);
  382. return true;
  383. }
  384. public bool DeleteItems(string userID, List<UUID> items, UUID sessionID)
  385. {
  386. try
  387. {
  388. List<Guid> guids = new List<Guid>();
  389. foreach (UUID u in items)
  390. guids.Add(u.Guid);
  391. return SynchronousRestSessionObjectPoster<List<Guid>, bool>.BeginPostObject(
  392. "POST", m_ServerURI + "/DeleteItem/", guids, sessionID.ToString(), userID);
  393. }
  394. catch (Exception e)
  395. {
  396. m_log.ErrorFormat("[INVENTORY CONNECTOR]: Delete inventory item operation failed, {0} {1}",
  397. e.Source, e.Message);
  398. }
  399. return false;
  400. }
  401. public InventoryItemBase QueryItem(string userID, InventoryItemBase item, UUID sessionID)
  402. {
  403. try
  404. {
  405. return SynchronousRestSessionObjectPoster<InventoryItemBase, InventoryItemBase>.BeginPostObject(
  406. "POST", m_ServerURI + "/QueryItem/", item, sessionID.ToString(), item.Owner.ToString());
  407. }
  408. catch (Exception e)
  409. {
  410. m_log.ErrorFormat("[INVENTORY CONNECTOR]: Query inventory item operation failed, {0} {1}",
  411. e.Source, e.Message);
  412. }
  413. return null;
  414. }
  415. public InventoryFolderBase QueryFolder(string userID, InventoryFolderBase folder, UUID sessionID)
  416. {
  417. try
  418. {
  419. return SynchronousRestSessionObjectPoster<InventoryFolderBase, InventoryFolderBase>.BeginPostObject(
  420. "POST", m_ServerURI + "/QueryFolder/", folder, sessionID.ToString(), userID);
  421. }
  422. catch (Exception e)
  423. {
  424. m_log.ErrorFormat("[INVENTORY CONNECTOR]: Query inventory item operation failed, {0} {1}",
  425. e.Source, e.Message);
  426. }
  427. return null;
  428. }
  429. public int GetAssetPermissions(string userID, UUID assetID, UUID sessionID)
  430. {
  431. try
  432. {
  433. InventoryItemBase item = new InventoryItemBase();
  434. item.Owner = new UUID(userID);
  435. item.AssetID = assetID;
  436. return SynchronousRestSessionObjectPoster<InventoryItemBase, int>.BeginPostObject(
  437. "POST", m_ServerURI + "/AssetPermissions/", item, sessionID.ToString(), userID);
  438. }
  439. catch (Exception e)
  440. {
  441. m_log.ErrorFormat("[INVENTORY CONNECTOR]: AssetPermissions operation failed, {0} {1}",
  442. e.Source, e.Message);
  443. }
  444. return 0;
  445. }
  446. #endregion
  447. /// <summary>
  448. /// Callback used by the inventory server GetInventory request
  449. /// </summary>
  450. /// <param name="userID"></param>
  451. private void InventoryResponse(InventoryCollection response)
  452. {
  453. UUID userID = response.UserID;
  454. InventoryReceiptCallback callback = null;
  455. lock (m_RequestingInventory)
  456. {
  457. if (m_RequestingInventory.ContainsKey(userID))
  458. {
  459. callback = m_RequestingInventory[userID];
  460. m_RequestingInventory.Remove(userID);
  461. lock (m_RequestTime)
  462. {
  463. if (m_RequestTime.ContainsKey(userID))
  464. {
  465. m_RequestTime.Remove(userID);
  466. }
  467. }
  468. }
  469. else
  470. {
  471. m_log.WarnFormat(
  472. "[INVENTORY CONNECTOR]: " +
  473. "Received inventory response for {0} for which we do not have a record of requesting!",
  474. userID);
  475. return;
  476. }
  477. }
  478. m_log.InfoFormat("[INVENTORY CONNECTOR]: " +
  479. "Received inventory response for user {0} containing {1} folders and {2} items",
  480. userID, response.Folders.Count, response.Items.Count);
  481. InventoryFolderImpl rootFolder = null;
  482. ICollection<InventoryFolderImpl> folders = new List<InventoryFolderImpl>();
  483. ICollection<InventoryItemBase> items = new List<InventoryItemBase>();
  484. foreach (InventoryFolderBase folder in response.Folders)
  485. {
  486. if (folder.ParentID == UUID.Zero)
  487. {
  488. rootFolder = new InventoryFolderImpl(folder);
  489. folders.Add(rootFolder);
  490. break;
  491. }
  492. }
  493. if (rootFolder != null)
  494. {
  495. foreach (InventoryFolderBase folder in response.Folders)
  496. {
  497. if (folder.ID != rootFolder.ID)
  498. {
  499. folders.Add(new InventoryFolderImpl(folder));
  500. }
  501. }
  502. foreach (InventoryItemBase item in response.Items)
  503. {
  504. items.Add(item);
  505. }
  506. }
  507. else
  508. {
  509. m_log.ErrorFormat("[INVENTORY CONNECTOR]: Did not get back an inventory containing a root folder for user {0}", userID);
  510. }
  511. callback(folders, items);
  512. }
  513. }
  514. }