1
0

FriendsServerPostHandler.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. protected override byte[] ProcessRequest(string path, Stream requestData,
  56. IOSHttpRequest httpRequest, IOSHttpResponse 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 request {0}", 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. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  130. }
  131. byte[] StoreFriend(Dictionary<string, object> request)
  132. {
  133. string principalID = string.Empty, friend = string.Empty; int flags = 0;
  134. FromKeyValuePairs(request, out principalID, out friend, out flags);
  135. bool success = m_FriendsService.StoreFriend(principalID, friend, flags);
  136. if (success)
  137. return SuccessResult();
  138. else
  139. return FailureResult();
  140. }
  141. byte[] DeleteFriend(Dictionary<string, object> request)
  142. {
  143. UUID principalID = UUID.Zero;
  144. if (request.ContainsKey("PRINCIPALID"))
  145. UUID.TryParse(request["PRINCIPALID"].ToString(), out principalID);
  146. else
  147. m_log.WarnFormat("[FRIENDS HANDLER]: no principalID in request to delete friend");
  148. string friend = string.Empty;
  149. if (request.ContainsKey("FRIEND"))
  150. friend = request["FRIEND"].ToString();
  151. bool success = m_FriendsService.Delete(principalID, friend);
  152. if (success)
  153. return SuccessResult();
  154. else
  155. return FailureResult();
  156. }
  157. byte[] DeleteFriendString(Dictionary<string, object> request)
  158. {
  159. string principalID = string.Empty;
  160. if (request.ContainsKey("PRINCIPALID"))
  161. principalID = request["PRINCIPALID"].ToString();
  162. else
  163. m_log.WarnFormat("[FRIENDS HANDLER]: no principalID in request to delete friend");
  164. string friend = string.Empty;
  165. if (request.ContainsKey("FRIEND"))
  166. friend = request["FRIEND"].ToString();
  167. bool success = m_FriendsService.Delete(principalID, friend);
  168. if (success)
  169. return SuccessResult();
  170. else
  171. return FailureResult();
  172. }
  173. #endregion
  174. #region Misc
  175. private byte[] SuccessResult()
  176. {
  177. XmlDocument doc = new XmlDocument();
  178. XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
  179. "", "");
  180. doc.AppendChild(xmlnode);
  181. XmlElement rootElement = doc.CreateElement("", "ServerResponse",
  182. "");
  183. doc.AppendChild(rootElement);
  184. XmlElement result = doc.CreateElement("", "Result", "");
  185. result.AppendChild(doc.CreateTextNode("Success"));
  186. rootElement.AppendChild(result);
  187. return DocToBytes(doc);
  188. }
  189. private byte[] FailureResult()
  190. {
  191. return FailureResult(String.Empty);
  192. }
  193. private byte[] FailureResult(string msg)
  194. {
  195. XmlDocument doc = new XmlDocument();
  196. XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
  197. "", "");
  198. doc.AppendChild(xmlnode);
  199. XmlElement rootElement = doc.CreateElement("", "ServerResponse",
  200. "");
  201. doc.AppendChild(rootElement);
  202. XmlElement result = doc.CreateElement("", "Result", "");
  203. result.AppendChild(doc.CreateTextNode("Failure"));
  204. rootElement.AppendChild(result);
  205. XmlElement message = doc.CreateElement("", "Message", "");
  206. message.AppendChild(doc.CreateTextNode(msg));
  207. rootElement.AppendChild(message);
  208. return DocToBytes(doc);
  209. }
  210. private byte[] DocToBytes(XmlDocument doc)
  211. {
  212. MemoryStream ms = new MemoryStream();
  213. XmlTextWriter xw = new XmlTextWriter(ms, null);
  214. xw.Formatting = Formatting.Indented;
  215. doc.WriteTo(xw);
  216. xw.Flush();
  217. return ms.ToArray();
  218. }
  219. void FromKeyValuePairs(Dictionary<string, object> kvp, out string principalID, out string friend, out int flags)
  220. {
  221. principalID = string.Empty;
  222. if (kvp.ContainsKey("PrincipalID") && kvp["PrincipalID"] != null)
  223. principalID = kvp["PrincipalID"].ToString();
  224. friend = string.Empty;
  225. if (kvp.ContainsKey("Friend") && kvp["Friend"] != null)
  226. friend = kvp["Friend"].ToString();
  227. flags = 0;
  228. if (kvp.ContainsKey("MyFlags") && kvp["MyFlags"] != null)
  229. Int32.TryParse(kvp["MyFlags"].ToString(), out flags);
  230. }
  231. #endregion
  232. }
  233. }