Statistics
| Branch: | Revision:

root / src / com / rackspace / cloud / servers / api / client / FlavorManager.java @ 038ac9a4

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()
40
                                        + "/flavors/detail.xml?now=cache_time2");
41
                        get.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
42
                        try {
43
                                HttpResponse resp = httpclient.execute(get);
44
                                BasicResponseHandler responseHandler = new BasicResponseHandler();
45
                                String body = responseHandler.handleResponse(resp);
46

    
47
                                if (resp.getStatusLine().getStatusCode() == 200
48
                                                || resp.getStatusLine().getStatusCode() == 203) {
49
                                        FlavorsXMLParser flavorsXMLParser = new FlavorsXMLParser();
50
                                        SAXParser saxParser = SAXParserFactory.newInstance()
51
                                                        .newSAXParser();
52
                                        XMLReader xmlReader = saxParser.getXMLReader();
53
                                        xmlReader.setContentHandler(flavorsXMLParser);
54
                                        xmlReader.parse(new InputSource(new StringReader(body)));
55
                                        flavors = flavorsXMLParser.getFlavors();
56
                                }
57
                        } catch (ClientProtocolException cpe) {
58
                                // we'll end up with an empty list; that's good enough
59
                        } catch (IOException e) {
60
                                // we'll end up with an empty list; that's good enough
61
                        } catch (ParserConfigurationException e) {
62
                                // we'll end up with an empty list; that's good enough
63
                        } catch (SAXException e) {
64
                                // we'll end up with an empty list; that's good enough
65
                        } catch (FactoryConfigurationError e) {
66
                                // we'll end up with an empty list; that's good enough
67
                        }
68
                }
69
                return flavors;
70
        }
71

    
72
}