SimianAssetServiceConnector.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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.Net;
  31. using System.Reflection;
  32. using log4net;
  33. using Mono.Addins;
  34. using Nini.Config;
  35. using OpenSim.Framework;
  36. using OpenSim.Region.Framework.Interfaces;
  37. using OpenSim.Region.Framework.Scenes;
  38. using OpenSim.Services.Interfaces;
  39. using OpenMetaverse;
  40. using OpenMetaverse.StructuredData;
  41. namespace OpenSim.Services.Connectors.SimianGrid
  42. {
  43. /// <summary>
  44. /// Connects to the SimianGrid asset service
  45. /// </summary>
  46. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule")]
  47. public class SimianAssetServiceConnector : IAssetService, ISharedRegionModule
  48. {
  49. private static readonly ILog m_log =
  50. LogManager.GetLogger(
  51. MethodBase.GetCurrentMethod().DeclaringType);
  52. private static string ZeroID = UUID.Zero.ToString();
  53. private string m_serverUrl = String.Empty;
  54. private IImprovedAssetCache m_cache;
  55. private bool m_Enabled = false;
  56. #region ISharedRegionModule
  57. public Type ReplaceableInterface { get { return null; } }
  58. public void RegionLoaded(Scene scene)
  59. {
  60. if (m_cache == null)
  61. {
  62. IImprovedAssetCache cache = scene.RequestModuleInterface<IImprovedAssetCache>();
  63. if (cache is ISharedRegionModule)
  64. m_cache = cache;
  65. }
  66. }
  67. public void PostInitialise() { }
  68. public void Close() { }
  69. public SimianAssetServiceConnector() { }
  70. public string Name { get { return "SimianAssetServiceConnector"; } }
  71. public void AddRegion(Scene scene) { if (m_Enabled) { scene.RegisterModuleInterface<IAssetService>(this); } }
  72. public void RemoveRegion(Scene scene) { if (m_Enabled) { scene.UnregisterModuleInterface<IAssetService>(this); } }
  73. #endregion ISharedRegionModule
  74. public SimianAssetServiceConnector(IConfigSource source)
  75. {
  76. CommonInit(source);
  77. }
  78. public SimianAssetServiceConnector(string url)
  79. {
  80. m_serverUrl = url;
  81. }
  82. public void Initialise(IConfigSource source)
  83. {
  84. IConfig moduleConfig = source.Configs["Modules"];
  85. if (moduleConfig != null)
  86. {
  87. string name = moduleConfig.GetString("AssetServices", "");
  88. if (name == Name)
  89. CommonInit(source);
  90. }
  91. }
  92. private void CommonInit(IConfigSource source)
  93. {
  94. IConfig gridConfig = source.Configs["AssetService"];
  95. if (gridConfig != null)
  96. {
  97. string serviceUrl = gridConfig.GetString("AssetServerURI");
  98. if (!String.IsNullOrEmpty(serviceUrl))
  99. {
  100. if (!serviceUrl.EndsWith("/") && !serviceUrl.EndsWith("="))
  101. serviceUrl = serviceUrl + '/';
  102. m_serverUrl = serviceUrl;
  103. }
  104. }
  105. if (String.IsNullOrEmpty(m_serverUrl))
  106. m_log.Info("[SIMIAN ASSET CONNECTOR]: No AssetServerURI specified, disabling connector");
  107. else
  108. m_Enabled = true;
  109. }
  110. #region IAssetService
  111. public AssetBase Get(string id)
  112. {
  113. if (String.IsNullOrEmpty(m_serverUrl))
  114. {
  115. m_log.Error("[SIMIAN ASSET CONNECTOR]: No AssetServerURI configured");
  116. throw new InvalidOperationException();
  117. }
  118. // Cache fetch
  119. if (m_cache != null)
  120. {
  121. AssetBase asset = m_cache.Get(id);
  122. if (asset != null)
  123. return asset;
  124. }
  125. return GetRemote(id);
  126. }
  127. public AssetBase GetCached(string id)
  128. {
  129. if (m_cache != null)
  130. return m_cache.Get(id);
  131. return null;
  132. }
  133. /// <summary>
  134. /// Get an asset's metadata
  135. /// </summary>
  136. /// <param name="id"></param>
  137. /// <returns></returns>
  138. public AssetMetadata GetMetadata(string id)
  139. {
  140. if (String.IsNullOrEmpty(m_serverUrl))
  141. {
  142. m_log.Error("[SIMIAN ASSET CONNECTOR]: No AssetServerURI configured");
  143. throw new InvalidOperationException();
  144. }
  145. AssetMetadata metadata = null;
  146. // Cache fetch
  147. if (m_cache != null)
  148. {
  149. AssetBase asset = m_cache.Get(id);
  150. if (asset != null)
  151. return asset.Metadata;
  152. }
  153. Uri url;
  154. // Determine if id is an absolute URL or a grid-relative UUID
  155. if (!Uri.TryCreate(id, UriKind.Absolute, out url))
  156. url = new Uri(m_serverUrl + id);
  157. try
  158. {
  159. HttpWebRequest request = UntrustedHttpWebRequest.Create(url);
  160. request.Method = "HEAD";
  161. using (WebResponse response = request.GetResponse())
  162. {
  163. using (Stream responseStream = response.GetResponseStream())
  164. {
  165. // Create the metadata object
  166. metadata = new AssetMetadata();
  167. metadata.ContentType = response.ContentType;
  168. metadata.ID = id;
  169. UUID uuid;
  170. if (UUID.TryParse(id, out uuid))
  171. metadata.FullID = uuid;
  172. string lastModifiedStr = response.Headers.Get("Last-Modified");
  173. if (!String.IsNullOrEmpty(lastModifiedStr))
  174. {
  175. DateTime lastModified;
  176. if (DateTime.TryParse(lastModifiedStr, out lastModified))
  177. metadata.CreationDate = lastModified;
  178. }
  179. }
  180. }
  181. }
  182. catch (Exception ex)
  183. {
  184. m_log.Warn("[SIMIAN ASSET CONNECTOR]: Asset HEAD from " + url + " failed: " + ex.Message);
  185. }
  186. return metadata;
  187. }
  188. public byte[] GetData(string id)
  189. {
  190. AssetBase asset = Get(id);
  191. if (asset != null)
  192. return asset.Data;
  193. return null;
  194. }
  195. /// <summary>
  196. /// Get an asset asynchronously
  197. /// </summary>
  198. /// <param name="id">The asset id</param>
  199. /// <param name="sender">Represents the requester. Passed back via the handler</param>
  200. /// <param name="handler">The handler to call back once the asset has been retrieved</param>
  201. /// <returns>True if the id was parseable, false otherwise</returns>
  202. public bool Get(string id, Object sender, AssetRetrieved handler)
  203. {
  204. if (String.IsNullOrEmpty(m_serverUrl))
  205. {
  206. m_log.Error("[SIMIAN ASSET CONNECTOR]: No AssetServerURI configured");
  207. throw new InvalidOperationException();
  208. }
  209. // Cache fetch
  210. if (m_cache != null)
  211. {
  212. AssetBase asset = m_cache.Get(id);
  213. if (asset != null)
  214. {
  215. handler(id, sender, asset);
  216. return true;
  217. }
  218. }
  219. Util.FireAndForget(
  220. delegate(object o)
  221. {
  222. AssetBase asset = GetRemote(id);
  223. handler(id, sender, asset);
  224. }
  225. );
  226. return true;
  227. }
  228. /// <summary>
  229. /// Creates a new asset
  230. /// </summary>
  231. /// Returns a random ID if none is passed into it
  232. /// <param name="asset"></param>
  233. /// <returns></returns>
  234. public string Store(AssetBase asset)
  235. {
  236. if (String.IsNullOrEmpty(m_serverUrl))
  237. {
  238. m_log.Error("[SIMIAN ASSET CONNECTOR]: No AssetServerURI configured");
  239. throw new InvalidOperationException();
  240. }
  241. bool storedInCache = false;
  242. string errorMessage = null;
  243. // AssetID handling
  244. if (String.IsNullOrEmpty(asset.ID) || asset.ID == ZeroID)
  245. {
  246. asset.FullID = UUID.Random();
  247. asset.ID = asset.FullID.ToString();
  248. }
  249. // Cache handling
  250. if (m_cache != null)
  251. {
  252. m_cache.Cache(asset);
  253. storedInCache = true;
  254. }
  255. // Local asset handling
  256. if (asset.Local)
  257. {
  258. if (!storedInCache)
  259. {
  260. m_log.Error("Cannot store local " + asset.Metadata.ContentType + " asset without an asset cache");
  261. asset.ID = null;
  262. asset.FullID = UUID.Zero;
  263. }
  264. return asset.ID;
  265. }
  266. // Distinguish public and private assets
  267. bool isPublic = true;
  268. switch ((AssetType)asset.Type)
  269. {
  270. case AssetType.CallingCard:
  271. case AssetType.Gesture:
  272. case AssetType.LSLBytecode:
  273. case AssetType.LSLText:
  274. isPublic = false;
  275. break;
  276. }
  277. // Make sure ContentType is set
  278. if (String.IsNullOrEmpty(asset.Metadata.ContentType))
  279. asset.Metadata.ContentType = SLUtil.SLAssetTypeToContentType(asset.Type);
  280. // Build the remote storage request
  281. List<MultipartForm.Element> postParameters = new List<MultipartForm.Element>()
  282. {
  283. new MultipartForm.Parameter("AssetID", asset.FullID.ToString()),
  284. new MultipartForm.Parameter("CreatorID", asset.Metadata.CreatorID),
  285. new MultipartForm.Parameter("Temporary", asset.Temporary ? "1" : "0"),
  286. new MultipartForm.Parameter("Public", isPublic ? "1" : "0"),
  287. new MultipartForm.File("Asset", asset.Name, asset.Metadata.ContentType, asset.Data)
  288. };
  289. // Make the remote storage request
  290. try
  291. {
  292. HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(m_serverUrl);
  293. HttpWebResponse response = MultipartForm.Post(request, postParameters);
  294. using (Stream responseStream = response.GetResponseStream())
  295. {
  296. string responseStr = null;
  297. try
  298. {
  299. responseStr = responseStream.GetStreamString();
  300. OSD responseOSD = OSDParser.Deserialize(responseStr);
  301. if (responseOSD.Type == OSDType.Map)
  302. {
  303. OSDMap responseMap = (OSDMap)responseOSD;
  304. if (responseMap["Success"].AsBoolean())
  305. return asset.ID;
  306. else
  307. errorMessage = "Upload failed: " + responseMap["Message"].AsString();
  308. }
  309. else
  310. {
  311. errorMessage = "Response format was invalid:\n" + responseStr;
  312. }
  313. }
  314. catch (Exception ex)
  315. {
  316. if (!String.IsNullOrEmpty(responseStr))
  317. errorMessage = "Failed to parse the response:\n" + responseStr;
  318. else
  319. errorMessage = "Failed to retrieve the response: " + ex.Message;
  320. }
  321. }
  322. }
  323. catch (WebException ex)
  324. {
  325. errorMessage = ex.Message;
  326. }
  327. m_log.WarnFormat("[SIMIAN ASSET CONNECTOR]: Failed to store asset \"{0}\" ({1}, {2}): {3}",
  328. asset.Name, asset.ID, asset.Metadata.ContentType, errorMessage);
  329. return null;
  330. }
  331. /// <summary>
  332. /// Update an asset's content
  333. /// </summary>
  334. /// Attachments and bare scripts need this!!
  335. /// <param name="id"> </param>
  336. /// <param name="data"></param>
  337. /// <returns></returns>
  338. public bool UpdateContent(string id, byte[] data)
  339. {
  340. AssetBase asset = Get(id);
  341. if (asset == null)
  342. {
  343. m_log.Warn("[SIMIAN ASSET CONNECTOR]: Failed to fetch asset " + id + " for updating");
  344. return false;
  345. }
  346. asset.Data = data;
  347. string result = Store(asset);
  348. return !String.IsNullOrEmpty(result);
  349. }
  350. /// <summary>
  351. /// Delete an asset
  352. /// </summary>
  353. /// <param name="id"></param>
  354. /// <returns></returns>
  355. public bool Delete(string id)
  356. {
  357. if (String.IsNullOrEmpty(m_serverUrl))
  358. {
  359. m_log.Error("[SIMIAN ASSET CONNECTOR]: No AssetServerURI configured");
  360. throw new InvalidOperationException();
  361. }
  362. //string errorMessage = String.Empty;
  363. string url = m_serverUrl + id;
  364. if (m_cache != null)
  365. m_cache.Expire(id);
  366. try
  367. {
  368. HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
  369. request.Method = "DELETE";
  370. using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
  371. {
  372. if (response.StatusCode != HttpStatusCode.NoContent)
  373. {
  374. m_log.Warn("[SIMIAN ASSET CONNECTOR]: Unexpected response when deleting asset " + url + ": " +
  375. response.StatusCode + " (" + response.StatusDescription + ")");
  376. }
  377. }
  378. return true;
  379. }
  380. catch (Exception ex)
  381. {
  382. m_log.Warn("[SIMIAN ASSET CONNECTOR]: Failed to delete asset " + id + " from the asset service: " + ex.Message);
  383. return false;
  384. }
  385. }
  386. #endregion IAssetService
  387. private AssetBase GetRemote(string id)
  388. {
  389. AssetBase asset = null;
  390. Uri url;
  391. // Determine if id is an absolute URL or a grid-relative UUID
  392. if (!Uri.TryCreate(id, UriKind.Absolute, out url))
  393. url = new Uri(m_serverUrl + id);
  394. try
  395. {
  396. HttpWebRequest request = UntrustedHttpWebRequest.Create(url);
  397. using (WebResponse response = request.GetResponse())
  398. {
  399. using (Stream responseStream = response.GetResponseStream())
  400. {
  401. string creatorID = response.Headers.GetOne("X-Asset-Creator-Id") ?? String.Empty;
  402. // Create the asset object
  403. asset = new AssetBase(id, String.Empty, SLUtil.ContentTypeToSLAssetType(response.ContentType), creatorID);
  404. UUID assetID;
  405. if (UUID.TryParse(id, out assetID))
  406. asset.FullID = assetID;
  407. // Grab the asset data from the response stream
  408. using (MemoryStream stream = new MemoryStream())
  409. {
  410. responseStream.CopyTo(stream, Int32.MaxValue);
  411. asset.Data = stream.ToArray();
  412. }
  413. }
  414. }
  415. // Cache store
  416. if (m_cache != null && asset != null)
  417. m_cache.Cache(asset);
  418. return asset;
  419. }
  420. catch (Exception ex)
  421. {
  422. m_log.Warn("[SIMIAN ASSET CONNECTOR]: Asset GET from " + url + " failed: " + ex.Message);
  423. return null;
  424. }
  425. }
  426. }
  427. }