1
0

HGRemoteAssetService.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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.IO;
  30. using System.Reflection;
  31. using System.Xml;
  32. using Nini.Config;
  33. using log4net;
  34. using OpenMetaverse;
  35. using OpenSim.Framework;
  36. using OpenSim.Framework.Serialization.External;
  37. using OpenSim.Server.Base;
  38. using OpenSim.Services.Interfaces;
  39. using OpenSim.Services.AssetService;
  40. namespace OpenSim.Services.HypergridService
  41. {
  42. /// <summary>
  43. /// Hypergrid asset service. It serves the IAssetService interface,
  44. /// but implements it in ways that are appropriate for inter-grid
  45. /// asset exchanges.
  46. /// </summary>
  47. public class HGRemoteAssetService : IAssetService
  48. {
  49. private static readonly ILog m_log =
  50. LogManager.GetLogger(
  51. MethodBase.GetCurrentMethod().DeclaringType);
  52. private string m_HomeURL;
  53. private IUserAccountService m_UserAccountService;
  54. private IAssetService m_assetConnector;
  55. private UserAccountCache m_Cache;
  56. private AssetPermissions m_AssetPerms;
  57. public HGRemoteAssetService(IConfigSource config, string configName)
  58. {
  59. m_log.Debug("[HGRemoteAsset Service]: Starting");
  60. IConfig assetConfig = config.Configs[configName];
  61. if (assetConfig == null)
  62. throw new Exception("No HGAssetService configuration");
  63. Object[] args = new Object[] { config };
  64. string assetConnectorDll = assetConfig.GetString("AssetConnector", String.Empty);
  65. if (assetConnectorDll.Length == 0)
  66. throw new Exception("Please specify AssetConnector in HGAssetService configuration");
  67. m_assetConnector = ServerUtils.LoadPlugin<IAssetService>(assetConnectorDll, args);
  68. if (m_assetConnector == null)
  69. throw new Exception(String.Format("Unable to create AssetConnector from {0}", assetConnectorDll));
  70. string userAccountsDll = assetConfig.GetString("UserAccountsService", string.Empty);
  71. if (userAccountsDll.Length == 0)
  72. throw new Exception("Please specify UserAccountsService in HGAssetService configuration");
  73. m_UserAccountService = ServerUtils.LoadPlugin<IUserAccountService>(userAccountsDll, args);
  74. if (m_UserAccountService == null)
  75. throw new Exception(String.Format("Unable to create UserAccountService from {0}", userAccountsDll));
  76. m_HomeURL = Util.GetConfigVarFromSections<string>(config, "HomeURI",
  77. new string[] { "Startup", "Hypergrid", configName }, string.Empty);
  78. if (m_HomeURL.Length == 0)
  79. throw new Exception("[HGAssetService] No HomeURI specified");
  80. m_Cache = UserAccountCache.CreateUserAccountCache(m_UserAccountService);
  81. // Permissions
  82. m_AssetPerms = new AssetPermissions(assetConfig);
  83. }
  84. #region IAssetService overrides
  85. public AssetBase Get(string id)
  86. {
  87. AssetBase asset = m_assetConnector.Get(id);
  88. if (asset == null)
  89. return null;
  90. if (!m_AssetPerms.AllowedExport(asset.Type))
  91. return null;
  92. if (asset.Metadata.Type == (sbyte)AssetType.Object)
  93. asset.Data = AdjustIdentifiers(asset.Data);
  94. AdjustIdentifiers(asset.Metadata);
  95. return asset;
  96. }
  97. public AssetBase Get(string id, string ForeignAssetService, bool dummy)
  98. {
  99. return null;
  100. }
  101. public AssetMetadata GetMetadata(string id)
  102. {
  103. AssetMetadata meta = m_assetConnector.GetMetadata(id);
  104. if (meta == null)
  105. return null;
  106. AdjustIdentifiers(meta);
  107. return meta;
  108. }
  109. public byte[] GetData(string id)
  110. {
  111. AssetBase asset = Get(id);
  112. if (asset == null)
  113. return null;
  114. if (!m_AssetPerms.AllowedExport(asset.Type))
  115. return null;
  116. // Deal with bug introduced in Oct. 20 (1eb3e6cc43e2a7b4053bc1185c7c88e22356c5e8)
  117. // Fix bad assets before sending them elsewhere
  118. if (asset.Type == (int)AssetType.Object && asset.Data != null)
  119. {
  120. string xml = ExternalRepresentationUtils.SanitizeXml(Utils.BytesToString(asset.Data));
  121. asset.Data = Utils.StringToBytes(xml);
  122. }
  123. return asset.Data;
  124. }
  125. // public delegate void AssetRetrieved(string id, Object sender, AssetBase asset);
  126. public virtual bool Get(string id, Object sender, AssetRetrieved handler)
  127. {
  128. return m_assetConnector.Get(id, sender, (i, s, asset) =>
  129. {
  130. if (asset != null)
  131. {
  132. if (!m_AssetPerms.AllowedExport(asset.Type))
  133. {
  134. asset = null;
  135. }
  136. else
  137. {
  138. if (asset.Metadata.Type == (sbyte)AssetType.Object)
  139. asset.Data = AdjustIdentifiers(asset.Data);
  140. AdjustIdentifiers(asset.Metadata);
  141. }
  142. }
  143. handler(i, s, asset);
  144. });
  145. }
  146. public void Get(string id, string ForeignAssetService, bool StoreOnLocalGrid, SimpleAssetRetrieved callBack)
  147. {
  148. m_assetConnector.Get(id, null, (i, s, asset) =>
  149. {
  150. if (asset != null)
  151. {
  152. if (!m_AssetPerms.AllowedExport(asset.Type))
  153. {
  154. asset = null;
  155. }
  156. else
  157. {
  158. if (asset.Metadata.Type == (sbyte)AssetType.Object)
  159. asset.Data = AdjustIdentifiers(asset.Data);
  160. AdjustIdentifiers(asset.Metadata);
  161. }
  162. }
  163. callBack(asset);
  164. });
  165. }
  166. public string Store(AssetBase asset, bool AllowRetry)
  167. {
  168. return Store(asset);
  169. }
  170. public string Store(AssetBase asset)
  171. {
  172. if (!m_AssetPerms.AllowedImport(asset.Type))
  173. return string.Empty;
  174. // Deal with bug introduced in Oct. 20 (1eb3e6cc43e2a7b4053bc1185c7c88e22356c5e8)
  175. // Fix bad assets before storing on this server
  176. if (asset.Type == (int)AssetType.Object && asset.Data != null)
  177. {
  178. string xml = ExternalRepresentationUtils.SanitizeXml(Utils.BytesToString(asset.Data));
  179. asset.Data = Utils.StringToBytes(xml);
  180. }
  181. return m_assetConnector.Store(asset);
  182. }
  183. public bool Delete(string id)
  184. {
  185. // NOGO
  186. return false;
  187. }
  188. #endregion
  189. protected void AdjustIdentifiers(AssetMetadata meta)
  190. {
  191. if (meta == null || m_Cache == null)
  192. return;
  193. UserAccount creator = m_Cache.GetUser(meta.CreatorID);
  194. if (creator != null)
  195. meta.CreatorID = meta.CreatorID + ";" + m_HomeURL + "/" + creator.FirstName + " " + creator.LastName;
  196. }
  197. // Only for Object
  198. protected byte[] AdjustIdentifiers(byte[] data)
  199. {
  200. string xml = Utils.BytesToString(data);
  201. // Deal with bug introduced in Oct. 20 (1eb3e6cc43e2a7b4053bc1185c7c88e22356c5e8)
  202. // Fix bad assets before sending them elsewhere
  203. xml = ExternalRepresentationUtils.SanitizeXml(xml);
  204. return Utils.StringToBytes(ExternalRepresentationUtils.RewriteSOP(xml, "HGAssetService", m_HomeURL, m_Cache, UUID.Zero));
  205. }
  206. public AssetBase GetCached(string id)
  207. {
  208. return Get(id);
  209. }
  210. public bool[] AssetsExist(string[] ids)
  211. {
  212. return m_assetConnector.AssetsExist(ids);
  213. }
  214. public bool UpdateContent(string id, byte[] data)
  215. {
  216. // SO not happening!!
  217. return false;
  218. }
  219. }
  220. }