OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
AvatarServerPostHandler.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.Avatar
47 {
49  {
50  private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
51 
52  private IAvatarService m_AvatarService;
53 
55  base("POST", "/avatar", auth)
56  {
57  m_AvatarService = 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 
70  try
71  {
72  Dictionary<string, object> request =
73  ServerUtils.ParseQueryString(body);
74 
75  if (!request.ContainsKey("METHOD"))
76  return FailureResult();
77 
78  string method = request["METHOD"].ToString();
79 
80  switch (method)
81  {
82  case "getavatar":
83  return GetAvatar(request);
84  case "setavatar":
85  return SetAvatar(request);
86  case "resetavatar":
87  return ResetAvatar(request);
88  case "setitems":
89  return SetItems(request);
90  case "removeitems":
91  return RemoveItems(request);
92  }
93  m_log.DebugFormat("[AVATAR HANDLER]: unknown method request: {0}", method);
94  }
95  catch (Exception e)
96  {
97  m_log.Debug("[AVATAR HANDLER]: Exception {0}" + e);
98  }
99 
100  return FailureResult();
101 
102  }
103 
104  byte[] GetAvatar(Dictionary<string, object> request)
105  {
106  UUID user = UUID.Zero;
107 
108  if (!request.ContainsKey("UserID"))
109  return FailureResult();
110 
111  if (UUID.TryParse(request["UserID"].ToString(), out user))
112  {
113  AvatarData avatar = m_AvatarService.GetAvatar(user);
114  if (avatar == null)
115  return FailureResult();
116 
117  Dictionary<string, object> result = new Dictionary<string, object>();
118  if (avatar == null)
119  result["result"] = "null";
120  else
121  result["result"] = avatar.ToKeyValuePairs();
122 
123  string xmlString = ServerUtils.BuildXmlResponse(result);
124 
125  return Util.UTF8NoBomEncoding.GetBytes(xmlString);
126  }
127 
128  return FailureResult();
129  }
130 
131  byte[] SetAvatar(Dictionary<string, object> request)
132  {
133  UUID user = UUID.Zero;
134 
135  if (!request.ContainsKey("UserID"))
136  return FailureResult();
137 
138  if (!UUID.TryParse(request["UserID"].ToString(), out user))
139  return FailureResult();
140 
141  RemoveRequestParamsNotForStorage(request);
142 
143  AvatarData avatar = new AvatarData(request);
144  if (m_AvatarService.SetAvatar(user, avatar))
145  return SuccessResult();
146 
147  return FailureResult();
148  }
149 
150  byte[] ResetAvatar(Dictionary<string, object> request)
151  {
152  UUID user = UUID.Zero;
153  if (!request.ContainsKey("UserID"))
154  return FailureResult();
155 
156  if (!UUID.TryParse(request["UserID"].ToString(), out user))
157  return FailureResult();
158 
159  RemoveRequestParamsNotForStorage(request);
160 
161  if (m_AvatarService.ResetAvatar(user))
162  return SuccessResult();
163 
164  return FailureResult();
165  }
166 
171  private void RemoveRequestParamsNotForStorage(Dictionary<string, object> request)
172  {
173  request.Remove("VERSIONMAX");
174  request.Remove("VERSIONMIN");
175  request.Remove("METHOD");
176  request.Remove("UserID");
177  }
178 
179  byte[] SetItems(Dictionary<string, object> request)
180  {
181  UUID user = UUID.Zero;
182  string[] names, values;
183 
184  if (!request.ContainsKey("UserID") || !request.ContainsKey("Names") || !request.ContainsKey("Values"))
185  return FailureResult();
186 
187  if (!UUID.TryParse(request["UserID"].ToString(), out user))
188  return FailureResult();
189 
190  if (!(request["Names"] is List<string> || request["Values"] is List<string>))
191  return FailureResult();
192 
193  RemoveRequestParamsNotForStorage(request);
194 
195  List<string> _names = (List<string>)request["Names"];
196  names = _names.ToArray();
197  List<string> _values = (List<string>)request["Values"];
198  values = _values.ToArray();
199 
200  if (m_AvatarService.SetItems(user, names, values))
201  return SuccessResult();
202 
203  return FailureResult();
204  }
205 
206  byte[] RemoveItems(Dictionary<string, object> request)
207  {
208  UUID user = UUID.Zero;
209  string[] names;
210 
211  if (!request.ContainsKey("UserID") || !request.ContainsKey("Names"))
212  return FailureResult();
213 
214  if (!UUID.TryParse(request["UserID"].ToString(), out user))
215  return FailureResult();
216 
217  if (!(request["Names"] is List<string>))
218  return FailureResult();
219 
220  List<string> _names = (List<string>)request["Names"];
221  names = _names.ToArray();
222 
223  if (m_AvatarService.RemoveItems(user, names))
224  return SuccessResult();
225 
226  return FailureResult();
227  }
228 
229 
230 
231  private byte[] SuccessResult()
232  {
233  XmlDocument doc = new XmlDocument();
234 
235  XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
236  "", "");
237 
238  doc.AppendChild(xmlnode);
239 
240  XmlElement rootElement = doc.CreateElement("", "ServerResponse",
241  "");
242 
243  doc.AppendChild(rootElement);
244 
245  XmlElement result = doc.CreateElement("", "result", "");
246  result.AppendChild(doc.CreateTextNode("Success"));
247 
248  rootElement.AppendChild(result);
249 
250  return Util.DocToBytes(doc);
251  }
252 
253  private byte[] FailureResult()
254  {
255  XmlDocument doc = new XmlDocument();
256 
257  XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
258  "", "");
259 
260  doc.AppendChild(xmlnode);
261 
262  XmlElement rootElement = doc.CreateElement("", "ServerResponse",
263  "");
264 
265  doc.AppendChild(rootElement);
266 
267  XmlElement result = doc.CreateElement("", "result", "");
268  result.AppendChild(doc.CreateTextNode("Failure"));
269 
270  rootElement.AppendChild(result);
271 
272  return Util.DocToBytes(doc);
273  }
274 
275  }
276 }
override byte[] ProcessRequest(string path, Stream requestData, IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
AvatarServerPostHandler(IAvatarService service, IServiceAuth auth)
Each region/client that uses avatars will have a data structure of this type representing the avatars...