root / src / com / rackspace / cloud / servers / api / client / ServerManager.java @ a7d1fdc2
History | View | Annotate | Download (21.4 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 |
import java.util.ArrayList; |
10 |
|
11 |
import javax.xml.parsers.FactoryConfigurationError; |
12 |
import javax.xml.parsers.ParserConfigurationException; |
13 |
import javax.xml.parsers.SAXParser; |
14 |
import javax.xml.parsers.SAXParserFactory; |
15 |
|
16 |
import org.apache.http.HttpResponse; |
17 |
import org.apache.http.client.ClientProtocolException; |
18 |
import org.apache.http.client.methods.HttpDelete; |
19 |
import org.apache.http.client.methods.HttpGet; |
20 |
import org.apache.http.client.methods.HttpPost; |
21 |
import org.apache.http.client.methods.HttpPut; |
22 |
import org.apache.http.entity.StringEntity; |
23 |
import org.apache.http.impl.client.BasicResponseHandler; |
24 |
import org.apache.http.protocol.RequestExpectContinue; |
25 |
import org.xml.sax.InputSource; |
26 |
import org.xml.sax.SAXException; |
27 |
import org.xml.sax.XMLReader; |
28 |
|
29 |
import android.content.Context; |
30 |
import com.rackspace.cloud.files.api.client.CustomHttpClient; |
31 |
import com.rackspace.cloud.servers.api.client.http.HttpBundle; |
32 |
import com.rackspace.cloud.servers.api.client.parsers.CloudServersFaultXMLParser; |
33 |
import com.rackspace.cloud.servers.api.client.parsers.ServersXMLParser; |
34 |
|
35 |
/**
|
36 |
* @author Mike Mayo - mike.mayo@rackspace.com - twitter.com/greenisus
|
37 |
*
|
38 |
*/
|
39 |
public class ServerManager extends EntityManager { |
40 |
|
41 |
public static final String SOFT_REBOOT = "SOFT"; |
42 |
public static final String HARD_REBOOT = "HARD"; |
43 |
|
44 |
public void create(Server entity, Context context) throws CloudServersException { |
45 |
|
46 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
47 |
HttpPost post = new HttpPost(Account.getAccount().getServerUrl() + "/servers.xml"); |
48 |
|
49 |
post.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
|
50 |
post.addHeader("Content-Type", "application/xml"); |
51 |
|
52 |
StringEntity tmp = null;
|
53 |
try {
|
54 |
tmp = new StringEntity(entity.toXML());
|
55 |
} catch (UnsupportedEncodingException e) { |
56 |
CloudServersException cse = new CloudServersException();
|
57 |
cse.setMessage(e.getLocalizedMessage()); |
58 |
throw cse;
|
59 |
} |
60 |
post.setEntity(tmp); |
61 |
|
62 |
httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class); |
63 |
|
64 |
try {
|
65 |
HttpResponse resp = httpclient.execute(post); |
66 |
BasicResponseHandler responseHandler = new BasicResponseHandler();
|
67 |
String body = responseHandler.handleResponse(resp);
|
68 |
|
69 |
if (resp.getStatusLine().getStatusCode() == 202) { |
70 |
ServersXMLParser serversXMLParser = new ServersXMLParser();
|
71 |
SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); |
72 |
XMLReader xmlReader = saxParser.getXMLReader(); |
73 |
xmlReader.setContentHandler(serversXMLParser); |
74 |
xmlReader.parse(new InputSource(new StringReader(body))); |
75 |
entity = serversXMLParser.getServer(); |
76 |
} else {
|
77 |
CloudServersFaultXMLParser parser = new CloudServersFaultXMLParser();
|
78 |
SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); |
79 |
XMLReader xmlReader = saxParser.getXMLReader(); |
80 |
xmlReader.setContentHandler(parser); |
81 |
xmlReader.parse(new InputSource(new StringReader(body))); |
82 |
CloudServersException cse = parser.getException(); |
83 |
throw cse;
|
84 |
} |
85 |
} catch (ClientProtocolException e) {
|
86 |
CloudServersException cse = new CloudServersException();
|
87 |
cse.setMessage(e.getLocalizedMessage()); |
88 |
throw cse;
|
89 |
} catch (IOException e) { |
90 |
CloudServersException cse = new CloudServersException();
|
91 |
cse.setMessage(e.getLocalizedMessage()); |
92 |
throw cse;
|
93 |
} catch (FactoryConfigurationError e) { |
94 |
CloudServersException cse = new CloudServersException();
|
95 |
cse.setMessage(e.getLocalizedMessage()); |
96 |
throw cse;
|
97 |
} catch (ParserConfigurationException e) { |
98 |
CloudServersException cse = new CloudServersException();
|
99 |
cse.setMessage(e.getLocalizedMessage()); |
100 |
throw cse;
|
101 |
} catch (SAXException e) {
|
102 |
CloudServersException cse = new CloudServersException();
|
103 |
cse.setMessage(e.getLocalizedMessage()); |
104 |
throw cse;
|
105 |
} |
106 |
} |
107 |
|
108 |
public ArrayList<Server> createList(boolean detail, Context context) throws CloudServersException { |
109 |
|
110 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
111 |
HttpGet get = new HttpGet(Account.getAccount().getServerUrl() + "/servers/detail.xml" + cacheBuster()); |
112 |
ArrayList<Server> servers = new ArrayList<Server>(); |
113 |
get.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
|
114 |
|
115 |
try {
|
116 |
HttpResponse resp = httpclient.execute(get); |
117 |
BasicResponseHandler responseHandler = new BasicResponseHandler();
|
118 |
String body = responseHandler.handleResponse(resp);
|
119 |
|
120 |
if (resp.getStatusLine().getStatusCode() == 200 || resp.getStatusLine().getStatusCode() == 203) { |
121 |
ServersXMLParser serversXMLParser = new ServersXMLParser();
|
122 |
SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); |
123 |
XMLReader xmlReader = saxParser.getXMLReader(); |
124 |
xmlReader.setContentHandler(serversXMLParser); |
125 |
xmlReader.parse(new InputSource(new StringReader(body))); |
126 |
servers = serversXMLParser.getServers(); |
127 |
} else {
|
128 |
CloudServersFaultXMLParser parser = new CloudServersFaultXMLParser();
|
129 |
SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); |
130 |
XMLReader xmlReader = saxParser.getXMLReader(); |
131 |
xmlReader.setContentHandler(parser); |
132 |
xmlReader.parse(new InputSource(new StringReader(body))); |
133 |
CloudServersException cse = parser.getException(); |
134 |
throw cse;
|
135 |
} |
136 |
} catch (ClientProtocolException e) {
|
137 |
CloudServersException cse = new CloudServersException();
|
138 |
cse.setMessage(e.getLocalizedMessage()); |
139 |
throw cse;
|
140 |
} catch (IOException e) { |
141 |
CloudServersException cse = new CloudServersException();
|
142 |
cse.setMessage(e.getLocalizedMessage()); |
143 |
throw cse;
|
144 |
} catch (ParserConfigurationException e) { |
145 |
CloudServersException cse = new CloudServersException();
|
146 |
cse.setMessage(e.getLocalizedMessage()); |
147 |
throw cse;
|
148 |
} catch (SAXException e) {
|
149 |
CloudServersException cse = new CloudServersException();
|
150 |
cse.setMessage(e.getLocalizedMessage()); |
151 |
throw cse;
|
152 |
} catch (FactoryConfigurationError e) { |
153 |
CloudServersException cse = new CloudServersException();
|
154 |
cse.setMessage(e.getLocalizedMessage()); |
155 |
throw cse;
|
156 |
} |
157 |
|
158 |
|
159 |
return servers;
|
160 |
} |
161 |
|
162 |
public Server find(long id, Context context) throws CloudServersException { |
163 |
|
164 |
Server server = null;
|
165 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
166 |
HttpGet get = new HttpGet(Account.getAccount().getServerUrl() + "/servers/" + id + ".xml" + cacheBuster()); |
167 |
get.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
|
168 |
|
169 |
try {
|
170 |
HttpResponse resp = httpclient.execute(get); |
171 |
BasicResponseHandler responseHandler = new BasicResponseHandler();
|
172 |
String body = responseHandler.handleResponse(resp);
|
173 |
|
174 |
if (resp.getStatusLine().getStatusCode() == 200 || resp.getStatusLine().getStatusCode() == 203) { |
175 |
ServersXMLParser serversXMLParser = new ServersXMLParser();
|
176 |
SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); |
177 |
XMLReader xmlReader = saxParser.getXMLReader(); |
178 |
xmlReader.setContentHandler(serversXMLParser); |
179 |
xmlReader.parse(new InputSource(new StringReader(body))); |
180 |
server = serversXMLParser.getServer(); |
181 |
} else {
|
182 |
CloudServersFaultXMLParser parser = new CloudServersFaultXMLParser();
|
183 |
SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); |
184 |
XMLReader xmlReader = saxParser.getXMLReader(); |
185 |
xmlReader.setContentHandler(parser); |
186 |
xmlReader.parse(new InputSource(new StringReader(body))); |
187 |
CloudServersException cse = parser.getException(); |
188 |
throw cse;
|
189 |
} |
190 |
} catch (ClientProtocolException e) {
|
191 |
CloudServersException cse = new CloudServersException();
|
192 |
cse.setMessage(e.getLocalizedMessage()); |
193 |
throw cse;
|
194 |
} catch (IOException e) { |
195 |
CloudServersException cse = new CloudServersException();
|
196 |
cse.setMessage(e.getLocalizedMessage()); |
197 |
throw cse;
|
198 |
} catch (ParserConfigurationException e) { |
199 |
CloudServersException cse = new CloudServersException();
|
200 |
cse.setMessage(e.getLocalizedMessage()); |
201 |
throw cse;
|
202 |
} catch (SAXException e) {
|
203 |
CloudServersException cse = new CloudServersException();
|
204 |
cse.setMessage(e.getLocalizedMessage()); |
205 |
throw cse;
|
206 |
} catch (FactoryConfigurationError e) { |
207 |
CloudServersException cse = new CloudServersException();
|
208 |
cse.setMessage(e.getLocalizedMessage()); |
209 |
throw cse;
|
210 |
} |
211 |
|
212 |
return server;
|
213 |
} |
214 |
|
215 |
public HttpBundle reboot(Server server, String rebootType, Context context) throws CloudServersException { |
216 |
HttpResponse resp = null;
|
217 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
218 |
HttpPost post = new HttpPost(Account.getAccount().getServerUrl() + "/servers/" + server.getId() + "/action.xml"); |
219 |
post.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
|
220 |
post.addHeader("Content-Type", "application/xml"); |
221 |
|
222 |
StringEntity tmp = null;
|
223 |
try {
|
224 |
tmp = new StringEntity("<reboot xmlns=\"http://docs.rackspacecloud.com/servers/api/v1.0\" type=\"" + rebootType + "\"/>"); |
225 |
} catch (UnsupportedEncodingException e) { |
226 |
CloudServersException cse = new CloudServersException();
|
227 |
cse.setMessage(e.getLocalizedMessage()); |
228 |
throw cse;
|
229 |
} |
230 |
post.setEntity(tmp); |
231 |
|
232 |
httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class); |
233 |
|
234 |
HttpBundle bundle = new HttpBundle();
|
235 |
bundle.setCurlRequest(post); |
236 |
|
237 |
try {
|
238 |
resp = httpclient.execute(post); |
239 |
bundle.setHttpResponse(resp); |
240 |
} catch (ClientProtocolException e) {
|
241 |
CloudServersException cse = new CloudServersException();
|
242 |
cse.setMessage(e.getLocalizedMessage()); |
243 |
throw cse;
|
244 |
} catch (IOException e) { |
245 |
CloudServersException cse = new CloudServersException();
|
246 |
cse.setMessage(e.getLocalizedMessage()); |
247 |
throw cse;
|
248 |
} catch (FactoryConfigurationError e) { |
249 |
CloudServersException cse = new CloudServersException();
|
250 |
cse.setMessage(e.getLocalizedMessage()); |
251 |
throw cse;
|
252 |
} |
253 |
return bundle;
|
254 |
} |
255 |
|
256 |
public HttpBundle resize(Server server, int flavorId, Context context) throws CloudServersException { |
257 |
HttpResponse resp = null;
|
258 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
259 |
HttpPost post = new HttpPost(Account.getAccount().getServerUrl() + "/servers/" + server.getId() + "/action.xml"); |
260 |
post.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
|
261 |
post.addHeader("Content-Type", "application/xml"); |
262 |
httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class); |
263 |
|
264 |
StringEntity tmp = null;
|
265 |
try {
|
266 |
tmp = new StringEntity("<resize xmlns=\"http://docs.rackspacecloud.com/servers/api/v1.0\" flavorId=\"" + flavorId + "\"/>"); |
267 |
} catch (UnsupportedEncodingException e) { |
268 |
CloudServersException cse = new CloudServersException();
|
269 |
cse.setMessage(e.getLocalizedMessage()); |
270 |
throw cse;
|
271 |
} |
272 |
post.setEntity(tmp); |
273 |
|
274 |
HttpBundle bundle = new HttpBundle();
|
275 |
bundle.setCurlRequest(post); |
276 |
|
277 |
try {
|
278 |
resp = httpclient.execute(post); |
279 |
bundle.setHttpResponse(resp); |
280 |
} catch (ClientProtocolException e) {
|
281 |
CloudServersException cse = new CloudServersException();
|
282 |
cse.setMessage(e.getLocalizedMessage()); |
283 |
throw cse;
|
284 |
} catch (IOException e) { |
285 |
CloudServersException cse = new CloudServersException();
|
286 |
cse.setMessage(e.getLocalizedMessage()); |
287 |
throw cse;
|
288 |
} catch (FactoryConfigurationError e) { |
289 |
CloudServersException cse = new CloudServersException();
|
290 |
cse.setMessage(e.getLocalizedMessage()); |
291 |
throw cse;
|
292 |
} |
293 |
return bundle;
|
294 |
} |
295 |
|
296 |
public HttpBundle confirmResize(Server server, Context context) throws CloudServersException { |
297 |
HttpResponse resp = null;
|
298 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
299 |
HttpPost post = new HttpPost(Account.getAccount().getServerUrl() + "/servers/" + server.getId() + "/action.xml"); |
300 |
post.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
|
301 |
post.addHeader("Content-Type", "application/xml"); |
302 |
httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class); |
303 |
|
304 |
StringEntity tmp = null;
|
305 |
try {
|
306 |
tmp = new StringEntity("<confirmResize xmlns=\"http://docs.rackspacecloud.com/servers/api/v1.0\"/>"); |
307 |
} catch (UnsupportedEncodingException e) { |
308 |
CloudServersException cse = new CloudServersException();
|
309 |
cse.setMessage(e.getLocalizedMessage()); |
310 |
throw cse;
|
311 |
} |
312 |
post.setEntity(tmp); |
313 |
|
314 |
HttpBundle bundle = new HttpBundle();
|
315 |
bundle.setCurlRequest(post); |
316 |
|
317 |
try {
|
318 |
resp = httpclient.execute(post); |
319 |
bundle.setHttpResponse(resp); |
320 |
} catch (ClientProtocolException e) {
|
321 |
CloudServersException cse = new CloudServersException();
|
322 |
cse.setMessage(e.getLocalizedMessage()); |
323 |
throw cse;
|
324 |
} catch (IOException e) { |
325 |
CloudServersException cse = new CloudServersException();
|
326 |
cse.setMessage(e.getLocalizedMessage()); |
327 |
throw cse;
|
328 |
} catch (FactoryConfigurationError e) { |
329 |
CloudServersException cse = new CloudServersException();
|
330 |
cse.setMessage(e.getLocalizedMessage()); |
331 |
throw cse;
|
332 |
} |
333 |
return bundle;
|
334 |
} |
335 |
|
336 |
public HttpBundle revertResize(Server server, Context context) throws CloudServersException { |
337 |
HttpResponse resp = null;
|
338 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
339 |
HttpPost post = new HttpPost(Account.getAccount().getServerUrl() + "/servers/" + server.getId() + "/action.xml"); |
340 |
post.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
|
341 |
post.addHeader("Content-Type", "application/xml"); |
342 |
httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class); |
343 |
|
344 |
StringEntity tmp = null;
|
345 |
try {
|
346 |
tmp = new StringEntity("<revertResize xmlns=\"http://docs.rackspacecloud.com/servers/api/v1.0\"/>"); |
347 |
} catch (UnsupportedEncodingException e) { |
348 |
CloudServersException cse = new CloudServersException();
|
349 |
cse.setMessage(e.getLocalizedMessage()); |
350 |
throw cse;
|
351 |
} |
352 |
post.setEntity(tmp); |
353 |
|
354 |
HttpBundle bundle = new HttpBundle();
|
355 |
bundle.setCurlRequest(post); |
356 |
|
357 |
try {
|
358 |
resp = httpclient.execute(post); |
359 |
bundle.setHttpResponse(resp); |
360 |
} catch (ClientProtocolException e) {
|
361 |
CloudServersException cse = new CloudServersException();
|
362 |
cse.setMessage(e.getLocalizedMessage()); |
363 |
throw cse;
|
364 |
} catch (IOException e) { |
365 |
CloudServersException cse = new CloudServersException();
|
366 |
cse.setMessage(e.getLocalizedMessage()); |
367 |
throw cse;
|
368 |
} catch (FactoryConfigurationError e) { |
369 |
CloudServersException cse = new CloudServersException();
|
370 |
cse.setMessage(e.getLocalizedMessage()); |
371 |
throw cse;
|
372 |
} |
373 |
return bundle;
|
374 |
} |
375 |
|
376 |
public HttpBundle delete(Server server, Context context) throws CloudServersException { |
377 |
HttpResponse resp = null;
|
378 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
379 |
HttpDelete delete = new HttpDelete(Account.getAccount().getServerUrl() + "/servers/" + server.getId() + ".xml"); |
380 |
delete.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
|
381 |
delete.addHeader("Content-Type", "application/xml"); |
382 |
httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class); |
383 |
|
384 |
HttpBundle bundle = new HttpBundle();
|
385 |
bundle.setCurlRequest(delete); |
386 |
|
387 |
try {
|
388 |
resp = httpclient.execute(delete); |
389 |
bundle.setHttpResponse(resp); |
390 |
} catch (ClientProtocolException e) {
|
391 |
CloudServersException cse = new CloudServersException();
|
392 |
cse.setMessage(e.getLocalizedMessage()); |
393 |
throw cse;
|
394 |
} catch (IOException e) { |
395 |
CloudServersException cse = new CloudServersException();
|
396 |
cse.setMessage(e.getLocalizedMessage()); |
397 |
throw cse;
|
398 |
} catch (FactoryConfigurationError e) { |
399 |
CloudServersException cse = new CloudServersException();
|
400 |
cse.setMessage(e.getLocalizedMessage()); |
401 |
throw cse;
|
402 |
} |
403 |
return bundle;
|
404 |
} |
405 |
|
406 |
public HttpBundle rename(Server server, String name, Context context) throws CloudServersException{ |
407 |
HttpResponse resp = null;
|
408 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
409 |
HttpPut put = new HttpPut(Account.getAccount().getServerUrl() + "/servers/" + server.getId() + ".xml"); |
410 |
|
411 |
put.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
|
412 |
put.addHeader("Content-Type", "application/xml"); |
413 |
httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class); |
414 |
|
415 |
StringEntity tmp = null;
|
416 |
try {
|
417 |
tmp = new StringEntity("<server xmlns=\"http://docs.rackspacecloud.com/servers/api/v1.0\" name=\"" + name + "\"/>"); |
418 |
} catch (UnsupportedEncodingException e) { |
419 |
CloudServersException cse = new CloudServersException();
|
420 |
cse.setMessage(e.getLocalizedMessage()); |
421 |
throw cse;
|
422 |
} |
423 |
put.setEntity(tmp); |
424 |
|
425 |
HttpBundle bundle = new HttpBundle();
|
426 |
bundle.setCurlRequest(put); |
427 |
|
428 |
try {
|
429 |
resp = httpclient.execute(put); |
430 |
bundle.setHttpResponse(resp); |
431 |
} catch (ClientProtocolException e) {
|
432 |
CloudServersException cse = new CloudServersException();
|
433 |
cse.setMessage(e.getLocalizedMessage()); |
434 |
throw cse;
|
435 |
} catch (IOException e) { |
436 |
CloudServersException cse = new CloudServersException();
|
437 |
cse.setMessage(e.getLocalizedMessage()); |
438 |
throw cse;
|
439 |
} catch (FactoryConfigurationError e) { |
440 |
CloudServersException cse = new CloudServersException();
|
441 |
cse.setMessage(e.getLocalizedMessage()); |
442 |
throw cse;
|
443 |
} |
444 |
return bundle;
|
445 |
} |
446 |
|
447 |
public HttpBundle rebuild(Server server, int imageId, Context context) throws CloudServersException { |
448 |
HttpResponse resp = null;
|
449 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
450 |
HttpPost post = new HttpPost(Account.getAccount().getServerUrl() + "/servers/" + server.getId() + "/action.xml"); |
451 |
|
452 |
post.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
|
453 |
post.addHeader("Content-Type", "application/xml"); |
454 |
httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class); |
455 |
|
456 |
StringEntity tmp = null;
|
457 |
try {
|
458 |
tmp = new StringEntity("<rebuild xmlns=\"http://docs.rackspacecloud.com/servers/api/v1.0\" imageId=\"" + imageId + "\"/>"); |
459 |
} catch (UnsupportedEncodingException e) { |
460 |
CloudServersException cse = new CloudServersException();
|
461 |
cse.setMessage(e.getLocalizedMessage()); |
462 |
throw cse;
|
463 |
} |
464 |
post.setEntity(tmp); |
465 |
|
466 |
HttpBundle bundle = new HttpBundle();
|
467 |
bundle.setCurlRequest(post); |
468 |
|
469 |
try {
|
470 |
resp = httpclient.execute(post); |
471 |
bundle.setHttpResponse(resp); |
472 |
} catch (ClientProtocolException e) {
|
473 |
CloudServersException cse = new CloudServersException();
|
474 |
cse.setMessage(e.getLocalizedMessage()); |
475 |
throw cse;
|
476 |
} catch (IOException e) { |
477 |
CloudServersException cse = new CloudServersException();
|
478 |
cse.setMessage(e.getLocalizedMessage()); |
479 |
throw cse;
|
480 |
} catch (FactoryConfigurationError e) { |
481 |
CloudServersException cse = new CloudServersException();
|
482 |
cse.setMessage(e.getLocalizedMessage()); |
483 |
throw cse;
|
484 |
} |
485 |
return bundle;
|
486 |
} |
487 |
|
488 |
public HttpBundle backup(Server server, String weeklyValue, String dailyValue, boolean enabled, Context context) throws CloudServersException { |
489 |
HttpResponse resp = null;
|
490 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
491 |
HttpPost post = new HttpPost(Account.getAccount().getServerUrl() + "/servers/" + server.getId() + "/backup_schedule.xml"); |
492 |
|
493 |
post.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
|
494 |
post.addHeader("Content-Type", "application/xml"); |
495 |
httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class); |
496 |
|
497 |
StringEntity tmp = null;
|
498 |
try {
|
499 |
tmp = new StringEntity("<backupSchedule xmlns=\"http://docs.rackspacecloud.com/servers/api/v1.0\" enabled=\"" + enabled + "\" " + |
500 |
"weekly=\"" + weeklyValue + "\" daily=\"" + dailyValue + "\"/>"); |
501 |
|
502 |
} catch (UnsupportedEncodingException e) { |
503 |
CloudServersException cse = new CloudServersException();
|
504 |
cse.setMessage(e.getLocalizedMessage()); |
505 |
throw cse;
|
506 |
} |
507 |
post.setEntity(tmp); |
508 |
|
509 |
HttpBundle bundle = new HttpBundle();
|
510 |
bundle.setCurlRequest(post); |
511 |
|
512 |
try {
|
513 |
resp = httpclient.execute(post); |
514 |
bundle.setHttpResponse(resp); |
515 |
} catch (ClientProtocolException e) {
|
516 |
CloudServersException cse = new CloudServersException();
|
517 |
cse.setMessage(e.getLocalizedMessage()); |
518 |
throw cse;
|
519 |
} catch (IOException e) { |
520 |
CloudServersException cse = new CloudServersException();
|
521 |
cse.setMessage(e.getLocalizedMessage()); |
522 |
throw cse;
|
523 |
} catch (FactoryConfigurationError e) { |
524 |
CloudServersException cse = new CloudServersException();
|
525 |
cse.setMessage(e.getLocalizedMessage()); |
526 |
throw cse;
|
527 |
} |
528 |
return bundle;
|
529 |
} |
530 |
|
531 |
public HttpBundle changePassword(Server server, String password, Context context) throws CloudServersException{ |
532 |
HttpResponse resp = null;
|
533 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
534 |
HttpPut put = new HttpPut(Account.getAccount().getServerUrl() + "/servers/" + server.getId() + ".xml"); |
535 |
|
536 |
put.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
|
537 |
put.addHeader("Content-Type", "application/xml"); |
538 |
httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class); |
539 |
|
540 |
StringEntity tmp = null;
|
541 |
try {
|
542 |
tmp = new StringEntity("<server xmlns=\"http://docs.rackspacecloud.com/servers/api/v1.0\" adminPass=\"" + password + "\"/>"); |
543 |
} catch (UnsupportedEncodingException e) { |
544 |
CloudServersException cse = new CloudServersException();
|
545 |
cse.setMessage(e.getLocalizedMessage()); |
546 |
throw cse;
|
547 |
} |
548 |
put.setEntity(tmp); |
549 |
|
550 |
HttpBundle bundle = new HttpBundle();
|
551 |
bundle.setCurlRequest(put); |
552 |
|
553 |
try {
|
554 |
resp = httpclient.execute(put); |
555 |
bundle.setHttpResponse(resp); |
556 |
} catch (ClientProtocolException e) {
|
557 |
CloudServersException cse = new CloudServersException();
|
558 |
cse.setMessage(e.getLocalizedMessage()); |
559 |
throw cse;
|
560 |
} catch (IOException e) { |
561 |
CloudServersException cse = new CloudServersException();
|
562 |
cse.setMessage(e.getLocalizedMessage()); |
563 |
throw cse;
|
564 |
} catch (FactoryConfigurationError e) { |
565 |
CloudServersException cse = new CloudServersException();
|
566 |
cse.setMessage(e.getLocalizedMessage()); |
567 |
throw cse;
|
568 |
} |
569 |
return bundle;
|
570 |
} |
571 |
|
572 |
|
573 |
} |