root / src / com / rackspace / cloud / loadbalancer / api / client / ProtocolManager.java @ 9c74202f
History | View | Annotate | Download (3.2 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.android.Preferences; |
26 |
import com.rackspace.cloud.files.api.client.CustomHttpClient; |
27 |
import com.rackspace.cloud.loadbalancer.api.parsers.ProtocolsXMLParser; |
28 |
import com.rackspace.cloud.servers.api.client.Account; |
29 |
|
30 |
public class ProtocolManager extends EntityManager { |
31 |
|
32 |
public ArrayList<Protocol> 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/protocols.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/protocols.xml";
|
44 |
} |
45 |
ArrayList<Protocol> protocols = new ArrayList<Protocol>(); |
46 |
if(url!=null){ |
47 |
HttpGet get = new HttpGet(url);
|
48 |
|
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 |
|
57 |
if (resp.getStatusLine().getStatusCode() == 200) { |
58 |
ProtocolsXMLParser protocolsXMLParser = new ProtocolsXMLParser();
|
59 |
SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); |
60 |
XMLReader xmlReader = saxParser.getXMLReader(); |
61 |
xmlReader.setContentHandler(protocolsXMLParser); |
62 |
xmlReader.parse(new InputSource(new StringReader(body))); |
63 |
protocols = protocolsXMLParser.getProtocols(); |
64 |
} |
65 |
} catch (ClientProtocolException cpe) {
|
66 |
cpe.printStackTrace(); |
67 |
// we'll end up with an empty list; that's good enough
|
68 |
} catch (IOException e) { |
69 |
e.printStackTrace(); |
70 |
// we'll end up with an empty list; that's good enough
|
71 |
} catch (ParserConfigurationException e) { |
72 |
e.printStackTrace(); |
73 |
// we'll end up with an empty list; that's good enough
|
74 |
} catch (SAXException e) {
|
75 |
e.printStackTrace(); |
76 |
// we'll end up with an empty list; that's good enough
|
77 |
} catch (FactoryConfigurationError e) { |
78 |
e.printStackTrace(); |
79 |
// we'll end up with an empty list; that's good enough
|
80 |
} |
81 |
catch(IllegalStateException e){} |
82 |
} |
83 |
return protocols;
|
84 |
} |
85 |
|
86 |
} |