root / src / com / rackspace / cloud / files / api / client / ContainerObjectManager.java @ 732d9c2c
History | View | Annotate | Download (16.9 kB)
1 |
package com.rackspace.cloud.files.api.client; |
---|---|
2 |
|
3 |
import java.io.IOException; |
4 |
import java.io.StringReader; |
5 |
import java.io.UnsupportedEncodingException; |
6 |
import java.net.MalformedURLException; |
7 |
import java.net.URI; |
8 |
import java.net.URISyntaxException; |
9 |
import java.util.ArrayList; |
10 |
import java.util.List; |
11 |
import java.util.Map; |
12 |
import java.util.Map.Entry; |
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.HttpResponse; |
20 |
import org.apache.http.client.ClientProtocolException; |
21 |
import org.apache.http.client.methods.HttpDelete; |
22 |
import org.apache.http.client.methods.HttpGet; |
23 |
import org.apache.http.client.methods.HttpPost; |
24 |
import org.apache.http.client.methods.HttpPut; |
25 |
import org.apache.http.entity.StringEntity; |
26 |
import org.apache.http.impl.client.BasicResponseHandler; |
27 |
import org.apache.http.protocol.RequestExpectContinue; |
28 |
import org.xml.sax.InputSource; |
29 |
import org.xml.sax.SAXException; |
30 |
import org.xml.sax.XMLReader; |
31 |
|
32 |
import android.content.Context; |
33 |
import android.util.Log; |
34 |
|
35 |
import com.rackspace.cloud.files.api.client.parsers.ContainerObjectXMLparser; |
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 |
*
|
44 |
* @author Phillip Toohill
|
45 |
*
|
46 |
*/
|
47 |
public class ContainerObjectManager extends EntityManager { |
48 |
|
49 |
public String LOG = "ContainerObjectManager"; |
50 |
private Context context; |
51 |
public static final String storageToken = Account.getAccount() |
52 |
.getStorageToken(); |
53 |
|
54 |
public ContainerObjectManager(Context context) { |
55 |
this.context = context;
|
56 |
} |
57 |
|
58 |
public ArrayList<ContainerObjects> createList(boolean detail, |
59 |
String passName) throws CloudServersException { |
60 |
Log.i(LOG,"Create List:"+passName);
|
61 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
62 |
String url = getSafeURL(Account.getAccount().getStorageUrl(), passName)
|
63 |
+ "?format=xml";
|
64 |
HttpGet get = new HttpGet(url);
|
65 |
ArrayList<ContainerObjects> files = new ArrayList<ContainerObjects>(); |
66 |
|
67 |
get.addHeader("Content-Type", "application/xml"); |
68 |
get.addHeader(CustomHttpClient.X_STORAGE_TOKEN, storageToken); |
69 |
|
70 |
try {
|
71 |
HttpResponse resp = httpclient.execute(get); |
72 |
BasicResponseHandler responseHandler = new BasicResponseHandler();
|
73 |
String body = responseHandler.handleResponse(resp);
|
74 |
|
75 |
if (resp.getStatusLine().getStatusCode() == 200 |
76 |
|| resp.getStatusLine().getStatusCode() == 203) {
|
77 |
ContainerObjectXMLparser filesXMLParser = new ContainerObjectXMLparser();
|
78 |
SAXParser saxParser = SAXParserFactory.newInstance() |
79 |
.newSAXParser(); |
80 |
XMLReader xmlReader = saxParser.getXMLReader(); |
81 |
xmlReader.setContentHandler(filesXMLParser); |
82 |
|
83 |
xmlReader.parse(new InputSource(new StringReader(body))); |
84 |
files = filesXMLParser.getViewFiles(); |
85 |
for(ContainerObjects o :files)
|
86 |
o.setContainerName(passName); |
87 |
|
88 |
} else {
|
89 |
CloudServersFaultXMLParser parser = new CloudServersFaultXMLParser();
|
90 |
SAXParser saxParser = SAXParserFactory.newInstance() |
91 |
.newSAXParser(); |
92 |
XMLReader xmlReader = saxParser.getXMLReader(); |
93 |
xmlReader.setContentHandler(parser); |
94 |
xmlReader.parse(new InputSource(new StringReader(body))); |
95 |
CloudServersException cse = parser.getException(); |
96 |
throw cse;
|
97 |
} |
98 |
} catch (ClientProtocolException e) {
|
99 |
CloudServersException cse = new CloudServersException();
|
100 |
cse.setMessage(e.getLocalizedMessage()); |
101 |
throw cse;
|
102 |
} catch (IOException e) { |
103 |
CloudServersException cse = new CloudServersException();
|
104 |
cse.setMessage(e.getLocalizedMessage()); |
105 |
throw cse;
|
106 |
} catch (ParserConfigurationException e) { |
107 |
CloudServersException cse = new CloudServersException();
|
108 |
cse.setMessage(e.getLocalizedMessage()); |
109 |
throw cse;
|
110 |
} catch (SAXException e) {
|
111 |
CloudServersException cse = new CloudServersException();
|
112 |
cse.setMessage(e.getLocalizedMessage()); |
113 |
throw cse;
|
114 |
} catch (FactoryConfigurationError e) { |
115 |
CloudServersException cse = new CloudServersException();
|
116 |
cse.setMessage(e.getLocalizedMessage()); |
117 |
throw cse;
|
118 |
} |
119 |
return files;
|
120 |
|
121 |
} |
122 |
|
123 |
public ArrayList<ContainerObjects> createOtherList(boolean detail, |
124 |
String passName, String user) throws CloudServersException { |
125 |
Log.i(LOG,"Create List:"+passName);
|
126 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
127 |
String url = getSafeURL(Account.getAccount().getStorageUrl().replaceAll(Account.getAccount().getUsername(), user), passName)
|
128 |
+ "?format=xml";
|
129 |
HttpGet get = new HttpGet(url);
|
130 |
ArrayList<ContainerObjects> files = new ArrayList<ContainerObjects>(); |
131 |
|
132 |
get.addHeader("Content-Type", "application/xml"); |
133 |
get.addHeader(CustomHttpClient.X_STORAGE_TOKEN, storageToken); |
134 |
|
135 |
try {
|
136 |
HttpResponse resp = httpclient.execute(get); |
137 |
BasicResponseHandler responseHandler = new BasicResponseHandler();
|
138 |
String body = responseHandler.handleResponse(resp);
|
139 |
|
140 |
if (resp.getStatusLine().getStatusCode() == 200 |
141 |
|| resp.getStatusLine().getStatusCode() == 203) {
|
142 |
ContainerObjectXMLparser filesXMLParser = new ContainerObjectXMLparser();
|
143 |
SAXParser saxParser = SAXParserFactory.newInstance() |
144 |
.newSAXParser(); |
145 |
XMLReader xmlReader = saxParser.getXMLReader(); |
146 |
xmlReader.setContentHandler(filesXMLParser); |
147 |
|
148 |
xmlReader.parse(new InputSource(new StringReader(body))); |
149 |
files = filesXMLParser.getViewFiles(); |
150 |
for(ContainerObjects o :files)
|
151 |
o.setContainerName(passName); |
152 |
|
153 |
} else {
|
154 |
CloudServersFaultXMLParser parser = new CloudServersFaultXMLParser();
|
155 |
SAXParser saxParser = SAXParserFactory.newInstance() |
156 |
.newSAXParser(); |
157 |
XMLReader xmlReader = saxParser.getXMLReader(); |
158 |
xmlReader.setContentHandler(parser); |
159 |
xmlReader.parse(new InputSource(new StringReader(body))); |
160 |
CloudServersException cse = parser.getException(); |
161 |
throw cse;
|
162 |
} |
163 |
} catch (ClientProtocolException e) {
|
164 |
CloudServersException cse = new CloudServersException();
|
165 |
cse.setMessage(e.getLocalizedMessage()); |
166 |
throw cse;
|
167 |
} catch (IOException e) { |
168 |
CloudServersException cse = new CloudServersException();
|
169 |
cse.setMessage(e.getLocalizedMessage()); |
170 |
throw cse;
|
171 |
} catch (ParserConfigurationException e) { |
172 |
CloudServersException cse = new CloudServersException();
|
173 |
cse.setMessage(e.getLocalizedMessage()); |
174 |
throw cse;
|
175 |
} catch (SAXException e) {
|
176 |
CloudServersException cse = new CloudServersException();
|
177 |
cse.setMessage(e.getLocalizedMessage()); |
178 |
throw cse;
|
179 |
} catch (FactoryConfigurationError e) { |
180 |
CloudServersException cse = new CloudServersException();
|
181 |
cse.setMessage(e.getLocalizedMessage()); |
182 |
throw cse;
|
183 |
} |
184 |
return files;
|
185 |
|
186 |
} |
187 |
|
188 |
public ArrayList<ContainerObjects> createList(boolean detail, |
189 |
String passName, String prefix) throws CloudServersException { |
190 |
|
191 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
192 |
String url = getSafeURL(Account.getAccount().getStorageUrl(), passName)
|
193 |
+ "?format=xml&prefix="+prefix;
|
194 |
HttpGet get = new HttpGet(url);
|
195 |
ArrayList<ContainerObjects> files = new ArrayList<ContainerObjects>(); |
196 |
|
197 |
get.addHeader("Content-Type", "application/xml"); |
198 |
get.addHeader(CustomHttpClient.X_STORAGE_TOKEN, storageToken); |
199 |
|
200 |
try {
|
201 |
HttpResponse resp = httpclient.execute(get); |
202 |
BasicResponseHandler responseHandler = new BasicResponseHandler();
|
203 |
String body = responseHandler.handleResponse(resp);
|
204 |
|
205 |
if (resp.getStatusLine().getStatusCode() == 200 |
206 |
|| resp.getStatusLine().getStatusCode() == 203) {
|
207 |
ContainerObjectXMLparser filesXMLParser = new ContainerObjectXMLparser();
|
208 |
SAXParser saxParser = SAXParserFactory.newInstance() |
209 |
.newSAXParser(); |
210 |
XMLReader xmlReader = saxParser.getXMLReader(); |
211 |
xmlReader.setContentHandler(filesXMLParser); |
212 |
|
213 |
xmlReader.parse(new InputSource(new StringReader(body))); |
214 |
files = filesXMLParser.getViewFiles(); |
215 |
for(ContainerObjects o :files)
|
216 |
o.setContainerName(passName); |
217 |
|
218 |
} else {
|
219 |
CloudServersFaultXMLParser parser = new CloudServersFaultXMLParser();
|
220 |
SAXParser saxParser = SAXParserFactory.newInstance() |
221 |
.newSAXParser(); |
222 |
XMLReader xmlReader = saxParser.getXMLReader(); |
223 |
xmlReader.setContentHandler(parser); |
224 |
xmlReader.parse(new InputSource(new StringReader(body))); |
225 |
CloudServersException cse = parser.getException(); |
226 |
throw cse;
|
227 |
} |
228 |
} catch (ClientProtocolException e) {
|
229 |
CloudServersException cse = new CloudServersException();
|
230 |
cse.setMessage(e.getLocalizedMessage()); |
231 |
throw cse;
|
232 |
} catch (IOException e) { |
233 |
CloudServersException cse = new CloudServersException();
|
234 |
cse.setMessage(e.getLocalizedMessage()); |
235 |
throw cse;
|
236 |
} catch (ParserConfigurationException e) { |
237 |
CloudServersException cse = new CloudServersException();
|
238 |
cse.setMessage(e.getLocalizedMessage()); |
239 |
throw cse;
|
240 |
} catch (SAXException e) {
|
241 |
CloudServersException cse = new CloudServersException();
|
242 |
cse.setMessage(e.getLocalizedMessage()); |
243 |
throw cse;
|
244 |
} catch (FactoryConfigurationError e) { |
245 |
CloudServersException cse = new CloudServersException();
|
246 |
cse.setMessage(e.getLocalizedMessage()); |
247 |
throw cse;
|
248 |
} |
249 |
return files;
|
250 |
|
251 |
} |
252 |
|
253 |
public ArrayList<ContainerObjects> createListMyShared(boolean detail, |
254 |
String passName, List<Container> containers) throws CloudServersException { |
255 |
|
256 |
ArrayList<ContainerObjects> files = new ArrayList<ContainerObjects>(); |
257 |
for(Container con :containers ){ |
258 |
Log.i(LOG,"GEETING FILES OF COntainer:"+con.getName());
|
259 |
if(con.getName().equalsIgnoreCase("trash")||con.getName().equals(Container.MYSHARED)||con.getName().equals(Container.OTHERS)){} |
260 |
else
|
261 |
try{
|
262 |
ArrayList<ContainerObjects> temp = createList(detail, con.getName());
|
263 |
for(ContainerObjects o : temp){
|
264 |
Log.i(LOG,o.getCName()+" "+o.isShared());
|
265 |
if(o.isShared()){
|
266 |
o.setContainerName(Container.MYSHARED);
|
267 |
files.add(o); |
268 |
} |
269 |
} |
270 |
} |
271 |
catch(CloudServersException e){
|
272 |
|
273 |
} |
274 |
} |
275 |
return files;
|
276 |
|
277 |
} |
278 |
|
279 |
public HttpBundle deleteObject(String Container, String Object) |
280 |
throws CloudServersException {
|
281 |
HttpResponse resp = null;
|
282 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
283 |
String url = getSafeURL(Account.getAccount().getStorageUrl(), Container |
284 |
+ "/" + Object); |
285 |
HttpDelete deleteObject = new HttpDelete(url);
|
286 |
|
287 |
deleteObject.addHeader("X-Auth-Token", Account.getAccount()
|
288 |
.getAuthToken()); |
289 |
httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class); |
290 |
|
291 |
HttpBundle bundle = new HttpBundle();
|
292 |
bundle.setCurlRequest(deleteObject); |
293 |
|
294 |
try {
|
295 |
resp = httpclient.execute(deleteObject); |
296 |
bundle.setHttpResponse(resp); |
297 |
} catch (ClientProtocolException e) {
|
298 |
CloudServersException cse = new CloudServersException();
|
299 |
cse.setMessage(e.getLocalizedMessage()); |
300 |
throw cse;
|
301 |
} catch (IOException e) { |
302 |
CloudServersException cse = new CloudServersException();
|
303 |
cse.setMessage(e.getLocalizedMessage()); |
304 |
throw cse;
|
305 |
} catch (FactoryConfigurationError e) { |
306 |
CloudServersException cse = new CloudServersException();
|
307 |
cse.setMessage(e.getLocalizedMessage()); |
308 |
throw cse;
|
309 |
} |
310 |
return bundle;
|
311 |
} |
312 |
|
313 |
public HttpBundle getObject(String Container, String Object) |
314 |
throws CloudServersException {
|
315 |
HttpResponse resp = null;
|
316 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
317 |
String url = getSafeURL(Account.getAccount().getStorageUrl(), Container |
318 |
+ "/" + Object); |
319 |
HttpGet getObject = new HttpGet(url);
|
320 |
getObject |
321 |
.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
|
322 |
httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class); |
323 |
|
324 |
HttpBundle bundle = new HttpBundle();
|
325 |
bundle.setCurlRequest(getObject); |
326 |
|
327 |
try {
|
328 |
resp = httpclient.execute(getObject); |
329 |
bundle.setHttpResponse(resp); |
330 |
} catch (ClientProtocolException e) {
|
331 |
CloudServersException cse = new CloudServersException();
|
332 |
cse.setMessage(e.getLocalizedMessage()); |
333 |
throw cse;
|
334 |
} catch (IOException e) { |
335 |
CloudServersException cse = new CloudServersException();
|
336 |
cse.setMessage(e.getLocalizedMessage()); |
337 |
throw cse;
|
338 |
} catch (FactoryConfigurationError e) { |
339 |
CloudServersException cse = new CloudServersException();
|
340 |
cse.setMessage(e.getLocalizedMessage()); |
341 |
throw cse;
|
342 |
} |
343 |
return bundle;
|
344 |
} |
345 |
|
346 |
public HttpBundle addObject(String Container, String Path, String Object, |
347 |
String type) throws CloudServersException { |
348 |
HttpResponse resp = null;
|
349 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
350 |
String url = getSafeURL(Account.getAccount().getStorageUrl(), Container |
351 |
+ "/" + Path + Object); |
352 |
HttpPut addObject = new HttpPut(url);
|
353 |
|
354 |
addObject |
355 |
.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
|
356 |
addObject.addHeader("Content-Type", type);
|
357 |
httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class); |
358 |
|
359 |
HttpBundle bundle = new HttpBundle();
|
360 |
bundle.setCurlRequest(addObject); |
361 |
|
362 |
try {
|
363 |
resp = httpclient.execute(addObject); |
364 |
bundle.setHttpResponse(resp); |
365 |
} catch (ClientProtocolException e) {
|
366 |
CloudServersException cse = new CloudServersException();
|
367 |
cse.setMessage(e.getLocalizedMessage()); |
368 |
throw cse;
|
369 |
} catch (IOException e) { |
370 |
CloudServersException cse = new CloudServersException();
|
371 |
cse.setMessage(e.getLocalizedMessage()); |
372 |
throw cse;
|
373 |
} catch (FactoryConfigurationError e) { |
374 |
CloudServersException cse = new CloudServersException();
|
375 |
cse.setMessage(e.getLocalizedMessage()); |
376 |
throw cse;
|
377 |
} |
378 |
return bundle;
|
379 |
} |
380 |
|
381 |
public HttpBundle updateObject(String Container, String Path, String Object, |
382 |
String type, Map<String,String> headers) throws CloudServersException { |
383 |
HttpResponse resp = null;
|
384 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
385 |
String url;
|
386 |
if(Container !=null) |
387 |
url = getSafeURL(Account.getAccount().getStorageUrl(), Container
|
388 |
+ "/" + Path + Object); |
389 |
else{
|
390 |
url = getSafeURL(Account.getAccount().getStorageUrl(),"");
|
391 |
if(url.endsWith("/")) |
392 |
url = url.substring(0, url.length()-1); |
393 |
} |
394 |
url =url+"?update=";
|
395 |
Log.d("papala", url);
|
396 |
HttpPost addObject = new HttpPost(url);
|
397 |
|
398 |
addObject |
399 |
.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
|
400 |
addObject.addHeader("Content-Type", " text/plain; charset=UTF-8"); |
401 |
for(Entry<String,String> entry : headers.entrySet()){ |
402 |
addObject.addHeader(entry.getKey(),entry.getValue()); |
403 |
} |
404 |
httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class); |
405 |
|
406 |
HttpBundle bundle = new HttpBundle();
|
407 |
bundle.setCurlRequest(addObject); |
408 |
|
409 |
try {
|
410 |
resp = httpclient.execute(addObject); |
411 |
bundle.setHttpResponse(resp); |
412 |
} catch (ClientProtocolException e) {
|
413 |
CloudServersException cse = new CloudServersException();
|
414 |
cse.setMessage(e.getLocalizedMessage()); |
415 |
throw cse;
|
416 |
} catch (IOException e) { |
417 |
CloudServersException cse = new CloudServersException();
|
418 |
cse.setMessage(e.getLocalizedMessage()); |
419 |
throw cse;
|
420 |
} catch (FactoryConfigurationError e) { |
421 |
CloudServersException cse = new CloudServersException();
|
422 |
cse.setMessage(e.getLocalizedMessage()); |
423 |
throw cse;
|
424 |
} |
425 |
return bundle;
|
426 |
} |
427 |
|
428 |
/*
|
429 |
* used for adding text files, requires an extra parameter to store the data
|
430 |
* for the file
|
431 |
*/
|
432 |
public HttpBundle addObject(String Container, String Path, String Object, |
433 |
String type, String data) throws CloudServersException { |
434 |
HttpResponse resp = null;
|
435 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
436 |
String url = getSafeURL(Account.getAccount().getStorageUrl(), Container |
437 |
+ "/" + Path + Object); |
438 |
HttpPut addObject = new HttpPut(url);
|
439 |
|
440 |
addObject |
441 |
.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
|
442 |
addObject.addHeader("Content-Type", type);
|
443 |
httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class); |
444 |
|
445 |
StringEntity tmp = null;
|
446 |
try {
|
447 |
tmp = new StringEntity(data);
|
448 |
} catch (UnsupportedEncodingException e) { |
449 |
CloudServersException cse = new CloudServersException();
|
450 |
cse.setMessage(e.getLocalizedMessage()); |
451 |
throw cse;
|
452 |
} |
453 |
addObject.setEntity(tmp); |
454 |
|
455 |
HttpBundle bundle = new HttpBundle();
|
456 |
bundle.setCurlRequest(addObject); |
457 |
|
458 |
try {
|
459 |
resp = httpclient.execute(addObject); |
460 |
bundle.setHttpResponse(resp); |
461 |
} catch (ClientProtocolException e) {
|
462 |
CloudServersException cse = new CloudServersException();
|
463 |
cse.setMessage(e.getLocalizedMessage()); |
464 |
throw cse;
|
465 |
} catch (IOException e) { |
466 |
CloudServersException cse = new CloudServersException();
|
467 |
cse.setMessage(e.getLocalizedMessage()); |
468 |
throw cse;
|
469 |
} catch (FactoryConfigurationError e) { |
470 |
CloudServersException cse = new CloudServersException();
|
471 |
cse.setMessage(e.getLocalizedMessage()); |
472 |
throw cse;
|
473 |
} |
474 |
return bundle;
|
475 |
} |
476 |
|
477 |
private String getSafeURL(String badURL, String name) { |
478 |
URI uri = null; |
479 |
try {
|
480 |
uri = new URI("https", badURL.substring(8), "/" + name + "/", ""); |
481 |
} catch (URISyntaxException e1) { |
482 |
// TODO Auto-generated catch block
|
483 |
e1.printStackTrace(); |
484 |
} |
485 |
String url = null; |
486 |
try {
|
487 |
url = uri.toURL().toString(); |
488 |
} catch (MalformedURLException e1) { |
489 |
// TODO Auto-generated catch block
|
490 |
e1.printStackTrace(); |
491 |
} |
492 |
return url.substring(0, url.length() - 2); |
493 |
} |
494 |
|
495 |
} |