BaseGetAssetStreamHandler.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 OpenSimulator 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.Collections.Generic;
  29. using System.IO;
  30. using System.Net;
  31. using System.Reflection;
  32. using System.Text;
  33. using System.Text.RegularExpressions;
  34. using System.Xml;
  35. using System.Xml.Serialization;
  36. using log4net;
  37. using OpenMetaverse;
  38. using OpenSim.Framework.Servers.HttpServer;
  39. using OpenSim.Framework.Statistics;
  40. namespace OpenSim.Framework.Servers
  41. {
  42. public abstract class BaseGetAssetStreamHandler : BaseStreamHandler
  43. {
  44. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  45. protected BaseGetAssetStreamHandler(string httpMethod, string path) : base(httpMethod, path)
  46. {
  47. }
  48. protected abstract AssetBase GetAsset(UUID assetID);
  49. public override byte[] Handle(string path, Stream request,
  50. OSHttpRequest httpRequest, OSHttpResponse httpResponse)
  51. {
  52. byte[] result = new byte[] { };
  53. string[] p = SplitParams(path);
  54. if (p.Length > 0)
  55. {
  56. UUID assetID;
  57. if (!UUID.TryParse(p[0], out assetID))
  58. {
  59. m_log.DebugFormat(
  60. "[REST]: GET:/asset ignoring request with malformed UUID {0}", p[0]);
  61. return result;
  62. }
  63. if (StatsManager.AssetStats != null)
  64. {
  65. StatsManager.AssetStats.AddRequest();
  66. }
  67. AssetBase asset = GetAsset(assetID);
  68. if (asset != null)
  69. {
  70. if (p.Length > 1 && p[1] == "data")
  71. {
  72. httpResponse.StatusCode = (int)HttpStatusCode.OK;
  73. httpResponse.ContentType = SLAssetTypeToContentType(asset.Type);
  74. result = asset.Data;
  75. }
  76. else
  77. {
  78. result = GetXml(asset);
  79. }
  80. }
  81. else
  82. {
  83. m_log.DebugFormat("[REST]: GET:/asset failed to find {0}", assetID);
  84. httpResponse.StatusCode = (int)HttpStatusCode.NotFound;
  85. if (StatsManager.AssetStats != null)
  86. {
  87. StatsManager.AssetStats.AddNotFoundRequest();
  88. }
  89. }
  90. }
  91. return result;
  92. }
  93. public static byte[] GetXml(AssetBase asset)
  94. {
  95. byte[] result;
  96. XmlSerializer xs = new XmlSerializer(typeof(AssetBase));
  97. MemoryStream ms = new MemoryStream();
  98. XmlTextWriter xw = new XmlTextWriter(ms, Encoding.UTF8);
  99. xw.Formatting = Formatting.Indented;
  100. xs.Serialize(xw, asset);
  101. xw.Flush();
  102. ms.Seek(0, SeekOrigin.Begin);
  103. //StreamReader sr = new StreamReader(ms);
  104. result = ms.GetBuffer();
  105. Array.Resize<byte>(ref result, (int)ms.Length);
  106. return result;
  107. }
  108. public string ProcessAssetDataString(string data)
  109. {
  110. Regex regex = new Regex("(creator_id|owner_id)\\s+(\\S+)");
  111. // IUserService userService = null;
  112. data = regex.Replace(data, delegate(Match m)
  113. {
  114. string result = String.Empty;
  115. // string key = m.Groups[1].Captures[0].Value;
  116. //
  117. // string value = m.Groups[2].Captures[0].Value;
  118. //
  119. // Guid userUri;
  120. //
  121. // switch (key)
  122. // {
  123. // case "creator_id":
  124. // userUri = new Guid(value);
  125. // // result = "creator_url " + userService(userService, userUri);
  126. // break;
  127. //
  128. // case "owner_id":
  129. // userUri = new Guid(value);
  130. // // result = "owner_url " + ResolveUserUri(userService, userUri);
  131. // break;
  132. // }
  133. return result;
  134. });
  135. return data;
  136. }
  137. private string SLAssetTypeToContentType(int assetType)
  138. {
  139. switch (assetType)
  140. {
  141. case 0:
  142. return "image/jp2";
  143. case 1:
  144. return "application/ogg";
  145. case 2:
  146. return "application/x-metaverse-callingcard";
  147. case 3:
  148. return "application/x-metaverse-landmark";
  149. case 5:
  150. return "application/x-metaverse-clothing";
  151. case 6:
  152. return "application/x-metaverse-primitive";
  153. case 7:
  154. return "application/x-metaverse-notecard";
  155. case 8:
  156. return "application/x-metaverse-folder";
  157. case 10:
  158. return "application/x-metaverse-lsl";
  159. case 11:
  160. return "application/x-metaverse-lso";
  161. case 12:
  162. return "image/tga";
  163. case 13:
  164. return "application/x-metaverse-bodypart";
  165. case 17:
  166. return "audio/x-wav";
  167. case 19:
  168. return "image/jpeg";
  169. case 20:
  170. return "application/x-metaverse-animation";
  171. case 21:
  172. return "application/x-metaverse-gesture";
  173. case 22:
  174. return "application/x-metaverse-simstate";
  175. default:
  176. return "application/octet-stream";
  177. }
  178. }
  179. }
  180. }