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