HGAssetServiceConnector.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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.Collections.Specialized;
  32. using System.Reflection;
  33. using System.Web;
  34. using OpenSim.Framework;
  35. using OpenSim.Services.Interfaces;
  36. using OpenSim.Services.Connectors.Hypergrid;
  37. using OpenSim.Services.Connectors.SimianGrid;
  38. namespace OpenSim.Services.Connectors
  39. {
  40. public class HGAssetServiceConnector : IAssetService
  41. {
  42. private static readonly ILog m_log =
  43. LogManager.GetLogger(
  44. MethodBase.GetCurrentMethod().DeclaringType);
  45. private Dictionary<string, IAssetService> m_connectors = new Dictionary<string, IAssetService>();
  46. public HGAssetServiceConnector(IConfigSource source)
  47. {
  48. IConfig moduleConfig = source.Configs["Modules"];
  49. if (moduleConfig != null)
  50. {
  51. // string name = moduleConfig.GetString("AssetServices", "");
  52. IConfig assetConfig = source.Configs["AssetService"];
  53. if (assetConfig == null)
  54. {
  55. m_log.Error("[HG ASSET SERVICE]: AssetService missing from OpenSim.ini");
  56. return;
  57. }
  58. m_log.Info("[HG ASSET SERVICE]: HG asset service enabled");
  59. }
  60. }
  61. private bool StringToUrlAndAssetID(string id, out string url, out string assetID)
  62. {
  63. url = String.Empty;
  64. assetID = String.Empty;
  65. Uri assetUri;
  66. if (Uri.TryCreate(id, UriKind.Absolute, out assetUri) &&
  67. assetUri.Scheme == Uri.UriSchemeHttp)
  68. {
  69. // Simian
  70. if (assetUri.Query != string.Empty)
  71. {
  72. NameValueCollection qscoll = HttpUtility.ParseQueryString(assetUri.Query);
  73. assetID = qscoll["id"];
  74. if (assetID != null)
  75. url = id.Replace(assetID, ""); // Malformed again, as simian expects
  76. else
  77. url = id; // !!! best effort
  78. }
  79. else // robust
  80. {
  81. url = "http://" + assetUri.Authority;
  82. assetID = assetUri.LocalPath.Trim(new char[] { '/' });
  83. }
  84. return true;
  85. }
  86. m_log.DebugFormat("[HG ASSET SERVICE]: Malformed URL {0}", id);
  87. return false;
  88. }
  89. private IAssetService GetConnector(string url)
  90. {
  91. IAssetService connector = null;
  92. lock (m_connectors)
  93. {
  94. if (m_connectors.ContainsKey(url))
  95. {
  96. connector = m_connectors[url];
  97. }
  98. else
  99. {
  100. // Still not as flexible as I would like this to be,
  101. // but good enough for now
  102. string connectorType = new HeloServicesConnector(url).Helo();
  103. m_log.DebugFormat("[HG ASSET SERVICE]: HELO returned {0}", connectorType);
  104. if (connectorType == "opensim-simian")
  105. {
  106. connector = new SimianAssetServiceConnector(url);
  107. }
  108. else
  109. connector = new AssetServicesConnector(url);
  110. m_connectors.Add(url, connector);
  111. }
  112. }
  113. return connector;
  114. }
  115. public AssetBase Get(string id)
  116. {
  117. string url = string.Empty;
  118. string assetID = string.Empty;
  119. if (StringToUrlAndAssetID(id, out url, out assetID))
  120. {
  121. IAssetService connector = GetConnector(url);
  122. return connector.Get(assetID);
  123. }
  124. return null;
  125. }
  126. public AssetBase GetCached(string id)
  127. {
  128. string url = string.Empty;
  129. string assetID = string.Empty;
  130. if (StringToUrlAndAssetID(id, out url, out assetID))
  131. {
  132. IAssetService connector = GetConnector(url);
  133. return connector.GetCached(assetID);
  134. }
  135. return null;
  136. }
  137. public AssetMetadata GetMetadata(string id)
  138. {
  139. string url = string.Empty;
  140. string assetID = string.Empty;
  141. if (StringToUrlAndAssetID(id, out url, out assetID))
  142. {
  143. IAssetService connector = GetConnector(url);
  144. return connector.GetMetadata(assetID);
  145. }
  146. return null;
  147. }
  148. public byte[] GetData(string id)
  149. {
  150. return null;
  151. }
  152. public bool Get(string id, Object sender, AssetRetrieved handler)
  153. {
  154. string url = string.Empty;
  155. string assetID = string.Empty;
  156. if (StringToUrlAndAssetID(id, out url, out assetID))
  157. {
  158. IAssetService connector = GetConnector(url);
  159. return connector.Get(assetID, sender, handler);
  160. }
  161. return false;
  162. }
  163. public string Store(AssetBase asset)
  164. {
  165. string url = string.Empty;
  166. string assetID = string.Empty;
  167. if (StringToUrlAndAssetID(asset.ID, out url, out assetID))
  168. {
  169. IAssetService connector = GetConnector(url);
  170. // Restore the assetID to a simple UUID
  171. asset.ID = assetID;
  172. return connector.Store(asset);
  173. }
  174. return String.Empty;
  175. }
  176. public bool UpdateContent(string id, byte[] data)
  177. {
  178. return false;
  179. }
  180. public bool Delete(string id)
  181. {
  182. return false;
  183. }
  184. }
  185. }