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