OfflineIMServiceRobustConnector.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 System;
  28. using System.Reflection;
  29. using System.Text;
  30. using System.Xml;
  31. using System.Collections.Generic;
  32. using System.IO;
  33. using Nini.Config;
  34. using OpenSim.Framework;
  35. using OpenSim.Server.Base;
  36. using OpenSim.Services.Interfaces;
  37. using OpenSim.Framework.Servers.HttpServer;
  38. using OpenSim.Server.Handlers.Base;
  39. using log4net;
  40. using OpenMetaverse;
  41. namespace OpenSim.OfflineIM
  42. {
  43. public class OfflineIMServiceRobustConnector : ServiceConnector
  44. {
  45. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  46. private IOfflineIMService m_OfflineIMService;
  47. private string m_ConfigName = "Messaging";
  48. public OfflineIMServiceRobustConnector(IConfigSource config, IHttpServer server, string configName) :
  49. base(config, server, configName)
  50. {
  51. if (configName != String.Empty)
  52. m_ConfigName = configName;
  53. m_log.DebugFormat("[OfflineIM.V2.RobustConnector]: Starting with config name {0}", m_ConfigName);
  54. m_OfflineIMService = new OfflineIMService(config);
  55. server.AddStreamHandler(new OfflineIMServicePostHandler(m_OfflineIMService));
  56. }
  57. }
  58. public class OfflineIMServicePostHandler : BaseStreamHandler
  59. {
  60. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  61. private IOfflineIMService m_OfflineIMService;
  62. public OfflineIMServicePostHandler(IOfflineIMService service) :
  63. base("POST", "/offlineim")
  64. {
  65. m_OfflineIMService = service;
  66. }
  67. protected override byte[] ProcessRequest(string path, Stream requestData,
  68. IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
  69. {
  70. StreamReader sr = new StreamReader(requestData);
  71. string body = sr.ReadToEnd();
  72. sr.Close();
  73. body = body.Trim();
  74. //m_log.DebugFormat("[XXX]: query String: {0}", body);
  75. try
  76. {
  77. Dictionary<string, object> request =
  78. ServerUtils.ParseQueryString(body);
  79. if (!request.ContainsKey("METHOD"))
  80. return FailureResult();
  81. string method = request["METHOD"].ToString();
  82. request.Remove("METHOD");
  83. m_log.DebugFormat("[OfflineIM.V2.Handler]: {0}", method);
  84. switch (method)
  85. {
  86. case "GET":
  87. return HandleGet(request);
  88. case "STORE":
  89. return HandleStore(request);
  90. }
  91. m_log.DebugFormat("[OFFLINE IM HANDLER]: unknown method request: {0}", method);
  92. }
  93. catch (Exception e)
  94. {
  95. m_log.DebugFormat("[OFFLINE IM HANDLER]: Exception {0}", e.StackTrace);
  96. }
  97. return FailureResult();
  98. }
  99. byte[] HandleStore(Dictionary<string, object> request)
  100. {
  101. Dictionary<string, object> result = new Dictionary<string, object>();
  102. GridInstantMessage im = OfflineIMDataUtils.GridInstantMessage(request);
  103. string reason = string.Empty;
  104. bool success = m_OfflineIMService.StoreMessage(im, out reason);
  105. result["RESULT"] = success.ToString();
  106. if (!success)
  107. result["REASON"] = reason;
  108. string xmlString = ServerUtils.BuildXmlResponse(result);
  109. //m_log.DebugFormat("[XXX]: resp string: {0}", xmlString);
  110. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  111. }
  112. byte[] HandleGet(Dictionary<string, object> request)
  113. {
  114. Dictionary<string, object> result = new Dictionary<string, object>();
  115. if (!request.ContainsKey("PrincipalID"))
  116. NullResult(result, "Bad network data");
  117. else
  118. {
  119. UUID principalID = new UUID(request["PrincipalID"].ToString());
  120. List<GridInstantMessage> ims = m_OfflineIMService.GetMessages(principalID);
  121. Dictionary<string, object> dict = new Dictionary<string, object>();
  122. int i = 0;
  123. foreach (GridInstantMessage m in ims)
  124. dict["im-" + i++] = OfflineIMDataUtils.GridInstantMessage(m);
  125. result["RESULT"] = dict;
  126. }
  127. string xmlString = ServerUtils.BuildXmlResponse(result);
  128. //m_log.DebugFormat("[XXX]: resp string: {0}", xmlString);
  129. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  130. }
  131. #region Helpers
  132. private void NullResult(Dictionary<string, object> result, string reason)
  133. {
  134. result["RESULT"] = "NULL";
  135. result["REASON"] = reason;
  136. }
  137. private byte[] FailureResult()
  138. {
  139. return BoolResult(false);
  140. }
  141. private byte[] SuccessResult()
  142. {
  143. return BoolResult(true);
  144. }
  145. private byte[] BoolResult(bool value)
  146. {
  147. XmlDocument doc = new XmlDocument();
  148. XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
  149. "", "");
  150. doc.AppendChild(xmlnode);
  151. XmlElement rootElement = doc.CreateElement("", "ServerResponse",
  152. "");
  153. doc.AppendChild(rootElement);
  154. XmlElement result = doc.CreateElement("", "RESULT", "");
  155. result.AppendChild(doc.CreateTextNode(value.ToString()));
  156. rootElement.AppendChild(result);
  157. return DocToBytes(doc);
  158. }
  159. private byte[] DocToBytes(XmlDocument doc)
  160. {
  161. MemoryStream ms = new MemoryStream();
  162. XmlTextWriter xw = new XmlTextWriter(ms, null);
  163. xw.Formatting = Formatting.Indented;
  164. doc.WriteTo(xw);
  165. xw.Flush();
  166. return ms.ToArray();
  167. }
  168. #endregion
  169. }
  170. }