root / src / com / rackspace / cloud / loadbalancer / api / client / LoadBalancerManager.java @ 6ae5d8db
History | View | Annotate | Download (16.7 kB)
1 |
/**
|
---|---|
2 |
*
|
3 |
*/
|
4 |
package com.rackspace.cloud.loadbalancer.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 android.util.Log; |
31 |
|
32 |
import com.rackspace.cloud.files.api.client.CustomHttpClient; |
33 |
import com.rackspace.cloud.loadbalancer.api.client.http.LoadBalancersException; |
34 |
import com.rackspace.cloud.loadbalancer.api.parsers.CloudLoadBalancersFaultXMLParser; |
35 |
import com.rackspace.cloud.loadbalancer.api.parsers.LoadBalancersXmlParser; |
36 |
import com.rackspace.cloud.servers.api.client.Account; |
37 |
import com.rackspace.cloud.servers.api.client.CloudServersException; |
38 |
import com.rackspace.cloud.servers.api.client.http.HttpBundle; |
39 |
import com.rackspacecloud.android.Preferences; |
40 |
|
41 |
public class LoadBalancerManager extends EntityManager { |
42 |
private Context context; |
43 |
|
44 |
public LoadBalancerManager(Context context) { |
45 |
this.context = context;
|
46 |
} |
47 |
|
48 |
public LoadBalancer getLoadBalancerById(long id) throws LoadBalancersException { |
49 |
Log.d("info", "the id is " + id); |
50 |
LoadBalancer loadBalancer = null;
|
51 |
//First try DFW
|
52 |
try{
|
53 |
loadBalancer = getLoadBalancerById(id, Account.getLoadBalancerDFWUrl()); |
54 |
loadBalancer.setRegion("DFW");
|
55 |
} catch(LoadBalancersException lbe){
|
56 |
//Didn't work
|
57 |
|
58 |
} |
59 |
|
60 |
//Then try ORD
|
61 |
if(loadBalancer == null){ |
62 |
try{
|
63 |
loadBalancer = getLoadBalancerById(id, Account.getLoadBalancerORDUrl()); |
64 |
loadBalancer.setRegion("ORD");
|
65 |
} |
66 |
catch(LoadBalancersException lbe){
|
67 |
|
68 |
} |
69 |
} |
70 |
|
71 |
//Then try LON
|
72 |
if(loadBalancer == null){ |
73 |
try{
|
74 |
loadBalancer = getLoadBalancerById(id, Account.getLoadBalancerLONUrl()); |
75 |
loadBalancer.setRegion("ORD");
|
76 |
} |
77 |
catch(LoadBalancersException lbe){
|
78 |
throw lbe;
|
79 |
} |
80 |
} |
81 |
return loadBalancer;
|
82 |
} |
83 |
|
84 |
private LoadBalancer getLoadBalancerById(long id, String url) throws LoadBalancersException { |
85 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
86 |
HttpGet get = new HttpGet(url + Account.getAccount().getAccountId() + "/loadbalancers/" + id); |
87 |
LoadBalancer loadBalancer = new LoadBalancer();
|
88 |
|
89 |
get.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
|
90 |
get.addHeader("Accept", "application/xml"); |
91 |
|
92 |
try {
|
93 |
HttpResponse resp = httpclient.execute(get); |
94 |
BasicResponseHandler responseHandler = new BasicResponseHandler();
|
95 |
String body = responseHandler.handleResponse(resp);
|
96 |
|
97 |
Log.d("info", "the xml body is " + body); |
98 |
|
99 |
if (resp.getStatusLine().getStatusCode() == 200 || resp.getStatusLine().getStatusCode() == 202) { |
100 |
LoadBalancersXmlParser loadBalancersXMLParser = new LoadBalancersXmlParser();
|
101 |
SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); |
102 |
XMLReader xmlReader = saxParser.getXMLReader(); |
103 |
xmlReader.setContentHandler(loadBalancersXMLParser); |
104 |
xmlReader.parse(new InputSource(new StringReader(body))); |
105 |
loadBalancer = loadBalancersXMLParser.getLoadBalancer(); |
106 |
} else {
|
107 |
CloudLoadBalancersFaultXMLParser parser = new CloudLoadBalancersFaultXMLParser();
|
108 |
SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); |
109 |
XMLReader xmlReader = saxParser.getXMLReader(); |
110 |
xmlReader.setContentHandler(parser); |
111 |
xmlReader.parse(new InputSource(new StringReader(body))); |
112 |
LoadBalancersException cse = parser.getException(); |
113 |
throw cse;
|
114 |
} |
115 |
} catch (ClientProtocolException e) {
|
116 |
LoadBalancersException cse = new LoadBalancersException();
|
117 |
cse.setMessage(e.getLocalizedMessage()); |
118 |
throw cse;
|
119 |
} catch (IOException e) { |
120 |
LoadBalancersException cse = new LoadBalancersException();
|
121 |
cse.setMessage(e.getLocalizedMessage()); |
122 |
throw cse;
|
123 |
} catch (ParserConfigurationException e) { |
124 |
LoadBalancersException cse = new LoadBalancersException();
|
125 |
cse.setMessage(e.getLocalizedMessage()); |
126 |
throw cse;
|
127 |
} catch (SAXException e) {
|
128 |
LoadBalancersException cse = new LoadBalancersException();
|
129 |
cse.setMessage(e.getLocalizedMessage()); |
130 |
throw cse;
|
131 |
} catch (FactoryConfigurationError e) { |
132 |
LoadBalancersException cse = new LoadBalancersException();
|
133 |
cse.setMessage(e.getLocalizedMessage()); |
134 |
throw cse;
|
135 |
} |
136 |
return loadBalancer;
|
137 |
} |
138 |
|
139 |
public ArrayList<LoadBalancer> createList() throws LoadBalancersException{ |
140 |
|
141 |
ArrayList<LoadBalancer> loadBalancers = new ArrayList<LoadBalancer>(); |
142 |
|
143 |
//if US account
|
144 |
if(Account.getAccount().getAuthServer().equals(Preferences.COUNTRY_US_AUTH_SERVER)){ |
145 |
loadBalancers.addAll(createSublist(Account.getLoadBalancerORDUrl())); |
146 |
for(LoadBalancer loadBalancer: loadBalancers){
|
147 |
loadBalancer.setRegion("ORD");
|
148 |
} |
149 |
ArrayList<LoadBalancer> DFWloadBalancers = createSublist(Account.getLoadBalancerDFWUrl());
|
150 |
for(LoadBalancer loadBalancer: DFWloadBalancers){
|
151 |
loadBalancer.setRegion("DFW");
|
152 |
} |
153 |
loadBalancers.addAll(DFWloadBalancers); |
154 |
} |
155 |
//if UK account
|
156 |
else if(Account.getAccount().getAuthServer().equals(Preferences.COUNTRY_UK_AUTH_SERVER)){ |
157 |
loadBalancers.addAll(createSublist(Account.getLoadBalancerLONUrl())); |
158 |
for(LoadBalancer loadBalancer: loadBalancers){
|
159 |
loadBalancer.setRegion("LON");
|
160 |
} |
161 |
} |
162 |
return loadBalancers;
|
163 |
} |
164 |
|
165 |
public ArrayList<LoadBalancer> createSublist(String regionUrl) throws LoadBalancersException { |
166 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
167 |
HttpGet get = new HttpGet(regionUrl + Account.getAccount().getAccountId() + "/loadbalancers"); |
168 |
ArrayList<LoadBalancer> loadBalancers = new ArrayList<LoadBalancer>(); |
169 |
|
170 |
get.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
|
171 |
get.addHeader("Accept", "application/xml"); |
172 |
|
173 |
try {
|
174 |
HttpResponse resp = httpclient.execute(get); |
175 |
BasicResponseHandler responseHandler = new BasicResponseHandler();
|
176 |
String body = responseHandler.handleResponse(resp);
|
177 |
if (resp.getStatusLine().getStatusCode() == 200 || resp.getStatusLine().getStatusCode() == 202) { |
178 |
LoadBalancersXmlParser loadBalancersXMLParser = new LoadBalancersXmlParser();
|
179 |
SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); |
180 |
XMLReader xmlReader = saxParser.getXMLReader(); |
181 |
xmlReader.setContentHandler(loadBalancersXMLParser); |
182 |
xmlReader.parse(new InputSource(new StringReader(body))); |
183 |
loadBalancers = loadBalancersXMLParser.getLoadBalancers(); |
184 |
} else {
|
185 |
CloudLoadBalancersFaultXMLParser parser = new CloudLoadBalancersFaultXMLParser();
|
186 |
SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); |
187 |
XMLReader xmlReader = saxParser.getXMLReader(); |
188 |
xmlReader.setContentHandler(parser); |
189 |
xmlReader.parse(new InputSource(new StringReader(body))); |
190 |
LoadBalancersException cse = parser.getException(); |
191 |
throw cse;
|
192 |
} |
193 |
} catch (ClientProtocolException e) {
|
194 |
LoadBalancersException cse = new LoadBalancersException();
|
195 |
cse.setMessage(e.getLocalizedMessage()); |
196 |
throw cse;
|
197 |
} catch (IOException e) { |
198 |
LoadBalancersException cse = new LoadBalancersException();
|
199 |
cse.setMessage(e.getLocalizedMessage()); |
200 |
throw cse;
|
201 |
} catch (ParserConfigurationException e) { |
202 |
LoadBalancersException cse = new LoadBalancersException();
|
203 |
cse.setMessage(e.getLocalizedMessage()); |
204 |
throw cse;
|
205 |
} catch (SAXException e) {
|
206 |
LoadBalancersException cse = new LoadBalancersException();
|
207 |
cse.setMessage(e.getLocalizedMessage()); |
208 |
throw cse;
|
209 |
} catch (FactoryConfigurationError e) { |
210 |
LoadBalancersException cse = new LoadBalancersException();
|
211 |
cse.setMessage(e.getLocalizedMessage()); |
212 |
throw cse;
|
213 |
} |
214 |
return loadBalancers;
|
215 |
} |
216 |
|
217 |
public HttpBundle create(LoadBalancer entity, String regionUrl) throws CloudServersException { |
218 |
HttpResponse resp = null;
|
219 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
220 |
|
221 |
HttpPost post = new HttpPost(regionUrl + Account.getAccount().getAccountId() + "/loadbalancers"); |
222 |
post.addHeader("Content-Type", "application/xml"); |
223 |
|
224 |
StringEntity tmp = null;
|
225 |
try {
|
226 |
tmp = new StringEntity(entity.toDetailedXML());
|
227 |
} catch (UnsupportedEncodingException e) { |
228 |
CloudServersException cse = new CloudServersException();
|
229 |
cse.setMessage(e.getLocalizedMessage()); |
230 |
throw cse;
|
231 |
} |
232 |
|
233 |
post.setEntity(tmp); |
234 |
|
235 |
post.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
|
236 |
httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class); |
237 |
|
238 |
HttpBundle bundle = new HttpBundle();
|
239 |
bundle.setCurlRequest(post); |
240 |
try {
|
241 |
resp = httpclient.execute(post); |
242 |
bundle.setHttpResponse(resp); |
243 |
} catch (ClientProtocolException e) {
|
244 |
CloudServersException cse = new CloudServersException();
|
245 |
cse.setMessage(e.getLocalizedMessage()); |
246 |
throw cse;
|
247 |
} catch (IOException e) { |
248 |
CloudServersException cse = new CloudServersException();
|
249 |
cse.setMessage(e.getLocalizedMessage()); |
250 |
throw cse;
|
251 |
} catch (FactoryConfigurationError e) { |
252 |
CloudServersException cse = new CloudServersException();
|
253 |
cse.setMessage(e.getLocalizedMessage()); |
254 |
throw cse;
|
255 |
} |
256 |
return bundle;
|
257 |
} |
258 |
|
259 |
public HttpBundle delete(LoadBalancer loadBalancer) throws CloudServersException { |
260 |
HttpResponse resp = null;
|
261 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
262 |
|
263 |
HttpDelete delete = new HttpDelete(LoadBalancer.getRegionUrl(loadBalancer.getRegion()) + Account.getAccount().getAccountId()
|
264 |
+ "/loadbalancers/" + loadBalancer.getId());
|
265 |
delete.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
|
266 |
delete.addHeader("Content-Type", "application/xml"); |
267 |
httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class); |
268 |
|
269 |
HttpBundle bundle = new HttpBundle();
|
270 |
bundle.setCurlRequest(delete); |
271 |
|
272 |
try {
|
273 |
resp = httpclient.execute(delete); |
274 |
bundle.setHttpResponse(resp); |
275 |
} catch (ClientProtocolException e) {
|
276 |
CloudServersException cse = new CloudServersException();
|
277 |
cse.setMessage(e.getLocalizedMessage()); |
278 |
throw cse;
|
279 |
} catch (IOException e) { |
280 |
CloudServersException cse = new CloudServersException();
|
281 |
cse.setMessage(e.getLocalizedMessage()); |
282 |
throw cse;
|
283 |
} catch (FactoryConfigurationError e) { |
284 |
CloudServersException cse = new CloudServersException();
|
285 |
cse.setMessage(e.getLocalizedMessage()); |
286 |
throw cse;
|
287 |
} |
288 |
return bundle;
|
289 |
} |
290 |
|
291 |
public HttpBundle update(LoadBalancer loadBalancer, String name, String algorithm, String protocol, String port) throws CloudServersException { |
292 |
HttpResponse resp = null;
|
293 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
294 |
|
295 |
HttpPut put = new HttpPut(LoadBalancer.getRegionUrl(loadBalancer.getRegion()) + Account.getAccount().getAccountId() + "/loadbalancers/" + loadBalancer.getId()); |
296 |
|
297 |
put.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
|
298 |
put.addHeader("Content-Type", "application/xml"); |
299 |
|
300 |
String xml = "<loadBalancer xmlns=\"http://docs.openstack.org/loadbalancers/api/v1.0\" " + |
301 |
"name=\"" + name + "\" " + |
302 |
"algorithm=\"" + algorithm.toUpperCase() + "\" " + |
303 |
"protocol=\"" + protocol.toUpperCase() + "\" " + |
304 |
"port=\"" + port + "\" />"; |
305 |
|
306 |
StringEntity tmp = null;
|
307 |
try {
|
308 |
tmp = new StringEntity(xml);
|
309 |
} catch (UnsupportedEncodingException e) { |
310 |
CloudServersException cse = new CloudServersException();
|
311 |
cse.setMessage(e.getLocalizedMessage()); |
312 |
throw cse;
|
313 |
} |
314 |
|
315 |
put.setEntity(tmp); |
316 |
httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class); |
317 |
|
318 |
HttpBundle bundle = new HttpBundle();
|
319 |
bundle.setCurlRequest(put); |
320 |
|
321 |
try {
|
322 |
resp = httpclient.execute(put); |
323 |
bundle.setHttpResponse(resp); |
324 |
} catch (ClientProtocolException e) {
|
325 |
CloudServersException cse = new CloudServersException();
|
326 |
cse.setMessage(e.getLocalizedMessage()); |
327 |
throw cse;
|
328 |
} catch (IOException e) { |
329 |
CloudServersException cse = new CloudServersException();
|
330 |
cse.setMessage(e.getLocalizedMessage()); |
331 |
throw cse;
|
332 |
} catch (FactoryConfigurationError e) { |
333 |
CloudServersException cse = new CloudServersException();
|
334 |
cse.setMessage(e.getLocalizedMessage()); |
335 |
throw cse;
|
336 |
} |
337 |
return bundle;
|
338 |
} |
339 |
|
340 |
public HttpBundle setLogging(LoadBalancer loadBalancer, Boolean setting) throws CloudServersException { |
341 |
HttpResponse resp = null;
|
342 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
343 |
|
344 |
HttpPut put = new HttpPut(LoadBalancer.getRegionUrl(loadBalancer.getRegion()) + Account.getAccount().getAccountId() + "/loadbalancers/" |
345 |
+ loadBalancer.getId() + "/connectionlogging");
|
346 |
|
347 |
put.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
|
348 |
put.addHeader("Content-Type", "application/xml"); |
349 |
|
350 |
String xml = "<connectionLogging xmlns=\"http://docs.openstack.org/loadbalancers/api/v1.0\" " + |
351 |
"enabled=\"" + setting.toString() + "\" />"; |
352 |
|
353 |
StringEntity tmp = null;
|
354 |
try {
|
355 |
tmp = new StringEntity(xml);
|
356 |
} catch (UnsupportedEncodingException e) { |
357 |
CloudServersException cse = new CloudServersException();
|
358 |
cse.setMessage(e.getLocalizedMessage()); |
359 |
throw cse;
|
360 |
} |
361 |
|
362 |
put.setEntity(tmp); |
363 |
httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class); |
364 |
|
365 |
HttpBundle bundle = new HttpBundle();
|
366 |
bundle.setCurlRequest(put); |
367 |
|
368 |
try {
|
369 |
resp = httpclient.execute(put); |
370 |
bundle.setHttpResponse(resp); |
371 |
} catch (ClientProtocolException e) {
|
372 |
CloudServersException cse = new CloudServersException();
|
373 |
cse.setMessage(e.getLocalizedMessage()); |
374 |
throw cse;
|
375 |
} catch (IOException e) { |
376 |
CloudServersException cse = new CloudServersException();
|
377 |
cse.setMessage(e.getLocalizedMessage()); |
378 |
throw cse;
|
379 |
} catch (FactoryConfigurationError e) { |
380 |
CloudServersException cse = new CloudServersException();
|
381 |
cse.setMessage(e.getLocalizedMessage()); |
382 |
throw cse;
|
383 |
} |
384 |
return bundle;
|
385 |
} |
386 |
|
387 |
public HttpBundle setSessionPersistence(LoadBalancer loadBalancer, String setting) throws CloudServersException { |
388 |
HttpResponse resp = null;
|
389 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
390 |
|
391 |
HttpPut put = new HttpPut(LoadBalancer.getRegionUrl(loadBalancer.getRegion()) + Account.getAccount().getAccountId() + "/loadbalancers/" |
392 |
+ loadBalancer.getId() + "/sessionpersistence");
|
393 |
|
394 |
put.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
|
395 |
put.addHeader("Content-Type", "application/xml"); |
396 |
|
397 |
String xml = "<sessionPersistence xmlns=\"http://docs.openstack.org/loadbalancers/api/v1.0\" persistenceType=\"" + setting + "\"/>"; |
398 |
|
399 |
Log.d("info", "session persist xml is: " + xml); |
400 |
|
401 |
StringEntity tmp = null;
|
402 |
try {
|
403 |
tmp = new StringEntity(xml);
|
404 |
} catch (UnsupportedEncodingException e) { |
405 |
CloudServersException cse = new CloudServersException();
|
406 |
cse.setMessage(e.getLocalizedMessage()); |
407 |
throw cse;
|
408 |
} |
409 |
|
410 |
put.setEntity(tmp); |
411 |
httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class); |
412 |
|
413 |
HttpBundle bundle = new HttpBundle();
|
414 |
bundle.setCurlRequest(put); |
415 |
|
416 |
try {
|
417 |
resp = httpclient.execute(put); |
418 |
bundle.setHttpResponse(resp); |
419 |
} catch (ClientProtocolException e) {
|
420 |
CloudServersException cse = new CloudServersException();
|
421 |
cse.setMessage(e.getLocalizedMessage()); |
422 |
throw cse;
|
423 |
} catch (IOException e) { |
424 |
CloudServersException cse = new CloudServersException();
|
425 |
cse.setMessage(e.getLocalizedMessage()); |
426 |
throw cse;
|
427 |
} catch (FactoryConfigurationError e) { |
428 |
CloudServersException cse = new CloudServersException();
|
429 |
cse.setMessage(e.getLocalizedMessage()); |
430 |
throw cse;
|
431 |
} |
432 |
return bundle;
|
433 |
} |
434 |
|
435 |
public HttpBundle disableSessionPersistence(LoadBalancer loadBalancer) throws CloudServersException { |
436 |
HttpResponse resp = null;
|
437 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
438 |
|
439 |
HttpDelete delete = new HttpDelete(LoadBalancer.getRegionUrl(loadBalancer.getRegion()) + Account.getAccount().getAccountId() + "/loadbalancers/" |
440 |
+ loadBalancer.getId() + "/sessionpersistence");
|
441 |
|
442 |
delete.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
|
443 |
|
444 |
httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class); |
445 |
|
446 |
HttpBundle bundle = new HttpBundle();
|
447 |
bundle.setCurlRequest(delete); |
448 |
|
449 |
try {
|
450 |
resp = httpclient.execute(delete); |
451 |
bundle.setHttpResponse(resp); |
452 |
} catch (ClientProtocolException e) {
|
453 |
CloudServersException cse = new CloudServersException();
|
454 |
cse.setMessage(e.getLocalizedMessage()); |
455 |
throw cse;
|
456 |
} catch (IOException e) { |
457 |
CloudServersException cse = new CloudServersException();
|
458 |
cse.setMessage(e.getLocalizedMessage()); |
459 |
throw cse;
|
460 |
} catch (FactoryConfigurationError e) { |
461 |
CloudServersException cse = new CloudServersException();
|
462 |
cse.setMessage(e.getLocalizedMessage()); |
463 |
throw cse;
|
464 |
} |
465 |
return bundle;
|
466 |
} |
467 |
|
468 |
} |