root / src / com / rackspace / cloud / loadbalancer / api / client / LoadBalancerManager.java @ 1b82ddb3
History | View | Annotate | Download (11.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 |
|
40 |
public class LoadBalancerManager extends EntityManager { |
41 |
private Context context; |
42 |
|
43 |
public LoadBalancerManager(Context context) { |
44 |
this.context = context;
|
45 |
} |
46 |
|
47 |
public LoadBalancer getLoadBalancerById(long id) throws LoadBalancersException { |
48 |
Log.d("info", "the id is " + id); |
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 |
throw lbe;
|
67 |
} |
68 |
} |
69 |
return loadBalancer;
|
70 |
} |
71 |
|
72 |
private LoadBalancer getLoadBalancerById(long id, String url) throws LoadBalancersException { |
73 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
74 |
HttpGet get = new HttpGet(url + Account.getAccount().getAccountId() + "/loadbalancers/" + id); |
75 |
LoadBalancer loadBalancer = new LoadBalancer();
|
76 |
|
77 |
get.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
|
78 |
get.addHeader("Accept", "application/xml"); |
79 |
|
80 |
try {
|
81 |
HttpResponse resp = httpclient.execute(get); |
82 |
BasicResponseHandler responseHandler = new BasicResponseHandler();
|
83 |
String body = responseHandler.handleResponse(resp);
|
84 |
|
85 |
Log.d("info", "the xml body is " + body); |
86 |
|
87 |
if (resp.getStatusLine().getStatusCode() == 200 || resp.getStatusLine().getStatusCode() == 202) { |
88 |
LoadBalancersXmlParser loadBalancersXMLParser = new LoadBalancersXmlParser();
|
89 |
SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); |
90 |
XMLReader xmlReader = saxParser.getXMLReader(); |
91 |
xmlReader.setContentHandler(loadBalancersXMLParser); |
92 |
xmlReader.parse(new InputSource(new StringReader(body))); |
93 |
loadBalancer = loadBalancersXMLParser.getLoadBalancer(); |
94 |
} else {
|
95 |
CloudLoadBalancersFaultXMLParser parser = new CloudLoadBalancersFaultXMLParser();
|
96 |
SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); |
97 |
XMLReader xmlReader = saxParser.getXMLReader(); |
98 |
xmlReader.setContentHandler(parser); |
99 |
xmlReader.parse(new InputSource(new StringReader(body))); |
100 |
LoadBalancersException cse = parser.getException(); |
101 |
throw cse;
|
102 |
} |
103 |
} catch (ClientProtocolException e) {
|
104 |
LoadBalancersException cse = new LoadBalancersException();
|
105 |
cse.setMessage(e.getLocalizedMessage()); |
106 |
throw cse;
|
107 |
} catch (IOException e) { |
108 |
LoadBalancersException cse = new LoadBalancersException();
|
109 |
cse.setMessage(e.getLocalizedMessage()); |
110 |
throw cse;
|
111 |
} catch (ParserConfigurationException e) { |
112 |
LoadBalancersException cse = new LoadBalancersException();
|
113 |
cse.setMessage(e.getLocalizedMessage()); |
114 |
throw cse;
|
115 |
} catch (SAXException e) {
|
116 |
LoadBalancersException cse = new LoadBalancersException();
|
117 |
cse.setMessage(e.getLocalizedMessage()); |
118 |
throw cse;
|
119 |
} catch (FactoryConfigurationError e) { |
120 |
LoadBalancersException cse = new LoadBalancersException();
|
121 |
cse.setMessage(e.getLocalizedMessage()); |
122 |
throw cse;
|
123 |
} |
124 |
return loadBalancer;
|
125 |
} |
126 |
|
127 |
public ArrayList<LoadBalancer> createList() throws LoadBalancersException{ |
128 |
ArrayList<LoadBalancer> loadBalancers = createSublist(Account.getLoadBalancerORDUrl());
|
129 |
for(LoadBalancer loadBalancer: loadBalancers){
|
130 |
loadBalancer.setRegion("ORD");
|
131 |
} |
132 |
ArrayList<LoadBalancer> DFWloadBalancers = createSublist(Account.getLoadBalancerDFWUrl());
|
133 |
for(LoadBalancer loadBalancer: DFWloadBalancers){
|
134 |
loadBalancer.setRegion("DFW");
|
135 |
} |
136 |
loadBalancers.addAll(DFWloadBalancers); |
137 |
return loadBalancers;
|
138 |
} |
139 |
|
140 |
public ArrayList<LoadBalancer> createSublist(String regionUrl) throws LoadBalancersException { |
141 |
//TODO:grab from ord and combine list
|
142 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
143 |
//TODO:check for uk or us
|
144 |
HttpGet get = new HttpGet(regionUrl + Account.getAccount().getAccountId() + "/loadbalancers"); |
145 |
ArrayList<LoadBalancer> loadBalancers = new ArrayList<LoadBalancer>(); |
146 |
|
147 |
get.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
|
148 |
get.addHeader("Accept", "application/xml"); |
149 |
|
150 |
try {
|
151 |
HttpResponse resp = httpclient.execute(get); |
152 |
BasicResponseHandler responseHandler = new BasicResponseHandler();
|
153 |
String body = responseHandler.handleResponse(resp);
|
154 |
if (resp.getStatusLine().getStatusCode() == 200 || resp.getStatusLine().getStatusCode() == 202) { |
155 |
LoadBalancersXmlParser loadBalancersXMLParser = new LoadBalancersXmlParser();
|
156 |
SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); |
157 |
XMLReader xmlReader = saxParser.getXMLReader(); |
158 |
xmlReader.setContentHandler(loadBalancersXMLParser); |
159 |
xmlReader.parse(new InputSource(new StringReader(body))); |
160 |
loadBalancers = loadBalancersXMLParser.getLoadBalancers(); |
161 |
} else {
|
162 |
CloudLoadBalancersFaultXMLParser parser = new CloudLoadBalancersFaultXMLParser();
|
163 |
SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); |
164 |
XMLReader xmlReader = saxParser.getXMLReader(); |
165 |
xmlReader.setContentHandler(parser); |
166 |
xmlReader.parse(new InputSource(new StringReader(body))); |
167 |
LoadBalancersException cse = parser.getException(); |
168 |
throw cse;
|
169 |
} |
170 |
} catch (ClientProtocolException e) {
|
171 |
LoadBalancersException cse = new LoadBalancersException();
|
172 |
cse.setMessage(e.getLocalizedMessage()); |
173 |
throw cse;
|
174 |
} catch (IOException e) { |
175 |
LoadBalancersException cse = new LoadBalancersException();
|
176 |
cse.setMessage(e.getLocalizedMessage()); |
177 |
throw cse;
|
178 |
} catch (ParserConfigurationException e) { |
179 |
LoadBalancersException cse = new LoadBalancersException();
|
180 |
cse.setMessage(e.getLocalizedMessage()); |
181 |
throw cse;
|
182 |
} catch (SAXException e) {
|
183 |
LoadBalancersException cse = new LoadBalancersException();
|
184 |
cse.setMessage(e.getLocalizedMessage()); |
185 |
throw cse;
|
186 |
} catch (FactoryConfigurationError e) { |
187 |
LoadBalancersException cse = new LoadBalancersException();
|
188 |
cse.setMessage(e.getLocalizedMessage()); |
189 |
throw cse;
|
190 |
} |
191 |
return loadBalancers;
|
192 |
} |
193 |
|
194 |
public HttpBundle create(LoadBalancer entity, String regionUrl) throws CloudServersException { |
195 |
HttpResponse resp = null;
|
196 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
197 |
|
198 |
HttpPost post = new HttpPost(regionUrl + Account.getAccount().getAccountId() + "/loadbalancers"); |
199 |
post.addHeader("Content-Type", "application/xml"); |
200 |
|
201 |
StringEntity tmp = null;
|
202 |
try {
|
203 |
tmp = new StringEntity(entity.toDetailedXML());
|
204 |
} catch (UnsupportedEncodingException e) { |
205 |
CloudServersException cse = new CloudServersException();
|
206 |
cse.setMessage(e.getLocalizedMessage()); |
207 |
throw cse;
|
208 |
} |
209 |
|
210 |
post.setEntity(tmp); |
211 |
|
212 |
post.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
|
213 |
httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class); |
214 |
|
215 |
HttpBundle bundle = new HttpBundle();
|
216 |
bundle.setCurlRequest(post); |
217 |
try {
|
218 |
resp = httpclient.execute(post); |
219 |
bundle.setHttpResponse(resp); |
220 |
} catch (ClientProtocolException e) {
|
221 |
CloudServersException cse = new CloudServersException();
|
222 |
cse.setMessage(e.getLocalizedMessage()); |
223 |
throw cse;
|
224 |
} catch (IOException e) { |
225 |
CloudServersException cse = new CloudServersException();
|
226 |
cse.setMessage(e.getLocalizedMessage()); |
227 |
throw cse;
|
228 |
} catch (FactoryConfigurationError e) { |
229 |
CloudServersException cse = new CloudServersException();
|
230 |
cse.setMessage(e.getLocalizedMessage()); |
231 |
throw cse;
|
232 |
} |
233 |
return bundle;
|
234 |
} |
235 |
|
236 |
public HttpBundle delete(LoadBalancer loadBalancer) throws CloudServersException { |
237 |
HttpResponse resp = null;
|
238 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
239 |
|
240 |
HttpDelete delete = new HttpDelete(LoadBalancer.getRegionUrl(loadBalancer.getRegion()) + Account.getAccount().getAccountId()
|
241 |
+ "/loadbalancers/" + loadBalancer.getId());
|
242 |
delete.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
|
243 |
delete.addHeader("Content-Type", "application/xml"); |
244 |
httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class); |
245 |
|
246 |
HttpBundle bundle = new HttpBundle();
|
247 |
bundle.setCurlRequest(delete); |
248 |
|
249 |
try {
|
250 |
resp = httpclient.execute(delete); |
251 |
bundle.setHttpResponse(resp); |
252 |
} catch (ClientProtocolException e) {
|
253 |
CloudServersException cse = new CloudServersException();
|
254 |
cse.setMessage(e.getLocalizedMessage()); |
255 |
throw cse;
|
256 |
} catch (IOException e) { |
257 |
CloudServersException cse = new CloudServersException();
|
258 |
cse.setMessage(e.getLocalizedMessage()); |
259 |
throw cse;
|
260 |
} catch (FactoryConfigurationError e) { |
261 |
CloudServersException cse = new CloudServersException();
|
262 |
cse.setMessage(e.getLocalizedMessage()); |
263 |
throw cse;
|
264 |
} |
265 |
return bundle;
|
266 |
} |
267 |
|
268 |
public HttpBundle update(LoadBalancer loadBalancer, String name, String algorithm, String protocol, String port) throws CloudServersException { |
269 |
HttpResponse resp = null;
|
270 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
271 |
|
272 |
HttpPut put = new HttpPut(LoadBalancer.getRegionUrl(loadBalancer.getRegion()) + Account.getAccount().getAccountId() + "/loadbalancers/" + loadBalancer.getId()); |
273 |
|
274 |
put.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
|
275 |
put.addHeader("Content-Type", "application/xml"); |
276 |
|
277 |
String xml = "<loadBalancer xmlns=\"http://docs.openstack.org/loadbalancers/api/v1.0\" " + |
278 |
"name=\"" + name + "\" " + |
279 |
"algorithm=\"" + algorithm.toUpperCase() + "\" " + |
280 |
"protocol=\"" + protocol.toUpperCase() + "\" " + |
281 |
"port=\"" + port + "\" />"; |
282 |
|
283 |
StringEntity tmp = null;
|
284 |
try {
|
285 |
tmp = new StringEntity(xml);
|
286 |
} catch (UnsupportedEncodingException e) { |
287 |
CloudServersException cse = new CloudServersException();
|
288 |
cse.setMessage(e.getLocalizedMessage()); |
289 |
throw cse;
|
290 |
} |
291 |
|
292 |
put.setEntity(tmp); |
293 |
httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class); |
294 |
|
295 |
HttpBundle bundle = new HttpBundle();
|
296 |
bundle.setCurlRequest(put); |
297 |
|
298 |
try {
|
299 |
resp = httpclient.execute(put); |
300 |
bundle.setHttpResponse(resp); |
301 |
} catch (ClientProtocolException e) {
|
302 |
CloudServersException cse = new CloudServersException();
|
303 |
cse.setMessage(e.getLocalizedMessage()); |
304 |
throw cse;
|
305 |
} catch (IOException e) { |
306 |
CloudServersException cse = new CloudServersException();
|
307 |
cse.setMessage(e.getLocalizedMessage()); |
308 |
throw cse;
|
309 |
} catch (FactoryConfigurationError e) { |
310 |
CloudServersException cse = new CloudServersException();
|
311 |
cse.setMessage(e.getLocalizedMessage()); |
312 |
throw cse;
|
313 |
} |
314 |
return bundle;
|
315 |
} |
316 |
|
317 |
|
318 |
} |