diff options
Diffstat (limited to 'OpenSim/Addons/Groups/Hypergrid/HGGroupsServiceRobustConnector.cs')
-rw-r--r-- | OpenSim/Addons/Groups/Hypergrid/HGGroupsServiceRobustConnector.cs | 444 |
1 files changed, 444 insertions, 0 deletions
diff --git a/OpenSim/Addons/Groups/Hypergrid/HGGroupsServiceRobustConnector.cs b/OpenSim/Addons/Groups/Hypergrid/HGGroupsServiceRobustConnector.cs new file mode 100644 index 0000000..3584f78 --- /dev/null +++ b/OpenSim/Addons/Groups/Hypergrid/HGGroupsServiceRobustConnector.cs | |||
@@ -0,0 +1,444 @@ | |||
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 | |||
28 | using System; | ||
29 | using System.Reflection; | ||
30 | using System.Text; | ||
31 | using System.Xml; | ||
32 | using System.Collections.Generic; | ||
33 | using System.IO; | ||
34 | using Nini.Config; | ||
35 | using OpenSim.Framework; | ||
36 | using OpenSim.Server.Base; | ||
37 | using OpenSim.Services.Interfaces; | ||
38 | using OpenSim.Framework.Servers.HttpServer; | ||
39 | using OpenSim.Server.Handlers.Base; | ||
40 | using log4net; | ||
41 | using OpenMetaverse; | ||
42 | |||
43 | namespace OpenSim.Groups | ||
44 | { | ||
45 | public class HGGroupsServiceRobustConnector : ServiceConnector | ||
46 | { | ||
47 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
48 | |||
49 | private HGGroupsService m_GroupsService; | ||
50 | private string m_HomeURI = string.Empty; | ||
51 | private string m_ConfigName = "Groups"; | ||
52 | |||
53 | // Called by Robust shell | ||
54 | public HGGroupsServiceRobustConnector(IConfigSource config, IHttpServer server, string configName) : | ||
55 | this(config, server, configName, null, null) | ||
56 | { | ||
57 | } | ||
58 | |||
59 | // Called by the sim-bound module | ||
60 | public HGGroupsServiceRobustConnector(IConfigSource config, IHttpServer server, string configName, IOfflineIMService im, IUserAccountService users) : | ||
61 | base(config, server, configName) | ||
62 | { | ||
63 | if (configName != String.Empty) | ||
64 | m_ConfigName = configName; | ||
65 | |||
66 | m_log.DebugFormat("[Groups.RobustHGConnector]: Starting with config name {0}", m_ConfigName); | ||
67 | |||
68 | string homeURI = Util.GetConfigVarFromSections<string>(config, "HomeURI", | ||
69 | new string[] { "Startup", "Hypergrid", m_ConfigName}, string.Empty); | ||
70 | if (homeURI == string.Empty) | ||
71 | throw new Exception(String.Format("[Groups.RobustHGConnector]: please provide the HomeURI [Startup] or in section {0}", m_ConfigName)); | ||
72 | |||
73 | IConfig cnf = config.Configs[m_ConfigName]; | ||
74 | if (cnf == null) | ||
75 | throw new Exception(String.Format("[Groups.RobustHGConnector]: {0} section does not exist", m_ConfigName)); | ||
76 | |||
77 | if (im == null) | ||
78 | { | ||
79 | string imDll = cnf.GetString("OfflineIMService", string.Empty); | ||
80 | if (imDll == string.Empty) | ||
81 | throw new Exception(String.Format("[Groups.RobustHGConnector]: please provide OfflineIMService in section {0}", m_ConfigName)); | ||
82 | |||
83 | Object[] args = new Object[] { config }; | ||
84 | im = ServerUtils.LoadPlugin<IOfflineIMService>(imDll, args); | ||
85 | } | ||
86 | |||
87 | if (users == null) | ||
88 | { | ||
89 | string usersDll = cnf.GetString("UserAccountService", string.Empty); | ||
90 | if (usersDll == string.Empty) | ||
91 | throw new Exception(String.Format("[Groups.RobustHGConnector]: please provide UserAccountService in section {0}", m_ConfigName)); | ||
92 | |||
93 | Object[] args = new Object[] { config }; | ||
94 | users = ServerUtils.LoadPlugin<IUserAccountService>(usersDll, args); | ||
95 | } | ||
96 | |||
97 | m_GroupsService = new HGGroupsService(config, im, users, homeURI); | ||
98 | |||
99 | server.AddStreamHandler(new HGGroupsServicePostHandler(m_GroupsService)); | ||
100 | } | ||
101 | |||
102 | } | ||
103 | |||
104 | public class HGGroupsServicePostHandler : BaseStreamHandler | ||
105 | { | ||
106 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
107 | |||
108 | private HGGroupsService m_GroupsService; | ||
109 | |||
110 | public HGGroupsServicePostHandler(HGGroupsService service) : | ||
111 | base("POST", "/hg-groups") | ||
112 | { | ||
113 | m_GroupsService = service; | ||
114 | } | ||
115 | |||
116 | public override byte[] Handle(string path, Stream requestData, | ||
117 | IOSHttpRequest httpRequest, IOSHttpResponse httpResponse) | ||
118 | { | ||
119 | StreamReader sr = new StreamReader(requestData); | ||
120 | string body = sr.ReadToEnd(); | ||
121 | sr.Close(); | ||
122 | body = body.Trim(); | ||
123 | |||
124 | //m_log.DebugFormat("[XXX]: query String: {0}", body); | ||
125 | |||
126 | try | ||
127 | { | ||
128 | Dictionary<string, object> request = | ||
129 | ServerUtils.ParseQueryString(body); | ||
130 | |||
131 | if (!request.ContainsKey("METHOD")) | ||
132 | return FailureResult(); | ||
133 | |||
134 | string method = request["METHOD"].ToString(); | ||
135 | request.Remove("METHOD"); | ||
136 | |||
137 | m_log.DebugFormat("[Groups.RobustHGConnector]: {0}", method); | ||
138 | switch (method) | ||
139 | { | ||
140 | case "POSTGROUP": | ||
141 | return HandleAddGroupProxy(request); | ||
142 | case "REMOVEAGENTFROMGROUP": | ||
143 | return HandleRemoveAgentFromGroup(request); | ||
144 | case "GETGROUP": | ||
145 | return HandleGetGroup(request); | ||
146 | case "ADDNOTICE": | ||
147 | return HandleAddNotice(request); | ||
148 | case "VERIFYNOTICE": | ||
149 | return HandleVerifyNotice(request); | ||
150 | case "GETGROUPMEMBERS": | ||
151 | return HandleGetGroupMembers(request); | ||
152 | case "GETGROUPROLES": | ||
153 | return HandleGetGroupRoles(request); | ||
154 | case "GETROLEMEMBERS": | ||
155 | return HandleGetRoleMembers(request); | ||
156 | |||
157 | } | ||
158 | m_log.DebugFormat("[Groups.RobustHGConnector]: unknown method request: {0}", method); | ||
159 | } | ||
160 | catch (Exception e) | ||
161 | { | ||
162 | m_log.DebugFormat("[Groups.RobustHGConnector]: Exception {0}", e.StackTrace); | ||
163 | } | ||
164 | |||
165 | return FailureResult(); | ||
166 | } | ||
167 | |||
168 | byte[] HandleAddGroupProxy(Dictionary<string, object> request) | ||
169 | { | ||
170 | Dictionary<string, object> result = new Dictionary<string, object>(); | ||
171 | |||
172 | if (!request.ContainsKey("RequestingAgentID") || !request.ContainsKey("GroupID") | ||
173 | || !request.ContainsKey("AgentID") | ||
174 | || !request.ContainsKey("AccessToken") || !request.ContainsKey("Location")) | ||
175 | NullResult(result, "Bad network data"); | ||
176 | |||
177 | else | ||
178 | { | ||
179 | string RequestingAgentID = request["RequestingAgentID"].ToString(); | ||
180 | string agentID = request["AgentID"].ToString(); | ||
181 | UUID groupID = new UUID(request["GroupID"].ToString()); | ||
182 | string accessToken = request["AccessToken"].ToString(); | ||
183 | string location = request["Location"].ToString(); | ||
184 | string name = string.Empty; | ||
185 | if (request.ContainsKey("Name")) | ||
186 | name = request["Name"].ToString(); | ||
187 | |||
188 | string reason = string.Empty; | ||
189 | bool success = m_GroupsService.CreateGroupProxy(RequestingAgentID, agentID, accessToken, groupID, location, name, out reason); | ||
190 | result["REASON"] = reason; | ||
191 | result["RESULT"] = success.ToString(); | ||
192 | } | ||
193 | |||
194 | string xmlString = ServerUtils.BuildXmlResponse(result); | ||
195 | |||
196 | //m_log.DebugFormat("[XXX]: resp string: {0}", xmlString); | ||
197 | return Util.UTF8NoBomEncoding.GetBytes(xmlString); | ||
198 | } | ||
199 | |||
200 | byte[] HandleRemoveAgentFromGroup(Dictionary<string, object> request) | ||
201 | { | ||
202 | Dictionary<string, object> result = new Dictionary<string, object>(); | ||
203 | |||
204 | if (!request.ContainsKey("AccessToken") || !request.ContainsKey("AgentID") || | ||
205 | !request.ContainsKey("GroupID")) | ||
206 | NullResult(result, "Bad network data"); | ||
207 | else | ||
208 | { | ||
209 | UUID groupID = new UUID(request["GroupID"].ToString()); | ||
210 | string agentID = request["AgentID"].ToString(); | ||
211 | string token = request["AccessToken"].ToString(); | ||
212 | string reason = string.Empty; | ||
213 | |||
214 | m_GroupsService.RemoveAgentFromGroup(agentID, agentID, groupID, token); | ||
215 | } | ||
216 | |||
217 | //m_log.DebugFormat("[XXX]: resp string: {0}", xmlString); | ||
218 | result["RESULT"] = "true"; | ||
219 | return Util.UTF8NoBomEncoding.GetBytes(ServerUtils.BuildXmlResponse(result)); | ||
220 | } | ||
221 | |||
222 | byte[] HandleGetGroup(Dictionary<string, object> request) | ||
223 | { | ||
224 | Dictionary<string, object> result = new Dictionary<string, object>(); | ||
225 | |||
226 | if (!request.ContainsKey("RequestingAgentID") || !request.ContainsKey("AccessToken")) | ||
227 | NullResult(result, "Bad network data"); | ||
228 | else | ||
229 | { | ||
230 | string RequestingAgentID = request["RequestingAgentID"].ToString(); | ||
231 | string token = request["AccessToken"].ToString(); | ||
232 | |||
233 | UUID groupID = UUID.Zero; | ||
234 | string groupName = string.Empty; | ||
235 | |||
236 | if (request.ContainsKey("GroupID")) | ||
237 | groupID = new UUID(request["GroupID"].ToString()); | ||
238 | if (request.ContainsKey("Name")) | ||
239 | groupName = request["Name"].ToString(); | ||
240 | |||
241 | ExtendedGroupRecord grec = m_GroupsService.GetGroupRecord(RequestingAgentID, groupID, groupName, token); | ||
242 | if (grec == null) | ||
243 | NullResult(result, "Group not found"); | ||
244 | else | ||
245 | result["RESULT"] = GroupsDataUtils.GroupRecord(grec); | ||
246 | } | ||
247 | |||
248 | string xmlString = ServerUtils.BuildXmlResponse(result); | ||
249 | |||
250 | //m_log.DebugFormat("[XXX]: resp string: {0}", xmlString); | ||
251 | return Util.UTF8NoBomEncoding.GetBytes(xmlString); | ||
252 | } | ||
253 | |||
254 | byte[] HandleGetGroupMembers(Dictionary<string, object> request) | ||
255 | { | ||
256 | Dictionary<string, object> result = new Dictionary<string, object>(); | ||
257 | |||
258 | if (!request.ContainsKey("RequestingAgentID") || !request.ContainsKey("GroupID") || !request.ContainsKey("AccessToken")) | ||
259 | NullResult(result, "Bad network data"); | ||
260 | else | ||
261 | { | ||
262 | UUID groupID = new UUID(request["GroupID"].ToString()); | ||
263 | string requestingAgentID = request["RequestingAgentID"].ToString(); | ||
264 | string token = request["AccessToken"].ToString(); | ||
265 | |||
266 | List<ExtendedGroupMembersData> members = m_GroupsService.GetGroupMembers(requestingAgentID, groupID, token); | ||
267 | if (members == null || (members != null && members.Count == 0)) | ||
268 | { | ||
269 | NullResult(result, "No members"); | ||
270 | } | ||
271 | else | ||
272 | { | ||
273 | Dictionary<string, object> dict = new Dictionary<string, object>(); | ||
274 | int i = 0; | ||
275 | foreach (ExtendedGroupMembersData m in members) | ||
276 | { | ||
277 | dict["m-" + i++] = GroupsDataUtils.GroupMembersData(m); | ||
278 | } | ||
279 | |||
280 | result["RESULT"] = dict; | ||
281 | } | ||
282 | } | ||
283 | |||
284 | string xmlString = ServerUtils.BuildXmlResponse(result); | ||
285 | |||
286 | //m_log.DebugFormat("[XXX]: resp string: {0}", xmlString); | ||
287 | return Util.UTF8NoBomEncoding.GetBytes(xmlString); | ||
288 | } | ||
289 | |||
290 | byte[] HandleGetGroupRoles(Dictionary<string, object> request) | ||
291 | { | ||
292 | Dictionary<string, object> result = new Dictionary<string, object>(); | ||
293 | |||
294 | if (!request.ContainsKey("RequestingAgentID") || !request.ContainsKey("GroupID") || !request.ContainsKey("AccessToken")) | ||
295 | NullResult(result, "Bad network data"); | ||
296 | else | ||
297 | { | ||
298 | UUID groupID = new UUID(request["GroupID"].ToString()); | ||
299 | string requestingAgentID = request["RequestingAgentID"].ToString(); | ||
300 | string token = request["AccessToken"].ToString(); | ||
301 | |||
302 | List<GroupRolesData> roles = m_GroupsService.GetGroupRoles(requestingAgentID, groupID, token); | ||
303 | if (roles == null || (roles != null && roles.Count == 0)) | ||
304 | { | ||
305 | NullResult(result, "No members"); | ||
306 | } | ||
307 | else | ||
308 | { | ||
309 | Dictionary<string, object> dict = new Dictionary<string, object>(); | ||
310 | int i = 0; | ||
311 | foreach (GroupRolesData r in roles) | ||
312 | dict["r-" + i++] = GroupsDataUtils.GroupRolesData(r); | ||
313 | |||
314 | result["RESULT"] = dict; | ||
315 | } | ||
316 | } | ||
317 | |||
318 | string xmlString = ServerUtils.BuildXmlResponse(result); | ||
319 | |||
320 | //m_log.DebugFormat("[XXX]: resp string: {0}", xmlString); | ||
321 | return Util.UTF8NoBomEncoding.GetBytes(xmlString); | ||
322 | } | ||
323 | |||
324 | byte[] HandleGetRoleMembers(Dictionary<string, object> request) | ||
325 | { | ||
326 | Dictionary<string, object> result = new Dictionary<string, object>(); | ||
327 | |||
328 | if (!request.ContainsKey("RequestingAgentID") || !request.ContainsKey("GroupID") || !request.ContainsKey("AccessToken")) | ||
329 | NullResult(result, "Bad network data"); | ||
330 | else | ||
331 | { | ||
332 | UUID groupID = new UUID(request["GroupID"].ToString()); | ||
333 | string requestingAgentID = request["RequestingAgentID"].ToString(); | ||
334 | string token = request["AccessToken"].ToString(); | ||
335 | |||
336 | List<ExtendedGroupRoleMembersData> rmembers = m_GroupsService.GetGroupRoleMembers(requestingAgentID, groupID, token); | ||
337 | if (rmembers == null || (rmembers != null && rmembers.Count == 0)) | ||
338 | { | ||
339 | NullResult(result, "No members"); | ||
340 | } | ||
341 | else | ||
342 | { | ||
343 | Dictionary<string, object> dict = new Dictionary<string, object>(); | ||
344 | int i = 0; | ||
345 | foreach (ExtendedGroupRoleMembersData rm in rmembers) | ||
346 | dict["rm-" + i++] = GroupsDataUtils.GroupRoleMembersData(rm); | ||
347 | |||
348 | result["RESULT"] = dict; | ||
349 | } | ||
350 | } | ||
351 | |||
352 | string xmlString = ServerUtils.BuildXmlResponse(result); | ||
353 | |||
354 | //m_log.DebugFormat("[XXX]: resp string: {0}", xmlString); | ||
355 | return Util.UTF8NoBomEncoding.GetBytes(xmlString); | ||
356 | } | ||
357 | |||
358 | byte[] HandleAddNotice(Dictionary<string, object> request) | ||
359 | { | ||
360 | Dictionary<string, object> result = new Dictionary<string, object>(); | ||
361 | |||
362 | if (!request.ContainsKey("RequestingAgentID") || !request.ContainsKey("GroupID") || !request.ContainsKey("NoticeID") || | ||
363 | !request.ContainsKey("FromName") || !request.ContainsKey("Subject") || !request.ContainsKey("Message") || | ||
364 | !request.ContainsKey("HasAttachment")) | ||
365 | NullResult(result, "Bad network data"); | ||
366 | |||
367 | else | ||
368 | { | ||
369 | |||
370 | bool hasAtt = bool.Parse(request["HasAttachment"].ToString()); | ||
371 | byte attType = 0; | ||
372 | string attName = string.Empty; | ||
373 | string attOwner = string.Empty; | ||
374 | UUID attItem = UUID.Zero; | ||
375 | if (request.ContainsKey("AttachmentType")) | ||
376 | attType = byte.Parse(request["AttachmentType"].ToString()); | ||
377 | if (request.ContainsKey("AttachmentName")) | ||
378 | attName = request["AttachmentType"].ToString(); | ||
379 | if (request.ContainsKey("AttachmentItemID")) | ||
380 | attItem = new UUID(request["AttachmentItemID"].ToString()); | ||
381 | if (request.ContainsKey("AttachmentOwnerID")) | ||
382 | attOwner = request["AttachmentOwnerID"].ToString(); | ||
383 | |||
384 | bool success = m_GroupsService.AddNotice(request["RequestingAgentID"].ToString(), new UUID(request["GroupID"].ToString()), | ||
385 | new UUID(request["NoticeID"].ToString()), request["FromName"].ToString(), request["Subject"].ToString(), | ||
386 | request["Message"].ToString(), hasAtt, attType, attName, attItem, attOwner); | ||
387 | |||
388 | result["RESULT"] = success.ToString(); | ||
389 | } | ||
390 | |||
391 | string xmlString = ServerUtils.BuildXmlResponse(result); | ||
392 | |||
393 | //m_log.DebugFormat("[XXX]: resp string: {0}", xmlString); | ||
394 | return Util.UTF8NoBomEncoding.GetBytes(xmlString); | ||
395 | } | ||
396 | |||
397 | byte[] HandleVerifyNotice(Dictionary<string, object> request) | ||
398 | { | ||
399 | Dictionary<string, object> result = new Dictionary<string, object>(); | ||
400 | |||
401 | if (!request.ContainsKey("NoticeID") || !request.ContainsKey("GroupID")) | ||
402 | NullResult(result, "Bad network data"); | ||
403 | |||
404 | else | ||
405 | { | ||
406 | UUID noticeID = new UUID(request["NoticeID"].ToString()); | ||
407 | UUID groupID = new UUID(request["GroupID"].ToString()); | ||
408 | |||
409 | bool success = m_GroupsService.VerifyNotice(noticeID, groupID); | ||
410 | //m_log.DebugFormat("[XXX]: VerifyNotice returned {0}", success); | ||
411 | result["RESULT"] = success.ToString(); | ||
412 | } | ||
413 | |||
414 | string xmlString = ServerUtils.BuildXmlResponse(result); | ||
415 | |||
416 | //m_log.DebugFormat("[XXX]: resp string: {0}", xmlString); | ||
417 | return Util.UTF8NoBomEncoding.GetBytes(xmlString); | ||
418 | } | ||
419 | |||
420 | // | ||
421 | // | ||
422 | // | ||
423 | // | ||
424 | // | ||
425 | |||
426 | #region Helpers | ||
427 | |||
428 | private void NullResult(Dictionary<string, object> result, string reason) | ||
429 | { | ||
430 | result["RESULT"] = "NULL"; | ||
431 | result["REASON"] = reason; | ||
432 | } | ||
433 | |||
434 | private byte[] FailureResult() | ||
435 | { | ||
436 | Dictionary<string, object> result = new Dictionary<string, object>(); | ||
437 | NullResult(result, "Unknown method"); | ||
438 | string xmlString = ServerUtils.BuildXmlResponse(result); | ||
439 | return Util.UTF8NoBomEncoding.GetBytes(xmlString); | ||
440 | } | ||
441 | |||
442 | #endregion | ||
443 | } | ||
444 | } | ||