FetchInvDescHandler.cs 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  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 = m_InventoryService.GetFolder(agentID, folderID);
  346. if (containingFolder != null)
  347. {
  348. //m_log.DebugFormat(
  349. // "[WEB FETCH INV DESC HANDLER]: Retrieved folder {0} {1} for agent id {2}",
  350. // containingFolder.Name, containingFolder.ID, agentID);
  351. version = containingFolder.Version;
  352. if (fetchItems && containingFolder.Type != (short)FolderType.Trash)
  353. {
  354. List<InventoryItemBase> itemsToReturn = contents.Items;
  355. List<InventoryItemBase> originalItems = new List<InventoryItemBase>(itemsToReturn);
  356. // descendents must only include the links, not the linked items we add
  357. descendents = originalItems.Count;
  358. // Add target items for links in this folder before the links themselves.
  359. foreach (InventoryItemBase item in originalItems)
  360. {
  361. if (item.AssetType == (int)AssetType.Link)
  362. {
  363. InventoryItemBase linkedItem = m_InventoryService.GetItem(agentID, item.AssetID);
  364. // Take care of genuinely broken links where the target doesn't exist
  365. // HACK: Also, don't follow up links that just point to other links. In theory this is legitimate,
  366. // but no viewer has been observed to set these up and this is the lazy way of avoiding cycles
  367. // rather than having to keep track of every folder requested in the recursion.
  368. if (linkedItem != null && linkedItem.AssetType != (int)AssetType.Link)
  369. itemsToReturn.Insert(0, linkedItem);
  370. }
  371. }
  372. // Now scan for folder links and insert the items they target and those links at the head of the return data
  373. /* dont send contents of LinkFolders.
  374. from docs seems this was never a spec
  375. foreach (InventoryItemBase item in originalItems)
  376. {
  377. if (item.AssetType == (int)AssetType.LinkFolder)
  378. {
  379. InventoryCollection linkedFolderContents = m_InventoryService.GetFolderContent(ownerID, item.AssetID);
  380. List<InventoryItemBase> links = linkedFolderContents.Items;
  381. itemsToReturn.InsertRange(0, links);
  382. foreach (InventoryItemBase link in linkedFolderContents.Items)
  383. {
  384. // Take care of genuinely broken links where the target doesn't exist
  385. // HACK: Also, don't follow up links that just point to other links. In theory this is legitimate,
  386. // but no viewer has been observed to set these up and this is the lazy way of avoiding cycles
  387. // rather than having to keep track of every folder requested in the recursion.
  388. if (link != null)
  389. {
  390. // m_log.DebugFormat(
  391. // "[WEB FETCH INV DESC HANDLER]: Adding item {0} {1} from folder {2} linked from {3}",
  392. // link.Name, (AssetType)link.AssetType, item.AssetID, containingFolder.Name);
  393. InventoryItemBase linkedItem
  394. = m_InventoryService.GetItem(new InventoryItemBase(link.AssetID));
  395. if (linkedItem != null)
  396. itemsToReturn.Insert(0, linkedItem);
  397. }
  398. }
  399. }
  400. }
  401. */
  402. }
  403. // foreach (InventoryItemBase item in contents.Items)
  404. // {
  405. // m_log.DebugFormat(
  406. // "[WEB FETCH INV DESC HANDLER]: Returning item {0}, type {1}, parent {2} in {3} {4}",
  407. // item.Name, (AssetType)item.AssetType, item.Folder, containingFolder.Name, containingFolder.ID);
  408. // }
  409. // =====
  410. //
  411. // foreach (InventoryItemBase linkedItem in linkedItemsToAdd)
  412. // {
  413. // m_log.DebugFormat(
  414. // "[WEB FETCH INV DESC HANDLER]: Inserted linked item {0} for link in folder {1} for agent {2}",
  415. // linkedItem.Name, folderID, agentID);
  416. //
  417. // contents.Items.Add(linkedItem);
  418. // }
  419. //
  420. // // If the folder requested contains links, then we need to send those folders first, otherwise the links
  421. // // will be broken in the viewer.
  422. // HashSet<UUID> linkedItemFolderIdsToSend = new HashSet<UUID>();
  423. // foreach (InventoryItemBase item in contents.Items)
  424. // {
  425. // if (item.AssetType == (int)AssetType.Link)
  426. // {
  427. // InventoryItemBase linkedItem = m_InventoryService.GetItem(new InventoryItemBase(item.AssetID));
  428. //
  429. // // Take care of genuinely broken links where the target doesn't exist
  430. // // HACK: Also, don't follow up links that just point to other links. In theory this is legitimate,
  431. // // but no viewer has been observed to set these up and this is the lazy way of avoiding cycles
  432. // // rather than having to keep track of every folder requested in the recursion.
  433. // if (linkedItem != null && linkedItem.AssetType != (int)AssetType.Link)
  434. // {
  435. // // We don't need to send the folder if source and destination of the link are in the same
  436. // // folder.
  437. // if (linkedItem.Folder != containingFolder.ID)
  438. // linkedItemFolderIdsToSend.Add(linkedItem.Folder);
  439. // }
  440. // }
  441. // }
  442. //
  443. // foreach (UUID linkedItemFolderId in linkedItemFolderIdsToSend)
  444. // {
  445. // m_log.DebugFormat(
  446. // "[WEB FETCH INV DESC HANDLER]: Recursively fetching folder {0} linked by item in folder {1} for agent {2}",
  447. // linkedItemFolderId, folderID, agentID);
  448. //
  449. // int dummyVersion;
  450. // InventoryCollection linkedCollection
  451. // = Fetch(
  452. // agentID, linkedItemFolderId, ownerID, fetchFolders, fetchItems, sortOrder, out dummyVersion);
  453. //
  454. // InventoryFolderBase linkedFolder = new InventoryFolderBase(linkedItemFolderId);
  455. // linkedFolder.Owner = agentID;
  456. // linkedFolder = m_InventoryService.GetFolder(linkedFolder);
  457. //
  458. //// contents.Folders.AddRange(linkedCollection.Folders);
  459. //
  460. // contents.Folders.Add(linkedFolder);
  461. // contents.Items.AddRange(linkedCollection.Items);
  462. // }
  463. // }
  464. }
  465. }
  466. else
  467. {
  468. // Lost items don't really need a version
  469. version = 1;
  470. }
  471. return contents;
  472. }
  473. private void AddLibraryFolders(List<LLSDFetchInventoryDescendents> fetchFolders, List<InventoryCollectionWithDescendents> result)
  474. {
  475. InventoryFolderImpl fold;
  476. if (m_LibraryService != null && m_LibraryService.LibraryRootFolder != null)
  477. {
  478. List<LLSDFetchInventoryDescendents> libfolders = fetchFolders.FindAll(f => f.owner_id == m_LibraryService.LibraryRootFolder.Owner);
  479. fetchFolders.RemoveAll(f => libfolders.Contains(f));
  480. //m_log.DebugFormat("[XXX]: Found {0} library folders in request", libfolders.Count);
  481. foreach (LLSDFetchInventoryDescendents f in libfolders)
  482. {
  483. if ((fold = m_LibraryService.LibraryRootFolder.FindFolder(f.folder_id)) != null)
  484. {
  485. InventoryCollectionWithDescendents ret = new InventoryCollectionWithDescendents();
  486. ret.Collection = new InventoryCollection();
  487. ret.Collection.Folders = new List<InventoryFolderBase>();
  488. ret.Collection.Items = fold.RequestListOfItems();
  489. ret.Collection.OwnerID = m_LibraryService.LibraryRootFolder.Owner;
  490. ret.Collection.FolderID = f.folder_id;
  491. ret.Collection.Version = fold.Version;
  492. ret.Descendents = ret.Collection.Items.Count;
  493. result.Add(ret);
  494. //m_log.DebugFormat("[XXX]: Added libfolder {0} ({1}) {2}", ret.Collection.FolderID, ret.Collection.OwnerID);
  495. }
  496. }
  497. }
  498. }
  499. private List<InventoryCollectionWithDescendents> Fetch(List<LLSDFetchInventoryDescendents> fetchFolders, List<UUID> bad_folders)
  500. {
  501. //m_log.DebugFormat(
  502. // "[WEB FETCH INV DESC HANDLER]: Fetching {0} folders for owner {1}", fetchFolders.Count, fetchFolders[0].owner_id);
  503. // FIXME MAYBE: We're not handling sortOrder!
  504. List<InventoryCollectionWithDescendents> result = new List<InventoryCollectionWithDescendents>();
  505. AddLibraryFolders(fetchFolders, result);
  506. // Filter folder Zero right here. Some viewers (Firestorm) send request for folder Zero, which doesn't make sense
  507. // and can kill the sim (all root folders have parent_id Zero)
  508. LLSDFetchInventoryDescendents zero = fetchFolders.Find(f => f.folder_id == UUID.Zero);
  509. if (zero != null)
  510. {
  511. fetchFolders.Remove(zero);
  512. BadFolder(zero, null, bad_folders);
  513. }
  514. if (fetchFolders.Count > 0)
  515. {
  516. UUID[] fids = new UUID[fetchFolders.Count];
  517. int i = 0;
  518. foreach (LLSDFetchInventoryDescendents f in fetchFolders)
  519. fids[i++] = f.folder_id;
  520. //m_log.DebugFormat("[XXX]: {0}", string.Join(",", fids));
  521. InventoryCollection[] fetchedContents = m_InventoryService.GetMultipleFoldersContent(fetchFolders[0].owner_id, fids);
  522. if (fetchedContents == null || (fetchedContents != null && fetchedContents.Length == 0))
  523. {
  524. m_log.WarnFormat("[WEB FETCH INV DESC HANDLER]: Could not get contents of multiple folders for user {0}", fetchFolders[0].owner_id);
  525. foreach (LLSDFetchInventoryDescendents freq in fetchFolders)
  526. BadFolder(freq, null, bad_folders);
  527. return null;
  528. }
  529. i = 0;
  530. // Do some post-processing. May need to fetch more from inv server for links
  531. foreach (InventoryCollection contents in fetchedContents)
  532. {
  533. // Find the original request
  534. LLSDFetchInventoryDescendents freq = fetchFolders[i++];
  535. InventoryCollectionWithDescendents coll = new InventoryCollectionWithDescendents();
  536. coll.Collection = contents;
  537. if (BadFolder(freq, contents, bad_folders))
  538. continue;
  539. // Next: link management
  540. ProcessLinks(freq, coll);
  541. result.Add(coll);
  542. }
  543. }
  544. return result;
  545. }
  546. private bool BadFolder(LLSDFetchInventoryDescendents freq, InventoryCollection contents, List<UUID> bad_folders)
  547. {
  548. bool bad = false;
  549. if (contents == null)
  550. {
  551. bad_folders.Add(freq.folder_id);
  552. bad = true;
  553. }
  554. // The inventory server isn't sending FolderID in the collection...
  555. // Must fetch it individually
  556. else if (contents.FolderID == UUID.Zero)
  557. {
  558. InventoryFolderBase containingFolder = m_InventoryService.GetFolder(freq.owner_id, freq.folder_id);
  559. if (containingFolder != null)
  560. {
  561. contents.FolderID = containingFolder.ID;
  562. contents.OwnerID = containingFolder.Owner;
  563. contents.Version = containingFolder.Version;
  564. }
  565. else
  566. {
  567. // Was it really a request for folder Zero?
  568. // This is an overkill, but Firestorm really asks for folder Zero.
  569. // I'm leaving the code here for the time being, but commented.
  570. if (freq.folder_id == UUID.Zero)
  571. {
  572. //coll.Collection.OwnerID = freq.owner_id;
  573. //coll.Collection.FolderID = contents.FolderID;
  574. //containingFolder = m_InventoryService.GetRootFolder(freq.owner_id);
  575. //if (containingFolder != null)
  576. //{
  577. // m_log.WarnFormat("[WEB FETCH INV DESC HANDLER]: Request for parent of folder {0}", containingFolder.ID);
  578. // coll.Collection.Folders.Clear();
  579. // coll.Collection.Folders.Add(containingFolder);
  580. // if (m_LibraryService != null && m_LibraryService.LibraryRootFolder != null)
  581. // {
  582. // InventoryFolderBase lib = new InventoryFolderBase(m_LibraryService.LibraryRootFolder.ID, m_LibraryService.LibraryRootFolder.Owner);
  583. // lib.Name = m_LibraryService.LibraryRootFolder.Name;
  584. // lib.Type = m_LibraryService.LibraryRootFolder.Type;
  585. // lib.Version = m_LibraryService.LibraryRootFolder.Version;
  586. // coll.Collection.Folders.Add(lib);
  587. // }
  588. // coll.Collection.Items.Clear();
  589. //}
  590. }
  591. else
  592. {
  593. m_log.WarnFormat("[WEB FETCH INV DESC HANDLER]: Unable to fetch folder {0}", freq.folder_id);
  594. bad_folders.Add(freq.folder_id);
  595. }
  596. bad = true;
  597. }
  598. }
  599. return bad;
  600. }
  601. private void ProcessLinks(LLSDFetchInventoryDescendents freq, InventoryCollectionWithDescendents coll)
  602. {
  603. InventoryCollection contents = coll.Collection;
  604. if (freq.fetch_items && contents.Items != null)
  605. {
  606. List<InventoryItemBase> itemsToReturn = contents.Items;
  607. // descendents must only include the links, not the linked items we add
  608. coll.Descendents = itemsToReturn.Count;
  609. // Add target items for links in this folder before the links themselves.
  610. List<UUID> itemIDs = new List<UUID>();
  611. List<UUID> folderIDs = new List<UUID>();
  612. foreach (InventoryItemBase item in itemsToReturn)
  613. {
  614. //m_log.DebugFormat("[XXX]: {0} {1}", item.Name, item.AssetType);
  615. if (item.AssetType == (int)AssetType.Link)
  616. itemIDs.Add(item.AssetID);
  617. // else if (item.AssetType == (int)AssetType.LinkFolder)
  618. // folderIDs.Add(item.AssetID);
  619. }
  620. //m_log.DebugFormat("[XXX]: folder {0} has {1} links and {2} linkfolders", contents.FolderID, itemIDs.Count, folderIDs.Count);
  621. // Scan for folder links and insert the items they target and those links at the head of the return data
  622. if (folderIDs.Count > 0)
  623. {
  624. InventoryCollection[] linkedFolders = m_InventoryService.GetMultipleFoldersContent(coll.Collection.OwnerID, folderIDs.ToArray());
  625. foreach (InventoryCollection linkedFolderContents in linkedFolders)
  626. {
  627. if (linkedFolderContents == null)
  628. continue;
  629. List<InventoryItemBase> links = linkedFolderContents.Items;
  630. itemsToReturn.InsertRange(0, links);
  631. }
  632. }
  633. if (itemIDs.Count > 0)
  634. {
  635. InventoryItemBase[] linked = m_InventoryService.GetMultipleItems(freq.owner_id, itemIDs.ToArray());
  636. if (linked == null)
  637. {
  638. // OMG!!! One by one!!! This is fallback code, in case the backend isn't updated
  639. m_log.WarnFormat("[WEB FETCH INV DESC HANDLER]: GetMultipleItems failed. Falling back to fetching inventory items one by one.");
  640. linked = new InventoryItemBase[itemIDs.Count];
  641. int i = 0;
  642. foreach (UUID id in itemIDs)
  643. {
  644. linked[i++] = m_InventoryService.GetItem(freq.owner_id, id);
  645. }
  646. }
  647. //m_log.DebugFormat("[WEB FETCH INV DESC HANDLER]: Processing folder {0}. Existing items:", freq.folder_id);
  648. //foreach (InventoryItemBase item in itemsToReturn)
  649. // m_log.DebugFormat("[XXX]: {0} {1} {2}", item.Name, item.AssetType, item.Folder);
  650. if (linked != null)
  651. {
  652. foreach (InventoryItemBase linkedItem in linked)
  653. {
  654. // Take care of genuinely broken links where the target doesn't exist
  655. // HACK: Also, don't follow up links that just point to other links. In theory this is legitimate,
  656. // but no viewer has been observed to set these up and this is the lazy way of avoiding cycles
  657. // rather than having to keep track of every folder requested in the recursion.
  658. if (linkedItem != null && linkedItem.AssetType != (int)AssetType.Link)
  659. {
  660. itemsToReturn.Insert(0, linkedItem);
  661. //m_log.DebugFormat("[WEB FETCH INV DESC HANDLER]: Added {0} {1} {2}", linkedItem.Name, linkedItem.AssetType, linkedItem.Folder);
  662. }
  663. }
  664. }
  665. }
  666. }
  667. }
  668. /// <summary>
  669. /// Convert an internal inventory folder object into an LLSD object.
  670. /// </summary>
  671. /// <param name="invFolder"></param>
  672. /// <returns></returns>
  673. private LLSDInventoryFolder ConvertInventoryFolder(InventoryFolderBase invFolder)
  674. {
  675. LLSDInventoryFolder llsdFolder = new LLSDInventoryFolder();
  676. llsdFolder.folder_id = invFolder.ID;
  677. llsdFolder.parent_id = invFolder.ParentID;
  678. llsdFolder.name = invFolder.Name;
  679. llsdFolder.type = invFolder.Type;
  680. llsdFolder.preferred_type = -1;
  681. return llsdFolder;
  682. }
  683. /// <summary>
  684. /// Convert an internal inventory item object into an LLSD object.
  685. /// </summary>
  686. /// <param name="invItem"></param>
  687. /// <returns></returns>
  688. private LLSDInventoryItem ConvertInventoryItem(InventoryItemBase invItem)
  689. {
  690. LLSDInventoryItem llsdItem = new LLSDInventoryItem();
  691. llsdItem.asset_id = invItem.AssetID;
  692. llsdItem.created_at = invItem.CreationDate;
  693. llsdItem.desc = invItem.Description;
  694. llsdItem.flags = (int)invItem.Flags;
  695. llsdItem.item_id = invItem.ID;
  696. llsdItem.name = invItem.Name;
  697. llsdItem.parent_id = invItem.Folder;
  698. llsdItem.type = invItem.AssetType;
  699. llsdItem.inv_type = invItem.InvType;
  700. llsdItem.permissions = new LLSDPermissions();
  701. llsdItem.permissions.creator_id = invItem.CreatorIdAsUuid;
  702. llsdItem.permissions.base_mask = (int)invItem.CurrentPermissions;
  703. llsdItem.permissions.everyone_mask = (int)invItem.EveryOnePermissions;
  704. llsdItem.permissions.group_id = invItem.GroupID;
  705. llsdItem.permissions.group_mask = (int)invItem.GroupPermissions;
  706. llsdItem.permissions.is_owner_group = invItem.GroupOwned;
  707. llsdItem.permissions.next_owner_mask = (int)invItem.NextPermissions;
  708. llsdItem.permissions.owner_id = invItem.Owner;
  709. llsdItem.permissions.owner_mask = (int)invItem.CurrentPermissions;
  710. llsdItem.sale_info = new LLSDSaleInfo();
  711. llsdItem.sale_info.sale_price = invItem.SalePrice;
  712. llsdItem.sale_info.sale_type = invItem.SaleType;
  713. return llsdItem;
  714. }
  715. }
  716. class InventoryCollectionWithDescendents
  717. {
  718. public InventoryCollection Collection;
  719. public int Descendents;
  720. }
  721. }