1
0

ObjectHandlers.cs 10.0 KB

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