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;
42 using OpenSim.Framework;
43 using OpenSim.Framework.ServiceAuth;
44 using OpenSim.Framework.Servers.HttpServer;
47 namespace OpenSim.Server.Handlers.Friends
51 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
56 base(
"POST",
"/friends", auth)
58 m_FriendsService = service;
64 StreamReader sr =
new StreamReader(requestData);
65 string body = sr.ReadToEnd();
73 Dictionary<string, object> request =
74 ServerUtils.ParseQueryString(body);
76 if (!request.ContainsKey(
"METHOD"))
77 return FailureResult();
79 string method = request[
"METHOD"].ToString();
84 return GetFriends(request);
86 case "getfriends_string":
87 return GetFriendsString(request);
90 return StoreFriend(request);
93 return DeleteFriend(request);
95 case "deletefriend_string":
96 return DeleteFriendString(request);
100 m_log.DebugFormat(
"[FRIENDS HANDLER]: unknown method request {0}", method);
104 m_log.DebugFormat(
"[FRIENDS HANDLER]: Exception {0}", e);
107 return FailureResult();
110 #region Method-specific handlers
112 byte[] GetFriends(Dictionary<string, object> request)
114 UUID principalID = UUID.Zero;
115 if (request.ContainsKey(
"PRINCIPALID"))
116 UUID.TryParse(request[
"PRINCIPALID"].ToString(), out principalID);
118 m_log.WarnFormat(
"[FRIENDS HANDLER]: no principalID in request to get friends");
120 FriendInfo[] finfos = m_FriendsService.GetFriends(principalID);
122 return PackageFriends(finfos);
125 byte[] GetFriendsString(Dictionary<string, object> request)
127 string principalID = string.Empty;
128 if (request.ContainsKey(
"PRINCIPALID"))
129 principalID = request[
"PRINCIPALID"].ToString();
131 m_log.WarnFormat(
"[FRIENDS HANDLER]: no principalID in request to get friends");
133 FriendInfo[] finfos = m_FriendsService.GetFriends(principalID);
135 return PackageFriends(finfos);
138 private byte[] PackageFriends(
FriendInfo[] finfos)
141 Dictionary<string, object> result =
new Dictionary<string, object>();
142 if ((finfos == null) || ((finfos != null) && (finfos.Length == 0)))
143 result[
"result"] =
"null";
149 Dictionary<string, object> rinfoDict = finfo.ToKeyValuePairs();
150 result[
"friend" + i] = rinfoDict;
155 string xmlString = ServerUtils.BuildXmlResponse(result);
158 return Util.UTF8NoBomEncoding.GetBytes(xmlString);
161 byte[] StoreFriend(Dictionary<string, object> request)
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);
168 return SuccessResult();
170 return FailureResult();
173 byte[] DeleteFriend(Dictionary<string, object> request)
175 UUID principalID = UUID.Zero;
176 if (request.ContainsKey(
"PRINCIPALID"))
177 UUID.TryParse(request[
"PRINCIPALID"].ToString(), out principalID);
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();
184 bool success = m_FriendsService.Delete(principalID,
friend);
186 return SuccessResult();
188 return FailureResult();
191 byte[] DeleteFriendString(Dictionary<string, object> request)
193 string principalID = string.Empty;
194 if (request.ContainsKey(
"PRINCIPALID"))
195 principalID = request[
"PRINCIPALID"].ToString();
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();
202 bool success = m_FriendsService.Delete(principalID,
friend);
204 return SuccessResult();
206 return FailureResult();
213 private byte[] SuccessResult()
215 XmlDocument doc =
new XmlDocument();
217 XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
220 doc.AppendChild(xmlnode);
222 XmlElement rootElement = doc.CreateElement(
"",
"ServerResponse",
225 doc.AppendChild(rootElement);
227 XmlElement result = doc.CreateElement(
"",
"Result",
"");
228 result.AppendChild(doc.CreateTextNode(
"Success"));
230 rootElement.AppendChild(result);
232 return Util.DocToBytes(doc);
235 private byte[] FailureResult()
237 return FailureResult(
String.Empty);
240 private byte[] FailureResult(
string msg)
242 XmlDocument doc =
new XmlDocument();
244 XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
247 doc.AppendChild(xmlnode);
249 XmlElement rootElement = doc.CreateElement(
"",
"ServerResponse",
252 doc.AppendChild(rootElement);
254 XmlElement result = doc.CreateElement(
"",
"Result",
"");
255 result.AppendChild(doc.CreateTextNode(
"Failure"));
257 rootElement.AppendChild(result);
259 XmlElement message = doc.CreateElement(
"",
"Message",
"");
260 message.AppendChild(doc.CreateTextNode(msg));
262 rootElement.AppendChild(message);
264 return Util.DocToBytes(doc);
267 void FromKeyValuePairs(Dictionary<string, object> kvp, out
string principalID, out
string friend, out
int flags)
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();
276 if (kvp.ContainsKey(
"MyFlags") && kvp[
"MyFlags"] != null)
277 Int32.TryParse(kvp[
"MyFlags"].ToString(), out flags);
Base streamed request handler.
FriendsServerPostHandler(IFriendsService service, IServiceAuth auth)
OpenSim.Services.Interfaces.FriendInfo FriendInfo
override byte[] ProcessRequest(string path, Stream requestData, IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)