FriendsServerPostHandler.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 "storefriend":
  75. return StoreFriend(request);
  76. case "deletefriend":
  77. return DeleteFriend(request);
  78. }
  79. m_log.DebugFormat("[FRIENDS HANDLER]: unknown method {0} request {1}", method.Length, method);
  80. }
  81. catch (Exception e)
  82. {
  83. m_log.DebugFormat("[FRIENDS HANDLER]: Exception {0}", e);
  84. }
  85. return FailureResult();
  86. }
  87. #region Method-specific handlers
  88. byte[] GetFriends(Dictionary<string, object> request)
  89. {
  90. UUID principalID = UUID.Zero;
  91. if (request.ContainsKey("PRINCIPALID"))
  92. UUID.TryParse(request["PRINCIPALID"].ToString(), out principalID);
  93. else
  94. m_log.WarnFormat("[FRIENDS HANDLER]: no principalID in request to get friends");
  95. FriendInfo[] finfos = m_FriendsService.GetFriends(principalID);
  96. //m_log.DebugFormat("[FRIENDS HANDLER]: neighbours for region {0}: {1}", regionID, rinfos.Count);
  97. Dictionary<string, object> result = new Dictionary<string, object>();
  98. if ((finfos == null) || ((finfos != null) && (finfos.Length == 0)))
  99. result["result"] = "null";
  100. else
  101. {
  102. int i = 0;
  103. foreach (FriendInfo finfo in finfos)
  104. {
  105. Dictionary<string, object> rinfoDict = finfo.ToKeyValuePairs();
  106. result["friend" + i] = rinfoDict;
  107. i++;
  108. }
  109. }
  110. string xmlString = ServerUtils.BuildXmlResponse(result);
  111. //m_log.DebugFormat("[FRIENDS HANDLER]: resp string: {0}", xmlString);
  112. UTF8Encoding encoding = new UTF8Encoding();
  113. return encoding.GetBytes(xmlString);
  114. }
  115. byte[] StoreFriend(Dictionary<string, object> request)
  116. {
  117. FriendInfo friend = new FriendInfo(request);
  118. bool success = m_FriendsService.StoreFriend(friend.PrincipalID, friend.Friend, friend.MyFlags);
  119. if (success)
  120. return SuccessResult();
  121. else
  122. return FailureResult();
  123. }
  124. byte[] DeleteFriend(Dictionary<string, object> request)
  125. {
  126. UUID principalID = UUID.Zero;
  127. if (request.ContainsKey("PRINCIPALID"))
  128. UUID.TryParse(request["PRINCIPALID"].ToString(), out principalID);
  129. else
  130. m_log.WarnFormat("[FRIENDS HANDLER]: no principalID in request to delete friend");
  131. string friend = string.Empty;
  132. if (request.ContainsKey("FRIEND"))
  133. friend = request["FRIEND"].ToString();
  134. bool success = m_FriendsService.Delete(principalID, friend);
  135. if (success)
  136. return SuccessResult();
  137. else
  138. return FailureResult();
  139. }
  140. #endregion
  141. #region Misc
  142. private byte[] SuccessResult()
  143. {
  144. XmlDocument doc = new XmlDocument();
  145. XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
  146. "", "");
  147. doc.AppendChild(xmlnode);
  148. XmlElement rootElement = doc.CreateElement("", "ServerResponse",
  149. "");
  150. doc.AppendChild(rootElement);
  151. XmlElement result = doc.CreateElement("", "Result", "");
  152. result.AppendChild(doc.CreateTextNode("Success"));
  153. rootElement.AppendChild(result);
  154. return DocToBytes(doc);
  155. }
  156. private byte[] FailureResult()
  157. {
  158. return FailureResult(String.Empty);
  159. }
  160. private byte[] FailureResult(string msg)
  161. {
  162. XmlDocument doc = new XmlDocument();
  163. XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
  164. "", "");
  165. doc.AppendChild(xmlnode);
  166. XmlElement rootElement = doc.CreateElement("", "ServerResponse",
  167. "");
  168. doc.AppendChild(rootElement);
  169. XmlElement result = doc.CreateElement("", "Result", "");
  170. result.AppendChild(doc.CreateTextNode("Failure"));
  171. rootElement.AppendChild(result);
  172. XmlElement message = doc.CreateElement("", "Message", "");
  173. message.AppendChild(doc.CreateTextNode(msg));
  174. rootElement.AppendChild(message);
  175. return DocToBytes(doc);
  176. }
  177. private byte[] DocToBytes(XmlDocument doc)
  178. {
  179. MemoryStream ms = new MemoryStream();
  180. XmlTextWriter xw = new XmlTextWriter(ms, null);
  181. xw.Formatting = Formatting.Indented;
  182. doc.WriteTo(xw);
  183. xw.Flush();
  184. return ms.ToArray();
  185. }
  186. #endregion
  187. }
  188. }