diff options
Diffstat (limited to '')
-rw-r--r-- | OpenSim/Server/Handlers/UserAccounts/UserAccountServerPostHandler.cs | 257 |
1 files changed, 257 insertions, 0 deletions
diff --git a/OpenSim/Server/Handlers/UserAccounts/UserAccountServerPostHandler.cs b/OpenSim/Server/Handlers/UserAccounts/UserAccountServerPostHandler.cs new file mode 100644 index 0000000..544ffea --- /dev/null +++ b/OpenSim/Server/Handlers/UserAccounts/UserAccountServerPostHandler.cs | |||
@@ -0,0 +1,257 @@ | |||
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 OpenSim.Framework; | ||
42 | using OpenSim.Framework.Servers.HttpServer; | ||
43 | using OpenMetaverse; | ||
44 | |||
45 | namespace OpenSim.Server.Handlers.UserAccounts | ||
46 | { | ||
47 | public class UserAccountServerPostHandler : BaseStreamHandler | ||
48 | { | ||
49 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
50 | |||
51 | private IUserAccountService m_UserAccountService; | ||
52 | |||
53 | public UserAccountServerPostHandler(IUserAccountService service) : | ||
54 | base("POST", "/accounts") | ||
55 | { | ||
56 | m_UserAccountService = service; | ||
57 | } | ||
58 | |||
59 | public override byte[] Handle(string path, Stream requestData, | ||
60 | OSHttpRequest httpRequest, OSHttpResponse httpResponse) | ||
61 | { | ||
62 | StreamReader sr = new StreamReader(requestData); | ||
63 | string body = sr.ReadToEnd(); | ||
64 | sr.Close(); | ||
65 | body = body.Trim(); | ||
66 | |||
67 | // We need to check the authorization header | ||
68 | //httpRequest.Headers["authorization"] ... | ||
69 | |||
70 | //m_log.DebugFormat("[XXX]: query String: {0}", body); | ||
71 | |||
72 | try | ||
73 | { | ||
74 | Dictionary<string, object> request = | ||
75 | ServerUtils.ParseQueryString(body); | ||
76 | |||
77 | if (!request.ContainsKey("METHOD")) | ||
78 | return FailureResult(); | ||
79 | |||
80 | string method = request["METHOD"].ToString(); | ||
81 | |||
82 | switch (method) | ||
83 | { | ||
84 | case "getaccount": | ||
85 | return GetAccount(request); | ||
86 | case "getaccounts": | ||
87 | return GetAccounts(request); | ||
88 | case "setaccount": | ||
89 | return StoreAccount(request); | ||
90 | } | ||
91 | m_log.DebugFormat("[PRESENCE HANDLER]: unknown method request: {0}", method); | ||
92 | } | ||
93 | catch (Exception e) | ||
94 | { | ||
95 | m_log.Debug("[PRESENCE HANDLER]: Exception {0}" + e); | ||
96 | } | ||
97 | |||
98 | return FailureResult(); | ||
99 | |||
100 | } | ||
101 | |||
102 | byte[] GetAccount(Dictionary<string, object> request) | ||
103 | { | ||
104 | UserAccount account = null; | ||
105 | UUID scopeID = UUID.Zero; | ||
106 | Dictionary<string, object> result = new Dictionary<string, object>(); | ||
107 | |||
108 | if (!request.ContainsKey("ScopeID")) | ||
109 | { | ||
110 | result["result"] = "null"; | ||
111 | return ResultToBytes(result); | ||
112 | } | ||
113 | |||
114 | if (!UUID.TryParse(request["ScopeID"].ToString(), out scopeID)) | ||
115 | { | ||
116 | result["result"] = "null"; | ||
117 | return ResultToBytes(result); | ||
118 | } | ||
119 | |||
120 | if (request.ContainsKey("UserID") && request["UserID"] != null) | ||
121 | { | ||
122 | UUID userID; | ||
123 | if (UUID.TryParse(request["UserID"].ToString(), out userID)) | ||
124 | account = m_UserAccountService.GetUserAccount(scopeID, userID); | ||
125 | } | ||
126 | |||
127 | else if (request.ContainsKey("Email") && request["Email"] != null) | ||
128 | account = m_UserAccountService.GetUserAccount(scopeID, request["Email"].ToString()); | ||
129 | |||
130 | else if (request.ContainsKey("FirstName") && request.ContainsKey("LastName") && | ||
131 | request["FirstName"] != null && request["LastName"] != null) | ||
132 | account = m_UserAccountService.GetUserAccount(scopeID, request["FirstName"].ToString(), request["LastName"].ToString()); | ||
133 | |||
134 | if (account == null) | ||
135 | result["result"] = "null"; | ||
136 | else | ||
137 | result["result"] = account.ToKeyValuePairs(); | ||
138 | |||
139 | return ResultToBytes(result); | ||
140 | } | ||
141 | |||
142 | byte[] GetAccounts(Dictionary<string, object> request) | ||
143 | { | ||
144 | if (!request.ContainsKey("ScopeID") || !request.ContainsKey("query")) | ||
145 | return FailureResult(); | ||
146 | |||
147 | UUID scopeID = UUID.Zero; | ||
148 | if (!UUID.TryParse(request["ScopeID"].ToString(), out scopeID)) | ||
149 | return FailureResult(); | ||
150 | |||
151 | string query = request["query"].ToString(); | ||
152 | |||
153 | List<UserAccount> accounts = m_UserAccountService.GetUserAccounts(scopeID, query); | ||
154 | |||
155 | Dictionary<string, object> result = new Dictionary<string, object>(); | ||
156 | if ((accounts == null) || ((accounts != null) && (accounts.Count == 0))) | ||
157 | result["result"] = "null"; | ||
158 | else | ||
159 | { | ||
160 | int i = 0; | ||
161 | foreach (UserAccount acc in accounts) | ||
162 | { | ||
163 | Dictionary<string, object> rinfoDict = acc.ToKeyValuePairs(); | ||
164 | result["account" + i] = rinfoDict; | ||
165 | i++; | ||
166 | } | ||
167 | } | ||
168 | |||
169 | string xmlString = ServerUtils.BuildXmlResponse(result); | ||
170 | //m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString); | ||
171 | UTF8Encoding encoding = new UTF8Encoding(); | ||
172 | return encoding.GetBytes(xmlString); | ||
173 | } | ||
174 | |||
175 | byte[] StoreAccount(Dictionary<string, object> request) | ||
176 | { | ||
177 | if (!request.ContainsKey("account")) | ||
178 | return FailureResult(); | ||
179 | if (request["account"] == null) | ||
180 | return FailureResult(); | ||
181 | if (!(request["account"] is Dictionary<string, object>)) | ||
182 | return FailureResult(); | ||
183 | |||
184 | UserAccount account = new UserAccount((Dictionary<string, object>)request["account"]); | ||
185 | |||
186 | if (m_UserAccountService.StoreUserAccount(account)) | ||
187 | return SuccessResult(); | ||
188 | |||
189 | return FailureResult(); | ||
190 | } | ||
191 | |||
192 | private byte[] SuccessResult() | ||
193 | { | ||
194 | XmlDocument doc = new XmlDocument(); | ||
195 | |||
196 | XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration, | ||
197 | "", ""); | ||
198 | |||
199 | doc.AppendChild(xmlnode); | ||
200 | |||
201 | XmlElement rootElement = doc.CreateElement("", "ServerResponse", | ||
202 | ""); | ||
203 | |||
204 | doc.AppendChild(rootElement); | ||
205 | |||
206 | XmlElement result = doc.CreateElement("", "result", ""); | ||
207 | result.AppendChild(doc.CreateTextNode("Success")); | ||
208 | |||
209 | rootElement.AppendChild(result); | ||
210 | |||
211 | return DocToBytes(doc); | ||
212 | } | ||
213 | |||
214 | private byte[] FailureResult() | ||
215 | { | ||
216 | XmlDocument doc = new XmlDocument(); | ||
217 | |||
218 | XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration, | ||
219 | "", ""); | ||
220 | |||
221 | doc.AppendChild(xmlnode); | ||
222 | |||
223 | XmlElement rootElement = doc.CreateElement("", "ServerResponse", | ||
224 | ""); | ||
225 | |||
226 | doc.AppendChild(rootElement); | ||
227 | |||
228 | XmlElement result = doc.CreateElement("", "result", ""); | ||
229 | result.AppendChild(doc.CreateTextNode("Failure")); | ||
230 | |||
231 | rootElement.AppendChild(result); | ||
232 | |||
233 | return DocToBytes(doc); | ||
234 | } | ||
235 | |||
236 | private byte[] DocToBytes(XmlDocument doc) | ||
237 | { | ||
238 | MemoryStream ms = new MemoryStream(); | ||
239 | XmlTextWriter xw = new XmlTextWriter(ms, null); | ||
240 | xw.Formatting = Formatting.Indented; | ||
241 | doc.WriteTo(xw); | ||
242 | xw.Flush(); | ||
243 | |||
244 | return ms.ToArray(); | ||
245 | } | ||
246 | |||
247 | private byte[] ResultToBytes(Dictionary<string, object> result) | ||
248 | { | ||
249 | string xmlString = ServerUtils.BuildXmlResponse(result); | ||
250 | //m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString); | ||
251 | UTF8Encoding encoding = new UTF8Encoding(); | ||
252 | return encoding.GetBytes(xmlString); | ||
253 | } | ||
254 | |||
255 | |||
256 | } | ||
257 | } | ||