OfflineIMServiceRobustConnector.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. switch (method)
  84. {
  85. case "GET":
  86. return HandleGet(request);
  87. case "STORE":
  88. return HandleStore(request);
  89. case "DELETE":
  90. return HandleDelete(request);
  91. }
  92. m_log.DebugFormat("[OFFLINE IM HANDLER]: unknown method request: {0}", method);
  93. }
  94. catch (Exception e)
  95. {
  96. m_log.Error(string.Format("[OFFLINE IM HANDLER]: Exception {0} ", e.Message), e);
  97. }
  98. return FailureResult();
  99. }
  100. byte[] HandleStore(Dictionary<string, object> request)
  101. {
  102. Dictionary<string, object> result = new Dictionary<string, object>();
  103. GridInstantMessage im = OfflineIMDataUtils.GridInstantMessage(request);
  104. string reason = string.Empty;
  105. bool success = m_OfflineIMService.StoreMessage(im, out reason);
  106. result["RESULT"] = success.ToString();
  107. if (!success)
  108. result["REASON"] = reason;
  109. string xmlString = ServerUtils.BuildXmlResponse(result);
  110. //m_log.DebugFormat("[XXX]: resp string: {0}", xmlString);
  111. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  112. }
  113. byte[] HandleGet(Dictionary<string, object> request)
  114. {
  115. Dictionary<string, object> result = new Dictionary<string, object>();
  116. if (!request.ContainsKey("PrincipalID"))
  117. NullResult(result, "Bad network data");
  118. else
  119. {
  120. UUID principalID = new UUID(request["PrincipalID"].ToString());
  121. List<GridInstantMessage> ims = m_OfflineIMService.GetMessages(principalID);
  122. Dictionary<string, object> dict = new Dictionary<string, object>();
  123. int i = 0;
  124. foreach (GridInstantMessage m in ims)
  125. dict["im-" + i++] = OfflineIMDataUtils.GridInstantMessage(m);
  126. result["RESULT"] = dict;
  127. }
  128. string xmlString = ServerUtils.BuildXmlResponse(result);
  129. //m_log.DebugFormat("[XXX]: resp string: {0}", xmlString);
  130. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  131. }
  132. byte[] HandleDelete(Dictionary<string, object> request)
  133. {
  134. if (!request.ContainsKey("UserID"))
  135. {
  136. return FailureResult();
  137. }
  138. else
  139. {
  140. UUID userID = new UUID(request["UserID"].ToString());
  141. m_OfflineIMService.DeleteMessages(userID);
  142. return SuccessResult();
  143. }
  144. }
  145. #region Helpers
  146. private void NullResult(Dictionary<string, object> result, string reason)
  147. {
  148. result["RESULT"] = "NULL";
  149. result["REASON"] = reason;
  150. }
  151. private byte[] FailureResult()
  152. {
  153. return BoolResult(false);
  154. }
  155. private byte[] SuccessResult()
  156. {
  157. return BoolResult(true);
  158. }
  159. private byte[] BoolResult(bool value)
  160. {
  161. XmlDocument doc = new XmlDocument();
  162. XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
  163. "", "");
  164. doc.AppendChild(xmlnode);
  165. XmlElement rootElement = doc.CreateElement("", "ServerResponse",
  166. "");
  167. doc.AppendChild(rootElement);
  168. XmlElement result = doc.CreateElement("", "RESULT", "");
  169. result.AppendChild(doc.CreateTextNode(value.ToString()));
  170. rootElement.AppendChild(result);
  171. return DocToBytes(doc);
  172. }
  173. private byte[] DocToBytes(XmlDocument doc)
  174. {
  175. MemoryStream ms = new MemoryStream();
  176. XmlTextWriter xw = new XmlTextWriter(ms, null);
  177. xw.Formatting = Formatting.Indented;
  178. doc.WriteTo(xw);
  179. xw.Flush();
  180. return ms.ToArray();
  181. }
  182. #endregion
  183. }
  184. }