diff options
Diffstat (limited to 'OpenSim/Server/Handlers/UserAccounts/UserAccountServerPostHandler.cs')
-rw-r--r-- | OpenSim/Server/Handlers/UserAccounts/UserAccountServerPostHandler.cs | 258 |
1 files changed, 258 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..6a82165 --- /dev/null +++ b/OpenSim/Server/Handlers/UserAccounts/UserAccountServerPostHandler.cs | |||
@@ -0,0 +1,258 @@ | |||
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 | string method = string.Empty; | ||
72 | try | ||
73 | { | ||
74 | Dictionary<string, object> request = | ||
75 | ServerUtils.ParseQueryString(body); | ||
76 | |||
77 | if (!request.ContainsKey("METHOD")) | ||
78 | return FailureResult(); | ||
79 | |||
80 | 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("[USER SERVICE HANDLER]: unknown method request: {0}", method); | ||
92 | } | ||
93 | catch (Exception e) | ||
94 | { | ||
95 | m_log.DebugFormat("[USER SERVICE HANDLER]: Exception in method {0}: {1}", method, 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 | { | ||
138 | result["result"] = account.ToKeyValuePairs(); | ||
139 | } | ||
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[] StoreAccount(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(request); | ||
187 | |||
188 | if (m_UserAccountService.StoreUserAccount(account)) | ||
189 | return SuccessResult(); | ||
190 | |||
191 | return FailureResult(); | ||
192 | } | ||
193 | |||
194 | private byte[] SuccessResult() | ||
195 | { | ||
196 | XmlDocument doc = new XmlDocument(); | ||
197 | |||
198 | XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration, | ||
199 | "", ""); | ||
200 | |||
201 | doc.AppendChild(xmlnode); | ||
202 | |||
203 | XmlElement rootElement = doc.CreateElement("", "ServerResponse", | ||
204 | ""); | ||
205 | |||
206 | doc.AppendChild(rootElement); | ||
207 | |||
208 | XmlElement result = doc.CreateElement("", "result", ""); | ||
209 | result.AppendChild(doc.CreateTextNode("Success")); | ||
210 | |||
211 | rootElement.AppendChild(result); | ||
212 | |||
213 | return DocToBytes(doc); | ||
214 | } | ||
215 | |||
216 | private byte[] FailureResult() | ||
217 | { | ||
218 | XmlDocument doc = new XmlDocument(); | ||
219 | |||
220 | XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration, | ||
221 | "", ""); | ||
222 | |||
223 | doc.AppendChild(xmlnode); | ||
224 | |||
225 | XmlElement rootElement = doc.CreateElement("", "ServerResponse", | ||
226 | ""); | ||
227 | |||
228 | doc.AppendChild(rootElement); | ||
229 | |||
230 | XmlElement result = doc.CreateElement("", "result", ""); | ||
231 | result.AppendChild(doc.CreateTextNode("Failure")); | ||
232 | |||
233 | rootElement.AppendChild(result); | ||
234 | |||
235 | return DocToBytes(doc); | ||
236 | } | ||
237 | |||
238 | private byte[] DocToBytes(XmlDocument doc) | ||
239 | { | ||
240 | MemoryStream ms = new MemoryStream(); | ||
241 | XmlTextWriter xw = new XmlTextWriter(ms, null); | ||
242 | xw.Formatting = Formatting.Indented; | ||
243 | doc.WriteTo(xw); | ||
244 | xw.Flush(); | ||
245 | |||
246 | return ms.ToArray(); | ||
247 | } | ||
248 | |||
249 | private byte[] ResultToBytes(Dictionary<string, object> result) | ||
250 | { | ||
251 | string xmlString = ServerUtils.BuildXmlResponse(result); | ||
252 | UTF8Encoding encoding = new UTF8Encoding(); | ||
253 | return encoding.GetBytes(xmlString); | ||
254 | } | ||
255 | |||
256 | |||
257 | } | ||
258 | } | ||