root / src / com / rackspace / cloud / loadbalancer / api / client / ProtocolManager.java @ aeff0a62
History | View | Annotate | Download (2.6 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 |
import android.util.Log; |
25 |
|
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 |
HttpGet get = new HttpGet(Account.getAccount().getLoadBalancerDFWUrl() + Account.getAccount().getAccountId() + "/loadbalancers/protocols.xml"); |
35 |
ArrayList<Protocol> protocols = new ArrayList<Protocol>(); |
36 |
|
37 |
get.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
|
38 |
try {
|
39 |
HttpResponse resp = httpclient.execute(get); |
40 |
BasicResponseHandler responseHandler = new BasicResponseHandler();
|
41 |
String body = responseHandler.handleResponse(resp);
|
42 |
Log.d("info", body);
|
43 |
|
44 |
if (resp.getStatusLine().getStatusCode() == 200) { |
45 |
ProtocolsXMLParser protocolsXMLParser = new ProtocolsXMLParser();
|
46 |
SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); |
47 |
XMLReader xmlReader = saxParser.getXMLReader(); |
48 |
xmlReader.setContentHandler(protocolsXMLParser); |
49 |
xmlReader.parse(new InputSource(new StringReader(body))); |
50 |
protocols = protocolsXMLParser.getProtocols(); |
51 |
} |
52 |
} catch (ClientProtocolException cpe) {
|
53 |
cpe.printStackTrace(); |
54 |
// we'll end up with an empty list; that's good enough
|
55 |
} catch (IOException e) { |
56 |
e.printStackTrace(); |
57 |
// we'll end up with an empty list; that's good enough
|
58 |
} catch (ParserConfigurationException e) { |
59 |
e.printStackTrace(); |
60 |
// we'll end up with an empty list; that's good enough
|
61 |
} catch (SAXException e) {
|
62 |
e.printStackTrace(); |
63 |
// we'll end up with an empty list; that's good enough
|
64 |
} catch (FactoryConfigurationError e) { |
65 |
e.printStackTrace(); |
66 |
// we'll end up with an empty list; that's good enough
|
67 |
} |
68 |
|
69 |
return protocols;
|
70 |
} |
71 |
|
72 |
} |