HGAssetService.cs 8.7 KB

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