root / src / com / rackspace / cloud / servers / api / client / EntityManager.java @ 9a4cdbc6
History | View | Annotate | Download (5.6 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 Entity find(long id) { |
110 |
return null; |
111 |
} |
112 |
|
113 |
//
|
114 |
// Polling Operations
|
115 |
//
|
116 |
public void wait(Entity e) { |
117 |
|
118 |
} |
119 |
|
120 |
public void wait(Entity e, long timeout) { |
121 |
|
122 |
} |
123 |
|
124 |
public void notify(Entity e, ChangeListener ch) { |
125 |
|
126 |
} |
127 |
|
128 |
public void stopNotify(Entity e, ChangeListener ch) { |
129 |
|
130 |
} |
131 |
|
132 |
//
|
133 |
// Lists
|
134 |
//
|
135 |
//public EntityList createList(boolean detail) {
|
136 |
public ArrayList createList(boolean detail) { |
137 |
|
138 |
// cache busting
|
139 |
Calendar calendar = Calendar.getInstance(); |
140 |
java.util.Date now = calendar.getTime(); |
141 |
java.sql.Timestamp currentTimestamp = new java.sql.Timestamp(now.getTime());
|
142 |
|
143 |
DefaultHttpClient httpclient = new DefaultHttpClient();
|
144 |
HttpGet get = new HttpGet(Account.getServerUrl() + "/servers/detail.xml?now=" + currentTimestamp.getTime()); |
145 |
ArrayList<Server> servers = new ArrayList<Server>(); |
146 |
|
147 |
get.addHeader("X-Auth-Token", Account.getAuthToken());
|
148 |
|
149 |
try {
|
150 |
HttpResponse resp = httpclient.execute(get); |
151 |
System.out.println(resp.getStatusLine().toString());
|
152 |
//System.out.println("body:\n\n" + getResponseBody(resp));
|
153 |
|
154 |
BasicResponseHandler responseHandler = new BasicResponseHandler();
|
155 |
String body = responseHandler.handleResponse(resp);
|
156 |
System.out.println("body:\n\n" + body); |
157 |
|
158 |
if (resp.getStatusLine().getStatusCode() == 200 || resp.getStatusLine().getStatusCode() == 203) { |
159 |
ServersXMLParser serversXMLParser = new ServersXMLParser();
|
160 |
SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); |
161 |
XMLReader xmlReader = saxParser.getXMLReader(); |
162 |
xmlReader.setContentHandler(serversXMLParser); |
163 |
xmlReader.parse(new InputSource(new StringReader(body))); |
164 |
servers = serversXMLParser.getServers(); |
165 |
} |
166 |
} catch (ClientProtocolException cpe) {
|
167 |
// TODO Auto-generated catch block
|
168 |
cpe.printStackTrace(); |
169 |
//return false;
|
170 |
} catch (IOException e) { |
171 |
// TODO Auto-generated catch block
|
172 |
e.printStackTrace(); |
173 |
//return false;
|
174 |
} catch (ParserConfigurationException e) { |
175 |
// TODO Auto-generated catch block
|
176 |
e.printStackTrace(); |
177 |
} catch (SAXException e) {
|
178 |
// TODO Auto-generated catch block
|
179 |
e.printStackTrace(); |
180 |
} catch (FactoryConfigurationError e) { |
181 |
// TODO Auto-generated catch block
|
182 |
e.printStackTrace(); |
183 |
} |
184 |
|
185 |
|
186 |
return servers;
|
187 |
} |
188 |
|
189 |
public EntityList createDeltaList(boolean detail, long changesSince) { |
190 |
return null; |
191 |
} |
192 |
|
193 |
public EntityList createList(boolean detail, long offset, long limit) { |
194 |
return null; |
195 |
} |
196 |
|
197 |
public EntityList createDeltaList(boolean detail, long changesSince, long offset, long limit) { |
198 |
return null; |
199 |
} |
200 |
|
201 |
} |