1
0

AssetServiceConnector.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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 OpenSim.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 AssetBase GetCached(string id)
  99. {
  100. if (m_Cache != null)
  101. return m_Cache.Get(id);
  102. return null;
  103. }
  104. public AssetMetadata GetMetadata(string id)
  105. {
  106. if (m_Cache != null)
  107. {
  108. AssetBase fullAsset = m_Cache.Get(id);
  109. if (fullAsset != null)
  110. return fullAsset.Metadata;
  111. }
  112. string uri = m_ServerURI + "/assets/" + id + "/metadata";
  113. AssetMetadata asset = SynchronousRestObjectRequester.
  114. MakeRequest<int, AssetMetadata>("GET", uri, 0);
  115. return asset;
  116. }
  117. public byte[] GetData(string id)
  118. {
  119. if (m_Cache != null)
  120. {
  121. AssetBase fullAsset = m_Cache.Get(id);
  122. if (fullAsset != null)
  123. return fullAsset.Data;
  124. }
  125. RestClient rc = new RestClient(m_ServerURI);
  126. rc.AddResourcePath("assets");
  127. rc.AddResourcePath(id);
  128. rc.AddResourcePath("data");
  129. rc.RequestMethod = "GET";
  130. Stream s = rc.Request();
  131. if (s == null)
  132. return null;
  133. if (s.Length > 0)
  134. {
  135. byte[] ret = new byte[s.Length];
  136. s.Read(ret, 0, (int)s.Length);
  137. return ret;
  138. }
  139. return null;
  140. }
  141. public bool Get(string id, Object sender, AssetRetrieved handler)
  142. {
  143. string uri = m_ServerURI + "/assets/" + id;
  144. AssetBase asset = null;
  145. if (m_Cache != null)
  146. asset = m_Cache.Get(id);
  147. if (asset == null)
  148. {
  149. bool result = false;
  150. AsynchronousRestObjectRequester.
  151. MakeRequest<int, AssetBase>("GET", uri, 0,
  152. delegate(AssetBase a)
  153. {
  154. if (m_Cache != null)
  155. m_Cache.Cache(a);
  156. handler(id, sender, a);
  157. result = true;
  158. });
  159. return result;
  160. }
  161. else
  162. {
  163. //Util.FireAndForget(delegate { handler(id, sender, asset); });
  164. handler(id, sender, asset);
  165. }
  166. return true;
  167. }
  168. public string Store(AssetBase asset)
  169. {
  170. if (asset.Temporary || asset.Local)
  171. {
  172. if (m_Cache != null)
  173. m_Cache.Cache(asset);
  174. return asset.ID;
  175. }
  176. string uri = m_ServerURI + "/assets/";
  177. string newID = string.Empty;
  178. try
  179. {
  180. newID = SynchronousRestObjectRequester.
  181. MakeRequest<AssetBase, string>("POST", uri, asset);
  182. }
  183. catch (Exception e)
  184. {
  185. m_log.WarnFormat("[ASSET CONNECTOR]: Unable to send asset {0} to asset server. Reason: {1}", asset.ID, e.Message);
  186. }
  187. if (newID != String.Empty)
  188. {
  189. // Placing this here, so that this work with old asset servers that don't send any reply back
  190. // SynchronousRestObjectRequester returns somethins that is not an empty string
  191. if (newID != null)
  192. asset.ID = newID;
  193. if (m_Cache != null)
  194. m_Cache.Cache(asset);
  195. }
  196. return newID;
  197. }
  198. public bool UpdateContent(string id, byte[] data)
  199. {
  200. AssetBase asset = null;
  201. if (m_Cache != null)
  202. asset = m_Cache.Get(id);
  203. if (asset == null)
  204. {
  205. AssetMetadata metadata = GetMetadata(id);
  206. if (metadata == null)
  207. return false;
  208. asset = new AssetBase(metadata.FullID, metadata.Name, metadata.Type, UUID.Zero.ToString());
  209. asset.Metadata = metadata;
  210. }
  211. asset.Data = data;
  212. string uri = m_ServerURI + "/assets/" + id;
  213. if (SynchronousRestObjectRequester.
  214. MakeRequest<AssetBase, bool>("POST", uri, asset))
  215. {
  216. if (m_Cache != null)
  217. m_Cache.Cache(asset);
  218. return true;
  219. }
  220. return false;
  221. }
  222. public bool Delete(string id)
  223. {
  224. string uri = m_ServerURI + "/assets/" + id;
  225. if (SynchronousRestObjectRequester.
  226. MakeRequest<int, bool>("DELETE", uri, 0))
  227. {
  228. if (m_Cache != null)
  229. m_Cache.Expire(id);
  230. return true;
  231. }
  232. return false;
  233. }
  234. private void HandleDumpAsset(string module, string[] args)
  235. {
  236. if (args.Length != 4)
  237. {
  238. MainConsole.Instance.Output("Syntax: dump asset <id> <file>");
  239. return;
  240. }
  241. UUID assetID;
  242. if (!UUID.TryParse(args[2], out assetID))
  243. {
  244. MainConsole.Instance.Output("Invalid asset ID");
  245. return;
  246. }
  247. if (m_Cache == null)
  248. {
  249. MainConsole.Instance.Output("Instance uses no cache");
  250. return;
  251. }
  252. AssetBase asset = m_Cache.Get(assetID.ToString());
  253. if (asset == null)
  254. {
  255. MainConsole.Instance.Output("Asset not found in cache");
  256. return;
  257. }
  258. string fileName = args[3];
  259. FileStream fs = File.Create(fileName);
  260. fs.Write(asset.Data, 0, asset.Data.Length);
  261. fs.Close();
  262. }
  263. }
  264. }