root / src / com / rackspace / cloud / servers / api / client / EntityManager.java @ af63e739
History | View | Annotate | Download (6.9 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.io.UnsupportedEncodingException; |
9 |
import java.util.ArrayList; |
10 |
import java.util.Calendar; |
11 |
|
12 |
import javax.xml.parsers.FactoryConfigurationError; |
13 |
import javax.xml.parsers.ParserConfigurationException; |
14 |
import javax.xml.parsers.SAXParser; |
15 |
import javax.xml.parsers.SAXParserFactory; |
16 |
|
17 |
import org.apache.http.HttpResponse; |
18 |
import org.apache.http.client.ClientProtocolException; |
19 |
import org.apache.http.client.methods.HttpGet; |
20 |
import org.apache.http.client.methods.HttpPost; |
21 |
import org.apache.http.entity.StringEntity; |
22 |
import org.apache.http.impl.client.BasicResponseHandler; |
23 |
import org.apache.http.impl.client.DefaultHttpClient; |
24 |
import org.apache.http.protocol.RequestExpectContinue; |
25 |
import org.xml.sax.InputSource; |
26 |
import org.xml.sax.SAXException; |
27 |
import org.xml.sax.XMLReader; |
28 |
|
29 |
import com.rackspace.cloud.servers.api.client.parsers.ServersXMLParser; |
30 |
|
31 |
/**
|
32 |
* @author mike
|
33 |
*
|
34 |
*/
|
35 |
public class EntityManager { |
36 |
|
37 |
//
|
38 |
// CRUD Operations
|
39 |
//
|
40 |
|
41 |
public void create(Server entity) { |
42 |
|
43 |
DefaultHttpClient httpclient = new DefaultHttpClient();
|
44 |
HttpPost post = new HttpPost(Account.getServerUrl() + "/servers.xml"); |
45 |
|
46 |
post.addHeader("X-Auth-Token", Account.getAuthToken());
|
47 |
post.addHeader("Content-Type", "application/xml"); |
48 |
|
49 |
StringEntity tmp = null;
|
50 |
try {
|
51 |
tmp = new StringEntity(entity.toXML());
|
52 |
} catch (UnsupportedEncodingException e) { |
53 |
System.out.println("HTTPHelp : UnsupportedEncodingException : " + e); |
54 |
// TODO: handle?
|
55 |
} |
56 |
post.setEntity(tmp); |
57 |
|
58 |
httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class); |
59 |
|
60 |
try {
|
61 |
HttpResponse resp = httpclient.execute(post); |
62 |
System.out.println(resp.getStatusLine().toString());
|
63 |
|
64 |
if (resp.getStatusLine().getStatusCode() == 202) { |
65 |
// TODO: handle success and failure
|
66 |
|
67 |
BasicResponseHandler responseHandler = new BasicResponseHandler();
|
68 |
String body = responseHandler.handleResponse(resp);
|
69 |
|
70 |
ServersXMLParser serversXMLParser = new ServersXMLParser();
|
71 |
SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); |
72 |
XMLReader xmlReader = saxParser.getXMLReader(); |
73 |
xmlReader.setContentHandler(serversXMLParser); |
74 |
xmlReader.parse(new InputSource(new StringReader(body))); |
75 |
entity = serversXMLParser.getServer(); |
76 |
|
77 |
} |
78 |
} catch (ClientProtocolException cpe) {
|
79 |
// TODO Auto-generated catch block
|
80 |
cpe.printStackTrace(); |
81 |
} catch (IOException e) { |
82 |
// TODO Auto-generated catch block
|
83 |
e.printStackTrace(); |
84 |
//return false;
|
85 |
} catch (FactoryConfigurationError e) { |
86 |
// TODO Auto-generated catch block
|
87 |
e.printStackTrace(); |
88 |
} catch (ParserConfigurationException e) { |
89 |
// TODO Auto-generated catch block
|
90 |
e.printStackTrace(); |
91 |
} catch (SAXException e) {
|
92 |
// TODO Auto-generated catch block
|
93 |
e.printStackTrace(); |
94 |
} |
95 |
} |
96 |
|
97 |
public void remove(Entity e) { |
98 |
|
99 |
} |
100 |
|
101 |
public void update(Entity e) { |
102 |
|
103 |
} |
104 |
|
105 |
public void refresh(Entity e) { |
106 |
|
107 |
} |
108 |
|
109 |
public Server find(long id) { |
110 |
Server server = null;
|
111 |
DefaultHttpClient httpclient = new DefaultHttpClient();
|
112 |
HttpGet get = new HttpGet(Account.getServerUrl() + "/servers/" + id + ".xml" + cacheBuster()); |
113 |
|
114 |
get.addHeader("X-Auth-Token", Account.getAuthToken());
|
115 |
|
116 |
try {
|
117 |
HttpResponse resp = httpclient.execute(get); |
118 |
BasicResponseHandler responseHandler = new BasicResponseHandler();
|
119 |
String body = responseHandler.handleResponse(resp);
|
120 |
|
121 |
if (resp.getStatusLine().getStatusCode() == 200 || resp.getStatusLine().getStatusCode() == 203) { |
122 |
ServersXMLParser serversXMLParser = new ServersXMLParser();
|
123 |
SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); |
124 |
XMLReader xmlReader = saxParser.getXMLReader(); |
125 |
xmlReader.setContentHandler(serversXMLParser); |
126 |
xmlReader.parse(new InputSource(new StringReader(body))); |
127 |
server = serversXMLParser.getServer(); |
128 |
} |
129 |
} catch (ClientProtocolException cpe) {
|
130 |
// TODO Auto-generated catch block
|
131 |
cpe.printStackTrace(); |
132 |
//return false;
|
133 |
} catch (IOException e) { |
134 |
// TODO Auto-generated catch block
|
135 |
e.printStackTrace(); |
136 |
//return false;
|
137 |
} catch (ParserConfigurationException e) { |
138 |
// TODO Auto-generated catch block
|
139 |
e.printStackTrace(); |
140 |
} catch (SAXException e) {
|
141 |
// TODO Auto-generated catch block
|
142 |
e.printStackTrace(); |
143 |
} catch (FactoryConfigurationError e) { |
144 |
// TODO Auto-generated catch block
|
145 |
e.printStackTrace(); |
146 |
} |
147 |
|
148 |
return server;
|
149 |
} |
150 |
|
151 |
//
|
152 |
// Polling Operations
|
153 |
//
|
154 |
public void wait(Entity e) { |
155 |
|
156 |
} |
157 |
|
158 |
public void wait(Entity e, long timeout) { |
159 |
|
160 |
} |
161 |
|
162 |
public void notify(Entity e, ChangeListener ch) { |
163 |
|
164 |
} |
165 |
|
166 |
public void stopNotify(Entity e, ChangeListener ch) { |
167 |
|
168 |
} |
169 |
|
170 |
private String cacheBuster() { |
171 |
Calendar calendar = Calendar.getInstance(); |
172 |
java.util.Date now = calendar.getTime(); |
173 |
java.sql.Timestamp currentTimestamp = new java.sql.Timestamp(now.getTime());
|
174 |
return "?now=" + currentTimestamp.getTime(); |
175 |
} |
176 |
|
177 |
//
|
178 |
// Lists
|
179 |
//
|
180 |
//public EntityList createList(boolean detail) {
|
181 |
public ArrayList createList(boolean detail) { |
182 |
|
183 |
DefaultHttpClient httpclient = new DefaultHttpClient();
|
184 |
HttpGet get = new HttpGet(Account.getServerUrl() + "/servers/detail.xml" + cacheBuster()); |
185 |
ArrayList<Server> servers = new ArrayList<Server>(); |
186 |
|
187 |
get.addHeader("X-Auth-Token", Account.getAuthToken());
|
188 |
|
189 |
try {
|
190 |
HttpResponse resp = httpclient.execute(get); |
191 |
BasicResponseHandler responseHandler = new BasicResponseHandler();
|
192 |
String body = responseHandler.handleResponse(resp);
|
193 |
|
194 |
if (resp.getStatusLine().getStatusCode() == 200 || resp.getStatusLine().getStatusCode() == 203) { |
195 |
ServersXMLParser serversXMLParser = new ServersXMLParser();
|
196 |
SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); |
197 |
XMLReader xmlReader = saxParser.getXMLReader(); |
198 |
xmlReader.setContentHandler(serversXMLParser); |
199 |
xmlReader.parse(new InputSource(new StringReader(body))); |
200 |
servers = serversXMLParser.getServers(); |
201 |
} |
202 |
} catch (ClientProtocolException cpe) {
|
203 |
// TODO Auto-generated catch block
|
204 |
cpe.printStackTrace(); |
205 |
//return false;
|
206 |
} catch (IOException e) { |
207 |
// TODO Auto-generated catch block
|
208 |
e.printStackTrace(); |
209 |
//return false;
|
210 |
} catch (ParserConfigurationException e) { |
211 |
// TODO Auto-generated catch block
|
212 |
e.printStackTrace(); |
213 |
} catch (SAXException e) {
|
214 |
// TODO Auto-generated catch block
|
215 |
e.printStackTrace(); |
216 |
} catch (FactoryConfigurationError e) { |
217 |
// TODO Auto-generated catch block
|
218 |
e.printStackTrace(); |
219 |
} |
220 |
|
221 |
|
222 |
return servers;
|
223 |
} |
224 |
|
225 |
public EntityList createDeltaList(boolean detail, long changesSince) { |
226 |
return null; |
227 |
} |
228 |
|
229 |
public EntityList createList(boolean detail, long offset, long limit) { |
230 |
return null; |
231 |
} |
232 |
|
233 |
public EntityList createDeltaList(boolean detail, long changesSince, long offset, long limit) { |
234 |
return null; |
235 |
} |
236 |
|
237 |
} |