PageRenderTime 63ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/OpenSim/Services/Handlers/UserAccounts/UserAccountServerPostHandler.cs

http://github.com/aurora-sim/Aurora-Sim
C# | 250 lines | 178 code | 41 blank | 31 comment | 38 complexity | 6449b6fed749382c2b71b5103189cbfa MD5 | raw file
Possible License(s): MIT, LGPL-3.0
  1. /*
  2. * Copyright (c) Contributors, http://aurora-sim.org/, 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 Aurora-Sim 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.Collections.Generic;
  29. using System.IO;
  30. using System.Linq;
  31. using System.Reflection;
  32. using System.Text;
  33. using System.Xml;
  34. using Aurora.Simulation.Base;
  35. using OpenMetaverse;
  36. using Aurora.Framework;
  37. using Aurora.Framework.Servers.HttpServer;
  38. using OpenSim.Services.Interfaces;
  39. namespace OpenSim.Services
  40. {
  41. public class UserAccountServerPostHandler : BaseStreamHandler
  42. {
  43. private readonly IUserAccountService m_UserAccountService;
  44. protected string m_SessionID;
  45. protected IRegistryCore m_registry;
  46. public UserAccountServerPostHandler(string url, IUserAccountService service, string SessionID,
  47. IRegistryCore registry) :
  48. base("POST", url)
  49. {
  50. m_UserAccountService = service.InnerService;
  51. m_SessionID = SessionID;
  52. m_registry = registry;
  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. //MainConsole.Instance.DebugFormat("[XXX]: query String: {0}", body);
  64. string method = string.Empty;
  65. try
  66. {
  67. Dictionary<string, object> request =
  68. WebUtils.ParseQueryString(body);
  69. if (!request.ContainsKey("METHOD"))
  70. return FailureResult();
  71. method = request["METHOD"].ToString();
  72. IGridRegistrationService urlModule =
  73. m_registry.RequestModuleInterface<IGridRegistrationService>();
  74. switch (method)
  75. {
  76. case "getaccount":
  77. if (urlModule != null)
  78. if (!urlModule.CheckThreatLevel(m_SessionID, method, ThreatLevel.None))
  79. return new byte[0];
  80. return GetAccount(request);
  81. case "getaccounts":
  82. if (urlModule != null)
  83. if (!urlModule.CheckThreatLevel(m_SessionID, method, ThreatLevel.None))
  84. return new byte[0];
  85. return GetAccounts(request);
  86. case "setaccount":
  87. if (urlModule != null)
  88. if (!urlModule.CheckThreatLevel(m_SessionID, method, ThreatLevel.Full))
  89. return new byte[0];
  90. return StoreAccount(request);
  91. }
  92. MainConsole.Instance.DebugFormat("[USER SERVICE HANDLER]: unknown method request: {0}", method);
  93. }
  94. catch (Exception e)
  95. {
  96. MainConsole.Instance.DebugFormat("[USER SERVICE HANDLER]: Exception in method {0}: {1}", method, e);
  97. }
  98. return FailureResult();
  99. }
  100. private byte[] GetAccount(Dictionary<string, object> request)
  101. {
  102. UserAccount account = null;
  103. UUID scopeID = UUID.Zero;
  104. Dictionary<string, object> result = new Dictionary<string, object>();
  105. if (!request.ContainsKey("ScopeID"))
  106. {
  107. result["result"] = "null";
  108. return ResultToBytes(result);
  109. }
  110. if (!UUID.TryParse(request["ScopeID"].ToString(), out scopeID))
  111. {
  112. result["result"] = "null";
  113. return ResultToBytes(result);
  114. }
  115. if (request.ContainsKey("UserID") && request["UserID"] != null)
  116. {
  117. UUID userID;
  118. if (UUID.TryParse(request["UserID"].ToString(), out userID))
  119. account = m_UserAccountService.GetUserAccount(scopeID, userID);
  120. }
  121. else if (request.ContainsKey("Name") && request["Name"] != null)
  122. account = m_UserAccountService.GetUserAccount(scopeID, request["Name"].ToString());
  123. else if (request.ContainsKey("FirstName") && request.ContainsKey("LastName") &&
  124. request["FirstName"] != null && request["LastName"] != null)
  125. account = m_UserAccountService.GetUserAccount(scopeID, request["FirstName"].ToString(),
  126. request["LastName"].ToString());
  127. if (account == null)
  128. result["result"] = "null";
  129. else
  130. {
  131. result["result"] = account.ToKVP();
  132. }
  133. return ResultToBytes(result);
  134. }
  135. private byte[] GetAccounts(Dictionary<string, object> request)
  136. {
  137. if (!request.ContainsKey("ScopeID") || !request.ContainsKey("query"))
  138. return FailureResult();
  139. UUID scopeID = UUID.Zero;
  140. if (!UUID.TryParse(request["ScopeID"].ToString(), out scopeID))
  141. return FailureResult();
  142. string query = request["query"].ToString();
  143. List<UserAccount> accounts = m_UserAccountService.GetUserAccounts(scopeID, query);
  144. Dictionary<string, object> result = new Dictionary<string, object>();
  145. if ((accounts == null) || ((accounts != null) && (accounts.Count == 0)))
  146. result["result"] = "null";
  147. else
  148. {
  149. int i = 0;
  150. foreach(UserAccount acc in accounts)
  151. {
  152. result["account" + i] = acc.ToKVP();
  153. i++;
  154. }
  155. }
  156. string xmlString = WebUtils.BuildXmlResponse(result);
  157. //MainConsole.Instance.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString);
  158. UTF8Encoding encoding = new UTF8Encoding();
  159. return encoding.GetBytes(xmlString);
  160. }
  161. private byte[] StoreAccount(Dictionary<string, object> request)
  162. {
  163. // No can do. No changing user accounts from remote sims
  164. return FailureResult();
  165. }
  166. private byte[] SuccessResult()
  167. {
  168. XmlDocument doc = new XmlDocument();
  169. XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
  170. "", "");
  171. doc.AppendChild(xmlnode);
  172. XmlElement rootElement = doc.CreateElement("", "ServerResponse",
  173. "");
  174. doc.AppendChild(rootElement);
  175. XmlElement result = doc.CreateElement("", "result", "");
  176. result.AppendChild(doc.CreateTextNode("Success"));
  177. rootElement.AppendChild(result);
  178. return DocToBytes(doc);
  179. }
  180. private byte[] FailureResult()
  181. {
  182. XmlDocument doc = new XmlDocument();
  183. XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
  184. "", "");
  185. doc.AppendChild(xmlnode);
  186. XmlElement rootElement = doc.CreateElement("", "ServerResponse",
  187. "");
  188. doc.AppendChild(rootElement);
  189. XmlElement result = doc.CreateElement("", "result", "");
  190. result.AppendChild(doc.CreateTextNode("Failure"));
  191. rootElement.AppendChild(result);
  192. return DocToBytes(doc);
  193. }
  194. private byte[] DocToBytes(XmlDocument doc)
  195. {
  196. MemoryStream ms = new MemoryStream();
  197. XmlTextWriter xw = new XmlTextWriter(ms, null) {Formatting = Formatting.Indented};
  198. doc.WriteTo(xw);
  199. xw.Flush();
  200. return ms.ToArray();
  201. }
  202. private byte[] ResultToBytes(Dictionary<string, object> result)
  203. {
  204. string xmlString = WebUtils.BuildXmlResponse(result);
  205. UTF8Encoding encoding = new UTF8Encoding();
  206. return encoding.GetBytes(xmlString);
  207. }
  208. }
  209. }