root / src / com / rackspace / cloud / loadbalancer / api / client / NodeManager.java @ 1b82ddb3
History | View | Annotate | Download (8.9 kB)
1 |
package com.rackspace.cloud.loadbalancer.api.client; |
---|---|
2 |
|
3 |
import java.io.IOException; |
4 |
import java.io.StringReader; |
5 |
import java.io.UnsupportedEncodingException; |
6 |
import java.util.ArrayList; |
7 |
|
8 |
import javax.xml.parsers.FactoryConfigurationError; |
9 |
import javax.xml.parsers.ParserConfigurationException; |
10 |
import javax.xml.parsers.SAXParser; |
11 |
import javax.xml.parsers.SAXParserFactory; |
12 |
|
13 |
import org.apache.http.HttpResponse; |
14 |
import org.apache.http.client.ClientProtocolException; |
15 |
import org.apache.http.client.methods.HttpDelete; |
16 |
import org.apache.http.client.methods.HttpGet; |
17 |
import org.apache.http.client.methods.HttpPost; |
18 |
import org.apache.http.client.methods.HttpPut; |
19 |
import org.apache.http.entity.StringEntity; |
20 |
import org.apache.http.impl.client.BasicResponseHandler; |
21 |
import org.apache.http.protocol.RequestExpectContinue; |
22 |
import org.xml.sax.InputSource; |
23 |
import org.xml.sax.SAXException; |
24 |
import org.xml.sax.XMLReader; |
25 |
|
26 |
import android.content.Context; |
27 |
|
28 |
import com.rackspace.cloud.files.api.client.CustomHttpClient; |
29 |
import com.rackspace.cloud.loadbalancer.api.client.http.LoadBalancersException; |
30 |
import com.rackspace.cloud.loadbalancer.api.parsers.CloudLoadBalancersFaultXMLParser; |
31 |
import com.rackspace.cloud.loadbalancer.api.parsers.NodesXMLParser; |
32 |
import com.rackspace.cloud.servers.api.client.Account; |
33 |
import com.rackspace.cloud.servers.api.client.CloudServersException; |
34 |
import com.rackspace.cloud.servers.api.client.http.HttpBundle; |
35 |
|
36 |
public class NodeManager { |
37 |
|
38 |
private Context context; |
39 |
|
40 |
public NodeManager(Context context) { |
41 |
this.context = context;
|
42 |
} |
43 |
|
44 |
public ArrayList<Node> createList(LoadBalancer loadBalancer) throws LoadBalancersException { |
45 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
46 |
HttpGet get = new HttpGet(LoadBalancer.getRegionUrl(loadBalancer.getRegion()) + Account.getAccount().getAccountId()
|
47 |
+ "/loadbalancers/" + loadBalancer.getId() + "/nodes.xml"); |
48 |
ArrayList<Node> nodes = new ArrayList<Node>(); |
49 |
|
50 |
get.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
|
51 |
|
52 |
try {
|
53 |
HttpResponse resp = httpclient.execute(get); |
54 |
BasicResponseHandler responseHandler = new BasicResponseHandler();
|
55 |
String body = responseHandler.handleResponse(resp);
|
56 |
if (resp.getStatusLine().getStatusCode() == 200 || resp.getStatusLine().getStatusCode() == 202) { |
57 |
NodesXMLParser nodesXMLParser = new NodesXMLParser();
|
58 |
SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); |
59 |
XMLReader xmlReader = saxParser.getXMLReader(); |
60 |
xmlReader.setContentHandler(nodesXMLParser); |
61 |
xmlReader.parse(new InputSource(new StringReader(body))); |
62 |
nodes = nodesXMLParser.getNodes(); |
63 |
} else {
|
64 |
CloudLoadBalancersFaultXMLParser parser = new CloudLoadBalancersFaultXMLParser();
|
65 |
SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); |
66 |
XMLReader xmlReader = saxParser.getXMLReader(); |
67 |
xmlReader.setContentHandler(parser); |
68 |
xmlReader.parse(new InputSource(new StringReader(body))); |
69 |
LoadBalancersException cse = parser.getException(); |
70 |
throw cse;
|
71 |
} |
72 |
} catch (ClientProtocolException e) {
|
73 |
LoadBalancersException cse = new LoadBalancersException();
|
74 |
cse.setMessage(e.getLocalizedMessage()); |
75 |
throw cse;
|
76 |
} catch (IOException e) { |
77 |
LoadBalancersException cse = new LoadBalancersException();
|
78 |
cse.setMessage(e.getLocalizedMessage()); |
79 |
throw cse;
|
80 |
} catch (ParserConfigurationException e) { |
81 |
LoadBalancersException cse = new LoadBalancersException();
|
82 |
cse.setMessage(e.getLocalizedMessage()); |
83 |
throw cse;
|
84 |
} catch (SAXException e) {
|
85 |
LoadBalancersException cse = new LoadBalancersException();
|
86 |
cse.setMessage(e.getLocalizedMessage()); |
87 |
throw cse;
|
88 |
} catch (FactoryConfigurationError e) { |
89 |
LoadBalancersException cse = new LoadBalancersException();
|
90 |
cse.setMessage(e.getLocalizedMessage()); |
91 |
throw cse;
|
92 |
} |
93 |
return nodes;
|
94 |
} |
95 |
|
96 |
public HttpBundle add(LoadBalancer loadBalancer, ArrayList<Node> nodes) throws CloudServersException { |
97 |
HttpResponse resp = null;
|
98 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
99 |
|
100 |
HttpPost post = new HttpPost(LoadBalancer.getRegionUrl(loadBalancer.getRegion()) + Account.getAccount().getAccountId() + "/loadbalancers/" + loadBalancer.getId() + "/nodes"); |
101 |
|
102 |
post.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
|
103 |
post.addHeader("Content-Type", "application/xml"); |
104 |
|
105 |
String xml = "<nodes xmlns=\"http://docs.openstack.org/loadbalancers/api/v1.0\"> "; |
106 |
for(int i = 0;i < nodes.size(); i++){ |
107 |
Node node = nodes.get(i); |
108 |
|
109 |
/*
|
110 |
* if the algorithm is not weighted then weight for the node will
|
111 |
* be null or ""
|
112 |
*/
|
113 |
if(node.getWeight() == null || node.getWeight().equals("")){ |
114 |
xml += "<node address=\"" + node.getAddress() + "\" port=\"" + node.getPort() + "\" condition=\"" + node.getCondition() + "\"/>"; |
115 |
} |
116 |
else{
|
117 |
xml += "<node address=\"" + node.getAddress() + "\" port=\"" + node.getPort() + "\" condition=\"" + node.getCondition() + "\" weight=\"" + node.getWeight() + "\"/>"; |
118 |
} |
119 |
} |
120 |
xml += " </nodes>";
|
121 |
|
122 |
StringEntity tmp = null;
|
123 |
try {
|
124 |
tmp = new StringEntity(xml);
|
125 |
} catch (UnsupportedEncodingException e) { |
126 |
CloudServersException cse = new CloudServersException();
|
127 |
cse.setMessage(e.getLocalizedMessage()); |
128 |
throw cse;
|
129 |
} |
130 |
|
131 |
post.setEntity(tmp); |
132 |
httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class); |
133 |
|
134 |
HttpBundle bundle = new HttpBundle();
|
135 |
bundle.setCurlRequest(post); |
136 |
|
137 |
try {
|
138 |
resp = httpclient.execute(post); |
139 |
bundle.setHttpResponse(resp); |
140 |
} catch (ClientProtocolException e) {
|
141 |
CloudServersException cse = new CloudServersException();
|
142 |
cse.setMessage(e.getLocalizedMessage()); |
143 |
throw cse;
|
144 |
} catch (IOException e) { |
145 |
CloudServersException cse = new CloudServersException();
|
146 |
cse.setMessage(e.getLocalizedMessage()); |
147 |
throw cse;
|
148 |
} catch (FactoryConfigurationError e) { |
149 |
CloudServersException cse = new CloudServersException();
|
150 |
cse.setMessage(e.getLocalizedMessage()); |
151 |
throw cse;
|
152 |
} |
153 |
return bundle;
|
154 |
} |
155 |
|
156 |
public HttpBundle update(LoadBalancer loadBalancer, Node node, String condition, String weight) throws CloudServersException { |
157 |
HttpResponse resp = null;
|
158 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
159 |
|
160 |
HttpPut put = new HttpPut(LoadBalancer.getRegionUrl(loadBalancer.getRegion()) + Account.getAccount().getAccountId() + "/loadbalancers/" + loadBalancer.getId() + "/nodes/" + node.getId()); |
161 |
|
162 |
put.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
|
163 |
put.addHeader("Content-Type", "application/xml"); |
164 |
|
165 |
String xml;
|
166 |
//different request body if the nodes have weight
|
167 |
if(weight == null || weight.equals("")){ |
168 |
xml = "<node xmlns=\"http://docs.openstack.org/loadbalancers/api/v1.0\" condition=\"" + condition.toUpperCase() + "\"/>"; |
169 |
} |
170 |
else{
|
171 |
xml = "<node xmlns=\"http://docs.openstack.org/loadbalancers/api/v1.0\" condition=\"" + condition.toUpperCase() + "\" weight=\"" + weight + "\"" + "/>"; |
172 |
} |
173 |
|
174 |
StringEntity tmp = null;
|
175 |
try {
|
176 |
tmp = new StringEntity(xml);
|
177 |
} catch (UnsupportedEncodingException e) { |
178 |
CloudServersException cse = new CloudServersException();
|
179 |
cse.setMessage(e.getLocalizedMessage()); |
180 |
throw cse;
|
181 |
} |
182 |
|
183 |
put.setEntity(tmp); |
184 |
httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class); |
185 |
|
186 |
HttpBundle bundle = new HttpBundle();
|
187 |
bundle.setCurlRequest(put); |
188 |
|
189 |
try {
|
190 |
resp = httpclient.execute(put); |
191 |
bundle.setHttpResponse(resp); |
192 |
} catch (ClientProtocolException e) {
|
193 |
CloudServersException cse = new CloudServersException();
|
194 |
cse.setMessage(e.getLocalizedMessage()); |
195 |
throw cse;
|
196 |
} catch (IOException e) { |
197 |
CloudServersException cse = new CloudServersException();
|
198 |
cse.setMessage(e.getLocalizedMessage()); |
199 |
throw cse;
|
200 |
} catch (FactoryConfigurationError e) { |
201 |
CloudServersException cse = new CloudServersException();
|
202 |
cse.setMessage(e.getLocalizedMessage()); |
203 |
throw cse;
|
204 |
} |
205 |
return bundle;
|
206 |
} |
207 |
|
208 |
public HttpBundle remove(LoadBalancer loadBalancer, Node node) throws CloudServersException { |
209 |
HttpResponse resp = null;
|
210 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
211 |
|
212 |
HttpDelete delete = new HttpDelete(LoadBalancer.getRegionUrl(loadBalancer.getRegion()) + Account.getAccount().getAccountId() + "/loadbalancers/" + loadBalancer.getId() + "/nodes/" + node.getId()); |
213 |
|
214 |
delete.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
|
215 |
|
216 |
httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class); |
217 |
|
218 |
HttpBundle bundle = new HttpBundle();
|
219 |
bundle.setCurlRequest(delete); |
220 |
|
221 |
try {
|
222 |
resp = httpclient.execute(delete); |
223 |
bundle.setHttpResponse(resp); |
224 |
} catch (ClientProtocolException e) {
|
225 |
CloudServersException cse = new CloudServersException();
|
226 |
cse.setMessage(e.getLocalizedMessage()); |
227 |
throw cse;
|
228 |
} catch (IOException e) { |
229 |
CloudServersException cse = new CloudServersException();
|
230 |
cse.setMessage(e.getLocalizedMessage()); |
231 |
throw cse;
|
232 |
} catch (FactoryConfigurationError e) { |
233 |
CloudServersException cse = new CloudServersException();
|
234 |
cse.setMessage(e.getLocalizedMessage()); |
235 |
throw cse;
|
236 |
} |
237 |
return bundle;
|
238 |
} |
239 |
|
240 |
} |