opensim-development – 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.GridUser |
||
46 | { |
||
47 | public class GridUserServerPostHandler : BaseStreamHandler |
||
48 | { |
||
49 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); |
||
50 | |||
51 | private IGridUserService m_GridUserService; |
||
52 | |||
53 | public GridUserServerPostHandler(IGridUserService service) : |
||
54 | base("POST", "/griduser") |
||
55 | { |
||
56 | m_GridUserService = 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 "loggedin": |
||
82 | return LoggedIn(request); |
||
83 | case "loggedout": |
||
84 | return LoggedOut(request); |
||
85 | case "sethome": |
||
86 | return SetHome(request); |
||
87 | case "setposition": |
||
88 | return SetPosition(request); |
||
89 | case "getgriduserinfo": |
||
90 | return GetGridUserInfo(request); |
||
91 | case "getgriduserinfos": |
||
92 | return GetGridUserInfos(request); |
||
93 | } |
||
94 | m_log.DebugFormat("[GRID USER HANDLER]: unknown method request: {0}", method); |
||
95 | } |
||
96 | catch (Exception e) |
||
97 | { |
||
98 | m_log.DebugFormat("[GRID USER HANDLER]: Exception in method {0}: {1}", method, e); |
||
99 | } |
||
100 | |||
101 | return FailureResult(); |
||
102 | |||
103 | } |
||
104 | |||
105 | byte[] LoggedIn(Dictionary<string, object> request) |
||
106 | { |
||
107 | string user = String.Empty; |
||
108 | |||
109 | if (!request.ContainsKey("UserID")) |
||
110 | return FailureResult(); |
||
111 | |||
112 | user = request["UserID"].ToString(); |
||
113 | |||
114 | GridUserInfo guinfo = m_GridUserService.LoggedIn(user); |
||
115 | |||
116 | Dictionary<string, object> result = new Dictionary<string, object>(); |
||
117 | result["result"] = guinfo.ToKeyValuePairs(); |
||
118 | |||
119 | string xmlString = ServerUtils.BuildXmlResponse(result); |
||
120 | |||
121 | //m_log.DebugFormat("[GRID USER HANDLER]: resp string: {0}", xmlString); |
||
122 | return Util.UTF8NoBomEncoding.GetBytes(xmlString); |
||
123 | } |
||
124 | |||
125 | byte[] LoggedOut(Dictionary<string, object> request) |
||
126 | { |
||
127 | string userID = string.Empty; |
||
128 | UUID regionID = UUID.Zero; |
||
129 | Vector3 position = Vector3.Zero; |
||
130 | Vector3 lookat = Vector3.Zero; |
||
131 | |||
132 | if (!UnpackArgs(request, out userID, out regionID, out position, out lookat)) |
||
133 | return FailureResult(); |
||
134 | |||
135 | if (m_GridUserService.LoggedOut(userID, UUID.Zero, regionID, position, lookat)) |
||
136 | return SuccessResult(); |
||
137 | |||
138 | return FailureResult(); |
||
139 | } |
||
140 | |||
141 | byte[] SetHome(Dictionary<string, object> request) |
||
142 | { |
||
143 | string user = string.Empty; |
||
144 | UUID region = UUID.Zero; |
||
145 | Vector3 position = new Vector3(128, 128, 70); |
||
146 | Vector3 look = Vector3.Zero; |
||
147 | |||
148 | if (!UnpackArgs(request, out user, out region, out position, out look)) |
||
149 | return FailureResult(); |
||
150 | |||
151 | if (m_GridUserService.SetHome(user, region, position, look)) |
||
152 | return SuccessResult(); |
||
153 | |||
154 | return FailureResult(); |
||
155 | } |
||
156 | |||
157 | byte[] SetPosition(Dictionary<string, object> request) |
||
158 | { |
||
159 | string user = string.Empty; |
||
160 | UUID region = UUID.Zero; |
||
161 | Vector3 position = new Vector3(128, 128, 70); |
||
162 | Vector3 look = Vector3.Zero; |
||
163 | |||
164 | if (!request.ContainsKey("UserID") || !request.ContainsKey("RegionID")) |
||
165 | return FailureResult(); |
||
166 | |||
167 | if (!UnpackArgs(request, out user, out region, out position, out look)) |
||
168 | return FailureResult(); |
||
169 | |||
170 | if (m_GridUserService.SetLastPosition(user, UUID.Zero, region, position, look)) |
||
171 | return SuccessResult(); |
||
172 | |||
173 | return FailureResult(); |
||
174 | } |
||
175 | |||
176 | byte[] GetGridUserInfo(Dictionary<string, object> request) |
||
177 | { |
||
178 | string user = String.Empty; |
||
179 | |||
180 | if (!request.ContainsKey("UserID")) |
||
181 | return FailureResult(); |
||
182 | |||
183 | user = request["UserID"].ToString(); |
||
184 | |||
185 | GridUserInfo guinfo = m_GridUserService.GetGridUserInfo(user); |
||
186 | |||
187 | Dictionary<string, object> result = new Dictionary<string, object>(); |
||
188 | if (guinfo != null) |
||
189 | result["result"] = guinfo.ToKeyValuePairs(); |
||
190 | else |
||
191 | result["result"] = "null"; |
||
192 | |||
193 | string xmlString = ServerUtils.BuildXmlResponse(result); |
||
194 | //m_log.DebugFormat("[GRID USER HANDLER]: resp string: {0}", xmlString); |
||
195 | return Util.UTF8NoBomEncoding.GetBytes(xmlString); |
||
196 | } |
||
197 | |||
198 | byte[] GetGridUserInfos(Dictionary<string, object> request) |
||
199 | { |
||
200 | |||
201 | string[] userIDs; |
||
202 | |||
203 | if (!request.ContainsKey("AgentIDs")) |
||
204 | { |
||
205 | m_log.DebugFormat("[GRID USER HANDLER]: GetGridUserInfos called without required uuids argument"); |
||
206 | return FailureResult(); |
||
207 | } |
||
208 | |||
209 | if (!(request["AgentIDs"] is List<string>)) |
||
210 | { |
||
211 | m_log.DebugFormat("[GRID USER HANDLER]: GetGridUserInfos input argument was of unexpected type {0}", request["uuids"].GetType().ToString()); |
||
212 | return FailureResult(); |
||
213 | } |
||
214 | |||
215 | userIDs = ((List<string>)request["AgentIDs"]).ToArray(); |
||
216 | |||
217 | GridUserInfo[] pinfos = m_GridUserService.GetGridUserInfo(userIDs); |
||
218 | |||
219 | Dictionary<string, object> result = new Dictionary<string, object>(); |
||
220 | if ((pinfos == null) || ((pinfos != null) && (pinfos.Length == 0))) |
||
221 | result["result"] = "null"; |
||
222 | else |
||
223 | { |
||
224 | int i = 0; |
||
225 | foreach (GridUserInfo pinfo in pinfos) |
||
226 | { |
||
227 | Dictionary<string, object> rinfoDict = pinfo.ToKeyValuePairs(); |
||
228 | result["griduser" + i] = rinfoDict; |
||
229 | i++; |
||
230 | } |
||
231 | } |
||
232 | |||
233 | string xmlString = ServerUtils.BuildXmlResponse(result); |
||
234 | return Util.UTF8NoBomEncoding.GetBytes(xmlString); |
||
235 | } |
||
236 | |||
237 | private bool UnpackArgs(Dictionary<string, object> request, out string user, out UUID region, out Vector3 position, out Vector3 lookAt) |
||
238 | { |
||
239 | user = string.Empty; |
||
240 | region = UUID.Zero; |
||
241 | position = new Vector3(128, 128, 70); |
||
242 | lookAt = Vector3.Zero; |
||
243 | |||
244 | if (!request.ContainsKey("UserID") || !request.ContainsKey("RegionID")) |
||
245 | return false; |
||
246 | |||
247 | user = request["UserID"].ToString(); |
||
248 | |||
249 | if (!UUID.TryParse(request["RegionID"].ToString(), out region)) |
||
250 | return false; |
||
251 | |||
252 | if (request.ContainsKey("Position")) |
||
253 | Vector3.TryParse(request["Position"].ToString(), out position); |
||
254 | |||
255 | if (request.ContainsKey("LookAt")) |
||
256 | Vector3.TryParse(request["LookAt"].ToString(), out lookAt); |
||
257 | |||
258 | return true; |
||
259 | } |
||
260 | |||
261 | |||
262 | private byte[] SuccessResult() |
||
263 | { |
||
264 | XmlDocument doc = new XmlDocument(); |
||
265 | |||
266 | XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration, |
||
267 | "", ""); |
||
268 | |||
269 | doc.AppendChild(xmlnode); |
||
270 | |||
271 | XmlElement rootElement = doc.CreateElement("", "ServerResponse", |
||
272 | ""); |
||
273 | |||
274 | doc.AppendChild(rootElement); |
||
275 | |||
276 | XmlElement result = doc.CreateElement("", "result", ""); |
||
277 | result.AppendChild(doc.CreateTextNode("Success")); |
||
278 | |||
279 | rootElement.AppendChild(result); |
||
280 | |||
281 | return DocToBytes(doc); |
||
282 | } |
||
283 | |||
284 | private byte[] FailureResult() |
||
285 | { |
||
286 | XmlDocument doc = new XmlDocument(); |
||
287 | |||
288 | XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration, |
||
289 | "", ""); |
||
290 | |||
291 | doc.AppendChild(xmlnode); |
||
292 | |||
293 | XmlElement rootElement = doc.CreateElement("", "ServerResponse", |
||
294 | ""); |
||
295 | |||
296 | doc.AppendChild(rootElement); |
||
297 | |||
298 | XmlElement result = doc.CreateElement("", "result", ""); |
||
299 | result.AppendChild(doc.CreateTextNode("Failure")); |
||
300 | |||
301 | rootElement.AppendChild(result); |
||
302 | |||
303 | return DocToBytes(doc); |
||
304 | } |
||
305 | |||
306 | private byte[] DocToBytes(XmlDocument doc) |
||
307 | { |
||
308 | MemoryStream ms = new MemoryStream(); |
||
309 | XmlTextWriter xw = new XmlTextWriter(ms, null); |
||
310 | xw.Formatting = Formatting.Indented; |
||
311 | doc.WriteTo(xw); |
||
312 | xw.Flush(); |
||
313 | |||
314 | return ms.ToArray(); |
||
315 | } |
||
316 | |||
317 | |||
318 | } |
||
319 | } |