1
0

NeighbourHandlers.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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.IO;
  29. using System.Reflection;
  30. using System.Net;
  31. using System.Text;
  32. using OpenSim.Server.Base;
  33. using OpenSim.Server.Handlers.Base;
  34. using OpenSim.Services.Interfaces;
  35. using OpenSim.Framework;
  36. using OpenSim.Framework.Servers.HttpServer;
  37. using GridRegion = OpenSim.Services.Interfaces.GridRegion;
  38. using OpenMetaverse;
  39. using OpenMetaverse.StructuredData;
  40. using Nini.Config;
  41. using log4net;
  42. namespace OpenSim.Server.Handlers.Neighbour
  43. {
  44. public class NeighbourGetHandler : BaseStreamHandler
  45. {
  46. // TODO: unused: private ISimulationService m_SimulationService;
  47. // TODO: unused: private IAuthenticationService m_AuthenticationService;
  48. public NeighbourGetHandler(INeighbourService service, IAuthenticationService authentication) :
  49. base("GET", "/region")
  50. {
  51. // TODO: unused: m_SimulationService = service;
  52. // TODO: unused: m_AuthenticationService = authentication;
  53. }
  54. public override byte[] Handle(string path, Stream request,
  55. OSHttpRequest httpRequest, OSHttpResponse httpResponse)
  56. {
  57. // Not implemented yet
  58. Console.WriteLine("--- Get region --- " + path);
  59. httpResponse.StatusCode = (int)HttpStatusCode.NotImplemented;
  60. return new byte[] { };
  61. }
  62. }
  63. public class NeighbourPostHandler : BaseStreamHandler
  64. {
  65. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  66. private INeighbourService m_NeighbourService;
  67. private IAuthenticationService m_AuthenticationService;
  68. // TODO: unused: private bool m_AllowForeignGuests;
  69. public NeighbourPostHandler(INeighbourService service, IAuthenticationService authentication) :
  70. base("POST", "/region")
  71. {
  72. m_NeighbourService = service;
  73. m_AuthenticationService = authentication;
  74. // TODO: unused: m_AllowForeignGuests = foreignGuests;
  75. }
  76. public override byte[] Handle(string path, Stream request,
  77. OSHttpRequest httpRequest, OSHttpResponse httpResponse)
  78. {
  79. byte[] result = new byte[0];
  80. UUID regionID;
  81. string action;
  82. ulong regionHandle;
  83. if (RestHandlerUtils.GetParams(path, out regionID, out regionHandle, out action))
  84. {
  85. m_log.InfoFormat("[RegionPostHandler]: Invalid parameters for neighbour message {0}", path);
  86. httpResponse.StatusCode = (int)HttpStatusCode.BadRequest;
  87. httpResponse.StatusDescription = "Invalid parameters for neighbour message " + path;
  88. return result;
  89. }
  90. if (m_AuthenticationService != null)
  91. {
  92. // Authentication
  93. string authority = string.Empty;
  94. string authToken = string.Empty;
  95. if (!RestHandlerUtils.GetAuthentication(httpRequest, out authority, out authToken))
  96. {
  97. m_log.InfoFormat("[RegionPostHandler]: Authentication failed for neighbour message {0}", path);
  98. httpResponse.StatusCode = (int)HttpStatusCode.Unauthorized;
  99. return result;
  100. }
  101. // TODO: Rethink this
  102. //if (!m_AuthenticationService.VerifyKey(regionID, authToken))
  103. //{
  104. // m_log.InfoFormat("[RegionPostHandler]: Authentication failed for neighbour message {0}", path);
  105. // httpResponse.StatusCode = (int)HttpStatusCode.Forbidden;
  106. // return result;
  107. //}
  108. m_log.DebugFormat("[RegionPostHandler]: Authentication succeeded for {0}", regionID);
  109. }
  110. OSDMap args = Util.GetOSDMap(request, (int)httpRequest.ContentLength);
  111. if (args == null)
  112. {
  113. httpResponse.StatusCode = (int)HttpStatusCode.BadRequest;
  114. httpResponse.StatusDescription = "Unable to retrieve data";
  115. m_log.DebugFormat("[RegionPostHandler]: Unable to retrieve data for post {0}", path);
  116. return result;
  117. }
  118. // retrieve the regionhandle
  119. ulong regionhandle = 0;
  120. if (args["destination_handle"] != null)
  121. UInt64.TryParse(args["destination_handle"].AsString(), out regionhandle);
  122. RegionInfo aRegion = new RegionInfo();
  123. try
  124. {
  125. aRegion.UnpackRegionInfoData(args);
  126. }
  127. catch (Exception ex)
  128. {
  129. m_log.InfoFormat("[RegionPostHandler]: exception on unpacking region info {0}", ex.Message);
  130. httpResponse.StatusCode = (int)HttpStatusCode.BadRequest;
  131. httpResponse.StatusDescription = "Problems with data deserialization";
  132. return result;
  133. }
  134. // Finally!
  135. GridRegion thisRegion = m_NeighbourService.HelloNeighbour(regionhandle, aRegion);
  136. OSDMap resp = new OSDMap(1);
  137. if (thisRegion != null)
  138. resp["success"] = OSD.FromBoolean(true);
  139. else
  140. resp["success"] = OSD.FromBoolean(false);
  141. httpResponse.StatusCode = (int)HttpStatusCode.OK;
  142. return Util.UTF8.GetBytes(OSDParser.SerializeJsonString(resp));
  143. }
  144. }
  145. public class NeighbourPutHandler : BaseStreamHandler
  146. {
  147. // TODO: unused: private ISimulationService m_SimulationService;
  148. // TODO: unused: private IAuthenticationService m_AuthenticationService;
  149. public NeighbourPutHandler(INeighbourService service, IAuthenticationService authentication) :
  150. base("PUT", "/region")
  151. {
  152. // TODO: unused: m_SimulationService = service;
  153. // TODO: unused: m_AuthenticationService = authentication;
  154. }
  155. public override byte[] Handle(string path, Stream request,
  156. OSHttpRequest httpRequest, OSHttpResponse httpResponse)
  157. {
  158. // Not implemented yet
  159. httpResponse.StatusCode = (int)HttpStatusCode.NotImplemented;
  160. return new byte[] { };
  161. }
  162. }
  163. public class NeighbourDeleteHandler : BaseStreamHandler
  164. {
  165. // TODO: unused: private ISimulationService m_SimulationService;
  166. // TODO: unused: private IAuthenticationService m_AuthenticationService;
  167. public NeighbourDeleteHandler(INeighbourService service, IAuthenticationService authentication) :
  168. base("DELETE", "/region")
  169. {
  170. // TODO: unused: m_SimulationService = service;
  171. // TODO: unused: m_AuthenticationService = authentication;
  172. }
  173. public override byte[] Handle(string path, Stream request,
  174. OSHttpRequest httpRequest, OSHttpResponse httpResponse)
  175. {
  176. // Not implemented yet
  177. httpResponse.StatusCode = (int)HttpStatusCode.NotImplemented;
  178. return new byte[] { };
  179. }
  180. }
  181. }