root / src / com / rackspace / cloud / files / api / client / ContainerManager.java @ 6c0d81f1
History | View | Annotate | Download (11.3 kB)
1 |
package com.rackspace.cloud.files.api.client; |
---|---|
2 |
|
3 |
import java.io.IOException; |
4 |
import java.io.StringReader; |
5 |
import java.net.MalformedURLException; |
6 |
import java.net.URI; |
7 |
import java.net.URISyntaxException; |
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.HttpDelete; |
18 |
import org.apache.http.client.methods.HttpGet; |
19 |
import org.apache.http.client.methods.HttpPost; |
20 |
import org.apache.http.client.methods.HttpPut; |
21 |
import org.apache.http.impl.client.BasicResponseHandler; |
22 |
import org.apache.http.protocol.RequestExpectContinue; |
23 |
import org.xml.sax.InputSource; |
24 |
import org.xml.sax.SAXException; |
25 |
import org.xml.sax.XMLReader; |
26 |
|
27 |
import android.content.Context; |
28 |
import android.text.Editable; |
29 |
import android.util.Log; |
30 |
|
31 |
import com.rackspace.cloud.files.api.client.parsers.ContainerXMLParser; |
32 |
import com.rackspace.cloud.servers.api.client.Account; |
33 |
import com.rackspace.cloud.servers.api.client.CloudServersException; |
34 |
import com.rackspace.cloud.servers.api.client.EntityManager; |
35 |
import com.rackspace.cloud.servers.api.client.http.HttpBundle; |
36 |
import com.rackspace.cloud.servers.api.client.parsers.CloudServersFaultXMLParser; |
37 |
|
38 |
/**
|
39 |
* @author Phillip Toohill
|
40 |
*
|
41 |
*/
|
42 |
public class ContainerManager extends EntityManager { |
43 |
private Context context; |
44 |
|
45 |
public ContainerManager(Context context) { |
46 |
this.context = context;
|
47 |
} |
48 |
|
49 |
public HttpBundle create(Editable editable) throws CloudServersException { |
50 |
HttpResponse resp = null;
|
51 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
52 |
|
53 |
String url = getSafeURL(Account.getAccount().getStorageUrl(),
|
54 |
editable.toString()); |
55 |
HttpPut put = new HttpPut(url);
|
56 |
|
57 |
put.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
|
58 |
httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class); |
59 |
|
60 |
HttpBundle bundle = new HttpBundle();
|
61 |
bundle.setCurlRequest(put); |
62 |
|
63 |
try {
|
64 |
resp = httpclient.execute(put); |
65 |
bundle.setHttpResponse(resp); |
66 |
} catch (ClientProtocolException e) {
|
67 |
CloudServersException cse = new CloudServersException();
|
68 |
cse.setMessage(e.getLocalizedMessage()); |
69 |
throw cse;
|
70 |
} catch (IOException e) { |
71 |
CloudServersException cse = new CloudServersException();
|
72 |
cse.setMessage(e.getLocalizedMessage()); |
73 |
throw cse;
|
74 |
} catch (FactoryConfigurationError e) { |
75 |
CloudServersException cse = new CloudServersException();
|
76 |
cse.setMessage(e.getLocalizedMessage()); |
77 |
throw cse;
|
78 |
} |
79 |
return bundle;
|
80 |
} |
81 |
|
82 |
public ArrayList<Container> createCDNList(boolean detail) |
83 |
throws CloudServersException {
|
84 |
ArrayList<Container> cdnContainers = new ArrayList<Container>(); |
85 |
if (Account.getAccount().getCdnManagementUrl() != null) { |
86 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
87 |
HttpGet get = new HttpGet(Account.getAccount()
|
88 |
.getCdnManagementUrl() + "?format=xml");
|
89 |
|
90 |
get.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
|
91 |
try {
|
92 |
HttpResponse resp = httpclient.execute(get); |
93 |
BasicResponseHandler responseHandler = new BasicResponseHandler();
|
94 |
String body = responseHandler.handleResponse(resp);
|
95 |
if (resp.getStatusLine().getStatusCode() == 200) { |
96 |
ContainerXMLParser cdnContainerXMLParser = new ContainerXMLParser();
|
97 |
SAXParser saxParser = SAXParserFactory.newInstance() |
98 |
.newSAXParser(); |
99 |
XMLReader xmlReader = saxParser.getXMLReader(); |
100 |
xmlReader.setContentHandler(cdnContainerXMLParser); |
101 |
|
102 |
xmlReader.parse(new InputSource(new StringReader(body))); |
103 |
cdnContainers = cdnContainerXMLParser.getContainers(); |
104 |
} else {
|
105 |
CloudServersFaultXMLParser parser = new CloudServersFaultXMLParser();
|
106 |
SAXParser saxParser = SAXParserFactory.newInstance() |
107 |
.newSAXParser(); |
108 |
XMLReader xmlReader = saxParser.getXMLReader(); |
109 |
xmlReader.setContentHandler(parser); |
110 |
xmlReader.parse(new InputSource(new StringReader(body))); |
111 |
CloudServersException cse = parser.getException(); |
112 |
throw cse;
|
113 |
} |
114 |
} catch (ClientProtocolException e) {
|
115 |
CloudServersException cse = new CloudServersException();
|
116 |
cse.setMessage(e.getLocalizedMessage()); |
117 |
throw cse;
|
118 |
} catch (IOException e) { |
119 |
CloudServersException cse = new CloudServersException();
|
120 |
cse.setMessage(e.getLocalizedMessage()); |
121 |
throw cse;
|
122 |
} catch (ParserConfigurationException e) { |
123 |
CloudServersException cse = new CloudServersException();
|
124 |
cse.setMessage(e.getLocalizedMessage()); |
125 |
throw cse;
|
126 |
} catch (SAXException e) {
|
127 |
CloudServersException cse = new CloudServersException();
|
128 |
cse.setMessage(e.getLocalizedMessage()); |
129 |
throw cse;
|
130 |
} catch (FactoryConfigurationError e) { |
131 |
CloudServersException cse = new CloudServersException();
|
132 |
cse.setMessage(e.getLocalizedMessage()); |
133 |
throw cse;
|
134 |
} |
135 |
} |
136 |
else{
|
137 |
Container c = new Container(); |
138 |
c.setCdnEnabled(true);
|
139 |
c.setCdnUrl(Account.getAccount().getStorageUrl()); |
140 |
c.setName("Pithos+");
|
141 |
cdnContainers.add(c); |
142 |
} |
143 |
|
144 |
return cdnContainers;
|
145 |
} |
146 |
|
147 |
public HttpBundle enable(String container, String ttl, String logRet) |
148 |
throws CloudServersException {
|
149 |
HttpResponse resp = null;
|
150 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
151 |
String url = getSafeURL(Account.getAccount().getCdnManagementUrl(),
|
152 |
container); |
153 |
HttpPut put = new HttpPut(url);
|
154 |
|
155 |
put.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
|
156 |
put.addHeader("X-TTL", ttl);
|
157 |
put.addHeader("X-Log-Retention", logRet);
|
158 |
Log.v("cdn manager", ttl + container + logRet);
|
159 |
httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class); |
160 |
|
161 |
HttpBundle bundle = new HttpBundle();
|
162 |
bundle.setCurlRequest(put); |
163 |
|
164 |
try {
|
165 |
resp = httpclient.execute(put); |
166 |
bundle.setHttpResponse(resp); |
167 |
} catch (ClientProtocolException e) {
|
168 |
CloudServersException cse = new CloudServersException();
|
169 |
cse.setMessage(e.getLocalizedMessage()); |
170 |
throw cse;
|
171 |
} catch (IOException e) { |
172 |
CloudServersException cse = new CloudServersException();
|
173 |
cse.setMessage(e.getLocalizedMessage()); |
174 |
throw cse;
|
175 |
} catch (FactoryConfigurationError e) { |
176 |
CloudServersException cse = new CloudServersException();
|
177 |
cse.setMessage(e.getLocalizedMessage()); |
178 |
throw cse;
|
179 |
} |
180 |
return bundle;
|
181 |
} |
182 |
|
183 |
public HttpBundle disable(String container, String cdn, String ttl, |
184 |
String logRet) throws CloudServersException { |
185 |
HttpResponse resp = null;
|
186 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
187 |
String url = getSafeURL(Account.getAccount().getCdnManagementUrl(),
|
188 |
container); |
189 |
HttpPost post = new HttpPost(url);
|
190 |
|
191 |
post.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
|
192 |
post.addHeader("X-TTL", ttl);
|
193 |
post.addHeader("X-Log-Retention", logRet);
|
194 |
post.addHeader("X-CDN-Enabled", cdn);
|
195 |
httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class); |
196 |
|
197 |
HttpBundle bundle = new HttpBundle();
|
198 |
bundle.setCurlRequest(post); |
199 |
|
200 |
try {
|
201 |
resp = httpclient.execute(post); |
202 |
bundle.setHttpResponse(resp); |
203 |
} catch (ClientProtocolException e) {
|
204 |
CloudServersException cse = new CloudServersException();
|
205 |
cse.setMessage(e.getLocalizedMessage()); |
206 |
throw cse;
|
207 |
} catch (IOException e) { |
208 |
CloudServersException cse = new CloudServersException();
|
209 |
cse.setMessage(e.getLocalizedMessage()); |
210 |
throw cse;
|
211 |
} catch (FactoryConfigurationError e) { |
212 |
CloudServersException cse = new CloudServersException();
|
213 |
cse.setMessage(e.getLocalizedMessage()); |
214 |
throw cse;
|
215 |
} |
216 |
return bundle;
|
217 |
} |
218 |
|
219 |
public HttpBundle delete(String string) throws CloudServersException { |
220 |
HttpResponse resp = null;
|
221 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
222 |
String url = getSafeURL(Account.getAccount().getStorageUrl(), string);
|
223 |
HttpDelete put = new HttpDelete(url);
|
224 |
|
225 |
put.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
|
226 |
httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class); |
227 |
|
228 |
HttpBundle bundle = new HttpBundle();
|
229 |
bundle.setCurlRequest(put); |
230 |
|
231 |
try {
|
232 |
resp = httpclient.execute(put); |
233 |
bundle.setHttpResponse(resp); |
234 |
} catch (ClientProtocolException e) {
|
235 |
CloudServersException cse = new CloudServersException();
|
236 |
cse.setMessage(e.getLocalizedMessage()); |
237 |
throw cse;
|
238 |
} catch (IOException e) { |
239 |
CloudServersException cse = new CloudServersException();
|
240 |
cse.setMessage(e.getLocalizedMessage()); |
241 |
throw cse;
|
242 |
} catch (FactoryConfigurationError e) { |
243 |
CloudServersException cse = new CloudServersException();
|
244 |
cse.setMessage(e.getLocalizedMessage()); |
245 |
throw cse;
|
246 |
} |
247 |
return bundle;
|
248 |
} |
249 |
|
250 |
public ArrayList<Container> createList(boolean detail) |
251 |
throws CloudServersException {
|
252 |
|
253 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
254 |
HttpGet get = new HttpGet(Account.getAccount().getStorageUrl()
|
255 |
+ "?format=xml");
|
256 |
ArrayList<Container> containers = new ArrayList<Container>(); |
257 |
get.addHeader(CustomHttpClient.X_STORAGE_TOKEN, Account.getAccount().getStorageToken()); |
258 |
get.addHeader("Content-Type", "application/xml"); |
259 |
|
260 |
try {
|
261 |
HttpResponse resp = httpclient.execute(get); |
262 |
BasicResponseHandler responseHandler = new BasicResponseHandler();
|
263 |
String body = responseHandler.handleResponse(resp);
|
264 |
Log.i("ContainerManager", body);
|
265 |
if (resp.getStatusLine().getStatusCode() == 200 |
266 |
|| resp.getStatusLine().getStatusCode() == 203) {
|
267 |
ContainerXMLParser containerXMLParser = new ContainerXMLParser();
|
268 |
SAXParser saxParser = SAXParserFactory.newInstance() |
269 |
.newSAXParser(); |
270 |
XMLReader xmlReader = saxParser.getXMLReader(); |
271 |
xmlReader.setContentHandler(containerXMLParser); |
272 |
|
273 |
xmlReader.parse(new InputSource(new StringReader(body))); |
274 |
containers = containerXMLParser.getContainers(); |
275 |
} else {
|
276 |
CloudServersFaultXMLParser parser = new CloudServersFaultXMLParser();
|
277 |
SAXParser saxParser = SAXParserFactory.newInstance() |
278 |
.newSAXParser(); |
279 |
XMLReader xmlReader = saxParser.getXMLReader(); |
280 |
xmlReader.setContentHandler(parser); |
281 |
xmlReader.parse(new InputSource(new StringReader(body))); |
282 |
CloudServersException cse = parser.getException(); |
283 |
throw cse;
|
284 |
} |
285 |
} catch (ClientProtocolException e) {
|
286 |
Log.e("ERR", "", e); |
287 |
CloudServersException cse = new CloudServersException();
|
288 |
cse.setMessage(e.getLocalizedMessage()); |
289 |
throw cse;
|
290 |
} catch (IOException e) { |
291 |
CloudServersException cse = new CloudServersException();
|
292 |
cse.setMessage(e.getLocalizedMessage()); |
293 |
throw cse;
|
294 |
} catch (ParserConfigurationException e) { |
295 |
CloudServersException cse = new CloudServersException();
|
296 |
cse.setMessage(e.getLocalizedMessage()); |
297 |
throw cse;
|
298 |
} catch (SAXException e) {
|
299 |
CloudServersException cse = new CloudServersException();
|
300 |
cse.setMessage(e.getLocalizedMessage()); |
301 |
throw cse;
|
302 |
} catch (FactoryConfigurationError e) { |
303 |
CloudServersException cse = new CloudServersException();
|
304 |
cse.setMessage(e.getLocalizedMessage()); |
305 |
throw cse;
|
306 |
} |
307 |
|
308 |
return containers;
|
309 |
} |
310 |
|
311 |
private String getSafeURL(String badURL, String name) { |
312 |
URI uri = null; |
313 |
try {
|
314 |
uri = new URI("https", badURL.substring(8), "/" + name.toString() |
315 |
+ "/", ""); |
316 |
} catch (URISyntaxException e1) { |
317 |
// TODO Auto-generated catch block
|
318 |
e1.printStackTrace(); |
319 |
} |
320 |
String url = null; |
321 |
try {
|
322 |
url = uri.toURL().toString(); |
323 |
} catch (MalformedURLException e1) { |
324 |
// TODO Auto-generated catch block
|
325 |
e1.printStackTrace(); |
326 |
} |
327 |
return url.substring(0, url.length() - 1); |
328 |
} |
329 |
|
330 |
} |