RestService.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. */
  28. using System;
  29. using System.IO;
  30. using System.Text;
  31. using System.Xml;
  32. using System.Xml.Serialization;
  33. using libsecondlife;
  34. using OpenSim.Framework;
  35. using OpenSim.Framework.Console;
  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 log4net.ILog m_log = log4net.LogManager.GetLogger(System.Reflection.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. try
  62. {
  63. string[] p = param.Split(new char[] {'/', '?', '&'}, StringSplitOptions.RemoveEmptyEntries);
  64. if (p.Length > 0)
  65. {
  66. LLUUID assetID = null;
  67. if (!LLUUID.TryParse(p[0], out assetID))
  68. {
  69. m_log.InfoFormat(
  70. "[REST]: GET:/asset ignoring request with malformed UUID {0}", p[0]);
  71. return result;
  72. }
  73. if (StatsManager.AssetStats != null)
  74. StatsManager.AssetStats.AddRequest();
  75. AssetBase asset = m_assetProvider.FetchAsset(assetID);
  76. if (asset != null)
  77. {
  78. XmlSerializer xs = new XmlSerializer(typeof (AssetBase));
  79. MemoryStream ms = new MemoryStream();
  80. XmlTextWriter xw = new XmlTextWriter(ms, Encoding.UTF8);
  81. xw.Formatting = Formatting.Indented;
  82. xs.Serialize(xw, asset);
  83. xw.Flush();
  84. ms.Seek(0, SeekOrigin.Begin);
  85. //StreamReader sr = new StreamReader(ms);
  86. result = ms.GetBuffer();
  87. m_log.InfoFormat(
  88. "[REST]: GET:/asset found {0} with name {1}, size {2} bytes",
  89. assetID, asset.Name, result.Length);
  90. Array.Resize<byte>(ref result, (int) ms.Length);
  91. }
  92. else
  93. {
  94. if (StatsManager.AssetStats != null)
  95. StatsManager.AssetStats.AddNotFoundRequest();
  96. m_log.InfoFormat("[REST]: GET:/asset failed to find {0}", assetID);
  97. }
  98. }
  99. }
  100. catch (Exception e)
  101. {
  102. m_log.Error(e.ToString());
  103. }
  104. return result;
  105. }
  106. }
  107. public class PostAssetStreamHandler : BaseStreamHandler
  108. {
  109. private static readonly log4net.ILog m_log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  110. private OpenAsset_Main m_assetManager;
  111. private IAssetProvider m_assetProvider;
  112. public override byte[] Handle(string path, Stream request)
  113. {
  114. string param = GetParam(path);
  115. LLUUID assetId;
  116. if (param.Length > 0)
  117. LLUUID.TryParse(param, out assetId);
  118. byte[] txBuffer = new byte[4096];
  119. XmlSerializer xs = new XmlSerializer(typeof (AssetBase));
  120. AssetBase asset = (AssetBase) xs.Deserialize(request);
  121. m_log.InfoFormat("[REST]: StoreAndCommitAsset {0}", asset.FullID);
  122. m_assetProvider.CreateAsset(asset);
  123. m_assetProvider.CommitAssets();
  124. return new byte[] {};
  125. }
  126. public PostAssetStreamHandler(OpenAsset_Main assetManager, IAssetProvider assetProvider)
  127. : base("POST", "/assets")
  128. {
  129. m_assetManager = assetManager;
  130. m_assetProvider = assetProvider;
  131. }
  132. }
  133. }