HGAssetService.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 HGAssetService : OpenSim.Services.AssetService.AssetService, 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 UserAccountCache m_Cache;
  55. public HGAssetService(IConfigSource config) : base(config)
  56. {
  57. m_log.Debug("[HGAsset Service]: Starting");
  58. IConfig assetConfig = config.Configs["HGAssetService"];
  59. if (assetConfig == null)
  60. throw new Exception("No HGAssetService configuration");
  61. string userAccountsDll = assetConfig.GetString("UserAccountsService", string.Empty);
  62. if (userAccountsDll == string.Empty)
  63. throw new Exception("Please specify UserAccountsService in HGAssetService configuration");
  64. Object[] args = new Object[] { config };
  65. m_UserAccountService = ServerUtils.LoadPlugin<IUserAccountService>(userAccountsDll, args);
  66. if (m_UserAccountService == null)
  67. throw new Exception(String.Format("Unable to create UserAccountService from {0}", userAccountsDll));
  68. // legacy configuration [obsolete]
  69. m_HomeURL = assetConfig.GetString("ProfileServerURI", string.Empty);
  70. // Preferred
  71. m_HomeURL = assetConfig.GetString("HomeURI", m_HomeURL);
  72. m_Cache = UserAccountCache.CreateUserAccountCache(m_UserAccountService);
  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 (asset.Metadata.Type == (sbyte)AssetType.Object)
  81. asset.Data = AdjustIdentifiers(asset.Data); ;
  82. AdjustIdentifiers(asset.Metadata);
  83. return asset;
  84. }
  85. public override AssetMetadata GetMetadata(string id)
  86. {
  87. AssetMetadata meta = base.GetMetadata(id);
  88. if (meta == null)
  89. return null;
  90. AdjustIdentifiers(meta);
  91. return meta;
  92. }
  93. public override byte[] GetData(string id)
  94. {
  95. byte[] data = base.GetData(id);
  96. if (data == null)
  97. return null;
  98. return AdjustIdentifiers(data);
  99. }
  100. //public virtual bool Get(string id, Object sender, AssetRetrieved handler)
  101. public override bool Delete(string id)
  102. {
  103. // NOGO
  104. return false;
  105. }
  106. #endregion
  107. protected void AdjustIdentifiers(AssetMetadata meta)
  108. {
  109. if (meta == null || m_Cache == null)
  110. return;
  111. UserAccount creator = m_Cache.GetUser(meta.CreatorID);
  112. if (creator != null)
  113. meta.CreatorID = meta.CreatorID + ";" + m_HomeURL + "/" + creator.FirstName + " " + creator.LastName;
  114. }
  115. protected byte[] AdjustIdentifiers(byte[] data)
  116. {
  117. string xml = Utils.BytesToString(data);
  118. return Utils.StringToBytes(ExternalRepresentationUtils.RewriteSOP(xml, m_HomeURL, m_Cache, UUID.Zero));
  119. }
  120. }
  121. }