AvatarServiceConnector.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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.Communications;
  35. using OpenSim.Framework.Servers.HttpServer;
  36. using OpenSim.Services.Interfaces;
  37. using GridRegion = OpenSim.Services.Interfaces.GridRegion;
  38. using IAvatarService = OpenSim.Services.Interfaces.IAvatarService;
  39. using OpenSim.Server.Base;
  40. using OpenMetaverse;
  41. namespace OpenSim.Services.Connectors
  42. {
  43. public class AvatarServicesConnector : IAvatarService
  44. {
  45. private static readonly ILog m_log =
  46. LogManager.GetLogger(
  47. MethodBase.GetCurrentMethod().DeclaringType);
  48. private string m_ServerURI = String.Empty;
  49. public AvatarServicesConnector()
  50. {
  51. }
  52. public AvatarServicesConnector(string serverURI)
  53. {
  54. m_ServerURI = serverURI.TrimEnd('/');
  55. }
  56. public AvatarServicesConnector(IConfigSource source)
  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 == String.Empty)
  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. }
  77. #region IAvatarService
  78. public AvatarData GetAvatar(UUID userID)
  79. {
  80. Dictionary<string, object> sendData = new Dictionary<string, object>();
  81. //sendData["SCOPEID"] = scopeID.ToString();
  82. sendData["VERSIONMIN"] = ProtocolVersions.ClientProtocolVersionMin.ToString();
  83. sendData["VERSIONMAX"] = ProtocolVersions.ClientProtocolVersionMax.ToString();
  84. sendData["METHOD"] = "getavatar";
  85. sendData["UserID"] = userID;
  86. string reply = string.Empty;
  87. string reqString = ServerUtils.BuildQueryString(sendData);
  88. // m_log.DebugFormat("[AVATAR CONNECTOR]: queryString = {0}", reqString);
  89. try
  90. {
  91. reply = SynchronousRestFormsRequester.MakeRequest("POST",
  92. m_ServerURI + "/avatar",
  93. reqString);
  94. if (reply == null || (reply != null && reply == string.Empty))
  95. {
  96. m_log.DebugFormat("[AVATAR CONNECTOR]: GetAgent received null or empty reply");
  97. return null;
  98. }
  99. }
  100. catch (Exception e)
  101. {
  102. m_log.DebugFormat("[AVATAR CONNECTOR]: Exception when contacting presence server: {0}", e.Message);
  103. }
  104. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  105. AvatarData avatar = null;
  106. if ((replyData != null) && replyData.ContainsKey("result") && (replyData["result"] != null))
  107. {
  108. if (replyData["result"] is Dictionary<string, object>)
  109. {
  110. avatar = new AvatarData((Dictionary<string, object>)replyData["result"]);
  111. }
  112. }
  113. return avatar;
  114. }
  115. public bool SetAvatar(UUID userID, AvatarData avatar)
  116. {
  117. Dictionary<string, object> sendData = new Dictionary<string, object>();
  118. //sendData["SCOPEID"] = scopeID.ToString();
  119. sendData["VERSIONMIN"] = ProtocolVersions.ClientProtocolVersionMin.ToString();
  120. sendData["VERSIONMAX"] = ProtocolVersions.ClientProtocolVersionMax.ToString();
  121. sendData["METHOD"] = "setavatar";
  122. sendData["UserID"] = userID.ToString();
  123. Dictionary<string, object> structData = avatar.ToKeyValuePairs();
  124. foreach (KeyValuePair<string, object> kvp in structData)
  125. sendData[kvp.Key] = kvp.Value.ToString();
  126. string reqString = ServerUtils.BuildQueryString(sendData);
  127. //m_log.DebugFormat("[AVATAR CONNECTOR]: queryString = {0}", reqString);
  128. try
  129. {
  130. string reply = SynchronousRestFormsRequester.MakeRequest("POST",
  131. m_ServerURI + "/avatar",
  132. reqString);
  133. if (reply != string.Empty)
  134. {
  135. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  136. if (replyData.ContainsKey("result"))
  137. {
  138. if (replyData["result"].ToString().ToLower() == "success")
  139. return true;
  140. else
  141. return false;
  142. }
  143. else
  144. m_log.DebugFormat("[AVATAR CONNECTOR]: SetAvatar reply data does not contain result field");
  145. }
  146. else
  147. m_log.DebugFormat("[AVATAR CONNECTOR]: SetAvatar received empty reply");
  148. }
  149. catch (Exception e)
  150. {
  151. m_log.DebugFormat("[AVATAR CONNECTOR]: Exception when contacting avatar server: {0}", e.Message);
  152. }
  153. return false;
  154. }
  155. public bool ResetAvatar(UUID userID)
  156. {
  157. Dictionary<string, object> sendData = new Dictionary<string, object>();
  158. //sendData["SCOPEID"] = scopeID.ToString();
  159. sendData["VERSIONMIN"] = ProtocolVersions.ClientProtocolVersionMin.ToString();
  160. sendData["VERSIONMAX"] = ProtocolVersions.ClientProtocolVersionMax.ToString();
  161. sendData["METHOD"] = "resetavatar";
  162. sendData["UserID"] = userID.ToString();
  163. string reqString = ServerUtils.BuildQueryString(sendData);
  164. // m_log.DebugFormat("[AVATAR CONNECTOR]: queryString = {0}", reqString);
  165. try
  166. {
  167. string reply = SynchronousRestFormsRequester.MakeRequest("POST",
  168. m_ServerURI + "/avatar",
  169. reqString);
  170. if (reply != string.Empty)
  171. {
  172. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  173. if (replyData.ContainsKey("result"))
  174. {
  175. if (replyData["result"].ToString().ToLower() == "success")
  176. return true;
  177. else
  178. return false;
  179. }
  180. else
  181. m_log.DebugFormat("[AVATAR CONNECTOR]: SetItems reply data does not contain result field");
  182. }
  183. else
  184. m_log.DebugFormat("[AVATAR CONNECTOR]: SetItems received empty reply");
  185. }
  186. catch (Exception e)
  187. {
  188. m_log.DebugFormat("[AVATAR CONNECTOR]: Exception when contacting avatar server: {0}", e.Message);
  189. }
  190. return false;
  191. }
  192. public bool SetItems(UUID userID, string[] names, string[] values)
  193. {
  194. Dictionary<string, object> sendData = new Dictionary<string, object>();
  195. sendData["VERSIONMIN"] = ProtocolVersions.ClientProtocolVersionMin.ToString();
  196. sendData["VERSIONMAX"] = ProtocolVersions.ClientProtocolVersionMax.ToString();
  197. sendData["METHOD"] = "setitems";
  198. sendData["UserID"] = userID.ToString();
  199. sendData["Names"] = new List<string>(names);
  200. sendData["Values"] = new List<string>(values);
  201. string reqString = ServerUtils.BuildQueryString(sendData);
  202. // m_log.DebugFormat("[AVATAR CONNECTOR]: queryString = {0}", reqString);
  203. try
  204. {
  205. string reply = SynchronousRestFormsRequester.MakeRequest("POST",
  206. m_ServerURI + "/avatar",
  207. reqString);
  208. if (reply != string.Empty)
  209. {
  210. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  211. if (replyData.ContainsKey("result"))
  212. {
  213. if (replyData["result"].ToString().ToLower() == "success")
  214. return true;
  215. else
  216. return false;
  217. }
  218. else
  219. m_log.DebugFormat("[AVATAR CONNECTOR]: SetItems reply data does not contain result field");
  220. }
  221. else
  222. m_log.DebugFormat("[AVATAR CONNECTOR]: SetItems received empty reply");
  223. }
  224. catch (Exception e)
  225. {
  226. m_log.DebugFormat("[AVATAR CONNECTOR]: Exception when contacting avatar server: {0}", e.Message);
  227. }
  228. return false;
  229. }
  230. public bool RemoveItems(UUID userID, string[] names)
  231. {
  232. Dictionary<string, object> sendData = new Dictionary<string, object>();
  233. //sendData["SCOPEID"] = scopeID.ToString();
  234. sendData["VERSIONMIN"] = ProtocolVersions.ClientProtocolVersionMin.ToString();
  235. sendData["VERSIONMAX"] = ProtocolVersions.ClientProtocolVersionMax.ToString();
  236. sendData["METHOD"] = "removeitems";
  237. sendData["UserID"] = userID.ToString();
  238. sendData["Names"] = new List<string>(names);
  239. string reqString = ServerUtils.BuildQueryString(sendData);
  240. // m_log.DebugFormat("[AVATAR CONNECTOR]: queryString = {0}", reqString);
  241. try
  242. {
  243. string reply = SynchronousRestFormsRequester.MakeRequest("POST",
  244. m_ServerURI + "/avatar",
  245. reqString);
  246. if (reply != string.Empty)
  247. {
  248. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  249. if (replyData.ContainsKey("result"))
  250. {
  251. if (replyData["result"].ToString().ToLower() == "success")
  252. return true;
  253. else
  254. return false;
  255. }
  256. else
  257. m_log.DebugFormat("[AVATAR CONNECTOR]: RemoveItems reply data does not contain result field");
  258. }
  259. else
  260. m_log.DebugFormat("[AVATAR CONNECTOR]: RemoveItems received empty reply");
  261. }
  262. catch (Exception e)
  263. {
  264. m_log.DebugFormat("[AVATAR CONNECTOR]: Exception when contacting avatar server: {0}", e.Message);
  265. }
  266. return false;
  267. }
  268. #endregion
  269. }
  270. }