AssetServiceConnector.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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.Communications;
  35. using OpenSim.Framework.Servers.HttpServer;
  36. using OpenSim.Services.Interfaces;
  37. namespace OpenSim.Services.Connectors
  38. {
  39. public class AssetServicesConnector : IAssetService
  40. {
  41. private static readonly ILog m_log =
  42. LogManager.GetLogger(
  43. MethodBase.GetCurrentMethod().DeclaringType);
  44. private string m_ServerURI = String.Empty;
  45. private IImprovedAssetCache m_Cache = null;
  46. public AssetServicesConnector()
  47. {
  48. }
  49. public AssetServicesConnector(string serverURI)
  50. {
  51. m_ServerURI = serverURI.TrimEnd('/');
  52. }
  53. public AssetServicesConnector(IConfigSource source)
  54. {
  55. Initialise(source);
  56. }
  57. public virtual void Initialise(IConfigSource source)
  58. {
  59. IConfig assetConfig = source.Configs["AssetService"];
  60. if (assetConfig == null)
  61. {
  62. m_log.Error("[ASSET CONNECTOR]: AssetService missing from OpanSim.ini");
  63. throw new Exception("Asset connector init error");
  64. }
  65. string serviceURI = assetConfig.GetString("AssetServerURI",
  66. String.Empty);
  67. if (serviceURI == String.Empty)
  68. {
  69. m_log.Error("[ASSET CONNECTOR]: No Server URI named in section AssetService");
  70. throw new Exception("Asset connector init error");
  71. }
  72. m_ServerURI = serviceURI;
  73. }
  74. protected void SetCache(IImprovedAssetCache cache)
  75. {
  76. m_Cache = cache;
  77. }
  78. public AssetBase Get(string id)
  79. {
  80. string uri = m_ServerURI + "/assets/" + id;
  81. AssetBase asset = null;
  82. if (m_Cache != null)
  83. asset = m_Cache.Get(id);
  84. if (asset == null)
  85. {
  86. asset = SynchronousRestObjectRequester.
  87. MakeRequest<int, AssetBase>("GET", uri, 0);
  88. if (m_Cache != null)
  89. m_Cache.Cache(asset);
  90. }
  91. return asset;
  92. }
  93. public AssetMetadata GetMetadata(string id)
  94. {
  95. if (m_Cache != null)
  96. {
  97. AssetBase fullAsset = m_Cache.Get(id);
  98. if (fullAsset != null)
  99. return fullAsset.Metadata;
  100. }
  101. string uri = m_ServerURI + "/assets/" + id + "/metadata";
  102. AssetMetadata asset = SynchronousRestObjectRequester.
  103. MakeRequest<int, AssetMetadata>("GET", uri, 0);
  104. return asset;
  105. }
  106. public byte[] GetData(string id)
  107. {
  108. if (m_Cache != null)
  109. {
  110. AssetBase fullAsset = m_Cache.Get(id);
  111. if (fullAsset != null)
  112. return fullAsset.Data;
  113. }
  114. RestClient rc = new RestClient(m_ServerURI);
  115. rc.AddResourcePath("assets");
  116. rc.AddResourcePath(id);
  117. rc.AddResourcePath("data");
  118. rc.RequestMethod = "GET";
  119. Stream s = rc.Request();
  120. if (s == null)
  121. return null;
  122. if (s.Length > 0)
  123. {
  124. byte[] ret = new byte[s.Length];
  125. s.Read(ret, 0, (int)s.Length);
  126. return ret;
  127. }
  128. return null;
  129. }
  130. public bool Get(string id, Object sender, AssetRetrieved handler)
  131. {
  132. string uri = m_ServerURI + "/assets/" + id;
  133. AssetBase asset = null;
  134. if (m_Cache != null)
  135. asset = m_Cache.Get(id);
  136. if (asset == null)
  137. {
  138. AsynchronousRestObjectRequester.
  139. MakeRequest<int, AssetBase>("GET", uri, 0,
  140. delegate(AssetBase a)
  141. {
  142. if (m_Cache != null)
  143. m_Cache.Cache(a);
  144. handler(id, sender, a);
  145. });
  146. }
  147. else
  148. {
  149. handler.BeginInvoke(id, sender, asset, null, null);
  150. }
  151. return true;
  152. }
  153. public string Store(AssetBase asset)
  154. {
  155. if (asset.Temporary || asset.Local)
  156. {
  157. if (m_Cache != null)
  158. m_Cache.Cache(asset);
  159. return asset.ID;
  160. }
  161. string uri = m_ServerURI + "/assets/";
  162. string newID = string.Empty;
  163. try
  164. {
  165. newID = SynchronousRestObjectRequester.
  166. MakeRequest<AssetBase, string>("POST", uri, asset);
  167. }
  168. catch (Exception e)
  169. {
  170. m_log.WarnFormat("[ASSET CONNECTOR]: Unable to send asset {0} to asset server. Reason: {1}", asset.ID, e.Message);
  171. }
  172. if (newID != String.Empty)
  173. {
  174. // Placing this here, so that this work with old asset servers that don't send any reply back
  175. // SynchronousRestObjectRequester returns somethins that is not an empty string
  176. if (newID != null)
  177. asset.ID = newID;
  178. if (m_Cache != null)
  179. m_Cache.Cache(asset);
  180. }
  181. return newID;
  182. }
  183. public bool UpdateContent(string id, byte[] data)
  184. {
  185. AssetBase asset = null;
  186. if (m_Cache != null)
  187. asset = m_Cache.Get(id);
  188. if (asset == null)
  189. {
  190. AssetMetadata metadata = GetMetadata(id);
  191. if (metadata == null)
  192. return false;
  193. asset = new AssetBase();
  194. asset.Metadata = metadata;
  195. }
  196. asset.Data = data;
  197. string uri = m_ServerURI + "/assets/" + id;
  198. if (SynchronousRestObjectRequester.
  199. MakeRequest<AssetBase, bool>("POST", uri, asset))
  200. {
  201. if (m_Cache != null)
  202. m_Cache.Cache(asset);
  203. return true;
  204. }
  205. return false;
  206. }
  207. public bool Delete(string id)
  208. {
  209. string uri = m_ServerURI + "/assets/" + id;
  210. if (SynchronousRestObjectRequester.
  211. MakeRequest<int, bool>("DELETE", uri, 0))
  212. {
  213. if (m_Cache != null)
  214. m_Cache.Expire(id);
  215. return true;
  216. }
  217. return false;
  218. }
  219. }
  220. }