RestService.cs 6.1 KB

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