root / src / com / rackspace / cloud / servers / api / client / Account.java @ 378fe36a
History | View | Annotate | Download (10.2 kB)
1 |
/**
|
---|---|
2 |
*
|
3 |
*/
|
4 |
package com.rackspace.cloud.servers.api.client; |
5 |
|
6 |
import java.util.ArrayList; |
7 |
import java.util.Arrays; |
8 |
import java.util.HashMap; |
9 |
import java.util.List; |
10 |
import java.util.Map; |
11 |
import java.util.concurrent.ExecutionException; |
12 |
|
13 |
import android.content.Context; |
14 |
import android.os.AsyncTask; |
15 |
|
16 |
import com.rackspace.cloud.android.Preferences; |
17 |
import com.rackspace.cloud.files.api.client.ContainerObjectManager; |
18 |
import com.rackspace.cloud.utils.StringUtils; |
19 |
|
20 |
/**
|
21 |
* @author Mike Mayo - mike.mayo@rackspace.com - twitter.com/greenisus
|
22 |
*
|
23 |
*/
|
24 |
public class Account implements java.io.Serializable { |
25 |
|
26 |
/**
|
27 |
*
|
28 |
*/
|
29 |
private static final long serialVersionUID = 2180740077014156769L; |
30 |
private String displayName; |
31 |
private String username; |
32 |
private String apiKey; |
33 |
private String password; |
34 |
private String accountId; |
35 |
private String authServer; |
36 |
private String authServerV2; |
37 |
private String loadBalancerUKUrl; |
38 |
private String[] loadBalancerRegions; |
39 |
private static String loadBalancerDFWUrl; |
40 |
private static String loadBalancerORDUrl; |
41 |
private static String loadBalancerLONUrl; |
42 |
private transient String authToken; |
43 |
private transient String serverUrl; |
44 |
private transient String storageUrl; |
45 |
private transient String storageToken; |
46 |
private transient String cdnManagementUrl; |
47 |
private transient static Account currentAccount; |
48 |
public Map<String, String> displayNames = new HashMap<String, String>(); |
49 |
public Map<String, String> uuids = new HashMap<String, String>(); |
50 |
|
51 |
public static Account getAccount() { |
52 |
return currentAccount;
|
53 |
} |
54 |
|
55 |
public static void setAccount(Account account) { |
56 |
Account.currentAccount = account; |
57 |
} |
58 |
|
59 |
/**
|
60 |
* @return the serverUrl
|
61 |
*/
|
62 |
public String getServerUrl() { |
63 |
return serverUrl;
|
64 |
} |
65 |
|
66 |
/**
|
67 |
* @param serverUrl
|
68 |
* the serverUrl to set
|
69 |
*/
|
70 |
public void setServerUrl(String serverUrl) { |
71 |
this.serverUrl = serverUrl;
|
72 |
} |
73 |
|
74 |
/**
|
75 |
* @return the storageUrl
|
76 |
*/
|
77 |
public String getStorageUrl() { |
78 |
return storageUrl;
|
79 |
} |
80 |
|
81 |
/**
|
82 |
* @return the storageToken
|
83 |
*/
|
84 |
public String getStorageToken() { |
85 |
return storageToken;
|
86 |
} |
87 |
|
88 |
/**
|
89 |
* @param storageUrl
|
90 |
* the storageUrl to set
|
91 |
*/
|
92 |
public void setStorageUrl(String storageUrl) { |
93 |
this.storageUrl = storageUrl;
|
94 |
} |
95 |
|
96 |
/**
|
97 |
* @return the cdnManagementUrl
|
98 |
*/
|
99 |
public String getCdnManagementUrl() { |
100 |
return cdnManagementUrl;
|
101 |
} |
102 |
|
103 |
/**
|
104 |
* @param cdnManagementUrl
|
105 |
* the cdnManagementUrl to set
|
106 |
*/
|
107 |
public void setCdnManagementUrl(String cdnManagementUrl) { |
108 |
this.cdnManagementUrl = cdnManagementUrl;
|
109 |
} |
110 |
|
111 |
/**
|
112 |
* @return the authToken
|
113 |
*/
|
114 |
public String getAuthToken() { |
115 |
return authToken;
|
116 |
} |
117 |
|
118 |
/**
|
119 |
* @param authToken
|
120 |
* the authToken to set
|
121 |
*/
|
122 |
public void setAuthToken(String authToken) { |
123 |
this.authToken = authToken;
|
124 |
} |
125 |
|
126 |
/**
|
127 |
* @return the authToken
|
128 |
*/
|
129 |
public String getAuthServer() { |
130 |
return authServer;
|
131 |
} |
132 |
|
133 |
/**
|
134 |
* @param authToken
|
135 |
* the authToken to set
|
136 |
*/
|
137 |
public void setAuthServer(String authServer) { |
138 |
this.authServer = authServer;
|
139 |
|
140 |
/*
|
141 |
* the auth server used determines which regions can be used for load
|
142 |
* balancers, so set available regions here.
|
143 |
*/
|
144 |
if (authServer.equals(Preferences.COUNTRY_UK_AUTH_SERVER)) { |
145 |
setLoadBalancerRegions(Preferences.UK_REGIONS);
|
146 |
} else if (authServer.equals(Preferences.COUNTRY_US_AUTH_SERVER)) { |
147 |
setLoadBalancerRegions(Preferences.US_REGIONS);
|
148 |
} else {
|
149 |
setLoadBalancerRegions(new String[0]); |
150 |
} |
151 |
} |
152 |
|
153 |
/**
|
154 |
* @return the authToken
|
155 |
*/
|
156 |
public String getAuthServerV2() { |
157 |
return authServerV2;
|
158 |
} |
159 |
|
160 |
/**
|
161 |
* @param authToken
|
162 |
* the authToken to set
|
163 |
*/
|
164 |
public void setAuthServerV2(String authServerV2) { |
165 |
this.authServerV2 = authServerV2;
|
166 |
|
167 |
/*
|
168 |
* the auth server used determines which regions can be used for load
|
169 |
* balancers, so set available regions here.
|
170 |
*/
|
171 |
if (authServerV2.equals(Preferences.COUNTRY_UK_AUTH_SERVER_V2)) { |
172 |
setLoadBalancerRegions(Preferences.UK_REGIONS);
|
173 |
} else if (authServerV2.equals(Preferences.COUNTRY_US_AUTH_SERVER_V2)) { |
174 |
setLoadBalancerRegions(Preferences.US_REGIONS);
|
175 |
} else {
|
176 |
setLoadBalancerRegions(new String[0]); |
177 |
} |
178 |
} |
179 |
|
180 |
// auth v1.1 should return loadbalancer endpoints and return account id ....
|
181 |
public String getAccountId() { |
182 |
String delemiter = "v1.0/"; |
183 |
int indexToReturn = 1; |
184 |
accountId = StringUtils.splitByDelemiter(getServerUrl(), delemiter, |
185 |
indexToReturn); |
186 |
return accountId;
|
187 |
} |
188 |
|
189 |
/**
|
190 |
* @return the loadBalancerDFWUrl
|
191 |
*/
|
192 |
public static String getLoadBalancerDFWUrl() { |
193 |
loadBalancerDFWUrl = "https://dfw.loadbalancers.api.rackspacecloud.com/v1.0/";
|
194 |
return loadBalancerDFWUrl;
|
195 |
} |
196 |
|
197 |
/**
|
198 |
* @param loadBalancerDFWUrl
|
199 |
* the loadBalancerDFWUrl to set
|
200 |
*/
|
201 |
public static void setLoadBalancerDFWUrl(String dfwUrl) { |
202 |
loadBalancerDFWUrl = dfwUrl; |
203 |
} |
204 |
|
205 |
/**
|
206 |
* @return the loadBalancerORDUrl
|
207 |
*/
|
208 |
public static String getLoadBalancerORDUrl() { |
209 |
loadBalancerORDUrl = "https://ord.loadbalancers.api.rackspacecloud.com/v1.0/";
|
210 |
return loadBalancerORDUrl;
|
211 |
} |
212 |
|
213 |
/**
|
214 |
* @param loadBalancerORDUrl
|
215 |
* the loadBalancerORDUrl to set
|
216 |
*/
|
217 |
public static void setLoadBalancerORDUrl(String ordUrl) { |
218 |
loadBalancerORDUrl = ordUrl; |
219 |
} |
220 |
|
221 |
/**
|
222 |
* @return the loadBalancerLONUrl
|
223 |
*/
|
224 |
public static String getLoadBalancerLONUrl() { |
225 |
loadBalancerLONUrl = "https://lon.loadbalancers.api.rackspacecloud.com/v1.0/";
|
226 |
return loadBalancerLONUrl;
|
227 |
} |
228 |
|
229 |
/**
|
230 |
* @param loadBalancerLONUrl
|
231 |
* the loadBalancerORDUrl to set
|
232 |
*/
|
233 |
public static void setLoadBalancerLONUrl(String lonUrl) { |
234 |
loadBalancerLONUrl = lonUrl; |
235 |
} |
236 |
|
237 |
/**
|
238 |
* @return the loadBalancerUKUrl
|
239 |
*/
|
240 |
public String getLoadBalancerUKUrl() { |
241 |
loadBalancerUKUrl = "https://lon.loadbalancers.api.rackspacecloud.com/v1.0/";
|
242 |
return loadBalancerUKUrl;
|
243 |
} |
244 |
|
245 |
/**
|
246 |
* @param loadBalancerUKUrl
|
247 |
* the loadBalancerUKUrl to set
|
248 |
*/
|
249 |
public void setLoadBalancerUKUrl(String loadBalancerUKUrl) { |
250 |
this.loadBalancerUKUrl = loadBalancerUKUrl;
|
251 |
} |
252 |
|
253 |
/**
|
254 |
* @return the username
|
255 |
*/
|
256 |
public String getUsername() { |
257 |
return username;
|
258 |
} |
259 |
|
260 |
/**
|
261 |
* @param username
|
262 |
* the username to set
|
263 |
*/
|
264 |
public void setUsername(String username) { |
265 |
this.username = username;
|
266 |
} |
267 |
|
268 |
/**
|
269 |
* @return the apiKey
|
270 |
*/
|
271 |
public String getApiKey() { |
272 |
return apiKey;
|
273 |
} |
274 |
|
275 |
/**
|
276 |
* @param password
|
277 |
* the password to set
|
278 |
*/
|
279 |
public void setPassword(String password) { |
280 |
this.password = password;
|
281 |
} |
282 |
|
283 |
/**
|
284 |
* @return the password
|
285 |
*/
|
286 |
public String getPassword() { |
287 |
return password;
|
288 |
} |
289 |
|
290 |
/**
|
291 |
* @param apiKey
|
292 |
* the apiKey to set
|
293 |
*/
|
294 |
public void setApiKey(String apiKey) { |
295 |
this.apiKey = apiKey;
|
296 |
} |
297 |
|
298 |
/**
|
299 |
*/
|
300 |
public void setStorageToken(String storageToken) { |
301 |
this.storageToken = storageToken;
|
302 |
} |
303 |
|
304 |
/**
|
305 |
* @return the load balancer regions
|
306 |
*/
|
307 |
public String[] getLoadBalancerRegions() { |
308 |
return loadBalancerRegions;
|
309 |
} |
310 |
|
311 |
/**
|
312 |
* @param loadBalancerRegions
|
313 |
* the load balancer regions to set
|
314 |
*/
|
315 |
public void setLoadBalancerRegions(String[] loadBalancerRegions) { |
316 |
this.loadBalancerRegions = new String[loadBalancerRegions.length]; |
317 |
for (int i = 0; i < loadBalancerRegions.length; i++) { |
318 |
this.loadBalancerRegions[i] = loadBalancerRegions[i];
|
319 |
} |
320 |
} |
321 |
|
322 |
public String getDisplayName() { |
323 |
return displayName;
|
324 |
} |
325 |
|
326 |
public void setDisplayName(String displayName) { |
327 |
this.displayName = displayName;
|
328 |
} |
329 |
|
330 |
public static void expandUUIDSOrDisplayNames(final Context context, |
331 |
final List<String> displayNames, final List<String> uuids) { |
332 |
final String url = Account.getAccount().getAuthServer(); |
333 |
AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() { |
334 |
|
335 |
@Override
|
336 |
protected Void doInBackground(Void... params) { |
337 |
try {
|
338 |
new ContainerObjectManager(context).getDisplayNames(url
|
339 |
.substring(0, url.length() - 3), Account |
340 |
.getAccount().getPassword(), displayNames, uuids); |
341 |
} catch (Exception e) { |
342 |
// TODO Auto-generated catch block
|
343 |
e.printStackTrace(); |
344 |
} |
345 |
return null; |
346 |
} |
347 |
|
348 |
}.execute(); |
349 |
try {
|
350 |
task.get(); |
351 |
} catch (InterruptedException e) { |
352 |
// TODO Auto-generated catch block
|
353 |
e.printStackTrace(); |
354 |
} catch (ExecutionException e) { |
355 |
// TODO Auto-generated catch block
|
356 |
e.printStackTrace(); |
357 |
} |
358 |
} |
359 |
|
360 |
public static String getDisplayNameForUUID(final String uuid, |
361 |
final Context context) { |
362 |
String result = Account.getAccount().uuids.get(uuid);
|
363 |
|
364 |
if (result != null) { |
365 |
return result;
|
366 |
} |
367 |
final String url = Account.getAccount().getAuthServer(); |
368 |
AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() { |
369 |
|
370 |
@Override
|
371 |
protected Void doInBackground(Void... params) { |
372 |
try {
|
373 |
new ContainerObjectManager(context).getDisplayNames(url
|
374 |
.substring(0, url.length() - 3), Account |
375 |
.getAccount().getPassword(), |
376 |
new ArrayList<String>(), Arrays.asList(uuid)); |
377 |
} catch (Exception e) { |
378 |
// TODO Auto-generated catch block
|
379 |
e.printStackTrace(); |
380 |
} |
381 |
return null; |
382 |
} |
383 |
|
384 |
}.execute(); |
385 |
try {
|
386 |
task.get(); |
387 |
} catch (InterruptedException e) { |
388 |
// TODO Auto-generated catch block
|
389 |
e.printStackTrace(); |
390 |
} catch (ExecutionException e) { |
391 |
// TODO Auto-generated catch block
|
392 |
e.printStackTrace(); |
393 |
} |
394 |
//System.out.println("ACOUNT UUIDS:" + Account.getAccount().uuids);
|
395 |
String res = Account.getAccount().uuids.get(uuid);
|
396 |
//System.out.println(res + " return ");
|
397 |
return res;
|
398 |
} |
399 |
|
400 |
public static String getUUIDForDisplayName(final String displayName, |
401 |
final Context context) { |
402 |
String result = Account.getAccount().displayNames.get(displayName);
|
403 |
if (result != null) { |
404 |
return result;
|
405 |
} |
406 |
final String url = Account.getAccount().getAuthServer(); |
407 |
AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() { |
408 |
|
409 |
@Override
|
410 |
protected Void doInBackground(Void... params) { |
411 |
try {
|
412 |
new ContainerObjectManager(context).getDisplayNames(url
|
413 |
.substring(0, url.length() - 3), Account |
414 |
.getAccount().getPassword(), Arrays
|
415 |
.asList(displayName), new ArrayList<String>()); |
416 |
} catch (Exception e) { |
417 |
// TODO Auto-generated catch block
|
418 |
e.printStackTrace(); |
419 |
} |
420 |
return null; |
421 |
} |
422 |
|
423 |
}; |
424 |
task.execute(); |
425 |
try {
|
426 |
task.get(); |
427 |
} catch (InterruptedException e) { |
428 |
// TODO Auto-generated catch block
|
429 |
e.printStackTrace(); |
430 |
} catch (ExecutionException e) { |
431 |
// TODO Auto-generated catch block
|
432 |
e.printStackTrace(); |
433 |
} |
434 |
//System.out.println(Account.getAccount().displayNames);
|
435 |
return Account.getAccount().displayNames.get(displayName);
|
436 |
} |
437 |
} |