1
0

FetchInventory2Handler.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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.Reflection;
  28. using OpenMetaverse;
  29. using OpenMetaverse.StructuredData;
  30. using OpenSim.Framework;
  31. using OpenSim.Framework.Capabilities;
  32. using OpenSim.Framework.Servers.HttpServer;
  33. using OpenSim.Services.Interfaces;
  34. using OSDArray = OpenMetaverse.StructuredData.OSDArray;
  35. using OSDMap = OpenMetaverse.StructuredData.OSDMap;
  36. using log4net;
  37. namespace OpenSim.Capabilities.Handlers
  38. {
  39. public class FetchInventory2Handler
  40. {
  41. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  42. private IInventoryService m_inventoryService;
  43. private UUID m_agentID;
  44. public FetchInventory2Handler(IInventoryService invService, UUID agentId)
  45. {
  46. m_inventoryService = invService;
  47. m_agentID = agentId;
  48. }
  49. public string FetchInventoryRequest(string request, string path, string param, IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
  50. {
  51. //m_log.DebugFormat("[FETCH INVENTORY HANDLER]: Received FetchInventory capability request {0}", request);
  52. OSDMap requestmap = (OSDMap)OSDParser.DeserializeLLSDXml(Utils.StringToBytes(request));
  53. OSDArray itemsRequested = (OSDArray)requestmap["items"];
  54. string reply;
  55. LLSDFetchInventory llsdReply = new LLSDFetchInventory();
  56. UUID[] itemIDs = new UUID[itemsRequested.Count];
  57. int i = 0;
  58. foreach (OSDMap osdItemId in itemsRequested)
  59. {
  60. itemIDs[i++] = osdItemId["item_id"].AsUUID();
  61. }
  62. InventoryItemBase[] items = null;
  63. if (m_agentID != UUID.Zero)
  64. {
  65. items = m_inventoryService.GetMultipleItems(m_agentID, itemIDs);
  66. if (items == null)
  67. {
  68. // OMG!!! One by one!!! This is fallback code, in case the backend isn't updated
  69. m_log.WarnFormat("[FETCH INVENTORY HANDLER]: GetMultipleItems failed. Falling back to fetching inventory items one by one.");
  70. items = new InventoryItemBase[itemsRequested.Count];
  71. InventoryItemBase item = new InventoryItemBase();
  72. item.Owner = m_agentID;
  73. foreach (UUID id in itemIDs)
  74. {
  75. item.ID = id;
  76. items[i++] = m_inventoryService.GetItem(item);
  77. }
  78. }
  79. }
  80. else
  81. {
  82. items = new InventoryItemBase[itemsRequested.Count];
  83. InventoryItemBase item = new InventoryItemBase();
  84. foreach (UUID id in itemIDs)
  85. {
  86. item.ID = id;
  87. items[i++] = m_inventoryService.GetItem(item);
  88. }
  89. }
  90. foreach (InventoryItemBase item in items)
  91. {
  92. if (item != null)
  93. {
  94. // We don't know the agent that this request belongs to so we'll use the agent id of the item
  95. // which will be the same for all items.
  96. llsdReply.agent_id = item.Owner;
  97. llsdReply.items.Array.Add(ConvertInventoryItem(item));
  98. }
  99. }
  100. reply = LLSDHelpers.SerialiseLLSDReply(llsdReply);
  101. return reply;
  102. }
  103. /// <summary>
  104. /// Convert an internal inventory item object into an LLSD object.
  105. /// </summary>
  106. /// <param name="invItem"></param>
  107. /// <returns></returns>
  108. private LLSDInventoryItem ConvertInventoryItem(InventoryItemBase invItem)
  109. {
  110. LLSDInventoryItem llsdItem = new LLSDInventoryItem();
  111. llsdItem.asset_id = invItem.AssetID;
  112. llsdItem.created_at = invItem.CreationDate;
  113. llsdItem.desc = invItem.Description;
  114. llsdItem.flags = ((int)invItem.Flags) & 0xff;
  115. llsdItem.item_id = invItem.ID;
  116. llsdItem.name = invItem.Name;
  117. llsdItem.parent_id = invItem.Folder;
  118. llsdItem.type = invItem.AssetType;
  119. llsdItem.inv_type = invItem.InvType;
  120. llsdItem.permissions = new LLSDPermissions();
  121. llsdItem.permissions.creator_id = invItem.CreatorIdAsUuid;
  122. llsdItem.permissions.base_mask = (int)invItem.CurrentPermissions;
  123. llsdItem.permissions.everyone_mask = (int)invItem.EveryOnePermissions;
  124. llsdItem.permissions.group_id = invItem.GroupID;
  125. llsdItem.permissions.group_mask = (int)invItem.GroupPermissions;
  126. llsdItem.permissions.is_owner_group = invItem.GroupOwned;
  127. llsdItem.permissions.next_owner_mask = (int)invItem.NextPermissions;
  128. llsdItem.permissions.owner_id = invItem.Owner;
  129. llsdItem.permissions.owner_mask = (int)invItem.CurrentPermissions;
  130. llsdItem.sale_info = new LLSDSaleInfo();
  131. llsdItem.sale_info.sale_price = invItem.SalePrice;
  132. llsdItem.sale_info.sale_type = invItem.SaleType;
  133. return llsdItem;
  134. }
  135. }
  136. }