root / src / com / rackspace / cloud / servers / api / client / http / Authentication.java @ f58bf9f4
History | View | Annotate | Download (1.6 kB)
1 |
/**
|
---|---|
2 |
*
|
3 |
*/
|
4 |
package com.rackspace.cloud.servers.api.client.http; |
5 |
|
6 |
import java.io.IOException; |
7 |
|
8 |
import org.apache.http.HttpResponse; |
9 |
import org.apache.http.client.ClientProtocolException; |
10 |
import org.apache.http.client.methods.HttpGet; |
11 |
import org.apache.http.impl.client.DefaultHttpClient; |
12 |
|
13 |
import android.util.Log; |
14 |
|
15 |
import com.rackspace.cloud.servers.api.client.Account; |
16 |
|
17 |
/**
|
18 |
* @author Mike Mayo - mike.mayo@rackspace.com - twitter.com/greenisus
|
19 |
*
|
20 |
*/
|
21 |
public class Authentication { |
22 |
|
23 |
public static boolean authenticate() { |
24 |
|
25 |
DefaultHttpClient httpclient = new DefaultHttpClient();
|
26 |
HttpGet get = new HttpGet(Account.getAuthServer());
|
27 |
|
28 |
get.addHeader("X-Auth-User", Account.getUsername());
|
29 |
get.addHeader("X-Auth-Key", Account.getApiKey());
|
30 |
|
31 |
try {
|
32 |
HttpResponse resp = httpclient.execute(get); |
33 |
|
34 |
if (resp.getStatusLine().getStatusCode() == 204) { |
35 |
Account.setAuthToken(resp.getFirstHeader("X-Auth-Token").getValue());
|
36 |
Account.setServerUrl(resp.getFirstHeader("X-Server-Management-Url").getValue());
|
37 |
Account.setStorageUrl(resp.getFirstHeader("X-Storage-Url").getValue());
|
38 |
Account.setStorageToken(resp.getFirstHeader("X-Storage-Token").getValue());
|
39 |
Account.setCdnManagementUrl(resp.getFirstHeader("X-Cdn-Management-Url").getValue());
|
40 |
return true; |
41 |
} else {
|
42 |
Log.d("status code", Integer.toString(resp.getStatusLine().getStatusCode())); |
43 |
return false; |
44 |
} |
45 |
} catch (ClientProtocolException cpe) {
|
46 |
return false; |
47 |
} catch (IOException e) { |
48 |
Log.v("info", e.toString());
|
49 |
return false; |
50 |
} |
51 |
} |
52 |
} |