diff options
Diffstat (limited to 'OpenSim/Server/Handlers/Avatar/AvatarServerPostHandler.cs')
-rw-r--r-- | OpenSim/Server/Handlers/Avatar/AvatarServerPostHandler.cs | 269 |
1 files changed, 269 insertions, 0 deletions
diff --git a/OpenSim/Server/Handlers/Avatar/AvatarServerPostHandler.cs b/OpenSim/Server/Handlers/Avatar/AvatarServerPostHandler.cs new file mode 100644 index 0000000..49c2e43 --- /dev/null +++ b/OpenSim/Server/Handlers/Avatar/AvatarServerPostHandler.cs | |||
@@ -0,0 +1,269 @@ | |||
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.Avatar | ||
46 | { | ||
47 | public class AvatarServerPostHandler : BaseStreamHandler | ||
48 | { | ||
49 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
50 | |||
51 | private IAvatarService m_AvatarService; | ||
52 | |||
53 | public AvatarServerPostHandler(IAvatarService service) : | ||
54 | base("POST", "/avatar") | ||
55 | { | ||
56 | m_AvatarService = service; | ||
57 | } | ||
58 | |||
59 | public override byte[] Handle(string path, Stream requestData, | ||
60 | OSHttpRequest httpRequest, OSHttpResponse 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 | |||
69 | try | ||
70 | { | ||
71 | Dictionary<string, object> request = | ||
72 | ServerUtils.ParseQueryString(body); | ||
73 | |||
74 | if (!request.ContainsKey("METHOD")) | ||
75 | return FailureResult(); | ||
76 | |||
77 | string method = request["METHOD"].ToString(); | ||
78 | |||
79 | switch (method) | ||
80 | { | ||
81 | case "getavatar": | ||
82 | return GetAvatar(request); | ||
83 | case "setavatar": | ||
84 | return SetAvatar(request); | ||
85 | case "resetavatar": | ||
86 | return ResetAvatar(request); | ||
87 | case "setitems": | ||
88 | return SetItems(request); | ||
89 | case "removeitems": | ||
90 | return RemoveItems(request); | ||
91 | } | ||
92 | m_log.DebugFormat("[AVATAR HANDLER]: unknown method request: {0}", method); | ||
93 | } | ||
94 | catch (Exception e) | ||
95 | { | ||
96 | m_log.Debug("[AVATAR HANDLER]: Exception {0}" + e); | ||
97 | } | ||
98 | |||
99 | return FailureResult(); | ||
100 | |||
101 | } | ||
102 | |||
103 | byte[] GetAvatar(Dictionary<string, object> request) | ||
104 | { | ||
105 | UUID user = UUID.Zero; | ||
106 | |||
107 | if (!request.ContainsKey("UserID")) | ||
108 | return FailureResult(); | ||
109 | |||
110 | if (UUID.TryParse(request["UserID"].ToString(), out user)) | ||
111 | { | ||
112 | AvatarData avatar = m_AvatarService.GetAvatar(user); | ||
113 | if (avatar == null) | ||
114 | return FailureResult(); | ||
115 | |||
116 | Dictionary<string, object> result = new Dictionary<string, object>(); | ||
117 | if (avatar == null) | ||
118 | result["result"] = "null"; | ||
119 | else | ||
120 | result["result"] = avatar.ToKeyValuePairs(); | ||
121 | |||
122 | string xmlString = ServerUtils.BuildXmlResponse(result); | ||
123 | |||
124 | UTF8Encoding encoding = new UTF8Encoding(); | ||
125 | return encoding.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 | AvatarData avatar = new AvatarData(request); | ||
142 | if (m_AvatarService.SetAvatar(user, avatar)) | ||
143 | return SuccessResult(); | ||
144 | |||
145 | return FailureResult(); | ||
146 | } | ||
147 | |||
148 | byte[] ResetAvatar(Dictionary<string, object> request) | ||
149 | { | ||
150 | UUID user = UUID.Zero; | ||
151 | if (!request.ContainsKey("UserID")) | ||
152 | return FailureResult(); | ||
153 | |||
154 | if (!UUID.TryParse(request["UserID"].ToString(), out user)) | ||
155 | return FailureResult(); | ||
156 | |||
157 | if (m_AvatarService.ResetAvatar(user)) | ||
158 | return SuccessResult(); | ||
159 | |||
160 | return FailureResult(); | ||
161 | } | ||
162 | |||
163 | byte[] SetItems(Dictionary<string, object> request) | ||
164 | { | ||
165 | UUID user = UUID.Zero; | ||
166 | string[] names, values; | ||
167 | |||
168 | if (!request.ContainsKey("UserID") || !request.ContainsKey("Names") || !request.ContainsKey("Values")) | ||
169 | return FailureResult(); | ||
170 | |||
171 | if (!UUID.TryParse(request["UserID"].ToString(), out user)) | ||
172 | return FailureResult(); | ||
173 | |||
174 | if (!(request["Names"] is List<string> || request["Values"] is List<string>)) | ||
175 | return FailureResult(); | ||
176 | |||
177 | List<string> _names = (List<string>)request["Names"]; | ||
178 | names = _names.ToArray(); | ||
179 | List<string> _values = (List<string>)request["Values"]; | ||
180 | values = _values.ToArray(); | ||
181 | |||
182 | if (m_AvatarService.SetItems(user, names, values)) | ||
183 | return SuccessResult(); | ||
184 | |||
185 | return FailureResult(); | ||
186 | } | ||
187 | |||
188 | byte[] RemoveItems(Dictionary<string, object> request) | ||
189 | { | ||
190 | UUID user = UUID.Zero; | ||
191 | string[] names; | ||
192 | |||
193 | if (!request.ContainsKey("UserID") || !request.ContainsKey("Names")) | ||
194 | return FailureResult(); | ||
195 | |||
196 | if (!UUID.TryParse(request["UserID"].ToString(), out user)) | ||
197 | return FailureResult(); | ||
198 | |||
199 | if (!(request["Names"] is List<string>)) | ||
200 | return FailureResult(); | ||
201 | |||
202 | List<string> _names = (List<string>)request["Names"]; | ||
203 | names = _names.ToArray(); | ||
204 | |||
205 | if (m_AvatarService.RemoveItems(user, names)) | ||
206 | return SuccessResult(); | ||
207 | |||
208 | return FailureResult(); | ||
209 | } | ||
210 | |||
211 | |||
212 | |||
213 | private byte[] SuccessResult() | ||
214 | { | ||
215 | XmlDocument doc = new XmlDocument(); | ||
216 | |||
217 | XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration, | ||
218 | "", ""); | ||
219 | |||
220 | doc.AppendChild(xmlnode); | ||
221 | |||
222 | XmlElement rootElement = doc.CreateElement("", "ServerResponse", | ||
223 | ""); | ||
224 | |||
225 | doc.AppendChild(rootElement); | ||
226 | |||
227 | XmlElement result = doc.CreateElement("", "result", ""); | ||
228 | result.AppendChild(doc.CreateTextNode("Success")); | ||
229 | |||
230 | rootElement.AppendChild(result); | ||
231 | |||
232 | return DocToBytes(doc); | ||
233 | } | ||
234 | |||
235 | private byte[] FailureResult() | ||
236 | { | ||
237 | XmlDocument doc = new XmlDocument(); | ||
238 | |||
239 | XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration, | ||
240 | "", ""); | ||
241 | |||
242 | doc.AppendChild(xmlnode); | ||
243 | |||
244 | XmlElement rootElement = doc.CreateElement("", "ServerResponse", | ||
245 | ""); | ||
246 | |||
247 | doc.AppendChild(rootElement); | ||
248 | |||
249 | XmlElement result = doc.CreateElement("", "result", ""); | ||
250 | result.AppendChild(doc.CreateTextNode("Failure")); | ||
251 | |||
252 | rootElement.AppendChild(result); | ||
253 | |||
254 | return DocToBytes(doc); | ||
255 | } | ||
256 | |||
257 | private byte[] DocToBytes(XmlDocument doc) | ||
258 | { | ||
259 | MemoryStream ms = new MemoryStream(); | ||
260 | XmlTextWriter xw = new XmlTextWriter(ms, null); | ||
261 | xw.Formatting = Formatting.Indented; | ||
262 | doc.WriteTo(xw); | ||
263 | xw.Flush(); | ||
264 | |||
265 | return ms.ToArray(); | ||
266 | } | ||
267 | |||
268 | } | ||
269 | } | ||