OGS1SecureInventoryService.cs 13 KB

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