Statistics
| Branch: | Revision:

root / src / com / rackspace / cloud / loadbalancer / api / client / ProtocolManager.java @ 6ecf99bb

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
                    
43
                    if (resp.getStatusLine().getStatusCode() == 200) {                            
44
                            ProtocolsXMLParser protocolsXMLParser = new ProtocolsXMLParser();
45
                            SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
46
                            XMLReader xmlReader = saxParser.getXMLReader();
47
                            xmlReader.setContentHandler(protocolsXMLParser);
48
                            xmlReader.parse(new InputSource(new StringReader(body)));        
49
                            protocols = protocolsXMLParser.getProtocols();        
50
                    }
51
                } catch (ClientProtocolException cpe) {
52
                        cpe.printStackTrace();
53
                        // we'll end up with an empty list; that's good enough
54
                } catch (IOException e) {
55
                        e.printStackTrace();
56
                        // we'll end up with an empty list; that's good enough
57
                } catch (ParserConfigurationException e) {
58
                        e.printStackTrace();
59
                        // we'll end up with an empty list; that's good enough
60
                } catch (SAXException e) {
61
                        e.printStackTrace();
62
                        // we'll end up with an empty list; that's good enough
63
                } catch (FactoryConfigurationError e) {
64
                        e.printStackTrace();
65
                        // we'll end up with an empty list; that's good enough
66
                }
67
                
68
                return protocols;
69
        }
70

    
71
}