GETHandler.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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.Xml.Serialization;
  30. using OpenMetaverse;
  31. using OpenSim.Framework;
  32. using OpenSim.Framework.Servers;
  33. using OpenSim.Framework.Servers.HttpServer;
  34. using OpenSim.Region.Framework.Interfaces;
  35. using OpenSim.Region.Framework.Scenes;
  36. namespace OpenSim.ApplicationPlugins.Rest.Regions
  37. {
  38. public partial class RestRegionPlugin : RestPlugin
  39. {
  40. #region GET methods
  41. public string GetHandler(string request, string path, string param,
  42. OSHttpRequest httpRequest, OSHttpResponse httpResponse)
  43. {
  44. // foreach (string h in httpRequest.Headers.AllKeys)
  45. // foreach (string v in httpRequest.Headers.GetValues(h))
  46. // m_log.DebugFormat("{0} IsGod: {1} -> {2}", MsgID, h, v);
  47. MsgID = RequestID;
  48. m_log.DebugFormat("{0} GET path {1} param {2}", MsgID, path, param);
  49. try
  50. {
  51. // param empty: regions list
  52. if (String.IsNullOrEmpty(param)) return GetHandlerRegions(httpResponse);
  53. // param not empty: specific region
  54. return GetHandlerRegion(httpResponse, param);
  55. }
  56. catch (Exception e)
  57. {
  58. return Failure(httpResponse, OSHttpStatusCode.ServerErrorInternalError, "GET", e);
  59. }
  60. }
  61. public string GetHandlerRegions(OSHttpResponse httpResponse)
  62. {
  63. RestXmlWriter rxw = new RestXmlWriter(new StringWriter());
  64. rxw.WriteStartElement(String.Empty, "regions", String.Empty);
  65. foreach (Scene s in App.SceneManager.Scenes)
  66. {
  67. rxw.WriteStartElement(String.Empty, "uuid", String.Empty);
  68. rxw.WriteString(s.RegionInfo.RegionID.ToString());
  69. rxw.WriteEndElement();
  70. }
  71. rxw.WriteEndElement();
  72. return rxw.ToString();
  73. }
  74. protected string ShortRegionInfo(string key, string value)
  75. {
  76. RestXmlWriter rxw = new RestXmlWriter(new StringWriter());
  77. if (String.IsNullOrEmpty(value) ||
  78. String.IsNullOrEmpty(key)) return null;
  79. rxw.WriteStartElement(String.Empty, "region", String.Empty);
  80. rxw.WriteStartElement(String.Empty, key, String.Empty);
  81. rxw.WriteString(value);
  82. rxw.WriteEndDocument();
  83. return rxw.ToString();
  84. }
  85. public string GetHandlerRegion(OSHttpResponse httpResponse, string param)
  86. {
  87. // be resilient and don't get confused by a terminating '/'
  88. param = param.TrimEnd(new char[]{'/'});
  89. string[] comps = param.Split('/');
  90. UUID regionID = (UUID)comps[0];
  91. m_log.DebugFormat("{0} GET region UUID {1}", MsgID, regionID.ToString());
  92. if (UUID.Zero == regionID) throw new Exception("missing region ID");
  93. Scene scene = null;
  94. App.SceneManager.TryGetScene(regionID, out scene);
  95. if (null == scene) return Failure(httpResponse, OSHttpStatusCode.ClientErrorNotFound,
  96. "GET", "cannot find region {0}", regionID.ToString());
  97. RegionDetails details = new RegionDetails(scene.RegionInfo);
  98. // m_log.DebugFormat("{0} GET comps {1}", MsgID, comps.Length);
  99. // for (int i = 0; i < comps.Length; i++) m_log.DebugFormat("{0} GET comps[{1}] >{2}<", MsgID, i, comps[i]);
  100. if (1 == comps.Length)
  101. {
  102. // complete region details requested
  103. RestXmlWriter rxw = new RestXmlWriter(new StringWriter());
  104. XmlSerializer xs = new XmlSerializer(typeof(RegionDetails));
  105. xs.Serialize(rxw, details, _xmlNs);
  106. return rxw.ToString();
  107. }
  108. if (2 == comps.Length)
  109. {
  110. string resp = ShortRegionInfo(comps[1], details[comps[1]]);
  111. if (null != resp) return resp;
  112. // m_log.DebugFormat("{0} GET comps advanced: >{1}<", MsgID, comps[1]);
  113. // check for {terrain,stats,prims}
  114. switch (comps[1].ToLower())
  115. {
  116. case "terrain":
  117. return RegionTerrain(httpResponse, scene);
  118. case "stats":
  119. return RegionStats(httpResponse, scene);
  120. case "prims":
  121. return RegionPrims(httpResponse, scene, Vector3.Zero, Vector3.Zero);
  122. }
  123. }
  124. if (3 == comps.Length)
  125. {
  126. switch (comps[1].ToLower())
  127. {
  128. case "prims":
  129. string[] subregion = comps[2].Split(',');
  130. if (subregion.Length == 6)
  131. {
  132. Vector3 min, max;
  133. try
  134. {
  135. min = new Vector3((float)Double.Parse(subregion[0], Culture.NumberFormatInfo), (float)Double.Parse(subregion[1], Culture.NumberFormatInfo), (float)Double.Parse(subregion[2], Culture.NumberFormatInfo));
  136. max = new Vector3((float)Double.Parse(subregion[3], Culture.NumberFormatInfo), (float)Double.Parse(subregion[4], Culture.NumberFormatInfo), (float)Double.Parse(subregion[5], Culture.NumberFormatInfo));
  137. }
  138. catch (Exception)
  139. {
  140. return Failure(httpResponse, OSHttpStatusCode.ClientErrorBadRequest,
  141. "GET", "invalid subregion parameter");
  142. }
  143. return RegionPrims(httpResponse, scene, min, max);
  144. }
  145. else
  146. {
  147. return Failure(httpResponse, OSHttpStatusCode.ClientErrorBadRequest,
  148. "GET", "invalid subregion parameter");
  149. }
  150. }
  151. }
  152. return Failure(httpResponse, OSHttpStatusCode.ClientErrorBadRequest,
  153. "GET", "too many parameters {0}", param);
  154. }
  155. #endregion GET methods
  156. protected string RegionTerrain(OSHttpResponse httpResponse, Scene scene)
  157. {
  158. httpResponse.SendChunked = true;
  159. httpResponse.ContentType = "text/xml";
  160. return scene.Heightmap.SaveToXmlString();
  161. //return Failure(httpResponse, OSHttpStatusCode.ServerErrorNotImplemented,
  162. // "GET", "terrain not implemented");
  163. }
  164. protected string RegionStats(OSHttpResponse httpResponse, Scene scene)
  165. {
  166. int users = scene.GetRootAgentCount();
  167. int objects = scene.Entities.Count - users;
  168. RestXmlWriter rxw = new RestXmlWriter(new StringWriter());
  169. rxw.WriteStartElement(String.Empty, "region", String.Empty);
  170. rxw.WriteStartElement(String.Empty, "stats", String.Empty);
  171. rxw.WriteStartElement(String.Empty, "users", String.Empty);
  172. rxw.WriteString(users.ToString());
  173. rxw.WriteEndElement();
  174. rxw.WriteStartElement(String.Empty, "objects", String.Empty);
  175. rxw.WriteString(objects.ToString());
  176. rxw.WriteEndElement();
  177. rxw.WriteEndDocument();
  178. return rxw.ToString();
  179. }
  180. protected string RegionPrims(OSHttpResponse httpResponse, Scene scene, Vector3 min, Vector3 max)
  181. {
  182. httpResponse.SendChunked = true;
  183. httpResponse.ContentType = "text/xml";
  184. IRegionSerialiserModule serialiser = scene.RequestModuleInterface<IRegionSerialiserModule>();
  185. if (serialiser != null)
  186. serialiser.SavePrimsToXml2(scene, new StreamWriter(httpResponse.OutputStream), min, max);
  187. return "";
  188. }
  189. }
  190. }