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