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