1
0

FriendsServerPostHandler.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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.ServiceAuth;
  43. using OpenSim.Framework.Servers.HttpServer;
  44. using OpenMetaverse;
  45. namespace OpenSim.Server.Handlers.Friends
  46. {
  47. public class FriendsServerPostHandler : BaseStreamHandler
  48. {
  49. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  50. private IFriendsService m_FriendsService;
  51. public FriendsServerPostHandler(IFriendsService service, IServiceAuth auth) :
  52. base("POST", "/friends", auth)
  53. {
  54. m_FriendsService = service;
  55. }
  56. protected override byte[] ProcessRequest(string path, Stream requestData,
  57. IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
  58. {
  59. StreamReader sr = new StreamReader(requestData);
  60. string body = sr.ReadToEnd();
  61. sr.Close();
  62. body = body.Trim();
  63. //m_log.DebugFormat("[XXX]: query String: {0}", body);
  64. try
  65. {
  66. Dictionary<string, object> request =
  67. ServerUtils.ParseQueryString(body);
  68. if (!request.ContainsKey("METHOD"))
  69. return FailureResult();
  70. string method = request["METHOD"].ToString();
  71. switch (method)
  72. {
  73. case "getfriends":
  74. return GetFriends(request);
  75. case "getfriends_string":
  76. return GetFriendsString(request);
  77. case "storefriend":
  78. return StoreFriend(request);
  79. case "deletefriend":
  80. return DeleteFriend(request);
  81. case "deletefriend_string":
  82. return DeleteFriendString(request);
  83. }
  84. m_log.DebugFormat("[FRIENDS HANDLER]: unknown method request {0}", method);
  85. }
  86. catch (Exception e)
  87. {
  88. m_log.DebugFormat("[FRIENDS HANDLER]: Exception {0}", e);
  89. }
  90. return FailureResult();
  91. }
  92. #region Method-specific handlers
  93. byte[] GetFriends(Dictionary<string, object> request)
  94. {
  95. UUID principalID = UUID.Zero;
  96. if (request.ContainsKey("PRINCIPALID"))
  97. UUID.TryParse(request["PRINCIPALID"].ToString(), out principalID);
  98. else
  99. m_log.WarnFormat("[FRIENDS HANDLER]: no principalID in request to get friends");
  100. FriendInfo[] finfos = m_FriendsService.GetFriends(principalID);
  101. return PackageFriends(finfos);
  102. }
  103. byte[] GetFriendsString(Dictionary<string, object> request)
  104. {
  105. string principalID = string.Empty;
  106. if (request.ContainsKey("PRINCIPALID"))
  107. principalID = request["PRINCIPALID"].ToString();
  108. else
  109. m_log.WarnFormat("[FRIENDS HANDLER]: no principalID in request to get friends");
  110. FriendInfo[] finfos = m_FriendsService.GetFriends(principalID);
  111. return PackageFriends(finfos);
  112. }
  113. private byte[] PackageFriends(FriendInfo[] finfos)
  114. {
  115. Dictionary<string, object> result = new Dictionary<string, object>();
  116. if ((finfos == null) || ((finfos != null) && (finfos.Length == 0)))
  117. result["result"] = "null";
  118. else
  119. {
  120. int i = 0;
  121. foreach (FriendInfo finfo in finfos)
  122. {
  123. Dictionary<string, object> rinfoDict = finfo.ToKeyValuePairs();
  124. result["friend" + i] = rinfoDict;
  125. i++;
  126. }
  127. }
  128. string xmlString = ServerUtils.BuildXmlResponse(result);
  129. //m_log.DebugFormat("[FRIENDS HANDLER]: resp string: {0}", xmlString);
  130. return Util.UTF8NoBomEncoding.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 Util.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 Util.DocToBytes(doc);
  210. }
  211. void FromKeyValuePairs(Dictionary<string, object> kvp, out string principalID, out string friend, out int flags)
  212. {
  213. principalID = string.Empty;
  214. if (kvp.ContainsKey("PrincipalID") && kvp["PrincipalID"] != null)
  215. principalID = kvp["PrincipalID"].ToString();
  216. friend = string.Empty;
  217. if (kvp.ContainsKey("Friend") && kvp["Friend"] != null)
  218. friend = kvp["Friend"].ToString();
  219. flags = 0;
  220. if (kvp.ContainsKey("MyFlags") && kvp["MyFlags"] != null)
  221. Int32.TryParse(kvp["MyFlags"].ToString(), out flags);
  222. }
  223. #endregion
  224. }
  225. }