GetMeshHandler.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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;
  29. using System.Collections.Specialized;
  30. using System.Reflection;
  31. using System.IO;
  32. using System.Text;
  33. using System.Web;
  34. using log4net;
  35. using Nini.Config;
  36. using OpenMetaverse;
  37. using OpenMetaverse.StructuredData;
  38. using OpenSim.Framework;
  39. using OpenSim.Framework.Servers;
  40. using OpenSim.Framework.Servers.HttpServer;
  41. using OpenSim.Services.Interfaces;
  42. using Caps = OpenSim.Framework.Capabilities.Caps;
  43. namespace OpenSim.Capabilities.Handlers
  44. {
  45. public class GetMeshHandler : BaseStreamHandler
  46. {
  47. // private static readonly ILog m_log =
  48. // LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  49. private IAssetService m_assetService;
  50. public GetMeshHandler(string path, IAssetService assService, string name, string description)
  51. : base("GET", path, name, description)
  52. {
  53. m_assetService = assService;
  54. }
  55. protected override byte[] ProcessRequest(string path, Stream request, IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
  56. {
  57. // Try to parse the texture ID from the request URL
  58. NameValueCollection query = HttpUtility.ParseQueryString(httpRequest.Url.Query);
  59. string meshStr = query.GetOne("mesh_id");
  60. // m_log.DebugFormat("Fetching mesh {0}", meshStr);
  61. UUID meshID = UUID.Zero;
  62. if (!String.IsNullOrEmpty(meshStr) && UUID.TryParse(meshStr, out meshID))
  63. {
  64. if (m_assetService == null)
  65. {
  66. httpResponse.StatusCode = 404;
  67. httpResponse.ContentType = "text/plain";
  68. byte[] data = Encoding.UTF8.GetBytes("The asset service is unavailable. So is your mesh.");
  69. httpResponse.Body.Write(data, 0, data.Length);
  70. return null;
  71. }
  72. AssetBase mesh = m_assetService.Get(meshID.ToString());
  73. if (mesh != null)
  74. {
  75. if (mesh.Type == (SByte)AssetType.Mesh)
  76. {
  77. byte[] data = mesh.Data;
  78. httpResponse.Body.Write(data, 0, data.Length);
  79. httpResponse.ContentType = "application/vnd.ll.mesh";
  80. httpResponse.StatusCode = 200;
  81. }
  82. // Optionally add additional mesh types here
  83. else
  84. {
  85. httpResponse.StatusCode = 404;
  86. httpResponse.ContentType = "text/plain";
  87. byte[] data = Encoding.UTF8.GetBytes("Unfortunately, this asset isn't a mesh.");
  88. httpResponse.Body.Write(data, 0, data.Length);
  89. httpResponse.KeepAlive = false;
  90. }
  91. }
  92. else
  93. {
  94. httpResponse.StatusCode = 404;
  95. httpResponse.ContentType = "text/plain";
  96. byte[] data = Encoding.UTF8.GetBytes("Your Mesh wasn't found. Sorry!");
  97. httpResponse.Body.Write(data, 0, data.Length);
  98. httpResponse.KeepAlive = false;
  99. }
  100. }
  101. return null;
  102. }
  103. }
  104. }