Statistics
| Branch: | Revision:

root / src / com / rackspace / cloud / servers / api / client / ImageManager.java @ a597c658

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.apache.http.impl.client.DefaultHttpClient;
20
import org.xml.sax.InputSource;
21
import org.xml.sax.SAXException;
22
import org.xml.sax.XMLReader;
23

    
24
import android.content.Context;
25
import android.util.Log;
26

    
27
import com.rackspace.cloud.files.api.client.CustomHttpClient;
28
import com.rackspace.cloud.servers.api.client.parsers.ImagesXMLParser;
29

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

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