GridUserServerPostHandler.cs 11 KB

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