WebFetchInvDescHandler.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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, OSHttpRequest httpRequest, OSHttpResponse httpResponse)
  56. {
  57. // nasty temporary hack here, the linden client falsely
  58. // identifies the uuid 00000000-0000-0000-0000-000000000000
  59. // as a string which breaks us
  60. //
  61. // correctly mark it as a uuid
  62. //
  63. request = request.Replace("<string>00000000-0000-0000-0000-000000000000</string>", "<uuid>00000000-0000-0000-0000-000000000000</uuid>");
  64. // another hack <integer>1</integer> results in a
  65. // System.ArgumentException: Object type System.Int32 cannot
  66. // be converted to target type: System.Boolean
  67. //
  68. request = request.Replace("<key>fetch_folders</key><integer>0</integer>", "<key>fetch_folders</key><boolean>0</boolean>");
  69. request = request.Replace("<key>fetch_folders</key><integer>1</integer>", "<key>fetch_folders</key><boolean>1</boolean>");
  70. Hashtable hash = new Hashtable();
  71. try
  72. {
  73. hash = (Hashtable)LLSD.LLSDDeserialize(Utils.StringToBytes(request));
  74. }
  75. catch (LLSD.LLSDParseException pe)
  76. {
  77. m_log.Error("[AGENT INVENTORY]: Fetch error: " + pe.Message);
  78. m_log.Error("Request: " + request.ToString());
  79. }
  80. ArrayList foldersrequested = (ArrayList)hash["folders"];
  81. string response = "";
  82. lock (m_fetchLock)
  83. {
  84. for (int i = 0; i < foldersrequested.Count; i++)
  85. {
  86. string inventoryitemstr = "";
  87. Hashtable inventoryhash = (Hashtable)foldersrequested[i];
  88. LLSDFetchInventoryDescendents llsdRequest = new LLSDFetchInventoryDescendents();
  89. try
  90. {
  91. LLSDHelpers.DeserialiseOSDMap(inventoryhash, llsdRequest);
  92. }
  93. catch (Exception e)
  94. {
  95. m_log.Debug("[CAPS]: caught exception doing OSD deserialize" + e);
  96. }
  97. LLSDInventoryDescendents reply = FetchInventoryReply(llsdRequest);
  98. inventoryitemstr = LLSDHelpers.SerialiseLLSDReply(reply);
  99. inventoryitemstr = inventoryitemstr.Replace("<llsd><map><key>folders</key><array>", "");
  100. inventoryitemstr = inventoryitemstr.Replace("</array></map></llsd>", "");
  101. response += inventoryitemstr;
  102. }
  103. if (response.Length == 0)
  104. {
  105. // Ter-guess: If requests fail a lot, the client seems to stop requesting descendants.
  106. // Therefore, I'm concluding that the client only has so many threads available to do requests
  107. // and when a thread stalls.. is stays stalled.
  108. // Therefore we need to return something valid
  109. response = "<llsd><map><key>folders</key><array /></map></llsd>";
  110. }
  111. else
  112. {
  113. response = "<llsd><map><key>folders</key><array>" + response + "</array></map></llsd>";
  114. }
  115. //m_log.DebugFormat("[CAPS]: Replying to CAPS fetch inventory request with following xml");
  116. //m_log.Debug("[CAPS] "+response);
  117. }
  118. return response;
  119. }
  120. /// <summary>
  121. /// Construct an LLSD reply packet to a CAPS inventory request
  122. /// </summary>
  123. /// <param name="invFetch"></param>
  124. /// <returns></returns>
  125. private LLSDInventoryDescendents FetchInventoryReply(LLSDFetchInventoryDescendents invFetch)
  126. {
  127. LLSDInventoryDescendents reply = new LLSDInventoryDescendents();
  128. LLSDInventoryFolderContents contents = new LLSDInventoryFolderContents();
  129. contents.agent_id = invFetch.owner_id;
  130. contents.owner_id = invFetch.owner_id;
  131. contents.folder_id = invFetch.folder_id;
  132. reply.folders.Array.Add(contents);
  133. InventoryCollection inv = new InventoryCollection();
  134. inv.Folders = new List<InventoryFolderBase>();
  135. inv.Items = new List<InventoryItemBase>();
  136. int version = 0;
  137. inv = Fetch(invFetch.owner_id, invFetch.folder_id, invFetch.owner_id, invFetch.fetch_folders, invFetch.fetch_items, invFetch.sort_order, out version);
  138. if (inv.Folders != null)
  139. {
  140. foreach (InventoryFolderBase invFolder in inv.Folders)
  141. {
  142. contents.categories.Array.Add(ConvertInventoryFolder(invFolder));
  143. }
  144. }
  145. if (inv.Items != null)
  146. {
  147. foreach (InventoryItemBase invItem in inv.Items)
  148. {
  149. contents.items.Array.Add(ConvertInventoryItem(invItem));
  150. }
  151. }
  152. contents.descendents = contents.items.Array.Count + contents.categories.Array.Count;
  153. contents.version = version;
  154. return reply;
  155. }
  156. public InventoryCollection Fetch(UUID agentID, UUID folderID, UUID ownerID,
  157. bool fetchFolders, bool fetchItems, int sortOrder, out int version)
  158. {
  159. m_log.DebugFormat(
  160. "[WEBFETCHINVENTORYDESCENDANTS]: Fetching folders ({0}), items ({1}) from {2} for agent {3}",
  161. fetchFolders, fetchItems, folderID, agentID);
  162. version = 0;
  163. InventoryFolderImpl fold;
  164. if (m_LibraryService != null && m_LibraryService.LibraryRootFolder != null && agentID == m_LibraryService.LibraryRootFolder.Owner)
  165. if ((fold = m_LibraryService.LibraryRootFolder.FindFolder(folderID)) != null)
  166. {
  167. InventoryCollection ret = new InventoryCollection();
  168. ret.Folders = new List<InventoryFolderBase>();
  169. ret.Items = fold.RequestListOfItems();
  170. return ret;
  171. }
  172. InventoryCollection contents = new InventoryCollection();
  173. if (folderID != UUID.Zero)
  174. {
  175. contents = m_InventoryService.GetFolderContent(agentID, folderID);
  176. InventoryFolderBase containingFolder = new InventoryFolderBase();
  177. containingFolder.ID = folderID;
  178. containingFolder.Owner = agentID;
  179. containingFolder = m_InventoryService.GetFolder(containingFolder);
  180. if (containingFolder != null)
  181. version = containingFolder.Version;
  182. }
  183. else
  184. {
  185. // Lost itemsm don't really need a version
  186. version = 1;
  187. }
  188. return contents;
  189. }
  190. /// <summary>
  191. /// Convert an internal inventory folder object into an LLSD object.
  192. /// </summary>
  193. /// <param name="invFolder"></param>
  194. /// <returns></returns>
  195. private LLSDInventoryFolder ConvertInventoryFolder(InventoryFolderBase invFolder)
  196. {
  197. LLSDInventoryFolder llsdFolder = new LLSDInventoryFolder();
  198. llsdFolder.folder_id = invFolder.ID;
  199. llsdFolder.parent_id = invFolder.ParentID;
  200. llsdFolder.name = invFolder.Name;
  201. if (invFolder.Type < 0 || invFolder.Type >= TaskInventoryItem.Types.Length)
  202. llsdFolder.type = "-1";
  203. else
  204. llsdFolder.type = TaskInventoryItem.Types[invFolder.Type];
  205. llsdFolder.preferred_type = "-1";
  206. return llsdFolder;
  207. }
  208. /// <summary>
  209. /// Convert an internal inventory item object into an LLSD object.
  210. /// </summary>
  211. /// <param name="invItem"></param>
  212. /// <returns></returns>
  213. private LLSDInventoryItem ConvertInventoryItem(InventoryItemBase invItem)
  214. {
  215. LLSDInventoryItem llsdItem = new LLSDInventoryItem();
  216. llsdItem.asset_id = invItem.AssetID;
  217. llsdItem.created_at = invItem.CreationDate;
  218. llsdItem.desc = invItem.Description;
  219. llsdItem.flags = (int)invItem.Flags;
  220. llsdItem.item_id = invItem.ID;
  221. llsdItem.name = invItem.Name;
  222. llsdItem.parent_id = invItem.Folder;
  223. try
  224. {
  225. // TODO reevaluate after upgrade to libomv >= r2566. Probably should use UtilsConversions.
  226. llsdItem.type = TaskInventoryItem.Types[invItem.AssetType];
  227. llsdItem.inv_type = TaskInventoryItem.InvTypes[invItem.InvType];
  228. }
  229. catch (Exception e)
  230. {
  231. m_log.ErrorFormat("[CAPS]: Problem setting asset {0} inventory {1} types while converting inventory item {2}: {3}", invItem.AssetType, invItem.InvType, invItem.Name, e.Message);
  232. }
  233. llsdItem.permissions = new LLSDPermissions();
  234. llsdItem.permissions.creator_id = invItem.CreatorIdAsUuid;
  235. llsdItem.permissions.base_mask = (int)invItem.CurrentPermissions;
  236. llsdItem.permissions.everyone_mask = (int)invItem.EveryOnePermissions;
  237. llsdItem.permissions.group_id = invItem.GroupID;
  238. llsdItem.permissions.group_mask = (int)invItem.GroupPermissions;
  239. llsdItem.permissions.is_owner_group = invItem.GroupOwned;
  240. llsdItem.permissions.next_owner_mask = (int)invItem.NextPermissions;
  241. llsdItem.permissions.owner_id = invItem.Owner;
  242. llsdItem.permissions.owner_mask = (int)invItem.CurrentPermissions;
  243. llsdItem.sale_info = new LLSDSaleInfo();
  244. llsdItem.sale_info.sale_price = invItem.SalePrice;
  245. switch (invItem.SaleType)
  246. {
  247. default:
  248. llsdItem.sale_info.sale_type = "not";
  249. break;
  250. case 1:
  251. llsdItem.sale_info.sale_type = "original";
  252. break;
  253. case 2:
  254. llsdItem.sale_info.sale_type = "copy";
  255. break;
  256. case 3:
  257. llsdItem.sale_info.sale_type = "contents";
  258. break;
  259. }
  260. return llsdItem;
  261. }
  262. }
  263. }