root / src / com / rackspace / cloud / loadbalancer / api / client / AlgorithmManager.java @ 1b82ddb3
History | View | Annotate | Download (2.5 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.util.ArrayList; |
9 |
|
10 |
import javax.xml.parsers.FactoryConfigurationError; |
11 |
import javax.xml.parsers.ParserConfigurationException; |
12 |
import javax.xml.parsers.SAXParser; |
13 |
import javax.xml.parsers.SAXParserFactory; |
14 |
|
15 |
import org.apache.http.HttpResponse; |
16 |
import org.apache.http.client.ClientProtocolException; |
17 |
import org.apache.http.client.methods.HttpGet; |
18 |
import org.apache.http.impl.client.BasicResponseHandler; |
19 |
import org.xml.sax.InputSource; |
20 |
import org.xml.sax.SAXException; |
21 |
import org.xml.sax.XMLReader; |
22 |
|
23 |
import android.content.Context; |
24 |
|
25 |
import com.rackspace.cloud.files.api.client.CustomHttpClient; |
26 |
import com.rackspace.cloud.loadbalancer.api.parsers.AlgorithmsXMLParser; |
27 |
import com.rackspace.cloud.servers.api.client.Account; |
28 |
|
29 |
public class AlgorithmManager extends EntityManager { |
30 |
|
31 |
public ArrayList<Algorithm> createList(Context context) { |
32 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
33 |
HttpGet get = new HttpGet(Account.getLoadBalancerDFWUrl() + Account.getAccount().getAccountId() + "/loadbalancers/algorithms.xml"); |
34 |
ArrayList<Algorithm> algorithms = new ArrayList<Algorithm>(); |
35 |
|
36 |
get.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
|
37 |
try {
|
38 |
HttpResponse resp = httpclient.execute(get); |
39 |
BasicResponseHandler responseHandler = new BasicResponseHandler();
|
40 |
String body = responseHandler.handleResponse(resp);
|
41 |
|
42 |
if (resp.getStatusLine().getStatusCode() == 200) { |
43 |
AlgorithmsXMLParser algorithmsXMLParser = new AlgorithmsXMLParser();
|
44 |
SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); |
45 |
XMLReader xmlReader = saxParser.getXMLReader(); |
46 |
xmlReader.setContentHandler(algorithmsXMLParser); |
47 |
xmlReader.parse(new InputSource(new StringReader(body))); |
48 |
algorithms = algorithmsXMLParser.getAlgorithms(); |
49 |
} |
50 |
} catch (ClientProtocolException cpe) {
|
51 |
cpe.printStackTrace(); |
52 |
// we'll end up with an empty list; that's good enough
|
53 |
} catch (IOException e) { |
54 |
e.printStackTrace(); |
55 |
// we'll end up with an empty list; that's good enough
|
56 |
} catch (ParserConfigurationException e) { |
57 |
e.printStackTrace(); |
58 |
// we'll end up with an empty list; that's good enough
|
59 |
} catch (SAXException e) {
|
60 |
e.printStackTrace(); |
61 |
// we'll end up with an empty list; that's good enough
|
62 |
} catch (FactoryConfigurationError e) { |
63 |
e.printStackTrace(); |
64 |
// we'll end up with an empty list; that's good enough
|
65 |
} |
66 |
|
67 |
return algorithms;
|
68 |
} |
69 |
|
70 |
} |