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