AgentPreferencesServerPostHandler.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. result = prefs.ToKeyValuePairs();
  97. string xmlString = ServerUtils.BuildXmlResponse(result);
  98. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  99. }
  100. byte[] SetAgentPrefs(Dictionary<string, object> request)
  101. {
  102. if (!request.ContainsKey("PrincipalID") || !request.ContainsKey("AccessPrefs") || !request.ContainsKey("HoverHeight")
  103. || !request.ContainsKey("Language") || !request.ContainsKey("LanguageIsPublic") || !request.ContainsKey("PermEveryone")
  104. || !request.ContainsKey("PermGroup") || !request.ContainsKey("PermNextOwner"))
  105. {
  106. return FailureResult();
  107. }
  108. UUID userID;
  109. if (!UUID.TryParse(request["PrincipalID"].ToString(), out userID))
  110. return FailureResult();
  111. AgentPrefs data = new AgentPrefs(userID);
  112. data.AccessPrefs = request["AccessPrefs"].ToString();
  113. data.HoverHeight = double.Parse(request["HoverHeight"].ToString());
  114. data.Language = request["Language"].ToString();
  115. data.LanguageIsPublic = bool.Parse(request["LanguageIsPublic"].ToString());
  116. data.PermEveryone = int.Parse(request["PermEveryone"].ToString());
  117. data.PermGroup = int.Parse(request["PermGroup"].ToString());
  118. data.PermNextOwner = int.Parse(request["PermNextOwner"].ToString());
  119. return m_AgentPreferencesService.StoreAgentPreferences(data) ? SuccessResult() : FailureResult();
  120. }
  121. byte[] GetAgentLang(Dictionary<string, object> request)
  122. {
  123. if (!request.ContainsKey("UserID"))
  124. return FailureResult();
  125. UUID userID;
  126. if (!UUID.TryParse(request["UserID"].ToString(), out userID))
  127. return FailureResult();
  128. string lang = "en-us";
  129. AgentPrefs prefs = m_AgentPreferencesService.GetAgentPreferences(userID);
  130. if (prefs != null)
  131. {
  132. if (prefs.LanguageIsPublic)
  133. lang = prefs.Language;
  134. }
  135. Dictionary<string, object> result = new Dictionary<string, object>();
  136. result["Language"] = lang;
  137. string xmlString = ServerUtils.BuildXmlResponse(result);
  138. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  139. }
  140. private byte[] SuccessResult()
  141. {
  142. XmlDocument doc = new XmlDocument();
  143. XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
  144. "", "");
  145. doc.AppendChild(xmlnode);
  146. XmlElement rootElement = doc.CreateElement("", "ServerResponse",
  147. "");
  148. doc.AppendChild(rootElement);
  149. XmlElement result = doc.CreateElement("", "result", "");
  150. result.AppendChild(doc.CreateTextNode("Success"));
  151. rootElement.AppendChild(result);
  152. return Util.DocToBytes(doc);
  153. }
  154. private byte[] FailureResult()
  155. {
  156. XmlDocument doc = new XmlDocument();
  157. XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
  158. "", "");
  159. doc.AppendChild(xmlnode);
  160. XmlElement rootElement = doc.CreateElement("", "ServerResponse",
  161. "");
  162. doc.AppendChild(rootElement);
  163. XmlElement result = doc.CreateElement("", "result", "");
  164. result.AppendChild(doc.CreateTextNode("Failure"));
  165. rootElement.AppendChild(result);
  166. return Util.DocToBytes(doc);
  167. }
  168. }
  169. }