root / src / com / rackspace / cloud / servers / api / client / ServerManager.java @ af63e739
History | View | Annotate | Download (3 kB)
1 |
/**
|
---|---|
2 |
*
|
3 |
*/
|
4 |
package com.rackspace.cloud.servers.api.client; |
5 |
|
6 |
import java.io.IOException; |
7 |
import java.io.StringReader; |
8 |
import java.io.UnsupportedEncodingException; |
9 |
|
10 |
import javax.xml.parsers.FactoryConfigurationError; |
11 |
import javax.xml.parsers.ParserConfigurationException; |
12 |
import javax.xml.parsers.SAXParser; |
13 |
import javax.xml.parsers.SAXParserFactory; |
14 |
|
15 |
import org.apache.http.HttpResponse; |
16 |
import org.apache.http.client.ClientProtocolException; |
17 |
import org.apache.http.client.methods.HttpDelete; |
18 |
import org.apache.http.client.methods.HttpPost; |
19 |
import org.apache.http.entity.StringEntity; |
20 |
import org.apache.http.impl.client.BasicResponseHandler; |
21 |
import org.apache.http.impl.client.DefaultHttpClient; |
22 |
import org.apache.http.protocol.RequestExpectContinue; |
23 |
import org.xml.sax.InputSource; |
24 |
import org.xml.sax.SAXException; |
25 |
import org.xml.sax.XMLReader; |
26 |
|
27 |
import com.rackspace.cloud.servers.api.client.parsers.ServersXMLParser; |
28 |
|
29 |
/**
|
30 |
* @author mike
|
31 |
*
|
32 |
*/
|
33 |
public class ServerManager extends EntityManager { |
34 |
|
35 |
public static final String SOFT_REBOOT = "SOFT"; |
36 |
public static final String HARD_REBOOT = "HARD"; |
37 |
|
38 |
public HttpResponse reboot(Server server, String rebootType) { |
39 |
HttpResponse resp = null;
|
40 |
DefaultHttpClient httpclient = new DefaultHttpClient();
|
41 |
HttpPost post = new HttpPost(Account.getServerUrl() + "/servers/" + server.getId() + "/action.xml"); |
42 |
|
43 |
post.addHeader("X-Auth-Token", Account.getAuthToken());
|
44 |
post.addHeader("Content-Type", "application/xml"); |
45 |
|
46 |
StringEntity tmp = null;
|
47 |
try {
|
48 |
tmp = new StringEntity("<reboot xmlns=\"http://docs.rackspacecloud.com/servers/api/v1.0\" type=\"" + rebootType + "\"/>"); |
49 |
} catch (UnsupportedEncodingException e) { |
50 |
System.out.println("HTTPHelp : UnsupportedEncodingException : " + e); |
51 |
// TODO: handle?
|
52 |
} |
53 |
post.setEntity(tmp); |
54 |
|
55 |
httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class); |
56 |
|
57 |
try {
|
58 |
resp = httpclient.execute(post); |
59 |
} catch (ClientProtocolException cpe) {
|
60 |
// TODO Auto-generated catch block
|
61 |
cpe.printStackTrace(); |
62 |
} catch (IOException e) { |
63 |
// TODO Auto-generated catch block
|
64 |
e.printStackTrace(); |
65 |
//return false;
|
66 |
} catch (FactoryConfigurationError e) { |
67 |
// TODO Auto-generated catch block
|
68 |
e.printStackTrace(); |
69 |
} |
70 |
return resp;
|
71 |
} |
72 |
|
73 |
public HttpResponse delete(Server server) {
|
74 |
HttpResponse resp = null;
|
75 |
DefaultHttpClient httpclient = new DefaultHttpClient();
|
76 |
HttpDelete delete = new HttpDelete(Account.getServerUrl() + "/servers/" + server.getId() + ".xml"); |
77 |
|
78 |
delete.addHeader("X-Auth-Token", Account.getAuthToken());
|
79 |
delete.addHeader("Content-Type", "application/xml"); |
80 |
httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class); |
81 |
|
82 |
try {
|
83 |
resp = httpclient.execute(delete); |
84 |
} catch (ClientProtocolException cpe) {
|
85 |
// TODO Auto-generated catch block
|
86 |
cpe.printStackTrace(); |
87 |
} catch (IOException e) { |
88 |
// TODO Auto-generated catch block
|
89 |
e.printStackTrace(); |
90 |
//return false;
|
91 |
} catch (FactoryConfigurationError e) { |
92 |
// TODO Auto-generated catch block
|
93 |
e.printStackTrace(); |
94 |
} |
95 |
return resp;
|
96 |
} |
97 |
|
98 |
} |