GridUserServerPostHandler.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. string body;
  59. using(StreamReader sr = new StreamReader(requestData))
  60. body = sr.ReadToEnd();
  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. if (guinfo == null)
  153. return FailureResult();
  154. Dictionary<string, object> result = new Dictionary<string, object>();
  155. if (guinfo != null)
  156. result["result"] = guinfo.ToKeyValuePairs();
  157. else
  158. result["result"] = "null";
  159. string xmlString = ServerUtils.BuildXmlResponse(result);
  160. //m_log.DebugFormat("[GRID USER HANDLER]: resp string: {0}", xmlString);
  161. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  162. }
  163. byte[] GetGridUserInfos(Dictionary<string, object> request)
  164. {
  165. string[] userIDs;
  166. if (!request.ContainsKey("AgentIDs"))
  167. {
  168. m_log.DebugFormat("[GRID USER HANDLER]: GetGridUserInfos called without required uuids argument");
  169. return FailureResult();
  170. }
  171. if (!(request["AgentIDs"] is List<string>))
  172. {
  173. m_log.DebugFormat("[GRID USER HANDLER]: GetGridUserInfos input argument was of unexpected type {0}", request["uuids"].GetType().ToString());
  174. return FailureResult();
  175. }
  176. userIDs = ((List<string>)request["AgentIDs"]).ToArray();
  177. GridUserInfo[] pinfos = m_GridUserService.GetGridUserInfo(userIDs);
  178. Dictionary<string, object> result = new Dictionary<string, object>();
  179. if ((pinfos == null) || ((pinfos != null) && (pinfos.Length == 0)))
  180. result["result"] = "null";
  181. else
  182. {
  183. int i = 0;
  184. foreach (GridUserInfo pinfo in pinfos)
  185. {
  186. if(pinfo == null)
  187. continue;
  188. Dictionary<string, object> rinfoDict = pinfo.ToKeyValuePairs();
  189. result["griduser" + i] = rinfoDict;
  190. i++;
  191. }
  192. }
  193. string xmlString = ServerUtils.BuildXmlResponse(result);
  194. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  195. }
  196. private bool UnpackArgs(Dictionary<string, object> request, out string user, out UUID region, out Vector3 position, out Vector3 lookAt)
  197. {
  198. user = string.Empty;
  199. region = UUID.Zero;
  200. position = new Vector3(128, 128, 70);
  201. lookAt = Vector3.Zero;
  202. if (!request.ContainsKey("UserID") || !request.ContainsKey("RegionID"))
  203. return false;
  204. user = request["UserID"].ToString();
  205. if (!UUID.TryParse(request["RegionID"].ToString(), out region))
  206. return false;
  207. if (request.ContainsKey("Position"))
  208. Vector3.TryParse(request["Position"].ToString(), out position);
  209. if (request.ContainsKey("LookAt"))
  210. Vector3.TryParse(request["LookAt"].ToString(), out lookAt);
  211. return true;
  212. }
  213. private byte[] SuccessResult()
  214. {
  215. XmlDocument doc = new XmlDocument();
  216. XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
  217. "", "");
  218. doc.AppendChild(xmlnode);
  219. XmlElement rootElement = doc.CreateElement("", "ServerResponse",
  220. "");
  221. doc.AppendChild(rootElement);
  222. XmlElement result = doc.CreateElement("", "result", "");
  223. result.AppendChild(doc.CreateTextNode("Success"));
  224. rootElement.AppendChild(result);
  225. return Util.DocToBytes(doc);
  226. }
  227. private byte[] FailureResult()
  228. {
  229. XmlDocument doc = new XmlDocument();
  230. XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
  231. "", "");
  232. doc.AppendChild(xmlnode);
  233. XmlElement rootElement = doc.CreateElement("", "ServerResponse",
  234. "");
  235. doc.AppendChild(rootElement);
  236. XmlElement result = doc.CreateElement("", "result", "");
  237. result.AppendChild(doc.CreateTextNode("Failure"));
  238. rootElement.AppendChild(result);
  239. return Util.DocToBytes(doc);
  240. }
  241. }
  242. }