root / src / com / rackspace / cloud / servers / api / client / http / Authentication.java @ 35e7942d
History | View | Annotate | Download (1.5 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 com.rackspace.cloud.servers.api.client.Account; |
14 |
|
15 |
/**
|
16 |
* @author Mike Mayo mike@overhrd.com
|
17 |
*
|
18 |
*/
|
19 |
public class Authentication { |
20 |
|
21 |
public static boolean authenticate() { |
22 |
DefaultHttpClient httpclient = new DefaultHttpClient();
|
23 |
HttpGet get = new HttpGet("https://auth.api.rackspacecloud.com/v1.0"); |
24 |
|
25 |
get.addHeader("X-Auth-User", Account.getUsername());
|
26 |
get.addHeader("X-Auth-Key", Account.getApiKey());
|
27 |
|
28 |
try {
|
29 |
HttpResponse resp = httpclient.execute(get); |
30 |
System.out.println(resp.getStatusLine().toString());
|
31 |
|
32 |
if (resp.getStatusLine().getStatusCode() == 204) { |
33 |
Account.setAuthToken(resp.getFirstHeader("X-Auth-Token").getValue());
|
34 |
Account.setServerUrl(resp.getFirstHeader("X-Server-Management-Url").getValue());
|
35 |
Account.setStorageUrl(resp.getFirstHeader("X-Storage-Url").getValue());
|
36 |
Account.setCdnManagementUrl(resp.getFirstHeader("X-Cdn-Management-Url").getValue());
|
37 |
return true; |
38 |
} else {
|
39 |
return false; |
40 |
} |
41 |
} catch (ClientProtocolException cpe) {
|
42 |
// TODO Auto-generated catch block
|
43 |
cpe.printStackTrace(); |
44 |
return false; |
45 |
} catch (IOException e) { |
46 |
// TODO Auto-generated catch block
|
47 |
e.printStackTrace(); |
48 |
return false; |
49 |
} |
50 |
} |
51 |
} |