ObjectHandlers.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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("PUT"))
  84. {
  85. DoObjectPut(request, responsedata, regionID);
  86. return responsedata;
  87. }
  88. //else if (method.Equals("DELETE"))
  89. //{
  90. // DoObjectDelete(request, responsedata, agentID, action, regionHandle);
  91. // return responsedata;
  92. //}
  93. else
  94. {
  95. m_log.InfoFormat("[OBJECT HANDLER]: method {0} not supported in object message", method);
  96. responsedata["int_response_code"] = HttpStatusCode.MethodNotAllowed;
  97. responsedata["str_response_string"] = "Method not allowed";
  98. return responsedata;
  99. }
  100. }
  101. catch (Exception e)
  102. {
  103. m_log.WarnFormat("[OBJECT HANDLER]: Caught exception {0}", e.StackTrace);
  104. responsedata["int_response_code"] = HttpStatusCode.InternalServerError;
  105. responsedata["str_response_string"] = "Internal server error";
  106. return responsedata;
  107. }
  108. }
  109. protected void DoObjectPost(Hashtable request, Hashtable responsedata, UUID regionID)
  110. {
  111. OSDMap args = Utils.GetOSDMap((string)request["body"]);
  112. if (args == null)
  113. {
  114. responsedata["int_response_code"] = 400;
  115. responsedata["str_response_string"] = "false";
  116. return;
  117. }
  118. // retrieve the input arguments
  119. int x = 0, y = 0;
  120. UUID uuid = UUID.Zero;
  121. string regionname = string.Empty;
  122. if (args.ContainsKey("destination_x") && args["destination_x"] != null)
  123. Int32.TryParse(args["destination_x"].AsString(), out x);
  124. if (args.ContainsKey("destination_y") && args["destination_y"] != null)
  125. Int32.TryParse(args["destination_y"].AsString(), out y);
  126. if (args.ContainsKey("destination_uuid") && args["destination_uuid"] != null)
  127. UUID.TryParse(args["destination_uuid"].AsString(), out uuid);
  128. if (args.ContainsKey("destination_name") && args["destination_name"] != null)
  129. regionname = args["destination_name"].ToString();
  130. GridRegion destination = new GridRegion();
  131. destination.RegionID = uuid;
  132. destination.RegionLocX = x;
  133. destination.RegionLocY = y;
  134. destination.RegionName = regionname;
  135. string sogXmlStr = "", extraStr = "", stateXmlStr = "";
  136. if (args.ContainsKey("sog") && args["sog"] != null)
  137. sogXmlStr = args["sog"].AsString();
  138. if (args.ContainsKey("extra") && args["extra"] != null)
  139. extraStr = args["extra"].AsString();
  140. IScene s = m_SimulationService.GetScene(destination.RegionHandle);
  141. ISceneObject sog = null;
  142. try
  143. {
  144. //m_log.DebugFormat("[OBJECT HANDLER]: received {0}", sogXmlStr);
  145. sog = s.DeserializeObject(sogXmlStr);
  146. sog.ExtraFromXmlString(extraStr);
  147. }
  148. catch (Exception ex)
  149. {
  150. m_log.InfoFormat("[OBJECT HANDLER]: exception on deserializing scene object {0}", ex.Message);
  151. responsedata["int_response_code"] = HttpStatusCode.BadRequest;
  152. responsedata["str_response_string"] = "Bad request";
  153. return;
  154. }
  155. if (args.ContainsKey("modified"))
  156. sog.HasGroupChanged = args["modified"].AsBoolean();
  157. else
  158. sog.HasGroupChanged = false;
  159. if ((args["state"] != null) && s.AllowScriptCrossings)
  160. {
  161. stateXmlStr = args["state"].AsString();
  162. if (stateXmlStr != "")
  163. {
  164. try
  165. {
  166. sog.SetState(stateXmlStr, s);
  167. }
  168. catch (Exception ex)
  169. {
  170. m_log.InfoFormat("[OBJECT HANDLER]: exception on setting state for scene object {0}", ex.Message);
  171. // ignore and continue
  172. }
  173. }
  174. }
  175. bool result = false;
  176. try
  177. {
  178. // This is the meaning of POST object
  179. result = CreateObject(destination, sog);
  180. }
  181. catch (Exception e)
  182. {
  183. m_log.DebugFormat("[OBJECT HANDLER]: Exception in CreateObject: {0}", e.StackTrace);
  184. }
  185. responsedata["int_response_code"] = HttpStatusCode.OK;
  186. responsedata["str_response_string"] = result.ToString();
  187. }
  188. // subclasses can override this
  189. protected virtual bool CreateObject(GridRegion destination, ISceneObject sog)
  190. {
  191. return m_SimulationService.CreateObject(destination, sog, false);
  192. }
  193. protected virtual void DoObjectPut(Hashtable request, Hashtable responsedata, UUID regionID)
  194. {
  195. OSDMap args = Utils.GetOSDMap((string)request["body"]);
  196. if (args == null)
  197. {
  198. responsedata["int_response_code"] = 400;
  199. responsedata["str_response_string"] = "false";
  200. return;
  201. }
  202. // retrieve the input arguments
  203. int x = 0, y = 0;
  204. UUID uuid = UUID.Zero;
  205. string regionname = string.Empty;
  206. if (args.ContainsKey("destination_x") && args["destination_x"] != null)
  207. Int32.TryParse(args["destination_x"].AsString(), out x);
  208. if (args.ContainsKey("destination_y") && args["destination_y"] != null)
  209. Int32.TryParse(args["destination_y"].AsString(), out y);
  210. if (args.ContainsKey("destination_uuid") && args["destination_uuid"] != null)
  211. UUID.TryParse(args["destination_uuid"].AsString(), out uuid);
  212. if (args.ContainsKey("destination_name") && args["destination_name"] != null)
  213. regionname = args["destination_name"].ToString();
  214. GridRegion destination = new GridRegion();
  215. destination.RegionID = uuid;
  216. destination.RegionLocX = x;
  217. destination.RegionLocY = y;
  218. destination.RegionName = regionname;
  219. UUID userID = UUID.Zero, itemID = UUID.Zero;
  220. if (args.ContainsKey("userid") && args["userid"] != null)
  221. userID = args["userid"].AsUUID();
  222. if (args.ContainsKey("itemid") && args["itemid"] != null)
  223. itemID = args["itemid"].AsUUID();
  224. // This is the meaning of PUT object
  225. bool result = m_SimulationService.CreateObject(destination, userID, itemID);
  226. responsedata["int_response_code"] = 200;
  227. responsedata["str_response_string"] = result.ToString();
  228. }
  229. }
  230. }