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