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