HGInventoryServiceConnector.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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 log4net;
  28. using Nini.Config;
  29. using System;
  30. using System.Collections.Generic;
  31. using System.Reflection;
  32. using OpenSim.Framework;
  33. using OpenSim.Services.Interfaces;
  34. using OpenMetaverse;
  35. namespace OpenSim.Services.Connectors.Inventory
  36. {
  37. public class HGInventoryServiceConnector : ISessionAuthInventoryService
  38. {
  39. private static readonly ILog m_log =
  40. LogManager.GetLogger(
  41. MethodBase.GetCurrentMethod().DeclaringType);
  42. private Dictionary<string, InventoryServicesConnector> m_connectors = new Dictionary<string, InventoryServicesConnector>();
  43. public HGInventoryServiceConnector(IConfigSource source)
  44. {
  45. IConfig moduleConfig = source.Configs["Modules"];
  46. if (moduleConfig != null)
  47. {
  48. IConfig inventoryConfig = source.Configs["InventoryService"];
  49. if (inventoryConfig == null)
  50. {
  51. m_log.Error("[HG INVENTORY SERVICE]: InventoryService missing from OpenSim.ini");
  52. return;
  53. }
  54. m_log.Info("[HG INVENTORY SERVICE]: HG inventory service enabled");
  55. }
  56. }
  57. private bool StringToUrlAndUserID(string id, out string url, out string userID)
  58. {
  59. url = String.Empty;
  60. userID = String.Empty;
  61. Uri assetUri;
  62. if (Uri.TryCreate(id, UriKind.Absolute, out assetUri) &&
  63. assetUri.Scheme == Uri.UriSchemeHttp)
  64. {
  65. url = "http://" + assetUri.Authority;
  66. userID = assetUri.LocalPath.Trim(new char[] { '/' });
  67. return true;
  68. }
  69. return false;
  70. }
  71. private ISessionAuthInventoryService GetConnector(string url)
  72. {
  73. InventoryServicesConnector connector = null;
  74. lock (m_connectors)
  75. {
  76. if (m_connectors.ContainsKey(url))
  77. {
  78. connector = m_connectors[url];
  79. }
  80. else
  81. {
  82. // We're instantiating this class explicitly, but this won't
  83. // work in general, because the remote grid may be running
  84. // an inventory server that has a different protocol.
  85. // Eventually we will want a piece of protocol asking
  86. // the remote server about its kind. Definitely cool thing to do!
  87. connector = new InventoryServicesConnector(url);
  88. m_connectors.Add(url, connector);
  89. }
  90. }
  91. return connector;
  92. }
  93. public string Host
  94. {
  95. get { return string.Empty; }
  96. }
  97. public void GetUserInventory(string id, UUID sessionID, InventoryReceiptCallback callback)
  98. {
  99. m_log.Debug("[HGInventory]: GetUserInventory " + id);
  100. string url = string.Empty;
  101. string userID = string.Empty;
  102. if (StringToUrlAndUserID(id, out url, out userID))
  103. {
  104. ISessionAuthInventoryService connector = GetConnector(url);
  105. connector.GetUserInventory(userID, sessionID, callback);
  106. }
  107. }
  108. /// <summary>
  109. /// Gets the user folder for the given folder-type
  110. /// </summary>
  111. /// <param name="userID"></param>
  112. /// <param name="type"></param>
  113. /// <returns></returns>
  114. public Dictionary<AssetType, InventoryFolderBase> GetSystemFolders(string id, UUID sessionID)
  115. {
  116. m_log.Debug("[HGInventory]: GetSystemFolders " + id);
  117. string url = string.Empty;
  118. string userID = string.Empty;
  119. if (StringToUrlAndUserID(id, out url, out userID))
  120. {
  121. ISessionAuthInventoryService connector = GetConnector(url);
  122. return connector.GetSystemFolders(userID, sessionID);
  123. }
  124. return new Dictionary<AssetType, InventoryFolderBase>();
  125. }
  126. /// <summary>
  127. /// Gets everything (folders and items) inside a folder
  128. /// </summary>
  129. /// <param name="userId"></param>
  130. /// <param name="folderID"></param>
  131. /// <returns></returns>
  132. public InventoryCollection GetFolderContent(string id, UUID folderID, UUID sessionID)
  133. {
  134. m_log.Debug("[HGInventory]: GetFolderContent " + id);
  135. string url = string.Empty;
  136. string userID = string.Empty;
  137. if (StringToUrlAndUserID(id, out url, out userID))
  138. {
  139. ISessionAuthInventoryService connector = GetConnector(url);
  140. return connector.GetFolderContent(userID, folderID, sessionID);
  141. }
  142. return null;
  143. }
  144. public bool AddFolder(string id, InventoryFolderBase folder, UUID sessionID)
  145. {
  146. string url = string.Empty;
  147. string userID = string.Empty;
  148. if (StringToUrlAndUserID(id, out url, out userID))
  149. {
  150. ISessionAuthInventoryService connector = GetConnector(url);
  151. return connector.AddFolder(userID, folder, sessionID);
  152. }
  153. return false;
  154. }
  155. public bool UpdateFolder(string id, InventoryFolderBase folder, UUID sessionID)
  156. {
  157. string url = string.Empty;
  158. string userID = string.Empty;
  159. if (StringToUrlAndUserID(id, out url, out userID))
  160. {
  161. ISessionAuthInventoryService connector = GetConnector(url);
  162. return connector.UpdateFolder(userID, folder, sessionID);
  163. }
  164. return false;
  165. }
  166. public bool MoveFolder(string id, InventoryFolderBase folder, UUID sessionID)
  167. {
  168. string url = string.Empty;
  169. string userID = string.Empty;
  170. if (StringToUrlAndUserID(id, out url, out userID))
  171. {
  172. ISessionAuthInventoryService connector = GetConnector(url);
  173. return connector.MoveFolder(userID, folder, sessionID);
  174. }
  175. return false;
  176. }
  177. public bool DeleteFolders(string id, List<UUID> folders, UUID sessionID)
  178. {
  179. string url = string.Empty;
  180. string userID = string.Empty;
  181. if (StringToUrlAndUserID(id, out url, out userID))
  182. {
  183. ISessionAuthInventoryService connector = GetConnector(url);
  184. return connector.DeleteFolders(userID, folders, sessionID);
  185. }
  186. return false;
  187. }
  188. public bool PurgeFolder(string id, InventoryFolderBase folder, UUID sessionID)
  189. {
  190. string url = string.Empty;
  191. string userID = string.Empty;
  192. if (StringToUrlAndUserID(id, out url, out userID))
  193. {
  194. ISessionAuthInventoryService connector = GetConnector(url);
  195. return connector.PurgeFolder(userID, folder, sessionID);
  196. }
  197. return false;
  198. }
  199. public List<InventoryItemBase> GetFolderItems(string id, UUID folderID, UUID sessionID)
  200. {
  201. string url = string.Empty;
  202. string userID = string.Empty;
  203. if (StringToUrlAndUserID(id, out url, out userID))
  204. {
  205. ISessionAuthInventoryService connector = GetConnector(url);
  206. return connector.GetFolderItems(userID, folderID, sessionID);
  207. }
  208. return new List<InventoryItemBase>();
  209. }
  210. public bool AddItem(string id, InventoryItemBase item, UUID sessionID)
  211. {
  212. string url = string.Empty;
  213. string userID = string.Empty;
  214. if (StringToUrlAndUserID(id, out url, out userID))
  215. {
  216. ISessionAuthInventoryService connector = GetConnector(url);
  217. return connector.AddItem(userID, item, sessionID);
  218. }
  219. return false;
  220. }
  221. public bool UpdateItem(string id, InventoryItemBase item, UUID sessionID)
  222. {
  223. string url = string.Empty;
  224. string userID = string.Empty;
  225. if (StringToUrlAndUserID(id, out url, out userID))
  226. {
  227. ISessionAuthInventoryService connector = GetConnector(url);
  228. return connector.UpdateItem(userID, item, sessionID);
  229. }
  230. return false;
  231. }
  232. public bool MoveItems(string id, List<InventoryItemBase> items, UUID sessionID)
  233. {
  234. string url = string.Empty;
  235. string userID = string.Empty;
  236. if (StringToUrlAndUserID(id, out url, out userID))
  237. {
  238. ISessionAuthInventoryService connector = GetConnector(url);
  239. return connector.MoveItems(userID, items, sessionID);
  240. }
  241. return false;
  242. }
  243. public bool DeleteItems(string id, List<UUID> itemIDs, UUID sessionID)
  244. {
  245. string url = string.Empty;
  246. string userID = string.Empty;
  247. if (StringToUrlAndUserID(id, out url, out userID))
  248. {
  249. ISessionAuthInventoryService connector = GetConnector(url);
  250. return connector.DeleteItems(userID, itemIDs, sessionID);
  251. }
  252. return false;
  253. }
  254. public InventoryItemBase QueryItem(string id, InventoryItemBase item, UUID sessionID)
  255. {
  256. string url = string.Empty;
  257. string userID = string.Empty;
  258. if (StringToUrlAndUserID(id, out url, out userID))
  259. {
  260. //m_log.DebugFormat("[HGInventory CONNECTOR]: calling {0}", url);
  261. ISessionAuthInventoryService connector = GetConnector(url);
  262. return connector.QueryItem(userID, item, sessionID);
  263. }
  264. return null;
  265. }
  266. public InventoryFolderBase QueryFolder(string id, InventoryFolderBase folder, UUID sessionID)
  267. {
  268. string url = string.Empty;
  269. string userID = string.Empty;
  270. if (StringToUrlAndUserID(id, out url, out userID))
  271. {
  272. ISessionAuthInventoryService connector = GetConnector(url);
  273. return connector.QueryFolder(userID, folder, sessionID);
  274. }
  275. return null;
  276. }
  277. public int GetAssetPermissions(string id, UUID assetID, UUID sessionID)
  278. {
  279. string url = string.Empty;
  280. string userID = string.Empty;
  281. if (StringToUrlAndUserID(id, out url, out userID))
  282. {
  283. ISessionAuthInventoryService connector = GetConnector(url);
  284. return connector.GetAssetPermissions(userID, assetID, sessionID);
  285. }
  286. return 0;
  287. }
  288. }
  289. }