GETHandler.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 OpenSim 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. XmlWriter.WriteStartElement(String.Empty, "regions", String.Empty);
  63. foreach (Scene s in App.SceneManager.Scenes)
  64. {
  65. XmlWriter.WriteStartElement(String.Empty, "uuid", String.Empty);
  66. XmlWriter.WriteString(s.RegionInfo.RegionID.ToString());
  67. XmlWriter.WriteEndElement();
  68. }
  69. XmlWriter.WriteEndElement();
  70. return XmlWriterResult;
  71. }
  72. protected string ShortRegionInfo(string key, string value)
  73. {
  74. if (String.IsNullOrEmpty(value) ||
  75. String.IsNullOrEmpty(key)) return null;
  76. XmlWriter.WriteStartElement(String.Empty, "region", String.Empty);
  77. XmlWriter.WriteStartElement(String.Empty, key, String.Empty);
  78. XmlWriter.WriteString(value);
  79. XmlWriter.WriteEndDocument();
  80. return XmlWriterResult;
  81. }
  82. public string GetHandlerRegion(OSHttpResponse httpResponse, string param)
  83. {
  84. // be resilient and don't get confused by a terminating '/'
  85. param = param.TrimEnd(new char[]{'/'});
  86. string[] comps = param.Split('/');
  87. UUID regionID = (UUID)comps[0];
  88. m_log.DebugFormat("{0} GET region UUID {1}", MsgID, regionID.ToString());
  89. if (UUID.Zero == regionID) throw new Exception("missing region ID");
  90. Scene scene = null;
  91. App.SceneManager.TryGetScene(regionID, out scene);
  92. if (null == scene) return Failure(httpResponse, OSHttpStatusCode.ClientErrorNotFound,
  93. "GET", "cannot find region {0}", regionID.ToString());
  94. RegionDetails details = new RegionDetails(scene.RegionInfo);
  95. // m_log.DebugFormat("{0} GET comps {1}", MsgID, comps.Length);
  96. // for (int i = 0; i < comps.Length; i++) m_log.DebugFormat("{0} GET comps[{1}] >{2}<", MsgID, i, comps[i]);
  97. if (1 == comps.Length)
  98. {
  99. // complete region details requested
  100. XmlSerializer xs = new XmlSerializer(typeof(RegionDetails));
  101. xs.Serialize(XmlWriter, details, _xmlNs);
  102. return XmlWriterResult;
  103. }
  104. if (2 == comps.Length)
  105. {
  106. string resp = ShortRegionInfo(comps[1], details[comps[1]]);
  107. if (null != resp) return resp;
  108. // m_log.DebugFormat("{0} GET comps advanced: >{1}<", MsgID, comps[1]);
  109. // check for {terrain,stats,prims}
  110. switch (comps[1].ToLower())
  111. {
  112. case "terrain":
  113. return RegionTerrain(httpResponse, scene);
  114. case "stats":
  115. return RegionStats(httpResponse, scene);
  116. case "prims":
  117. return RegionPrims(httpResponse, scene, Vector3.Zero, Vector3.Zero);
  118. }
  119. }
  120. if (3 == comps.Length)
  121. {
  122. switch (comps[1].ToLower())
  123. {
  124. case "prims":
  125. string[] subregion = comps[2].Split(',');
  126. if (subregion.Length == 6)
  127. {
  128. Vector3 min, max;
  129. try
  130. {
  131. min = new Vector3((float)Double.Parse(subregion[0]), (float)Double.Parse(subregion[1]), (float)Double.Parse(subregion[2]));
  132. max = new Vector3((float)Double.Parse(subregion[3]), (float)Double.Parse(subregion[4]), (float)Double.Parse(subregion[5]));
  133. }
  134. catch (Exception)
  135. {
  136. return Failure(httpResponse, OSHttpStatusCode.ClientErrorBadRequest,
  137. "GET", "invalid subregion parameter");
  138. }
  139. return RegionPrims(httpResponse, scene, min, max);
  140. }
  141. else
  142. {
  143. return Failure(httpResponse, OSHttpStatusCode.ClientErrorBadRequest,
  144. "GET", "invalid subregion parameter");
  145. }
  146. }
  147. }
  148. return Failure(httpResponse, OSHttpStatusCode.ClientErrorBadRequest,
  149. "GET", "too many parameters {0}", param);
  150. }
  151. #endregion GET methods
  152. protected string RegionTerrain(OSHttpResponse httpResponse, Scene scene)
  153. {
  154. return Failure(httpResponse, OSHttpStatusCode.ServerErrorNotImplemented,
  155. "GET", "terrain not implemented");
  156. }
  157. protected string RegionStats(OSHttpResponse httpResponse, Scene scene)
  158. {
  159. int users = scene.GetAvatars().Count;
  160. int objects = scene.Entities.Count - users;
  161. XmlWriter.WriteStartElement(String.Empty, "region", String.Empty);
  162. XmlWriter.WriteStartElement(String.Empty, "stats", String.Empty);
  163. XmlWriter.WriteStartElement(String.Empty, "users", String.Empty);
  164. XmlWriter.WriteString(users.ToString());
  165. XmlWriter.WriteEndElement();
  166. XmlWriter.WriteStartElement(String.Empty, "objects", String.Empty);
  167. XmlWriter.WriteString(objects.ToString());
  168. XmlWriter.WriteEndElement();
  169. XmlWriter.WriteEndDocument();
  170. return XmlWriterResult;
  171. }
  172. protected string RegionPrims(OSHttpResponse httpResponse, Scene scene, Vector3 min, Vector3 max)
  173. {
  174. httpResponse.SendChunked = true;
  175. httpResponse.ContentType = "text/xml";
  176. IRegionSerialiserModule serialiser = scene.RequestModuleInterface<IRegionSerialiserModule>();
  177. if (serialiser != null)
  178. serialiser.SavePrimsToXml2(scene, new StreamWriter(httpResponse.OutputStream), min, max);
  179. return "";
  180. }
  181. }
  182. }