RestService.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 OpenSim 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.IO;
  29. using System.Reflection;
  30. using System.Text;
  31. using System.Xml;
  32. using System.Xml.Serialization;
  33. using libsecondlife;
  34. using log4net;
  35. using OpenSim.Framework;
  36. using OpenSim.Framework.Servers;
  37. using OpenSim.Framework.Statistics;
  38. namespace OpenSim.Grid.AssetServer
  39. {
  40. public class GetAssetStreamHandler : BaseStreamHandler
  41. {
  42. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  43. private OpenAsset_Main m_assetManager;
  44. private IAssetProvider m_assetProvider;
  45. /// <summary>
  46. /// Constructor.
  47. /// </summary>
  48. /// <param name="assetManager"></param>
  49. /// <param name="assetProvider"></param>
  50. public GetAssetStreamHandler(OpenAsset_Main assetManager, IAssetProvider assetProvider)
  51. : base("GET", "/assets")
  52. {
  53. m_log.Info("[REST]: In Get Request");
  54. m_assetManager = assetManager;
  55. m_assetProvider = assetProvider;
  56. }
  57. public override byte[] Handle(string path, Stream request)
  58. {
  59. string param = GetParam(path);
  60. byte[] result = new byte[] {};
  61. string[] p = param.Split(new char[] {'/', '?', '&'}, StringSplitOptions.RemoveEmptyEntries);
  62. if (p.Length > 0)
  63. {
  64. LLUUID assetID = null;
  65. if (!LLUUID.TryParse(p[0], out assetID))
  66. {
  67. m_log.InfoFormat(
  68. "[REST]: GET:/asset ignoring request with malformed UUID {0}", p[0]);
  69. return result;
  70. }
  71. if (StatsManager.AssetStats != null)
  72. StatsManager.AssetStats.AddRequest();
  73. AssetBase asset = m_assetProvider.FetchAsset(assetID);
  74. if (asset != null)
  75. {
  76. XmlSerializer xs = new XmlSerializer(typeof (AssetBase));
  77. MemoryStream ms = new MemoryStream();
  78. XmlTextWriter xw = new XmlTextWriter(ms, Encoding.UTF8);
  79. xw.Formatting = Formatting.Indented;
  80. xs.Serialize(xw, asset);
  81. xw.Flush();
  82. ms.Seek(0, SeekOrigin.Begin);
  83. //StreamReader sr = new StreamReader(ms);
  84. result = ms.GetBuffer();
  85. m_log.InfoFormat(
  86. "[REST]: GET:/asset found {0} with name {1}, size {2} bytes",
  87. assetID, asset.Name, result.Length);
  88. Array.Resize<byte>(ref result, (int) ms.Length);
  89. }
  90. else
  91. {
  92. if (StatsManager.AssetStats != null)
  93. StatsManager.AssetStats.AddNotFoundRequest();
  94. m_log.InfoFormat("[REST]: GET:/asset failed to find {0}", assetID);
  95. }
  96. }
  97. return result;
  98. }
  99. }
  100. public class PostAssetStreamHandler : BaseStreamHandler
  101. {
  102. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  103. private OpenAsset_Main m_assetManager;
  104. private IAssetProvider m_assetProvider;
  105. public override byte[] Handle(string path, Stream request)
  106. {
  107. string param = GetParam(path);
  108. LLUUID assetId;
  109. if (param.Length > 0)
  110. LLUUID.TryParse(param, out assetId);
  111. byte[] txBuffer = new byte[4096];
  112. XmlSerializer xs = new XmlSerializer(typeof (AssetBase));
  113. AssetBase asset = (AssetBase) xs.Deserialize(request);
  114. m_log.InfoFormat("[REST]: StoreAndCommitAsset {0}", asset.FullID);
  115. m_assetProvider.CreateAsset(asset);
  116. m_assetProvider.CommitAssets();
  117. return new byte[] {};
  118. }
  119. public PostAssetStreamHandler(OpenAsset_Main assetManager, IAssetProvider assetProvider)
  120. : base("POST", "/assets")
  121. {
  122. m_assetManager = assetManager;
  123. m_assetProvider = assetProvider;
  124. }
  125. }
  126. }