root / src / com / rackspace / cloud / loadbalancer / api / client / AlgorithmManager.java @ f3633a16
History | View | Annotate | Download (3.1 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 |
import com.rackspacecloud.android.Preferences; |
29 |
|
30 |
public class AlgorithmManager extends EntityManager { |
31 |
|
32 |
public ArrayList<Algorithm> createList(Context context) { |
33 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
34 |
|
35 |
String url = ""; |
36 |
String authServer = Account.getAccount().getAuthServer();
|
37 |
if(authServer == null){ |
38 |
authServer = Account.getAccount().getAuthServerV2(); |
39 |
} |
40 |
if(authServer.equals(Preferences.COUNTRY_US_AUTH_SERVER) || authServer.equals(Preferences.COUNTRY_US_AUTH_SERVER_V2)){ |
41 |
url = Account.getLoadBalancerDFWUrl() + Account.getAccount().getAccountId() + "/loadbalancers/algorithms.xml";
|
42 |
} else if(authServer.equals(Preferences.COUNTRY_UK_AUTH_SERVER) || authServer.equals(Preferences.COUNTRY_UK_AUTH_SERVER_V2)){ |
43 |
url = Account.getLoadBalancerLONUrl() + Account.getAccount().getAccountId() + "/loadbalancers/algorithms.xml";
|
44 |
} |
45 |
HttpGet get = new HttpGet(url);
|
46 |
|
47 |
ArrayList<Algorithm> algorithms = new ArrayList<Algorithm>(); |
48 |
|
49 |
get.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
|
50 |
try {
|
51 |
HttpResponse resp = httpclient.execute(get); |
52 |
BasicResponseHandler responseHandler = new BasicResponseHandler();
|
53 |
String body = responseHandler.handleResponse(resp);
|
54 |
|
55 |
if (resp.getStatusLine().getStatusCode() == 200) { |
56 |
AlgorithmsXMLParser algorithmsXMLParser = new AlgorithmsXMLParser();
|
57 |
SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); |
58 |
XMLReader xmlReader = saxParser.getXMLReader(); |
59 |
xmlReader.setContentHandler(algorithmsXMLParser); |
60 |
xmlReader.parse(new InputSource(new StringReader(body))); |
61 |
algorithms = algorithmsXMLParser.getAlgorithms(); |
62 |
} |
63 |
} catch (ClientProtocolException cpe) {
|
64 |
cpe.printStackTrace(); |
65 |
// we'll end up with an empty list; that's good enough
|
66 |
} catch (IOException e) { |
67 |
e.printStackTrace(); |
68 |
// we'll end up with an empty list; that's good enough
|
69 |
} catch (ParserConfigurationException e) { |
70 |
e.printStackTrace(); |
71 |
// we'll end up with an empty list; that's good enough
|
72 |
} catch (SAXException e) {
|
73 |
e.printStackTrace(); |
74 |
// we'll end up with an empty list; that's good enough
|
75 |
} catch (FactoryConfigurationError e) { |
76 |
e.printStackTrace(); |
77 |
// we'll end up with an empty list; that's good enough
|
78 |
} |
79 |
|
80 |
return algorithms;
|
81 |
} |
82 |
|
83 |
} |