Revision 856ad13d src/com/rackspace/cloud/files/api/client/ContainerManager.java
b/src/com/rackspace/cloud/files/api/client/ContainerManager.java | ||
---|---|---|
6 | 6 |
import java.net.URI; |
7 | 7 |
import java.net.URISyntaxException; |
8 | 8 |
import java.util.ArrayList; |
9 |
import java.util.List; |
|
9 | 10 |
|
10 | 11 |
import javax.xml.parsers.FactoryConfigurationError; |
11 | 12 |
import javax.xml.parsers.ParserConfigurationException; |
12 | 13 |
import javax.xml.parsers.SAXParser; |
13 | 14 |
import javax.xml.parsers.SAXParserFactory; |
14 | 15 |
|
16 |
import org.apache.http.Header; |
|
15 | 17 |
import org.apache.http.HttpResponse; |
16 | 18 |
import org.apache.http.client.ClientProtocolException; |
17 | 19 |
import org.apache.http.client.methods.HttpDelete; |
... | ... | |
28 | 30 |
import android.text.Editable; |
29 | 31 |
import android.util.Log; |
30 | 32 |
|
33 |
import com.rackspace.cloud.android.AndroidCloudApplication; |
|
31 | 34 |
import com.rackspace.cloud.files.api.client.parsers.ContainerXMLParser; |
35 |
import com.rackspace.cloud.files.api.client.parsers.OthersXmlParser; |
|
32 | 36 |
import com.rackspace.cloud.servers.api.client.Account; |
33 | 37 |
import com.rackspace.cloud.servers.api.client.CloudServersException; |
34 | 38 |
import com.rackspace.cloud.servers.api.client.EntityManager; |
... | ... | |
41 | 45 |
*/ |
42 | 46 |
public class ContainerManager extends EntityManager { |
43 | 47 |
private Context context; |
44 |
|
|
48 |
public String LOG = "ContainerManager"; |
|
45 | 49 |
public ContainerManager(Context context) { |
46 | 50 |
this.context = context; |
47 | 51 |
} |
... | ... | |
248 | 252 |
|
249 | 253 |
public ArrayList<Container> createList(boolean detail) |
250 | 254 |
throws CloudServersException { |
251 |
|
|
255 |
Log.i(LOG,"Create List:"+Account.getAccount().getStorageUrl()); |
|
252 | 256 |
CustomHttpClient httpclient = new CustomHttpClient(context); |
253 | 257 |
HttpGet get = new HttpGet(Account.getAccount().getStorageUrl() |
254 | 258 |
+ "?format=xml"); |
... | ... | |
256 | 260 |
get.addHeader(CustomHttpClient.X_STORAGE_TOKEN, Account.getAccount() |
257 | 261 |
.getStorageToken()); |
258 | 262 |
get.addHeader("Content-Type", "application/xml"); |
259 |
|
|
263 |
|
|
260 | 264 |
try { |
261 | 265 |
HttpResponse resp = httpclient.execute(get); |
266 |
List<GroupResource> groups = new ArrayList<GroupResource>(); |
|
267 |
for(Header h : resp.getAllHeaders()){ |
|
268 |
if(h.getName().startsWith("X-Account-Group-")){ |
|
269 |
GroupResource group = new GroupResource(); |
|
270 |
group.setName(h.getName().replaceAll("X-Account-Group-", "")); |
|
271 |
String users = h.getValue(); |
|
272 |
String[] us = users.split(","); |
|
273 |
for(String u : us){ |
|
274 |
group.getUsers().add(u); |
|
275 |
} |
|
276 |
groups.add(group); |
|
277 |
} |
|
278 |
} |
|
279 |
List<String> others = createOthersList(); |
|
280 |
((AndroidCloudApplication)context.getApplicationContext()).setOthers(others); |
|
281 |
((AndroidCloudApplication)context.getApplicationContext()).setGroups(groups); |
|
262 | 282 |
BasicResponseHandler responseHandler = new BasicResponseHandler(); |
263 | 283 |
String body = responseHandler.handleResponse(resp); |
264 | 284 |
Log.i("ContainerManager", body); |
... | ... | |
306 | 326 |
|
307 | 327 |
return containers; |
308 | 328 |
} |
329 |
|
|
330 |
|
|
331 |
public ArrayList<String> createOthersList() |
|
332 |
throws CloudServersException { |
|
333 |
CustomHttpClient httpclient = new CustomHttpClient(context); |
|
334 |
HttpGet get = new HttpGet(Account.getAccount().getStorageUrl().replaceAll(Account.getAccount().getUsername(), "") |
|
335 |
+ "?format=xml"); |
|
336 |
ArrayList<String> containers = new ArrayList<String>(); |
|
337 |
get.addHeader(CustomHttpClient.X_STORAGE_TOKEN, Account.getAccount() |
|
338 |
.getStorageToken()); |
|
339 |
get.addHeader("Content-Type", "application/xml"); |
|
340 |
|
|
341 |
try { |
|
342 |
HttpResponse resp = httpclient.execute(get); |
|
343 |
BasicResponseHandler responseHandler = new BasicResponseHandler(); |
|
344 |
String body = responseHandler.handleResponse(resp); |
|
345 |
Log.i("ContainerManager", body); |
|
346 |
if (resp.getStatusLine().getStatusCode() == 200 |
|
347 |
|| resp.getStatusLine().getStatusCode() == 203) { |
|
348 |
OthersXmlParser containerXMLParser = new OthersXmlParser(); |
|
349 |
SAXParser saxParser = SAXParserFactory.newInstance() |
|
350 |
.newSAXParser(); |
|
351 |
XMLReader xmlReader = saxParser.getXMLReader(); |
|
352 |
xmlReader.setContentHandler(containerXMLParser); |
|
353 |
|
|
354 |
xmlReader.parse(new InputSource(new StringReader(body))); |
|
355 |
containers = containerXMLParser.getContainers(); |
|
356 |
Log.i(LOG,containers.toString()); |
|
357 |
} else { |
|
358 |
CloudServersFaultXMLParser parser = new CloudServersFaultXMLParser(); |
|
359 |
SAXParser saxParser = SAXParserFactory.newInstance() |
|
360 |
.newSAXParser(); |
|
361 |
XMLReader xmlReader = saxParser.getXMLReader(); |
|
362 |
xmlReader.setContentHandler(parser); |
|
363 |
xmlReader.parse(new InputSource(new StringReader(body))); |
|
364 |
CloudServersException cse = parser.getException(); |
|
365 |
throw cse; |
|
366 |
} |
|
367 |
} catch (ClientProtocolException e) { |
|
368 |
CloudServersException cse = new CloudServersException(); |
|
369 |
cse.setMessage(e.getLocalizedMessage()); |
|
370 |
throw cse; |
|
371 |
} catch (IOException e) { |
|
372 |
CloudServersException cse = new CloudServersException(); |
|
373 |
cse.setMessage(e.getLocalizedMessage()); |
|
374 |
throw cse; |
|
375 |
} catch (ParserConfigurationException e) { |
|
376 |
CloudServersException cse = new CloudServersException(); |
|
377 |
cse.setMessage(e.getLocalizedMessage()); |
|
378 |
throw cse; |
|
379 |
} catch (SAXException e) { |
|
380 |
CloudServersException cse = new CloudServersException(); |
|
381 |
cse.setMessage(e.getLocalizedMessage()); |
|
382 |
throw cse; |
|
383 |
} catch (FactoryConfigurationError e) { |
|
384 |
CloudServersException cse = new CloudServersException(); |
|
385 |
cse.setMessage(e.getLocalizedMessage()); |
|
386 |
throw cse; |
|
387 |
} |
|
388 |
|
|
389 |
return containers; |
|
390 |
} |
|
309 | 391 |
|
310 | 392 |
private String getSafeURL(String badURL, String name) { |
311 | 393 |
URI uri = null; |
... | ... | |
326 | 408 |
return url.substring(0, url.length() - 1); |
327 | 409 |
} |
328 | 410 |
|
411 |
public ArrayList<Container> createOthersList(String user, boolean b) throws CloudServersException { |
|
412 |
Log.i(LOG,"Create List:"+Account.getAccount().getStorageUrl()); |
|
413 |
CustomHttpClient httpclient = new CustomHttpClient(context); |
|
414 |
HttpGet get = new HttpGet(Account.getAccount().getStorageUrl().replaceAll(Account.getAccount().getUsername(), user) |
|
415 |
+ "?format=xml"); |
|
416 |
ArrayList<Container> containers = new ArrayList<Container>(); |
|
417 |
get.addHeader(CustomHttpClient.X_STORAGE_TOKEN, Account.getAccount() |
|
418 |
.getStorageToken()); |
|
419 |
get.addHeader("Content-Type", "application/xml"); |
|
420 |
|
|
421 |
try { |
|
422 |
HttpResponse resp = httpclient.execute(get); |
|
423 |
List<GroupResource> groups = new ArrayList<GroupResource>(); |
|
424 |
BasicResponseHandler responseHandler = new BasicResponseHandler(); |
|
425 |
String body = responseHandler.handleResponse(resp); |
|
426 |
Log.i("ContainerManager", body); |
|
427 |
if (resp.getStatusLine().getStatusCode() == 200 |
|
428 |
|| resp.getStatusLine().getStatusCode() == 203) { |
|
429 |
ContainerXMLParser containerXMLParser = new ContainerXMLParser(); |
|
430 |
SAXParser saxParser = SAXParserFactory.newInstance() |
|
431 |
.newSAXParser(); |
|
432 |
XMLReader xmlReader = saxParser.getXMLReader(); |
|
433 |
xmlReader.setContentHandler(containerXMLParser); |
|
434 |
|
|
435 |
xmlReader.parse(new InputSource(new StringReader(body))); |
|
436 |
containers = containerXMLParser.getContainers(); |
|
437 |
for(Container c : containers){ |
|
438 |
c.setOtherUser(user); |
|
439 |
} |
|
440 |
} else { |
|
441 |
CloudServersFaultXMLParser parser = new CloudServersFaultXMLParser(); |
|
442 |
SAXParser saxParser = SAXParserFactory.newInstance() |
|
443 |
.newSAXParser(); |
|
444 |
XMLReader xmlReader = saxParser.getXMLReader(); |
|
445 |
xmlReader.setContentHandler(parser); |
|
446 |
xmlReader.parse(new InputSource(new StringReader(body))); |
|
447 |
CloudServersException cse = parser.getException(); |
|
448 |
throw cse; |
|
449 |
} |
|
450 |
} catch (ClientProtocolException e) { |
|
451 |
CloudServersException cse = new CloudServersException(); |
|
452 |
cse.setMessage(e.getLocalizedMessage()); |
|
453 |
throw cse; |
|
454 |
} catch (IOException e) { |
|
455 |
CloudServersException cse = new CloudServersException(); |
|
456 |
cse.setMessage(e.getLocalizedMessage()); |
|
457 |
throw cse; |
|
458 |
} catch (ParserConfigurationException e) { |
|
459 |
CloudServersException cse = new CloudServersException(); |
|
460 |
cse.setMessage(e.getLocalizedMessage()); |
|
461 |
throw cse; |
|
462 |
} catch (SAXException e) { |
|
463 |
CloudServersException cse = new CloudServersException(); |
|
464 |
cse.setMessage(e.getLocalizedMessage()); |
|
465 |
throw cse; |
|
466 |
} catch (FactoryConfigurationError e) { |
|
467 |
CloudServersException cse = new CloudServersException(); |
|
468 |
cse.setMessage(e.getLocalizedMessage()); |
|
469 |
throw cse; |
|
470 |
} |
|
471 |
|
|
472 |
return containers; |
|
473 |
} |
|
474 |
|
|
329 | 475 |
} |
Also available in: Unified diff