WebFetchInvDescHandler.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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.Reflection;
  31. using log4net;
  32. using Nini.Config;
  33. using OpenMetaverse;
  34. using OpenMetaverse.StructuredData;
  35. using OpenSim.Framework;
  36. using OpenSim.Framework.Capabilities;
  37. using OpenSim.Region.Framework.Interfaces;
  38. using OpenSim.Framework.Servers.HttpServer;
  39. using OpenSim.Services.Interfaces;
  40. using Caps = OpenSim.Framework.Capabilities.Caps;
  41. namespace OpenSim.Capabilities.Handlers
  42. {
  43. public class WebFetchInvDescHandler
  44. {
  45. private static readonly ILog m_log =
  46. LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  47. private IInventoryService m_InventoryService;
  48. private ILibraryService m_LibraryService;
  49. // private object m_fetchLock = new Object();
  50. public WebFetchInvDescHandler(IInventoryService invService, ILibraryService libService)
  51. {
  52. m_InventoryService = invService;
  53. m_LibraryService = libService;
  54. }
  55. public string FetchInventoryDescendentsRequest(string request, string path, string param, IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
  56. {
  57. // lock (m_fetchLock)
  58. // {
  59. // m_log.DebugFormat("[WEB FETCH INV DESC HANDLER]: Received request {0}", request);
  60. // nasty temporary hack here, the linden client falsely
  61. // identifies the uuid 00000000-0000-0000-0000-000000000000
  62. // as a string which breaks us
  63. //
  64. // correctly mark it as a uuid
  65. //
  66. request = request.Replace("<string>00000000-0000-0000-0000-000000000000</string>", "<uuid>00000000-0000-0000-0000-000000000000</uuid>");
  67. // another hack <integer>1</integer> results in a
  68. // System.ArgumentException: Object type System.Int32 cannot
  69. // be converted to target type: System.Boolean
  70. //
  71. request = request.Replace("<key>fetch_folders</key><integer>0</integer>", "<key>fetch_folders</key><boolean>0</boolean>");
  72. request = request.Replace("<key>fetch_folders</key><integer>1</integer>", "<key>fetch_folders</key><boolean>1</boolean>");
  73. Hashtable hash = new Hashtable();
  74. try
  75. {
  76. hash = (Hashtable)LLSD.LLSDDeserialize(Utils.StringToBytes(request));
  77. }
  78. catch (LLSD.LLSDParseException e)
  79. {
  80. m_log.ErrorFormat("[WEB FETCH INV DESC HANDLER]: Fetch error: {0}{1}" + e.Message, e.StackTrace);
  81. m_log.Error("Request: " + request);
  82. }
  83. ArrayList foldersrequested = (ArrayList)hash["folders"];
  84. string response = "";
  85. for (int i = 0; i < foldersrequested.Count; i++)
  86. {
  87. string inventoryitemstr = "";
  88. Hashtable inventoryhash = (Hashtable)foldersrequested[i];
  89. LLSDFetchInventoryDescendents llsdRequest = new LLSDFetchInventoryDescendents();
  90. try
  91. {
  92. LLSDHelpers.DeserialiseOSDMap(inventoryhash, llsdRequest);
  93. }
  94. catch (Exception e)
  95. {
  96. m_log.Debug("[WEB FETCH INV DESC HANDLER]: caught exception doing OSD deserialize" + e);
  97. }
  98. LLSDInventoryDescendents reply = FetchInventoryReply(llsdRequest);
  99. inventoryitemstr = LLSDHelpers.SerialiseLLSDReply(reply);
  100. inventoryitemstr = inventoryitemstr.Replace("<llsd><map><key>folders</key><array>", "");
  101. inventoryitemstr = inventoryitemstr.Replace("</array></map></llsd>", "");
  102. response += inventoryitemstr;
  103. }
  104. if (response.Length == 0)
  105. {
  106. // Ter-guess: If requests fail a lot, the client seems to stop requesting descendants.
  107. // Therefore, I'm concluding that the client only has so many threads available to do requests
  108. // and when a thread stalls.. is stays stalled.
  109. // Therefore we need to return something valid
  110. response = "<llsd><map><key>folders</key><array /></map></llsd>";
  111. }
  112. else
  113. {
  114. response = "<llsd><map><key>folders</key><array>" + response + "</array></map></llsd>";
  115. }
  116. // m_log.DebugFormat("[WEB FETCH INV DESC HANDLER]: Replying to CAPS fetch inventory request");
  117. //m_log.Debug("[WEB FETCH INV DESC HANDLER] "+response);
  118. return response;
  119. // }
  120. }
  121. /// <summary>
  122. /// Construct an LLSD reply packet to a CAPS inventory request
  123. /// </summary>
  124. /// <param name="invFetch"></param>
  125. /// <returns></returns>
  126. private LLSDInventoryDescendents FetchInventoryReply(LLSDFetchInventoryDescendents invFetch)
  127. {
  128. LLSDInventoryDescendents reply = new LLSDInventoryDescendents();
  129. LLSDInventoryFolderContents contents = new LLSDInventoryFolderContents();
  130. contents.agent_id = invFetch.owner_id;
  131. contents.owner_id = invFetch.owner_id;
  132. contents.folder_id = invFetch.folder_id;
  133. reply.folders.Array.Add(contents);
  134. InventoryCollection inv = new InventoryCollection();
  135. inv.Folders = new List<InventoryFolderBase>();
  136. inv.Items = new List<InventoryItemBase>();
  137. int version = 0;
  138. int descendents = 0;
  139. inv
  140. = Fetch(
  141. invFetch.owner_id, invFetch.folder_id, invFetch.owner_id,
  142. invFetch.fetch_folders, invFetch.fetch_items, invFetch.sort_order, out version, out descendents);
  143. if (inv != null && inv.Folders != null)
  144. {
  145. foreach (InventoryFolderBase invFolder in inv.Folders)
  146. {
  147. contents.categories.Array.Add(ConvertInventoryFolder(invFolder));
  148. }
  149. descendents += inv.Folders.Count;
  150. }
  151. if (inv != null && inv.Items != null)
  152. {
  153. foreach (InventoryItemBase invItem in inv.Items)
  154. {
  155. contents.items.Array.Add(ConvertInventoryItem(invItem));
  156. }
  157. }
  158. contents.descendents = descendents;
  159. contents.version = version;
  160. // m_log.DebugFormat(
  161. // "[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}",
  162. // invFetch.folder_id,
  163. // invFetch.fetch_items,
  164. // invFetch.fetch_folders,
  165. // contents.items.Array.Count,
  166. // contents.categories.Array.Count,
  167. // invFetch.owner_id);
  168. return reply;
  169. }
  170. /// <summary>
  171. /// Handle the caps inventory descendents fetch.
  172. /// </summary>
  173. /// <param name="agentID"></param>
  174. /// <param name="folderID"></param>
  175. /// <param name="ownerID"></param>
  176. /// <param name="fetchFolders"></param>
  177. /// <param name="fetchItems"></param>
  178. /// <param name="sortOrder"></param>
  179. /// <param name="version"></param>
  180. /// <returns>An empty InventoryCollection if the inventory look up failed</returns>
  181. private InventoryCollection Fetch(
  182. UUID agentID, UUID folderID, UUID ownerID,
  183. bool fetchFolders, bool fetchItems, int sortOrder, out int version, out int descendents)
  184. {
  185. // m_log.DebugFormat(
  186. // "[WEB FETCH INV DESC HANDLER]: Fetching folders ({0}), items ({1}) from {2} for agent {3}",
  187. // fetchFolders, fetchItems, folderID, agentID);
  188. // FIXME MAYBE: We're not handling sortOrder!
  189. version = 0;
  190. descendents = 0;
  191. InventoryFolderImpl fold;
  192. if (m_LibraryService != null && m_LibraryService.LibraryRootFolder != null && agentID == m_LibraryService.LibraryRootFolder.Owner)
  193. {
  194. if ((fold = m_LibraryService.LibraryRootFolder.FindFolder(folderID)) != null)
  195. {
  196. InventoryCollection ret = new InventoryCollection();
  197. ret.Folders = new List<InventoryFolderBase>();
  198. ret.Items = fold.RequestListOfItems();
  199. descendents = ret.Folders.Count + ret.Items.Count;
  200. return ret;
  201. }
  202. }
  203. InventoryCollection contents = new InventoryCollection();
  204. if (folderID != UUID.Zero)
  205. {
  206. InventoryCollection fetchedContents = m_InventoryService.GetFolderContent(agentID, folderID);
  207. if (fetchedContents == null)
  208. {
  209. m_log.WarnFormat("[WEB FETCH INV DESC HANDLER]: Could not get contents of folder {0} for user {1}", folderID, agentID);
  210. return contents;
  211. }
  212. contents = fetchedContents;
  213. InventoryFolderBase containingFolder = new InventoryFolderBase();
  214. containingFolder.ID = folderID;
  215. containingFolder.Owner = agentID;
  216. containingFolder = m_InventoryService.GetFolder(containingFolder);
  217. if (containingFolder != null)
  218. {
  219. // m_log.DebugFormat(
  220. // "[WEB FETCH INV DESC HANDLER]: Retrieved folder {0} {1} for agent id {2}",
  221. // containingFolder.Name, containingFolder.ID, agentID);
  222. version = containingFolder.Version;
  223. if (fetchItems)
  224. {
  225. List<InventoryItemBase> itemsToReturn = contents.Items;
  226. List<InventoryItemBase> originalItems = new List<InventoryItemBase>(itemsToReturn);
  227. // descendents must only include the links, not the linked items we add
  228. descendents = originalItems.Count;
  229. // Add target items for links in this folder before the links themselves.
  230. foreach (InventoryItemBase item in originalItems)
  231. {
  232. if (item.AssetType == (int)AssetType.Link)
  233. {
  234. InventoryItemBase linkedItem = m_InventoryService.GetItem(new InventoryItemBase(item.AssetID));
  235. // Take care of genuinely broken links where the target doesn't exist
  236. // HACK: Also, don't follow up links that just point to other links. In theory this is legitimate,
  237. // but no viewer has been observed to set these up and this is the lazy way of avoiding cycles
  238. // rather than having to keep track of every folder requested in the recursion.
  239. if (linkedItem != null && linkedItem.AssetType != (int)AssetType.Link)
  240. itemsToReturn.Insert(0, linkedItem);
  241. }
  242. }
  243. // Now scan for folder links and insert the items they target and those links at the head of the return data
  244. foreach (InventoryItemBase item in originalItems)
  245. {
  246. if (item.AssetType == (int)AssetType.LinkFolder)
  247. {
  248. InventoryCollection linkedFolderContents = m_InventoryService.GetFolderContent(ownerID, item.AssetID);
  249. List<InventoryItemBase> links = linkedFolderContents.Items;
  250. itemsToReturn.InsertRange(0, links);
  251. foreach (InventoryItemBase link in linkedFolderContents.Items)
  252. {
  253. // Take care of genuinely broken links where the target doesn't exist
  254. // HACK: Also, don't follow up links that just point to other links. In theory this is legitimate,
  255. // but no viewer has been observed to set these up and this is the lazy way of avoiding cycles
  256. // rather than having to keep track of every folder requested in the recursion.
  257. if (link != null)
  258. {
  259. // m_log.DebugFormat(
  260. // "[WEB FETCH INV DESC HANDLER]: Adding item {0} {1} from folder {2} linked from {3}",
  261. // link.Name, (AssetType)link.AssetType, item.AssetID, containingFolder.Name);
  262. InventoryItemBase linkedItem
  263. = m_InventoryService.GetItem(new InventoryItemBase(link.AssetID));
  264. if (linkedItem != null)
  265. itemsToReturn.Insert(0, linkedItem);
  266. }
  267. }
  268. }
  269. }
  270. }
  271. // foreach (InventoryItemBase item in contents.Items)
  272. // {
  273. // m_log.DebugFormat(
  274. // "[WEB FETCH INV DESC HANDLER]: Returning item {0}, type {1}, parent {2} in {3} {4}",
  275. // item.Name, (AssetType)item.AssetType, item.Folder, containingFolder.Name, containingFolder.ID);
  276. // }
  277. // =====
  278. //
  279. // foreach (InventoryItemBase linkedItem in linkedItemsToAdd)
  280. // {
  281. // m_log.DebugFormat(
  282. // "[WEB FETCH INV DESC HANDLER]: Inserted linked item {0} for link in folder {1} for agent {2}",
  283. // linkedItem.Name, folderID, agentID);
  284. //
  285. // contents.Items.Add(linkedItem);
  286. // }
  287. //
  288. // // If the folder requested contains links, then we need to send those folders first, otherwise the links
  289. // // will be broken in the viewer.
  290. // HashSet<UUID> linkedItemFolderIdsToSend = new HashSet<UUID>();
  291. // foreach (InventoryItemBase item in contents.Items)
  292. // {
  293. // if (item.AssetType == (int)AssetType.Link)
  294. // {
  295. // InventoryItemBase linkedItem = m_InventoryService.GetItem(new InventoryItemBase(item.AssetID));
  296. //
  297. // // Take care of genuinely broken links where the target doesn't exist
  298. // // HACK: Also, don't follow up links that just point to other links. In theory this is legitimate,
  299. // // but no viewer has been observed to set these up and this is the lazy way of avoiding cycles
  300. // // rather than having to keep track of every folder requested in the recursion.
  301. // if (linkedItem != null && linkedItem.AssetType != (int)AssetType.Link)
  302. // {
  303. // // We don't need to send the folder if source and destination of the link are in the same
  304. // // folder.
  305. // if (linkedItem.Folder != containingFolder.ID)
  306. // linkedItemFolderIdsToSend.Add(linkedItem.Folder);
  307. // }
  308. // }
  309. // }
  310. //
  311. // foreach (UUID linkedItemFolderId in linkedItemFolderIdsToSend)
  312. // {
  313. // m_log.DebugFormat(
  314. // "[WEB FETCH INV DESC HANDLER]: Recursively fetching folder {0} linked by item in folder {1} for agent {2}",
  315. // linkedItemFolderId, folderID, agentID);
  316. //
  317. // int dummyVersion;
  318. // InventoryCollection linkedCollection
  319. // = Fetch(
  320. // agentID, linkedItemFolderId, ownerID, fetchFolders, fetchItems, sortOrder, out dummyVersion);
  321. //
  322. // InventoryFolderBase linkedFolder = new InventoryFolderBase(linkedItemFolderId);
  323. // linkedFolder.Owner = agentID;
  324. // linkedFolder = m_InventoryService.GetFolder(linkedFolder);
  325. //
  326. //// contents.Folders.AddRange(linkedCollection.Folders);
  327. //
  328. // contents.Folders.Add(linkedFolder);
  329. // contents.Items.AddRange(linkedCollection.Items);
  330. // }
  331. // }
  332. }
  333. }
  334. else
  335. {
  336. // Lost items don't really need a version
  337. version = 1;
  338. }
  339. return contents;
  340. }
  341. /// <summary>
  342. /// Convert an internal inventory folder object into an LLSD object.
  343. /// </summary>
  344. /// <param name="invFolder"></param>
  345. /// <returns></returns>
  346. private LLSDInventoryFolder ConvertInventoryFolder(InventoryFolderBase invFolder)
  347. {
  348. LLSDInventoryFolder llsdFolder = new LLSDInventoryFolder();
  349. llsdFolder.folder_id = invFolder.ID;
  350. llsdFolder.parent_id = invFolder.ParentID;
  351. llsdFolder.name = invFolder.Name;
  352. llsdFolder.type = invFolder.Type;
  353. llsdFolder.preferred_type = -1;
  354. return llsdFolder;
  355. }
  356. /// <summary>
  357. /// Convert an internal inventory item object into an LLSD object.
  358. /// </summary>
  359. /// <param name="invItem"></param>
  360. /// <returns></returns>
  361. private LLSDInventoryItem ConvertInventoryItem(InventoryItemBase invItem)
  362. {
  363. LLSDInventoryItem llsdItem = new LLSDInventoryItem();
  364. llsdItem.asset_id = invItem.AssetID;
  365. llsdItem.created_at = invItem.CreationDate;
  366. llsdItem.desc = invItem.Description;
  367. llsdItem.flags = (int)invItem.Flags;
  368. llsdItem.item_id = invItem.ID;
  369. llsdItem.name = invItem.Name;
  370. llsdItem.parent_id = invItem.Folder;
  371. llsdItem.type = invItem.AssetType;
  372. llsdItem.inv_type = invItem.InvType;
  373. llsdItem.permissions = new LLSDPermissions();
  374. llsdItem.permissions.creator_id = invItem.CreatorIdAsUuid;
  375. llsdItem.permissions.base_mask = (int)invItem.CurrentPermissions;
  376. llsdItem.permissions.everyone_mask = (int)invItem.EveryOnePermissions;
  377. llsdItem.permissions.group_id = invItem.GroupID;
  378. llsdItem.permissions.group_mask = (int)invItem.GroupPermissions;
  379. llsdItem.permissions.is_owner_group = invItem.GroupOwned;
  380. llsdItem.permissions.next_owner_mask = (int)invItem.NextPermissions;
  381. llsdItem.permissions.owner_id = invItem.Owner;
  382. llsdItem.permissions.owner_mask = (int)invItem.CurrentPermissions;
  383. llsdItem.sale_info = new LLSDSaleInfo();
  384. llsdItem.sale_info.sale_price = invItem.SalePrice;
  385. llsdItem.sale_info.sale_type = invItem.SaleType;
  386. return llsdItem;
  387. }
  388. }
  389. }