diff options
Diffstat (limited to 'OpenSim/Server/Handlers/UserAccounts/UserAccountServerPostHandler.cs')
-rw-r--r-- | OpenSim/Server/Handlers/UserAccounts/UserAccountServerPostHandler.cs | 276 |
1 files changed, 276 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..a92148c --- /dev/null +++ b/OpenSim/Server/Handlers/UserAccounts/UserAccountServerPostHandler.cs | |||
@@ -0,0 +1,276 @@ | |||
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 "createaccount": | ||
89 | return CreateAccount(request); | ||
90 | case "setaccount": | ||
91 | return SetAccount(request); | ||
92 | } | ||
93 | m_log.DebugFormat("[PRESENCE HANDLER]: unknown method request: {0}", method); | ||
94 | } | ||
95 | catch (Exception e) | ||
96 | { | ||
97 | m_log.Debug("[PRESENCE HANDLER]: Exception {0}" + e); | ||
98 | } | ||
99 | |||
100 | return FailureResult(); | ||
101 | |||
102 | } | ||
103 | |||
104 | byte[] GetAccount(Dictionary<string, object> request) | ||
105 | { | ||
106 | UserAccount account = null; | ||
107 | UUID scopeID = UUID.Zero; | ||
108 | Dictionary<string, object> result = new Dictionary<string, object>(); | ||
109 | |||
110 | if (!request.ContainsKey("ScopeID")) | ||
111 | { | ||
112 | result["result"] = "null"; | ||
113 | return ResultToBytes(result); | ||
114 | } | ||
115 | |||
116 | if (!UUID.TryParse(request["ScopeID"].ToString(), out scopeID)) | ||
117 | { | ||
118 | result["result"] = "null"; | ||
119 | return ResultToBytes(result); | ||
120 | } | ||
121 | |||
122 | if (request.ContainsKey("UserID") && request["UserID"] != null) | ||
123 | { | ||
124 | UUID userID; | ||
125 | if (UUID.TryParse(request["UserID"].ToString(), out userID)) | ||
126 | account = m_UserAccountService.GetUserAccount(scopeID, userID); | ||
127 | } | ||
128 | |||
129 | else if (request.ContainsKey("Email") && request["Email"] != null) | ||
130 | account = m_UserAccountService.GetUserAccount(scopeID, request["Email"].ToString()); | ||
131 | |||
132 | else if (request.ContainsKey("FirstName") && request.ContainsKey("LastName") && | ||
133 | request["FirstName"] != null && request["LastName"] != null) | ||
134 | account = m_UserAccountService.GetUserAccount(scopeID, request["FirstName"].ToString(), request["LastName"].ToString()); | ||
135 | |||
136 | if (account == null) | ||
137 | result["result"] = "null"; | ||
138 | else | ||
139 | result["result"] = account.ToKeyValuePairs(); | ||
140 | |||
141 | return ResultToBytes(result); | ||
142 | } | ||
143 | |||
144 | byte[] GetAccounts(Dictionary<string, object> request) | ||
145 | { | ||
146 | if (!request.ContainsKey("ScopeID") || !request.ContainsKey("query")) | ||
147 | return FailureResult(); | ||
148 | |||
149 | UUID scopeID = UUID.Zero; | ||
150 | if (!UUID.TryParse(request["ScopeID"].ToString(), out scopeID)) | ||
151 | return FailureResult(); | ||
152 | |||
153 | string query = request["query"].ToString(); | ||
154 | |||
155 | List<UserAccount> accounts = m_UserAccountService.GetUserAccounts(scopeID, query); | ||
156 | |||
157 | Dictionary<string, object> result = new Dictionary<string, object>(); | ||
158 | if ((accounts == null) || ((accounts != null) && (accounts.Count == 0))) | ||
159 | result["result"] = "null"; | ||
160 | else | ||
161 | { | ||
162 | int i = 0; | ||
163 | foreach (UserAccount acc in accounts) | ||
164 | { | ||
165 | Dictionary<string, object> rinfoDict = acc.ToKeyValuePairs(); | ||
166 | result["account" + i] = rinfoDict; | ||
167 | i++; | ||
168 | } | ||
169 | } | ||
170 | |||
171 | string xmlString = ServerUtils.BuildXmlResponse(result); | ||
172 | //m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString); | ||
173 | UTF8Encoding encoding = new UTF8Encoding(); | ||
174 | return encoding.GetBytes(xmlString); | ||
175 | } | ||
176 | |||
177 | byte[] CreateAccount(Dictionary<string, object> request) | ||
178 | { | ||
179 | if (!request.ContainsKey("account")) | ||
180 | return FailureResult(); | ||
181 | if (request["account"] == null) | ||
182 | return FailureResult(); | ||
183 | if (!(request["account"] is Dictionary<string, object>)) | ||
184 | return FailureResult(); | ||
185 | |||
186 | UserAccount account = new UserAccount((Dictionary<string, object>) request["account"]); | ||
187 | |||
188 | if (m_UserAccountService.CreateUserAccount(account)) | ||
189 | return SuccessResult(); | ||
190 | |||
191 | return FailureResult(); | ||
192 | } | ||
193 | |||
194 | byte[] SetAccount(Dictionary<string, object> request) | ||
195 | { | ||
196 | if (!request.ContainsKey("account")) | ||
197 | return FailureResult(); | ||
198 | if (request["account"] == null) | ||
199 | return FailureResult(); | ||
200 | if (!(request["account"] is Dictionary<string, object>)) | ||
201 | return FailureResult(); | ||
202 | |||
203 | UserAccount account = new UserAccount((Dictionary<string, object>)request["account"]); | ||
204 | |||
205 | if (m_UserAccountService.SetUserAccount(account)) | ||
206 | return SuccessResult(); | ||
207 | |||
208 | return FailureResult(); | ||
209 | } | ||
210 | |||
211 | private byte[] SuccessResult() | ||
212 | { | ||
213 | XmlDocument doc = new XmlDocument(); | ||
214 | |||
215 | XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration, | ||
216 | "", ""); | ||
217 | |||
218 | doc.AppendChild(xmlnode); | ||
219 | |||
220 | XmlElement rootElement = doc.CreateElement("", "ServerResponse", | ||
221 | ""); | ||
222 | |||
223 | doc.AppendChild(rootElement); | ||
224 | |||
225 | XmlElement result = doc.CreateElement("", "result", ""); | ||
226 | result.AppendChild(doc.CreateTextNode("Success")); | ||
227 | |||
228 | rootElement.AppendChild(result); | ||
229 | |||
230 | return DocToBytes(doc); | ||
231 | } | ||
232 | |||
233 | private byte[] FailureResult() | ||
234 | { | ||
235 | XmlDocument doc = new XmlDocument(); | ||
236 | |||
237 | XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration, | ||
238 | "", ""); | ||
239 | |||
240 | doc.AppendChild(xmlnode); | ||
241 | |||
242 | XmlElement rootElement = doc.CreateElement("", "ServerResponse", | ||
243 | ""); | ||
244 | |||
245 | doc.AppendChild(rootElement); | ||
246 | |||
247 | XmlElement result = doc.CreateElement("", "result", ""); | ||
248 | result.AppendChild(doc.CreateTextNode("Failure")); | ||
249 | |||
250 | rootElement.AppendChild(result); | ||
251 | |||
252 | return DocToBytes(doc); | ||
253 | } | ||
254 | |||
255 | private byte[] DocToBytes(XmlDocument doc) | ||
256 | { | ||
257 | MemoryStream ms = new MemoryStream(); | ||
258 | XmlTextWriter xw = new XmlTextWriter(ms, null); | ||
259 | xw.Formatting = Formatting.Indented; | ||
260 | doc.WriteTo(xw); | ||
261 | xw.Flush(); | ||
262 | |||
263 | return ms.ToArray(); | ||
264 | } | ||
265 | |||
266 | private byte[] ResultToBytes(Dictionary<string, object> result) | ||
267 | { | ||
268 | string xmlString = ServerUtils.BuildXmlResponse(result); | ||
269 | //m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString); | ||
270 | UTF8Encoding encoding = new UTF8Encoding(); | ||
271 | return encoding.GetBytes(xmlString); | ||
272 | } | ||
273 | |||
274 | |||
275 | } | ||
276 | } | ||