diff options
Diffstat (limited to 'OpenSim/Server/Handlers/UserAccounts/UserAccountServerPostHandler.cs')
-rw-r--r-- | OpenSim/Server/Handlers/UserAccounts/UserAccountServerPostHandler.cs | 247 |
1 files changed, 247 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..a1d4871 --- /dev/null +++ b/OpenSim/Server/Handlers/UserAccounts/UserAccountServerPostHandler.cs | |||
@@ -0,0 +1,247 @@ | |||
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 | // No can do. No changing user accounts from remote sims | ||
180 | return FailureResult(); | ||
181 | } | ||
182 | |||
183 | private byte[] SuccessResult() | ||
184 | { | ||
185 | XmlDocument doc = new XmlDocument(); | ||
186 | |||
187 | XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration, | ||
188 | "", ""); | ||
189 | |||
190 | doc.AppendChild(xmlnode); | ||
191 | |||
192 | XmlElement rootElement = doc.CreateElement("", "ServerResponse", | ||
193 | ""); | ||
194 | |||
195 | doc.AppendChild(rootElement); | ||
196 | |||
197 | XmlElement result = doc.CreateElement("", "result", ""); | ||
198 | result.AppendChild(doc.CreateTextNode("Success")); | ||
199 | |||
200 | rootElement.AppendChild(result); | ||
201 | |||
202 | return DocToBytes(doc); | ||
203 | } | ||
204 | |||
205 | private byte[] FailureResult() | ||
206 | { | ||
207 | XmlDocument doc = new XmlDocument(); | ||
208 | |||
209 | XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration, | ||
210 | "", ""); | ||
211 | |||
212 | doc.AppendChild(xmlnode); | ||
213 | |||
214 | XmlElement rootElement = doc.CreateElement("", "ServerResponse", | ||
215 | ""); | ||
216 | |||
217 | doc.AppendChild(rootElement); | ||
218 | |||
219 | XmlElement result = doc.CreateElement("", "result", ""); | ||
220 | result.AppendChild(doc.CreateTextNode("Failure")); | ||
221 | |||
222 | rootElement.AppendChild(result); | ||
223 | |||
224 | return DocToBytes(doc); | ||
225 | } | ||
226 | |||
227 | private byte[] DocToBytes(XmlDocument doc) | ||
228 | { | ||
229 | MemoryStream ms = new MemoryStream(); | ||
230 | XmlTextWriter xw = new XmlTextWriter(ms, null); | ||
231 | xw.Formatting = Formatting.Indented; | ||
232 | doc.WriteTo(xw); | ||
233 | xw.Flush(); | ||
234 | |||
235 | return ms.ToArray(); | ||
236 | } | ||
237 | |||
238 | private byte[] ResultToBytes(Dictionary<string, object> result) | ||
239 | { | ||
240 | string xmlString = ServerUtils.BuildXmlResponse(result); | ||
241 | UTF8Encoding encoding = new UTF8Encoding(); | ||
242 | return encoding.GetBytes(xmlString); | ||
243 | } | ||
244 | |||
245 | |||
246 | } | ||
247 | } | ||