/Aurora/Services/GenericServices/SimulationService/Simulation/ObjectHandlers.cs

https://bitbucket.org/VirtualReality/software-testing · C# · 225 lines · 155 code · 26 blank · 44 comment · 47 complexity · fe9dc2c9ae865a8d6528ed0eca3b1f82 MD5 · raw file

  1. /*
  2. * Copyright (c) Contributors, http://aurora-sim.org/, 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 Aurora-Sim 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 Aurora.Framework;
  28. using Aurora.Framework.ConsoleFramework;
  29. using Aurora.Framework.Modules;
  30. using Aurora.Framework.SceneInfo.Entities;
  31. using Aurora.Framework.Services;
  32. using Aurora.Framework.Utilities;
  33. using Nini.Config;
  34. using OpenMetaverse;
  35. using OpenMetaverse.StructuredData;
  36. using System;
  37. using System.Collections;
  38. using System.Net;
  39. using GridRegion = Aurora.Framework.Services.GridRegion;
  40. namespace Aurora.Services
  41. {
  42. public class ObjectHandler
  43. {
  44. private readonly ISimulationService m_SimulationService;
  45. private readonly bool m_allowForeignIncomingObjects;
  46. public ObjectHandler()
  47. {
  48. }
  49. public ObjectHandler(ISimulationService sim, IConfigSource source)
  50. {
  51. IConfig simulationConfig = source.Configs["Handlers"];
  52. if (simulationConfig != null)
  53. m_allowForeignIncomingObjects = simulationConfig.GetBoolean("AllowIncomingForeignObjects",
  54. m_allowForeignIncomingObjects);
  55. m_SimulationService = sim.InnerService;
  56. }
  57. public Hashtable Handler(Hashtable request)
  58. {
  59. //MainConsole.Instance.Debug("[CONNECTION DEBUGGING]: ObjectHandler Called");
  60. //MainConsole.Instance.Debug("---------------------------");
  61. //MainConsole.Instance.Debug(" >> uri=" + request["uri"]);
  62. //MainConsole.Instance.Debug(" >> content-type=" + request["content-type"]);
  63. //MainConsole.Instance.Debug(" >> http-method=" + request["http-method"]);
  64. //MainConsole.Instance.Debug("---------------------------\n");
  65. Hashtable responsedata = new Hashtable();
  66. responsedata["content_type"] = "text/html";
  67. UUID objectID;
  68. UUID regionID;
  69. string action;
  70. if (!WebUtils.GetParams((string) request["uri"], out objectID, out regionID, out action) ||
  71. m_allowForeignIncomingObjects)
  72. {
  73. //MainConsole.Instance.InfoFormat("[OBJECT HANDLER]: Invalid parameters for object message {0}", request["uri"]);
  74. responsedata["int_response_code"] = 404;
  75. responsedata["str_response_string"] = "false";
  76. return responsedata;
  77. }
  78. // Next, let's parse the verb
  79. string method = (string) request["http-method"];
  80. if (method.Equals("POST"))
  81. {
  82. DoObjectPost(request, responsedata, regionID);
  83. return responsedata;
  84. }
  85. else if (method.Equals("PUT"))
  86. {
  87. DoObjectPut(request, responsedata, regionID);
  88. return responsedata;
  89. }
  90. //else if (method.Equals("DELETE"))
  91. //{
  92. // DoObjectDelete(request, responsedata, agentID, action, regionHandle);
  93. // return responsedata;
  94. //}
  95. else
  96. {
  97. MainConsole.Instance.InfoFormat("[OBJECT HANDLER]: method {0} not supported in object message", method);
  98. responsedata["int_response_code"] = HttpStatusCode.MethodNotAllowed;
  99. responsedata["str_response_string"] = "Mthod not allowed";
  100. return responsedata;
  101. }
  102. }
  103. protected virtual void DoObjectPost(Hashtable request, Hashtable responsedata, UUID regionID)
  104. {
  105. OSDMap args = WebUtils.GetOSDMap((string) request["body"]);
  106. if (args == null)
  107. {
  108. responsedata["int_response_code"] = 400;
  109. responsedata["str_response_string"] = "false";
  110. return;
  111. }
  112. // retrieve the input arguments
  113. int x = 0, y = 0;
  114. UUID uuid = UUID.Zero;
  115. string regionname = string.Empty;
  116. if (args.ContainsKey("destination_x") && args["destination_x"] != null)
  117. Int32.TryParse(args["destination_x"].AsString(), out x);
  118. if (args.ContainsKey("destination_y") && args["destination_y"] != null)
  119. Int32.TryParse(args["destination_y"].AsString(), out y);
  120. if (args.ContainsKey("destination_uuid") && args["destination_uuid"] != null)
  121. UUID.TryParse(args["destination_uuid"].AsString(), out uuid);
  122. if (args.ContainsKey("destination_name") && args["destination_name"] != null)
  123. regionname = args["destination_name"].ToString();
  124. GridRegion destination = new GridRegion
  125. {RegionID = uuid, RegionLocX = x, RegionLocY = y, RegionName = regionname};
  126. string sogXmlStr = "";
  127. if (args.ContainsKey("sog") && args["sog"] != null)
  128. sogXmlStr = args["sog"].AsString();
  129. ISceneEntity sog = null;
  130. try
  131. {
  132. //MainConsole.Instance.DebugFormat("[OBJECT HANDLER]: received {0}", sogXmlStr);
  133. IRegionSerialiserModule mod =
  134. m_SimulationService.Scene.RequestModuleInterface<IRegionSerialiserModule>();
  135. if (mod != null)
  136. sog = mod.DeserializeGroupFromXml2(sogXmlStr, m_SimulationService.Scene);
  137. }
  138. catch (Exception ex)
  139. {
  140. MainConsole.Instance.InfoFormat("[OBJECT HANDLER]: exception on deserializing scene object {0}", ex);
  141. responsedata["int_response_code"] = HttpStatusCode.BadRequest;
  142. responsedata["str_response_string"] = "Bad request";
  143. return;
  144. }
  145. bool result = false;
  146. if (sog == null)
  147. {
  148. MainConsole.Instance.ErrorFormat(
  149. "[OBJECT HANDLER]: error on deserializing scene object as the object was null!");
  150. responsedata["int_response_code"] = HttpStatusCode.OK;
  151. responsedata["str_response_string"] = result.ToString();
  152. }
  153. try
  154. {
  155. // This is the meaning of POST object
  156. result = m_SimulationService.CreateObject(destination, sog);
  157. }
  158. catch (Exception e)
  159. {
  160. MainConsole.Instance.DebugFormat("[OBJECT HANDLER]: Exception in CreateObject: {0}", e.StackTrace);
  161. }
  162. responsedata["int_response_code"] = HttpStatusCode.OK;
  163. responsedata["str_response_string"] = result.ToString();
  164. }
  165. protected virtual void DoObjectPut(Hashtable request, Hashtable responsedata, UUID regionID)
  166. {
  167. OSDMap args = WebUtils.GetOSDMap((string) request["body"]);
  168. if (args == null)
  169. {
  170. responsedata["int_response_code"] = 400;
  171. responsedata["str_response_string"] = "false";
  172. return;
  173. }
  174. // retrieve the input arguments
  175. int x = 0, y = 0;
  176. UUID uuid = UUID.Zero;
  177. string regionname = string.Empty;
  178. if (args.ContainsKey("destination_x") && args["destination_x"] != null)
  179. Int32.TryParse(args["destination_x"].AsString(), out x);
  180. if (args.ContainsKey("destination_y") && args["destination_y"] != null)
  181. Int32.TryParse(args["destination_y"].AsString(), out y);
  182. if (args.ContainsKey("destination_uuid") && args["destination_uuid"] != null)
  183. UUID.TryParse(args["destination_uuid"].AsString(), out uuid);
  184. if (args.ContainsKey("destination_name") && args["destination_name"] != null)
  185. regionname = args["destination_name"].ToString();
  186. GridRegion destination = new GridRegion
  187. {RegionID = uuid, RegionLocX = x, RegionLocY = y, RegionName = regionname};
  188. UUID userID = UUID.Zero, itemID = UUID.Zero;
  189. if (args.ContainsKey("userid") && args["userid"] != null)
  190. userID = args["userid"].AsUUID();
  191. if (args.ContainsKey("itemid") && args["itemid"] != null)
  192. itemID = args["itemid"].AsUUID();
  193. // This is the meaning of PUT object
  194. bool result = m_SimulationService.CreateObject(destination, userID, itemID);
  195. responsedata["int_response_code"] = 200;
  196. responsedata["str_response_string"] = result.ToString();
  197. }
  198. }
  199. }