1
0

ObjectHandlers.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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.Reflection;
  29. using System.Net;
  30. using OpenSim.Services.Interfaces;
  31. using GridRegion = OpenSim.Services.Interfaces.GridRegion;
  32. using OpenSim.Framework;
  33. using OpenSim.Framework.Servers.HttpServer;
  34. using OpenMetaverse;
  35. using OpenMetaverse.StructuredData;
  36. using log4net;
  37. namespace OpenSim.Server.Handlers.Simulation
  38. {
  39. public class ObjectSimpleHandler : SimpleStreamHandler
  40. {
  41. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  42. private ISimulationService m_SimulationService;
  43. protected bool m_Proxy = false;
  44. public ObjectSimpleHandler(ISimulationService service) : base("/object")
  45. {
  46. m_SimulationService = service;
  47. }
  48. protected override void ProcessRequest(IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
  49. {
  50. httpResponse.KeepAlive = false;
  51. if (m_SimulationService == null)
  52. {
  53. httpResponse.StatusCode = (int)HttpStatusCode.InternalServerError;
  54. httpResponse.RawBuffer = Utils.falseStrBytes;
  55. return;
  56. }
  57. /*this things are ignored
  58. if (!Utils.GetParams(httpRequest.UriPath, out UUID objectID, out UUID regionID, out string action))
  59. {
  60. m_log.InfoFormat("[OBJECT HANDLER]: Invalid parameters for object message {0}", httpRequest.UriPath);
  61. httpResponse.StatusCode = (int)HttpStatusCode.NotFound;
  62. return;
  63. }
  64. */
  65. switch (httpRequest.HttpMethod)
  66. {
  67. case "POST":
  68. {
  69. OSDMap args = Utils.DeserializeJSONOSMap(httpRequest);
  70. if (args == null)
  71. {
  72. httpResponse.StatusCode = (int)HttpStatusCode.BadRequest;
  73. httpResponse.RawBuffer = Utils.falseStrBytes;
  74. return;
  75. }
  76. DoObjectPost(args, httpResponse);
  77. break;
  78. }
  79. case "DELETE":
  80. default:
  81. {
  82. httpResponse.StatusCode = (int)HttpStatusCode.MethodNotAllowed;
  83. return;
  84. }
  85. }
  86. }
  87. protected void DoObjectPost(OSDMap args, IOSHttpResponse httpResponse)
  88. {
  89. // retrieve the input arguments
  90. int x = 0, y = 0;
  91. UUID uuid = UUID.Zero;
  92. string regionname = string.Empty;
  93. Vector3 newPosition = Vector3.Zero;
  94. if (args.ContainsKey("destination_x") && args["destination_x"] != null)
  95. Int32.TryParse(args["destination_x"].AsString(), out x);
  96. if (args.ContainsKey("destination_y") && args["destination_y"] != null)
  97. Int32.TryParse(args["destination_y"].AsString(), out y);
  98. if (args.ContainsKey("destination_uuid") && args["destination_uuid"] != null)
  99. UUID.TryParse(args["destination_uuid"].AsString(), out uuid);
  100. if (args.ContainsKey("destination_name") && args["destination_name"] != null)
  101. regionname = args["destination_name"].ToString();
  102. if (args.ContainsKey("new_position") && args["new_position"] != null)
  103. Vector3.TryParse(args["new_position"], out newPosition);
  104. GridRegion destination = new GridRegion();
  105. destination.RegionID = uuid;
  106. destination.RegionLocX = x;
  107. destination.RegionLocY = y;
  108. destination.RegionName = regionname;
  109. string sogXmlStr = "", extraStr = "", stateXmlStr = "";
  110. if (args.ContainsKey("sog") && args["sog"] != null)
  111. sogXmlStr = args["sog"].AsString();
  112. if (args.ContainsKey("extra") && args["extra"] != null)
  113. extraStr = args["extra"].AsString();
  114. IScene s = m_SimulationService.GetScene(destination.RegionID);
  115. ISceneObject sog = null;
  116. try
  117. {
  118. //m_log.DebugFormat("[OBJECT HANDLER]: received {0}", sogXmlStr);
  119. sog = s.DeserializeObject(sogXmlStr);
  120. sog.ExtraFromXmlString(extraStr);
  121. }
  122. catch (Exception ex)
  123. {
  124. m_log.InfoFormat("[OBJECT HANDLER]: exception on deserializing scene object {0}", ex.Message);
  125. httpResponse.StatusCode = (int)HttpStatusCode.BadRequest;
  126. return;
  127. }
  128. if (args.ContainsKey("modified"))
  129. sog.HasGroupChanged = args["modified"].AsBoolean();
  130. else
  131. sog.HasGroupChanged = false;
  132. if ((args["state"] != null) && s.AllowScriptCrossings)
  133. {
  134. stateXmlStr = args["state"].AsString();
  135. if (stateXmlStr != "")
  136. {
  137. try
  138. {
  139. sog.SetState(stateXmlStr, s);
  140. }
  141. catch (Exception ex)
  142. {
  143. m_log.InfoFormat("[OBJECT HANDLER]: exception on setting state for scene object {0}", ex.Message);
  144. // ignore and continue
  145. }
  146. }
  147. }
  148. bool result = false;
  149. try
  150. {
  151. // This is the meaning of POST object
  152. result = CreateObject(destination, newPosition, sog);
  153. }
  154. catch (Exception e)
  155. {
  156. m_log.DebugFormat("[OBJECT HANDLER]: Exception in CreateObject: {0}", e.StackTrace);
  157. result = false;
  158. }
  159. httpResponse.StatusCode = (int)HttpStatusCode.OK;
  160. httpResponse.RawBuffer = Util.UTF8.GetBytes(result.ToString());
  161. }
  162. // subclasses can override this
  163. protected virtual bool CreateObject(GridRegion destination, Vector3 newPosition, ISceneObject sog)
  164. {
  165. return m_SimulationService.CreateObject(destination, newPosition, sog, false);
  166. }
  167. }
  168. }