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