HGGroupsServiceRobustConnector.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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.Length == 0)
  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.Length == 0)
  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.Length == 0)
  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. string body;
  100. using(StreamReader sr = new StreamReader(requestData))
  101. body = sr.ReadToEnd();
  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.Error(string.Format("[Groups.RobustHGConnector]: Exception {0} ", e.Message), e);
  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. if (!m_GroupsService.RemoveAgentFromGroup(agentID, agentID, groupID, token))
  178. NullResult(result, "Internal error");
  179. else
  180. result["RESULT"] = "true";
  181. }
  182. //m_log.DebugFormat("[XXX]: resp string: {0}", xmlString);
  183. return Util.UTF8NoBomEncoding.GetBytes(ServerUtils.BuildXmlResponse(result));
  184. }
  185. byte[] HandleGetGroup(Dictionary<string, object> request)
  186. {
  187. Dictionary<string, object> result = new Dictionary<string, object>();
  188. if (!request.ContainsKey("RequestingAgentID") || !request.ContainsKey("AccessToken"))
  189. NullResult(result, "Bad network data");
  190. else
  191. {
  192. string RequestingAgentID = request["RequestingAgentID"].ToString();
  193. string token = request["AccessToken"].ToString();
  194. UUID groupID = UUID.Zero;
  195. string groupName = string.Empty;
  196. if (request.ContainsKey("GroupID"))
  197. groupID = new UUID(request["GroupID"].ToString());
  198. if (request.ContainsKey("Name"))
  199. groupName = request["Name"].ToString();
  200. ExtendedGroupRecord grec = m_GroupsService.GetGroupRecord(RequestingAgentID, groupID, groupName, token);
  201. if (grec == null)
  202. NullResult(result, "Group not found");
  203. else
  204. result["RESULT"] = GroupsDataUtils.GroupRecord(grec);
  205. }
  206. string xmlString = ServerUtils.BuildXmlResponse(result);
  207. //m_log.DebugFormat("[XXX]: resp string: {0}", xmlString);
  208. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  209. }
  210. byte[] HandleGetGroupMembers(Dictionary<string, object> request)
  211. {
  212. Dictionary<string, object> result = new Dictionary<string, object>();
  213. if (!request.ContainsKey("RequestingAgentID") || !request.ContainsKey("GroupID") || !request.ContainsKey("AccessToken"))
  214. NullResult(result, "Bad network data");
  215. else
  216. {
  217. UUID groupID = new UUID(request["GroupID"].ToString());
  218. string requestingAgentID = request["RequestingAgentID"].ToString();
  219. string token = request["AccessToken"].ToString();
  220. List<ExtendedGroupMembersData> members = m_GroupsService.GetGroupMembers(requestingAgentID, groupID, token);
  221. if (members == null || (members != null && members.Count == 0))
  222. {
  223. NullResult(result, "No members");
  224. }
  225. else
  226. {
  227. Dictionary<string, object> dict = new Dictionary<string, object>();
  228. int i = 0;
  229. foreach (ExtendedGroupMembersData m in members)
  230. {
  231. dict["m-" + i++] = GroupsDataUtils.GroupMembersData(m);
  232. }
  233. result["RESULT"] = dict;
  234. }
  235. }
  236. string xmlString = ServerUtils.BuildXmlResponse(result);
  237. //m_log.DebugFormat("[XXX]: resp string: {0}", xmlString);
  238. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  239. }
  240. byte[] HandleGetGroupRoles(Dictionary<string, object> request)
  241. {
  242. Dictionary<string, object> result = new Dictionary<string, object>();
  243. if (!request.ContainsKey("RequestingAgentID") || !request.ContainsKey("GroupID") || !request.ContainsKey("AccessToken"))
  244. NullResult(result, "Bad network data");
  245. else
  246. {
  247. UUID groupID = new UUID(request["GroupID"].ToString());
  248. string requestingAgentID = request["RequestingAgentID"].ToString();
  249. string token = request["AccessToken"].ToString();
  250. List<GroupRolesData> roles = m_GroupsService.GetGroupRoles(requestingAgentID, groupID, token);
  251. if (roles == null || (roles != null && roles.Count == 0))
  252. {
  253. NullResult(result, "No members");
  254. }
  255. else
  256. {
  257. Dictionary<string, object> dict = new Dictionary<string, object>();
  258. int i = 0;
  259. foreach (GroupRolesData r in roles)
  260. dict["r-" + i++] = GroupsDataUtils.GroupRolesData(r);
  261. result["RESULT"] = dict;
  262. }
  263. }
  264. string xmlString = ServerUtils.BuildXmlResponse(result);
  265. //m_log.DebugFormat("[XXX]: resp string: {0}", xmlString);
  266. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  267. }
  268. byte[] HandleGetRoleMembers(Dictionary<string, object> request)
  269. {
  270. Dictionary<string, object> result = new Dictionary<string, object>();
  271. if (!request.ContainsKey("RequestingAgentID") || !request.ContainsKey("GroupID") || !request.ContainsKey("AccessToken"))
  272. NullResult(result, "Bad network data");
  273. else
  274. {
  275. UUID groupID = new UUID(request["GroupID"].ToString());
  276. string requestingAgentID = request["RequestingAgentID"].ToString();
  277. string token = request["AccessToken"].ToString();
  278. List<ExtendedGroupRoleMembersData> rmembers = m_GroupsService.GetGroupRoleMembers(requestingAgentID, groupID, token);
  279. if (rmembers == null || (rmembers != null && rmembers.Count == 0))
  280. {
  281. NullResult(result, "No members");
  282. }
  283. else
  284. {
  285. Dictionary<string, object> dict = new Dictionary<string, object>();
  286. int i = 0;
  287. foreach (ExtendedGroupRoleMembersData rm in rmembers)
  288. dict["rm-" + i++] = GroupsDataUtils.GroupRoleMembersData(rm);
  289. result["RESULT"] = dict;
  290. }
  291. }
  292. string xmlString = ServerUtils.BuildXmlResponse(result);
  293. //m_log.DebugFormat("[XXX]: resp string: {0}", xmlString);
  294. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  295. }
  296. byte[] HandleAddNotice(Dictionary<string, object> request)
  297. {
  298. Dictionary<string, object> result = new Dictionary<string, object>();
  299. if (!request.ContainsKey("RequestingAgentID") || !request.ContainsKey("GroupID") || !request.ContainsKey("NoticeID") ||
  300. !request.ContainsKey("FromName") || !request.ContainsKey("Subject") || !request.ContainsKey("Message") ||
  301. !request.ContainsKey("HasAttachment"))
  302. NullResult(result, "Bad network data");
  303. else
  304. {
  305. bool hasAtt = bool.Parse(request["HasAttachment"].ToString());
  306. byte attType = 0;
  307. string attName = string.Empty;
  308. string attOwner = string.Empty;
  309. UUID attItem = UUID.Zero;
  310. if (request.ContainsKey("AttachmentType"))
  311. attType = byte.Parse(request["AttachmentType"].ToString());
  312. if (request.ContainsKey("AttachmentName"))
  313. attName = request["AttachmentType"].ToString();
  314. if (request.ContainsKey("AttachmentItemID"))
  315. attItem = new UUID(request["AttachmentItemID"].ToString());
  316. if (request.ContainsKey("AttachmentOwnerID"))
  317. attOwner = request["AttachmentOwnerID"].ToString();
  318. bool success = m_GroupsService.AddNotice(request["RequestingAgentID"].ToString(), new UUID(request["GroupID"].ToString()),
  319. new UUID(request["NoticeID"].ToString()), request["FromName"].ToString(), request["Subject"].ToString(),
  320. request["Message"].ToString(), hasAtt, attType, attName, attItem, attOwner);
  321. result["RESULT"] = success.ToString();
  322. }
  323. string xmlString = ServerUtils.BuildXmlResponse(result);
  324. //m_log.DebugFormat("[XXX]: resp string: {0}", xmlString);
  325. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  326. }
  327. byte[] HandleVerifyNotice(Dictionary<string, object> request)
  328. {
  329. Dictionary<string, object> result = new Dictionary<string, object>();
  330. if (!request.ContainsKey("NoticeID") || !request.ContainsKey("GroupID"))
  331. NullResult(result, "Bad network data");
  332. else
  333. {
  334. UUID noticeID = new UUID(request["NoticeID"].ToString());
  335. UUID groupID = new UUID(request["GroupID"].ToString());
  336. bool success = m_GroupsService.VerifyNotice(noticeID, groupID);
  337. //m_log.DebugFormat("[XXX]: VerifyNotice returned {0}", success);
  338. result["RESULT"] = success.ToString();
  339. }
  340. string xmlString = ServerUtils.BuildXmlResponse(result);
  341. //m_log.DebugFormat("[XXX]: resp string: {0}", xmlString);
  342. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  343. }
  344. //
  345. //
  346. //
  347. //
  348. //
  349. #region Helpers
  350. private void NullResult(Dictionary<string, object> result, string reason)
  351. {
  352. result["RESULT"] = "NULL";
  353. result["REASON"] = reason;
  354. }
  355. private byte[] FailureResult()
  356. {
  357. Dictionary<string, object> result = new Dictionary<string, object>();
  358. NullResult(result, "Unknown method");
  359. string xmlString = ServerUtils.BuildXmlResponse(result);
  360. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  361. }
  362. #endregion
  363. }
  364. }