AgentPreferencesConnector.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 log4net;
  28. using System;
  29. using System.Collections.Generic;
  30. using System.IO;
  31. using System.Reflection;
  32. using Nini.Config;
  33. using OpenSim.Framework;
  34. using OpenSim.Framework.ServiceAuth;
  35. using OpenSim.Services.Interfaces;
  36. using GridRegion = OpenSim.Services.Interfaces.GridRegion;
  37. using IAvatarService = OpenSim.Services.Interfaces.IAvatarService;
  38. using OpenSim.Server.Base;
  39. using OpenMetaverse;
  40. namespace OpenSim.Services.Connectors
  41. {
  42. public class AgentPreferencesServicesConnector : BaseServiceConnector, IAgentPreferencesService
  43. {
  44. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  45. private string m_ServerURI = String.Empty;
  46. public AgentPreferencesServicesConnector()
  47. {
  48. }
  49. public AgentPreferencesServicesConnector(string serverURI)
  50. {
  51. m_ServerURI = serverURI.TrimEnd('/');
  52. }
  53. public AgentPreferencesServicesConnector(IConfigSource source)
  54. : base(source, "AgentPreferencesService")
  55. {
  56. Initialise(source);
  57. }
  58. public void Initialise(IConfigSource source)
  59. {
  60. IConfig gridConfig = source.Configs["AgentPreferencesService"];
  61. if (gridConfig == null)
  62. {
  63. m_log.Error("[AGENT PREFERENCES CONNECTOR]: AgentPreferencesService missing from OpenSim.ini");
  64. throw new Exception("Agent Preferences connector init error");
  65. }
  66. string serviceURI = gridConfig.GetString("AgentPreferencesServerURI", string.Empty);
  67. if (string.IsNullOrEmpty(serviceURI))
  68. {
  69. m_log.Error("[AGENT PREFERENCES CONNECTOR]: No Server URI named in section AgentPreferences");
  70. throw new Exception("Agent Preferences connector init error");
  71. }
  72. m_ServerURI = serviceURI;
  73. base.Initialise(source, "AgentPreferencesService");
  74. }
  75. #region IAgentPreferencesService
  76. public AgentPrefs GetAgentPreferences(UUID principalID)
  77. {
  78. Dictionary<string, object> sendData = new Dictionary<string, object>();
  79. string reply = string.Empty;
  80. string uri = String.Concat(m_ServerURI, "/agentprefs");
  81. sendData["METHOD"] = "getagentprefs";
  82. sendData["UserID"] = principalID;
  83. string reqString = ServerUtils.BuildQueryString(sendData);
  84. // m_log.DebugFormat("[AGENT PREFS CONNECTOR]: queryString = {0}", reqString);
  85. try
  86. {
  87. reply = SynchronousRestFormsRequester.MakeRequest("POST", uri, reqString, m_Auth);
  88. if (string.IsNullOrEmpty(reply))
  89. {
  90. m_log.DebugFormat("[AGENT PREFERENCES CONNECTOR]: GetAgentPreferences received null or empty reply");
  91. return null;
  92. }
  93. }
  94. catch (Exception e)
  95. {
  96. m_log.DebugFormat("[AGENT PREFERENCES CONNECTOR]: Exception when contacting agent preferences server at {0}: {1}", uri, e.Message);
  97. }
  98. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  99. if (replyData != null)
  100. {
  101. if (replyData.ContainsKey("result") &&
  102. (replyData["result"].ToString() == "null" || replyData["result"].ToString() == "Failure"))
  103. {
  104. m_log.DebugFormat("[AGENT PREFERENCES CONNECTOR]: GetAgentPreferences received Failure response");
  105. return null;
  106. }
  107. }
  108. else
  109. {
  110. m_log.DebugFormat("[AGENT PREFERENCES CONNECTOR]: GetAgentPreferences received null response");
  111. return null;
  112. }
  113. AgentPrefs prefs = new AgentPrefs(replyData);
  114. return prefs;
  115. }
  116. public bool StoreAgentPreferences(AgentPrefs data)
  117. {
  118. Dictionary<string, object> sendData = new Dictionary<string, object>();
  119. sendData["METHOD"] = "setagentprefs";
  120. sendData["PrincipalID"] = data.PrincipalID.ToString();
  121. sendData["AccessPrefs"] = data.AccessPrefs;
  122. sendData["HoverHeight"] = data.HoverHeight.ToString();
  123. sendData["Language"] = data.Language;
  124. sendData["LanguageIsPublic"] = data.LanguageIsPublic.ToString();
  125. sendData["PermEveryone"] = data.PermEveryone.ToString();
  126. sendData["PermGroup"] = data.PermGroup.ToString();
  127. sendData["PermNextOwner"] = data.PermNextOwner.ToString();
  128. string uri = string.Concat(m_ServerURI, "/agentprefs");
  129. string reqString = ServerUtils.BuildQueryString(sendData);
  130. // m_log.DebugFormat("[AGENT PREFS CONNECTOR]: queryString = {0}", reqString);
  131. try
  132. {
  133. string reply = SynchronousRestFormsRequester.MakeRequest("POST", uri, reqString, m_Auth);
  134. if (reply != string.Empty)
  135. {
  136. int indx = reply.IndexOf("success", StringComparison.InvariantCultureIgnoreCase);
  137. if (indx > 0)
  138. return true;
  139. return false;
  140. }
  141. else
  142. m_log.DebugFormat("[AGENT PREFERENCES CONNECTOR]: StoreAgentPreferences received empty reply");
  143. }
  144. catch (Exception e)
  145. {
  146. m_log.DebugFormat("[AGENT PREFERENCES CONNECTOR]: Exception when contacting agent preferences server at {0}: {1}", uri, e.Message);
  147. }
  148. return false;
  149. }
  150. public string GetLang(UUID principalID)
  151. {
  152. Dictionary<string, object> sendData = new Dictionary<string, object>();
  153. string reply = string.Empty;
  154. sendData["METHOD"] = "getagentlang";
  155. sendData["UserID"] = principalID.ToString();
  156. string uri = string.Concat(m_ServerURI, "/agentprefs");
  157. string reqString = ServerUtils.BuildQueryString(sendData);
  158. try
  159. {
  160. reply = SynchronousRestFormsRequester.MakeRequest("POST", uri, reqString, m_Auth);
  161. if (string.IsNullOrEmpty(reply))
  162. {
  163. m_log.DebugFormat("[AGENT PREFERENCES CONNECTOR]: GetLang received null or empty reply");
  164. return "en-us"; // I guess? Gotta return somethin'!
  165. }
  166. }
  167. catch (Exception e)
  168. {
  169. m_log.DebugFormat("[AGENT PREFERENCES CONNECTOR]: Exception when contacting agent preferences server at {0}: {1}", uri, e.Message);
  170. }
  171. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  172. if (replyData != null)
  173. {
  174. if (replyData.ContainsKey("result") &&
  175. (replyData["result"].ToString() == "null" || replyData["result"].ToString() == "Failure"))
  176. {
  177. m_log.DebugFormat("[AGENT PREFERENCES CONNECTOR]: GetLang received Failure response");
  178. return "en-us";
  179. }
  180. if (replyData.ContainsKey("Language"))
  181. return replyData["Language"].ToString();
  182. }
  183. else
  184. {
  185. m_log.DebugFormat("[AGENT PREFERENCES CONNECTOR]: GetLang received null response");
  186. }
  187. return "en-us";
  188. }
  189. #endregion IAgentPreferencesService
  190. }
  191. }