PresenceServerPostHandler.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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.Servers.HttpServer;
  42. using OpenMetaverse;
  43. namespace OpenSim.Server.Handlers.Presence
  44. {
  45. public class PresenceServerPostHandler : BaseStreamHandler
  46. {
  47. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  48. private IPresenceService m_PresenceService;
  49. public PresenceServerPostHandler(IPresenceService service) :
  50. base("POST", "/presence")
  51. {
  52. m_PresenceService = service;
  53. }
  54. public override byte[] Handle(string path, Stream requestData,
  55. OSHttpRequest httpRequest, OSHttpResponse httpResponse)
  56. {
  57. StreamReader sr = new StreamReader(requestData);
  58. string body = sr.ReadToEnd();
  59. sr.Close();
  60. body = body.Trim();
  61. //m_log.DebugFormat("[XXX]: query String: {0}", body);
  62. try
  63. {
  64. Dictionary<string, object> request =
  65. ServerUtils.ParseQueryString(body);
  66. if (!request.ContainsKey("METHOD"))
  67. return FailureResult();
  68. string method = request["METHOD"].ToString();
  69. switch (method)
  70. {
  71. case "report":
  72. return Report(request);
  73. }
  74. m_log.DebugFormat("[PRESENCE HANDLER]: unknown method request: {0}", method);
  75. }
  76. catch (Exception e)
  77. {
  78. m_log.Debug("[PRESENCE HANDLER]: Exception {0}" + e);
  79. }
  80. return FailureResult();
  81. }
  82. byte[] Report(Dictionary<string, object> request)
  83. {
  84. PresenceInfo info = new PresenceInfo();
  85. info.Data = new Dictionary<string, string>();
  86. if (!request.ContainsKey("PrincipalID") || !request.ContainsKey("RegionID"))
  87. return FailureResult();
  88. if (!UUID.TryParse(request["PrincipalID"].ToString(),
  89. out info.PrincipalID))
  90. return FailureResult();
  91. if (!UUID.TryParse(request["RegionID"].ToString(),
  92. out info.RegionID))
  93. return FailureResult();
  94. foreach (KeyValuePair<string, object> kvp in request)
  95. {
  96. if (kvp.Key == "METHOD" ||
  97. kvp.Key == "PrincipalID" ||
  98. kvp.Key == "RegionID")
  99. continue;
  100. info.Data[kvp.Key] = kvp.Value.ToString();
  101. }
  102. if (m_PresenceService.Report(info))
  103. return SuccessResult();
  104. return FailureResult();
  105. }
  106. private byte[] SuccessResult()
  107. {
  108. XmlDocument doc = new XmlDocument();
  109. XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
  110. "", "");
  111. doc.AppendChild(xmlnode);
  112. XmlElement rootElement = doc.CreateElement("", "ServerResponse",
  113. "");
  114. doc.AppendChild(rootElement);
  115. XmlElement result = doc.CreateElement("", "Result", "");
  116. result.AppendChild(doc.CreateTextNode("Success"));
  117. rootElement.AppendChild(result);
  118. return DocToBytes(doc);
  119. }
  120. private byte[] FailureResult()
  121. {
  122. XmlDocument doc = new XmlDocument();
  123. XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
  124. "", "");
  125. doc.AppendChild(xmlnode);
  126. XmlElement rootElement = doc.CreateElement("", "ServerResponse",
  127. "");
  128. doc.AppendChild(rootElement);
  129. XmlElement result = doc.CreateElement("", "Result", "");
  130. result.AppendChild(doc.CreateTextNode("Failure"));
  131. rootElement.AppendChild(result);
  132. return DocToBytes(doc);
  133. }
  134. private byte[] DocToBytes(XmlDocument doc)
  135. {
  136. MemoryStream ms = new MemoryStream();
  137. XmlTextWriter xw = new XmlTextWriter(ms, null);
  138. xw.Formatting = Formatting.Indented;
  139. doc.WriteTo(xw);
  140. xw.Flush();
  141. return ms.ToArray();
  142. }
  143. }
  144. }