1
0

AvatarServerPostHandler.cs 9.2 KB

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