RestService.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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.Net;
  30. using System.Reflection;
  31. using System.Text;
  32. using System.Xml;
  33. using System.Xml.Serialization;
  34. using OpenMetaverse;
  35. using log4net;
  36. using OpenSim.Framework;
  37. using OpenSim.Framework.Servers;
  38. using OpenSim.Framework.Statistics;
  39. namespace OpenSim.Grid.AssetServer
  40. {
  41. public class GetAssetStreamHandler : BaseStreamHandler
  42. {
  43. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  44. // private OpenAsset_Main m_assetManager;
  45. private IAssetProviderPlugin m_assetProvider;
  46. /// <summary>
  47. /// Constructor.
  48. /// </summary>
  49. /// <param name="assetManager"></param>
  50. /// <param name="assetProvider"></param>
  51. public GetAssetStreamHandler(IAssetProviderPlugin assetProvider)
  52. : base("GET", "/assets")
  53. {
  54. m_log.Info("[REST]: In Get Request");
  55. // m_assetManager = assetManager;
  56. m_assetProvider = assetProvider;
  57. }
  58. public override byte[] Handle(string path, Stream request,
  59. OSHttpRequest httpRequest, OSHttpResponse httpResponse)
  60. {
  61. string param = GetParam(path);
  62. byte[] result = new byte[] {};
  63. string[] p = param.Split(new char[] {'/', '?', '&'}, StringSplitOptions.RemoveEmptyEntries);
  64. if (p.Length > 0)
  65. {
  66. UUID assetID = UUID.Zero;
  67. if (!UUID.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. //Ckrinke 1/11/09 Commenting out the succesful REST message as under heavy use there
  88. //are multiple messages in a second and that is usually (in my experience) meaning
  89. //the logging itself is slowing down the program. Leaving the unsuccesful message
  90. //as we need to know about that path.
  91. // m_log.InfoFormat(
  92. // "[REST]: GET:/asset found {0} with name {1}, size {2} bytes",
  93. // assetID, asset.Name, result.Length);
  94. Array.Resize<byte>(ref result, (int) ms.Length);
  95. }
  96. else
  97. {
  98. if (StatsManager.AssetStats != null)
  99. StatsManager.AssetStats.AddNotFoundRequest();
  100. m_log.InfoFormat("[REST]: GET:/asset failed to find {0}", assetID);
  101. }
  102. }
  103. return result;
  104. }
  105. }
  106. public class PostAssetStreamHandler : BaseStreamHandler
  107. {
  108. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  109. // private OpenAsset_Main m_assetManager;
  110. private IAssetProviderPlugin m_assetProvider;
  111. public override byte[] Handle(string path, Stream request,
  112. OSHttpRequest httpRequest, OSHttpResponse httpResponse)
  113. {
  114. string param = GetParam(path);
  115. UUID assetId;
  116. if (param.Length > 0)
  117. UUID.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]: Creating asset {0}", asset.FullID);
  122. m_assetProvider.CreateAsset(asset);
  123. return new byte[] {};
  124. }
  125. public PostAssetStreamHandler(IAssetProviderPlugin assetProvider)
  126. : base("POST", "/assets")
  127. {
  128. // m_assetManager = assetManager;
  129. m_assetProvider = assetProvider;
  130. }
  131. }
  132. }