1
0

OfflineIMServiceRobustConnector.cs 7.7 KB

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