1
0

GridUserServerPostHandler.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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 Nini.Config;
  28. using log4net;
  29. using System;
  30. using System.Reflection;
  31. using System.IO;
  32. using System.Net;
  33. using System.Text;
  34. using System.Text.RegularExpressions;
  35. using System.Xml;
  36. using System.Xml.Serialization;
  37. using System.Collections.Generic;
  38. using OpenSim.Server.Base;
  39. using OpenSim.Services.Interfaces;
  40. using OpenSim.Framework;
  41. using OpenSim.Framework.ServiceAuth;
  42. using OpenSim.Framework.Servers.HttpServer;
  43. using OpenMetaverse;
  44. namespace OpenSim.Server.Handlers.GridUser
  45. {
  46. public class GridUserServerPostHandler : BaseStreamHandler
  47. {
  48. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  49. private IGridUserService m_GridUserService;
  50. public GridUserServerPostHandler(IGridUserService service, IServiceAuth auth) :
  51. base("POST", "/griduser", auth)
  52. {
  53. m_GridUserService = service;
  54. }
  55. protected override byte[] ProcessRequest(string path, Stream requestData,
  56. IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
  57. {
  58. StreamReader sr = new StreamReader(requestData);
  59. string body = sr.ReadToEnd();
  60. sr.Close();
  61. body = body.Trim();
  62. //m_log.DebugFormat("[XXX]: query String: {0}", body);
  63. string method = string.Empty;
  64. try
  65. {
  66. Dictionary<string, object> request =
  67. ServerUtils.ParseQueryString(body);
  68. if (!request.ContainsKey("METHOD"))
  69. return FailureResult();
  70. method = request["METHOD"].ToString();
  71. switch (method)
  72. {
  73. case "loggedin":
  74. return LoggedIn(request);
  75. case "loggedout":
  76. return LoggedOut(request);
  77. case "sethome":
  78. return SetHome(request);
  79. case "setposition":
  80. return SetPosition(request);
  81. case "getgriduserinfo":
  82. return GetGridUserInfo(request);
  83. case "getgriduserinfos":
  84. return GetGridUserInfos(request);
  85. }
  86. m_log.DebugFormat("[GRID USER HANDLER]: unknown method request: {0}", method);
  87. }
  88. catch (Exception e)
  89. {
  90. m_log.DebugFormat("[GRID USER HANDLER]: Exception in method {0}: {1}", method, e);
  91. }
  92. return FailureResult();
  93. }
  94. byte[] LoggedIn(Dictionary<string, object> request)
  95. {
  96. string user = String.Empty;
  97. if (!request.ContainsKey("UserID"))
  98. return FailureResult();
  99. user = request["UserID"].ToString();
  100. GridUserInfo guinfo = m_GridUserService.LoggedIn(user);
  101. Dictionary<string, object> result = new Dictionary<string, object>();
  102. result["result"] = guinfo.ToKeyValuePairs();
  103. string xmlString = ServerUtils.BuildXmlResponse(result);
  104. //m_log.DebugFormat("[GRID USER HANDLER]: resp string: {0}", xmlString);
  105. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  106. }
  107. byte[] LoggedOut(Dictionary<string, object> request)
  108. {
  109. string userID = string.Empty;
  110. UUID regionID = UUID.Zero;
  111. Vector3 position = Vector3.Zero;
  112. Vector3 lookat = Vector3.Zero;
  113. if (!UnpackArgs(request, out userID, out regionID, out position, out lookat))
  114. return FailureResult();
  115. if (m_GridUserService.LoggedOut(userID, UUID.Zero, regionID, position, lookat))
  116. return SuccessResult();
  117. return FailureResult();
  118. }
  119. byte[] SetHome(Dictionary<string, object> request)
  120. {
  121. string user = string.Empty;
  122. UUID region = UUID.Zero;
  123. Vector3 position = new Vector3(128, 128, 70);
  124. Vector3 look = Vector3.Zero;
  125. if (!UnpackArgs(request, out user, out region, out position, out look))
  126. return FailureResult();
  127. if (m_GridUserService.SetHome(user, region, position, look))
  128. return SuccessResult();
  129. return FailureResult();
  130. }
  131. byte[] SetPosition(Dictionary<string, object> request)
  132. {
  133. string user = string.Empty;
  134. UUID region = UUID.Zero;
  135. Vector3 position = new Vector3(128, 128, 70);
  136. Vector3 look = Vector3.Zero;
  137. if (!request.ContainsKey("UserID") || !request.ContainsKey("RegionID"))
  138. return FailureResult();
  139. if (!UnpackArgs(request, out user, out region, out position, out look))
  140. return FailureResult();
  141. if (m_GridUserService.SetLastPosition(user, UUID.Zero, region, position, look))
  142. return SuccessResult();
  143. return FailureResult();
  144. }
  145. byte[] GetGridUserInfo(Dictionary<string, object> request)
  146. {
  147. string user = String.Empty;
  148. if (!request.ContainsKey("UserID"))
  149. return FailureResult();
  150. user = request["UserID"].ToString();
  151. GridUserInfo guinfo = m_GridUserService.GetGridUserInfo(user);
  152. Dictionary<string, object> result = new Dictionary<string, object>();
  153. if (guinfo != null)
  154. result["result"] = guinfo.ToKeyValuePairs();
  155. else
  156. result["result"] = "null";
  157. string xmlString = ServerUtils.BuildXmlResponse(result);
  158. //m_log.DebugFormat("[GRID USER HANDLER]: resp string: {0}", xmlString);
  159. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  160. }
  161. byte[] GetGridUserInfos(Dictionary<string, object> request)
  162. {
  163. string[] userIDs;
  164. if (!request.ContainsKey("AgentIDs"))
  165. {
  166. m_log.DebugFormat("[GRID USER HANDLER]: GetGridUserInfos called without required uuids argument");
  167. return FailureResult();
  168. }
  169. if (!(request["AgentIDs"] is List<string>))
  170. {
  171. m_log.DebugFormat("[GRID USER HANDLER]: GetGridUserInfos input argument was of unexpected type {0}", request["uuids"].GetType().ToString());
  172. return FailureResult();
  173. }
  174. userIDs = ((List<string>)request["AgentIDs"]).ToArray();
  175. GridUserInfo[] pinfos = m_GridUserService.GetGridUserInfo(userIDs);
  176. Dictionary<string, object> result = new Dictionary<string, object>();
  177. if ((pinfos == null) || ((pinfos != null) && (pinfos.Length == 0)))
  178. result["result"] = "null";
  179. else
  180. {
  181. int i = 0;
  182. foreach (GridUserInfo pinfo in pinfos)
  183. {
  184. Dictionary<string, object> rinfoDict = pinfo.ToKeyValuePairs();
  185. result["griduser" + i] = rinfoDict;
  186. i++;
  187. }
  188. }
  189. string xmlString = ServerUtils.BuildXmlResponse(result);
  190. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  191. }
  192. private bool UnpackArgs(Dictionary<string, object> request, out string user, out UUID region, out Vector3 position, out Vector3 lookAt)
  193. {
  194. user = string.Empty;
  195. region = UUID.Zero;
  196. position = new Vector3(128, 128, 70);
  197. lookAt = Vector3.Zero;
  198. if (!request.ContainsKey("UserID") || !request.ContainsKey("RegionID"))
  199. return false;
  200. user = request["UserID"].ToString();
  201. if (!UUID.TryParse(request["RegionID"].ToString(), out region))
  202. return false;
  203. if (request.ContainsKey("Position"))
  204. Vector3.TryParse(request["Position"].ToString(), out position);
  205. if (request.ContainsKey("LookAt"))
  206. Vector3.TryParse(request["LookAt"].ToString(), out lookAt);
  207. return true;
  208. }
  209. private byte[] SuccessResult()
  210. {
  211. XmlDocument doc = new XmlDocument();
  212. XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
  213. "", "");
  214. doc.AppendChild(xmlnode);
  215. XmlElement rootElement = doc.CreateElement("", "ServerResponse",
  216. "");
  217. doc.AppendChild(rootElement);
  218. XmlElement result = doc.CreateElement("", "result", "");
  219. result.AppendChild(doc.CreateTextNode("Success"));
  220. rootElement.AppendChild(result);
  221. return Util.DocToBytes(doc);
  222. }
  223. private byte[] FailureResult()
  224. {
  225. XmlDocument doc = new XmlDocument();
  226. XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
  227. "", "");
  228. doc.AppendChild(xmlnode);
  229. XmlElement rootElement = doc.CreateElement("", "ServerResponse",
  230. "");
  231. doc.AppendChild(rootElement);
  232. XmlElement result = doc.CreateElement("", "result", "");
  233. result.AppendChild(doc.CreateTextNode("Failure"));
  234. rootElement.AppendChild(result);
  235. return Util.DocToBytes(doc);
  236. }
  237. }
  238. }