FetchInvDescHandler.cs 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  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 System;
  28. using System.Collections;
  29. using System.Collections.Generic;
  30. using System.Linq;
  31. using System.Reflection;
  32. using log4net;
  33. using Nini.Config;
  34. using OpenMetaverse;
  35. using OpenMetaverse.StructuredData;
  36. using OpenSim.Framework;
  37. using OpenSim.Framework.Capabilities;
  38. using OpenSim.Region.Framework.Interfaces;
  39. using OpenSim.Framework.Servers.HttpServer;
  40. using OpenSim.Services.Interfaces;
  41. using Caps = OpenSim.Framework.Capabilities.Caps;
  42. namespace OpenSim.Capabilities.Handlers
  43. {
  44. public class FetchInvDescHandler
  45. {
  46. private static readonly ILog m_log =
  47. LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  48. private IInventoryService m_InventoryService;
  49. private ILibraryService m_LibraryService;
  50. private IScene m_Scene;
  51. // private object m_fetchLock = new Object();
  52. public FetchInvDescHandler(IInventoryService invService, ILibraryService libService, IScene s)
  53. {
  54. m_InventoryService = invService;
  55. m_LibraryService = libService;
  56. m_Scene = s;
  57. }
  58. public string FetchInventoryDescendentsRequest(string request, string path, string param, IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
  59. {
  60. //m_log.DebugFormat("[XXX]: FetchInventoryDescendentsRequest in {0}, {1}", (m_Scene == null) ? "none" : m_Scene.Name, request);
  61. // nasty temporary hack here, the linden client falsely
  62. // identifies the uuid 00000000-0000-0000-0000-000000000000
  63. // as a string which breaks us
  64. //
  65. // correctly mark it as a uuid
  66. //
  67. request = request.Replace("<string>00000000-0000-0000-0000-000000000000</string>", "<uuid>00000000-0000-0000-0000-000000000000</uuid>");
  68. // another hack <integer>1</integer> results in a
  69. // System.ArgumentException: Object type System.Int32 cannot
  70. // be converted to target type: System.Boolean
  71. //
  72. request = request.Replace("<key>fetch_folders</key><integer>0</integer>", "<key>fetch_folders</key><boolean>0</boolean>");
  73. request = request.Replace("<key>fetch_folders</key><integer>1</integer>", "<key>fetch_folders</key><boolean>1</boolean>");
  74. Hashtable hash = new Hashtable();
  75. try
  76. {
  77. hash = (Hashtable)LLSD.LLSDDeserialize(Utils.StringToBytes(request));
  78. }
  79. catch (LLSD.LLSDParseException e)
  80. {
  81. m_log.ErrorFormat("[WEB FETCH INV DESC HANDLER]: Fetch error: {0}{1}" + e.Message, e.StackTrace);
  82. m_log.Error("Request: " + request);
  83. }
  84. ArrayList foldersrequested = (ArrayList)hash["folders"];
  85. string response = "";
  86. string bad_folders_response = "";
  87. List<LLSDFetchInventoryDescendents> folders = new List<LLSDFetchInventoryDescendents>();
  88. for (int i = 0; i < foldersrequested.Count; i++)
  89. {
  90. Hashtable inventoryhash = (Hashtable)foldersrequested[i];
  91. LLSDFetchInventoryDescendents llsdRequest = new LLSDFetchInventoryDescendents();
  92. try
  93. {
  94. LLSDHelpers.DeserialiseOSDMap(inventoryhash, llsdRequest);
  95. }
  96. catch (Exception e)
  97. {
  98. m_log.Debug("[WEB FETCH INV DESC HANDLER]: caught exception doing OSD deserialize" + e);
  99. continue;
  100. }
  101. // Filter duplicate folder ids that bad viewers may send
  102. if (folders.Find(f => f.folder_id == llsdRequest.folder_id) == null)
  103. folders.Add(llsdRequest);
  104. }
  105. if (folders.Count > 0)
  106. {
  107. List<UUID> bad_folders = new List<UUID>();
  108. List<InventoryCollectionWithDescendents> invcollSet = Fetch(folders, bad_folders);
  109. //m_log.DebugFormat("[XXX]: Got {0} folders from a request of {1}", invcollSet.Count, folders.Count);
  110. if (invcollSet == null)
  111. {
  112. m_log.DebugFormat("[WEB FETCH INV DESC HANDLER]: Multiple folder fetch failed. Trying old protocol.");
  113. #pragma warning disable 0612
  114. return FetchInventoryDescendentsRequest(foldersrequested, httpRequest, httpResponse);
  115. #pragma warning restore 0612
  116. }
  117. string inventoryitemstr = string.Empty;
  118. foreach (InventoryCollectionWithDescendents icoll in invcollSet)
  119. {
  120. LLSDInventoryDescendents reply = ToLLSD(icoll.Collection, icoll.Descendents);
  121. inventoryitemstr = LLSDHelpers.SerialiseLLSDReply(reply);
  122. inventoryitemstr = inventoryitemstr.Replace("<llsd><map><key>folders</key><array>", "");
  123. inventoryitemstr = inventoryitemstr.Replace("</array></map></llsd>", "");
  124. response += inventoryitemstr;
  125. }
  126. //m_log.DebugFormat("[WEB FETCH INV DESC HANDLER]: Bad folders {0}", string.Join(", ", bad_folders));
  127. foreach (UUID bad in bad_folders)
  128. bad_folders_response += "<uuid>" + bad + "</uuid>";
  129. }
  130. if (response.Length == 0)
  131. {
  132. /* Viewers expect a bad_folders array when not available */
  133. if (bad_folders_response.Length != 0)
  134. {
  135. response = "<llsd><map><key>bad_folders</key><array>" + bad_folders_response + "</array></map></llsd>";
  136. }
  137. else
  138. {
  139. response = "<llsd><map><key>folders</key><array /></map></llsd>";
  140. }
  141. }
  142. else
  143. {
  144. if (bad_folders_response.Length != 0)
  145. {
  146. response = "<llsd><map><key>folders</key><array>" + response + "</array><key>bad_folders</key><array>" + bad_folders_response + "</array></map></llsd>";
  147. }
  148. else
  149. {
  150. response = "<llsd><map><key>folders</key><array>" + response + "</array></map></llsd>";
  151. }
  152. }
  153. //m_log.DebugFormat("[WEB FETCH INV DESC HANDLER]: Replying to CAPS fetch inventory request for {0} folders. Item count {1}", folders.Count, item_count);
  154. //m_log.Debug("[WEB FETCH INV DESC HANDLER] " + response);
  155. return response;
  156. }
  157. /// <summary>
  158. /// Construct an LLSD reply packet to a CAPS inventory request
  159. /// </summary>
  160. /// <param name="invFetch"></param>
  161. /// <returns></returns>
  162. private LLSDInventoryDescendents FetchInventoryReply(LLSDFetchInventoryDescendents invFetch)
  163. {
  164. LLSDInventoryDescendents reply = new LLSDInventoryDescendents();
  165. LLSDInventoryFolderContents contents = new LLSDInventoryFolderContents();
  166. contents.agent_id = invFetch.owner_id;
  167. contents.owner_id = invFetch.owner_id;
  168. contents.folder_id = invFetch.folder_id;
  169. reply.folders.Array.Add(contents);
  170. InventoryCollection inv = new InventoryCollection();
  171. inv.Folders = new List<InventoryFolderBase>();
  172. inv.Items = new List<InventoryItemBase>();
  173. int version = 0;
  174. int descendents = 0;
  175. #pragma warning disable 0612
  176. inv = Fetch(
  177. invFetch.owner_id, invFetch.folder_id, invFetch.owner_id,
  178. invFetch.fetch_folders, invFetch.fetch_items, invFetch.sort_order, out version, out descendents);
  179. #pragma warning restore 0612
  180. if (inv != null && inv.Folders != null)
  181. {
  182. foreach (InventoryFolderBase invFolder in inv.Folders)
  183. {
  184. contents.categories.Array.Add(ConvertInventoryFolder(invFolder));
  185. }
  186. descendents += inv.Folders.Count;
  187. }
  188. if (inv != null && inv.Items != null)
  189. {
  190. foreach (InventoryItemBase invItem in inv.Items)
  191. {
  192. contents.items.Array.Add(ConvertInventoryItem(invItem));
  193. }
  194. }
  195. contents.descendents = descendents;
  196. contents.version = version;
  197. //m_log.DebugFormat(
  198. // "[WEB FETCH INV DESC HANDLER]: Replying to request for folder {0} (fetch items {1}, fetch folders {2}) with {3} items and {4} folders for agent {5}",
  199. // invFetch.folder_id,
  200. // invFetch.fetch_items,
  201. // invFetch.fetch_folders,
  202. // contents.items.Array.Count,
  203. // contents.categories.Array.Count,
  204. // invFetch.owner_id);
  205. return reply;
  206. }
  207. private LLSDInventoryDescendents ToLLSD(InventoryCollection inv, int descendents)
  208. {
  209. LLSDInventoryDescendents reply = new LLSDInventoryDescendents();
  210. LLSDInventoryFolderContents contents = new LLSDInventoryFolderContents();
  211. contents.agent_id = inv.OwnerID;
  212. contents.owner_id = inv.OwnerID;
  213. contents.folder_id = inv.FolderID;
  214. reply.folders.Array.Add(contents);
  215. if (inv.Folders != null)
  216. {
  217. foreach (InventoryFolderBase invFolder in inv.Folders)
  218. {
  219. contents.categories.Array.Add(ConvertInventoryFolder(invFolder));
  220. }
  221. descendents += inv.Folders.Count;
  222. }
  223. if (inv.Items != null)
  224. {
  225. foreach (InventoryItemBase invItem in inv.Items)
  226. {
  227. contents.items.Array.Add(ConvertInventoryItem(invItem));
  228. }
  229. }
  230. contents.descendents = descendents;
  231. contents.version = inv.Version;
  232. return reply;
  233. }
  234. /// <summary>
  235. /// Old style. Soon to be deprecated.
  236. /// </summary>
  237. /// <param name="request"></param>
  238. /// <param name="httpRequest"></param>
  239. /// <param name="httpResponse"></param>
  240. /// <returns></returns>
  241. [Obsolete]
  242. private string FetchInventoryDescendentsRequest(ArrayList foldersrequested, IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
  243. {
  244. //m_log.DebugFormat("[WEB FETCH INV DESC HANDLER]: Received request for {0} folders", foldersrequested.Count);
  245. string response = "";
  246. string bad_folders_response = "";
  247. for (int i = 0; i < foldersrequested.Count; i++)
  248. {
  249. string inventoryitemstr = "";
  250. Hashtable inventoryhash = (Hashtable)foldersrequested[i];
  251. LLSDFetchInventoryDescendents llsdRequest = new LLSDFetchInventoryDescendents();
  252. try
  253. {
  254. LLSDHelpers.DeserialiseOSDMap(inventoryhash, llsdRequest);
  255. }
  256. catch (Exception e)
  257. {
  258. m_log.Debug("[WEB FETCH INV DESC HANDLER]: caught exception doing OSD deserialize" + e);
  259. }
  260. LLSDInventoryDescendents reply = FetchInventoryReply(llsdRequest);
  261. if (null == reply)
  262. {
  263. bad_folders_response += "<uuid>" + llsdRequest.folder_id.ToString() + "</uuid>";
  264. }
  265. else
  266. {
  267. inventoryitemstr = LLSDHelpers.SerialiseLLSDReply(reply);
  268. inventoryitemstr = inventoryitemstr.Replace("<llsd><map><key>folders</key><array>", "");
  269. inventoryitemstr = inventoryitemstr.Replace("</array></map></llsd>", "");
  270. }
  271. response += inventoryitemstr;
  272. }
  273. if (response.Length == 0)
  274. {
  275. /* Viewers expect a bad_folders array when not available */
  276. if (bad_folders_response.Length != 0)
  277. {
  278. response = "<llsd><map><key>bad_folders</key><array>" + bad_folders_response + "</array></map></llsd>";
  279. }
  280. else
  281. {
  282. response = "<llsd><map><key>folders</key><array /></map></llsd>";
  283. }
  284. }
  285. else
  286. {
  287. if (bad_folders_response.Length != 0)
  288. {
  289. response = "<llsd><map><key>folders</key><array>" + response + "</array><key>bad_folders</key><array>" + bad_folders_response + "</array></map></llsd>";
  290. }
  291. else
  292. {
  293. response = "<llsd><map><key>folders</key><array>" + response + "</array></map></llsd>";
  294. }
  295. }
  296. // m_log.DebugFormat("[WEB FETCH INV DESC HANDLER]: Replying to CAPS fetch inventory request");
  297. //m_log.Debug("[WEB FETCH INV DESC HANDLER] "+response);
  298. return response;
  299. // }
  300. }
  301. /// <summary>
  302. /// Handle the caps inventory descendents fetch.
  303. /// </summary>
  304. /// <param name="agentID"></param>
  305. /// <param name="folderID"></param>
  306. /// <param name="ownerID"></param>
  307. /// <param name="fetchFolders"></param>
  308. /// <param name="fetchItems"></param>
  309. /// <param name="sortOrder"></param>
  310. /// <param name="version"></param>
  311. /// <returns>An empty InventoryCollection if the inventory look up failed</returns>
  312. [Obsolete]
  313. private InventoryCollection Fetch(
  314. UUID agentID, UUID folderID, UUID ownerID,
  315. bool fetchFolders, bool fetchItems, int sortOrder, out int version, out int descendents)
  316. {
  317. //m_log.DebugFormat(
  318. // "[WEB FETCH INV DESC HANDLER]: Fetching folders ({0}), items ({1}) from {2} for agent {3}",
  319. // fetchFolders, fetchItems, folderID, agentID);
  320. // FIXME MAYBE: We're not handling sortOrder!
  321. version = 0;
  322. descendents = 0;
  323. InventoryFolderImpl fold;
  324. if (m_LibraryService != null && m_LibraryService.LibraryRootFolder != null && agentID == m_LibraryService.LibraryRootFolder.Owner)
  325. {
  326. if ((fold = m_LibraryService.LibraryRootFolder.FindFolder(folderID)) != null)
  327. {
  328. InventoryCollection ret = new InventoryCollection();
  329. ret.Folders = new List<InventoryFolderBase>();
  330. ret.Items = fold.RequestListOfItems();
  331. descendents = ret.Folders.Count + ret.Items.Count;
  332. return ret;
  333. }
  334. }
  335. InventoryCollection contents = new InventoryCollection();
  336. if (folderID != UUID.Zero)
  337. {
  338. InventoryCollection fetchedContents = m_InventoryService.GetFolderContent(agentID, folderID);
  339. if (fetchedContents == null)
  340. {
  341. m_log.WarnFormat("[WEB FETCH INV DESC HANDLER]: Could not get contents of folder {0} for user {1}", folderID, agentID);
  342. return contents;
  343. }
  344. contents = fetchedContents;
  345. InventoryFolderBase containingFolder = new InventoryFolderBase();
  346. containingFolder.ID = folderID;
  347. containingFolder.Owner = agentID;
  348. containingFolder = m_InventoryService.GetFolder(containingFolder);
  349. if (containingFolder != null)
  350. {
  351. //m_log.DebugFormat(
  352. // "[WEB FETCH INV DESC HANDLER]: Retrieved folder {0} {1} for agent id {2}",
  353. // containingFolder.Name, containingFolder.ID, agentID);
  354. version = containingFolder.Version;
  355. if (fetchItems && containingFolder.Type != (short)FolderType.Trash)
  356. {
  357. List<InventoryItemBase> itemsToReturn = contents.Items;
  358. List<InventoryItemBase> originalItems = new List<InventoryItemBase>(itemsToReturn);
  359. // descendents must only include the links, not the linked items we add
  360. descendents = originalItems.Count;
  361. // Add target items for links in this folder before the links themselves.
  362. foreach (InventoryItemBase item in originalItems)
  363. {
  364. if (item.AssetType == (int)AssetType.Link)
  365. {
  366. InventoryItemBase linkedItem = m_InventoryService.GetItem(new InventoryItemBase(item.AssetID));
  367. // Take care of genuinely broken links where the target doesn't exist
  368. // HACK: Also, don't follow up links that just point to other links. In theory this is legitimate,
  369. // but no viewer has been observed to set these up and this is the lazy way of avoiding cycles
  370. // rather than having to keep track of every folder requested in the recursion.
  371. if (linkedItem != null && linkedItem.AssetType != (int)AssetType.Link)
  372. itemsToReturn.Insert(0, linkedItem);
  373. }
  374. }
  375. // Now scan for folder links and insert the items they target and those links at the head of the return data
  376. /* dont send contents of LinkFolders.
  377. from docs seems this was never a spec
  378. foreach (InventoryItemBase item in originalItems)
  379. {
  380. if (item.AssetType == (int)AssetType.LinkFolder)
  381. {
  382. InventoryCollection linkedFolderContents = m_InventoryService.GetFolderContent(ownerID, item.AssetID);
  383. List<InventoryItemBase> links = linkedFolderContents.Items;
  384. itemsToReturn.InsertRange(0, links);
  385. foreach (InventoryItemBase link in linkedFolderContents.Items)
  386. {
  387. // Take care of genuinely broken links where the target doesn't exist
  388. // HACK: Also, don't follow up links that just point to other links. In theory this is legitimate,
  389. // but no viewer has been observed to set these up and this is the lazy way of avoiding cycles
  390. // rather than having to keep track of every folder requested in the recursion.
  391. if (link != null)
  392. {
  393. // m_log.DebugFormat(
  394. // "[WEB FETCH INV DESC HANDLER]: Adding item {0} {1} from folder {2} linked from {3}",
  395. // link.Name, (AssetType)link.AssetType, item.AssetID, containingFolder.Name);
  396. InventoryItemBase linkedItem
  397. = m_InventoryService.GetItem(new InventoryItemBase(link.AssetID));
  398. if (linkedItem != null)
  399. itemsToReturn.Insert(0, linkedItem);
  400. }
  401. }
  402. }
  403. }
  404. */
  405. }
  406. // foreach (InventoryItemBase item in contents.Items)
  407. // {
  408. // m_log.DebugFormat(
  409. // "[WEB FETCH INV DESC HANDLER]: Returning item {0}, type {1}, parent {2} in {3} {4}",
  410. // item.Name, (AssetType)item.AssetType, item.Folder, containingFolder.Name, containingFolder.ID);
  411. // }
  412. // =====
  413. //
  414. // foreach (InventoryItemBase linkedItem in linkedItemsToAdd)
  415. // {
  416. // m_log.DebugFormat(
  417. // "[WEB FETCH INV DESC HANDLER]: Inserted linked item {0} for link in folder {1} for agent {2}",
  418. // linkedItem.Name, folderID, agentID);
  419. //
  420. // contents.Items.Add(linkedItem);
  421. // }
  422. //
  423. // // If the folder requested contains links, then we need to send those folders first, otherwise the links
  424. // // will be broken in the viewer.
  425. // HashSet<UUID> linkedItemFolderIdsToSend = new HashSet<UUID>();
  426. // foreach (InventoryItemBase item in contents.Items)
  427. // {
  428. // if (item.AssetType == (int)AssetType.Link)
  429. // {
  430. // InventoryItemBase linkedItem = m_InventoryService.GetItem(new InventoryItemBase(item.AssetID));
  431. //
  432. // // Take care of genuinely broken links where the target doesn't exist
  433. // // HACK: Also, don't follow up links that just point to other links. In theory this is legitimate,
  434. // // but no viewer has been observed to set these up and this is the lazy way of avoiding cycles
  435. // // rather than having to keep track of every folder requested in the recursion.
  436. // if (linkedItem != null && linkedItem.AssetType != (int)AssetType.Link)
  437. // {
  438. // // We don't need to send the folder if source and destination of the link are in the same
  439. // // folder.
  440. // if (linkedItem.Folder != containingFolder.ID)
  441. // linkedItemFolderIdsToSend.Add(linkedItem.Folder);
  442. // }
  443. // }
  444. // }
  445. //
  446. // foreach (UUID linkedItemFolderId in linkedItemFolderIdsToSend)
  447. // {
  448. // m_log.DebugFormat(
  449. // "[WEB FETCH INV DESC HANDLER]: Recursively fetching folder {0} linked by item in folder {1} for agent {2}",
  450. // linkedItemFolderId, folderID, agentID);
  451. //
  452. // int dummyVersion;
  453. // InventoryCollection linkedCollection
  454. // = Fetch(
  455. // agentID, linkedItemFolderId, ownerID, fetchFolders, fetchItems, sortOrder, out dummyVersion);
  456. //
  457. // InventoryFolderBase linkedFolder = new InventoryFolderBase(linkedItemFolderId);
  458. // linkedFolder.Owner = agentID;
  459. // linkedFolder = m_InventoryService.GetFolder(linkedFolder);
  460. //
  461. //// contents.Folders.AddRange(linkedCollection.Folders);
  462. //
  463. // contents.Folders.Add(linkedFolder);
  464. // contents.Items.AddRange(linkedCollection.Items);
  465. // }
  466. // }
  467. }
  468. }
  469. else
  470. {
  471. // Lost items don't really need a version
  472. version = 1;
  473. }
  474. return contents;
  475. }
  476. private void AddLibraryFolders(List<LLSDFetchInventoryDescendents> fetchFolders, List<InventoryCollectionWithDescendents> result)
  477. {
  478. InventoryFolderImpl fold;
  479. if (m_LibraryService != null && m_LibraryService.LibraryRootFolder != null)
  480. {
  481. List<LLSDFetchInventoryDescendents> libfolders = fetchFolders.FindAll(f => f.owner_id == m_LibraryService.LibraryRootFolder.Owner);
  482. fetchFolders.RemoveAll(f => libfolders.Contains(f));
  483. //m_log.DebugFormat("[XXX]: Found {0} library folders in request", libfolders.Count);
  484. foreach (LLSDFetchInventoryDescendents f in libfolders)
  485. {
  486. if ((fold = m_LibraryService.LibraryRootFolder.FindFolder(f.folder_id)) != null)
  487. {
  488. InventoryCollectionWithDescendents ret = new InventoryCollectionWithDescendents();
  489. ret.Collection = new InventoryCollection();
  490. ret.Collection.Folders = new List<InventoryFolderBase>();
  491. ret.Collection.Items = fold.RequestListOfItems();
  492. ret.Collection.OwnerID = m_LibraryService.LibraryRootFolder.Owner;
  493. ret.Collection.FolderID = f.folder_id;
  494. ret.Collection.Version = fold.Version;
  495. ret.Descendents = ret.Collection.Items.Count;
  496. result.Add(ret);
  497. //m_log.DebugFormat("[XXX]: Added libfolder {0} ({1}) {2}", ret.Collection.FolderID, ret.Collection.OwnerID);
  498. }
  499. }
  500. }
  501. }
  502. private List<InventoryCollectionWithDescendents> Fetch(List<LLSDFetchInventoryDescendents> fetchFolders, List<UUID> bad_folders)
  503. {
  504. //m_log.DebugFormat(
  505. // "[WEB FETCH INV DESC HANDLER]: Fetching {0} folders for owner {1}", fetchFolders.Count, fetchFolders[0].owner_id);
  506. // FIXME MAYBE: We're not handling sortOrder!
  507. List<InventoryCollectionWithDescendents> result = new List<InventoryCollectionWithDescendents>();
  508. AddLibraryFolders(fetchFolders, result);
  509. // Filter folder Zero right here. Some viewers (Firestorm) send request for folder Zero, which doesn't make sense
  510. // and can kill the sim (all root folders have parent_id Zero)
  511. LLSDFetchInventoryDescendents zero = fetchFolders.Find(f => f.folder_id == UUID.Zero);
  512. if (zero != null)
  513. {
  514. fetchFolders.Remove(zero);
  515. BadFolder(zero, null, bad_folders);
  516. }
  517. if (fetchFolders.Count > 0)
  518. {
  519. UUID[] fids = new UUID[fetchFolders.Count];
  520. int i = 0;
  521. foreach (LLSDFetchInventoryDescendents f in fetchFolders)
  522. fids[i++] = f.folder_id;
  523. //m_log.DebugFormat("[XXX]: {0}", string.Join(",", fids));
  524. InventoryCollection[] fetchedContents = m_InventoryService.GetMultipleFoldersContent(fetchFolders[0].owner_id, fids);
  525. if (fetchedContents == null || (fetchedContents != null && fetchedContents.Length == 0))
  526. {
  527. m_log.WarnFormat("[WEB FETCH INV DESC HANDLER]: Could not get contents of multiple folders for user {0}", fetchFolders[0].owner_id);
  528. foreach (LLSDFetchInventoryDescendents freq in fetchFolders)
  529. BadFolder(freq, null, bad_folders);
  530. return null;
  531. }
  532. i = 0;
  533. // Do some post-processing. May need to fetch more from inv server for links
  534. foreach (InventoryCollection contents in fetchedContents)
  535. {
  536. // Find the original request
  537. LLSDFetchInventoryDescendents freq = fetchFolders[i++];
  538. InventoryCollectionWithDescendents coll = new InventoryCollectionWithDescendents();
  539. coll.Collection = contents;
  540. if (BadFolder(freq, contents, bad_folders))
  541. continue;
  542. // Next: link management
  543. ProcessLinks(freq, coll);
  544. result.Add(coll);
  545. }
  546. }
  547. return result;
  548. }
  549. private bool BadFolder(LLSDFetchInventoryDescendents freq, InventoryCollection contents, List<UUID> bad_folders)
  550. {
  551. bool bad = false;
  552. if (contents == null)
  553. {
  554. bad_folders.Add(freq.folder_id);
  555. bad = true;
  556. }
  557. // The inventory server isn't sending FolderID in the collection...
  558. // Must fetch it individually
  559. else if (contents.FolderID == UUID.Zero)
  560. {
  561. InventoryFolderBase containingFolder = new InventoryFolderBase();
  562. containingFolder.ID = freq.folder_id;
  563. containingFolder.Owner = freq.owner_id;
  564. containingFolder = m_InventoryService.GetFolder(containingFolder);
  565. if (containingFolder != null)
  566. {
  567. contents.FolderID = containingFolder.ID;
  568. contents.OwnerID = containingFolder.Owner;
  569. contents.Version = containingFolder.Version;
  570. }
  571. else
  572. {
  573. // Was it really a request for folder Zero?
  574. // This is an overkill, but Firestorm really asks for folder Zero.
  575. // I'm leaving the code here for the time being, but commented.
  576. if (freq.folder_id == UUID.Zero)
  577. {
  578. //coll.Collection.OwnerID = freq.owner_id;
  579. //coll.Collection.FolderID = contents.FolderID;
  580. //containingFolder = m_InventoryService.GetRootFolder(freq.owner_id);
  581. //if (containingFolder != null)
  582. //{
  583. // m_log.WarnFormat("[WEB FETCH INV DESC HANDLER]: Request for parent of folder {0}", containingFolder.ID);
  584. // coll.Collection.Folders.Clear();
  585. // coll.Collection.Folders.Add(containingFolder);
  586. // if (m_LibraryService != null && m_LibraryService.LibraryRootFolder != null)
  587. // {
  588. // InventoryFolderBase lib = new InventoryFolderBase(m_LibraryService.LibraryRootFolder.ID, m_LibraryService.LibraryRootFolder.Owner);
  589. // lib.Name = m_LibraryService.LibraryRootFolder.Name;
  590. // lib.Type = m_LibraryService.LibraryRootFolder.Type;
  591. // lib.Version = m_LibraryService.LibraryRootFolder.Version;
  592. // coll.Collection.Folders.Add(lib);
  593. // }
  594. // coll.Collection.Items.Clear();
  595. //}
  596. }
  597. else
  598. {
  599. m_log.WarnFormat("[WEB FETCH INV DESC HANDLER]: Unable to fetch folder {0}", freq.folder_id);
  600. bad_folders.Add(freq.folder_id);
  601. }
  602. bad = true;
  603. }
  604. }
  605. return bad;
  606. }
  607. private void ProcessLinks(LLSDFetchInventoryDescendents freq, InventoryCollectionWithDescendents coll)
  608. {
  609. InventoryCollection contents = coll.Collection;
  610. if (freq.fetch_items && contents.Items != null)
  611. {
  612. List<InventoryItemBase> itemsToReturn = contents.Items;
  613. // descendents must only include the links, not the linked items we add
  614. coll.Descendents = itemsToReturn.Count;
  615. // Add target items for links in this folder before the links themselves.
  616. List<UUID> itemIDs = new List<UUID>();
  617. List<UUID> folderIDs = new List<UUID>();
  618. foreach (InventoryItemBase item in itemsToReturn)
  619. {
  620. //m_log.DebugFormat("[XXX]: {0} {1}", item.Name, item.AssetType);
  621. if (item.AssetType == (int)AssetType.Link)
  622. itemIDs.Add(item.AssetID);
  623. // else if (item.AssetType == (int)AssetType.LinkFolder)
  624. // folderIDs.Add(item.AssetID);
  625. }
  626. //m_log.DebugFormat("[XXX]: folder {0} has {1} links and {2} linkfolders", contents.FolderID, itemIDs.Count, folderIDs.Count);
  627. // Scan for folder links and insert the items they target and those links at the head of the return data
  628. if (folderIDs.Count > 0)
  629. {
  630. InventoryCollection[] linkedFolders = m_InventoryService.GetMultipleFoldersContent(coll.Collection.OwnerID, folderIDs.ToArray());
  631. foreach (InventoryCollection linkedFolderContents in linkedFolders)
  632. {
  633. if (linkedFolderContents == null)
  634. continue;
  635. List<InventoryItemBase> links = linkedFolderContents.Items;
  636. itemsToReturn.InsertRange(0, links);
  637. }
  638. }
  639. if (itemIDs.Count > 0)
  640. {
  641. InventoryItemBase[] linked = m_InventoryService.GetMultipleItems(freq.owner_id, itemIDs.ToArray());
  642. if (linked == null)
  643. {
  644. // OMG!!! One by one!!! This is fallback code, in case the backend isn't updated
  645. m_log.WarnFormat("[WEB FETCH INV DESC HANDLER]: GetMultipleItems failed. Falling back to fetching inventory items one by one.");
  646. linked = new InventoryItemBase[itemIDs.Count];
  647. int i = 0;
  648. InventoryItemBase item = new InventoryItemBase();
  649. item.Owner = freq.owner_id;
  650. foreach (UUID id in itemIDs)
  651. {
  652. item.ID = id;
  653. linked[i++] = m_InventoryService.GetItem(item);
  654. }
  655. }
  656. //m_log.DebugFormat("[WEB FETCH INV DESC HANDLER]: Processing folder {0}. Existing items:", freq.folder_id);
  657. //foreach (InventoryItemBase item in itemsToReturn)
  658. // m_log.DebugFormat("[XXX]: {0} {1} {2}", item.Name, item.AssetType, item.Folder);
  659. if (linked != null)
  660. {
  661. foreach (InventoryItemBase linkedItem in linked)
  662. {
  663. // Take care of genuinely broken links where the target doesn't exist
  664. // HACK: Also, don't follow up links that just point to other links. In theory this is legitimate,
  665. // but no viewer has been observed to set these up and this is the lazy way of avoiding cycles
  666. // rather than having to keep track of every folder requested in the recursion.
  667. if (linkedItem != null && linkedItem.AssetType != (int)AssetType.Link)
  668. {
  669. itemsToReturn.Insert(0, linkedItem);
  670. //m_log.DebugFormat("[WEB FETCH INV DESC HANDLER]: Added {0} {1} {2}", linkedItem.Name, linkedItem.AssetType, linkedItem.Folder);
  671. }
  672. }
  673. }
  674. }
  675. }
  676. }
  677. /// <summary>
  678. /// Convert an internal inventory folder object into an LLSD object.
  679. /// </summary>
  680. /// <param name="invFolder"></param>
  681. /// <returns></returns>
  682. private LLSDInventoryFolder ConvertInventoryFolder(InventoryFolderBase invFolder)
  683. {
  684. LLSDInventoryFolder llsdFolder = new LLSDInventoryFolder();
  685. llsdFolder.folder_id = invFolder.ID;
  686. llsdFolder.parent_id = invFolder.ParentID;
  687. llsdFolder.name = invFolder.Name;
  688. llsdFolder.type = invFolder.Type;
  689. llsdFolder.preferred_type = -1;
  690. return llsdFolder;
  691. }
  692. /// <summary>
  693. /// Convert an internal inventory item object into an LLSD object.
  694. /// </summary>
  695. /// <param name="invItem"></param>
  696. /// <returns></returns>
  697. private LLSDInventoryItem ConvertInventoryItem(InventoryItemBase invItem)
  698. {
  699. LLSDInventoryItem llsdItem = new LLSDInventoryItem();
  700. llsdItem.asset_id = invItem.AssetID;
  701. llsdItem.created_at = invItem.CreationDate;
  702. llsdItem.desc = invItem.Description;
  703. llsdItem.flags = (int)invItem.Flags;
  704. llsdItem.item_id = invItem.ID;
  705. llsdItem.name = invItem.Name;
  706. llsdItem.parent_id = invItem.Folder;
  707. llsdItem.type = invItem.AssetType;
  708. llsdItem.inv_type = invItem.InvType;
  709. llsdItem.permissions = new LLSDPermissions();
  710. llsdItem.permissions.creator_id = invItem.CreatorIdAsUuid;
  711. llsdItem.permissions.base_mask = (int)invItem.CurrentPermissions;
  712. llsdItem.permissions.everyone_mask = (int)invItem.EveryOnePermissions;
  713. llsdItem.permissions.group_id = invItem.GroupID;
  714. llsdItem.permissions.group_mask = (int)invItem.GroupPermissions;
  715. llsdItem.permissions.is_owner_group = invItem.GroupOwned;
  716. llsdItem.permissions.next_owner_mask = (int)invItem.NextPermissions;
  717. llsdItem.permissions.owner_id = invItem.Owner;
  718. llsdItem.permissions.owner_mask = (int)invItem.CurrentPermissions;
  719. llsdItem.sale_info = new LLSDSaleInfo();
  720. llsdItem.sale_info.sale_price = invItem.SalePrice;
  721. llsdItem.sale_info.sale_type = invItem.SaleType;
  722. return llsdItem;
  723. }
  724. }
  725. class InventoryCollectionWithDescendents
  726. {
  727. public InventoryCollection Collection;
  728. public int Descendents;
  729. }
  730. }