BrowseFrontendPlugin.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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.IO;
  29. using System.Reflection;
  30. using System.Collections.Specialized;
  31. using System.Net;
  32. using System.Text;
  33. using System.Web;
  34. using OpenMetaverse;
  35. using OpenSim.Framework;
  36. using OpenSim.Framework.Servers;
  37. using OpenSim.Framework.Servers.HttpServer;
  38. using log4net;
  39. namespace OpenSim.Grid.AssetInventoryServer.Plugins
  40. {
  41. public class BrowseFrontendPlugin : IAssetInventoryServerPlugin
  42. {
  43. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  44. private AssetInventoryServer m_server;
  45. public BrowseFrontendPlugin()
  46. {
  47. }
  48. #region IPlugin implementation
  49. public void Initialise(AssetInventoryServer server)
  50. {
  51. m_server = server;
  52. // Request for / or /?...
  53. m_server.HttpServer.AddStreamHandler(new BrowseRequestHandler(server));
  54. m_log.Info("[BROWSEFRONTEND]: Browser Frontend loaded.");
  55. }
  56. /// <summary>
  57. /// <para>Initialises asset interface</para>
  58. /// </summary>
  59. public void Initialise()
  60. {
  61. m_log.InfoFormat("[BROWSEFRONTEND]: {0} cannot be default-initialized!", Name);
  62. throw new PluginNotInitialisedException(Name);
  63. }
  64. public void Dispose()
  65. {
  66. }
  67. public string Version
  68. {
  69. // TODO: this should be something meaningful and not hardcoded?
  70. get { return "0.1"; }
  71. }
  72. public string Name
  73. {
  74. get { return "BrowseFrontend"; }
  75. }
  76. #endregion IPlugin implementation
  77. public class BrowseRequestHandler : BaseStreamHandler
  78. {
  79. AssetInventoryServer m_server;
  80. //public BrowseRequestHandler(AssetInventoryServer server) : base("GET", "(^/$|(^/\?.*)")
  81. public BrowseRequestHandler(AssetInventoryServer server) : base("GET", "/")
  82. {
  83. m_server = server;
  84. }
  85. public override string ContentType
  86. {
  87. get { return "text/html"; }
  88. }
  89. #region IStreamedRequestHandler implementation
  90. public override byte[] Handle(string path, Stream request, OSHttpRequest httpRequest, OSHttpResponse httpResponse)
  91. {
  92. const int ASSETS_PER_PAGE = 25;
  93. const string HEADER = "<html><head><title>Asset Server</title></head><body>";
  94. const string TABLE_HEADER =
  95. "<table><tr><th>Name</th><th>Description</th><th>Type</th><th>ID</th><th>Temporary</th><th>SHA-1</th></tr>";
  96. const string TABLE_FOOTER = "</table>";
  97. const string FOOTER = "</body></html>";
  98. UUID authToken = Utils.GetAuthToken(httpRequest);
  99. StringBuilder html = new StringBuilder();
  100. int start = 0;
  101. uint page = 0;
  102. if (!String.IsNullOrEmpty(httpRequest.Url.Query))
  103. {
  104. NameValueCollection query = HttpUtility.ParseQueryString(httpRequest.Url.Query);
  105. if (!String.IsNullOrEmpty(query["page"]) && UInt32.TryParse(query["page"], out page))
  106. start = (int)page * ASSETS_PER_PAGE;
  107. }
  108. html.AppendLine(HEADER);
  109. html.AppendLine("<p>");
  110. if (page > 0)
  111. html.AppendFormat("<a href=\"{0}?page={1}\">&lt; Previous Page</a> | ", httpRequest.RawUrl, page - 1);
  112. html.AppendFormat("<a href=\"{0}?page={1}\">Next Page &gt;</a>", httpRequest.RawUrl, page + 1);
  113. html.AppendLine("</p>");
  114. html.AppendLine(TABLE_HEADER);
  115. m_server.StorageProvider.ForEach(
  116. delegate(AssetMetadata data)
  117. {
  118. if (m_server.AuthorizationProvider.IsMetadataAuthorized(authToken, data.FullID))
  119. {
  120. html.AppendLine(String.Format(
  121. "<tr><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td><td>{4}</td><td>{5}</td></tr>",
  122. data.Name, data.Description, data.ContentType, data.ID, data.Temporary,
  123. BitConverter.ToString(data.SHA1).Replace("-", String.Empty)));
  124. }
  125. else
  126. {
  127. html.AppendLine(String.Format(
  128. "<tr><td>[Protected Asset]</td><td>&nbsp;</td><td>&nbsp;</td><td>{0}</td><td>{1}</td><td>&nbsp;</td></tr>",
  129. data.ID, data.Temporary));
  130. }
  131. }, start, ASSETS_PER_PAGE
  132. );
  133. html.AppendLine(TABLE_FOOTER);
  134. html.AppendLine(FOOTER);
  135. byte[] responseData = System.Text.Encoding.UTF8.GetBytes(html.ToString());
  136. httpResponse.StatusCode = (int) HttpStatusCode.OK;
  137. //httpResponse.Body.Write(responseData, 0, responseData.Length);
  138. //httpResponse.Body.Flush();
  139. return responseData;
  140. }
  141. #endregion IStreamedRequestHandler implementation
  142. }
  143. }
  144. }