UserAccountServerPostHandler.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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.UserAccounts
  44. {
  45. public class UserAccountServerPostHandler : BaseStreamHandler
  46. {
  47. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  48. private IUserAccountService m_UserAccountService;
  49. public UserAccountServerPostHandler(IUserAccountService service) :
  50. base("POST", "/accounts")
  51. {
  52. m_UserAccountService = service;
  53. }
  54. public override byte[] Handle(string path, Stream requestData,
  55. OSHttpRequest httpRequest, OSHttpResponse httpResponse)
  56. {
  57. StreamReader sr = new StreamReader(requestData);
  58. string body = sr.ReadToEnd();
  59. sr.Close();
  60. body = body.Trim();
  61. // We need to check the authorization header
  62. //httpRequest.Headers["authorization"] ...
  63. //m_log.DebugFormat("[XXX]: query String: {0}", body);
  64. string method = string.Empty;
  65. try
  66. {
  67. Dictionary<string, object> request =
  68. ServerUtils.ParseQueryString(body);
  69. if (!request.ContainsKey("METHOD"))
  70. return FailureResult();
  71. method = request["METHOD"].ToString();
  72. switch (method)
  73. {
  74. case "getaccount":
  75. return GetAccount(request);
  76. case "getaccounts":
  77. return GetAccounts(request);
  78. case "setaccount":
  79. return StoreAccount(request);
  80. }
  81. m_log.DebugFormat("[USER SERVICE HANDLER]: unknown method request: {0}", method);
  82. }
  83. catch (Exception e)
  84. {
  85. m_log.DebugFormat("[USER SERVICE HANDLER]: Exception in method {0}: {1}", method, e);
  86. }
  87. return FailureResult();
  88. }
  89. byte[] GetAccount(Dictionary<string, object> request)
  90. {
  91. UserAccount account = null;
  92. UUID scopeID = UUID.Zero;
  93. Dictionary<string, object> result = new Dictionary<string, object>();
  94. if (!request.ContainsKey("ScopeID"))
  95. {
  96. result["result"] = "null";
  97. return ResultToBytes(result);
  98. }
  99. if (!UUID.TryParse(request["ScopeID"].ToString(), out scopeID))
  100. {
  101. result["result"] = "null";
  102. return ResultToBytes(result);
  103. }
  104. if (request.ContainsKey("UserID") && request["UserID"] != null)
  105. {
  106. UUID userID;
  107. if (UUID.TryParse(request["UserID"].ToString(), out userID))
  108. account = m_UserAccountService.GetUserAccount(scopeID, userID);
  109. }
  110. else if (request.ContainsKey("Email") && request["Email"] != null)
  111. account = m_UserAccountService.GetUserAccount(scopeID, request["Email"].ToString());
  112. else if (request.ContainsKey("FirstName") && request.ContainsKey("LastName") &&
  113. request["FirstName"] != null && request["LastName"] != null)
  114. account = m_UserAccountService.GetUserAccount(scopeID, request["FirstName"].ToString(), request["LastName"].ToString());
  115. if (account == null)
  116. result["result"] = "null";
  117. else
  118. {
  119. result["result"] = account.ToKeyValuePairs();
  120. }
  121. return ResultToBytes(result);
  122. }
  123. byte[] GetAccounts(Dictionary<string, object> request)
  124. {
  125. if (!request.ContainsKey("ScopeID") || !request.ContainsKey("query"))
  126. return FailureResult();
  127. UUID scopeID = UUID.Zero;
  128. if (!UUID.TryParse(request["ScopeID"].ToString(), out scopeID))
  129. return FailureResult();
  130. string query = request["query"].ToString();
  131. List<UserAccount> accounts = m_UserAccountService.GetUserAccounts(scopeID, query);
  132. Dictionary<string, object> result = new Dictionary<string, object>();
  133. if ((accounts == null) || ((accounts != null) && (accounts.Count == 0)))
  134. result["result"] = "null";
  135. else
  136. {
  137. int i = 0;
  138. foreach (UserAccount acc in accounts)
  139. {
  140. Dictionary<string, object> rinfoDict = acc.ToKeyValuePairs();
  141. result["account" + i] = rinfoDict;
  142. i++;
  143. }
  144. }
  145. string xmlString = ServerUtils.BuildXmlResponse(result);
  146. //m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString);
  147. UTF8Encoding encoding = new UTF8Encoding();
  148. return encoding.GetBytes(xmlString);
  149. }
  150. byte[] StoreAccount(Dictionary<string, object> request)
  151. {
  152. // No can do. No changing user accounts from remote sims
  153. return FailureResult();
  154. }
  155. private byte[] SuccessResult()
  156. {
  157. XmlDocument doc = new XmlDocument();
  158. XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
  159. "", "");
  160. doc.AppendChild(xmlnode);
  161. XmlElement rootElement = doc.CreateElement("", "ServerResponse",
  162. "");
  163. doc.AppendChild(rootElement);
  164. XmlElement result = doc.CreateElement("", "result", "");
  165. result.AppendChild(doc.CreateTextNode("Success"));
  166. rootElement.AppendChild(result);
  167. return DocToBytes(doc);
  168. }
  169. private byte[] FailureResult()
  170. {
  171. XmlDocument doc = new XmlDocument();
  172. XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
  173. "", "");
  174. doc.AppendChild(xmlnode);
  175. XmlElement rootElement = doc.CreateElement("", "ServerResponse",
  176. "");
  177. doc.AppendChild(rootElement);
  178. XmlElement result = doc.CreateElement("", "result", "");
  179. result.AppendChild(doc.CreateTextNode("Failure"));
  180. rootElement.AppendChild(result);
  181. return DocToBytes(doc);
  182. }
  183. private byte[] DocToBytes(XmlDocument doc)
  184. {
  185. MemoryStream ms = new MemoryStream();
  186. XmlTextWriter xw = new XmlTextWriter(ms, null);
  187. xw.Formatting = Formatting.Indented;
  188. doc.WriteTo(xw);
  189. xw.Flush();
  190. return ms.ToArray();
  191. }
  192. private byte[] ResultToBytes(Dictionary<string, object> result)
  193. {
  194. string xmlString = ServerUtils.BuildXmlResponse(result);
  195. UTF8Encoding encoding = new UTF8Encoding();
  196. return encoding.GetBytes(xmlString);
  197. }
  198. }
  199. }