root / src / com / rackspace / cloud / servers / api / client / ImageManager.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.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()
|
40 |
+ "/images/detail.xml?now=cache_time2");
|
41 |
get.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
|
42 |
|
43 |
try {
|
44 |
HttpResponse resp = httpclient.execute(get); |
45 |
BasicResponseHandler responseHandler = new BasicResponseHandler();
|
46 |
String body = responseHandler.handleResponse(resp);
|
47 |
|
48 |
if (resp.getStatusLine().getStatusCode() == 200 |
49 |
|| resp.getStatusLine().getStatusCode() == 203) {
|
50 |
|
51 |
ImagesXMLParser imagesXMLParser = new ImagesXMLParser();
|
52 |
SAXParser saxParser = SAXParserFactory.newInstance() |
53 |
.newSAXParser(); |
54 |
XMLReader xmlReader = saxParser.getXMLReader(); |
55 |
xmlReader.setContentHandler(imagesXMLParser); |
56 |
xmlReader.parse(new InputSource(new StringReader(body))); |
57 |
images = imagesXMLParser.getImages(); |
58 |
} |
59 |
} catch (ClientProtocolException cpe) {
|
60 |
// we'll end up with an empty list; that's good enough
|
61 |
} catch (IOException e) { |
62 |
// we'll end up with an empty list; that's good enough
|
63 |
} catch (ParserConfigurationException e) { |
64 |
// we'll end up with an empty list; that's good enough
|
65 |
} catch (SAXException e) {
|
66 |
// we'll end up with an empty list; that's good enough
|
67 |
} catch (FactoryConfigurationError e) { |
68 |
// we'll end up with an empty list; that's good enough
|
69 |
} |
70 |
} |
71 |
|
72 |
return images;
|
73 |
} |
74 |
} |