Statistics
| Branch: | Revision:

root / src / com / rackspace / cloud / servers / api / client / FlavorManager.java @ 6c0d81f1

History | View | Annotate | Download (2.5 kB)

1
/**
2
 * 
3
 */
4
package com.rackspace.cloud.servers.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.servers.api.client.parsers.FlavorsXMLParser;
27

    
28
/**
29
 * @author Mike Mayo - mike.mayo@rackspace.com - twitter.com/greenisus
30
 *
31
 */
32
public class FlavorManager extends EntityManager {
33

    
34
        public ArrayList<Flavor> createList(boolean detail, Context context) {
35
                
36
                ArrayList<Flavor> flavors = new ArrayList<Flavor>();
37
                if(Account.getAccount().getServerUrl()!=null){
38
                        CustomHttpClient httpclient = new CustomHttpClient(context);
39
                        HttpGet get = new HttpGet(Account.getAccount().getServerUrl() + "/flavors/detail.xml?now=cache_time2");
40
                        get.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
41
                        try {                        
42
                                HttpResponse resp = httpclient.execute(get);
43
                            BasicResponseHandler responseHandler = new BasicResponseHandler();
44
                            String body = responseHandler.handleResponse(resp);
45
                            
46
                            if (resp.getStatusLine().getStatusCode() == 200 || resp.getStatusLine().getStatusCode() == 203) {                            
47
                                    FlavorsXMLParser flavorsXMLParser = new FlavorsXMLParser();
48
                                    SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
49
                                    XMLReader xmlReader = saxParser.getXMLReader();
50
                                    xmlReader.setContentHandler(flavorsXMLParser);
51
                                    xmlReader.parse(new InputSource(new StringReader(body)));        
52
                                    flavors = flavorsXMLParser.getFlavors();        
53
                            }
54
                        } catch (ClientProtocolException cpe) {
55
                                // we'll end up with an empty list; that's good enough
56
                        } catch (IOException e) {
57
                                // we'll end up with an empty list; that's good enough
58
                        } catch (ParserConfigurationException e) {
59
                                // we'll end up with an empty list; that's good enough
60
                        } catch (SAXException e) {
61
                                // we'll end up with an empty list; that's good enough
62
                        } catch (FactoryConfigurationError e) {
63
                                // we'll end up with an empty list; that's good enough
64
                        }
65
                }
66
                return flavors;
67
        }
68

    
69
}