1
0

ObjectHandlers.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. Hashtable responsedata = new Hashtable();
  63. responsedata["content_type"] = "text/html";
  64. UUID objectID;
  65. UUID regionID;
  66. string action;
  67. if (!Utils.GetParams((string)request["uri"], out objectID, out regionID, out action))
  68. {
  69. m_log.InfoFormat("[OBJECT HANDLER]: Invalid parameters for object message {0}", request["uri"]);
  70. responsedata["int_response_code"] = 404;
  71. responsedata["str_response_string"] = "false";
  72. return responsedata;
  73. }
  74. try
  75. {
  76. // Next, let's parse the verb
  77. string method = (string)request["http-method"];
  78. if (method.Equals("POST"))
  79. {
  80. DoObjectPost(request, responsedata, regionID);
  81. return responsedata;
  82. }
  83. //else if (method.Equals("DELETE"))
  84. //{
  85. // DoObjectDelete(request, responsedata, agentID, action, regionHandle);
  86. // return responsedata;
  87. //}
  88. else
  89. {
  90. m_log.InfoFormat("[OBJECT HANDLER]: method {0} not supported in object message", method);
  91. responsedata["int_response_code"] = HttpStatusCode.MethodNotAllowed;
  92. responsedata["str_response_string"] = "Method not allowed";
  93. return responsedata;
  94. }
  95. }
  96. catch (Exception e)
  97. {
  98. m_log.WarnFormat("[OBJECT HANDLER]: Caught exception {0}", e.StackTrace);
  99. responsedata["int_response_code"] = HttpStatusCode.InternalServerError;
  100. responsedata["str_response_string"] = "Internal server error";
  101. return responsedata;
  102. }
  103. }
  104. protected void DoObjectPost(Hashtable request, Hashtable responsedata, UUID regionID)
  105. {
  106. OSDMap args = Utils.GetOSDMap((string)request["body"]);
  107. if (args == null)
  108. {
  109. responsedata["int_response_code"] = 400;
  110. responsedata["str_response_string"] = "false";
  111. return;
  112. }
  113. // retrieve the input arguments
  114. int x = 0, y = 0;
  115. UUID uuid = UUID.Zero;
  116. string regionname = string.Empty;
  117. Vector3 newPosition = Vector3.Zero;
  118. if (args.ContainsKey("destination_x") && args["destination_x"] != null)
  119. Int32.TryParse(args["destination_x"].AsString(), out x);
  120. if (args.ContainsKey("destination_y") && args["destination_y"] != null)
  121. Int32.TryParse(args["destination_y"].AsString(), out y);
  122. if (args.ContainsKey("destination_uuid") && args["destination_uuid"] != null)
  123. UUID.TryParse(args["destination_uuid"].AsString(), out uuid);
  124. if (args.ContainsKey("destination_name") && args["destination_name"] != null)
  125. regionname = args["destination_name"].ToString();
  126. if (args.ContainsKey("new_position") && args["new_position"] != null)
  127. Vector3.TryParse(args["new_position"], out newPosition);
  128. GridRegion destination = new GridRegion();
  129. destination.RegionID = uuid;
  130. destination.RegionLocX = x;
  131. destination.RegionLocY = y;
  132. destination.RegionName = regionname;
  133. string sogXmlStr = "", extraStr = "", stateXmlStr = "";
  134. if (args.ContainsKey("sog") && args["sog"] != null)
  135. sogXmlStr = args["sog"].AsString();
  136. if (args.ContainsKey("extra") && args["extra"] != null)
  137. extraStr = args["extra"].AsString();
  138. IScene s = m_SimulationService.GetScene(destination.RegionID);
  139. ISceneObject sog = null;
  140. try
  141. {
  142. //m_log.DebugFormat("[OBJECT HANDLER]: received {0}", sogXmlStr);
  143. sog = s.DeserializeObject(sogXmlStr);
  144. sog.ExtraFromXmlString(extraStr);
  145. }
  146. catch (Exception ex)
  147. {
  148. m_log.InfoFormat("[OBJECT HANDLER]: exception on deserializing scene object {0}", ex.Message);
  149. responsedata["int_response_code"] = HttpStatusCode.BadRequest;
  150. responsedata["str_response_string"] = "Bad request";
  151. return;
  152. }
  153. if (args.ContainsKey("modified"))
  154. sog.HasGroupChanged = args["modified"].AsBoolean();
  155. else
  156. sog.HasGroupChanged = false;
  157. if ((args["state"] != null) && s.AllowScriptCrossings)
  158. {
  159. stateXmlStr = args["state"].AsString();
  160. if (stateXmlStr != "")
  161. {
  162. try
  163. {
  164. sog.SetState(stateXmlStr, s);
  165. }
  166. catch (Exception ex)
  167. {
  168. m_log.InfoFormat("[OBJECT HANDLER]: exception on setting state for scene object {0}", ex.Message);
  169. // ignore and continue
  170. }
  171. }
  172. }
  173. bool result = false;
  174. try
  175. {
  176. // This is the meaning of POST object
  177. result = CreateObject(destination, newPosition, sog);
  178. }
  179. catch (Exception e)
  180. {
  181. m_log.DebugFormat("[OBJECT HANDLER]: Exception in CreateObject: {0}", e.StackTrace);
  182. }
  183. responsedata["int_response_code"] = HttpStatusCode.OK;
  184. responsedata["str_response_string"] = result.ToString();
  185. }
  186. // subclasses can override this
  187. protected virtual bool CreateObject(GridRegion destination, Vector3 newPosition, ISceneObject sog)
  188. {
  189. return m_SimulationService.CreateObject(destination, newPosition, sog, false);
  190. }
  191. }
  192. }