AgentPreferencesServerPostHandler.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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.AgentPreferences
  45. {
  46. public class AgentPreferencesServerPostHandler : BaseStreamHandler
  47. {
  48. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  49. private IAgentPreferencesService m_AgentPreferencesService;
  50. public AgentPreferencesServerPostHandler(IAgentPreferencesService service, IServiceAuth auth) :
  51. base("POST", "/agentprefs", auth)
  52. {
  53. m_AgentPreferencesService = 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. try
  64. {
  65. Dictionary<string, object> request =
  66. ServerUtils.ParseQueryString(body);
  67. if (!request.ContainsKey("METHOD"))
  68. return FailureResult();
  69. string method = request["METHOD"].ToString();
  70. switch (method)
  71. {
  72. case "getagentprefs":
  73. return GetAgentPrefs(request);
  74. case "setagentprefs":
  75. return SetAgentPrefs(request);
  76. case "getagentlang":
  77. return GetAgentLang(request);
  78. }
  79. m_log.DebugFormat("[AGENT PREFERENCES HANDLER]: unknown method request: {0}", method);
  80. }
  81. catch (Exception e)
  82. {
  83. m_log.DebugFormat("[AGENT PREFERENCES HANDLER]: Exception {0}", e);
  84. }
  85. return FailureResult();
  86. }
  87. byte[] GetAgentPrefs(Dictionary<string, object> request)
  88. {
  89. if (!request.ContainsKey("UserID"))
  90. return FailureResult();
  91. UUID userID;
  92. if (!UUID.TryParse(request["UserID"].ToString(), out userID))
  93. return FailureResult();
  94. AgentPrefs prefs = m_AgentPreferencesService.GetAgentPreferences(userID);
  95. Dictionary<string, object> result = new Dictionary<string, object>();
  96. if (prefs != null)
  97. result = prefs.ToKeyValuePairs();
  98. string xmlString = ServerUtils.BuildXmlResponse(result);
  99. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  100. }
  101. byte[] SetAgentPrefs(Dictionary<string, object> request)
  102. {
  103. if (!request.ContainsKey("PrincipalID") || !request.ContainsKey("AccessPrefs") || !request.ContainsKey("HoverHeight")
  104. || !request.ContainsKey("Language") || !request.ContainsKey("LanguageIsPublic") || !request.ContainsKey("PermEveryone")
  105. || !request.ContainsKey("PermGroup") || !request.ContainsKey("PermNextOwner"))
  106. {
  107. return FailureResult();
  108. }
  109. UUID userID;
  110. if (!UUID.TryParse(request["PrincipalID"].ToString(), out userID))
  111. return FailureResult();
  112. AgentPrefs data = new AgentPrefs(userID);
  113. data.AccessPrefs = request["AccessPrefs"].ToString();
  114. data.HoverHeight = float.Parse(request["HoverHeight"].ToString());
  115. data.Language = request["Language"].ToString();
  116. data.LanguageIsPublic = bool.Parse(request["LanguageIsPublic"].ToString());
  117. data.PermEveryone = int.Parse(request["PermEveryone"].ToString());
  118. data.PermGroup = int.Parse(request["PermGroup"].ToString());
  119. data.PermNextOwner = int.Parse(request["PermNextOwner"].ToString());
  120. return m_AgentPreferencesService.StoreAgentPreferences(data) ? SuccessResult() : FailureResult();
  121. }
  122. byte[] GetAgentLang(Dictionary<string, object> request)
  123. {
  124. if (!request.ContainsKey("UserID"))
  125. return FailureResult();
  126. UUID userID;
  127. if (!UUID.TryParse(request["UserID"].ToString(), out userID))
  128. return FailureResult();
  129. string lang = "en-us";
  130. AgentPrefs prefs = m_AgentPreferencesService.GetAgentPreferences(userID);
  131. if (prefs != null)
  132. {
  133. if (prefs.LanguageIsPublic)
  134. lang = prefs.Language;
  135. }
  136. Dictionary<string, object> result = new Dictionary<string, object>();
  137. result["Language"] = lang;
  138. string xmlString = ServerUtils.BuildXmlResponse(result);
  139. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  140. }
  141. private byte[] SuccessResult()
  142. {
  143. XmlDocument doc = new XmlDocument();
  144. XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
  145. "", "");
  146. doc.AppendChild(xmlnode);
  147. XmlElement rootElement = doc.CreateElement("", "ServerResponse",
  148. "");
  149. doc.AppendChild(rootElement);
  150. XmlElement result = doc.CreateElement("", "result", "");
  151. result.AppendChild(doc.CreateTextNode("Success"));
  152. rootElement.AppendChild(result);
  153. return Util.DocToBytes(doc);
  154. }
  155. private byte[] FailureResult()
  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("Failure"));
  166. rootElement.AppendChild(result);
  167. return Util.DocToBytes(doc);
  168. }
  169. }
  170. }