aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Server/Handlers/Friends/FriendsServerPostHandler.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Server/Handlers/Friends/FriendsServerPostHandler.cs')
-rw-r--r--OpenSim/Server/Handlers/Friends/FriendsServerPostHandler.cs238
1 files changed, 238 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..b168bb3
--- /dev/null
+++ b/OpenSim/Server/Handlers/Friends/FriendsServerPostHandler.cs
@@ -0,0 +1,238 @@
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
28using Nini.Config;
29using log4net;
30using System;
31using System.Reflection;
32using System.IO;
33using System.Net;
34using System.Text;
35using System.Text.RegularExpressions;
36using System.Xml;
37using System.Xml.Serialization;
38using System.Collections.Generic;
39using OpenSim.Server.Base;
40using OpenSim.Services.Interfaces;
41using FriendInfo = OpenSim.Services.Interfaces.FriendInfo;
42using OpenSim.Framework;
43using OpenSim.Framework.Servers.HttpServer;
44using OpenMetaverse;
45
46namespace OpenSim.Server.Handlers.Friends
47{
48 public class FriendsServerPostHandler : BaseStreamHandler
49 {
50 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
51
52 private IFriendsService m_FriendsService;
53
54 public FriendsServerPostHandler(IFriendsService service) :
55 base("POST", "/friends")
56 {
57 m_FriendsService = service;
58 }
59
60 public override byte[] Handle(string path, Stream requestData,
61 OSHttpRequest httpRequest, OSHttpResponse 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 "getfriends":
83 return GetFriends(request);
84
85 case "storefriend":
86 return StoreFriend(request);
87
88 case "deletefriend":
89 return DeleteFriend(request);
90
91 }
92 m_log.DebugFormat("[FRIENDS HANDLER]: unknown method {0} request {1}", method.Length, method);
93 }
94 catch (Exception e)
95 {
96 m_log.DebugFormat("[FRIENDS HANDLER]: Exception {0}", e);
97 }
98
99 return FailureResult();
100
101 }
102
103 #region Method-specific handlers
104
105 byte[] GetFriends(Dictionary<string, object> request)
106 {
107 UUID principalID = UUID.Zero;
108 if (request.ContainsKey("PRINCIPALID"))
109 UUID.TryParse(request["PRINCIPALID"].ToString(), out principalID);
110 else
111 m_log.WarnFormat("[FRIENDS HANDLER]: no principalID in request to get friends");
112
113 FriendInfo[] finfos = m_FriendsService.GetFriends(principalID);
114 //m_log.DebugFormat("[FRIENDS HANDLER]: neighbours for region {0}: {1}", regionID, rinfos.Count);
115
116 Dictionary<string, object> result = new Dictionary<string, object>();
117 if ((finfos == null) || ((finfos != null) && (finfos.Length == 0)))
118 result["result"] = "null";
119 else
120 {
121 int i = 0;
122 foreach (FriendInfo finfo in finfos)
123 {
124 Dictionary<string, object> rinfoDict = finfo.ToKeyValuePairs();
125 result["friend" + i] = rinfoDict;
126 i++;
127 }
128 }
129
130 string xmlString = ServerUtils.BuildXmlResponse(result);
131 //m_log.DebugFormat("[FRIENDS HANDLER]: resp string: {0}", xmlString);
132 UTF8Encoding encoding = new UTF8Encoding();
133 return encoding.GetBytes(xmlString);
134
135 }
136
137 byte[] StoreFriend(Dictionary<string, object> request)
138 {
139 FriendInfo friend = new FriendInfo(request);
140
141 bool success = m_FriendsService.StoreFriend(friend.PrincipalID, friend.Friend, friend.MyFlags);
142
143 if (success)
144 return SuccessResult();
145 else
146 return FailureResult();
147 }
148
149 byte[] DeleteFriend(Dictionary<string, object> request)
150 {
151 UUID principalID = UUID.Zero;
152 if (request.ContainsKey("PRINCIPALID"))
153 UUID.TryParse(request["PRINCIPALID"].ToString(), out principalID);
154 else
155 m_log.WarnFormat("[FRIENDS HANDLER]: no principalID in request to delete friend");
156 string friend = string.Empty;
157 if (request.ContainsKey("FRIEND"))
158 friend = request["FRIEND"].ToString();
159
160 bool success = m_FriendsService.Delete(principalID, friend);
161 if (success)
162 return SuccessResult();
163 else
164 return FailureResult();
165 }
166
167 #endregion
168
169 #region Misc
170
171 private byte[] SuccessResult()
172 {
173 XmlDocument doc = new XmlDocument();
174
175 XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
176 "", "");
177
178 doc.AppendChild(xmlnode);
179
180 XmlElement rootElement = doc.CreateElement("", "ServerResponse",
181 "");
182
183 doc.AppendChild(rootElement);
184
185 XmlElement result = doc.CreateElement("", "Result", "");
186 result.AppendChild(doc.CreateTextNode("Success"));
187
188 rootElement.AppendChild(result);
189
190 return DocToBytes(doc);
191 }
192
193 private byte[] FailureResult()
194 {
195 return FailureResult(String.Empty);
196 }
197
198 private byte[] FailureResult(string msg)
199 {
200 XmlDocument doc = new XmlDocument();
201
202 XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
203 "", "");
204
205 doc.AppendChild(xmlnode);
206
207 XmlElement rootElement = doc.CreateElement("", "ServerResponse",
208 "");
209
210 doc.AppendChild(rootElement);
211
212 XmlElement result = doc.CreateElement("", "Result", "");
213 result.AppendChild(doc.CreateTextNode("Failure"));
214
215 rootElement.AppendChild(result);
216
217 XmlElement message = doc.CreateElement("", "Message", "");
218 message.AppendChild(doc.CreateTextNode(msg));
219
220 rootElement.AppendChild(message);
221
222 return DocToBytes(doc);
223 }
224
225 private byte[] DocToBytes(XmlDocument doc)
226 {
227 MemoryStream ms = new MemoryStream();
228 XmlTextWriter xw = new XmlTextWriter(ms, null);
229 xw.Formatting = Formatting.Indented;
230 doc.WriteTo(xw);
231 xw.Flush();
232
233 return ms.ToArray();
234 }
235
236 #endregion
237 }
238}