HGGroupsServiceRobustConnector.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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.Groups
  42. {
  43. public class HGGroupsServiceRobustConnector : ServiceConnector
  44. {
  45. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  46. private HGGroupsService m_GroupsService;
  47. private string m_ConfigName = "Groups";
  48. // Called by Robust shell
  49. public HGGroupsServiceRobustConnector(IConfigSource config, IHttpServer server, string configName) :
  50. this(config, server, configName, null, null)
  51. {
  52. }
  53. // Called by the sim-bound module
  54. public HGGroupsServiceRobustConnector(IConfigSource config, IHttpServer server, string configName, IOfflineIMService im, IUserAccountService users) :
  55. base(config, server, configName)
  56. {
  57. if (configName != String.Empty)
  58. m_ConfigName = configName;
  59. m_log.DebugFormat("[Groups.RobustHGConnector]: Starting with config name {0}", m_ConfigName);
  60. string homeURI = Util.GetConfigVarFromSections<string>(config, "HomeURI",
  61. new string[] { "Startup", "Hypergrid", m_ConfigName}, string.Empty);
  62. if (homeURI == string.Empty)
  63. throw new Exception(String.Format("[Groups.RobustHGConnector]: please provide the HomeURI [Startup] or in section {0}", m_ConfigName));
  64. IConfig cnf = config.Configs[m_ConfigName];
  65. if (cnf == null)
  66. throw new Exception(String.Format("[Groups.RobustHGConnector]: {0} section does not exist", m_ConfigName));
  67. if (im == null)
  68. {
  69. string imDll = cnf.GetString("OfflineIMService", string.Empty);
  70. if (imDll == string.Empty)
  71. throw new Exception(String.Format("[Groups.RobustHGConnector]: please provide OfflineIMService in section {0}", m_ConfigName));
  72. Object[] args = new Object[] { config };
  73. im = ServerUtils.LoadPlugin<IOfflineIMService>(imDll, args);
  74. }
  75. if (users == null)
  76. {
  77. string usersDll = cnf.GetString("UserAccountService", string.Empty);
  78. if (usersDll == string.Empty)
  79. throw new Exception(String.Format("[Groups.RobustHGConnector]: please provide UserAccountService in section {0}", m_ConfigName));
  80. Object[] args = new Object[] { config };
  81. users = ServerUtils.LoadPlugin<IUserAccountService>(usersDll, args);
  82. }
  83. m_GroupsService = new HGGroupsService(config, im, users, homeURI);
  84. server.AddStreamHandler(new HGGroupsServicePostHandler(m_GroupsService));
  85. }
  86. }
  87. public class HGGroupsServicePostHandler : BaseStreamHandler
  88. {
  89. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  90. private HGGroupsService m_GroupsService;
  91. public HGGroupsServicePostHandler(HGGroupsService service) :
  92. base("POST", "/hg-groups")
  93. {
  94. m_GroupsService = service;
  95. }
  96. protected override byte[] ProcessRequest(string path, Stream requestData,
  97. IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
  98. {
  99. StreamReader sr = new StreamReader(requestData);
  100. string body = sr.ReadToEnd();
  101. sr.Close();
  102. body = body.Trim();
  103. //m_log.DebugFormat("[XXX]: query String: {0}", body);
  104. try
  105. {
  106. Dictionary<string, object> request =
  107. ServerUtils.ParseQueryString(body);
  108. if (!request.ContainsKey("METHOD"))
  109. return FailureResult();
  110. string method = request["METHOD"].ToString();
  111. request.Remove("METHOD");
  112. m_log.DebugFormat("[Groups.RobustHGConnector]: {0}", method);
  113. switch (method)
  114. {
  115. case "POSTGROUP":
  116. return HandleAddGroupProxy(request);
  117. case "REMOVEAGENTFROMGROUP":
  118. return HandleRemoveAgentFromGroup(request);
  119. case "GETGROUP":
  120. return HandleGetGroup(request);
  121. case "ADDNOTICE":
  122. return HandleAddNotice(request);
  123. case "VERIFYNOTICE":
  124. return HandleVerifyNotice(request);
  125. case "GETGROUPMEMBERS":
  126. return HandleGetGroupMembers(request);
  127. case "GETGROUPROLES":
  128. return HandleGetGroupRoles(request);
  129. case "GETROLEMEMBERS":
  130. return HandleGetRoleMembers(request);
  131. }
  132. m_log.DebugFormat("[Groups.RobustHGConnector]: unknown method request: {0}", method);
  133. }
  134. catch (Exception e)
  135. {
  136. m_log.DebugFormat("[Groups.RobustHGConnector]: Exception {0}", e.StackTrace);
  137. }
  138. return FailureResult();
  139. }
  140. byte[] HandleAddGroupProxy(Dictionary<string, object> request)
  141. {
  142. Dictionary<string, object> result = new Dictionary<string, object>();
  143. if (!request.ContainsKey("RequestingAgentID") || !request.ContainsKey("GroupID")
  144. || !request.ContainsKey("AgentID")
  145. || !request.ContainsKey("AccessToken") || !request.ContainsKey("Location"))
  146. NullResult(result, "Bad network data");
  147. else
  148. {
  149. string RequestingAgentID = request["RequestingAgentID"].ToString();
  150. string agentID = request["AgentID"].ToString();
  151. UUID groupID = new UUID(request["GroupID"].ToString());
  152. string accessToken = request["AccessToken"].ToString();
  153. string location = request["Location"].ToString();
  154. string name = string.Empty;
  155. if (request.ContainsKey("Name"))
  156. name = request["Name"].ToString();
  157. string reason = string.Empty;
  158. bool success = m_GroupsService.CreateGroupProxy(RequestingAgentID, agentID, accessToken, groupID, location, name, out reason);
  159. result["REASON"] = reason;
  160. result["RESULT"] = success.ToString();
  161. }
  162. string xmlString = ServerUtils.BuildXmlResponse(result);
  163. //m_log.DebugFormat("[XXX]: resp string: {0}", xmlString);
  164. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  165. }
  166. byte[] HandleRemoveAgentFromGroup(Dictionary<string, object> request)
  167. {
  168. Dictionary<string, object> result = new Dictionary<string, object>();
  169. if (!request.ContainsKey("AccessToken") || !request.ContainsKey("AgentID") ||
  170. !request.ContainsKey("GroupID"))
  171. NullResult(result, "Bad network data");
  172. else
  173. {
  174. UUID groupID = new UUID(request["GroupID"].ToString());
  175. string agentID = request["AgentID"].ToString();
  176. string token = request["AccessToken"].ToString();
  177. m_GroupsService.RemoveAgentFromGroup(agentID, agentID, groupID, token);
  178. }
  179. //m_log.DebugFormat("[XXX]: resp string: {0}", xmlString);
  180. result["RESULT"] = "true";
  181. return Util.UTF8NoBomEncoding.GetBytes(ServerUtils.BuildXmlResponse(result));
  182. }
  183. byte[] HandleGetGroup(Dictionary<string, object> request)
  184. {
  185. Dictionary<string, object> result = new Dictionary<string, object>();
  186. if (!request.ContainsKey("RequestingAgentID") || !request.ContainsKey("AccessToken"))
  187. NullResult(result, "Bad network data");
  188. else
  189. {
  190. string RequestingAgentID = request["RequestingAgentID"].ToString();
  191. string token = request["AccessToken"].ToString();
  192. UUID groupID = UUID.Zero;
  193. string groupName = string.Empty;
  194. if (request.ContainsKey("GroupID"))
  195. groupID = new UUID(request["GroupID"].ToString());
  196. if (request.ContainsKey("Name"))
  197. groupName = request["Name"].ToString();
  198. ExtendedGroupRecord grec = m_GroupsService.GetGroupRecord(RequestingAgentID, groupID, groupName, token);
  199. if (grec == null)
  200. NullResult(result, "Group not found");
  201. else
  202. result["RESULT"] = GroupsDataUtils.GroupRecord(grec);
  203. }
  204. string xmlString = ServerUtils.BuildXmlResponse(result);
  205. //m_log.DebugFormat("[XXX]: resp string: {0}", xmlString);
  206. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  207. }
  208. byte[] HandleGetGroupMembers(Dictionary<string, object> request)
  209. {
  210. Dictionary<string, object> result = new Dictionary<string, object>();
  211. if (!request.ContainsKey("RequestingAgentID") || !request.ContainsKey("GroupID") || !request.ContainsKey("AccessToken"))
  212. NullResult(result, "Bad network data");
  213. else
  214. {
  215. UUID groupID = new UUID(request["GroupID"].ToString());
  216. string requestingAgentID = request["RequestingAgentID"].ToString();
  217. string token = request["AccessToken"].ToString();
  218. List<ExtendedGroupMembersData> members = m_GroupsService.GetGroupMembers(requestingAgentID, groupID, token);
  219. if (members == null || (members != null && members.Count == 0))
  220. {
  221. NullResult(result, "No members");
  222. }
  223. else
  224. {
  225. Dictionary<string, object> dict = new Dictionary<string, object>();
  226. int i = 0;
  227. foreach (ExtendedGroupMembersData m in members)
  228. {
  229. dict["m-" + i++] = GroupsDataUtils.GroupMembersData(m);
  230. }
  231. result["RESULT"] = dict;
  232. }
  233. }
  234. string xmlString = ServerUtils.BuildXmlResponse(result);
  235. //m_log.DebugFormat("[XXX]: resp string: {0}", xmlString);
  236. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  237. }
  238. byte[] HandleGetGroupRoles(Dictionary<string, object> request)
  239. {
  240. Dictionary<string, object> result = new Dictionary<string, object>();
  241. if (!request.ContainsKey("RequestingAgentID") || !request.ContainsKey("GroupID") || !request.ContainsKey("AccessToken"))
  242. NullResult(result, "Bad network data");
  243. else
  244. {
  245. UUID groupID = new UUID(request["GroupID"].ToString());
  246. string requestingAgentID = request["RequestingAgentID"].ToString();
  247. string token = request["AccessToken"].ToString();
  248. List<GroupRolesData> roles = m_GroupsService.GetGroupRoles(requestingAgentID, groupID, token);
  249. if (roles == null || (roles != null && roles.Count == 0))
  250. {
  251. NullResult(result, "No members");
  252. }
  253. else
  254. {
  255. Dictionary<string, object> dict = new Dictionary<string, object>();
  256. int i = 0;
  257. foreach (GroupRolesData r in roles)
  258. dict["r-" + i++] = GroupsDataUtils.GroupRolesData(r);
  259. result["RESULT"] = dict;
  260. }
  261. }
  262. string xmlString = ServerUtils.BuildXmlResponse(result);
  263. //m_log.DebugFormat("[XXX]: resp string: {0}", xmlString);
  264. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  265. }
  266. byte[] HandleGetRoleMembers(Dictionary<string, object> request)
  267. {
  268. Dictionary<string, object> result = new Dictionary<string, object>();
  269. if (!request.ContainsKey("RequestingAgentID") || !request.ContainsKey("GroupID") || !request.ContainsKey("AccessToken"))
  270. NullResult(result, "Bad network data");
  271. else
  272. {
  273. UUID groupID = new UUID(request["GroupID"].ToString());
  274. string requestingAgentID = request["RequestingAgentID"].ToString();
  275. string token = request["AccessToken"].ToString();
  276. List<ExtendedGroupRoleMembersData> rmembers = m_GroupsService.GetGroupRoleMembers(requestingAgentID, groupID, token);
  277. if (rmembers == null || (rmembers != null && rmembers.Count == 0))
  278. {
  279. NullResult(result, "No members");
  280. }
  281. else
  282. {
  283. Dictionary<string, object> dict = new Dictionary<string, object>();
  284. int i = 0;
  285. foreach (ExtendedGroupRoleMembersData rm in rmembers)
  286. dict["rm-" + i++] = GroupsDataUtils.GroupRoleMembersData(rm);
  287. result["RESULT"] = dict;
  288. }
  289. }
  290. string xmlString = ServerUtils.BuildXmlResponse(result);
  291. //m_log.DebugFormat("[XXX]: resp string: {0}", xmlString);
  292. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  293. }
  294. byte[] HandleAddNotice(Dictionary<string, object> request)
  295. {
  296. Dictionary<string, object> result = new Dictionary<string, object>();
  297. if (!request.ContainsKey("RequestingAgentID") || !request.ContainsKey("GroupID") || !request.ContainsKey("NoticeID") ||
  298. !request.ContainsKey("FromName") || !request.ContainsKey("Subject") || !request.ContainsKey("Message") ||
  299. !request.ContainsKey("HasAttachment"))
  300. NullResult(result, "Bad network data");
  301. else
  302. {
  303. bool hasAtt = bool.Parse(request["HasAttachment"].ToString());
  304. byte attType = 0;
  305. string attName = string.Empty;
  306. string attOwner = string.Empty;
  307. UUID attItem = UUID.Zero;
  308. if (request.ContainsKey("AttachmentType"))
  309. attType = byte.Parse(request["AttachmentType"].ToString());
  310. if (request.ContainsKey("AttachmentName"))
  311. attName = request["AttachmentType"].ToString();
  312. if (request.ContainsKey("AttachmentItemID"))
  313. attItem = new UUID(request["AttachmentItemID"].ToString());
  314. if (request.ContainsKey("AttachmentOwnerID"))
  315. attOwner = request["AttachmentOwnerID"].ToString();
  316. bool success = m_GroupsService.AddNotice(request["RequestingAgentID"].ToString(), new UUID(request["GroupID"].ToString()),
  317. new UUID(request["NoticeID"].ToString()), request["FromName"].ToString(), request["Subject"].ToString(),
  318. request["Message"].ToString(), hasAtt, attType, attName, attItem, attOwner);
  319. result["RESULT"] = success.ToString();
  320. }
  321. string xmlString = ServerUtils.BuildXmlResponse(result);
  322. //m_log.DebugFormat("[XXX]: resp string: {0}", xmlString);
  323. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  324. }
  325. byte[] HandleVerifyNotice(Dictionary<string, object> request)
  326. {
  327. Dictionary<string, object> result = new Dictionary<string, object>();
  328. if (!request.ContainsKey("NoticeID") || !request.ContainsKey("GroupID"))
  329. NullResult(result, "Bad network data");
  330. else
  331. {
  332. UUID noticeID = new UUID(request["NoticeID"].ToString());
  333. UUID groupID = new UUID(request["GroupID"].ToString());
  334. bool success = m_GroupsService.VerifyNotice(noticeID, groupID);
  335. //m_log.DebugFormat("[XXX]: VerifyNotice returned {0}", success);
  336. result["RESULT"] = success.ToString();
  337. }
  338. string xmlString = ServerUtils.BuildXmlResponse(result);
  339. //m_log.DebugFormat("[XXX]: resp string: {0}", xmlString);
  340. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  341. }
  342. //
  343. //
  344. //
  345. //
  346. //
  347. #region Helpers
  348. private void NullResult(Dictionary<string, object> result, string reason)
  349. {
  350. result["RESULT"] = "NULL";
  351. result["REASON"] = reason;
  352. }
  353. private byte[] FailureResult()
  354. {
  355. Dictionary<string, object> result = new Dictionary<string, object>();
  356. NullResult(result, "Unknown method");
  357. string xmlString = ServerUtils.BuildXmlResponse(result);
  358. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  359. }
  360. #endregion
  361. }
  362. }