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