AgentPreferencesConnector.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 (serviceURI == String.Empty)
  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. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  137. if (replyData.ContainsKey("result"))
  138. {
  139. if (replyData["result"].ToString().ToLower() == "success")
  140. return true;
  141. else
  142. return false;
  143. }
  144. else
  145. {
  146. m_log.DebugFormat("[AGENT PREFERENCES CONNECTOR]: StoreAgentPreferences reply data does not contain result field");
  147. }
  148. }
  149. else
  150. m_log.DebugFormat("[AGENT PREFERENCES CONNECTOR]: StoreAgentPreferences received empty reply");
  151. }
  152. catch (Exception e)
  153. {
  154. m_log.DebugFormat("[AGENT PREFERENCES CONNECTOR]: Exception when contacting agent preferences server at {0}: {1}", uri, e.Message);
  155. }
  156. return false;
  157. }
  158. public string GetLang(UUID principalID)
  159. {
  160. Dictionary<string, object> sendData = new Dictionary<string, object>();
  161. string reply = string.Empty;
  162. sendData["METHOD"] = "getagentlang";
  163. sendData["UserID"] = principalID.ToString();
  164. string uri = String.Concat(m_ServerURI, "/agentprefs");
  165. string reqString = ServerUtils.BuildQueryString(sendData);
  166. try
  167. {
  168. reply = SynchronousRestFormsRequester.MakeRequest("POST", uri, reqString, m_Auth);
  169. if (String.IsNullOrEmpty(reply))
  170. {
  171. m_log.DebugFormat("[AGENT PREFERENCES CONNECTOR]: GetLang received null or empty reply");
  172. return "en-us"; // I guess? Gotta return somethin'!
  173. }
  174. }
  175. catch (Exception e)
  176. {
  177. m_log.DebugFormat("[AGENT PREFERENCES CONNECTOR]: Exception when contacting agent preferences server at {0}: {1}", uri, e.Message);
  178. }
  179. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  180. if (replyData != null)
  181. {
  182. if (replyData.ContainsKey("result") &&
  183. (replyData["result"].ToString() == "null" || replyData["result"].ToString() == "Failure"))
  184. {
  185. m_log.DebugFormat("[AGENT PREFERENCES CONNECTOR]: GetLang received Failure response");
  186. return "en-us";
  187. }
  188. if (replyData.ContainsKey("Language"))
  189. return replyData["Language"].ToString();
  190. }
  191. else
  192. {
  193. m_log.DebugFormat("[AGENT PREFERENCES CONNECTOR]: GetLang received null response");
  194. }
  195. return "en-us";
  196. }
  197. #endregion IAgentPreferencesService
  198. }
  199. }