Statistics
| Branch: | Revision:

root / src / com / rackspace / cloud / servers / api / client / ImageManager.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.ImagesXMLParser;
27

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

    
34
        public ArrayList<Image> createList(boolean detail, Context context) {
35
                
36
                ArrayList<Image> images = new ArrayList<Image>();
37
                if(Account.getAccount().getServerUrl()!=null){
38
                        CustomHttpClient httpclient = new CustomHttpClient(context);
39
                        HttpGet get = new HttpGet(Account.getAccount().getServerUrl() + "/images/detail.xml?now=cache_time2");
40
                        get.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
41
        
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 || resp.getStatusLine().getStatusCode() == 203) {                            
48
                                    
49
                                    ImagesXMLParser imagesXMLParser = new ImagesXMLParser();
50
                                    SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
51
                                    XMLReader xmlReader = saxParser.getXMLReader();
52
                                    xmlReader.setContentHandler(imagesXMLParser);
53
                                    xmlReader.parse(new InputSource(new StringReader(body)));                            
54
                                    images = imagesXMLParser.getImages();                
55
                            } 
56
                        } catch (ClientProtocolException cpe) {
57
                                // we'll end up with an empty list; that's good enough
58
                        } catch (IOException e) {
59
                                // we'll end up with an empty list; that's good enough
60
                        } catch (ParserConfigurationException e) {
61
                                // we'll end up with an empty list; that's good enough
62
                        } catch (SAXException e) {
63
                                // we'll end up with an empty list; that's good enough
64
                        } catch (FactoryConfigurationError e) {
65
                                // we'll end up with an empty list; that's good enough
66
                        }
67
                }
68
                
69
                return images;
70
        }
71
}