root / src / com / rackspace / cloud / files / api / client / ContainerObjectManager.java @ da02192c
History | View | Annotate | Download (23.4 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.util.ArrayList; |
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.entity.FileEntity; |
27 |
import org.apache.http.entity.StringEntity; |
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 |
|
52 |
public String LOG = "ContainerObjectManager"; |
53 |
private Context context; |
54 |
public static final String storageToken = Account.getAccount() |
55 |
.getStorageToken(); |
56 |
|
57 |
public ContainerObjectManager(Context context) { |
58 |
this.context = context;
|
59 |
} |
60 |
|
61 |
public ArrayList<ContainerObjects> createList(boolean detail, |
62 |
String passName) throws CloudServersException { |
63 |
Log.i(LOG,"Create List:"+passName);
|
64 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
65 |
String url = getSafeURL(Account.getAccount().getStorageUrl(), passName)
|
66 |
+ "?format=xml";
|
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(passName); |
90 |
|
91 |
} else {
|
92 |
CloudServersFaultXMLParser parser = new CloudServersFaultXMLParser();
|
93 |
SAXParser saxParser = SAXParserFactory.newInstance() |
94 |
.newSAXParser(); |
95 |
XMLReader xmlReader = saxParser.getXMLReader(); |
96 |
xmlReader.setContentHandler(parser); |
97 |
xmlReader.parse(new InputSource(new StringReader(body))); |
98 |
CloudServersException cse = parser.getException(); |
99 |
throw cse;
|
100 |
} |
101 |
} catch (ClientProtocolException e) {
|
102 |
CloudServersException cse = new CloudServersException();
|
103 |
cse.setMessage(e.getLocalizedMessage()); |
104 |
throw cse;
|
105 |
} catch (IOException e) { |
106 |
CloudServersException cse = new CloudServersException();
|
107 |
cse.setMessage(e.getLocalizedMessage()); |
108 |
throw cse;
|
109 |
} catch (ParserConfigurationException e) { |
110 |
CloudServersException cse = new CloudServersException();
|
111 |
cse.setMessage(e.getLocalizedMessage()); |
112 |
throw cse;
|
113 |
} catch (SAXException e) {
|
114 |
CloudServersException cse = new CloudServersException();
|
115 |
cse.setMessage(e.getLocalizedMessage()); |
116 |
throw cse;
|
117 |
} catch (FactoryConfigurationError e) { |
118 |
CloudServersException cse = new CloudServersException();
|
119 |
cse.setMessage(e.getLocalizedMessage()); |
120 |
throw cse;
|
121 |
} |
122 |
return files;
|
123 |
|
124 |
} |
125 |
|
126 |
|
127 |
public ArrayList<ContainerObjects> createList(boolean detail, |
128 |
String passName, boolean shared) throws CloudServersException { |
129 |
Log.i(LOG,"Create List:"+passName);
|
130 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
131 |
String url = getSafeURL(Account.getAccount().getStorageUrl(), passName)
|
132 |
+ "?format=xml&shared="+shared;
|
133 |
HttpGet get = new HttpGet(url);
|
134 |
ArrayList<ContainerObjects> files = new ArrayList<ContainerObjects>(); |
135 |
|
136 |
get.addHeader("Content-Type", "application/xml"); |
137 |
get.addHeader(CustomHttpClient.X_STORAGE_TOKEN, storageToken); |
138 |
|
139 |
try {
|
140 |
HttpResponse resp = httpclient.execute(get); |
141 |
BasicResponseHandler responseHandler = new BasicResponseHandler();
|
142 |
String body = responseHandler.handleResponse(resp);
|
143 |
|
144 |
if (resp.getStatusLine().getStatusCode() == 200 |
145 |
|| resp.getStatusLine().getStatusCode() == 203) {
|
146 |
ContainerObjectXMLparser filesXMLParser = new ContainerObjectXMLparser();
|
147 |
SAXParser saxParser = SAXParserFactory.newInstance() |
148 |
.newSAXParser(); |
149 |
XMLReader xmlReader = saxParser.getXMLReader(); |
150 |
xmlReader.setContentHandler(filesXMLParser); |
151 |
|
152 |
xmlReader.parse(new InputSource(new StringReader(body))); |
153 |
files = filesXMLParser.getViewFiles(); |
154 |
for(ContainerObjects o :files)
|
155 |
o.setContainerName(passName); |
156 |
|
157 |
} else {
|
158 |
CloudServersFaultXMLParser parser = new CloudServersFaultXMLParser();
|
159 |
SAXParser saxParser = SAXParserFactory.newInstance() |
160 |
.newSAXParser(); |
161 |
XMLReader xmlReader = saxParser.getXMLReader(); |
162 |
xmlReader.setContentHandler(parser); |
163 |
xmlReader.parse(new InputSource(new StringReader(body))); |
164 |
CloudServersException cse = parser.getException(); |
165 |
throw cse;
|
166 |
} |
167 |
} catch (ClientProtocolException e) {
|
168 |
CloudServersException cse = new CloudServersException();
|
169 |
cse.setMessage(e.getLocalizedMessage()); |
170 |
throw cse;
|
171 |
} catch (IOException e) { |
172 |
CloudServersException cse = new CloudServersException();
|
173 |
cse.setMessage(e.getLocalizedMessage()); |
174 |
throw cse;
|
175 |
} catch (ParserConfigurationException e) { |
176 |
CloudServersException cse = new CloudServersException();
|
177 |
cse.setMessage(e.getLocalizedMessage()); |
178 |
throw cse;
|
179 |
} catch (SAXException e) {
|
180 |
CloudServersException cse = new CloudServersException();
|
181 |
cse.setMessage(e.getLocalizedMessage()); |
182 |
throw cse;
|
183 |
} catch (FactoryConfigurationError e) { |
184 |
CloudServersException cse = new CloudServersException();
|
185 |
cse.setMessage(e.getLocalizedMessage()); |
186 |
throw cse;
|
187 |
} |
188 |
return files;
|
189 |
|
190 |
} |
191 |
|
192 |
public ArrayList<ContainerObjects> createOtherList(boolean detail, |
193 |
String passName, String user) throws CloudServersException { |
194 |
Log.i(LOG,"Create List:"+passName);
|
195 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
196 |
String url = getSafeURL(Account.getAccount().getStorageUrl().replaceAll(Account.getAccount().getUsername(), user), passName)
|
197 |
+ "?format=xml";
|
198 |
HttpGet get = new HttpGet(url);
|
199 |
ArrayList<ContainerObjects> files = new ArrayList<ContainerObjects>(); |
200 |
|
201 |
get.addHeader("Content-Type", "application/xml"); |
202 |
get.addHeader(CustomHttpClient.X_STORAGE_TOKEN, storageToken); |
203 |
|
204 |
try {
|
205 |
HttpResponse resp = httpclient.execute(get); |
206 |
BasicResponseHandler responseHandler = new BasicResponseHandler();
|
207 |
String body = responseHandler.handleResponse(resp);
|
208 |
|
209 |
if (resp.getStatusLine().getStatusCode() == 200 |
210 |
|| resp.getStatusLine().getStatusCode() == 203) {
|
211 |
ContainerObjectXMLparser filesXMLParser = new ContainerObjectXMLparser();
|
212 |
SAXParser saxParser = SAXParserFactory.newInstance() |
213 |
.newSAXParser(); |
214 |
XMLReader xmlReader = saxParser.getXMLReader(); |
215 |
xmlReader.setContentHandler(filesXMLParser); |
216 |
|
217 |
xmlReader.parse(new InputSource(new StringReader(body))); |
218 |
files = filesXMLParser.getViewFiles(); |
219 |
for(ContainerObjects o :files)
|
220 |
o.setContainerName(passName); |
221 |
|
222 |
} else {
|
223 |
CloudServersFaultXMLParser parser = new CloudServersFaultXMLParser();
|
224 |
SAXParser saxParser = SAXParserFactory.newInstance() |
225 |
.newSAXParser(); |
226 |
XMLReader xmlReader = saxParser.getXMLReader(); |
227 |
xmlReader.setContentHandler(parser); |
228 |
xmlReader.parse(new InputSource(new StringReader(body))); |
229 |
CloudServersException cse = parser.getException(); |
230 |
throw cse;
|
231 |
} |
232 |
} catch (ClientProtocolException e) {
|
233 |
CloudServersException cse = new CloudServersException();
|
234 |
cse.setMessage(e.getLocalizedMessage()); |
235 |
throw cse;
|
236 |
} catch (IOException e) { |
237 |
CloudServersException cse = new CloudServersException();
|
238 |
cse.setMessage(e.getLocalizedMessage()); |
239 |
throw cse;
|
240 |
} catch (ParserConfigurationException e) { |
241 |
CloudServersException cse = new CloudServersException();
|
242 |
cse.setMessage(e.getLocalizedMessage()); |
243 |
throw cse;
|
244 |
} catch (SAXException e) {
|
245 |
CloudServersException cse = new CloudServersException();
|
246 |
cse.setMessage(e.getLocalizedMessage()); |
247 |
throw cse;
|
248 |
} catch (FactoryConfigurationError e) { |
249 |
CloudServersException cse = new CloudServersException();
|
250 |
cse.setMessage(e.getLocalizedMessage()); |
251 |
throw cse;
|
252 |
} |
253 |
return files;
|
254 |
|
255 |
} |
256 |
|
257 |
public ArrayList<ContainerObjects> createList(boolean detail, |
258 |
String passName, String prefix) throws CloudServersException { |
259 |
if(prefix.endsWith("/")) |
260 |
prefix = prefix.substring(0,prefix.length()-1); |
261 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
262 |
String url = getSafeURL(Account.getAccount().getStorageUrl(), passName)
|
263 |
+ "?format=xml&prefix="+prefix+"&delimiter=/"; |
264 |
Log.i(LOG,url); |
265 |
HttpGet get = new HttpGet(url);
|
266 |
ArrayList<ContainerObjects> files = new ArrayList<ContainerObjects>(); |
267 |
|
268 |
get.addHeader("Content-Type", "application/xml"); |
269 |
get.addHeader(CustomHttpClient.X_STORAGE_TOKEN, storageToken); |
270 |
|
271 |
try {
|
272 |
HttpResponse resp = httpclient.execute(get); |
273 |
BasicResponseHandler responseHandler = new BasicResponseHandler();
|
274 |
String body = responseHandler.handleResponse(resp);
|
275 |
|
276 |
if (resp.getStatusLine().getStatusCode() == 200 |
277 |
|| resp.getStatusLine().getStatusCode() == 203) {
|
278 |
ContainerObjectXMLparser filesXMLParser = new ContainerObjectXMLparser();
|
279 |
SAXParser saxParser = SAXParserFactory.newInstance() |
280 |
.newSAXParser(); |
281 |
XMLReader xmlReader = saxParser.getXMLReader(); |
282 |
xmlReader.setContentHandler(filesXMLParser); |
283 |
|
284 |
xmlReader.parse(new InputSource(new StringReader(body))); |
285 |
files = filesXMLParser.getViewFiles(); |
286 |
for(ContainerObjects o :files)
|
287 |
o.setContainerName(passName); |
288 |
|
289 |
} else {
|
290 |
CloudServersFaultXMLParser parser = new CloudServersFaultXMLParser();
|
291 |
SAXParser saxParser = SAXParserFactory.newInstance() |
292 |
.newSAXParser(); |
293 |
XMLReader xmlReader = saxParser.getXMLReader(); |
294 |
xmlReader.setContentHandler(parser); |
295 |
xmlReader.parse(new InputSource(new StringReader(body))); |
296 |
CloudServersException cse = parser.getException(); |
297 |
throw cse;
|
298 |
} |
299 |
} catch (ClientProtocolException e) {
|
300 |
CloudServersException cse = new CloudServersException();
|
301 |
cse.setMessage(e.getLocalizedMessage()); |
302 |
throw cse;
|
303 |
} catch (IOException e) { |
304 |
CloudServersException cse = new CloudServersException();
|
305 |
cse.setMessage(e.getLocalizedMessage()); |
306 |
throw cse;
|
307 |
} catch (ParserConfigurationException e) { |
308 |
CloudServersException cse = new CloudServersException();
|
309 |
cse.setMessage(e.getLocalizedMessage()); |
310 |
throw cse;
|
311 |
} catch (SAXException e) {
|
312 |
CloudServersException cse = new CloudServersException();
|
313 |
cse.setMessage(e.getLocalizedMessage()); |
314 |
throw cse;
|
315 |
} catch (FactoryConfigurationError e) { |
316 |
CloudServersException cse = new CloudServersException();
|
317 |
cse.setMessage(e.getLocalizedMessage()); |
318 |
throw cse;
|
319 |
} |
320 |
return files;
|
321 |
|
322 |
} |
323 |
|
324 |
public List<ObjectVersion> getObjectVersions(String Container, String Object) |
325 |
throws CloudServersException {
|
326 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
327 |
String url = Account.getAccount().getStorageUrl()+"/"+ Container |
328 |
+ "/" + Object+"?format=xml&version=list"; |
329 |
Log.i("papala",url);
|
330 |
HttpGet get = new HttpGet(url);
|
331 |
List<ObjectVersion> versions =null; |
332 |
get.addHeader("Content-Type", "application/xml"); |
333 |
get.addHeader(CustomHttpClient.X_STORAGE_TOKEN, storageToken); |
334 |
|
335 |
try {
|
336 |
HttpResponse resp = httpclient.execute(get); |
337 |
BasicResponseHandler responseHandler = new BasicResponseHandler();
|
338 |
String body = responseHandler.handleResponse(resp);
|
339 |
|
340 |
if (resp.getStatusLine().getStatusCode() == 200 |
341 |
|| resp.getStatusLine().getStatusCode() == 203) {
|
342 |
ObjectVersionXMLParser filesXMLParser = new ObjectVersionXMLParser();
|
343 |
SAXParser saxParser = SAXParserFactory.newInstance() |
344 |
.newSAXParser(); |
345 |
XMLReader xmlReader = saxParser.getXMLReader(); |
346 |
xmlReader.setContentHandler(filesXMLParser); |
347 |
|
348 |
xmlReader.parse(new InputSource(new StringReader(body))); |
349 |
versions = filesXMLParser.getContainers(); |
350 |
|
351 |
|
352 |
} else {
|
353 |
CloudServersFaultXMLParser parser = new CloudServersFaultXMLParser();
|
354 |
SAXParser saxParser = SAXParserFactory.newInstance() |
355 |
.newSAXParser(); |
356 |
XMLReader xmlReader = saxParser.getXMLReader(); |
357 |
xmlReader.setContentHandler(parser); |
358 |
xmlReader.parse(new InputSource(new StringReader(body))); |
359 |
CloudServersException cse = parser.getException(); |
360 |
throw cse;
|
361 |
} |
362 |
} catch (ClientProtocolException e) {
|
363 |
CloudServersException cse = new CloudServersException();
|
364 |
cse.setMessage(e.getLocalizedMessage()); |
365 |
throw cse;
|
366 |
} catch (IOException e) { |
367 |
CloudServersException cse = new CloudServersException();
|
368 |
cse.setMessage(e.getLocalizedMessage()); |
369 |
throw cse;
|
370 |
} catch (ParserConfigurationException e) { |
371 |
CloudServersException cse = new CloudServersException();
|
372 |
cse.setMessage(e.getLocalizedMessage()); |
373 |
throw cse;
|
374 |
} catch (SAXException e) {
|
375 |
CloudServersException cse = new CloudServersException();
|
376 |
cse.setMessage(e.getLocalizedMessage()); |
377 |
throw cse;
|
378 |
} catch (FactoryConfigurationError e) { |
379 |
CloudServersException cse = new CloudServersException();
|
380 |
cse.setMessage(e.getLocalizedMessage()); |
381 |
throw cse;
|
382 |
} |
383 |
return versions;
|
384 |
} |
385 |
|
386 |
public ArrayList<ContainerObjects> createListMyShared(boolean detail, |
387 |
String passName, List<Container> containers) throws CloudServersException { |
388 |
|
389 |
ArrayList<ContainerObjects> files = new ArrayList<ContainerObjects>(); |
390 |
for(Container con :containers ){ |
391 |
Log.i(LOG,"GEETING FILES OF COntainer:"+con.getName());
|
392 |
if(con.getName().equalsIgnoreCase("trash")||con.getName().equals(Container.MYSHARED)||con.getName().equals(Container.OTHERS)){} |
393 |
else
|
394 |
try{
|
395 |
ArrayList<ContainerObjects> temp = createList(detail, con.getName(), true); |
396 |
for(ContainerObjects o : temp){
|
397 |
Log.i(LOG,o.getCName()+" "+o.isShared());
|
398 |
if(o.isShared()){
|
399 |
o.setContainerName(Container.MYSHARED);
|
400 |
files.add(o); |
401 |
} |
402 |
} |
403 |
} |
404 |
catch(CloudServersException e){
|
405 |
|
406 |
} |
407 |
} |
408 |
return files;
|
409 |
|
410 |
} |
411 |
|
412 |
public HttpBundle deleteObject(String Container, String Object) |
413 |
throws CloudServersException {
|
414 |
HttpResponse resp = null;
|
415 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
416 |
String url = getSafeURL(Account.getAccount().getStorageUrl(), Container |
417 |
+ "/" + Object); |
418 |
HttpDelete deleteObject = new HttpDelete(url);
|
419 |
|
420 |
deleteObject.addHeader("X-Auth-Token", Account.getAccount()
|
421 |
.getAuthToken()); |
422 |
httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class); |
423 |
|
424 |
HttpBundle bundle = new HttpBundle();
|
425 |
bundle.setCurlRequest(deleteObject); |
426 |
|
427 |
try {
|
428 |
resp = httpclient.execute(deleteObject); |
429 |
bundle.setHttpResponse(resp); |
430 |
} catch (ClientProtocolException e) {
|
431 |
CloudServersException cse = new CloudServersException();
|
432 |
cse.setMessage(e.getLocalizedMessage()); |
433 |
throw cse;
|
434 |
} catch (IOException e) { |
435 |
CloudServersException cse = new CloudServersException();
|
436 |
cse.setMessage(e.getLocalizedMessage()); |
437 |
throw cse;
|
438 |
} catch (FactoryConfigurationError e) { |
439 |
CloudServersException cse = new CloudServersException();
|
440 |
cse.setMessage(e.getLocalizedMessage()); |
441 |
throw cse;
|
442 |
} |
443 |
return bundle;
|
444 |
} |
445 |
|
446 |
public HttpBundle getObject(String Container, String Object) |
447 |
throws CloudServersException {
|
448 |
HttpResponse resp = null;
|
449 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
450 |
String url = getSafeURL(Account.getAccount().getStorageUrl(), Container |
451 |
+ "/" + Object); |
452 |
HttpGet getObject = new HttpGet(url);
|
453 |
getObject |
454 |
.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
|
455 |
httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class); |
456 |
|
457 |
HttpBundle bundle = new HttpBundle();
|
458 |
bundle.setCurlRequest(getObject); |
459 |
|
460 |
try {
|
461 |
resp = httpclient.execute(getObject); |
462 |
bundle.setHttpResponse(resp); |
463 |
} catch (ClientProtocolException e) {
|
464 |
CloudServersException cse = new CloudServersException();
|
465 |
cse.setMessage(e.getLocalizedMessage()); |
466 |
throw cse;
|
467 |
} catch (IOException e) { |
468 |
CloudServersException cse = new CloudServersException();
|
469 |
cse.setMessage(e.getLocalizedMessage()); |
470 |
throw cse;
|
471 |
} catch (FactoryConfigurationError e) { |
472 |
CloudServersException cse = new CloudServersException();
|
473 |
cse.setMessage(e.getLocalizedMessage()); |
474 |
throw cse;
|
475 |
} |
476 |
return bundle;
|
477 |
} |
478 |
|
479 |
|
480 |
|
481 |
public HttpBundle addObject(String Container, String Path, String Object, |
482 |
String type) throws CloudServersException { |
483 |
HttpResponse resp = null;
|
484 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
485 |
String url = getSafeURL(Account.getAccount().getStorageUrl(), Container |
486 |
+ "/" + Path + Object); |
487 |
HttpPut addObject = new HttpPut(url);
|
488 |
|
489 |
addObject |
490 |
.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
|
491 |
addObject.addHeader("Content-Type", type);
|
492 |
httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class); |
493 |
|
494 |
HttpBundle bundle = new HttpBundle();
|
495 |
bundle.setCurlRequest(addObject); |
496 |
|
497 |
try {
|
498 |
resp = httpclient.execute(addObject); |
499 |
bundle.setHttpResponse(resp); |
500 |
} catch (ClientProtocolException e) {
|
501 |
CloudServersException cse = new CloudServersException();
|
502 |
cse.setMessage(e.getLocalizedMessage()); |
503 |
throw cse;
|
504 |
} catch (IOException e) { |
505 |
CloudServersException cse = new CloudServersException();
|
506 |
cse.setMessage(e.getLocalizedMessage()); |
507 |
throw cse;
|
508 |
} catch (FactoryConfigurationError e) { |
509 |
CloudServersException cse = new CloudServersException();
|
510 |
cse.setMessage(e.getLocalizedMessage()); |
511 |
throw cse;
|
512 |
} |
513 |
return bundle;
|
514 |
} |
515 |
|
516 |
public HttpBundle updateObject(String Container, String Path, String Object, |
517 |
String type, Map<String,String> headers) throws CloudServersException { |
518 |
HttpResponse resp = null;
|
519 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
520 |
String url;
|
521 |
if(Container !=null) |
522 |
url = getSafeURL(Account.getAccount().getStorageUrl(), Container
|
523 |
+ "/" + Path + Object); |
524 |
else{
|
525 |
url = getSafeURL(Account.getAccount().getStorageUrl(),"");
|
526 |
if(url.endsWith("/")) |
527 |
url = url.substring(0, url.length()-1); |
528 |
} |
529 |
url =url+"?update=";
|
530 |
Log.d("papala", url);
|
531 |
HttpPost addObject = new HttpPost(url);
|
532 |
|
533 |
addObject |
534 |
.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
|
535 |
addObject.addHeader("Content-Type", " text/plain; charset=UTF-8"); |
536 |
//addObject.addHeader("Content-Type",t);
|
537 |
for(Entry<String,String> entry : headers.entrySet()){ |
538 |
addObject.addHeader(entry.getKey(),entry.getValue()); |
539 |
} |
540 |
httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class); |
541 |
|
542 |
HttpBundle bundle = new HttpBundle();
|
543 |
bundle.setCurlRequest(addObject); |
544 |
|
545 |
try {
|
546 |
resp = httpclient.execute(addObject); |
547 |
bundle.setHttpResponse(resp); |
548 |
} catch (ClientProtocolException e) {
|
549 |
CloudServersException cse = new CloudServersException();
|
550 |
cse.setMessage(e.getLocalizedMessage()); |
551 |
throw cse;
|
552 |
} catch (IOException e) { |
553 |
CloudServersException cse = new CloudServersException();
|
554 |
cse.setMessage(e.getLocalizedMessage()); |
555 |
throw cse;
|
556 |
} catch (FactoryConfigurationError e) { |
557 |
CloudServersException cse = new CloudServersException();
|
558 |
cse.setMessage(e.getLocalizedMessage()); |
559 |
throw cse;
|
560 |
} |
561 |
return bundle;
|
562 |
} |
563 |
|
564 |
/*
|
565 |
* used for adding text files, requires an extra parameter to store the data
|
566 |
* for the file
|
567 |
*/
|
568 |
public HttpBundle addObject(String Container, String Path, String Object, |
569 |
String type, String data) throws CloudServersException { |
570 |
HttpResponse resp = null;
|
571 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
572 |
String url = getSafeURL(Account.getAccount().getStorageUrl(), Container |
573 |
+ "/" + Path + Object); |
574 |
HttpPut addObject = new HttpPut(url);
|
575 |
|
576 |
addObject |
577 |
.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
|
578 |
addObject.addHeader("Content-Type", type);
|
579 |
httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class); |
580 |
|
581 |
StringEntity tmp = null;
|
582 |
try {
|
583 |
tmp = new StringEntity(data);
|
584 |
} catch (UnsupportedEncodingException e) { |
585 |
CloudServersException cse = new CloudServersException();
|
586 |
cse.setMessage(e.getLocalizedMessage()); |
587 |
throw cse;
|
588 |
} |
589 |
addObject.setEntity(tmp); |
590 |
|
591 |
HttpBundle bundle = new HttpBundle();
|
592 |
bundle.setCurlRequest(addObject); |
593 |
|
594 |
try {
|
595 |
resp = httpclient.execute(addObject); |
596 |
bundle.setHttpResponse(resp); |
597 |
} catch (ClientProtocolException e) {
|
598 |
CloudServersException cse = new CloudServersException();
|
599 |
cse.setMessage(e.getLocalizedMessage()); |
600 |
throw cse;
|
601 |
} catch (IOException e) { |
602 |
CloudServersException cse = new CloudServersException();
|
603 |
cse.setMessage(e.getLocalizedMessage()); |
604 |
throw cse;
|
605 |
} catch (FactoryConfigurationError e) { |
606 |
CloudServersException cse = new CloudServersException();
|
607 |
cse.setMessage(e.getLocalizedMessage()); |
608 |
throw cse;
|
609 |
} |
610 |
return bundle;
|
611 |
} |
612 |
|
613 |
/*
|
614 |
* used for adding text files, requires an extra parameter to store the data
|
615 |
* for the file
|
616 |
*/
|
617 |
public HttpBundle addFileObject(String Container, String Path, String Object, |
618 |
String type, String file) throws CloudServersException { |
619 |
HttpResponse resp = null;
|
620 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
621 |
String url = getSafeURL(Account.getAccount().getStorageUrl(), Container |
622 |
+ "/" + Path + Object); |
623 |
HttpPut addObject = new HttpPut(url);
|
624 |
|
625 |
addObject |
626 |
.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
|
627 |
addObject.addHeader("Content-Type", type);
|
628 |
httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class); |
629 |
if (file.startsWith("file://")) { |
630 |
file = file.substring(7);
|
631 |
} |
632 |
FileEntity tmp = null;
|
633 |
|
634 |
tmp = new FileEntity(new File(file),type); |
635 |
|
636 |
addObject.setEntity(tmp); |
637 |
|
638 |
HttpBundle bundle = new HttpBundle();
|
639 |
bundle.setCurlRequest(addObject); |
640 |
|
641 |
try {
|
642 |
resp = httpclient.execute(addObject); |
643 |
bundle.setHttpResponse(resp); |
644 |
} catch (ClientProtocolException e) {
|
645 |
CloudServersException cse = new CloudServersException();
|
646 |
cse.setMessage(e.getLocalizedMessage()); |
647 |
throw cse;
|
648 |
} catch (IOException e) { |
649 |
CloudServersException cse = new CloudServersException();
|
650 |
cse.setMessage(e.getLocalizedMessage()); |
651 |
throw cse;
|
652 |
} catch (FactoryConfigurationError e) { |
653 |
CloudServersException cse = new CloudServersException();
|
654 |
cse.setMessage(e.getLocalizedMessage()); |
655 |
throw cse;
|
656 |
} |
657 |
return bundle;
|
658 |
} |
659 |
|
660 |
private String getSafeURL(String badURL, String name) { |
661 |
URI uri = null; |
662 |
try {
|
663 |
uri = new URI("https", badURL.substring(8), "/" + name + "/", ""); |
664 |
} catch (URISyntaxException e1) { |
665 |
// TODO Auto-generated catch block
|
666 |
e1.printStackTrace(); |
667 |
} |
668 |
String url = null; |
669 |
try {
|
670 |
url = uri.toURL().toString(); |
671 |
} catch (MalformedURLException e1) { |
672 |
// TODO Auto-generated catch block
|
673 |
e1.printStackTrace(); |
674 |
} |
675 |
return url.substring(0, url.length() - 2); |
676 |
} |
677 |
|
678 |
} |