AssetServiceConnector.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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 System;
  29. using System.Collections.Generic;
  30. using System.IO;
  31. using System.Reflection;
  32. using Nini.Config;
  33. using OpenSim.Framework;
  34. using OpenSim.Framework.Console;
  35. using OpenSim.Framework.Communications;
  36. using OpenSim.Framework.Servers.HttpServer;
  37. using OpenSim.Services.Interfaces;
  38. using OpenMetaverse;
  39. namespace OpenSim.Services.Connectors
  40. {
  41. public class AssetServicesConnector : IAssetService
  42. {
  43. private static readonly ILog m_log =
  44. LogManager.GetLogger(
  45. MethodBase.GetCurrentMethod().DeclaringType);
  46. private string m_ServerURI = String.Empty;
  47. private IImprovedAssetCache m_Cache = null;
  48. public AssetServicesConnector()
  49. {
  50. }
  51. public AssetServicesConnector(string serverURI)
  52. {
  53. m_ServerURI = serverURI.TrimEnd('/');
  54. }
  55. public AssetServicesConnector(IConfigSource source)
  56. {
  57. Initialise(source);
  58. }
  59. public virtual void Initialise(IConfigSource source)
  60. {
  61. IConfig assetConfig = source.Configs["AssetService"];
  62. if (assetConfig == null)
  63. {
  64. m_log.Error("[ASSET CONNECTOR]: AssetService missing from OpanSim.ini");
  65. throw new Exception("Asset connector init error");
  66. }
  67. string serviceURI = assetConfig.GetString("AssetServerURI",
  68. String.Empty);
  69. if (serviceURI == String.Empty)
  70. {
  71. m_log.Error("[ASSET CONNECTOR]: No Server URI named in section AssetService");
  72. throw new Exception("Asset connector init error");
  73. }
  74. m_ServerURI = serviceURI;
  75. MainConsole.Instance.Commands.AddCommand("asset", false, "dump asset",
  76. "dump asset <id> <file>",
  77. "dump one cached asset", HandleDumpAsset);
  78. }
  79. protected void SetCache(IImprovedAssetCache cache)
  80. {
  81. m_Cache = cache;
  82. }
  83. public AssetBase Get(string id)
  84. {
  85. string uri = m_ServerURI + "/assets/" + id;
  86. AssetBase asset = null;
  87. if (m_Cache != null)
  88. asset = m_Cache.Get(id);
  89. if (asset == null)
  90. {
  91. asset = SynchronousRestObjectRequester.
  92. MakeRequest<int, AssetBase>("GET", uri, 0);
  93. if (m_Cache != null)
  94. m_Cache.Cache(asset);
  95. }
  96. return asset;
  97. }
  98. public AssetMetadata GetMetadata(string id)
  99. {
  100. if (m_Cache != null)
  101. {
  102. AssetBase fullAsset = m_Cache.Get(id);
  103. if (fullAsset != null)
  104. return fullAsset.Metadata;
  105. }
  106. string uri = m_ServerURI + "/assets/" + id + "/metadata";
  107. AssetMetadata asset = SynchronousRestObjectRequester.
  108. MakeRequest<int, AssetMetadata>("GET", uri, 0);
  109. return asset;
  110. }
  111. public byte[] GetData(string id)
  112. {
  113. if (m_Cache != null)
  114. {
  115. AssetBase fullAsset = m_Cache.Get(id);
  116. if (fullAsset != null)
  117. return fullAsset.Data;
  118. }
  119. RestClient rc = new RestClient(m_ServerURI);
  120. rc.AddResourcePath("assets");
  121. rc.AddResourcePath(id);
  122. rc.AddResourcePath("data");
  123. rc.RequestMethod = "GET";
  124. Stream s = rc.Request();
  125. if (s == null)
  126. return null;
  127. if (s.Length > 0)
  128. {
  129. byte[] ret = new byte[s.Length];
  130. s.Read(ret, 0, (int)s.Length);
  131. return ret;
  132. }
  133. return null;
  134. }
  135. public bool Get(string id, Object sender, AssetRetrieved handler)
  136. {
  137. string uri = m_ServerURI + "/assets/" + id;
  138. AssetBase asset = null;
  139. if (m_Cache != null)
  140. asset = m_Cache.Get(id);
  141. if (asset == null)
  142. {
  143. bool result = false;
  144. AsynchronousRestObjectRequester.
  145. MakeRequest<int, AssetBase>("GET", uri, 0,
  146. delegate(AssetBase a)
  147. {
  148. if (m_Cache != null)
  149. m_Cache.Cache(a);
  150. handler(id, sender, a);
  151. result = true;
  152. });
  153. return result;
  154. }
  155. else
  156. {
  157. //Util.FireAndForget(delegate { handler(id, sender, asset); });
  158. handler(id, sender, asset);
  159. }
  160. return true;
  161. }
  162. public string Store(AssetBase asset)
  163. {
  164. if (asset.Temporary || asset.Local)
  165. {
  166. if (m_Cache != null)
  167. m_Cache.Cache(asset);
  168. return asset.ID;
  169. }
  170. string uri = m_ServerURI + "/assets/";
  171. string newID = string.Empty;
  172. try
  173. {
  174. newID = SynchronousRestObjectRequester.
  175. MakeRequest<AssetBase, string>("POST", uri, asset);
  176. }
  177. catch (Exception e)
  178. {
  179. m_log.WarnFormat("[ASSET CONNECTOR]: Unable to send asset {0} to asset server. Reason: {1}", asset.ID, e.Message);
  180. }
  181. if (newID != String.Empty)
  182. {
  183. // Placing this here, so that this work with old asset servers that don't send any reply back
  184. // SynchronousRestObjectRequester returns somethins that is not an empty string
  185. if (newID != null)
  186. asset.ID = newID;
  187. if (m_Cache != null)
  188. m_Cache.Cache(asset);
  189. }
  190. return newID;
  191. }
  192. public bool UpdateContent(string id, byte[] data)
  193. {
  194. AssetBase asset = null;
  195. if (m_Cache != null)
  196. asset = m_Cache.Get(id);
  197. if (asset == null)
  198. {
  199. AssetMetadata metadata = GetMetadata(id);
  200. if (metadata == null)
  201. return false;
  202. asset = new AssetBase(metadata.FullID, metadata.Name, metadata.Type);
  203. asset.Metadata = metadata;
  204. }
  205. asset.Data = data;
  206. string uri = m_ServerURI + "/assets/" + id;
  207. if (SynchronousRestObjectRequester.
  208. MakeRequest<AssetBase, bool>("POST", uri, asset))
  209. {
  210. if (m_Cache != null)
  211. m_Cache.Cache(asset);
  212. return true;
  213. }
  214. return false;
  215. }
  216. public bool Delete(string id)
  217. {
  218. string uri = m_ServerURI + "/assets/" + id;
  219. if (SynchronousRestObjectRequester.
  220. MakeRequest<int, bool>("DELETE", uri, 0))
  221. {
  222. if (m_Cache != null)
  223. m_Cache.Expire(id);
  224. return true;
  225. }
  226. return false;
  227. }
  228. private void HandleDumpAsset(string module, string[] args)
  229. {
  230. if (args.Length != 4)
  231. {
  232. MainConsole.Instance.Output("Syntax: dump asset <id> <file>");
  233. return;
  234. }
  235. UUID assetID;
  236. if (!UUID.TryParse(args[2], out assetID))
  237. {
  238. MainConsole.Instance.Output("Invalid asset ID");
  239. return;
  240. }
  241. if (m_Cache == null)
  242. {
  243. MainConsole.Instance.Output("Instance uses no cache");
  244. return;
  245. }
  246. AssetBase asset = m_Cache.Get(assetID.ToString());
  247. if (asset == null)
  248. {
  249. MainConsole.Instance.Output("Asset not found in cache");
  250. return;
  251. }
  252. string fileName = args[3];
  253. FileStream fs = File.Create(fileName);
  254. fs.Write(asset.Data, 0, asset.Data.Length);
  255. fs.Close();
  256. }
  257. }
  258. }