diff options
Diffstat (limited to 'OpenSim/Services/Connectors/UserAccounts/UserAccountServiceConnector.cs')
-rw-r--r-- | OpenSim/Services/Connectors/UserAccounts/UserAccountServiceConnector.cs | 278 |
1 files changed, 278 insertions, 0 deletions
diff --git a/OpenSim/Services/Connectors/UserAccounts/UserAccountServiceConnector.cs b/OpenSim/Services/Connectors/UserAccounts/UserAccountServiceConnector.cs new file mode 100644 index 0000000..1527db2 --- /dev/null +++ b/OpenSim/Services/Connectors/UserAccounts/UserAccountServiceConnector.cs | |||
@@ -0,0 +1,278 @@ | |||
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 log4net; | ||
29 | using System; | ||
30 | using System.Collections.Generic; | ||
31 | using System.IO; | ||
32 | using System.Reflection; | ||
33 | using Nini.Config; | ||
34 | using OpenSim.Framework; | ||
35 | using OpenSim.Framework.Communications; | ||
36 | using OpenSim.Framework.Servers.HttpServer; | ||
37 | using OpenSim.Server.Base; | ||
38 | using OpenSim.Services.Interfaces; | ||
39 | using OpenMetaverse; | ||
40 | |||
41 | namespace OpenSim.Services.Connectors | ||
42 | { | ||
43 | public class UserAccountServicesConnector : IUserAccountService | ||
44 | { | ||
45 | private static readonly ILog m_log = | ||
46 | LogManager.GetLogger( | ||
47 | MethodBase.GetCurrentMethod().DeclaringType); | ||
48 | |||
49 | private string m_ServerURI = String.Empty; | ||
50 | |||
51 | public UserAccountServicesConnector() | ||
52 | { | ||
53 | } | ||
54 | |||
55 | public UserAccountServicesConnector(string serverURI) | ||
56 | { | ||
57 | m_ServerURI = serverURI.TrimEnd('/'); | ||
58 | } | ||
59 | |||
60 | public UserAccountServicesConnector(IConfigSource source) | ||
61 | { | ||
62 | Initialise(source); | ||
63 | } | ||
64 | |||
65 | public virtual void Initialise(IConfigSource source) | ||
66 | { | ||
67 | IConfig assetConfig = source.Configs["UserAccountService"]; | ||
68 | if (assetConfig == null) | ||
69 | { | ||
70 | m_log.Error("[ACCOUNT CONNECTOR]: UserAccountService missing from OpenSim.ini"); | ||
71 | throw new Exception("User account connector init error"); | ||
72 | } | ||
73 | |||
74 | string serviceURI = assetConfig.GetString("UserAccountServerURI", | ||
75 | String.Empty); | ||
76 | |||
77 | if (serviceURI == String.Empty) | ||
78 | { | ||
79 | m_log.Error("[ACCOUNT CONNECTOR]: No Server URI named in section UserAccountService"); | ||
80 | throw new Exception("User account connector init error"); | ||
81 | } | ||
82 | m_ServerURI = serviceURI; | ||
83 | } | ||
84 | |||
85 | public virtual UserAccount GetUserAccount(UUID scopeID, string firstName, string lastName) | ||
86 | { | ||
87 | Dictionary<string, object> sendData = new Dictionary<string, object>(); | ||
88 | //sendData["SCOPEID"] = scopeID.ToString(); | ||
89 | sendData["VERSIONMIN"] = ProtocolVersions.ClientProtocolVersionMin.ToString(); | ||
90 | sendData["VERSIONMAX"] = ProtocolVersions.ClientProtocolVersionMax.ToString(); | ||
91 | sendData["METHOD"] = "getaccount"; | ||
92 | |||
93 | sendData["ScopeID"] = scopeID; | ||
94 | sendData["FirstName"] = firstName.ToString(); | ||
95 | sendData["LastName"] = lastName.ToString(); | ||
96 | |||
97 | return SendAndGetReply(sendData); | ||
98 | } | ||
99 | |||
100 | public virtual UserAccount GetUserAccount(UUID scopeID, string email) | ||
101 | { | ||
102 | Dictionary<string, object> sendData = new Dictionary<string, object>(); | ||
103 | //sendData["SCOPEID"] = scopeID.ToString(); | ||
104 | sendData["VERSIONMIN"] = ProtocolVersions.ClientProtocolVersionMin.ToString(); | ||
105 | sendData["VERSIONMAX"] = ProtocolVersions.ClientProtocolVersionMax.ToString(); | ||
106 | sendData["METHOD"] = "getaccount"; | ||
107 | |||
108 | sendData["ScopeID"] = scopeID; | ||
109 | sendData["Email"] = email; | ||
110 | |||
111 | return SendAndGetReply(sendData); | ||
112 | } | ||
113 | |||
114 | public virtual UserAccount GetUserAccount(UUID scopeID, UUID userID) | ||
115 | { | ||
116 | m_log.DebugFormat("[ACCOUNTS CONNECTOR]: GetUSerAccount {0}", userID); | ||
117 | Dictionary<string, object> sendData = new Dictionary<string, object>(); | ||
118 | //sendData["SCOPEID"] = scopeID.ToString(); | ||
119 | sendData["VERSIONMIN"] = ProtocolVersions.ClientProtocolVersionMin.ToString(); | ||
120 | sendData["VERSIONMAX"] = ProtocolVersions.ClientProtocolVersionMax.ToString(); | ||
121 | sendData["METHOD"] = "getaccount"; | ||
122 | |||
123 | sendData["ScopeID"] = scopeID; | ||
124 | sendData["UserID"] = userID.ToString(); | ||
125 | |||
126 | return SendAndGetReply(sendData); | ||
127 | } | ||
128 | |||
129 | public List<UserAccount> GetUserAccounts(UUID scopeID, string query) | ||
130 | { | ||
131 | Dictionary<string, object> sendData = new Dictionary<string, object>(); | ||
132 | //sendData["SCOPEID"] = scopeID.ToString(); | ||
133 | sendData["VERSIONMIN"] = ProtocolVersions.ClientProtocolVersionMin.ToString(); | ||
134 | sendData["VERSIONMAX"] = ProtocolVersions.ClientProtocolVersionMax.ToString(); | ||
135 | sendData["METHOD"] = "getaccounts"; | ||
136 | |||
137 | sendData["ScopeID"] = scopeID.ToString(); | ||
138 | sendData["query"] = query; | ||
139 | |||
140 | string reply = string.Empty; | ||
141 | string reqString = ServerUtils.BuildQueryString(sendData); | ||
142 | // m_log.DebugFormat("[ACCOUNTS CONNECTOR]: queryString = {0}", reqString); | ||
143 | try | ||
144 | { | ||
145 | reply = SynchronousRestFormsRequester.MakeRequest("POST", | ||
146 | m_ServerURI + "/accounts", | ||
147 | reqString); | ||
148 | if (reply == null || (reply != null && reply == string.Empty)) | ||
149 | { | ||
150 | m_log.DebugFormat("[ACCOUNT CONNECTOR]: GetUserAccounts received null or empty reply"); | ||
151 | return null; | ||
152 | } | ||
153 | } | ||
154 | catch (Exception e) | ||
155 | { | ||
156 | m_log.DebugFormat("[ACCOUNT CONNECTOR]: Exception when contacting accounts server: {0}", e.Message); | ||
157 | } | ||
158 | |||
159 | List<UserAccount> accounts = new List<UserAccount>(); | ||
160 | |||
161 | Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply); | ||
162 | |||
163 | if (replyData != null) | ||
164 | { | ||
165 | if (replyData.ContainsKey("result") && replyData.ContainsKey("result").ToString() == "null") | ||
166 | { | ||
167 | return accounts; | ||
168 | } | ||
169 | |||
170 | Dictionary<string, object>.ValueCollection accountList = replyData.Values; | ||
171 | //m_log.DebugFormat("[ACCOUNTS CONNECTOR]: GetAgents returned {0} elements", pinfosList.Count); | ||
172 | foreach (object acc in accountList) | ||
173 | { | ||
174 | if (acc is Dictionary<string, object>) | ||
175 | { | ||
176 | UserAccount pinfo = new UserAccount((Dictionary<string, object>)acc); | ||
177 | accounts.Add(pinfo); | ||
178 | } | ||
179 | else | ||
180 | m_log.DebugFormat("[ACCOUNT CONNECTOR]: GetUserAccounts received invalid response type {0}", | ||
181 | acc.GetType()); | ||
182 | } | ||
183 | } | ||
184 | else | ||
185 | m_log.DebugFormat("[ACCOUNTS CONNECTOR]: GetUserAccounts received null response"); | ||
186 | |||
187 | return accounts; | ||
188 | } | ||
189 | |||
190 | public virtual bool StoreUserAccount(UserAccount data) | ||
191 | { | ||
192 | Dictionary<string, object> sendData = new Dictionary<string, object>(); | ||
193 | //sendData["SCOPEID"] = scopeID.ToString(); | ||
194 | sendData["VERSIONMIN"] = ProtocolVersions.ClientProtocolVersionMin.ToString(); | ||
195 | sendData["VERSIONMAX"] = ProtocolVersions.ClientProtocolVersionMax.ToString(); | ||
196 | sendData["METHOD"] = "setaccount"; | ||
197 | |||
198 | Dictionary<string, object> structData = data.ToKeyValuePairs(); | ||
199 | |||
200 | foreach (KeyValuePair<string,object> kvp in structData) | ||
201 | sendData[kvp.Key] = kvp.Value.ToString(); | ||
202 | |||
203 | return SendAndGetBoolReply(sendData); | ||
204 | } | ||
205 | |||
206 | private UserAccount SendAndGetReply(Dictionary<string, object> sendData) | ||
207 | { | ||
208 | string reply = string.Empty; | ||
209 | string reqString = ServerUtils.BuildQueryString(sendData); | ||
210 | // m_log.DebugFormat("[ACCOUNTS CONNECTOR]: queryString = {0}", reqString); | ||
211 | try | ||
212 | { | ||
213 | reply = SynchronousRestFormsRequester.MakeRequest("POST", | ||
214 | m_ServerURI + "/accounts", | ||
215 | reqString); | ||
216 | if (reply == null || (reply != null && reply == string.Empty)) | ||
217 | { | ||
218 | m_log.DebugFormat("[ACCOUNT CONNECTOR]: GetUserAccount received null or empty reply"); | ||
219 | return null; | ||
220 | } | ||
221 | } | ||
222 | catch (Exception e) | ||
223 | { | ||
224 | m_log.DebugFormat("[ACCOUNT CONNECTOR]: Exception when contacting user account server: {0}", e.Message); | ||
225 | } | ||
226 | |||
227 | Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply); | ||
228 | UserAccount account = null; | ||
229 | |||
230 | if ((replyData != null) && replyData.ContainsKey("result") && (replyData["result"] != null)) | ||
231 | { | ||
232 | if (replyData["result"] is Dictionary<string, object>) | ||
233 | { | ||
234 | account = new UserAccount((Dictionary<string, object>)replyData["result"]); | ||
235 | } | ||
236 | } | ||
237 | |||
238 | return account; | ||
239 | |||
240 | } | ||
241 | |||
242 | private bool SendAndGetBoolReply(Dictionary<string, object> sendData) | ||
243 | { | ||
244 | string reqString = ServerUtils.BuildQueryString(sendData); | ||
245 | // m_log.DebugFormat("[ACCOUNTS CONNECTOR]: queryString = {0}", reqString); | ||
246 | try | ||
247 | { | ||
248 | string reply = SynchronousRestFormsRequester.MakeRequest("POST", | ||
249 | m_ServerURI + "/accounts", | ||
250 | reqString); | ||
251 | if (reply != string.Empty) | ||
252 | { | ||
253 | Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply); | ||
254 | |||
255 | if (replyData.ContainsKey("result")) | ||
256 | { | ||
257 | if (replyData["result"].ToString().ToLower() == "success") | ||
258 | return true; | ||
259 | else | ||
260 | return false; | ||
261 | } | ||
262 | else | ||
263 | m_log.DebugFormat("[ACCOUNTS CONNECTOR]: Set or Create UserAccount reply data does not contain result field"); | ||
264 | |||
265 | } | ||
266 | else | ||
267 | m_log.DebugFormat("[ACCOUNTS CONNECTOR]: Set or Create UserAccount received empty reply"); | ||
268 | } | ||
269 | catch (Exception e) | ||
270 | { | ||
271 | m_log.DebugFormat("[ACCOUNTS CONNECTOR]: Exception when contacting user account server: {0}", e.Message); | ||
272 | } | ||
273 | |||
274 | return false; | ||
275 | } | ||
276 | |||
277 | } | ||
278 | } | ||