root / src / com / rackspace / cloud / files / api / client / ContainerManager.java @ 732d9c2c
History | View | Annotate | Download (17.1 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 |
import java.util.List; |
10 |
|
11 |
import javax.xml.parsers.FactoryConfigurationError; |
12 |
import javax.xml.parsers.ParserConfigurationException; |
13 |
import javax.xml.parsers.SAXParser; |
14 |
import javax.xml.parsers.SAXParserFactory; |
15 |
|
16 |
import org.apache.http.Header; |
17 |
import org.apache.http.HttpResponse; |
18 |
import org.apache.http.client.ClientProtocolException; |
19 |
import org.apache.http.client.methods.HttpDelete; |
20 |
import org.apache.http.client.methods.HttpGet; |
21 |
import org.apache.http.client.methods.HttpPost; |
22 |
import org.apache.http.client.methods.HttpPut; |
23 |
import org.apache.http.impl.client.BasicResponseHandler; |
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 android.content.Context; |
30 |
import android.text.Editable; |
31 |
import android.util.Log; |
32 |
|
33 |
import com.rackspace.cloud.android.AndroidCloudApplication; |
34 |
import com.rackspace.cloud.files.api.client.parsers.ContainerXMLParser; |
35 |
import com.rackspace.cloud.files.api.client.parsers.OthersXmlParser; |
36 |
import com.rackspace.cloud.servers.api.client.Account; |
37 |
import com.rackspace.cloud.servers.api.client.CloudServersException; |
38 |
import com.rackspace.cloud.servers.api.client.EntityManager; |
39 |
import com.rackspace.cloud.servers.api.client.http.HttpBundle; |
40 |
import com.rackspace.cloud.servers.api.client.parsers.CloudServersFaultXMLParser; |
41 |
|
42 |
/**
|
43 |
* @author Phillip Toohill
|
44 |
*
|
45 |
*/
|
46 |
public class ContainerManager extends EntityManager { |
47 |
private Context context; |
48 |
public String LOG = "ContainerManager"; |
49 |
public ContainerManager(Context context) { |
50 |
this.context = context;
|
51 |
} |
52 |
|
53 |
public HttpBundle create(Editable editable) throws CloudServersException { |
54 |
HttpResponse resp = null;
|
55 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
56 |
|
57 |
String url = getSafeURL(Account.getAccount().getStorageUrl(),
|
58 |
editable.toString()); |
59 |
HttpPut put = new HttpPut(url);
|
60 |
|
61 |
put.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
|
62 |
httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class); |
63 |
|
64 |
HttpBundle bundle = new HttpBundle();
|
65 |
bundle.setCurlRequest(put); |
66 |
|
67 |
try {
|
68 |
resp = httpclient.execute(put); |
69 |
bundle.setHttpResponse(resp); |
70 |
} catch (ClientProtocolException e) {
|
71 |
CloudServersException cse = new CloudServersException();
|
72 |
cse.setMessage(e.getLocalizedMessage()); |
73 |
throw cse;
|
74 |
} catch (IOException e) { |
75 |
CloudServersException cse = new CloudServersException();
|
76 |
cse.setMessage(e.getLocalizedMessage()); |
77 |
throw cse;
|
78 |
} catch (FactoryConfigurationError e) { |
79 |
CloudServersException cse = new CloudServersException();
|
80 |
cse.setMessage(e.getLocalizedMessage()); |
81 |
throw cse;
|
82 |
} |
83 |
return bundle;
|
84 |
} |
85 |
|
86 |
public ArrayList<Container> createCDNList(boolean detail) |
87 |
throws CloudServersException {
|
88 |
ArrayList<Container> cdnContainers = new ArrayList<Container>(); |
89 |
if (Account.getAccount().getCdnManagementUrl() != null) { |
90 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
91 |
HttpGet get = new HttpGet(Account.getAccount()
|
92 |
.getCdnManagementUrl() + "?format=xml");
|
93 |
|
94 |
get.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
|
95 |
try {
|
96 |
HttpResponse resp = httpclient.execute(get); |
97 |
BasicResponseHandler responseHandler = new BasicResponseHandler();
|
98 |
String body = responseHandler.handleResponse(resp);
|
99 |
if (resp.getStatusLine().getStatusCode() == 200) { |
100 |
ContainerXMLParser cdnContainerXMLParser = new ContainerXMLParser();
|
101 |
SAXParser saxParser = SAXParserFactory.newInstance() |
102 |
.newSAXParser(); |
103 |
XMLReader xmlReader = saxParser.getXMLReader(); |
104 |
xmlReader.setContentHandler(cdnContainerXMLParser); |
105 |
|
106 |
xmlReader.parse(new InputSource(new StringReader(body))); |
107 |
cdnContainers = cdnContainerXMLParser.getContainers(); |
108 |
} else {
|
109 |
CloudServersFaultXMLParser parser = new CloudServersFaultXMLParser();
|
110 |
SAXParser saxParser = SAXParserFactory.newInstance() |
111 |
.newSAXParser(); |
112 |
XMLReader xmlReader = saxParser.getXMLReader(); |
113 |
xmlReader.setContentHandler(parser); |
114 |
xmlReader.parse(new InputSource(new StringReader(body))); |
115 |
CloudServersException cse = parser.getException(); |
116 |
throw cse;
|
117 |
} |
118 |
} catch (ClientProtocolException e) {
|
119 |
CloudServersException cse = new CloudServersException();
|
120 |
cse.setMessage(e.getLocalizedMessage()); |
121 |
throw cse;
|
122 |
} catch (IOException e) { |
123 |
CloudServersException cse = new CloudServersException();
|
124 |
cse.setMessage(e.getLocalizedMessage()); |
125 |
throw cse;
|
126 |
} catch (ParserConfigurationException e) { |
127 |
CloudServersException cse = new CloudServersException();
|
128 |
cse.setMessage(e.getLocalizedMessage()); |
129 |
throw cse;
|
130 |
} catch (SAXException e) {
|
131 |
CloudServersException cse = new CloudServersException();
|
132 |
cse.setMessage(e.getLocalizedMessage()); |
133 |
throw cse;
|
134 |
} catch (FactoryConfigurationError e) { |
135 |
CloudServersException cse = new CloudServersException();
|
136 |
cse.setMessage(e.getLocalizedMessage()); |
137 |
throw cse;
|
138 |
} |
139 |
} else {
|
140 |
Container c = new Container(); |
141 |
c.setCdnEnabled(true);
|
142 |
c.setCdnUrl(Account.getAccount().getStorageUrl()); |
143 |
c.setName("Pithos+");
|
144 |
cdnContainers.add(c); |
145 |
} |
146 |
|
147 |
return cdnContainers;
|
148 |
} |
149 |
|
150 |
public HttpBundle enable(String container, String ttl, String logRet) |
151 |
throws CloudServersException {
|
152 |
HttpResponse resp = null;
|
153 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
154 |
String url = getSafeURL(Account.getAccount().getCdnManagementUrl(),
|
155 |
container); |
156 |
HttpPut put = new HttpPut(url);
|
157 |
|
158 |
put.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
|
159 |
put.addHeader("X-TTL", ttl);
|
160 |
put.addHeader("X-Log-Retention", logRet);
|
161 |
Log.v("cdn manager", ttl + container + logRet);
|
162 |
httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class); |
163 |
|
164 |
HttpBundle bundle = new HttpBundle();
|
165 |
bundle.setCurlRequest(put); |
166 |
|
167 |
try {
|
168 |
resp = httpclient.execute(put); |
169 |
bundle.setHttpResponse(resp); |
170 |
} catch (ClientProtocolException e) {
|
171 |
CloudServersException cse = new CloudServersException();
|
172 |
cse.setMessage(e.getLocalizedMessage()); |
173 |
throw cse;
|
174 |
} catch (IOException e) { |
175 |
CloudServersException cse = new CloudServersException();
|
176 |
cse.setMessage(e.getLocalizedMessage()); |
177 |
throw cse;
|
178 |
} catch (FactoryConfigurationError e) { |
179 |
CloudServersException cse = new CloudServersException();
|
180 |
cse.setMessage(e.getLocalizedMessage()); |
181 |
throw cse;
|
182 |
} |
183 |
return bundle;
|
184 |
} |
185 |
|
186 |
public HttpBundle disable(String container, String cdn, String ttl, |
187 |
String logRet) throws CloudServersException { |
188 |
HttpResponse resp = null;
|
189 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
190 |
String url = getSafeURL(Account.getAccount().getCdnManagementUrl(),
|
191 |
container); |
192 |
HttpPost post = new HttpPost(url);
|
193 |
|
194 |
post.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
|
195 |
post.addHeader("X-TTL", ttl);
|
196 |
post.addHeader("X-Log-Retention", logRet);
|
197 |
post.addHeader("X-CDN-Enabled", cdn);
|
198 |
httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class); |
199 |
|
200 |
HttpBundle bundle = new HttpBundle();
|
201 |
bundle.setCurlRequest(post); |
202 |
|
203 |
try {
|
204 |
resp = httpclient.execute(post); |
205 |
bundle.setHttpResponse(resp); |
206 |
} catch (ClientProtocolException e) {
|
207 |
CloudServersException cse = new CloudServersException();
|
208 |
cse.setMessage(e.getLocalizedMessage()); |
209 |
throw cse;
|
210 |
} catch (IOException e) { |
211 |
CloudServersException cse = new CloudServersException();
|
212 |
cse.setMessage(e.getLocalizedMessage()); |
213 |
throw cse;
|
214 |
} catch (FactoryConfigurationError e) { |
215 |
CloudServersException cse = new CloudServersException();
|
216 |
cse.setMessage(e.getLocalizedMessage()); |
217 |
throw cse;
|
218 |
} |
219 |
return bundle;
|
220 |
} |
221 |
|
222 |
public HttpBundle delete(String string) throws CloudServersException { |
223 |
HttpResponse resp = null;
|
224 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
225 |
String url = getSafeURL(Account.getAccount().getStorageUrl(), string);
|
226 |
HttpDelete put = new HttpDelete(url);
|
227 |
|
228 |
put.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
|
229 |
httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class); |
230 |
|
231 |
HttpBundle bundle = new HttpBundle();
|
232 |
bundle.setCurlRequest(put); |
233 |
|
234 |
try {
|
235 |
resp = httpclient.execute(put); |
236 |
bundle.setHttpResponse(resp); |
237 |
} catch (ClientProtocolException e) {
|
238 |
CloudServersException cse = new CloudServersException();
|
239 |
cse.setMessage(e.getLocalizedMessage()); |
240 |
throw cse;
|
241 |
} catch (IOException e) { |
242 |
CloudServersException cse = new CloudServersException();
|
243 |
cse.setMessage(e.getLocalizedMessage()); |
244 |
throw cse;
|
245 |
} catch (FactoryConfigurationError e) { |
246 |
CloudServersException cse = new CloudServersException();
|
247 |
cse.setMessage(e.getLocalizedMessage()); |
248 |
throw cse;
|
249 |
} |
250 |
return bundle;
|
251 |
} |
252 |
|
253 |
public ArrayList<Container> createList(boolean detail) |
254 |
throws CloudServersException {
|
255 |
Log.i(LOG,"Create List:"+Account.getAccount().getStorageUrl());
|
256 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
257 |
HttpGet get = new HttpGet(Account.getAccount().getStorageUrl()
|
258 |
+ "?format=xml");
|
259 |
ArrayList<Container> containers = new ArrayList<Container>(); |
260 |
get.addHeader(CustomHttpClient.X_STORAGE_TOKEN, Account.getAccount() |
261 |
.getStorageToken()); |
262 |
get.addHeader("Content-Type", "application/xml"); |
263 |
|
264 |
try {
|
265 |
HttpResponse resp = httpclient.execute(get); |
266 |
List<GroupResource> groups = new ArrayList<GroupResource>(); |
267 |
for(Header h : resp.getAllHeaders()){
|
268 |
if(h.getName().startsWith("X-Account-Group-")){ |
269 |
GroupResource group = new GroupResource();
|
270 |
group.setName(h.getName().replaceAll("X-Account-Group-", "")); |
271 |
String users = h.getValue();
|
272 |
String[] us = users.split(","); |
273 |
for(String u : us){ |
274 |
group.getUsers().add(u); |
275 |
} |
276 |
groups.add(group); |
277 |
} |
278 |
} |
279 |
List<String> others = createOthersList(); |
280 |
if(others!=null) |
281 |
((AndroidCloudApplication)context.getApplicationContext()).setOthers(others); |
282 |
((AndroidCloudApplication)context.getApplicationContext()).setGroups(groups); |
283 |
BasicResponseHandler responseHandler = new BasicResponseHandler();
|
284 |
String body = responseHandler.handleResponse(resp);
|
285 |
Log.i("ContainerManager", body);
|
286 |
if (resp.getStatusLine().getStatusCode() == 200 |
287 |
|| resp.getStatusLine().getStatusCode() == 203) {
|
288 |
ContainerXMLParser containerXMLParser = new ContainerXMLParser();
|
289 |
SAXParser saxParser = SAXParserFactory.newInstance() |
290 |
.newSAXParser(); |
291 |
XMLReader xmlReader = saxParser.getXMLReader(); |
292 |
xmlReader.setContentHandler(containerXMLParser); |
293 |
|
294 |
xmlReader.parse(new InputSource(new StringReader(body))); |
295 |
containers = containerXMLParser.getContainers(); |
296 |
} else {
|
297 |
CloudServersFaultXMLParser parser = new CloudServersFaultXMLParser();
|
298 |
SAXParser saxParser = SAXParserFactory.newInstance() |
299 |
.newSAXParser(); |
300 |
XMLReader xmlReader = saxParser.getXMLReader(); |
301 |
xmlReader.setContentHandler(parser); |
302 |
xmlReader.parse(new InputSource(new StringReader(body))); |
303 |
CloudServersException cse = parser.getException(); |
304 |
throw cse;
|
305 |
} |
306 |
} catch (ClientProtocolException e) {
|
307 |
CloudServersException cse = new CloudServersException();
|
308 |
cse.setMessage(e.getLocalizedMessage()); |
309 |
throw cse;
|
310 |
} catch (IOException e) { |
311 |
CloudServersException cse = new CloudServersException();
|
312 |
cse.setMessage(e.getLocalizedMessage()); |
313 |
throw cse;
|
314 |
} catch (ParserConfigurationException e) { |
315 |
CloudServersException cse = new CloudServersException();
|
316 |
cse.setMessage(e.getLocalizedMessage()); |
317 |
throw cse;
|
318 |
} catch (SAXException e) {
|
319 |
CloudServersException cse = new CloudServersException();
|
320 |
cse.setMessage(e.getLocalizedMessage()); |
321 |
throw cse;
|
322 |
} catch (FactoryConfigurationError e) { |
323 |
CloudServersException cse = new CloudServersException();
|
324 |
cse.setMessage(e.getLocalizedMessage()); |
325 |
throw cse;
|
326 |
} |
327 |
|
328 |
return containers;
|
329 |
} |
330 |
|
331 |
|
332 |
public ArrayList<String> createOthersList() |
333 |
throws CloudServersException {
|
334 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
335 |
HttpGet get = new HttpGet(Account.getAccount().getStorageUrl().replaceAll(Account.getAccount().getUsername(), "") |
336 |
+ "?format=xml");
|
337 |
ArrayList<String> containers = new ArrayList<String>(); |
338 |
get.addHeader(CustomHttpClient.X_STORAGE_TOKEN, Account.getAccount() |
339 |
.getStorageToken()); |
340 |
get.addHeader("Content-Type", "application/xml"); |
341 |
|
342 |
try {
|
343 |
HttpResponse resp = httpclient.execute(get); |
344 |
BasicResponseHandler responseHandler = new BasicResponseHandler();
|
345 |
String body = responseHandler.handleResponse(resp);
|
346 |
Log.i("ContainerManager", body);
|
347 |
if (resp.getStatusLine().getStatusCode() == 200 |
348 |
|| resp.getStatusLine().getStatusCode() == 203) {
|
349 |
OthersXmlParser containerXMLParser = new OthersXmlParser();
|
350 |
SAXParser saxParser = SAXParserFactory.newInstance() |
351 |
.newSAXParser(); |
352 |
XMLReader xmlReader = saxParser.getXMLReader(); |
353 |
xmlReader.setContentHandler(containerXMLParser); |
354 |
|
355 |
xmlReader.parse(new InputSource(new StringReader(body))); |
356 |
containers = containerXMLParser.getContainers(); |
357 |
Log.i(LOG,containers.toString()); |
358 |
} else {
|
359 |
CloudServersFaultXMLParser parser = new CloudServersFaultXMLParser();
|
360 |
SAXParser saxParser = SAXParserFactory.newInstance() |
361 |
.newSAXParser(); |
362 |
XMLReader xmlReader = saxParser.getXMLReader(); |
363 |
xmlReader.setContentHandler(parser); |
364 |
xmlReader.parse(new InputSource(new StringReader(body))); |
365 |
CloudServersException cse = parser.getException(); |
366 |
throw cse;
|
367 |
} |
368 |
} catch (ClientProtocolException e) {
|
369 |
CloudServersException cse = new CloudServersException();
|
370 |
cse.setMessage(e.getLocalizedMessage()); |
371 |
throw cse;
|
372 |
} catch (IOException e) { |
373 |
CloudServersException cse = new CloudServersException();
|
374 |
cse.setMessage(e.getLocalizedMessage()); |
375 |
throw cse;
|
376 |
} catch (ParserConfigurationException e) { |
377 |
CloudServersException cse = new CloudServersException();
|
378 |
cse.setMessage(e.getLocalizedMessage()); |
379 |
throw cse;
|
380 |
} catch (SAXException e) {
|
381 |
CloudServersException cse = new CloudServersException();
|
382 |
cse.setMessage(e.getLocalizedMessage()); |
383 |
throw cse;
|
384 |
} catch (FactoryConfigurationError e) { |
385 |
CloudServersException cse = new CloudServersException();
|
386 |
cse.setMessage(e.getLocalizedMessage()); |
387 |
throw cse;
|
388 |
} |
389 |
|
390 |
return containers;
|
391 |
} |
392 |
|
393 |
private String getSafeURL(String badURL, String name) { |
394 |
URI uri = null; |
395 |
try {
|
396 |
uri = new URI("https", badURL.substring(8), "/" + name.toString() |
397 |
+ "/", ""); |
398 |
} catch (URISyntaxException e1) { |
399 |
// TODO Auto-generated catch block
|
400 |
e1.printStackTrace(); |
401 |
} |
402 |
String url = null; |
403 |
try {
|
404 |
url = uri.toURL().toString(); |
405 |
} catch (MalformedURLException e1) { |
406 |
// TODO Auto-generated catch block
|
407 |
e1.printStackTrace(); |
408 |
} |
409 |
return url.substring(0, url.length() - 1); |
410 |
} |
411 |
|
412 |
public ArrayList<Container> createOthersList(String user, boolean b) throws CloudServersException { |
413 |
Log.i(LOG,"Create List:"+Account.getAccount().getStorageUrl());
|
414 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
415 |
HttpGet get = new HttpGet(Account.getAccount().getStorageUrl().replaceAll(Account.getAccount().getUsername(), user)
|
416 |
+ "?format=xml");
|
417 |
ArrayList<Container> containers = new ArrayList<Container>(); |
418 |
get.addHeader(CustomHttpClient.X_STORAGE_TOKEN, Account.getAccount() |
419 |
.getStorageToken()); |
420 |
get.addHeader("Content-Type", "application/xml"); |
421 |
|
422 |
try {
|
423 |
HttpResponse resp = httpclient.execute(get); |
424 |
List<GroupResource> groups = new ArrayList<GroupResource>(); |
425 |
BasicResponseHandler responseHandler = new BasicResponseHandler();
|
426 |
String body = responseHandler.handleResponse(resp);
|
427 |
Log.i("ContainerManager", body);
|
428 |
if (resp.getStatusLine().getStatusCode() == 200 |
429 |
|| resp.getStatusLine().getStatusCode() == 203) {
|
430 |
ContainerXMLParser containerXMLParser = new ContainerXMLParser();
|
431 |
SAXParser saxParser = SAXParserFactory.newInstance() |
432 |
.newSAXParser(); |
433 |
XMLReader xmlReader = saxParser.getXMLReader(); |
434 |
xmlReader.setContentHandler(containerXMLParser); |
435 |
|
436 |
xmlReader.parse(new InputSource(new StringReader(body))); |
437 |
containers = containerXMLParser.getContainers(); |
438 |
for(Container c : containers){ |
439 |
c.setOtherUser(user); |
440 |
} |
441 |
} else {
|
442 |
CloudServersFaultXMLParser parser = new CloudServersFaultXMLParser();
|
443 |
SAXParser saxParser = SAXParserFactory.newInstance() |
444 |
.newSAXParser(); |
445 |
XMLReader xmlReader = saxParser.getXMLReader(); |
446 |
xmlReader.setContentHandler(parser); |
447 |
xmlReader.parse(new InputSource(new StringReader(body))); |
448 |
CloudServersException cse = parser.getException(); |
449 |
throw cse;
|
450 |
} |
451 |
} catch (ClientProtocolException e) {
|
452 |
CloudServersException cse = new CloudServersException();
|
453 |
cse.setMessage(e.getLocalizedMessage()); |
454 |
throw cse;
|
455 |
} catch (IOException e) { |
456 |
CloudServersException cse = new CloudServersException();
|
457 |
cse.setMessage(e.getLocalizedMessage()); |
458 |
throw cse;
|
459 |
} catch (ParserConfigurationException e) { |
460 |
CloudServersException cse = new CloudServersException();
|
461 |
cse.setMessage(e.getLocalizedMessage()); |
462 |
throw cse;
|
463 |
} catch (SAXException e) {
|
464 |
CloudServersException cse = new CloudServersException();
|
465 |
cse.setMessage(e.getLocalizedMessage()); |
466 |
throw cse;
|
467 |
} catch (FactoryConfigurationError e) { |
468 |
CloudServersException cse = new CloudServersException();
|
469 |
cse.setMessage(e.getLocalizedMessage()); |
470 |
throw cse;
|
471 |
} |
472 |
|
473 |
return containers;
|
474 |
} |
475 |
|
476 |
} |