FriendsServerPostHandler.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 FriendInfo = OpenSim.Services.Interfaces.FriendInfo;
  41. using OpenSim.Framework;
  42. using OpenSim.Framework.Servers.HttpServer;
  43. using OpenMetaverse;
  44. namespace OpenSim.Server.Handlers.Friends
  45. {
  46. public class FriendsServerPostHandler : BaseStreamHandler
  47. {
  48. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  49. private IFriendsService m_FriendsService;
  50. public FriendsServerPostHandler(IFriendsService service) :
  51. base("POST", "/friends")
  52. {
  53. m_FriendsService = service;
  54. }
  55. public override byte[] Handle(string path, Stream requestData,
  56. OSHttpRequest httpRequest, OSHttpResponse httpResponse)
  57. {
  58. StreamReader sr = new StreamReader(requestData);
  59. string body = sr.ReadToEnd();
  60. sr.Close();
  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 "getfriends":
  73. return GetFriends(request);
  74. case "getfriends_string":
  75. return GetFriendsString(request);
  76. case "storefriend":
  77. return StoreFriend(request);
  78. case "deletefriend":
  79. return DeleteFriend(request);
  80. case "deletefriend_string":
  81. return DeleteFriendString(request);
  82. }
  83. m_log.DebugFormat("[FRIENDS HANDLER]: unknown method {0} request {1}", method.Length, method);
  84. }
  85. catch (Exception e)
  86. {
  87. m_log.DebugFormat("[FRIENDS HANDLER]: Exception {0}", e);
  88. }
  89. return FailureResult();
  90. }
  91. #region Method-specific handlers
  92. byte[] GetFriends(Dictionary<string, object> request)
  93. {
  94. UUID principalID = UUID.Zero;
  95. if (request.ContainsKey("PRINCIPALID"))
  96. UUID.TryParse(request["PRINCIPALID"].ToString(), out principalID);
  97. else
  98. m_log.WarnFormat("[FRIENDS HANDLER]: no principalID in request to get friends");
  99. FriendInfo[] finfos = m_FriendsService.GetFriends(principalID);
  100. return PackageFriends(finfos);
  101. }
  102. byte[] GetFriendsString(Dictionary<string, object> request)
  103. {
  104. string principalID = string.Empty;
  105. if (request.ContainsKey("PRINCIPALID"))
  106. principalID = request["PRINCIPALID"].ToString();
  107. else
  108. m_log.WarnFormat("[FRIENDS HANDLER]: no principalID in request to get friends");
  109. FriendInfo[] finfos = m_FriendsService.GetFriends(principalID);
  110. return PackageFriends(finfos);
  111. }
  112. private byte[] PackageFriends(FriendInfo[] finfos)
  113. {
  114. Dictionary<string, object> result = new Dictionary<string, object>();
  115. if ((finfos == null) || ((finfos != null) && (finfos.Length == 0)))
  116. result["result"] = "null";
  117. else
  118. {
  119. int i = 0;
  120. foreach (FriendInfo finfo in finfos)
  121. {
  122. Dictionary<string, object> rinfoDict = finfo.ToKeyValuePairs();
  123. result["friend" + i] = rinfoDict;
  124. i++;
  125. }
  126. }
  127. string xmlString = ServerUtils.BuildXmlResponse(result);
  128. //m_log.DebugFormat("[FRIENDS HANDLER]: resp string: {0}", xmlString);
  129. UTF8Encoding encoding = new UTF8Encoding();
  130. return encoding.GetBytes(xmlString);
  131. }
  132. byte[] StoreFriend(Dictionary<string, object> request)
  133. {
  134. string principalID = string.Empty, friend = string.Empty; int flags = 0;
  135. FromKeyValuePairs(request, out principalID, out friend, out flags);
  136. bool success = m_FriendsService.StoreFriend(principalID, friend, flags);
  137. if (success)
  138. return SuccessResult();
  139. else
  140. return FailureResult();
  141. }
  142. byte[] DeleteFriend(Dictionary<string, object> request)
  143. {
  144. UUID principalID = UUID.Zero;
  145. if (request.ContainsKey("PRINCIPALID"))
  146. UUID.TryParse(request["PRINCIPALID"].ToString(), out principalID);
  147. else
  148. m_log.WarnFormat("[FRIENDS HANDLER]: no principalID in request to delete friend");
  149. string friend = string.Empty;
  150. if (request.ContainsKey("FRIEND"))
  151. friend = request["FRIEND"].ToString();
  152. bool success = m_FriendsService.Delete(principalID, friend);
  153. if (success)
  154. return SuccessResult();
  155. else
  156. return FailureResult();
  157. }
  158. byte[] DeleteFriendString(Dictionary<string, object> request)
  159. {
  160. string principalID = string.Empty;
  161. if (request.ContainsKey("PRINCIPALID"))
  162. principalID = request["PRINCIPALID"].ToString();
  163. else
  164. m_log.WarnFormat("[FRIENDS HANDLER]: no principalID in request to delete friend");
  165. string friend = string.Empty;
  166. if (request.ContainsKey("FRIEND"))
  167. friend = request["FRIEND"].ToString();
  168. bool success = m_FriendsService.Delete(principalID, friend);
  169. if (success)
  170. return SuccessResult();
  171. else
  172. return FailureResult();
  173. }
  174. #endregion
  175. #region Misc
  176. private byte[] SuccessResult()
  177. {
  178. XmlDocument doc = new XmlDocument();
  179. XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
  180. "", "");
  181. doc.AppendChild(xmlnode);
  182. XmlElement rootElement = doc.CreateElement("", "ServerResponse",
  183. "");
  184. doc.AppendChild(rootElement);
  185. XmlElement result = doc.CreateElement("", "Result", "");
  186. result.AppendChild(doc.CreateTextNode("Success"));
  187. rootElement.AppendChild(result);
  188. return DocToBytes(doc);
  189. }
  190. private byte[] FailureResult()
  191. {
  192. return FailureResult(String.Empty);
  193. }
  194. private byte[] FailureResult(string msg)
  195. {
  196. XmlDocument doc = new XmlDocument();
  197. XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
  198. "", "");
  199. doc.AppendChild(xmlnode);
  200. XmlElement rootElement = doc.CreateElement("", "ServerResponse",
  201. "");
  202. doc.AppendChild(rootElement);
  203. XmlElement result = doc.CreateElement("", "Result", "");
  204. result.AppendChild(doc.CreateTextNode("Failure"));
  205. rootElement.AppendChild(result);
  206. XmlElement message = doc.CreateElement("", "Message", "");
  207. message.AppendChild(doc.CreateTextNode(msg));
  208. rootElement.AppendChild(message);
  209. return DocToBytes(doc);
  210. }
  211. private byte[] DocToBytes(XmlDocument doc)
  212. {
  213. MemoryStream ms = new MemoryStream();
  214. XmlTextWriter xw = new XmlTextWriter(ms, null);
  215. xw.Formatting = Formatting.Indented;
  216. doc.WriteTo(xw);
  217. xw.Flush();
  218. return ms.ToArray();
  219. }
  220. void FromKeyValuePairs(Dictionary<string, object> kvp, out string principalID, out string friend, out int flags)
  221. {
  222. principalID = string.Empty;
  223. if (kvp.ContainsKey("PrincipalID") && kvp["PrincipalID"] != null)
  224. principalID = kvp["PrincipalID"].ToString();
  225. friend = string.Empty;
  226. if (kvp.ContainsKey("Friend") && kvp["Friend"] != null)
  227. friend = kvp["Friend"].ToString();
  228. flags = 0;
  229. if (kvp.ContainsKey("MyFlags") && kvp["MyFlags"] != null)
  230. Int32.TryParse(kvp["MyFlags"].ToString(), out flags);
  231. }
  232. #endregion
  233. }
  234. }