Statistics
| Branch: | Revision:

root / src / com / rackspace / cloud / loadbalancer / api / client / ProtocolManager.java @ 403bb53b

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

    
78
}