root / src / com / rackspace / cloud / servers / api / client / http / Authentication.java @ 038ac9a4
History | View | Annotate | Download (2.1 kB)
1 |
/**
|
---|---|
2 |
*
|
3 |
*/
|
4 |
package com.rackspace.cloud.servers.api.client.http; |
5 |
|
6 |
import java.io.IOException; |
7 |
import java.net.URLEncoder; |
8 |
|
9 |
import org.apache.http.HttpResponse; |
10 |
import org.apache.http.client.ClientProtocolException; |
11 |
import org.apache.http.client.methods.HttpGet; |
12 |
|
13 |
import android.content.Context; |
14 |
import android.util.Log; |
15 |
|
16 |
import com.rackspace.cloud.files.api.client.CustomHttpClient; |
17 |
import com.rackspace.cloud.servers.api.client.Account; |
18 |
|
19 |
/**
|
20 |
* @author Mike Mayo - mike.mayo@rackspace.com - twitter.com/greenisus
|
21 |
*
|
22 |
*/
|
23 |
public class Authentication { |
24 |
|
25 |
public static boolean authenticate(Context context) { |
26 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
27 |
HttpGet get = new HttpGet(Account.getAccount().getAuthServer());
|
28 |
get.addHeader("X-Auth-User", Account.getAccount().getUsername());
|
29 |
get.addHeader("X-Auth-Key", "lMvqjHxChbQY+LgDqwWpYg==");// Account.getAccount().getApiKey()); |
30 |
Log.i("AUTH STARTED", "AUTH STARTED"); |
31 |
try {
|
32 |
HttpResponse resp = httpclient.execute(get); |
33 |
if (resp.getStatusLine().getStatusCode() == 204) { |
34 |
|
35 |
Account.getAccount().setAuthToken( |
36 |
getHeaderValue(resp, "X-Auth-Token"));
|
37 |
Account.getAccount().setServerUrl( |
38 |
getHeaderValue(resp, "X-Server-Management-Url"));
|
39 |
Account.getAccount().setStorageUrl( |
40 |
getHeaderValue(resp, "X-Storage-Url"));
|
41 |
Account.getAccount().setStorageToken( |
42 |
getHeaderValue(resp, "X-Storage-Token"));
|
43 |
if (Account.getAccount().getStorageToken() == null) |
44 |
Account.getAccount().setStorageToken( |
45 |
Account.getAccount().getAuthToken()); |
46 |
Account.getAccount().setCdnManagementUrl( |
47 |
getHeaderValue(resp, "X-Cdn-Management-Url"));
|
48 |
return true; |
49 |
} else {
|
50 |
Log.d("status code",
|
51 |
Integer.toString(resp.getStatusLine().getStatusCode()));
|
52 |
return false; |
53 |
} |
54 |
} catch (ClientProtocolException cpe) {
|
55 |
return false; |
56 |
} catch (IOException e) { |
57 |
Log.v("info", e.toString());
|
58 |
return false; |
59 |
} |
60 |
} |
61 |
|
62 |
private static String getHeaderValue(HttpResponse resp, String key) { |
63 |
if (resp.getFirstHeader(key) != null) |
64 |
return resp.getFirstHeader(key).getValue();
|
65 |
return null; |
66 |
} |
67 |
} |