opensim-development – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 eva 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.Servers.HttpServer;
44 using OpenMetaverse;
45  
46 namespace 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 protected override byte[] ProcessRequest(string path, Stream requestData,
61 IOSHttpRequest httpRequest, IOSHttpResponse 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 "getfriends_string":
86 return GetFriendsString(request);
87  
88 case "storefriend":
89 return StoreFriend(request);
90  
91 case "deletefriend":
92 return DeleteFriend(request);
93  
94 case "deletefriend_string":
95 return DeleteFriendString(request);
96  
97 }
98  
99 m_log.DebugFormat("[FRIENDS HANDLER]: unknown method request {0}", method);
100 }
101 catch (Exception e)
102 {
103 m_log.DebugFormat("[FRIENDS HANDLER]: Exception {0}", e);
104 }
105  
106 return FailureResult();
107 }
108  
109 #region Method-specific handlers
110  
111 byte[] GetFriends(Dictionary<string, object> request)
112 {
113 UUID principalID = UUID.Zero;
114 if (request.ContainsKey("PRINCIPALID"))
115 UUID.TryParse(request["PRINCIPALID"].ToString(), out principalID);
116 else
117 m_log.WarnFormat("[FRIENDS HANDLER]: no principalID in request to get friends");
118  
119 FriendInfo[] finfos = m_FriendsService.GetFriends(principalID);
120  
121 return PackageFriends(finfos);
122 }
123  
124 byte[] GetFriendsString(Dictionary<string, object> request)
125 {
126 string principalID = string.Empty;
127 if (request.ContainsKey("PRINCIPALID"))
128 principalID = request["PRINCIPALID"].ToString();
129 else
130 m_log.WarnFormat("[FRIENDS HANDLER]: no principalID in request to get friends");
131  
132 FriendInfo[] finfos = m_FriendsService.GetFriends(principalID);
133  
134 return PackageFriends(finfos);
135 }
136  
137 private byte[] PackageFriends(FriendInfo[] finfos)
138 {
139  
140 Dictionary<string, object> result = new Dictionary<string, object>();
141 if ((finfos == null) || ((finfos != null) && (finfos.Length == 0)))
142 result["result"] = "null";
143 else
144 {
145 int i = 0;
146 foreach (FriendInfo finfo in finfos)
147 {
148 Dictionary<string, object> rinfoDict = finfo.ToKeyValuePairs();
149 result["friend" + i] = rinfoDict;
150 i++;
151 }
152 }
153  
154 string xmlString = ServerUtils.BuildXmlResponse(result);
155  
156 //m_log.DebugFormat("[FRIENDS HANDLER]: resp string: {0}", xmlString);
157 return Util.UTF8NoBomEncoding.GetBytes(xmlString);
158 }
159  
160 byte[] StoreFriend(Dictionary<string, object> request)
161 {
162 string principalID = string.Empty, friend = string.Empty; int flags = 0;
163 FromKeyValuePairs(request, out principalID, out friend, out flags);
164 bool success = m_FriendsService.StoreFriend(principalID, friend, flags);
165  
166 if (success)
167 return SuccessResult();
168 else
169 return FailureResult();
170 }
171  
172 byte[] DeleteFriend(Dictionary<string, object> request)
173 {
174 UUID principalID = UUID.Zero;
175 if (request.ContainsKey("PRINCIPALID"))
176 UUID.TryParse(request["PRINCIPALID"].ToString(), out principalID);
177 else
178 m_log.WarnFormat("[FRIENDS HANDLER]: no principalID in request to delete friend");
179 string friend = string.Empty;
180 if (request.ContainsKey("FRIEND"))
181 friend = request["FRIEND"].ToString();
182  
183 bool success = m_FriendsService.Delete(principalID, friend);
184 if (success)
185 return SuccessResult();
186 else
187 return FailureResult();
188 }
189  
190 byte[] DeleteFriendString(Dictionary<string, object> request)
191 {
192 string principalID = string.Empty;
193 if (request.ContainsKey("PRINCIPALID"))
194 principalID = request["PRINCIPALID"].ToString();
195 else
196 m_log.WarnFormat("[FRIENDS HANDLER]: no principalID in request to delete friend");
197 string friend = string.Empty;
198 if (request.ContainsKey("FRIEND"))
199 friend = request["FRIEND"].ToString();
200  
201 bool success = m_FriendsService.Delete(principalID, friend);
202 if (success)
203 return SuccessResult();
204 else
205 return FailureResult();
206 }
207  
208 #endregion
209  
210 #region Misc
211  
212 private byte[] SuccessResult()
213 {
214 XmlDocument doc = new XmlDocument();
215  
216 XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
217 "", "");
218  
219 doc.AppendChild(xmlnode);
220  
221 XmlElement rootElement = doc.CreateElement("", "ServerResponse",
222 "");
223  
224 doc.AppendChild(rootElement);
225  
226 XmlElement result = doc.CreateElement("", "Result", "");
227 result.AppendChild(doc.CreateTextNode("Success"));
228  
229 rootElement.AppendChild(result);
230  
231 return DocToBytes(doc);
232 }
233  
234 private byte[] FailureResult()
235 {
236 return FailureResult(String.Empty);
237 }
238  
239 private byte[] FailureResult(string msg)
240 {
241 XmlDocument doc = new XmlDocument();
242  
243 XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
244 "", "");
245  
246 doc.AppendChild(xmlnode);
247  
248 XmlElement rootElement = doc.CreateElement("", "ServerResponse",
249 "");
250  
251 doc.AppendChild(rootElement);
252  
253 XmlElement result = doc.CreateElement("", "Result", "");
254 result.AppendChild(doc.CreateTextNode("Failure"));
255  
256 rootElement.AppendChild(result);
257  
258 XmlElement message = doc.CreateElement("", "Message", "");
259 message.AppendChild(doc.CreateTextNode(msg));
260  
261 rootElement.AppendChild(message);
262  
263 return DocToBytes(doc);
264 }
265  
266 private byte[] DocToBytes(XmlDocument doc)
267 {
268 MemoryStream ms = new MemoryStream();
269 XmlTextWriter xw = new XmlTextWriter(ms, null);
270 xw.Formatting = Formatting.Indented;
271 doc.WriteTo(xw);
272 xw.Flush();
273  
274 return ms.ToArray();
275 }
276  
277 void FromKeyValuePairs(Dictionary<string, object> kvp, out string principalID, out string friend, out int flags)
278 {
279 principalID = string.Empty;
280 if (kvp.ContainsKey("PrincipalID") && kvp["PrincipalID"] != null)
281 principalID = kvp["PrincipalID"].ToString();
282 friend = string.Empty;
283 if (kvp.ContainsKey("Friend") && kvp["Friend"] != null)
284 friend = kvp["Friend"].ToString();
285 flags = 0;
286 if (kvp.ContainsKey("MyFlags") && kvp["MyFlags"] != null)
287 Int32.TryParse(kvp["MyFlags"].ToString(), out flags);
288 }
289  
290 #endregion
291 }
292 }