root / src / com / rackspace / cloud / files / api / client / ContainerObjectManager.java @ 378fe36a
History | View | Annotate | Download (28.1 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.io.UnsupportedEncodingException; |
7 |
import java.net.MalformedURLException; |
8 |
import java.net.URI; |
9 |
import java.net.URISyntaxException; |
10 |
import java.net.URLEncoder; |
11 |
import java.util.ArrayList; |
12 |
import java.util.HashMap; |
13 |
import java.util.Iterator; |
14 |
import java.util.List; |
15 |
import java.util.Map; |
16 |
import java.util.Map.Entry; |
17 |
import java.util.StringTokenizer; |
18 |
|
19 |
import javax.xml.parsers.FactoryConfigurationError; |
20 |
import javax.xml.parsers.ParserConfigurationException; |
21 |
import javax.xml.parsers.SAXParser; |
22 |
import javax.xml.parsers.SAXParserFactory; |
23 |
|
24 |
import org.apache.http.Header; |
25 |
import org.apache.http.HttpResponse; |
26 |
import org.apache.http.client.ClientProtocolException; |
27 |
import org.apache.http.client.methods.HttpDelete; |
28 |
import org.apache.http.client.methods.HttpGet; |
29 |
import org.apache.http.client.methods.HttpHead; |
30 |
import org.apache.http.client.methods.HttpPost; |
31 |
import org.apache.http.client.methods.HttpPut; |
32 |
import org.apache.http.client.methods.HttpRequestBase; |
33 |
import org.apache.http.entity.FileEntity; |
34 |
import org.apache.http.entity.StringEntity; |
35 |
import org.apache.http.impl.client.BasicResponseHandler; |
36 |
import org.apache.http.protocol.RequestExpectContinue; |
37 |
import org.json.JSONArray; |
38 |
import org.json.JSONException; |
39 |
import org.json.JSONObject; |
40 |
import org.xml.sax.InputSource; |
41 |
import org.xml.sax.SAXException; |
42 |
import org.xml.sax.XMLReader; |
43 |
|
44 |
import android.content.Context; |
45 |
import android.util.Log; |
46 |
|
47 |
import com.rackspace.cloud.android.AndroidCloudApplication; |
48 |
import com.rackspace.cloud.android.Preferences; |
49 |
import com.rackspace.cloud.files.api.client.parsers.ContainerObjectXMLparser; |
50 |
import com.rackspace.cloud.files.api.client.parsers.ObjectVersionXMLParser; |
51 |
import com.rackspace.cloud.servers.api.client.Account; |
52 |
import com.rackspace.cloud.servers.api.client.CloudServersException; |
53 |
import com.rackspace.cloud.servers.api.client.EntityManager; |
54 |
import com.rackspace.cloud.servers.api.client.http.HttpBundle; |
55 |
import com.rackspace.cloud.servers.api.client.parsers.CloudServersFaultXMLParser; |
56 |
|
57 |
/**
|
58 |
*
|
59 |
* @author Phillip Toohill
|
60 |
*
|
61 |
*/
|
62 |
public class ContainerObjectManager extends EntityManager { |
63 |
public final static int GET = 0; |
64 |
public final static int PUT = 1; |
65 |
public final static int POST = 2; |
66 |
public final static int DELETE = 3; |
67 |
public String LOG = "ContainerObjectManager"; |
68 |
private Context context; |
69 |
/*
|
70 |
* public static final String storageToken = Account.getAccount()
|
71 |
* .getStorageToken();
|
72 |
*/
|
73 |
private String storageToken; |
74 |
|
75 |
public ContainerObjectManager(Context context) { |
76 |
this.context = context;
|
77 |
if (Account.getAccount() != null) |
78 |
this.storageToken = Account.getAccount().getStorageToken();
|
79 |
} |
80 |
|
81 |
public ContainerObjects executeHead(String containerName, String cname) |
82 |
throws CloudServersException {
|
83 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
84 |
String url = getSafeURL(Account.getAccount().getStorageUrl() + "/" |
85 |
+ containerName, cname) |
86 |
+ "?format=xml";
|
87 |
Log.i(LOG, url); |
88 |
HttpHead get = new HttpHead(url);
|
89 |
get.addHeader("Content-Type", "application/xml"); |
90 |
get.addHeader(CustomHttpClient.X_STORAGE_TOKEN, storageToken); |
91 |
|
92 |
try {
|
93 |
HttpResponse resp = httpclient.execute(get); |
94 |
BasicResponseHandler responseHandler = new BasicResponseHandler();
|
95 |
String body = responseHandler.handleResponse(resp);
|
96 |
|
97 |
if (resp.getStatusLine().getStatusCode() == 200 |
98 |
|| resp.getStatusLine().getStatusCode() == 203) {
|
99 |
|
100 |
ContainerObjects objects = new ContainerObjects();
|
101 |
for (Header h : resp.getAllHeaders()) {
|
102 |
if (h.getName().equals("Content-Type")) |
103 |
objects.setContentType(h.getValue()); |
104 |
else if (h.getName().equals("X-Object-Public")) |
105 |
objects.setIsPublic(h.getValue()); |
106 |
else if (h.getName().equals("Content-Length")) |
107 |
objects.setBytes(Integer.parseInt(h.getValue()));
|
108 |
else if (h.getName().equals("X-Object-Public")) |
109 |
objects.setIsPublic(h.getValue()); |
110 |
else if (h.getName().equals("X-Object-Modified-By")) |
111 |
objects.setModifiedBy(h.getValue()); |
112 |
else if (h.getName().equals("X-Object-UUID")) |
113 |
objects.setObjectUUID(h.getValue()); |
114 |
else if (h.getName().equals("X-Object-Hash")) |
115 |
objects.setObjectHash(h.getValue()); |
116 |
else if (h.getName().equals("X-Object-Version")) |
117 |
objects.setVersion(h.getValue()); |
118 |
else if (h.getName().equals("X-Object-Version-Timestamp")) |
119 |
objects.setVersionTimestamp(h.getValue()); |
120 |
else if (h.getName().equals("X-Object-Sharing")) |
121 |
objects.setObjectSharing(h.getValue()); |
122 |
else if (h.getName().equals("Last-Modified")) |
123 |
objects.setLastMod(h.getValue()); |
124 |
else if (h.getName().startsWith("X-Object-Meta")) { |
125 |
if (objects.getMetadata() == null) |
126 |
objects.setMetadata(new HashMap<String, String>()); |
127 |
objects.getMetadata().put( |
128 |
h.getName().replaceAll("X-Object-Meta-", ""), |
129 |
h.getValue()); |
130 |
} |
131 |
|
132 |
} |
133 |
objects.setContainerName(containerName); |
134 |
objects.setCName(cname); |
135 |
return objects;
|
136 |
|
137 |
} else {
|
138 |
CloudServersFaultXMLParser parser = new CloudServersFaultXMLParser();
|
139 |
SAXParser saxParser = SAXParserFactory.newInstance() |
140 |
.newSAXParser(); |
141 |
XMLReader xmlReader = saxParser.getXMLReader(); |
142 |
xmlReader.setContentHandler(parser); |
143 |
xmlReader.parse(new InputSource(new StringReader(body))); |
144 |
CloudServersException cse = parser.getException(); |
145 |
throw cse;
|
146 |
} |
147 |
} catch (ClientProtocolException e) {
|
148 |
CloudServersException cse = new CloudServersException();
|
149 |
cse.setMessage(e.getLocalizedMessage()); |
150 |
throw cse;
|
151 |
} catch (IOException e) { |
152 |
CloudServersException cse = new CloudServersException();
|
153 |
cse.setMessage(e.getLocalizedMessage()); |
154 |
throw cse;
|
155 |
} catch (ParserConfigurationException e) { |
156 |
CloudServersException cse = new CloudServersException();
|
157 |
cse.setMessage(e.getLocalizedMessage()); |
158 |
throw cse;
|
159 |
} catch (SAXException e) {
|
160 |
CloudServersException cse = new CloudServersException();
|
161 |
cse.setMessage(e.getLocalizedMessage()); |
162 |
throw cse;
|
163 |
} catch (FactoryConfigurationError e) { |
164 |
CloudServersException cse = new CloudServersException();
|
165 |
cse.setMessage(e.getLocalizedMessage()); |
166 |
throw cse;
|
167 |
} |
168 |
|
169 |
} |
170 |
|
171 |
private String encodePath(String cname) { |
172 |
String res = ""; |
173 |
StringTokenizer str = new StringTokenizer(cname, "/"); |
174 |
while (str.hasMoreTokens()) {
|
175 |
if (!"".equals(res)) |
176 |
res = res + "/";
|
177 |
res = res + URLEncoder.encode(str.nextToken());
|
178 |
} |
179 |
return res;
|
180 |
|
181 |
} |
182 |
|
183 |
public ContainerObjects executeHead(String containerName, String cname, |
184 |
String otherUser) throws CloudServersException { |
185 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
186 |
String url = getSafeURL(Account.getAccount().getStorageUrl() + "/" |
187 |
+ containerName, cname) |
188 |
+ "?format=xml";
|
189 |
url = url.replaceAll(Account.getAccount().getUsername(), otherUser); |
190 |
Log.i(LOG, url); |
191 |
HttpHead get = new HttpHead(url);
|
192 |
get.addHeader("Content-Type", "application/xml"); |
193 |
get.addHeader(CustomHttpClient.X_STORAGE_TOKEN, storageToken); |
194 |
|
195 |
try {
|
196 |
HttpResponse resp = httpclient.execute(get); |
197 |
BasicResponseHandler responseHandler = new BasicResponseHandler();
|
198 |
String body = responseHandler.handleResponse(resp);
|
199 |
|
200 |
if (resp.getStatusLine().getStatusCode() == 200 |
201 |
|| resp.getStatusLine().getStatusCode() == 203) {
|
202 |
|
203 |
ContainerObjects objects = new ContainerObjects();
|
204 |
for (Header h : resp.getAllHeaders()) {
|
205 |
if (h.getName().equals("Content-Type")) |
206 |
objects.setContentType(h.getValue()); |
207 |
else if (h.getName().equals("X-Object-Public")) |
208 |
objects.setIsPublic(h.getValue()); |
209 |
else if (h.getName().equals("Content-Length")) |
210 |
objects.setBytes(Integer.parseInt(h.getValue()));
|
211 |
else if (h.getName().equals("X-Object-Public")) |
212 |
objects.setIsPublic(h.getValue()); |
213 |
else if (h.getName().equals("X-Object-Modified-By")) |
214 |
objects.setModifiedBy(h.getValue()); |
215 |
else if (h.getName().equals("X-Object-UUID")) |
216 |
objects.setObjectUUID(h.getValue()); |
217 |
else if (h.getName().equals("X-Object-Hash")) |
218 |
objects.setObjectHash(h.getValue()); |
219 |
else if (h.getName().equals("X-Object-Version")) |
220 |
objects.setVersion(h.getValue()); |
221 |
else if (h.getName().equals("X-Object-Version-Timestamp")) |
222 |
objects.setVersionTimestamp(h.getValue()); |
223 |
else if (h.getName().equals("X-Object-Sharing")) |
224 |
objects.setObjectSharing(h.getValue()); |
225 |
else if (h.getName().equals("Last-Modified")) |
226 |
objects.setLastMod(h.getValue()); |
227 |
else if (h.getName().startsWith("X-Object-Meta")) { |
228 |
if (objects.getMetadata() == null) |
229 |
objects.setMetadata(new HashMap<String, String>()); |
230 |
objects.getMetadata().put( |
231 |
h.getName().replaceAll("X-Object-Meta-", ""), |
232 |
h.getValue()); |
233 |
} |
234 |
|
235 |
} |
236 |
objects.setContainerName(containerName); |
237 |
objects.setCName(cname); |
238 |
return objects;
|
239 |
|
240 |
} else {
|
241 |
CloudServersFaultXMLParser parser = new CloudServersFaultXMLParser();
|
242 |
SAXParser saxParser = SAXParserFactory.newInstance() |
243 |
.newSAXParser(); |
244 |
XMLReader xmlReader = saxParser.getXMLReader(); |
245 |
xmlReader.setContentHandler(parser); |
246 |
xmlReader.parse(new InputSource(new StringReader(body))); |
247 |
CloudServersException cse = parser.getException(); |
248 |
throw cse;
|
249 |
} |
250 |
} catch (ClientProtocolException e) {
|
251 |
CloudServersException cse = new CloudServersException();
|
252 |
cse.setMessage(e.getLocalizedMessage()); |
253 |
throw cse;
|
254 |
} catch (IOException e) { |
255 |
CloudServersException cse = new CloudServersException();
|
256 |
cse.setMessage(e.getLocalizedMessage()); |
257 |
throw cse;
|
258 |
} catch (ParserConfigurationException e) { |
259 |
CloudServersException cse = new CloudServersException();
|
260 |
cse.setMessage(e.getLocalizedMessage()); |
261 |
throw cse;
|
262 |
} catch (SAXException e) {
|
263 |
CloudServersException cse = new CloudServersException();
|
264 |
cse.setMessage(e.getLocalizedMessage()); |
265 |
throw cse;
|
266 |
} catch (FactoryConfigurationError e) { |
267 |
CloudServersException cse = new CloudServersException();
|
268 |
cse.setMessage(e.getLocalizedMessage()); |
269 |
throw cse;
|
270 |
} |
271 |
|
272 |
} |
273 |
|
274 |
private List<ContainerObjects> executeGet(String containerName, String url) |
275 |
throws CloudServersException {
|
276 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
277 |
HttpGet get = new HttpGet(url);
|
278 |
ArrayList<ContainerObjects> files = new ArrayList<ContainerObjects>(); |
279 |
|
280 |
get.addHeader("Content-Type", "application/xml"); |
281 |
get.addHeader(CustomHttpClient.X_STORAGE_TOKEN, storageToken); |
282 |
|
283 |
try {
|
284 |
HttpResponse resp = httpclient.execute(get); |
285 |
BasicResponseHandler responseHandler = new BasicResponseHandler();
|
286 |
String body = responseHandler.handleResponse(resp);
|
287 |
|
288 |
if (resp.getStatusLine().getStatusCode() == 200 |
289 |
|| resp.getStatusLine().getStatusCode() == 203) {
|
290 |
ContainerObjectXMLparser filesXMLParser = new ContainerObjectXMLparser();
|
291 |
SAXParser saxParser = SAXParserFactory.newInstance() |
292 |
.newSAXParser(); |
293 |
XMLReader xmlReader = saxParser.getXMLReader(); |
294 |
xmlReader.setContentHandler(filesXMLParser); |
295 |
|
296 |
xmlReader.parse(new InputSource(new StringReader(body))); |
297 |
files = filesXMLParser.getViewFiles(); |
298 |
for (ContainerObjects o : files)
|
299 |
o.setContainerName(containerName); |
300 |
} else {
|
301 |
CloudServersFaultXMLParser parser = new CloudServersFaultXMLParser();
|
302 |
SAXParser saxParser = SAXParserFactory.newInstance() |
303 |
.newSAXParser(); |
304 |
XMLReader xmlReader = saxParser.getXMLReader(); |
305 |
xmlReader.setContentHandler(parser); |
306 |
xmlReader.parse(new InputSource(new StringReader(body))); |
307 |
CloudServersException cse = parser.getException(); |
308 |
throw cse;
|
309 |
} |
310 |
} catch (ClientProtocolException e) {
|
311 |
CloudServersException cse = new CloudServersException();
|
312 |
cse.setMessage(e.getLocalizedMessage()); |
313 |
throw cse;
|
314 |
} catch (IOException e) { |
315 |
CloudServersException cse = new CloudServersException();
|
316 |
cse.setMessage(e.getLocalizedMessage()); |
317 |
throw cse;
|
318 |
} catch (ParserConfigurationException e) { |
319 |
CloudServersException cse = new CloudServersException();
|
320 |
cse.setMessage(e.getLocalizedMessage()); |
321 |
throw cse;
|
322 |
} catch (SAXException e) {
|
323 |
CloudServersException cse = new CloudServersException();
|
324 |
cse.setMessage(e.getLocalizedMessage()); |
325 |
throw cse;
|
326 |
} catch (FactoryConfigurationError e) { |
327 |
CloudServersException cse = new CloudServersException();
|
328 |
cse.setMessage(e.getLocalizedMessage()); |
329 |
throw cse;
|
330 |
} |
331 |
return files;
|
332 |
} |
333 |
|
334 |
public List<ContainerObjects> createList(String containerName, |
335 |
boolean shared, String prefix) throws CloudServersException { |
336 |
Log.i(LOG, "Create List:" + containerName + " prefix " + prefix); |
337 |
while (prefix.endsWith("/")) |
338 |
prefix = prefix.substring(0, prefix.length() - 1); |
339 |
String url = getSafeURL(Account.getAccount().getStorageUrl(),
|
340 |
containerName) |
341 |
+ "?format=xml&shared="
|
342 |
+ shared |
343 |
+ "&prefix="
|
344 |
+ prefix + "&delimiter=/";
|
345 |
Log.i(LOG, " Getting " + containerName + ":" + shared + " " + prefix); |
346 |
Log.i(LOG, " URL:" + url);
|
347 |
return executeGet(containerName, url);
|
348 |
|
349 |
} |
350 |
|
351 |
public List<ContainerObjects> createOtherList(String containerName, |
352 |
String user) throws CloudServersException { |
353 |
Log.i(LOG, "Create List:" + containerName);
|
354 |
String url = getSafeURL(Account.getAccount().getStorageUrl()
|
355 |
.replaceAll(Account.getAccount().getUsername(), user), |
356 |
containerName) |
357 |
+ "?format=xml";
|
358 |
return executeGet(containerName, url);
|
359 |
|
360 |
} |
361 |
|
362 |
public List<ContainerObjects> createOtherList(String containerName, |
363 |
String user, String prefix) throws CloudServersException { |
364 |
Log.i(LOG, "Create List:" + containerName);
|
365 |
while (prefix.endsWith("/")) |
366 |
prefix = prefix.substring(0, prefix.length() - 1); |
367 |
String url = getSafeURL(Account.getAccount().getStorageUrl()
|
368 |
.replaceAll(Account.getAccount().getUsername(), user), |
369 |
containerName) |
370 |
+ "?format=xml" + "&prefix=" + prefix + "&delimiter=/"; |
371 |
return executeGet(containerName, url);
|
372 |
|
373 |
} |
374 |
|
375 |
public List<ContainerObjects> createList(String containerName, String prefix) |
376 |
throws CloudServersException {
|
377 |
while (prefix.endsWith("/")) |
378 |
prefix = prefix.substring(0, prefix.length() - 1); |
379 |
String url = getSafeURL(Account.getAccount().getStorageUrl(),
|
380 |
containerName) |
381 |
+ "?format=xml&prefix="
|
382 |
+ prefix |
383 |
+ "&delimiter=/";
|
384 |
Log.i(LOG, url); |
385 |
return executeGet(containerName, url);
|
386 |
|
387 |
} |
388 |
|
389 |
public List<ObjectVersion> getObjectVersions(String Container, |
390 |
String Object, String otherUser) throws CloudServersException { |
391 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
392 |
String url = getSafeURL(Account.getAccount().getStorageUrl() + "/" |
393 |
+ Container, Object) |
394 |
+ "?format=xml&version=list";
|
395 |
if (otherUser != null) { |
396 |
url = url.replaceFirst(Account.getAccount().getUsername(), |
397 |
otherUser); |
398 |
} |
399 |
Log.i(LOG, url); |
400 |
HttpGet get = new HttpGet(url);
|
401 |
List<ObjectVersion> versions = null; |
402 |
get.addHeader("Content-Type", "application/xml"); |
403 |
get.addHeader(CustomHttpClient.X_STORAGE_TOKEN, storageToken); |
404 |
|
405 |
try {
|
406 |
HttpResponse resp = httpclient.execute(get); |
407 |
BasicResponseHandler responseHandler = new BasicResponseHandler();
|
408 |
String body = responseHandler.handleResponse(resp);
|
409 |
|
410 |
if (resp.getStatusLine().getStatusCode() == 200 |
411 |
|| resp.getStatusLine().getStatusCode() == 203) {
|
412 |
ObjectVersionXMLParser filesXMLParser = new ObjectVersionXMLParser();
|
413 |
SAXParser saxParser = SAXParserFactory.newInstance() |
414 |
.newSAXParser(); |
415 |
XMLReader xmlReader = saxParser.getXMLReader(); |
416 |
xmlReader.setContentHandler(filesXMLParser); |
417 |
|
418 |
xmlReader.parse(new InputSource(new StringReader(body))); |
419 |
versions = filesXMLParser.getContainers(); |
420 |
|
421 |
} else {
|
422 |
CloudServersFaultXMLParser parser = new CloudServersFaultXMLParser();
|
423 |
SAXParser saxParser = SAXParserFactory.newInstance() |
424 |
.newSAXParser(); |
425 |
XMLReader xmlReader = saxParser.getXMLReader(); |
426 |
xmlReader.setContentHandler(parser); |
427 |
xmlReader.parse(new InputSource(new StringReader(body))); |
428 |
CloudServersException cse = parser.getException(); |
429 |
throw cse;
|
430 |
} |
431 |
} catch (ClientProtocolException e) {
|
432 |
CloudServersException cse = new CloudServersException();
|
433 |
cse.setMessage(e.getLocalizedMessage()); |
434 |
throw cse;
|
435 |
} catch (IOException e) { |
436 |
CloudServersException cse = new CloudServersException();
|
437 |
cse.setMessage(e.getLocalizedMessage()); |
438 |
throw cse;
|
439 |
} catch (ParserConfigurationException e) { |
440 |
CloudServersException cse = new CloudServersException();
|
441 |
cse.setMessage(e.getLocalizedMessage()); |
442 |
throw cse;
|
443 |
} catch (SAXException e) {
|
444 |
CloudServersException cse = new CloudServersException();
|
445 |
cse.setMessage(e.getLocalizedMessage()); |
446 |
throw cse;
|
447 |
} catch (FactoryConfigurationError e) { |
448 |
CloudServersException cse = new CloudServersException();
|
449 |
cse.setMessage(e.getLocalizedMessage()); |
450 |
throw cse;
|
451 |
} |
452 |
return versions;
|
453 |
} |
454 |
|
455 |
public List<ContainerObjects> createListMyShared(String passName, |
456 |
List<Container> containers, String prefix) |
457 |
throws CloudServersException {
|
458 |
|
459 |
ArrayList<ContainerObjects> files = new ArrayList<ContainerObjects>(); |
460 |
for (Container con : containers) { |
461 |
Log.i(LOG, "GEETING FILES OF COntainer:" + con.getName());
|
462 |
if (con.getName().equalsIgnoreCase("trash") |
463 |
|| con.getName().equals(Container.MYSHARED)
|
464 |
|| con.getName().equals(Container.OTHERS)) {
|
465 |
} else
|
466 |
try {
|
467 |
List<ContainerObjects> temp = createList(con.getName(),
|
468 |
true, prefix);
|
469 |
for (ContainerObjects o : temp) {
|
470 |
Log.i(LOG, o.getCName() + " " + o.isShared());
|
471 |
if (o.isShared() || o.isSubDir()) {
|
472 |
o.setContainerName(Container.MYSHARED);
|
473 |
files.add(o); |
474 |
} |
475 |
} |
476 |
} catch (CloudServersException e) {
|
477 |
|
478 |
} |
479 |
} |
480 |
return files;
|
481 |
|
482 |
} |
483 |
|
484 |
private HttpBundle executeMethod(int methodName, String url) |
485 |
throws CloudServersException {
|
486 |
return executeMethod(methodName, url, null, null); |
487 |
} |
488 |
|
489 |
private HttpBundle executeMethod(int methodName, String url, |
490 |
String contentType) throws CloudServersException { |
491 |
return executeMethod(methodName, url, contentType, null); |
492 |
} |
493 |
|
494 |
private HttpBundle executeMethod(int methodName, String url, |
495 |
String contentType, Map<String, String> headers) |
496 |
throws CloudServersException {
|
497 |
HttpResponse resp = null;
|
498 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
499 |
Log.d(LOG, url); |
500 |
HttpRequestBase addObject = null;
|
501 |
switch (methodName) {
|
502 |
case GET:
|
503 |
addObject = new HttpGet(url);
|
504 |
break;
|
505 |
case PUT:
|
506 |
addObject = new HttpPut(url);
|
507 |
break;
|
508 |
case POST:
|
509 |
addObject = new HttpPost(url);
|
510 |
break;
|
511 |
case DELETE:
|
512 |
addObject = new HttpDelete(url);
|
513 |
break;
|
514 |
|
515 |
default:
|
516 |
CloudServersException cse = new CloudServersException();
|
517 |
cse.setMessage("Invalid method");
|
518 |
throw cse;
|
519 |
} |
520 |
|
521 |
addObject |
522 |
.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
|
523 |
if (contentType != null) |
524 |
addObject.addHeader("Content-Type", contentType);
|
525 |
// addObject.addHeader("Content-Type",t);
|
526 |
if (headers != null) |
527 |
for (Entry<String, String> entry : headers.entrySet()) { |
528 |
addObject.addHeader(entry.getKey(), entry.getValue()); |
529 |
} |
530 |
httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class); |
531 |
|
532 |
HttpBundle bundle = new HttpBundle();
|
533 |
bundle.setCurlRequest(addObject); |
534 |
|
535 |
try {
|
536 |
resp = httpclient.execute(addObject); |
537 |
bundle.setHttpResponse(resp); |
538 |
} catch (ClientProtocolException e) {
|
539 |
e.printStackTrace(); |
540 |
CloudServersException cse = new CloudServersException();
|
541 |
cse.setMessage(e.getLocalizedMessage()); |
542 |
throw cse;
|
543 |
} catch (IOException e) { |
544 |
CloudServersException cse = new CloudServersException();
|
545 |
cse.setMessage(e.getLocalizedMessage()); |
546 |
throw cse;
|
547 |
} catch (FactoryConfigurationError e) { |
548 |
CloudServersException cse = new CloudServersException();
|
549 |
cse.setMessage(e.getLocalizedMessage()); |
550 |
throw cse;
|
551 |
} |
552 |
return bundle;
|
553 |
} |
554 |
|
555 |
public HttpBundle deleteObject(String Container, String Object, |
556 |
String username) throws CloudServersException { |
557 |
|
558 |
String url = getSafeURL(Account.getAccount().getStorageUrl(), Container |
559 |
+ "/" + Object); |
560 |
if (username != null) |
561 |
url = url |
562 |
.replaceFirst(Account.getAccount().getUsername(), username); |
563 |
return executeMethod(DELETE, url);
|
564 |
} |
565 |
|
566 |
public HttpBundle trashObject(String container, String Object) |
567 |
throws CloudServersException {
|
568 |
|
569 |
String url = getSafeURL(Account.getAccount().getStorageUrl(),
|
570 |
Container.TRASH + "/" + Object) + "?ignore_content_type="; |
571 |
Map<String, String> headers = new HashMap<String, String>(); |
572 |
headers.put("X-Move-From", "/" + container + "/" + Object); |
573 |
return executeMethod(PUT, url, "text/plain; charset=UTF-8", headers); |
574 |
} |
575 |
|
576 |
public HttpBundle restoreObject(String container, String Object) |
577 |
throws CloudServersException {
|
578 |
|
579 |
String url = getSafeURL(Account.getAccount().getStorageUrl(),
|
580 |
Container.PITHOS + "/" + Object) + "?ignore_content_type="; |
581 |
Map<String, String> headers = new HashMap<String, String>(); |
582 |
headers.put("X-Move-From", "/" + container + "/" + Object); |
583 |
return executeMethod(PUT, url, "text/plain; charset=UTF-8", headers); |
584 |
} |
585 |
|
586 |
public HttpBundle getObject(String Container, String Object, String username) |
587 |
throws CloudServersException {
|
588 |
String url = getSafeURL(Account.getAccount().getStorageUrl(), Container |
589 |
+ "/" + Object); |
590 |
if (username != null) |
591 |
url = url |
592 |
.replaceFirst(Account.getAccount().getUsername(), username); |
593 |
return executeMethod(GET, url);
|
594 |
} |
595 |
|
596 |
public HttpBundle getObject(String Container, String Object, |
597 |
String version, String username) throws CloudServersException { |
598 |
String url = getSafeURL(Account.getAccount().getStorageUrl(), Container |
599 |
+ "/" + Object + "?version=" + version); |
600 |
if (username != null) |
601 |
url = url |
602 |
.replaceFirst(Account.getAccount().getUsername(), username); |
603 |
return executeMethod(GET, url);
|
604 |
} |
605 |
|
606 |
public HttpBundle addObject(String Container, String Path, String Object, |
607 |
String type, String username) throws CloudServersException { |
608 |
String url = getSafeURL(Account.getAccount().getStorageUrl(), Container |
609 |
+ "/" + Path + Object); |
610 |
if (username != null) |
611 |
url = url |
612 |
.replaceFirst(Account.getAccount().getUsername(), username); |
613 |
return executeMethod(PUT, url, type);
|
614 |
} |
615 |
|
616 |
public HttpBundle updateObject(String Container, String Path, |
617 |
String Object, String type, Map<String, String> headers, |
618 |
String username) throws CloudServersException { |
619 |
String url;
|
620 |
if (Container != null) |
621 |
url = getSafeURL(Account.getAccount().getStorageUrl(), Container
|
622 |
+ "/" + Path + Object); |
623 |
else {
|
624 |
url = getSafeURL(Account.getAccount().getStorageUrl(), "");
|
625 |
if (url.endsWith("/")) |
626 |
url = url.substring(0, url.length() - 1); |
627 |
} |
628 |
url = url + "?update=";
|
629 |
if (username != null) |
630 |
url = url |
631 |
.replaceFirst(Account.getAccount().getUsername(), username); |
632 |
Log.d(LOG, "Update URL=" + url);
|
633 |
return executeMethod(POST, url, " text/plain; charset=UTF-8", headers); |
634 |
} |
635 |
|
636 |
/*
|
637 |
* used for adding sdcard files, requires an extra parameter to store the
|
638 |
* data for the file
|
639 |
*/
|
640 |
public HttpBundle addFileObject(String Container, String Path, |
641 |
String Object, String type, String file, String username) |
642 |
throws CloudServersException {
|
643 |
HttpResponse resp = null;
|
644 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
645 |
String url = getSafeURL(Account.getAccount().getStorageUrl(), Container |
646 |
+ "/" + Path + Object); |
647 |
if (username != null) |
648 |
url = url |
649 |
.replaceFirst(Account.getAccount().getUsername(), username); |
650 |
HttpPut addObject = new HttpPut(url);
|
651 |
|
652 |
addObject |
653 |
.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
|
654 |
addObject.addHeader("Content-Type", type);
|
655 |
httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class); |
656 |
if (file.startsWith("file://")) { |
657 |
file = file.substring(7);
|
658 |
} |
659 |
FileEntity tmp = null;
|
660 |
|
661 |
tmp = new FileEntity(new File(file), type); |
662 |
|
663 |
addObject.setEntity(tmp); |
664 |
|
665 |
HttpBundle bundle = new HttpBundle();
|
666 |
bundle.setCurlRequest(addObject); |
667 |
|
668 |
try {
|
669 |
resp = httpclient.execute(addObject); |
670 |
bundle.setHttpResponse(resp); |
671 |
} catch (ClientProtocolException e) {
|
672 |
CloudServersException cse = new CloudServersException();
|
673 |
cse.setMessage(e.getLocalizedMessage()); |
674 |
throw cse;
|
675 |
} catch (IOException e) { |
676 |
CloudServersException cse = new CloudServersException();
|
677 |
cse.setMessage(e.getLocalizedMessage()); |
678 |
throw cse;
|
679 |
} catch (FactoryConfigurationError e) { |
680 |
CloudServersException cse = new CloudServersException();
|
681 |
cse.setMessage(e.getLocalizedMessage()); |
682 |
throw cse;
|
683 |
} |
684 |
return bundle;
|
685 |
} |
686 |
|
687 |
private String getSafeURL(String badURL, String name) { |
688 |
URI uri = null; |
689 |
try {
|
690 |
uri = new URI("https", badURL.substring(8), "/" + name + "/", ""); |
691 |
} catch (URISyntaxException e1) { |
692 |
// TODO Auto-generated catch block
|
693 |
e1.printStackTrace(); |
694 |
} |
695 |
String url = null; |
696 |
try {
|
697 |
url = uri.toURL().toString(); |
698 |
} catch (MalformedURLException e1) { |
699 |
// TODO Auto-generated catch block
|
700 |
e1.printStackTrace(); |
701 |
} |
702 |
return url.substring(0, url.length() - 2); |
703 |
} |
704 |
|
705 |
public Map<String, String> getDisplayNames(String baseUrl, String token, |
706 |
List<String> displayNames, List<String> uuids) |
707 |
throws CloudServersException {
|
708 |
String url = getSafeURL(baseUrl, "user_catalogs"); |
709 |
//System.out.println("Using URL:"+url);
|
710 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
711 |
HttpPost post = new HttpPost(url);
|
712 |
Map<String, String> files = new HashMap<String, String>(); |
713 |
|
714 |
post.addHeader("Content-Type", "application/json"); |
715 |
post.addHeader(CustomHttpClient.X_STORAGE_TOKEN, token); |
716 |
try {
|
717 |
JSONObject object = new JSONObject();
|
718 |
JSONArray array = new JSONArray();
|
719 |
for (String displayName : displayNames) { |
720 |
array.put(displayName); |
721 |
} |
722 |
|
723 |
object.put("displaynames", array);
|
724 |
|
725 |
JSONArray array2 = new JSONArray();
|
726 |
for (String uuid : uuids) { |
727 |
array2.put(uuid); |
728 |
} |
729 |
object.put("uuids", array2);
|
730 |
post.setEntity(new StringEntity(object.toString()));
|
731 |
//System.out.println("Post:"+object.toString());
|
732 |
} catch (JSONException e1) {
|
733 |
throw new CloudServersException(-1, e1.getMessage(), ""); |
734 |
} catch (UnsupportedEncodingException e) { |
735 |
throw new CloudServersException(-1, e.getMessage(), ""); |
736 |
} |
737 |
try {
|
738 |
HttpResponse resp = httpclient.execute(post); |
739 |
BasicResponseHandler responseHandler = new BasicResponseHandler();
|
740 |
String body = responseHandler.handleResponse(resp);
|
741 |
|
742 |
if (resp.getStatusLine().getStatusCode() == 200) { |
743 |
//System.out.println("Body is "+body);
|
744 |
JSONObject result = new JSONObject(body);
|
745 |
JSONObject array1 = result.optJSONObject("displayname_catalog");
|
746 |
if (array1 != null) { |
747 |
Iterator<String> it = array1.keys(); |
748 |
while (it.hasNext()) {
|
749 |
String o = it.next();
|
750 |
String value = array1.optString(o);
|
751 |
if (value != null && !"null".equals(value)) { |
752 |
files.put(o, |
753 |
array1.getString(o.toString())); |
754 |
Account.getAccount().displayNames.put( |
755 |
o, array1.getString(o)); |
756 |
Account.getAccount().uuids.put( |
757 |
array1.getString(o), o); |
758 |
} |
759 |
} |
760 |
} |
761 |
JSONObject array2 = result.optJSONObject("uuid_catalog");
|
762 |
if (array2 != null) { |
763 |
Iterator<String> it = array2.keys(); |
764 |
while (it.hasNext()) {
|
765 |
String o = it.next();
|
766 |
String value = array2.optString(o);
|
767 |
if (value != null && !"null".equals(value)) { |
768 |
files.put(o.toString(), |
769 |
array2.getString(o.toString())); |
770 |
Account.getAccount().uuids.put(o, |
771 |
array2.getString(o)); |
772 |
Account.getAccount().displayNames.put( |
773 |
array2.getString(o), o); |
774 |
} |
775 |
} |
776 |
} |
777 |
} else {
|
778 |
CloudServersFaultXMLParser parser = new CloudServersFaultXMLParser();
|
779 |
SAXParser saxParser = SAXParserFactory.newInstance() |
780 |
.newSAXParser(); |
781 |
XMLReader xmlReader = saxParser.getXMLReader(); |
782 |
xmlReader.setContentHandler(parser); |
783 |
xmlReader.parse(new InputSource(new StringReader(body))); |
784 |
CloudServersException cse = parser.getException(); |
785 |
throw cse;
|
786 |
} |
787 |
} catch (ClientProtocolException e) {
|
788 |
CloudServersException cse = new CloudServersException();
|
789 |
cse.setMessage(e.getLocalizedMessage()); |
790 |
throw cse;
|
791 |
} catch (IOException e) { |
792 |
CloudServersException cse = new CloudServersException();
|
793 |
cse.setMessage(e.getLocalizedMessage()); |
794 |
throw cse;
|
795 |
} catch (ParserConfigurationException e) { |
796 |
CloudServersException cse = new CloudServersException();
|
797 |
cse.setMessage(e.getLocalizedMessage()); |
798 |
throw cse;
|
799 |
} catch (SAXException e) {
|
800 |
CloudServersException cse = new CloudServersException();
|
801 |
cse.setMessage(e.getLocalizedMessage()); |
802 |
throw cse;
|
803 |
} catch (FactoryConfigurationError e) { |
804 |
CloudServersException cse = new CloudServersException();
|
805 |
cse.setMessage(e.getLocalizedMessage()); |
806 |
throw cse;
|
807 |
} catch (JSONException e) {
|
808 |
e.printStackTrace(); |
809 |
CloudServersException cse = new CloudServersException();
|
810 |
cse.setMessage(e.getLocalizedMessage()); |
811 |
throw cse;
|
812 |
} |
813 |
return files;
|
814 |
} |
815 |
|
816 |
} |