aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Server/Handlers/Friends
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Server/Handlers/Friends')
-rw-r--r--OpenSim/Server/Handlers/Friends/FriendServerConnector.cs63
-rw-r--r--OpenSim/Server/Handlers/Friends/FriendsServerPostHandler.cs282
2 files changed, 345 insertions, 0 deletions
diff --git a/OpenSim/Server/Handlers/Friends/FriendServerConnector.cs b/OpenSim/Server/Handlers/Friends/FriendServerConnector.cs
new file mode 100644
index 0000000..b0e6c7d
--- /dev/null
+++ b/OpenSim/Server/Handlers/Friends/FriendServerConnector.cs
@@ -0,0 +1,63 @@
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 System;
29using Nini.Config;
30using OpenSim.Server.Base;
31using OpenSim.Services.Interfaces;
32using OpenSim.Framework.ServiceAuth;
33using OpenSim.Framework.Servers.HttpServer;
34using OpenSim.Server.Handlers.Base;
35
36namespace OpenSim.Server.Handlers.Friends
37{
38 public class FriendsServiceConnector : ServiceConnector
39 {
40 private IFriendsService m_FriendsService;
41 private string m_ConfigName = "FriendsService";
42
43 public FriendsServiceConnector(IConfigSource config, IHttpServer server, string configName) :
44 base(config, server, configName)
45 {
46 IConfig serverConfig = config.Configs[m_ConfigName];
47 if (serverConfig == null)
48 throw new Exception(String.Format("No section {0} in config file", m_ConfigName));
49
50 string theService = serverConfig.GetString("LocalServiceModule",
51 String.Empty);
52
53 if (theService == String.Empty)
54 throw new Exception("No LocalServiceModule in config file");
55
56 Object[] args = new Object[] { config };
57 m_FriendsService = ServerUtils.LoadPlugin<IFriendsService>(theService, args);
58
59 IServiceAuth auth = ServiceAuth.Create(config, m_ConfigName);
60 server.AddStreamHandler(new FriendsServerPostHandler(m_FriendsService, auth));
61 }
62 }
63}
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
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.ServiceAuth;
44using OpenSim.Framework.Servers.HttpServer;
45using OpenMetaverse;
46
47namespace 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}