ObjectHandlers.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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.IO;
  30. using System.Reflection;
  31. using System.Net;
  32. using System.Text;
  33. using OpenSim.Server.Base;
  34. using OpenSim.Server.Handlers.Base;
  35. using OpenSim.Services.Interfaces;
  36. using GridRegion = OpenSim.Services.Interfaces.GridRegion;
  37. using OpenSim.Framework;
  38. using OpenSim.Framework.Servers.HttpServer;
  39. using OpenMetaverse;
  40. using OpenMetaverse.StructuredData;
  41. using Nini.Config;
  42. using log4net;
  43. namespace OpenSim.Server.Handlers.Simulation
  44. {
  45. public class ObjectHandler
  46. {
  47. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  48. private ISimulationService m_SimulationService;
  49. public ObjectHandler() { }
  50. public ObjectHandler(ISimulationService sim)
  51. {
  52. m_SimulationService = sim;
  53. }
  54. public Hashtable Handler(Hashtable request)
  55. {
  56. //m_log.Debug("[CONNECTION DEBUGGING]: ObjectHandler Called");
  57. //m_log.Debug("---------------------------");
  58. //m_log.Debug(" >> uri=" + request["uri"]);
  59. //m_log.Debug(" >> content-type=" + request["content-type"]);
  60. //m_log.Debug(" >> http-method=" + request["http-method"]);
  61. //m_log.Debug("---------------------------\n");
  62. Culture.SetCurrentCulture();
  63. Hashtable responsedata = new Hashtable();
  64. responsedata["content_type"] = "text/html";
  65. UUID objectID;
  66. UUID regionID;
  67. string action;
  68. if (!Utils.GetParams((string)request["uri"], out objectID, out regionID, out action))
  69. {
  70. m_log.InfoFormat("[OBJECT HANDLER]: Invalid parameters for object message {0}", request["uri"]);
  71. responsedata["int_response_code"] = 404;
  72. responsedata["str_response_string"] = "false";
  73. return responsedata;
  74. }
  75. try
  76. {
  77. // Next, let's parse the verb
  78. string method = (string)request["http-method"];
  79. if (method.Equals("POST"))
  80. {
  81. DoObjectPost(request, responsedata, regionID);
  82. return responsedata;
  83. }
  84. //else if (method.Equals("DELETE"))
  85. //{
  86. // DoObjectDelete(request, responsedata, agentID, action, regionHandle);
  87. // return responsedata;
  88. //}
  89. else
  90. {
  91. m_log.InfoFormat("[OBJECT HANDLER]: method {0} not supported in object message", method);
  92. responsedata["int_response_code"] = HttpStatusCode.MethodNotAllowed;
  93. responsedata["str_response_string"] = "Method not allowed";
  94. return responsedata;
  95. }
  96. }
  97. catch (Exception e)
  98. {
  99. m_log.WarnFormat("[OBJECT HANDLER]: Caught exception {0}", e.StackTrace);
  100. responsedata["int_response_code"] = HttpStatusCode.InternalServerError;
  101. responsedata["str_response_string"] = "Internal server error";
  102. return responsedata;
  103. }
  104. }
  105. protected void DoObjectPost(Hashtable request, Hashtable responsedata, UUID regionID)
  106. {
  107. OSDMap args = Utils.GetOSDMap((string)request["body"]);
  108. if (args == null)
  109. {
  110. responsedata["int_response_code"] = 400;
  111. responsedata["str_response_string"] = "false";
  112. return;
  113. }
  114. // retrieve the input arguments
  115. int x = 0, y = 0;
  116. UUID uuid = UUID.Zero;
  117. string regionname = string.Empty;
  118. Vector3 newPosition = Vector3.Zero;
  119. if (args.ContainsKey("destination_x") && args["destination_x"] != null)
  120. Int32.TryParse(args["destination_x"].AsString(), out x);
  121. if (args.ContainsKey("destination_y") && args["destination_y"] != null)
  122. Int32.TryParse(args["destination_y"].AsString(), out y);
  123. if (args.ContainsKey("destination_uuid") && args["destination_uuid"] != null)
  124. UUID.TryParse(args["destination_uuid"].AsString(), out uuid);
  125. if (args.ContainsKey("destination_name") && args["destination_name"] != null)
  126. regionname = args["destination_name"].ToString();
  127. if (args.ContainsKey("new_position") && args["new_position"] != null)
  128. Vector3.TryParse(args["new_position"], out newPosition);
  129. GridRegion destination = new GridRegion();
  130. destination.RegionID = uuid;
  131. destination.RegionLocX = x;
  132. destination.RegionLocY = y;
  133. destination.RegionName = regionname;
  134. string sogXmlStr = "", extraStr = "", stateXmlStr = "";
  135. if (args.ContainsKey("sog") && args["sog"] != null)
  136. sogXmlStr = args["sog"].AsString();
  137. if (args.ContainsKey("extra") && args["extra"] != null)
  138. extraStr = args["extra"].AsString();
  139. IScene s = m_SimulationService.GetScene(destination.RegionID);
  140. ISceneObject sog = null;
  141. try
  142. {
  143. //m_log.DebugFormat("[OBJECT HANDLER]: received {0}", sogXmlStr);
  144. sog = s.DeserializeObject(sogXmlStr);
  145. sog.ExtraFromXmlString(extraStr);
  146. }
  147. catch (Exception ex)
  148. {
  149. m_log.InfoFormat("[OBJECT HANDLER]: exception on deserializing scene object {0}", ex.Message);
  150. responsedata["int_response_code"] = HttpStatusCode.BadRequest;
  151. responsedata["str_response_string"] = "Bad request";
  152. return;
  153. }
  154. if (args.ContainsKey("modified"))
  155. sog.HasGroupChanged = args["modified"].AsBoolean();
  156. else
  157. sog.HasGroupChanged = false;
  158. if ((args["state"] != null) && s.AllowScriptCrossings)
  159. {
  160. stateXmlStr = args["state"].AsString();
  161. if (stateXmlStr != "")
  162. {
  163. try
  164. {
  165. sog.SetState(stateXmlStr, s);
  166. }
  167. catch (Exception ex)
  168. {
  169. m_log.InfoFormat("[OBJECT HANDLER]: exception on setting state for scene object {0}", ex.Message);
  170. // ignore and continue
  171. }
  172. }
  173. }
  174. bool result = false;
  175. try
  176. {
  177. // This is the meaning of POST object
  178. result = CreateObject(destination, newPosition, sog);
  179. }
  180. catch (Exception e)
  181. {
  182. m_log.DebugFormat("[OBJECT HANDLER]: Exception in CreateObject: {0}", e.StackTrace);
  183. }
  184. responsedata["int_response_code"] = HttpStatusCode.OK;
  185. responsedata["str_response_string"] = result.ToString();
  186. }
  187. // subclasses can override this
  188. protected virtual bool CreateObject(GridRegion destination, Vector3 newPosition, ISceneObject sog)
  189. {
  190. return m_SimulationService.CreateObject(destination, newPosition, sog, false);
  191. }
  192. }
  193. }