OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
PresenceServerPostHandler.cs
Go to the documentation of this file.
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 Nini.Config;
29 using log4net;
30 using System;
31 using System.Reflection;
32 using System.IO;
33 using System.Net;
34 using System.Text;
35 using System.Text.RegularExpressions;
36 using System.Xml;
37 using System.Xml.Serialization;
38 using System.Collections.Generic;
39 using OpenSim.Server.Base;
40 using OpenSim.Services.Interfaces;
41 using OpenSim.Framework;
42 using OpenSim.Framework.Servers.HttpServer;
43 using OpenSim.Framework.ServiceAuth;
44 using OpenMetaverse;
45 
46 namespace OpenSim.Server.Handlers.Presence
47 {
49  {
50  private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
51 
52  private IPresenceService m_PresenceService;
53 
55  base("POST", "/presence", auth)
56  {
57  m_PresenceService = service;
58  }
59 
60  protected override byte[] ProcessRequest(string path, Stream requestData,
61  IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
62  {
63  StreamReader sr = new StreamReader(requestData);
64  string body = sr.ReadToEnd();
65  sr.Close();
66  body = body.Trim();
67 
68  //m_log.DebugFormat("[XXX]: query String: {0}", body);
69  string method = string.Empty;
70  try
71  {
72  Dictionary<string, object> request =
73  ServerUtils.ParseQueryString(body);
74 
75  if (!request.ContainsKey("METHOD"))
76  return FailureResult();
77 
78  method = request["METHOD"].ToString();
79 
80  switch (method)
81  {
82  case "login":
83  return LoginAgent(request);
84  case "logout":
85  return LogoutAgent(request);
86  case "logoutregion":
87  return LogoutRegionAgents(request);
88  case "report":
89  return Report(request);
90  case "getagent":
91  return GetAgent(request);
92  case "getagents":
93  return GetAgents(request);
94  }
95  m_log.DebugFormat("[PRESENCE HANDLER]: unknown method request: {0}", method);
96  }
97  catch (Exception e)
98  {
99  m_log.DebugFormat("[PRESENCE HANDLER]: Exception in method {0}: {1}", method, e);
100  }
101 
102  return FailureResult();
103 
104  }
105 
106  byte[] LoginAgent(Dictionary<string, object> request)
107  {
108  string user = String.Empty;
109  UUID session = UUID.Zero;
110  UUID ssession = UUID.Zero;
111 
112  if (!request.ContainsKey("UserID") || !request.ContainsKey("SessionID"))
113  return FailureResult();
114 
115  user = request["UserID"].ToString();
116 
117  if (!UUID.TryParse(request["SessionID"].ToString(), out session))
118  return FailureResult();
119 
120  if (request.ContainsKey("SecureSessionID"))
121  // If it's malformed, we go on with a Zero on it
122  UUID.TryParse(request["SecureSessionID"].ToString(), out ssession);
123 
124  if (m_PresenceService.LoginAgent(user, session, ssession))
125  return SuccessResult();
126 
127  return FailureResult();
128  }
129 
130  byte[] LogoutAgent(Dictionary<string, object> request)
131  {
132  UUID session = UUID.Zero;
133 
134  if (!request.ContainsKey("SessionID"))
135  return FailureResult();
136 
137  if (!UUID.TryParse(request["SessionID"].ToString(), out session))
138  return FailureResult();
139 
140  if (m_PresenceService.LogoutAgent(session))
141  return SuccessResult();
142 
143  return FailureResult();
144  }
145 
146  byte[] LogoutRegionAgents(Dictionary<string, object> request)
147  {
148  UUID region = UUID.Zero;
149 
150  if (!request.ContainsKey("RegionID"))
151  return FailureResult();
152 
153  if (!UUID.TryParse(request["RegionID"].ToString(), out region))
154  return FailureResult();
155 
156  if (m_PresenceService.LogoutRegionAgents(region))
157  return SuccessResult();
158 
159  return FailureResult();
160  }
161 
162  byte[] Report(Dictionary<string, object> request)
163  {
164  UUID session = UUID.Zero;
165  UUID region = UUID.Zero;
166 
167  if (!request.ContainsKey("SessionID") || !request.ContainsKey("RegionID"))
168  return FailureResult();
169 
170  if (!UUID.TryParse(request["SessionID"].ToString(), out session))
171  return FailureResult();
172 
173  if (!UUID.TryParse(request["RegionID"].ToString(), out region))
174  return FailureResult();
175 
176  if (m_PresenceService.ReportAgent(session, region))
177  {
178  return SuccessResult();
179  }
180 
181  return FailureResult();
182  }
183 
184  byte[] GetAgent(Dictionary<string, object> request)
185  {
186  UUID session = UUID.Zero;
187 
188  if (!request.ContainsKey("SessionID"))
189  return FailureResult();
190 
191  if (!UUID.TryParse(request["SessionID"].ToString(), out session))
192  return FailureResult();
193 
194  PresenceInfo pinfo = m_PresenceService.GetAgent(session);
195 
196  Dictionary<string, object> result = new Dictionary<string, object>();
197  if (pinfo == null)
198  result["result"] = "null";
199  else
200  result["result"] = pinfo.ToKeyValuePairs();
201 
202  string xmlString = ServerUtils.BuildXmlResponse(result);
203 
204  //m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString);
205  return Util.UTF8NoBomEncoding.GetBytes(xmlString);
206  }
207 
208  byte[] GetAgents(Dictionary<string, object> request)
209  {
210 
211  string[] userIDs;
212 
213  if (!request.ContainsKey("uuids"))
214  {
215  m_log.DebugFormat("[PRESENCE HANDLER]: GetAgents called without required uuids argument");
216  return FailureResult();
217  }
218 
219  if (!(request["uuids"] is List<string>))
220  {
221  m_log.DebugFormat("[PRESENCE HANDLER]: GetAgents input argument was of unexpected type {0}", request["uuids"].GetType().ToString());
222  return FailureResult();
223  }
224 
225  userIDs = ((List<string>)request["uuids"]).ToArray();
226 
227  PresenceInfo[] pinfos = m_PresenceService.GetAgents(userIDs);
228 
229  Dictionary<string, object> result = new Dictionary<string, object>();
230  if ((pinfos == null) || ((pinfos != null) && (pinfos.Length == 0)))
231  result["result"] = "null";
232  else
233  {
234  int i = 0;
235  foreach (PresenceInfo pinfo in pinfos)
236  {
237  Dictionary<string, object> rinfoDict = pinfo.ToKeyValuePairs();
238  result["presence" + i] = rinfoDict;
239  i++;
240  }
241  }
242 
243  string xmlString = ServerUtils.BuildXmlResponse(result);
244 
245  //m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString);
246  return Util.UTF8NoBomEncoding.GetBytes(xmlString);
247  }
248 
249  private byte[] SuccessResult()
250  {
251  XmlDocument doc = new XmlDocument();
252 
253  XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
254  "", "");
255 
256  doc.AppendChild(xmlnode);
257 
258  XmlElement rootElement = doc.CreateElement("", "ServerResponse",
259  "");
260 
261  doc.AppendChild(rootElement);
262 
263  XmlElement result = doc.CreateElement("", "result", "");
264  result.AppendChild(doc.CreateTextNode("Success"));
265 
266  rootElement.AppendChild(result);
267 
268  return Util.DocToBytes(doc);
269  }
270 
271  private byte[] FailureResult()
272  {
273  XmlDocument doc = new XmlDocument();
274 
275  XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
276  "", "");
277 
278  doc.AppendChild(xmlnode);
279 
280  XmlElement rootElement = doc.CreateElement("", "ServerResponse",
281  "");
282 
283  doc.AppendChild(rootElement);
284 
285  XmlElement result = doc.CreateElement("", "result", "");
286  result.AppendChild(doc.CreateTextNode("Failure"));
287 
288  rootElement.AppendChild(result);
289 
290  return Util.DocToBytes(doc);
291  }
292 
293  }
294 }
PresenceServerPostHandler(IPresenceService service, IServiceAuth auth)
override byte[] ProcessRequest(string path, Stream requestData, IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)