RestService.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 libsecondlife;
  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 IAssetProvider m_assetProvider;
  46. /// <summary>
  47. /// Constructor.
  48. /// </summary>
  49. /// <param name="assetManager"></param>
  50. /// <param name="assetProvider"></param>
  51. public GetAssetStreamHandler(OpenAsset_Main assetManager, IAssetProvider 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. 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. return result;
  100. }
  101. }
  102. public class PostAssetStreamHandler : BaseStreamHandler
  103. {
  104. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  105. private OpenAsset_Main m_assetManager;
  106. private IAssetProvider m_assetProvider;
  107. public override byte[] Handle(string path, Stream request,
  108. OSHttpRequest httpRequest, OSHttpResponse httpResponse)
  109. {
  110. string param = GetParam(path);
  111. LLUUID assetId;
  112. if (param.Length > 0)
  113. LLUUID.TryParse(param, out assetId);
  114. byte[] txBuffer = new byte[4096];
  115. XmlSerializer xs = new XmlSerializer(typeof (AssetBase));
  116. AssetBase asset = (AssetBase) xs.Deserialize(request);
  117. m_log.InfoFormat("[REST]: StoreAndCommitAsset {0}", asset.FullID);
  118. m_assetProvider.CreateAsset(asset);
  119. m_assetProvider.CommitAssets();
  120. return new byte[] {};
  121. }
  122. public PostAssetStreamHandler(OpenAsset_Main assetManager, IAssetProvider assetProvider)
  123. : base("POST", "/assets")
  124. {
  125. m_assetManager = assetManager;
  126. m_assetProvider = assetProvider;
  127. }
  128. }
  129. }