GETHandler.cs 9.2 KB

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