AvatarServerPostHandler.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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.Servers.HttpServer;
  42. using OpenMetaverse;
  43. namespace OpenSim.Server.Handlers.Avatar
  44. {
  45. public class AvatarServerPostHandler : BaseStreamHandler
  46. {
  47. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  48. private IAvatarService m_AvatarService;
  49. public AvatarServerPostHandler(IAvatarService service) :
  50. base("POST", "/avatar")
  51. {
  52. m_AvatarService = service;
  53. }
  54. protected override byte[] ProcessRequest(string path, Stream requestData,
  55. IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
  56. {
  57. StreamReader sr = new StreamReader(requestData);
  58. string body = sr.ReadToEnd();
  59. sr.Close();
  60. body = body.Trim();
  61. //m_log.DebugFormat("[XXX]: query String: {0}", body);
  62. try
  63. {
  64. Dictionary<string, object> request =
  65. ServerUtils.ParseQueryString(body);
  66. if (!request.ContainsKey("METHOD"))
  67. return FailureResult();
  68. string method = request["METHOD"].ToString();
  69. switch (method)
  70. {
  71. case "getavatar":
  72. return GetAvatar(request);
  73. case "setavatar":
  74. return SetAvatar(request);
  75. case "resetavatar":
  76. return ResetAvatar(request);
  77. case "setitems":
  78. return SetItems(request);
  79. case "removeitems":
  80. return RemoveItems(request);
  81. }
  82. m_log.DebugFormat("[AVATAR HANDLER]: unknown method request: {0}", method);
  83. }
  84. catch (Exception e)
  85. {
  86. m_log.Debug("[AVATAR HANDLER]: Exception {0}" + e);
  87. }
  88. return FailureResult();
  89. }
  90. byte[] GetAvatar(Dictionary<string, object> request)
  91. {
  92. UUID user = UUID.Zero;
  93. if (!request.ContainsKey("UserID"))
  94. return FailureResult();
  95. if (UUID.TryParse(request["UserID"].ToString(), out user))
  96. {
  97. AvatarData avatar = m_AvatarService.GetAvatar(user);
  98. if (avatar == null)
  99. return FailureResult();
  100. Dictionary<string, object> result = new Dictionary<string, object>();
  101. if (avatar == null)
  102. result["result"] = "null";
  103. else
  104. result["result"] = avatar.ToKeyValuePairs();
  105. string xmlString = ServerUtils.BuildXmlResponse(result);
  106. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  107. }
  108. return FailureResult();
  109. }
  110. byte[] SetAvatar(Dictionary<string, object> request)
  111. {
  112. UUID user = UUID.Zero;
  113. if (!request.ContainsKey("UserID"))
  114. return FailureResult();
  115. if (!UUID.TryParse(request["UserID"].ToString(), out user))
  116. return FailureResult();
  117. RemoveRequestParamsNotForStorage(request);
  118. AvatarData avatar = new AvatarData(request);
  119. if (m_AvatarService.SetAvatar(user, avatar))
  120. return SuccessResult();
  121. return FailureResult();
  122. }
  123. byte[] ResetAvatar(Dictionary<string, object> request)
  124. {
  125. UUID user = UUID.Zero;
  126. if (!request.ContainsKey("UserID"))
  127. return FailureResult();
  128. if (!UUID.TryParse(request["UserID"].ToString(), out user))
  129. return FailureResult();
  130. RemoveRequestParamsNotForStorage(request);
  131. if (m_AvatarService.ResetAvatar(user))
  132. return SuccessResult();
  133. return FailureResult();
  134. }
  135. /// <summary>
  136. /// Remove parameters that were used to invoke the method and should not in themselves be persisted.
  137. /// </summary>
  138. /// <param name='request'></param>
  139. private void RemoveRequestParamsNotForStorage(Dictionary<string, object> request)
  140. {
  141. request.Remove("VERSIONMAX");
  142. request.Remove("VERSIONMIN");
  143. request.Remove("METHOD");
  144. request.Remove("UserID");
  145. }
  146. byte[] SetItems(Dictionary<string, object> request)
  147. {
  148. UUID user = UUID.Zero;
  149. string[] names, values;
  150. if (!request.ContainsKey("UserID") || !request.ContainsKey("Names") || !request.ContainsKey("Values"))
  151. return FailureResult();
  152. if (!UUID.TryParse(request["UserID"].ToString(), out user))
  153. return FailureResult();
  154. if (!(request["Names"] is List<string> || request["Values"] is List<string>))
  155. return FailureResult();
  156. RemoveRequestParamsNotForStorage(request);
  157. List<string> _names = (List<string>)request["Names"];
  158. names = _names.ToArray();
  159. List<string> _values = (List<string>)request["Values"];
  160. values = _values.ToArray();
  161. if (m_AvatarService.SetItems(user, names, values))
  162. return SuccessResult();
  163. return FailureResult();
  164. }
  165. byte[] RemoveItems(Dictionary<string, object> request)
  166. {
  167. UUID user = UUID.Zero;
  168. string[] names;
  169. if (!request.ContainsKey("UserID") || !request.ContainsKey("Names"))
  170. return FailureResult();
  171. if (!UUID.TryParse(request["UserID"].ToString(), out user))
  172. return FailureResult();
  173. if (!(request["Names"] is List<string>))
  174. return FailureResult();
  175. List<string> _names = (List<string>)request["Names"];
  176. names = _names.ToArray();
  177. if (m_AvatarService.RemoveItems(user, names))
  178. return SuccessResult();
  179. return FailureResult();
  180. }
  181. private byte[] SuccessResult()
  182. {
  183. XmlDocument doc = new XmlDocument();
  184. XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
  185. "", "");
  186. doc.AppendChild(xmlnode);
  187. XmlElement rootElement = doc.CreateElement("", "ServerResponse",
  188. "");
  189. doc.AppendChild(rootElement);
  190. XmlElement result = doc.CreateElement("", "result", "");
  191. result.AppendChild(doc.CreateTextNode("Success"));
  192. rootElement.AppendChild(result);
  193. return DocToBytes(doc);
  194. }
  195. private byte[] FailureResult()
  196. {
  197. XmlDocument doc = new XmlDocument();
  198. XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
  199. "", "");
  200. doc.AppendChild(xmlnode);
  201. XmlElement rootElement = doc.CreateElement("", "ServerResponse",
  202. "");
  203. doc.AppendChild(rootElement);
  204. XmlElement result = doc.CreateElement("", "result", "");
  205. result.AppendChild(doc.CreateTextNode("Failure"));
  206. rootElement.AppendChild(result);
  207. return DocToBytes(doc);
  208. }
  209. private byte[] DocToBytes(XmlDocument doc)
  210. {
  211. MemoryStream ms = new MemoryStream();
  212. XmlTextWriter xw = new XmlTextWriter(ms, null);
  213. xw.Formatting = Formatting.Indented;
  214. doc.WriteTo(xw);
  215. xw.Flush();
  216. return ms.ToArray();
  217. }
  218. }
  219. }