diff options
Diffstat (limited to 'OpenSim/Server/Handlers/Friends/FriendsServerPostHandler.cs')
-rw-r--r-- | OpenSim/Server/Handlers/Friends/FriendsServerPostHandler.cs | 282 |
1 files changed, 282 insertions, 0 deletions
diff --git a/OpenSim/Server/Handlers/Friends/FriendsServerPostHandler.cs b/OpenSim/Server/Handlers/Friends/FriendsServerPostHandler.cs new file mode 100644 index 0000000..3aab30b --- /dev/null +++ b/OpenSim/Server/Handlers/Friends/FriendsServerPostHandler.cs | |||
@@ -0,0 +1,282 @@ | |||
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 FriendInfo = OpenSim.Services.Interfaces.FriendInfo; | ||
42 | using OpenSim.Framework; | ||
43 | using OpenSim.Framework.ServiceAuth; | ||
44 | using OpenSim.Framework.Servers.HttpServer; | ||
45 | using OpenMetaverse; | ||
46 | |||
47 | namespace OpenSim.Server.Handlers.Friends | ||
48 | { | ||
49 | public class FriendsServerPostHandler : BaseStreamHandler | ||
50 | { | ||
51 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
52 | |||
53 | private IFriendsService m_FriendsService; | ||
54 | |||
55 | public FriendsServerPostHandler(IFriendsService service, IServiceAuth auth) : | ||
56 | base("POST", "/friends", auth) | ||
57 | { | ||
58 | m_FriendsService = service; | ||
59 | } | ||
60 | |||
61 | protected override byte[] ProcessRequest(string path, Stream requestData, | ||
62 | IOSHttpRequest httpRequest, IOSHttpResponse httpResponse) | ||
63 | { | ||
64 | StreamReader sr = new StreamReader(requestData); | ||
65 | string body = sr.ReadToEnd(); | ||
66 | sr.Close(); | ||
67 | body = body.Trim(); | ||
68 | |||
69 | //m_log.DebugFormat("[XXX]: query String: {0}", body); | ||
70 | |||
71 | try | ||
72 | { | ||
73 | Dictionary<string, object> request = | ||
74 | ServerUtils.ParseQueryString(body); | ||
75 | |||
76 | if (!request.ContainsKey("METHOD")) | ||
77 | return FailureResult(); | ||
78 | |||
79 | string method = request["METHOD"].ToString(); | ||
80 | |||
81 | switch (method) | ||
82 | { | ||
83 | case "getfriends": | ||
84 | return GetFriends(request); | ||
85 | |||
86 | case "getfriends_string": | ||
87 | return GetFriendsString(request); | ||
88 | |||
89 | case "storefriend": | ||
90 | return StoreFriend(request); | ||
91 | |||
92 | case "deletefriend": | ||
93 | return DeleteFriend(request); | ||
94 | |||
95 | case "deletefriend_string": | ||
96 | return DeleteFriendString(request); | ||
97 | |||
98 | } | ||
99 | |||
100 | m_log.DebugFormat("[FRIENDS HANDLER]: unknown method request {0}", method); | ||
101 | } | ||
102 | catch (Exception e) | ||
103 | { | ||
104 | m_log.DebugFormat("[FRIENDS HANDLER]: Exception {0}", e); | ||
105 | } | ||
106 | |||
107 | return FailureResult(); | ||
108 | } | ||
109 | |||
110 | #region Method-specific handlers | ||
111 | |||
112 | byte[] GetFriends(Dictionary<string, object> request) | ||
113 | { | ||
114 | UUID principalID = UUID.Zero; | ||
115 | if (request.ContainsKey("PRINCIPALID")) | ||
116 | UUID.TryParse(request["PRINCIPALID"].ToString(), out principalID); | ||
117 | else | ||
118 | m_log.WarnFormat("[FRIENDS HANDLER]: no principalID in request to get friends"); | ||
119 | |||
120 | FriendInfo[] finfos = m_FriendsService.GetFriends(principalID); | ||
121 | |||
122 | return PackageFriends(finfos); | ||
123 | } | ||
124 | |||
125 | byte[] GetFriendsString(Dictionary<string, object> request) | ||
126 | { | ||
127 | string principalID = string.Empty; | ||
128 | if (request.ContainsKey("PRINCIPALID")) | ||
129 | principalID = request["PRINCIPALID"].ToString(); | ||
130 | else | ||
131 | m_log.WarnFormat("[FRIENDS HANDLER]: no principalID in request to get friends"); | ||
132 | |||
133 | FriendInfo[] finfos = m_FriendsService.GetFriends(principalID); | ||
134 | |||
135 | return PackageFriends(finfos); | ||
136 | } | ||
137 | |||
138 | private byte[] PackageFriends(FriendInfo[] finfos) | ||
139 | { | ||
140 | |||
141 | Dictionary<string, object> result = new Dictionary<string, object>(); | ||
142 | if ((finfos == null) || ((finfos != null) && (finfos.Length == 0))) | ||
143 | result["result"] = "null"; | ||
144 | else | ||
145 | { | ||
146 | int i = 0; | ||
147 | foreach (FriendInfo finfo in finfos) | ||
148 | { | ||
149 | Dictionary<string, object> rinfoDict = finfo.ToKeyValuePairs(); | ||
150 | result["friend" + i] = rinfoDict; | ||
151 | i++; | ||
152 | } | ||
153 | } | ||
154 | |||
155 | string xmlString = ServerUtils.BuildXmlResponse(result); | ||
156 | |||
157 | //m_log.DebugFormat("[FRIENDS HANDLER]: resp string: {0}", xmlString); | ||
158 | return Util.UTF8NoBomEncoding.GetBytes(xmlString); | ||
159 | } | ||
160 | |||
161 | byte[] StoreFriend(Dictionary<string, object> request) | ||
162 | { | ||
163 | string principalID = string.Empty, friend = string.Empty; int flags = 0; | ||
164 | FromKeyValuePairs(request, out principalID, out friend, out flags); | ||
165 | bool success = m_FriendsService.StoreFriend(principalID, friend, flags); | ||
166 | |||
167 | if (success) | ||
168 | return SuccessResult(); | ||
169 | else | ||
170 | return FailureResult(); | ||
171 | } | ||
172 | |||
173 | byte[] DeleteFriend(Dictionary<string, object> request) | ||
174 | { | ||
175 | UUID principalID = UUID.Zero; | ||
176 | if (request.ContainsKey("PRINCIPALID")) | ||
177 | UUID.TryParse(request["PRINCIPALID"].ToString(), out principalID); | ||
178 | else | ||
179 | m_log.WarnFormat("[FRIENDS HANDLER]: no principalID in request to delete friend"); | ||
180 | string friend = string.Empty; | ||
181 | if (request.ContainsKey("FRIEND")) | ||
182 | friend = request["FRIEND"].ToString(); | ||
183 | |||
184 | bool success = m_FriendsService.Delete(principalID, friend); | ||
185 | if (success) | ||
186 | return SuccessResult(); | ||
187 | else | ||
188 | return FailureResult(); | ||
189 | } | ||
190 | |||
191 | byte[] DeleteFriendString(Dictionary<string, object> request) | ||
192 | { | ||
193 | string principalID = string.Empty; | ||
194 | if (request.ContainsKey("PRINCIPALID")) | ||
195 | principalID = request["PRINCIPALID"].ToString(); | ||
196 | else | ||
197 | m_log.WarnFormat("[FRIENDS HANDLER]: no principalID in request to delete friend"); | ||
198 | string friend = string.Empty; | ||
199 | if (request.ContainsKey("FRIEND")) | ||
200 | friend = request["FRIEND"].ToString(); | ||
201 | |||
202 | bool success = m_FriendsService.Delete(principalID, friend); | ||
203 | if (success) | ||
204 | return SuccessResult(); | ||
205 | else | ||
206 | return FailureResult(); | ||
207 | } | ||
208 | |||
209 | #endregion | ||
210 | |||
211 | #region Misc | ||
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 Util.DocToBytes(doc); | ||
233 | } | ||
234 | |||
235 | private byte[] FailureResult() | ||
236 | { | ||
237 | return FailureResult(String.Empty); | ||
238 | } | ||
239 | |||
240 | private byte[] FailureResult(string msg) | ||
241 | { | ||
242 | XmlDocument doc = new XmlDocument(); | ||
243 | |||
244 | XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration, | ||
245 | "", ""); | ||
246 | |||
247 | doc.AppendChild(xmlnode); | ||
248 | |||
249 | XmlElement rootElement = doc.CreateElement("", "ServerResponse", | ||
250 | ""); | ||
251 | |||
252 | doc.AppendChild(rootElement); | ||
253 | |||
254 | XmlElement result = doc.CreateElement("", "Result", ""); | ||
255 | result.AppendChild(doc.CreateTextNode("Failure")); | ||
256 | |||
257 | rootElement.AppendChild(result); | ||
258 | |||
259 | XmlElement message = doc.CreateElement("", "Message", ""); | ||
260 | message.AppendChild(doc.CreateTextNode(msg)); | ||
261 | |||
262 | rootElement.AppendChild(message); | ||
263 | |||
264 | return Util.DocToBytes(doc); | ||
265 | } | ||
266 | |||
267 | void FromKeyValuePairs(Dictionary<string, object> kvp, out string principalID, out string friend, out int flags) | ||
268 | { | ||
269 | principalID = string.Empty; | ||
270 | if (kvp.ContainsKey("PrincipalID") && kvp["PrincipalID"] != null) | ||
271 | principalID = kvp["PrincipalID"].ToString(); | ||
272 | friend = string.Empty; | ||
273 | if (kvp.ContainsKey("Friend") && kvp["Friend"] != null) | ||
274 | friend = kvp["Friend"].ToString(); | ||
275 | flags = 0; | ||
276 | if (kvp.ContainsKey("MyFlags") && kvp["MyFlags"] != null) | ||
277 | Int32.TryParse(kvp["MyFlags"].ToString(), out flags); | ||
278 | } | ||
279 | |||
280 | #endregion | ||
281 | } | ||
282 | } | ||