31 using System.Reflection;
35 using System.Text.RegularExpressions;
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;
46 namespace OpenSim.Server.Handlers.Presence
50 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
55 base(
"POST",
"/presence", auth)
57 m_PresenceService = service;
63 StreamReader sr =
new StreamReader(requestData);
64 string body = sr.ReadToEnd();
69 string method = string.Empty;
72 Dictionary<string, object> request =
73 ServerUtils.ParseQueryString(body);
75 if (!request.ContainsKey(
"METHOD"))
76 return FailureResult();
78 method = request[
"METHOD"].ToString();
83 return LoginAgent(request);
85 return LogoutAgent(request);
87 return LogoutRegionAgents(request);
89 return Report(request);
91 return GetAgent(request);
93 return GetAgents(request);
95 m_log.DebugFormat(
"[PRESENCE HANDLER]: unknown method request: {0}", method);
99 m_log.DebugFormat(
"[PRESENCE HANDLER]: Exception in method {0}: {1}", method, e);
102 return FailureResult();
106 byte[] LoginAgent(Dictionary<string, object> request)
108 string user = String.Empty;
109 UUID session = UUID.Zero;
110 UUID ssession = UUID.Zero;
112 if (!request.ContainsKey(
"UserID") || !request.ContainsKey(
"SessionID"))
113 return FailureResult();
115 user = request[
"UserID"].ToString();
117 if (!UUID.TryParse(request[
"SessionID"].ToString(), out session))
118 return FailureResult();
120 if (request.ContainsKey(
"SecureSessionID"))
122 UUID.TryParse(request[
"SecureSessionID"].ToString(), out ssession);
124 if (m_PresenceService.LoginAgent(user, session, ssession))
125 return SuccessResult();
127 return FailureResult();
130 byte[] LogoutAgent(Dictionary<string, object> request)
132 UUID session = UUID.Zero;
134 if (!request.ContainsKey(
"SessionID"))
135 return FailureResult();
137 if (!UUID.TryParse(request[
"SessionID"].ToString(), out session))
138 return FailureResult();
140 if (m_PresenceService.LogoutAgent(session))
141 return SuccessResult();
143 return FailureResult();
146 byte[] LogoutRegionAgents(Dictionary<string, object> request)
148 UUID region = UUID.Zero;
150 if (!request.ContainsKey(
"RegionID"))
151 return FailureResult();
153 if (!UUID.TryParse(request[
"RegionID"].ToString(), out region))
154 return FailureResult();
156 if (m_PresenceService.LogoutRegionAgents(region))
157 return SuccessResult();
159 return FailureResult();
162 byte[] Report(Dictionary<string, object> request)
164 UUID session = UUID.Zero;
165 UUID region = UUID.Zero;
167 if (!request.ContainsKey(
"SessionID") || !request.ContainsKey(
"RegionID"))
168 return FailureResult();
170 if (!UUID.TryParse(request[
"SessionID"].ToString(), out session))
171 return FailureResult();
173 if (!UUID.TryParse(request[
"RegionID"].ToString(), out region))
174 return FailureResult();
176 if (m_PresenceService.ReportAgent(session, region))
178 return SuccessResult();
181 return FailureResult();
184 byte[] GetAgent(Dictionary<string, object> request)
186 UUID session = UUID.Zero;
188 if (!request.ContainsKey(
"SessionID"))
189 return FailureResult();
191 if (!
UUID.TryParse(request[
"SessionID"].ToString(), out session))
192 return FailureResult();
194 PresenceInfo pinfo = m_PresenceService.GetAgent(session);
196 Dictionary<string, object> result =
new Dictionary<string, object>();
198 result[
"result"] =
"null";
200 result[
"result"] = pinfo.ToKeyValuePairs();
202 string xmlString = ServerUtils.BuildXmlResponse(result);
205 return Util.UTF8NoBomEncoding.GetBytes(xmlString);
208 byte[] GetAgents(Dictionary<string, object> request)
213 if (!request.ContainsKey(
"uuids"))
215 m_log.DebugFormat(
"[PRESENCE HANDLER]: GetAgents called without required uuids argument");
216 return FailureResult();
219 if (!(request[
"uuids"] is List<string>))
221 m_log.DebugFormat(
"[PRESENCE HANDLER]: GetAgents input argument was of unexpected type {0}", request[
"uuids"].GetType().ToString());
222 return FailureResult();
225 userIDs = ((List<string>)request[
"uuids"]).ToArray();
227 PresenceInfo[] pinfos = m_PresenceService.GetAgents(userIDs);
229 Dictionary<string, object> result =
new Dictionary<string, object>();
230 if ((pinfos == null) || ((pinfos != null) && (pinfos.Length == 0)))
231 result[
"result"] =
"null";
237 Dictionary<string, object> rinfoDict = pinfo.ToKeyValuePairs();
238 result[
"presence" + i] = rinfoDict;
243 string xmlString = ServerUtils.BuildXmlResponse(result);
246 return Util.UTF8NoBomEncoding.GetBytes(xmlString);
249 private byte[] SuccessResult()
251 XmlDocument doc =
new XmlDocument();
253 XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
256 doc.AppendChild(xmlnode);
258 XmlElement rootElement = doc.CreateElement(
"",
"ServerResponse",
261 doc.AppendChild(rootElement);
263 XmlElement result = doc.CreateElement(
"",
"result",
"");
264 result.AppendChild(doc.CreateTextNode(
"Success"));
266 rootElement.AppendChild(result);
268 return Util.DocToBytes(doc);
271 private byte[] FailureResult()
273 XmlDocument doc =
new XmlDocument();
275 XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
278 doc.AppendChild(xmlnode);
280 XmlElement rootElement = doc.CreateElement(
"",
"ServerResponse",
283 doc.AppendChild(rootElement);
285 XmlElement result = doc.CreateElement(
"",
"result",
"");
286 result.AppendChild(doc.CreateTextNode(
"Failure"));
288 rootElement.AppendChild(result);
290 return Util.DocToBytes(doc);
Base streamed request handler.
PresenceServerPostHandler(IPresenceService service, IServiceAuth auth)
override byte[] ProcessRequest(string path, Stream requestData, IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)