Statistics
| Branch: | Revision:

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

    
79
}