1
0

NeighbourHandlers.cs 8.3 KB

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