1
0

AvatarServicesConnector.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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 AvatarServicesConnector : BaseServiceConnector, IAvatarService
  43. {
  44. private static readonly ILog m_log =
  45. LogManager.GetLogger(
  46. MethodBase.GetCurrentMethod().DeclaringType);
  47. private string m_ServerURI = String.Empty;
  48. public AvatarServicesConnector()
  49. {
  50. }
  51. public AvatarServicesConnector(string serverURI)
  52. {
  53. m_ServerURI = serverURI.TrimEnd('/');
  54. }
  55. public AvatarServicesConnector(IConfigSource source)
  56. : base(source, "AvatarService")
  57. {
  58. Initialise(source);
  59. }
  60. public virtual void Initialise(IConfigSource source)
  61. {
  62. IConfig gridConfig = source.Configs["AvatarService"];
  63. if (gridConfig == null)
  64. {
  65. m_log.Error("[AVATAR CONNECTOR]: AvatarService missing from OpenSim.ini");
  66. throw new Exception("Avatar connector init error");
  67. }
  68. string serviceURI = gridConfig.GetString("AvatarServerURI",
  69. String.Empty);
  70. if (serviceURI.Length == 0)
  71. {
  72. m_log.Error("[AVATAR CONNECTOR]: No Server URI named in section AvatarService");
  73. throw new Exception("Avatar connector init error");
  74. }
  75. m_ServerURI = serviceURI;
  76. base.Initialise(source, "AvatarService");
  77. }
  78. #region IAvatarService
  79. public AvatarAppearance GetAppearance(UUID userID)
  80. {
  81. AvatarData avatar = GetAvatar(userID);
  82. return avatar.ToAvatarAppearance();
  83. }
  84. public bool SetAppearance(UUID userID, AvatarAppearance appearance)
  85. {
  86. AvatarData avatar = new AvatarData(appearance);
  87. return SetAvatar(userID,avatar);
  88. }
  89. public AvatarData GetAvatar(UUID userID)
  90. {
  91. Dictionary<string, object> sendData = new Dictionary<string, object>();
  92. //sendData["SCOPEID"] = scopeID.ToString();
  93. sendData["VERSIONMIN"] = ProtocolVersions.ClientProtocolVersionMin.ToString();
  94. sendData["VERSIONMAX"] = ProtocolVersions.ClientProtocolVersionMax.ToString();
  95. sendData["METHOD"] = "getavatar";
  96. sendData["UserID"] = userID;
  97. string reply = string.Empty;
  98. string reqString = ServerUtils.BuildQueryString(sendData);
  99. string uri = m_ServerURI + "/avatar";
  100. // m_log.DebugFormat("[AVATAR CONNECTOR]: queryString = {0}", reqString);
  101. try
  102. {
  103. reply = SynchronousRestFormsRequester.MakeRequest("POST", uri, reqString, m_Auth);
  104. if (string.IsNullOrEmpty(reply))
  105. {
  106. m_log.DebugFormat("[AVATAR CONNECTOR]: GetAgent received null or empty reply");
  107. return null;
  108. }
  109. }
  110. catch (Exception e)
  111. {
  112. m_log.DebugFormat("[AVATAR CONNECTOR]: Exception when contacting presence server at {0}: {1}", uri, e.Message);
  113. }
  114. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  115. AvatarData avatar = null;
  116. if ((replyData != null) && replyData.ContainsKey("result") && (replyData["result"] != null))
  117. {
  118. if (replyData["result"] is Dictionary<string, object>)
  119. {
  120. avatar = new AvatarData((Dictionary<string, object>)replyData["result"]);
  121. }
  122. }
  123. return avatar;
  124. }
  125. public bool SetAvatar(UUID userID, AvatarData avatar)
  126. {
  127. Dictionary<string, object> sendData = new Dictionary<string, object>();
  128. //sendData["SCOPEID"] = scopeID.ToString();
  129. sendData["VERSIONMIN"] = ProtocolVersions.ClientProtocolVersionMin.ToString();
  130. sendData["VERSIONMAX"] = ProtocolVersions.ClientProtocolVersionMax.ToString();
  131. sendData["METHOD"] = "setavatar";
  132. sendData["UserID"] = userID.ToString();
  133. Dictionary<string, object> structData = avatar.ToKeyValuePairs();
  134. foreach (KeyValuePair<string, object> kvp in structData)
  135. sendData[kvp.Key] = kvp.Value.ToString();
  136. string reqString = ServerUtils.BuildQueryString(sendData);
  137. string uri = m_ServerURI + "/avatar";
  138. //m_log.DebugFormat("[AVATAR CONNECTOR]: queryString = {0}", reqString);
  139. try
  140. {
  141. string reply = SynchronousRestFormsRequester.MakeRequest("POST", uri, reqString, m_Auth);
  142. if (reply != string.Empty)
  143. {
  144. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  145. if (replyData.ContainsKey("result"))
  146. {
  147. if (replyData["result"].ToString().ToLower() == "success")
  148. return true;
  149. else
  150. return false;
  151. }
  152. else
  153. {
  154. m_log.DebugFormat("[AVATAR CONNECTOR]: SetAvatar reply data does not contain result field");
  155. }
  156. }
  157. else
  158. {
  159. m_log.DebugFormat("[AVATAR CONNECTOR]: SetAvatar received empty reply");
  160. }
  161. }
  162. catch (Exception e)
  163. {
  164. m_log.DebugFormat("[AVATAR CONNECTOR]: Exception when contacting presence server at {0}: {1}", uri, e.Message);
  165. }
  166. return false;
  167. }
  168. public bool ResetAvatar(UUID userID)
  169. {
  170. Dictionary<string, object> sendData = new Dictionary<string, object>();
  171. //sendData["SCOPEID"] = scopeID.ToString();
  172. sendData["VERSIONMIN"] = ProtocolVersions.ClientProtocolVersionMin.ToString();
  173. sendData["VERSIONMAX"] = ProtocolVersions.ClientProtocolVersionMax.ToString();
  174. sendData["METHOD"] = "resetavatar";
  175. sendData["UserID"] = userID.ToString();
  176. string reqString = ServerUtils.BuildQueryString(sendData);
  177. string uri = m_ServerURI + "/avatar";
  178. // m_log.DebugFormat("[AVATAR CONNECTOR]: queryString = {0}", reqString);
  179. try
  180. {
  181. string reply = SynchronousRestFormsRequester.MakeRequest("POST", uri, reqString, m_Auth);
  182. if (reply != string.Empty)
  183. {
  184. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  185. if (replyData.ContainsKey("result"))
  186. {
  187. if (replyData["result"].ToString().ToLower() == "success")
  188. return true;
  189. else
  190. return false;
  191. }
  192. else
  193. m_log.DebugFormat("[AVATAR CONNECTOR]: SetItems reply data does not contain result field");
  194. }
  195. else
  196. m_log.DebugFormat("[AVATAR CONNECTOR]: SetItems received empty reply");
  197. }
  198. catch (Exception e)
  199. {
  200. m_log.DebugFormat("[AVATAR CONNECTOR]: Exception when contacting presence server at {0}: {1}", uri, e.Message);
  201. }
  202. return false;
  203. }
  204. public bool SetItems(UUID userID, string[] names, string[] values)
  205. {
  206. Dictionary<string, object> sendData = new Dictionary<string, object>();
  207. sendData["VERSIONMIN"] = ProtocolVersions.ClientProtocolVersionMin.ToString();
  208. sendData["VERSIONMAX"] = ProtocolVersions.ClientProtocolVersionMax.ToString();
  209. sendData["METHOD"] = "setitems";
  210. sendData["UserID"] = userID.ToString();
  211. sendData["Names"] = new List<string>(names);
  212. sendData["Values"] = new List<string>(values);
  213. string reqString = ServerUtils.BuildQueryString(sendData);
  214. string uri = m_ServerURI + "/avatar";
  215. // m_log.DebugFormat("[AVATAR CONNECTOR]: queryString = {0}", reqString);
  216. try
  217. {
  218. string reply = SynchronousRestFormsRequester.MakeRequest("POST", uri, reqString, m_Auth);
  219. if (reply != string.Empty)
  220. {
  221. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  222. if (replyData.ContainsKey("result"))
  223. {
  224. if (replyData["result"].ToString().ToLower() == "success")
  225. return true;
  226. else
  227. return false;
  228. }
  229. else
  230. m_log.DebugFormat("[AVATAR CONNECTOR]: SetItems reply data does not contain result field");
  231. }
  232. else
  233. m_log.DebugFormat("[AVATAR CONNECTOR]: SetItems received empty reply");
  234. }
  235. catch (Exception e)
  236. {
  237. m_log.DebugFormat("[AVATAR CONNECTOR]: Exception when contacting presence server at {0}: {1}", uri, e.Message);
  238. }
  239. return false;
  240. }
  241. public bool RemoveItems(UUID userID, string[] names)
  242. {
  243. Dictionary<string, object> sendData = new Dictionary<string, object>();
  244. //sendData["SCOPEID"] = scopeID.ToString();
  245. sendData["VERSIONMIN"] = ProtocolVersions.ClientProtocolVersionMin.ToString();
  246. sendData["VERSIONMAX"] = ProtocolVersions.ClientProtocolVersionMax.ToString();
  247. sendData["METHOD"] = "removeitems";
  248. sendData["UserID"] = userID.ToString();
  249. sendData["Names"] = new List<string>(names);
  250. string reqString = ServerUtils.BuildQueryString(sendData);
  251. string uri = m_ServerURI + "/avatar";
  252. // m_log.DebugFormat("[AVATAR CONNECTOR]: queryString = {0}", reqString);
  253. try
  254. {
  255. string reply = SynchronousRestFormsRequester.MakeRequest("POST", uri, reqString, m_Auth);
  256. if (reply != string.Empty)
  257. {
  258. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  259. if (replyData.ContainsKey("result"))
  260. {
  261. if (replyData["result"].ToString().ToLower() == "success")
  262. return true;
  263. else
  264. return false;
  265. }
  266. else
  267. m_log.DebugFormat("[AVATAR CONNECTOR]: RemoveItems reply data does not contain result field");
  268. }
  269. else
  270. m_log.DebugFormat("[AVATAR CONNECTOR]: RemoveItems received empty reply");
  271. }
  272. catch (Exception e)
  273. {
  274. m_log.DebugFormat("[AVATAR CONNECTOR]: Exception when contacting presence server at {0}: {1}", uri, e.Message);
  275. }
  276. return false;
  277. }
  278. #endregion
  279. }
  280. }