root / src / com / rackspace / cloud / servers / api / client / EntityManager.java @ 35e7942d
History | View | Annotate | Download (6.3 kB)
1 |
/**
|
---|---|
2 |
*
|
3 |
*/
|
4 |
package com.rackspace.cloud.servers.api.client; |
5 |
|
6 |
import java.io.BufferedReader; |
7 |
import java.io.IOException; |
8 |
import java.io.InputStream; |
9 |
import java.io.InputStreamReader; |
10 |
import java.io.Reader; |
11 |
import java.io.StringReader; |
12 |
import java.util.ArrayList; |
13 |
|
14 |
import javax.xml.parsers.FactoryConfigurationError; |
15 |
import javax.xml.parsers.ParserConfigurationException; |
16 |
import javax.xml.parsers.SAXParser; |
17 |
import javax.xml.parsers.SAXParserFactory; |
18 |
|
19 |
import org.apache.http.HeaderElement; |
20 |
import org.apache.http.HttpEntity; |
21 |
import org.apache.http.HttpResponse; |
22 |
import org.apache.http.NameValuePair; |
23 |
import org.apache.http.client.ClientProtocolException; |
24 |
import org.apache.http.client.methods.HttpGet; |
25 |
import org.apache.http.impl.client.BasicResponseHandler; |
26 |
import org.apache.http.impl.client.DefaultHttpClient; |
27 |
import org.apache.http.protocol.HTTP; |
28 |
import org.xml.sax.InputSource; |
29 |
import org.xml.sax.SAXException; |
30 |
import org.xml.sax.XMLReader; |
31 |
|
32 |
import android.net.ParseException; |
33 |
|
34 |
import com.rackspace.cloud.servers.api.client.parsers.ServersXMLParser; |
35 |
//import com.slicehost.android.models.SlicesXMLParser;
|
36 |
|
37 |
/**
|
38 |
* @author mike
|
39 |
*
|
40 |
*/
|
41 |
public class EntityManager { |
42 |
|
43 |
//
|
44 |
// CRUD Operations
|
45 |
//
|
46 |
|
47 |
public void create(Entity e) { |
48 |
|
49 |
} |
50 |
|
51 |
public void remove(Entity e) { |
52 |
|
53 |
} |
54 |
|
55 |
public void update(Entity e) { |
56 |
|
57 |
} |
58 |
|
59 |
public void refresh(Entity e) { |
60 |
|
61 |
} |
62 |
|
63 |
public Entity find(long id) { |
64 |
return null; |
65 |
} |
66 |
|
67 |
//
|
68 |
// Polling Operations
|
69 |
//
|
70 |
public void wait(Entity e) { |
71 |
|
72 |
} |
73 |
|
74 |
public void wait(Entity e, long timeout) { |
75 |
|
76 |
} |
77 |
|
78 |
public void notify(Entity e, ChangeListener ch) { |
79 |
|
80 |
} |
81 |
|
82 |
public void stopNotify(Entity e, ChangeListener ch) { |
83 |
|
84 |
} |
85 |
|
86 |
public static String getResponseBody(HttpResponse response) { |
87 |
String response_text = null; |
88 |
HttpEntity entity = null;
|
89 |
try {
|
90 |
entity = response.getEntity(); |
91 |
response_text = _getResponseBody(entity); |
92 |
} catch (ParseException e) { |
93 |
e.printStackTrace(); |
94 |
} catch (IOException e) { |
95 |
if (entity != null) { |
96 |
try {
|
97 |
entity.consumeContent(); |
98 |
} catch (IOException e1) { |
99 |
} |
100 |
} |
101 |
} |
102 |
return response_text;
|
103 |
} |
104 |
|
105 |
public static String _getResponseBody(final HttpEntity entity) |
106 |
throws IOException, ParseException { |
107 |
if (entity == null) { |
108 |
throw new IllegalArgumentException("HTTP entity may not be null"); |
109 |
} |
110 |
InputStream instream = entity.getContent();
|
111 |
if (instream == null) { |
112 |
return ""; |
113 |
} |
114 |
if (entity.getContentLength() > Integer.MAX_VALUE) { |
115 |
throw new IllegalArgumentException("HTTP entity too large to be buffered in memory"); |
116 |
} |
117 |
String charset = getContentCharSet(entity);
|
118 |
if (charset == null) { |
119 |
charset = HTTP.DEFAULT_CONTENT_CHARSET; |
120 |
} |
121 |
Reader reader = new InputStreamReader(instream, charset); |
122 |
StringBuilder buffer = new StringBuilder(); |
123 |
try {
|
124 |
char[] tmp = new char[1024]; |
125 |
int l;
|
126 |
while ((l = reader.read(tmp)) != -1) { |
127 |
buffer.append(tmp, 0, l);
|
128 |
} |
129 |
} finally {
|
130 |
reader.close(); |
131 |
} |
132 |
return buffer.toString();
|
133 |
} |
134 |
|
135 |
public static String getContentCharSet(final HttpEntity entity) |
136 |
throws ParseException { |
137 |
if (entity == null) { |
138 |
throw new IllegalArgumentException("HTTP entity may not be null"); |
139 |
} |
140 |
String charset = null; |
141 |
if (entity.getContentType() != null) { |
142 |
HeaderElement values[] = entity.getContentType().getElements();
|
143 |
if (values.length > 0) { |
144 |
NameValuePair param = values[0].getParameterByName("charset"); |
145 |
if (param != null) { |
146 |
charset = param.getValue(); |
147 |
} |
148 |
} |
149 |
} |
150 |
return charset;
|
151 |
} |
152 |
|
153 |
//
|
154 |
// Lists
|
155 |
//
|
156 |
//public EntityList createList(boolean detail) {
|
157 |
public ArrayList createList(boolean detail) { |
158 |
|
159 |
DefaultHttpClient httpclient = new DefaultHttpClient();
|
160 |
HttpGet get = new HttpGet(Account.getServerUrl() + "/servers/detail.xml?now=cache_time2"); |
161 |
ArrayList<Server> servers = new ArrayList<Server>(); |
162 |
|
163 |
//get.addHeader("X-Auth-User", Account.getUsername());
|
164 |
//get.addHeader("X-Auth-Key", Account.getApiKey());
|
165 |
get.addHeader("X-Auth-Token", Account.getAuthToken());
|
166 |
|
167 |
try {
|
168 |
HttpResponse resp = httpclient.execute(get); |
169 |
System.out.println(resp.getStatusLine().toString());
|
170 |
//System.out.println("body:\n\n" + getResponseBody(resp));
|
171 |
|
172 |
BasicResponseHandler responseHandler = new BasicResponseHandler();
|
173 |
String body = responseHandler.handleResponse(resp);
|
174 |
System.out.println("body:\n\n" + body); |
175 |
|
176 |
if (resp.getStatusLine().getStatusCode() == 200 || resp.getStatusLine().getStatusCode() == 203) { |
177 |
|
178 |
// TODO: parse the servers
|
179 |
ServersXMLParser serversXMLParser = new ServersXMLParser();
|
180 |
SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); |
181 |
XMLReader xmlReader = saxParser.getXMLReader(); |
182 |
xmlReader.setContentHandler(serversXMLParser); |
183 |
|
184 |
//xmlReader.parse(getResponseBody(resp));
|
185 |
xmlReader.parse(new InputSource(new StringReader(body))); |
186 |
|
187 |
//new EntityList(serversXMLParser.getServers());
|
188 |
|
189 |
servers = serversXMLParser.getServers(); |
190 |
|
191 |
//xmlReader.parse(new StringReader(resp.getBody()));
|
192 |
/*
|
193 |
|
194 |
SlicesXMLParser parser = new SlicesXMLParser();
|
195 |
BufferedReader xml = c.getStream("/slices.xml", credentials);
|
196 |
|
197 |
try {
|
198 |
SAXParserFactory spf = SAXParserFactory.newInstance();
|
199 |
SAXParser sp = spf.newSAXParser();
|
200 |
XMLReader xr = sp.getXMLReader();
|
201 |
xr.setContentHandler(parser);
|
202 |
xr.parse(new InputSource(xml));
|
203 |
} catch (Exception se) {
|
204 |
se.printStackTrace();
|
205 |
}
|
206 |
return parser.getSlices();
|
207 |
|
208 |
*/
|
209 |
|
210 |
//return true;
|
211 |
} else {
|
212 |
//return false;
|
213 |
} |
214 |
} catch (ClientProtocolException cpe) {
|
215 |
// TODO Auto-generated catch block
|
216 |
cpe.printStackTrace(); |
217 |
//return false;
|
218 |
} catch (IOException e) { |
219 |
// TODO Auto-generated catch block
|
220 |
e.printStackTrace(); |
221 |
//return false;
|
222 |
} catch (ParserConfigurationException e) { |
223 |
// TODO Auto-generated catch block
|
224 |
e.printStackTrace(); |
225 |
} catch (SAXException e) {
|
226 |
// TODO Auto-generated catch block
|
227 |
e.printStackTrace(); |
228 |
} catch (FactoryConfigurationError e) { |
229 |
// TODO Auto-generated catch block
|
230 |
e.printStackTrace(); |
231 |
} |
232 |
|
233 |
|
234 |
return servers;
|
235 |
} |
236 |
|
237 |
public EntityList createDeltaList(boolean detail, long changesSince) { |
238 |
return null; |
239 |
} |
240 |
|
241 |
public EntityList createList(boolean detail, long offset, long limit) { |
242 |
return null; |
243 |
} |
244 |
|
245 |
public EntityList createDeltaList(boolean detail, long changesSince, long offset, long limit) { |
246 |
return null; |
247 |
} |
248 |
|
249 |
} |