1
0

OGS1InventoryService.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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.Generic;
  29. using System.Net;
  30. using System.Reflection;
  31. using log4net;
  32. using OpenMetaverse;
  33. using OpenSim.Framework;
  34. using OpenSim.Framework.Communications;
  35. using OpenSim.Framework.Communications.Cache;
  36. using OpenSim.Framework.Servers.HttpServer;
  37. using OpenSim.Framework.Statistics;
  38. namespace OpenSim.Region.Communications.OGS1
  39. {
  40. public class OGS1InventoryService : IInventoryServices
  41. {
  42. private static readonly ILog m_log
  43. = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  44. private string _inventoryServerUrl;
  45. private Uri m_Uri;
  46. private Dictionary<UUID, InventoryReceiptCallback> m_RequestingInventory
  47. = new Dictionary<UUID, InventoryReceiptCallback>();
  48. public OGS1InventoryService(string inventoryServerUrl)
  49. {
  50. _inventoryServerUrl = inventoryServerUrl;
  51. m_Uri = new Uri(_inventoryServerUrl);
  52. }
  53. #region IInventoryServices Members
  54. public string Host
  55. {
  56. get { return m_Uri.Host; }
  57. }
  58. /// <summary>
  59. /// <see cref="OpenSim.Framework.Communications.IInventoryServices"></see>
  60. /// </summary>
  61. /// <param name="userID"></param>
  62. /// <param name="callback"></param>
  63. public void RequestInventoryForUser(UUID userID, InventoryReceiptCallback callback)
  64. {
  65. if (!m_RequestingInventory.ContainsKey(userID))
  66. {
  67. m_RequestingInventory.Add(userID, callback);
  68. try
  69. {
  70. m_log.InfoFormat(
  71. "[OGS1 INVENTORY SERVICE]: Requesting inventory from {0}/GetInventory/ for user {1}",
  72. _inventoryServerUrl, userID);
  73. RestObjectPosterResponse<InventoryCollection> requester
  74. = new RestObjectPosterResponse<InventoryCollection>();
  75. requester.ResponseCallback = InventoryResponse;
  76. requester.BeginPostObject<Guid>(_inventoryServerUrl + "/GetInventory/", userID.Guid);
  77. }
  78. catch (WebException e)
  79. {
  80. if (StatsManager.SimExtraStats != null)
  81. StatsManager.SimExtraStats.AddInventoryServiceRetrievalFailure();
  82. m_log.ErrorFormat("[OGS1 INVENTORY SERVICE]: Request inventory operation failed, {0} {1}",
  83. e.Source, e.Message);
  84. }
  85. }
  86. else
  87. {
  88. m_log.ErrorFormat("[OGS1 INVENTORY SERVICE]: RequestInventoryForUser() - could you not find user profile for {0}", userID);
  89. }
  90. }
  91. /// <summary>
  92. /// Callback used by the inventory server GetInventory request
  93. /// </summary>
  94. /// <param name="userID"></param>
  95. private void InventoryResponse(InventoryCollection response)
  96. {
  97. UUID userID = response.UserID;
  98. if (m_RequestingInventory.ContainsKey(userID))
  99. {
  100. m_log.InfoFormat("[OGS1 INVENTORY SERVICE]: " +
  101. "Received inventory response for user {0} containing {1} folders and {2} items",
  102. userID, response.Folders.Count, response.Items.Count);
  103. InventoryFolderImpl rootFolder = null;
  104. InventoryReceiptCallback callback = m_RequestingInventory[userID];
  105. ICollection<InventoryFolderImpl> folders = new List<InventoryFolderImpl>();
  106. ICollection<InventoryItemBase> items = new List<InventoryItemBase>();
  107. foreach (InventoryFolderBase folder in response.Folders)
  108. {
  109. if (folder.ParentID == UUID.Zero)
  110. {
  111. rootFolder = new InventoryFolderImpl(folder);
  112. folders.Add(rootFolder);
  113. break;
  114. }
  115. }
  116. if (rootFolder != null)
  117. {
  118. foreach (InventoryFolderBase folder in response.Folders)
  119. {
  120. if (folder.ID != rootFolder.ID)
  121. {
  122. folders.Add(new InventoryFolderImpl(folder));
  123. }
  124. }
  125. foreach (InventoryItemBase item in response.Items)
  126. {
  127. items.Add(item);
  128. }
  129. }
  130. else
  131. {
  132. m_log.ErrorFormat("[OGS1 INVENTORY SERVICE]: Did not get back an inventory containing a root folder for user {0}", userID);
  133. }
  134. callback(folders, items);
  135. m_RequestingInventory.Remove(userID);
  136. }
  137. else
  138. {
  139. m_log.WarnFormat(
  140. "[OGS1 INVENTORY SERVICE]: " +
  141. "Received inventory response for {0} for which we do not have a record of requesting!",
  142. userID);
  143. }
  144. }
  145. /// <summary>
  146. /// <see cref="OpenSim.Framework.Communications.IInventoryServices"></see>
  147. /// </summary>
  148. public bool AddFolder(InventoryFolderBase folder)
  149. {
  150. try
  151. {
  152. return SynchronousRestObjectPoster.BeginPostObject<InventoryFolderBase, bool>(
  153. "POST", _inventoryServerUrl + "/NewFolder/", folder);
  154. }
  155. catch (WebException e)
  156. {
  157. m_log.ErrorFormat("[OGS1 INVENTORY SERVICE]: Add new inventory folder operation failed, {0} {1}",
  158. e.Source, e.Message);
  159. }
  160. return false;
  161. }
  162. /// <summary>
  163. /// <see cref="OpenSim.Framework.Communications.IInventoryServices"></see>
  164. /// </summary>
  165. /// <param name="folder"></param>
  166. public bool UpdateFolder(InventoryFolderBase folder)
  167. {
  168. try
  169. {
  170. return SynchronousRestObjectPoster.BeginPostObject<InventoryFolderBase, bool>(
  171. "POST", _inventoryServerUrl + "/UpdateFolder/", folder);
  172. }
  173. catch (WebException e)
  174. {
  175. m_log.ErrorFormat("[OGS1 INVENTORY SERVICE]: Update inventory folder operation failed, {0} {1}",
  176. e.Source, e.Message);
  177. }
  178. return false;
  179. }
  180. /// <summary>
  181. /// <see cref="OpenSim.Framework.Communications.IInventoryServices"></see>
  182. /// </summary>
  183. /// <param name="folder"></param>
  184. public bool MoveFolder(InventoryFolderBase folder)
  185. {
  186. try
  187. {
  188. return SynchronousRestObjectPoster.BeginPostObject<InventoryFolderBase, bool>(
  189. "POST", _inventoryServerUrl + "/MoveFolder/", folder);
  190. }
  191. catch (WebException e)
  192. {
  193. m_log.ErrorFormat("[OGS1 INVENTORY SERVICE]: Move inventory folder operation failed, {0} {1}",
  194. e.Source, e.Message);
  195. }
  196. return false;
  197. }
  198. /// <summary>
  199. /// <see cref="OpenSim.Framework.Communications.IInventoryServices"></see>
  200. /// </summary>
  201. public bool PurgeFolder(InventoryFolderBase folder)
  202. {
  203. try
  204. {
  205. return SynchronousRestObjectPoster.BeginPostObject<InventoryFolderBase, bool>(
  206. "POST", _inventoryServerUrl + "/PurgeFolder/", folder);
  207. }
  208. catch (WebException e)
  209. {
  210. m_log.ErrorFormat("[OGS1 INVENTORY SERVICE]: Move inventory folder operation failed, {0} {1}",
  211. e.Source, e.Message);
  212. }
  213. return false;
  214. }
  215. /// <summary>
  216. /// <see cref="OpenSim.Framework.Communications.IInventoryServices"></see>
  217. /// </summary>
  218. public bool AddItem(InventoryItemBase item)
  219. {
  220. try
  221. {
  222. return SynchronousRestObjectPoster.BeginPostObject<InventoryItemBase, bool>(
  223. "POST", _inventoryServerUrl + "/NewItem/", item);
  224. }
  225. catch (WebException e)
  226. {
  227. m_log.ErrorFormat("[OGS1 INVENTORY SERVICE]: Add new inventory item operation failed, {0} {1}",
  228. e.Source, e.Message);
  229. }
  230. return false;
  231. }
  232. // TODO: this is a temporary workaround, the UpdateInventoryItem method need to be implemented
  233. public bool UpdateItem(InventoryItemBase item)
  234. {
  235. try
  236. {
  237. return SynchronousRestObjectPoster.BeginPostObject<InventoryItemBase, bool>(
  238. "POST", _inventoryServerUrl + "/NewItem/", item);
  239. }
  240. catch (WebException e)
  241. {
  242. m_log.ErrorFormat("[OGS1 INVENTORY SERVICE]: Update new inventory item operation failed, {0} {1}",
  243. e.Source, e.Message);
  244. }
  245. return false;
  246. }
  247. /// <summary>
  248. /// <see cref="OpenSim.Framework.Communications.IInventoryServices"></see>
  249. /// </summary>
  250. public bool DeleteItem(InventoryItemBase item)
  251. {
  252. try
  253. {
  254. return SynchronousRestObjectPoster.BeginPostObject<InventoryItemBase, bool>(
  255. "POST", _inventoryServerUrl + "/DeleteItem/", item);
  256. }
  257. catch (WebException e)
  258. {
  259. m_log.ErrorFormat("[OGS1 INVENTORY SERVICE]: Delete inventory item operation failed, {0} {1}",
  260. e.Source, e.Message);
  261. }
  262. return false;
  263. }
  264. public InventoryItemBase QueryItem(InventoryItemBase item)
  265. {
  266. try
  267. {
  268. return SynchronousRestObjectPoster.BeginPostObject<InventoryItemBase, InventoryItemBase>(
  269. "POST", _inventoryServerUrl + "/QueryItem/", item);
  270. }
  271. catch (WebException e)
  272. {
  273. m_log.ErrorFormat("[OGS1 INVENTORY SERVICE]: Query inventory item operation failed, {0} {1}",
  274. e.Source, e.Message);
  275. }
  276. return null;
  277. }
  278. public InventoryFolderBase QueryFolder(InventoryFolderBase item)
  279. {
  280. try
  281. {
  282. return SynchronousRestObjectPoster.BeginPostObject<InventoryFolderBase, InventoryFolderBase>(
  283. "POST", _inventoryServerUrl + "/QueryFolder/", item);
  284. }
  285. catch (WebException e)
  286. {
  287. m_log.ErrorFormat("[OGS1 INVENTORY SERVICE]: Query inventory item operation failed, {0} {1}",
  288. e.Source, e.Message);
  289. }
  290. return null;
  291. }
  292. public bool HasInventoryForUser(UUID userID)
  293. {
  294. return false;
  295. }
  296. public InventoryFolderBase RequestRootFolder(UUID userID)
  297. {
  298. return null;
  299. }
  300. #endregion
  301. }
  302. }