Statistics
| Branch: | Revision:

root / src / com / rackspace / cloud / servers / api / client / http / Authentication.java @ 2aaecc36

History | View | Annotate | Download (1.4 kB)

1 35e7942d Michael Mayo
/**
2 35e7942d Michael Mayo
 * 
3 35e7942d Michael Mayo
 */
4 35e7942d Michael Mayo
package com.rackspace.cloud.servers.api.client.http;
5 35e7942d Michael Mayo
6 35e7942d Michael Mayo
import java.io.IOException;
7 35e7942d Michael Mayo
8 35e7942d Michael Mayo
import org.apache.http.HttpResponse;
9 35e7942d Michael Mayo
import org.apache.http.client.ClientProtocolException;
10 35e7942d Michael Mayo
import org.apache.http.client.methods.HttpGet;
11 35e7942d Michael Mayo
import org.apache.http.impl.client.DefaultHttpClient;
12 35e7942d Michael Mayo
13 35e7942d Michael Mayo
import com.rackspace.cloud.servers.api.client.Account;
14 35e7942d Michael Mayo
15 35e7942d Michael Mayo
/**
16 e7274b25 Michael Mayo
 * @author Mike Mayo - mike.mayo@rackspace.com - twitter.com/greenisus
17 35e7942d Michael Mayo
 *
18 35e7942d Michael Mayo
 */
19 35e7942d Michael Mayo
public class Authentication {
20 35e7942d Michael Mayo
21 35e7942d Michael Mayo
        public static boolean authenticate() {
22 35e7942d Michael Mayo
                DefaultHttpClient httpclient = new DefaultHttpClient();
23 2aaecc36 Chmouel Boudjnah
                HttpGet get = new HttpGet(Account.getAuthServer());
24 35e7942d Michael Mayo
                
25 35e7942d Michael Mayo
                get.addHeader("X-Auth-User", Account.getUsername());
26 35e7942d Michael Mayo
                get.addHeader("X-Auth-Key", Account.getApiKey());
27 35e7942d Michael Mayo
                
28 35e7942d Michael Mayo
                try {                        
29 35e7942d Michael Mayo
                        HttpResponse resp = httpclient.execute(get);
30 35e7942d Michael Mayo
                    
31 35e7942d Michael Mayo
                    if (resp.getStatusLine().getStatusCode() == 204) {
32 35e7942d Michael Mayo
                            Account.setAuthToken(resp.getFirstHeader("X-Auth-Token").getValue());
33 35e7942d Michael Mayo
                            Account.setServerUrl(resp.getFirstHeader("X-Server-Management-Url").getValue());
34 35e7942d Michael Mayo
                            Account.setStorageUrl(resp.getFirstHeader("X-Storage-Url").getValue());
35 fb3e5d24 Phillip Toohill
                            Account.setStorageToken(resp.getFirstHeader("X-Storage-Token").getValue());
36 35e7942d Michael Mayo
                            Account.setCdnManagementUrl(resp.getFirstHeader("X-Cdn-Management-Url").getValue());
37 35e7942d Michael Mayo
                            return true;
38 35e7942d Michael Mayo
                    } else {
39 35e7942d Michael Mayo
                            return false;
40 35e7942d Michael Mayo
                    }
41 35e7942d Michael Mayo
                } catch (ClientProtocolException cpe) {
42 35e7942d Michael Mayo
                        return false;
43 35e7942d Michael Mayo
                } catch (IOException e) {
44 35e7942d Michael Mayo
                        return false;
45 35e7942d Michael Mayo
                }
46 35e7942d Michael Mayo
        }
47 35e7942d Michael Mayo
}