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