Revision 71e52c4a src/com/rackspace/cloud/loadbalancer/api/client/LoadBalancerManager.java

b/src/com/rackspace/cloud/loadbalancer/api/client/LoadBalancerManager.java
45 45
		this.context = context;
46 46
	}
47 47

  
48
	public LoadBalancer getLoadBalancerById(long id) throws LoadBalancersException {
48
	public LoadBalancer getLoadBalancerById(long id)
49
			throws LoadBalancersException {
49 50
		LoadBalancer loadBalancer = null;
50
		//First try DFW
51
		try{
52
			loadBalancer = getLoadBalancerById(id, Account.getLoadBalancerDFWUrl());
51
		// First try DFW
52
		try {
53
			loadBalancer = getLoadBalancerById(id,
54
					Account.getLoadBalancerDFWUrl());
53 55
			loadBalancer.setRegion("DFW");
54
		} catch(LoadBalancersException lbe){
55
			//Didn't work
56
		} catch (LoadBalancersException lbe) {
57
			// Didn't work
56 58

  
57 59
		}
58 60

  
59
		//Then try ORD
60
		if(loadBalancer == null){
61
			try{
62
				loadBalancer = getLoadBalancerById(id, Account.getLoadBalancerORDUrl());
61
		// Then try ORD
62
		if (loadBalancer == null) {
63
			try {
64
				loadBalancer = getLoadBalancerById(id,
65
						Account.getLoadBalancerORDUrl());
63 66
				loadBalancer.setRegion("ORD");
64
			}
65
			catch(LoadBalancersException lbe){
67
			} catch (LoadBalancersException lbe) {
66 68

  
67 69
			}
68 70
		}
69 71

  
70
		//Then try LON
71
		if(loadBalancer == null){
72
			try{
73
				loadBalancer = getLoadBalancerById(id, Account.getLoadBalancerLONUrl());
72
		// Then try LON
73
		if (loadBalancer == null) {
74
			try {
75
				loadBalancer = getLoadBalancerById(id,
76
						Account.getLoadBalancerLONUrl());
74 77
				loadBalancer.setRegion("LON");
75
			}
76
			catch(LoadBalancersException lbe){
78
			} catch (LoadBalancersException lbe) {
77 79
				throw lbe;
78 80
			}
79 81
		}
80 82
		return loadBalancer;
81 83
	}
82 84

  
83
	private LoadBalancer getLoadBalancerById(long id, String url) throws LoadBalancersException {
85
	private LoadBalancer getLoadBalancerById(long id, String url)
86
			throws LoadBalancersException {
84 87
		CustomHttpClient httpclient = new CustomHttpClient(context);
85
		HttpGet get = new HttpGet(url + Account.getAccount().getAccountId() + "/loadbalancers/" + id);
88
		HttpGet get = new HttpGet(url + Account.getAccount().getAccountId()
89
				+ "/loadbalancers/" + id);
86 90
		LoadBalancer loadBalancer = new LoadBalancer();
87 91

  
88 92
		get.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
89 93
		get.addHeader("Accept", "application/xml");
90 94

  
91
		try {			
92
			HttpResponse resp = httpclient.execute(get);		    
95
		try {
96
			HttpResponse resp = httpclient.execute(get);
93 97
			BasicResponseHandler responseHandler = new BasicResponseHandler();
94 98
			String body = responseHandler.handleResponse(resp);
95 99

  
96
			if (resp.getStatusLine().getStatusCode() == 200 || resp.getStatusLine().getStatusCode() == 202) {		    	
100
			if (resp.getStatusLine().getStatusCode() == 200
101
					|| resp.getStatusLine().getStatusCode() == 202) {
97 102
				LoadBalancersXmlParser loadBalancersXMLParser = new LoadBalancersXmlParser();
98
				SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
103
				SAXParser saxParser = SAXParserFactory.newInstance()
104
						.newSAXParser();
99 105
				XMLReader xmlReader = saxParser.getXMLReader();
100 106
				xmlReader.setContentHandler(loadBalancersXMLParser);
101
				xmlReader.parse(new InputSource(new StringReader(body)));		    	
102
				loadBalancer = loadBalancersXMLParser.getLoadBalancer();		    	
107
				xmlReader.parse(new InputSource(new StringReader(body)));
108
				loadBalancer = loadBalancersXMLParser.getLoadBalancer();
103 109
			} else {
104 110
				CloudLoadBalancersFaultXMLParser parser = new CloudLoadBalancersFaultXMLParser();
105
				SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
111
				SAXParser saxParser = SAXParserFactory.newInstance()
112
						.newSAXParser();
106 113
				XMLReader xmlReader = saxParser.getXMLReader();
107 114
				xmlReader.setContentHandler(parser);
108
				xmlReader.parse(new InputSource(new StringReader(body)));		    	
109
				LoadBalancersException cse = parser.getException();		    	
115
				xmlReader.parse(new InputSource(new StringReader(body)));
116
				LoadBalancersException cse = parser.getException();
110 117
				throw cse;
111 118
			}
112 119
		} catch (ClientProtocolException e) {
......
133 140
		return loadBalancer;
134 141
	}
135 142

  
136
	public ArrayList<LoadBalancer> createList() throws LoadBalancersException{
137
		
143
	public ArrayList<LoadBalancer> createList() throws LoadBalancersException {
144

  
138 145
		ArrayList<LoadBalancer> loadBalancers = new ArrayList<LoadBalancer>();
139
		
140
		//if US account
141
		if(Account.getAccount().getAuthServer().equals(Preferences.COUNTRY_US_AUTH_SERVER)){
142
			loadBalancers.addAll(createSublist(Account.getLoadBalancerORDUrl()));
143
			for(LoadBalancer loadBalancer: loadBalancers){
146

  
147
		// if US account
148
		if (Account.getAccount().getAuthServer()
149
				.equals(Preferences.COUNTRY_US_AUTH_SERVER)) {
150
			loadBalancers
151
					.addAll(createSublist(Account.getLoadBalancerORDUrl()));
152
			for (LoadBalancer loadBalancer : loadBalancers) {
144 153
				loadBalancer.setRegion("ORD");
145 154
			}
146
			ArrayList<LoadBalancer> DFWloadBalancers = createSublist(Account.getLoadBalancerDFWUrl());
147
			for(LoadBalancer loadBalancer: DFWloadBalancers){
155
			ArrayList<LoadBalancer> DFWloadBalancers = createSublist(Account
156
					.getLoadBalancerDFWUrl());
157
			for (LoadBalancer loadBalancer : DFWloadBalancers) {
148 158
				loadBalancer.setRegion("DFW");
149 159
			}
150 160
			loadBalancers.addAll(DFWloadBalancers);
151 161
		}
152
		//if UK account
153
		else if(Account.getAccount().getAuthServer().equals(Preferences.COUNTRY_UK_AUTH_SERVER)){
154
			loadBalancers.addAll(createSublist(Account.getLoadBalancerLONUrl()));
155
			for(LoadBalancer loadBalancer: loadBalancers){
162
		// if UK account
163
		else if (Account.getAccount().getAuthServer()
164
				.equals(Preferences.COUNTRY_UK_AUTH_SERVER)) {
165
			loadBalancers
166
					.addAll(createSublist(Account.getLoadBalancerLONUrl()));
167
			for (LoadBalancer loadBalancer : loadBalancers) {
156 168
				loadBalancer.setRegion("LON");
157 169
			}
158 170
		}
159 171
		return loadBalancers;
160 172
	}
161 173

  
162
	public ArrayList<LoadBalancer> createSublist(String regionUrl) throws LoadBalancersException {
174
	public ArrayList<LoadBalancer> createSublist(String regionUrl)
175
			throws LoadBalancersException {
163 176
		CustomHttpClient httpclient = new CustomHttpClient(context);
164
		HttpGet get = new HttpGet(regionUrl + Account.getAccount().getAccountId() + "/loadbalancers");
177
		HttpGet get = new HttpGet(regionUrl
178
				+ Account.getAccount().getAccountId() + "/loadbalancers");
165 179
		ArrayList<LoadBalancer> loadBalancers = new ArrayList<LoadBalancer>();
166 180

  
167 181
		get.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
168 182
		get.addHeader("Accept", "application/xml");
169 183

  
170
		try {			
171
			HttpResponse resp = httpclient.execute(get);		    
184
		try {
185
			HttpResponse resp = httpclient.execute(get);
172 186
			BasicResponseHandler responseHandler = new BasicResponseHandler();
173 187
			String body = responseHandler.handleResponse(resp);
174
			if (resp.getStatusLine().getStatusCode() == 200 || resp.getStatusLine().getStatusCode() == 202) {		    	
188
			if (resp.getStatusLine().getStatusCode() == 200
189
					|| resp.getStatusLine().getStatusCode() == 202) {
175 190
				LoadBalancersXmlParser loadBalancersXMLParser = new LoadBalancersXmlParser();
176
				SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
191
				SAXParser saxParser = SAXParserFactory.newInstance()
192
						.newSAXParser();
177 193
				XMLReader xmlReader = saxParser.getXMLReader();
178 194
				xmlReader.setContentHandler(loadBalancersXMLParser);
179
				xmlReader.parse(new InputSource(new StringReader(body)));		    	
180
				loadBalancers = loadBalancersXMLParser.getLoadBalancers();		    	
195
				xmlReader.parse(new InputSource(new StringReader(body)));
196
				loadBalancers = loadBalancersXMLParser.getLoadBalancers();
181 197
			} else {
182 198
				CloudLoadBalancersFaultXMLParser parser = new CloudLoadBalancersFaultXMLParser();
183
				SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
199
				SAXParser saxParser = SAXParserFactory.newInstance()
200
						.newSAXParser();
184 201
				XMLReader xmlReader = saxParser.getXMLReader();
185 202
				xmlReader.setContentHandler(parser);
186
				xmlReader.parse(new InputSource(new StringReader(body)));		    	
187
				LoadBalancersException cse = parser.getException();		    	
203
				xmlReader.parse(new InputSource(new StringReader(body)));
204
				LoadBalancersException cse = parser.getException();
188 205
				throw cse;
189 206
			}
190 207
		} catch (ClientProtocolException e) {
......
211 228
		return loadBalancers;
212 229
	}
213 230

  
214
	public HttpBundle create(LoadBalancer entity, String regionUrl) throws CloudServersException {
231
	public HttpBundle create(LoadBalancer entity, String regionUrl)
232
			throws CloudServersException {
215 233
		HttpResponse resp = null;
216 234
		CustomHttpClient httpclient = new CustomHttpClient(context);
217 235

  
218
		HttpPost post = new HttpPost(regionUrl + Account.getAccount().getAccountId() + "/loadbalancers");
236
		HttpPost post = new HttpPost(regionUrl
237
				+ Account.getAccount().getAccountId() + "/loadbalancers");
219 238
		post.addHeader("Content-Type", "application/xml");
220 239

  
221 240
		StringEntity tmp = null;
......
253 272
		return bundle;
254 273
	}
255 274

  
256
	public HttpBundle delete(LoadBalancer loadBalancer) throws CloudServersException {
275
	public HttpBundle delete(LoadBalancer loadBalancer)
276
			throws CloudServersException {
257 277
		HttpResponse resp = null;
258 278
		CustomHttpClient httpclient = new CustomHttpClient(context);
259 279

  
260
		HttpDelete delete = new HttpDelete(LoadBalancer.getRegionUrl(loadBalancer.getRegion()) + Account.getAccount().getAccountId() 
261
				+ "/loadbalancers/" + loadBalancer.getId());				
280
		HttpDelete delete = new HttpDelete(
281
				LoadBalancer.getRegionUrl(loadBalancer.getRegion())
282
						+ Account.getAccount().getAccountId()
283
						+ "/loadbalancers/" + loadBalancer.getId());
262 284
		delete.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
263 285
		delete.addHeader("Content-Type", "application/xml");
264 286
		httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class);
......
266 288
		HttpBundle bundle = new HttpBundle();
267 289
		bundle.setCurlRequest(delete);
268 290

  
269
		try {			
291
		try {
270 292
			resp = httpclient.execute(delete);
271 293
			bundle.setHttpResponse(resp);
272 294
		} catch (ClientProtocolException e) {
......
281 303
			CloudServersException cse = new CloudServersException();
282 304
			cse.setMessage(e.getLocalizedMessage());
283 305
			throw cse;
284
		}	
306
		}
285 307
		return bundle;
286 308
	}
287 309

  
288
	public HttpBundle update(LoadBalancer loadBalancer, String name, String algorithm, String protocol, String port) throws CloudServersException {
310
	public HttpBundle update(LoadBalancer loadBalancer, String name,
311
			String algorithm, String protocol, String port)
312
			throws CloudServersException {
289 313
		HttpResponse resp = null;
290 314
		CustomHttpClient httpclient = new CustomHttpClient(context);
291 315

  
292
		HttpPut put = new HttpPut(LoadBalancer.getRegionUrl(loadBalancer.getRegion()) + Account.getAccount().getAccountId() + "/loadbalancers/" + loadBalancer.getId());				
316
		HttpPut put = new HttpPut(LoadBalancer.getRegionUrl(loadBalancer
317
				.getRegion())
318
				+ Account.getAccount().getAccountId()
319
				+ "/loadbalancers/" + loadBalancer.getId());
293 320

  
294 321
		put.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
295 322
		put.addHeader("Content-Type", "application/xml");
296 323

  
297
		String xml = "<loadBalancer xmlns=\"http://docs.openstack.org/loadbalancers/api/v1.0\" " + 
298
		"name=\"" + name + "\" " + 
299
		"algorithm=\"" + algorithm.toUpperCase() + "\" " + 
300
		"protocol=\"" + protocol.toUpperCase() + "\" " + 
301
		"port=\"" + port + "\" />";
324
		String xml = "<loadBalancer xmlns=\"http://docs.openstack.org/loadbalancers/api/v1.0\" "
325
				+ "name=\""
326
				+ name
327
				+ "\" "
328
				+ "algorithm=\""
329
				+ algorithm.toUpperCase()
330
				+ "\" "
331
				+ "protocol=\""
332
				+ protocol + "\" " + "port=\"" + port + "\" />";
302 333

  
303 334
		StringEntity tmp = null;
304 335
		try {
......
315 346
		HttpBundle bundle = new HttpBundle();
316 347
		bundle.setCurlRequest(put);
317 348

  
318
		try {			
349
		try {
319 350
			resp = httpclient.execute(put);
320 351
			bundle.setHttpResponse(resp);
321 352
		} catch (ClientProtocolException e) {
......
330 361
			CloudServersException cse = new CloudServersException();
331 362
			cse.setMessage(e.getLocalizedMessage());
332 363
			throw cse;
333
		}	
364
		}
334 365
		return bundle;
335 366
	}
336
	
337
	public HttpBundle setLogging(LoadBalancer loadBalancer, Boolean setting) throws CloudServersException {
367

  
368
	public HttpBundle setLogging(LoadBalancer loadBalancer, Boolean setting)
369
			throws CloudServersException {
338 370
		HttpResponse resp = null;
339 371
		CustomHttpClient httpclient = new CustomHttpClient(context);
340 372

  
341
		HttpPut put = new HttpPut(LoadBalancer.getRegionUrl(loadBalancer.getRegion()) + Account.getAccount().getAccountId() + "/loadbalancers/" 
342
				+ loadBalancer.getId() + "/connectionlogging");				
373
		HttpPut put = new HttpPut(LoadBalancer.getRegionUrl(loadBalancer
374
				.getRegion())
375
				+ Account.getAccount().getAccountId()
376
				+ "/loadbalancers/"
377
				+ loadBalancer.getId()
378
				+ "/connectionlogging");
343 379

  
344 380
		put.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
345 381
		put.addHeader("Content-Type", "application/xml");
346 382

  
347
		String xml = "<connectionLogging xmlns=\"http://docs.openstack.org/loadbalancers/api/v1.0\" " + 
348
		"enabled=\"" + setting.toString() + "\" />";
383
		String xml = "<connectionLogging xmlns=\"http://docs.openstack.org/loadbalancers/api/v1.0\" "
384
				+ "enabled=\"" + setting.toString() + "\" />";
349 385

  
350 386
		StringEntity tmp = null;
351 387
		try {
......
362 398
		HttpBundle bundle = new HttpBundle();
363 399
		bundle.setCurlRequest(put);
364 400

  
365
		try {			
401
		try {
366 402
			resp = httpclient.execute(put);
367 403
			bundle.setHttpResponse(resp);
368 404
		} catch (ClientProtocolException e) {
......
377 413
			CloudServersException cse = new CloudServersException();
378 414
			cse.setMessage(e.getLocalizedMessage());
379 415
			throw cse;
380
		}	
416
		}
381 417
		return bundle;
382 418
	}
383
	
384
	public HttpBundle setSessionPersistence(LoadBalancer loadBalancer, String setting) throws CloudServersException {
419

  
420
	public HttpBundle setSessionPersistence(LoadBalancer loadBalancer,
421
			String setting) throws CloudServersException {
385 422
		HttpResponse resp = null;
386 423
		CustomHttpClient httpclient = new CustomHttpClient(context);
387 424

  
388
		HttpPut put = new HttpPut(LoadBalancer.getRegionUrl(loadBalancer.getRegion()) + Account.getAccount().getAccountId() + "/loadbalancers/" 
389
				+ loadBalancer.getId() + "/sessionpersistence");				
425
		HttpPut put = new HttpPut(LoadBalancer.getRegionUrl(loadBalancer
426
				.getRegion())
427
				+ Account.getAccount().getAccountId()
428
				+ "/loadbalancers/"
429
				+ loadBalancer.getId()
430
				+ "/sessionpersistence");
390 431

  
391 432
		put.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
392 433
		put.addHeader("Content-Type", "application/xml");
393 434

  
394
		String xml = "<sessionPersistence xmlns=\"http://docs.openstack.org/loadbalancers/api/v1.0\" persistenceType=\"" + setting + "\"/>";
435
		String xml = "<sessionPersistence xmlns=\"http://docs.openstack.org/loadbalancers/api/v1.0\" persistenceType=\""
436
				+ setting + "\"/>";
395 437

  
396 438
		Log.d("info", "session persist xml is: " + xml);
397
		
439

  
398 440
		StringEntity tmp = null;
399 441
		try {
400 442
			tmp = new StringEntity(xml);
......
410 452
		HttpBundle bundle = new HttpBundle();
411 453
		bundle.setCurlRequest(put);
412 454

  
413
		try {			
455
		try {
414 456
			resp = httpclient.execute(put);
415 457
			bundle.setHttpResponse(resp);
416 458
		} catch (ClientProtocolException e) {
......
425 467
			CloudServersException cse = new CloudServersException();
426 468
			cse.setMessage(e.getLocalizedMessage());
427 469
			throw cse;
428
		}	
470
		}
429 471
		return bundle;
430 472
	}
431 473

  
432
	public HttpBundle disableSessionPersistence(LoadBalancer loadBalancer) throws CloudServersException {
474
	public HttpBundle disableSessionPersistence(LoadBalancer loadBalancer)
475
			throws CloudServersException {
433 476
		HttpResponse resp = null;
434 477
		CustomHttpClient httpclient = new CustomHttpClient(context);
435 478

  
436
		HttpDelete delete = new HttpDelete(LoadBalancer.getRegionUrl(loadBalancer.getRegion()) + Account.getAccount().getAccountId() + "/loadbalancers/" 
437
				+ loadBalancer.getId() + "/sessionpersistence");				
479
		HttpDelete delete = new HttpDelete(
480
				LoadBalancer.getRegionUrl(loadBalancer.getRegion())
481
						+ Account.getAccount().getAccountId()
482
						+ "/loadbalancers/" + loadBalancer.getId()
483
						+ "/sessionpersistence");
438 484

  
439 485
		delete.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
440
		
486

  
441 487
		httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class);
442 488

  
443 489
		HttpBundle bundle = new HttpBundle();
444 490
		bundle.setCurlRequest(delete);
445 491

  
446
		try {			
492
		try {
447 493
			resp = httpclient.execute(delete);
448 494
			bundle.setHttpResponse(resp);
449 495
		} catch (ClientProtocolException e) {
......
458 504
			CloudServersException cse = new CloudServersException();
459 505
			cse.setMessage(e.getLocalizedMessage());
460 506
			throw cse;
461
		}	
507
		}
462 508
		return bundle;
463 509
	}
464 510

  

Also available in: Unified diff