1
0

WebFetchInvDescHandler.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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. contents = m_InventoryService.GetFolderContent(agentID, folderID);
  207. InventoryFolderBase containingFolder = new InventoryFolderBase();
  208. containingFolder.ID = folderID;
  209. containingFolder.Owner = agentID;
  210. containingFolder = m_InventoryService.GetFolder(containingFolder);
  211. if (containingFolder != null)
  212. {
  213. // m_log.DebugFormat(
  214. // "[WEB FETCH INV DESC HANDLER]: Retrieved folder {0} {1} for agent id {2}",
  215. // containingFolder.Name, containingFolder.ID, agentID);
  216. version = containingFolder.Version;
  217. if (fetchItems)
  218. {
  219. List<InventoryItemBase> itemsToReturn = contents.Items;
  220. List<InventoryItemBase> originalItems = new List<InventoryItemBase>(itemsToReturn);
  221. // descendents must only include the links, not the linked items we add
  222. descendents = originalItems.Count;
  223. // Add target items for links in this folder before the links themselves.
  224. foreach (InventoryItemBase item in originalItems)
  225. {
  226. if (item.AssetType == (int)AssetType.Link)
  227. {
  228. InventoryItemBase linkedItem = m_InventoryService.GetItem(new InventoryItemBase(item.AssetID));
  229. // Take care of genuinely broken links where the target doesn't exist
  230. // HACK: Also, don't follow up links that just point to other links. In theory this is legitimate,
  231. // but no viewer has been observed to set these up and this is the lazy way of avoiding cycles
  232. // rather than having to keep track of every folder requested in the recursion.
  233. if (linkedItem != null && linkedItem.AssetType != (int)AssetType.Link)
  234. itemsToReturn.Insert(0, linkedItem);
  235. }
  236. }
  237. // Now scan for folder links and insert the items they target and those links at the head of the return data
  238. foreach (InventoryItemBase item in originalItems)
  239. {
  240. if (item.AssetType == (int)AssetType.LinkFolder)
  241. {
  242. InventoryCollection linkedFolderContents = m_InventoryService.GetFolderContent(ownerID, item.AssetID);
  243. List<InventoryItemBase> links = linkedFolderContents.Items;
  244. itemsToReturn.InsertRange(0, links);
  245. foreach (InventoryItemBase link in linkedFolderContents.Items)
  246. {
  247. // Take care of genuinely broken links where the target doesn't exist
  248. // HACK: Also, don't follow up links that just point to other links. In theory this is legitimate,
  249. // but no viewer has been observed to set these up and this is the lazy way of avoiding cycles
  250. // rather than having to keep track of every folder requested in the recursion.
  251. if (link != null)
  252. {
  253. // m_log.DebugFormat(
  254. // "[WEB FETCH INV DESC HANDLER]: Adding item {0} {1} from folder {2} linked from {3}",
  255. // link.Name, (AssetType)link.AssetType, item.AssetID, containingFolder.Name);
  256. InventoryItemBase linkedItem
  257. = m_InventoryService.GetItem(new InventoryItemBase(link.AssetID));
  258. if (linkedItem != null)
  259. itemsToReturn.Insert(0, linkedItem);
  260. }
  261. }
  262. }
  263. }
  264. }
  265. // foreach (InventoryItemBase item in contents.Items)
  266. // {
  267. // m_log.DebugFormat(
  268. // "[WEB FETCH INV DESC HANDLER]: Returning item {0}, type {1}, parent {2} in {3} {4}",
  269. // item.Name, (AssetType)item.AssetType, item.Folder, containingFolder.Name, containingFolder.ID);
  270. // }
  271. // =====
  272. //
  273. // foreach (InventoryItemBase linkedItem in linkedItemsToAdd)
  274. // {
  275. // m_log.DebugFormat(
  276. // "[WEB FETCH INV DESC HANDLER]: Inserted linked item {0} for link in folder {1} for agent {2}",
  277. // linkedItem.Name, folderID, agentID);
  278. //
  279. // contents.Items.Add(linkedItem);
  280. // }
  281. //
  282. // // If the folder requested contains links, then we need to send those folders first, otherwise the links
  283. // // will be broken in the viewer.
  284. // HashSet<UUID> linkedItemFolderIdsToSend = new HashSet<UUID>();
  285. // foreach (InventoryItemBase item in contents.Items)
  286. // {
  287. // if (item.AssetType == (int)AssetType.Link)
  288. // {
  289. // InventoryItemBase linkedItem = m_InventoryService.GetItem(new InventoryItemBase(item.AssetID));
  290. //
  291. // // Take care of genuinely broken links where the target doesn't exist
  292. // // HACK: Also, don't follow up links that just point to other links. In theory this is legitimate,
  293. // // but no viewer has been observed to set these up and this is the lazy way of avoiding cycles
  294. // // rather than having to keep track of every folder requested in the recursion.
  295. // if (linkedItem != null && linkedItem.AssetType != (int)AssetType.Link)
  296. // {
  297. // // We don't need to send the folder if source and destination of the link are in the same
  298. // // folder.
  299. // if (linkedItem.Folder != containingFolder.ID)
  300. // linkedItemFolderIdsToSend.Add(linkedItem.Folder);
  301. // }
  302. // }
  303. // }
  304. //
  305. // foreach (UUID linkedItemFolderId in linkedItemFolderIdsToSend)
  306. // {
  307. // m_log.DebugFormat(
  308. // "[WEB FETCH INV DESC HANDLER]: Recursively fetching folder {0} linked by item in folder {1} for agent {2}",
  309. // linkedItemFolderId, folderID, agentID);
  310. //
  311. // int dummyVersion;
  312. // InventoryCollection linkedCollection
  313. // = Fetch(
  314. // agentID, linkedItemFolderId, ownerID, fetchFolders, fetchItems, sortOrder, out dummyVersion);
  315. //
  316. // InventoryFolderBase linkedFolder = new InventoryFolderBase(linkedItemFolderId);
  317. // linkedFolder.Owner = agentID;
  318. // linkedFolder = m_InventoryService.GetFolder(linkedFolder);
  319. //
  320. //// contents.Folders.AddRange(linkedCollection.Folders);
  321. //
  322. // contents.Folders.Add(linkedFolder);
  323. // contents.Items.AddRange(linkedCollection.Items);
  324. // }
  325. // }
  326. }
  327. }
  328. else
  329. {
  330. // Lost items don't really need a version
  331. version = 1;
  332. }
  333. return contents;
  334. }
  335. /// <summary>
  336. /// Convert an internal inventory folder object into an LLSD object.
  337. /// </summary>
  338. /// <param name="invFolder"></param>
  339. /// <returns></returns>
  340. private LLSDInventoryFolder ConvertInventoryFolder(InventoryFolderBase invFolder)
  341. {
  342. LLSDInventoryFolder llsdFolder = new LLSDInventoryFolder();
  343. llsdFolder.folder_id = invFolder.ID;
  344. llsdFolder.parent_id = invFolder.ParentID;
  345. llsdFolder.name = invFolder.Name;
  346. llsdFolder.type = invFolder.Type;
  347. llsdFolder.preferred_type = -1;
  348. return llsdFolder;
  349. }
  350. /// <summary>
  351. /// Convert an internal inventory item object into an LLSD object.
  352. /// </summary>
  353. /// <param name="invItem"></param>
  354. /// <returns></returns>
  355. private LLSDInventoryItem ConvertInventoryItem(InventoryItemBase invItem)
  356. {
  357. LLSDInventoryItem llsdItem = new LLSDInventoryItem();
  358. llsdItem.asset_id = invItem.AssetID;
  359. llsdItem.created_at = invItem.CreationDate;
  360. llsdItem.desc = invItem.Description;
  361. llsdItem.flags = (int)invItem.Flags;
  362. llsdItem.item_id = invItem.ID;
  363. llsdItem.name = invItem.Name;
  364. llsdItem.parent_id = invItem.Folder;
  365. llsdItem.type = invItem.AssetType;
  366. llsdItem.inv_type = invItem.InvType;
  367. llsdItem.permissions = new LLSDPermissions();
  368. llsdItem.permissions.creator_id = invItem.CreatorIdAsUuid;
  369. llsdItem.permissions.base_mask = (int)invItem.CurrentPermissions;
  370. llsdItem.permissions.everyone_mask = (int)invItem.EveryOnePermissions;
  371. llsdItem.permissions.group_id = invItem.GroupID;
  372. llsdItem.permissions.group_mask = (int)invItem.GroupPermissions;
  373. llsdItem.permissions.is_owner_group = invItem.GroupOwned;
  374. llsdItem.permissions.next_owner_mask = (int)invItem.NextPermissions;
  375. llsdItem.permissions.owner_id = invItem.Owner;
  376. llsdItem.permissions.owner_mask = (int)invItem.CurrentPermissions;
  377. llsdItem.sale_info = new LLSDSaleInfo();
  378. llsdItem.sale_info.sale_price = invItem.SalePrice;
  379. llsdItem.sale_info.sale_type = invItem.SaleType;
  380. return llsdItem;
  381. }
  382. }
  383. }