1
0

HGFSAssetService.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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.Reflection;
  29. using Nini.Config;
  30. using log4net;
  31. using OpenMetaverse;
  32. using OpenSim.Framework;
  33. using OpenSim.Framework.Serialization.External;
  34. using OpenSim.Server.Base;
  35. using OpenSim.Services.Interfaces;
  36. using OpenSim.Services.FSAssetService;
  37. namespace OpenSim.Services.HypergridService
  38. {
  39. /// <summary>
  40. /// Hypergrid asset service. It serves the IAssetService interface,
  41. /// but implements it in ways that are appropriate for inter-grid
  42. /// asset exchanges. This version is for FSAssets.
  43. /// </summary>
  44. public class HGFSAssetService : FSAssetConnector, IAssetService
  45. {
  46. private static readonly ILog m_log =
  47. LogManager.GetLogger(
  48. MethodBase.GetCurrentMethod().DeclaringType);
  49. private string m_HomeURL;
  50. private IUserAccountService m_UserAccountService;
  51. private UserAccountCache m_Cache;
  52. private AssetPermissions m_AssetPerms;
  53. public HGFSAssetService(IConfigSource config, string configName) : base(config, "AssetService")
  54. {
  55. m_log.Debug("[HGAsset Service]: Starting in FSAsset mode");
  56. IConfig assetConfig = config.Configs[configName];
  57. if (assetConfig == null)
  58. throw new Exception("No HGAssetService configuration");
  59. string userAccountsDll = assetConfig.GetString("UserAccountsService", string.Empty);
  60. if (userAccountsDll == string.Empty)
  61. throw new Exception("Please specify UserAccountsService in HGAssetService configuration");
  62. Object[] args = new Object[] { config };
  63. m_UserAccountService = ServerUtils.LoadPlugin<IUserAccountService>(userAccountsDll, args);
  64. if (m_UserAccountService == null)
  65. throw new Exception(String.Format("Unable to create UserAccountService from {0}", userAccountsDll));
  66. m_HomeURL = Util.GetConfigVarFromSections<string>(config, "HomeURI",
  67. new string[] { "Startup", "Hypergrid", configName }, string.Empty);
  68. if (m_HomeURL == string.Empty)
  69. throw new Exception("[HGAssetService] No HomeURI specified");
  70. m_Cache = UserAccountCache.CreateUserAccountCache(m_UserAccountService);
  71. // Permissions
  72. m_AssetPerms = new AssetPermissions(assetConfig);
  73. }
  74. #region IAssetService overrides
  75. public override AssetBase Get(string id)
  76. {
  77. AssetBase asset = base.Get(id);
  78. if (asset == null)
  79. return null;
  80. if (!m_AssetPerms.AllowedExport(asset.Type))
  81. return null;
  82. if (asset.Metadata.Type == (sbyte)AssetType.Object)
  83. asset.Data = AdjustIdentifiers(asset.Data);
  84. AdjustIdentifiers(asset.Metadata);
  85. return asset;
  86. }
  87. public override AssetMetadata GetMetadata(string id)
  88. {
  89. AssetMetadata meta = base.GetMetadata(id);
  90. if (meta == null)
  91. return null;
  92. AdjustIdentifiers(meta);
  93. return meta;
  94. }
  95. public override byte[] GetData(string id)
  96. {
  97. AssetBase asset = Get(id);
  98. if (asset == null)
  99. return null;
  100. if (!m_AssetPerms.AllowedExport(asset.Type))
  101. return null;
  102. // Deal with bug introduced in Oct. 20 (1eb3e6cc43e2a7b4053bc1185c7c88e22356c5e8)
  103. // Fix bad assets before sending them elsewhere
  104. if (asset.Type == (int)AssetType.Object && asset.Data != null)
  105. {
  106. string xml = ExternalRepresentationUtils.SanitizeXml(Utils.BytesToString(asset.Data));
  107. asset.Data = Utils.StringToBytes(xml);
  108. }
  109. return asset.Data;
  110. }
  111. //public virtual bool Get(string id, Object sender, AssetRetrieved handler)
  112. public override string Store(AssetBase asset)
  113. {
  114. if (!m_AssetPerms.AllowedImport(asset.Type))
  115. return string.Empty;
  116. // Deal with bug introduced in Oct. 20 (1eb3e6cc43e2a7b4053bc1185c7c88e22356c5e8)
  117. // Fix bad assets before storing on this server
  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 base.Store(asset);
  124. }
  125. public override bool Delete(string id)
  126. {
  127. // NOGO
  128. return false;
  129. }
  130. #endregion
  131. protected void AdjustIdentifiers(AssetMetadata meta)
  132. {
  133. if (meta == null || m_Cache == null)
  134. return;
  135. UserAccount creator = m_Cache.GetUser(meta.CreatorID);
  136. if (creator != null)
  137. meta.CreatorID = meta.CreatorID + ";" + m_HomeURL + "/" + creator.FirstName + " " + creator.LastName;
  138. }
  139. // Only for Object
  140. protected byte[] AdjustIdentifiers(byte[] data)
  141. {
  142. string xml = Utils.BytesToString(data);
  143. // Deal with bug introduced in Oct. 20 (1eb3e6cc43e2a7b4053bc1185c7c88e22356c5e8)
  144. // Fix bad assets before sending them elsewhere
  145. xml = ExternalRepresentationUtils.SanitizeXml(xml);
  146. return Utils.StringToBytes(ExternalRepresentationUtils.RewriteSOP(xml, "HGAssetService", m_HomeURL, m_Cache, UUID.Zero));
  147. }
  148. }
  149. }