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.ServiceAuth;
43 using OpenSim.Framework.Servers.HttpServer;
46 namespace OpenSim.Server.Handlers.
Avatar
50 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
55 base(
"POST",
"/avatar", auth)
57 m_AvatarService = service;
63 StreamReader sr =
new StreamReader(requestData);
64 string body = sr.ReadToEnd();
72 Dictionary<string, object> request =
73 ServerUtils.ParseQueryString(body);
75 if (!request.ContainsKey(
"METHOD"))
76 return FailureResult();
78 string method = request[
"METHOD"].ToString();
83 return GetAvatar(request);
85 return SetAvatar(request);
87 return ResetAvatar(request);
89 return SetItems(request);
91 return RemoveItems(request);
93 m_log.DebugFormat(
"[AVATAR HANDLER]: unknown method request: {0}", method);
97 m_log.Debug(
"[AVATAR HANDLER]: Exception {0}" + e);
100 return FailureResult();
104 byte[] GetAvatar(Dictionary<string, object> request)
106 UUID user = UUID.Zero;
108 if (!request.ContainsKey(
"UserID"))
109 return FailureResult();
111 if (UUID.TryParse(request[
"UserID"].ToString(), out user))
113 AvatarData avatar = m_AvatarService.GetAvatar(user);
115 return FailureResult();
117 Dictionary<string, object> result =
new Dictionary<string, object>();
119 result[
"result"] =
"null";
121 result[
"result"] = avatar.ToKeyValuePairs();
123 string xmlString = ServerUtils.BuildXmlResponse(result);
125 return Util.UTF8NoBomEncoding.GetBytes(xmlString);
128 return FailureResult();
131 byte[] SetAvatar(Dictionary<string, object> request)
133 UUID user = UUID.Zero;
135 if (!request.ContainsKey(
"UserID"))
136 return FailureResult();
138 if (!UUID.TryParse(request[
"UserID"].ToString(), out user))
139 return FailureResult();
141 RemoveRequestParamsNotForStorage(request);
144 if (m_AvatarService.SetAvatar(user, avatar))
145 return SuccessResult();
147 return FailureResult();
150 byte[] ResetAvatar(Dictionary<string, object> request)
152 UUID user = UUID.Zero;
153 if (!request.ContainsKey(
"UserID"))
154 return FailureResult();
156 if (!UUID.TryParse(request[
"UserID"].ToString(), out user))
157 return FailureResult();
159 RemoveRequestParamsNotForStorage(request);
161 if (m_AvatarService.ResetAvatar(user))
162 return SuccessResult();
164 return FailureResult();
171 private void RemoveRequestParamsNotForStorage(Dictionary<string, object> request)
173 request.Remove(
"VERSIONMAX");
174 request.Remove(
"VERSIONMIN");
175 request.Remove(
"METHOD");
176 request.Remove(
"UserID");
179 byte[] SetItems(Dictionary<string, object> request)
181 UUID user = UUID.Zero;
182 string[] names, values;
184 if (!request.ContainsKey(
"UserID") || !request.ContainsKey(
"Names") || !request.ContainsKey(
"Values"))
185 return FailureResult();
187 if (!
UUID.TryParse(request[
"UserID"].ToString(), out user))
188 return FailureResult();
190 if (!(request[
"Names"] is List<string> || request[
"Values"] is List<string>))
191 return FailureResult();
193 RemoveRequestParamsNotForStorage(request);
195 List<string> _names = (List<string>)request[
"Names"];
196 names = _names.ToArray();
197 List<string> _values = (List<string>)request[
"Values"];
198 values = _values.ToArray();
200 if (m_AvatarService.SetItems(user, names, values))
201 return SuccessResult();
203 return FailureResult();
206 byte[] RemoveItems(Dictionary<string, object> request)
208 UUID user = UUID.Zero;
211 if (!request.ContainsKey(
"UserID") || !request.ContainsKey(
"Names"))
212 return FailureResult();
214 if (!
UUID.TryParse(request[
"UserID"].ToString(), out user))
215 return FailureResult();
217 if (!(request[
"Names"] is List<string>))
218 return FailureResult();
220 List<string> _names = (List<string>)request[
"Names"];
221 names = _names.ToArray();
223 if (m_AvatarService.RemoveItems(user, names))
224 return SuccessResult();
226 return FailureResult();
231 private byte[] SuccessResult()
233 XmlDocument doc =
new XmlDocument();
235 XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
238 doc.AppendChild(xmlnode);
240 XmlElement rootElement = doc.CreateElement(
"",
"ServerResponse",
243 doc.AppendChild(rootElement);
245 XmlElement result = doc.CreateElement(
"",
"result",
"");
246 result.AppendChild(doc.CreateTextNode(
"Success"));
248 rootElement.AppendChild(result);
250 return Util.DocToBytes(doc);
253 private byte[] FailureResult()
255 XmlDocument doc =
new XmlDocument();
257 XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
260 doc.AppendChild(xmlnode);
262 XmlElement rootElement = doc.CreateElement(
"",
"ServerResponse",
265 doc.AppendChild(rootElement);
267 XmlElement result = doc.CreateElement(
"",
"result",
"");
268 result.AppendChild(doc.CreateTextNode(
"Failure"));
270 rootElement.AppendChild(result);
272 return Util.DocToBytes(doc);
override byte[] ProcessRequest(string path, Stream requestData, IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
Base streamed request handler.
AvatarServerPostHandler(IAvatarService service, IServiceAuth auth)
Each region/client that uses avatars will have a data structure of this type representing the avatars...