Android Open Source - android-rackspacecloud Container Object Manager






From Project

Back to project page android-rackspacecloud.

License

The source code is released under:

MIT License

If you think the Android project android-rackspacecloud listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.rackspace.cloud.files.api.client;
/*from  ww  w . jav  a 2s  .  c o m*/
import java.io.IOException;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;

import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.protocol.RequestExpectContinue;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

import android.content.Context;
import android.util.Log;

import com.rackspace.cloud.files.api.client.parsers.ContainerObjectXMLparser;
import com.rackspace.cloud.servers.api.client.Account;
import com.rackspace.cloud.servers.api.client.CloudServersException;
import com.rackspace.cloud.servers.api.client.EntityManager;
import com.rackspace.cloud.servers.api.client.http.HttpBundle;
import com.rackspace.cloud.servers.api.client.parsers.CloudServersFaultXMLParser;

/** 
 * 
 * @author Phillip Toohill
 *
 */
public class ContainerObjectManager extends EntityManager {

  public String LOG = "ContainerObjectManager";
  private Context context;
  public static final String storageToken = Account.getAccount().getStorageToken();
  
  public ContainerObjectManager(Context context) {
    this.context = context;
  }

  public ArrayList<ContainerObjects> createList(boolean detail, String passName) throws CloudServersException {
    
    CustomHttpClient httpclient = new CustomHttpClient(context);
    String url = getSafeURL(Account.getAccount().getStorageUrl(), passName) + "?format=xml";
    HttpGet get = new HttpGet(url);
    ArrayList<ContainerObjects> files = new ArrayList<ContainerObjects>();
    
    
    get.addHeader("Content-Type", "application/xml");
    get.addHeader("X-Storage-Token", storageToken);
    
    
        
    try {      
      HttpResponse resp = httpclient.execute(get);        
        BasicResponseHandler responseHandler = new BasicResponseHandler();
        String body = responseHandler.handleResponse(resp);
        
        if (resp.getStatusLine().getStatusCode() == 200 || resp.getStatusLine().getStatusCode() == 203) {          
          ContainerObjectXMLparser filesXMLParser = new ContainerObjectXMLparser();
          SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
          XMLReader xmlReader = saxParser.getXMLReader();
          xmlReader.setContentHandler(filesXMLParser);
              
          xmlReader.parse(new InputSource(new StringReader(body)));
          files = filesXMLParser.getViewFiles();
          
        } else {
          CloudServersFaultXMLParser parser = new CloudServersFaultXMLParser();
          SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
          XMLReader xmlReader = saxParser.getXMLReader();
          xmlReader.setContentHandler(parser);
          xmlReader.parse(new InputSource(new StringReader(body)));          
          CloudServersException cse = parser.getException();          
          throw cse;
        }
    } catch (ClientProtocolException e) {
      CloudServersException cse = new CloudServersException();
      cse.setMessage(e.getLocalizedMessage());
      throw cse;
    } catch (IOException e) {
      CloudServersException cse = new CloudServersException();
      cse.setMessage(e.getLocalizedMessage());
      throw cse;
    } catch (ParserConfigurationException e) {
      CloudServersException cse = new CloudServersException();
      cse.setMessage(e.getLocalizedMessage());
      throw cse;
    } catch (SAXException e) {
      CloudServersException cse = new CloudServersException();
      cse.setMessage(e.getLocalizedMessage());
      throw cse;
    } catch (FactoryConfigurationError e) {
      CloudServersException cse = new CloudServersException();
      cse.setMessage(e.getLocalizedMessage());
      throw cse;
    }
    return files;
    
  }

  public HttpBundle deleteObject(String Container, String Object) throws CloudServersException {
    HttpResponse resp = null;
    CustomHttpClient httpclient = new CustomHttpClient(context);
    String url = getSafeURL(Account.getAccount().getStorageUrl(), Container + "/" + Object);
    HttpDelete deleteObject = new HttpDelete(url);
        
    deleteObject.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
    httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class);

    HttpBundle bundle = new HttpBundle();
    bundle.setCurlRequest(deleteObject);
    
    try {      
      resp = httpclient.execute(deleteObject);
      bundle.setHttpResponse(resp);
    } catch (ClientProtocolException e) {
      CloudServersException cse = new CloudServersException();
      cse.setMessage(e.getLocalizedMessage());
      throw cse;
    } catch (IOException e) {
      CloudServersException cse = new CloudServersException();
      cse.setMessage(e.getLocalizedMessage());
      throw cse;
    } catch (FactoryConfigurationError e) {
      CloudServersException cse = new CloudServersException();
      cse.setMessage(e.getLocalizedMessage());
      throw cse;
    }  
    return bundle;
  }
  
  public HttpBundle getObject(String Container, String Object) throws CloudServersException {
    HttpResponse resp = null;
    CustomHttpClient httpclient = new CustomHttpClient(context);
    String url = getSafeURL(Account.getAccount().getStorageUrl(), Container + "/" + Object);
    HttpGet getObject = new HttpGet(url);
    getObject.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
    httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class);

    HttpBundle bundle = new HttpBundle();
    bundle.setCurlRequest(getObject);
    
    try {      
      resp = httpclient.execute(getObject);
      bundle.setHttpResponse(resp);
    } catch (ClientProtocolException e) {
      CloudServersException cse = new CloudServersException();
      cse.setMessage(e.getLocalizedMessage());
      throw cse;
    } catch (IOException e) {
      CloudServersException cse = new CloudServersException();
      cse.setMessage(e.getLocalizedMessage());
      throw cse;
    } catch (FactoryConfigurationError e) {
      CloudServersException cse = new CloudServersException();
      cse.setMessage(e.getLocalizedMessage());
      throw cse;
    }  
    return bundle;
  }
  
  public HttpBundle addObject(String Container, String Path, String Object, String type) throws CloudServersException {
    HttpResponse resp = null;
    CustomHttpClient httpclient = new CustomHttpClient(context);
    String url = getSafeURL(Account.getAccount().getStorageUrl(), Container + "/" + Path + Object);
    HttpPut addObject = new HttpPut(url);
    
    addObject.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
    addObject.addHeader("Content-Type", type);
    httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class);
    
    HttpBundle bundle = new HttpBundle();
    bundle.setCurlRequest(addObject);

    try {      
      resp = httpclient.execute(addObject);
      bundle.setHttpResponse(resp);
    } catch (ClientProtocolException e) {
      CloudServersException cse = new CloudServersException();
      cse.setMessage(e.getLocalizedMessage());
      throw cse;
    } catch (IOException e) {
      CloudServersException cse = new CloudServersException();
      cse.setMessage(e.getLocalizedMessage());
      throw cse;
    } catch (FactoryConfigurationError e) {
      CloudServersException cse = new CloudServersException();
      cse.setMessage(e.getLocalizedMessage());
      throw cse;
    }  
    return bundle;
  }
  
  /*
   * used for adding text files, requires an extra parameter to 
   * store the data for the file
   */
  public HttpBundle addObject(String Container, String Path, String Object, String type, String data) throws CloudServersException {
    HttpResponse resp = null;
    CustomHttpClient httpclient = new CustomHttpClient(context);
    String url = getSafeURL(Account.getAccount().getStorageUrl(), Container + "/" + Path + Object);
    HttpPut addObject = new HttpPut(url);
        
    addObject.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
    addObject.addHeader("Content-Type", type);
    httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class);

    StringEntity tmp = null;
    try {
      tmp = new StringEntity(data);
    } catch (UnsupportedEncodingException e) {
      CloudServersException cse = new CloudServersException();
      cse.setMessage(e.getLocalizedMessage());
      throw cse;
    }
    addObject.setEntity(tmp);
    
    HttpBundle bundle = new HttpBundle();
    bundle.setCurlRequest(addObject);
    
    try {      
      resp = httpclient.execute(addObject);
      bundle.setHttpResponse(resp);
    } catch (ClientProtocolException e) {
      CloudServersException cse = new CloudServersException();
      cse.setMessage(e.getLocalizedMessage());
      throw cse;
    } catch (IOException e) {
      CloudServersException cse = new CloudServersException();
      cse.setMessage(e.getLocalizedMessage());
      throw cse;
    } catch (FactoryConfigurationError e) {
      CloudServersException cse = new CloudServersException();
      cse.setMessage(e.getLocalizedMessage());
      throw cse;
    }  
    return bundle;
  }
  
  private String getSafeURL(String badURL, String name){
    URI uri = null;
    try {
      uri = new URI("https", badURL.substring(8), "/" + name + "/", "");
    } catch (URISyntaxException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
    }
    String url = null;
    try {
      url = uri.toURL().toString();
    } catch (MalformedURLException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
    }
    return url.substring(0, url.length()-2);
  }

}




Java Source Code List

com.rackspace.cloud.files.api.client.ContainerManager.java
com.rackspace.cloud.files.api.client.ContainerObjectManager.java
com.rackspace.cloud.files.api.client.ContainerObjects.java
com.rackspace.cloud.files.api.client.Container.java
com.rackspace.cloud.files.api.client.CustomHttpClient.java
com.rackspace.cloud.files.api.client.parsers.ContainerObjectXMLparser.java
com.rackspace.cloud.files.api.client.parsers.ContainerXMLParser.java
com.rackspace.cloud.servers.api.client.Account.java
com.rackspace.cloud.servers.api.client.Backup.java
com.rackspace.cloud.servers.api.client.CloudServersException.java
com.rackspace.cloud.servers.api.client.EntityManager.java
com.rackspace.cloud.servers.api.client.Entity.java
com.rackspace.cloud.servers.api.client.FlavorManager.java
com.rackspace.cloud.servers.api.client.Flavor.java
com.rackspace.cloud.servers.api.client.ImageManager.java
com.rackspace.cloud.servers.api.client.Image.java
com.rackspace.cloud.servers.api.client.ServerManager.java
com.rackspace.cloud.servers.api.client.Server.java
com.rackspace.cloud.servers.api.client.http.Authentication.java
com.rackspace.cloud.servers.api.client.http.HttpBundle.java
com.rackspace.cloud.servers.api.client.parsers.CloudServersFaultXMLParser.java
com.rackspace.cloud.servers.api.client.parsers.FlavorsXMLParser.java
com.rackspace.cloud.servers.api.client.parsers.ImagesXMLParser.java
com.rackspace.cloud.servers.api.client.parsers.ServersXMLParser.java
com.rackspacecloud.android.AddAccountActivity.java
com.rackspacecloud.android.AddContainerActivity.java
com.rackspacecloud.android.AddFileActivity.java
com.rackspacecloud.android.AddServerActivity.java
com.rackspacecloud.android.AndroidCloudApplication.java
com.rackspacecloud.android.BackupServerActivity.java
com.rackspacecloud.android.Config.java
com.rackspacecloud.android.ConfirmResizeActivity.java
com.rackspacecloud.android.ContactActivity.java
com.rackspacecloud.android.ContainerObjectDetails.java
com.rackspacecloud.android.ContainerObjectsActivity.java
com.rackspacecloud.android.CreatePasswordActivity.java
com.rackspacecloud.android.EnableCDNActivity.java
com.rackspacecloud.android.ErrorDetailsActivity.java
com.rackspacecloud.android.GaActivity.java
com.rackspacecloud.android.GaListActivity.java
com.rackspacecloud.android.ListAccountsActivity.java
com.rackspacecloud.android.ListContainerActivity.java
com.rackspacecloud.android.ListServersActivity.java
com.rackspacecloud.android.PasswordManager.java
com.rackspacecloud.android.PasswordServerActivity.java
com.rackspacecloud.android.PingServerActivity.java
com.rackspacecloud.android.Preferences.java
com.rackspacecloud.android.ServerErrorActivity.java
com.rackspacecloud.android.TabViewActivity.java
com.rackspacecloud.android.ViewServerActivity.java
net.elasticgrid.rackspace.cloudservers.Addresses.java
net.elasticgrid.rackspace.cloudservers.BackupSchedule.java
net.elasticgrid.rackspace.cloudservers.CloudServersException.java
net.elasticgrid.rackspace.cloudservers.CloudServers.java
net.elasticgrid.rackspace.cloudservers.File.java
net.elasticgrid.rackspace.cloudservers.Flavor.java
net.elasticgrid.rackspace.cloudservers.HTTPVerb.java
net.elasticgrid.rackspace.cloudservers.Image.java
net.elasticgrid.rackspace.cloudservers.Limits.java
net.elasticgrid.rackspace.cloudservers.Personality.java
net.elasticgrid.rackspace.cloudservers.RebootType.java
net.elasticgrid.rackspace.cloudservers.Server.java
net.elasticgrid.rackspace.cloudservers.SharedIPGroup.java
net.elasticgrid.rackspace.cloudservers.XMLCloudServers.java
net.elasticgrid.rackspace.cloudservers.package-info.java
net.elasticgrid.rackspace.common.RackspaceConnection.java
net.elasticgrid.rackspace.common.RackspaceException.java
net.elasticgrid.rackspace.common.package-info.java
org.jclouds.date.joda.JodaDateService.java
org.jclouds.date.joda.config.JodaDateServiceModule.java
org.jclouds.encryption.bouncycastle.BouncyCastleEncryptionService.java
org.jclouds.encryption.bouncycastle.config.BouncyCastleEncryptionServiceModule.java
org.jclouds.enterprise.config.EnterpriseConfigurationModule.java
org.jclouds.gae.GaeHttpCommandExecutorService.java
org.jclouds.gae.config.GoogleAppEngineConfigurationModule.java
org.jclouds.http.apachehc.ApacheHCHttpCommandExecutorService.java
org.jclouds.http.apachehc.ApacheHCUtils.java
org.jclouds.http.apachehc.config.ApacheHCHttpCommandExecutorServiceModule.java
org.jclouds.http.httpnio.config.NioTransformingHttpCommandExecutorServiceModule.java
org.jclouds.http.httpnio.pool.NioHttpCommandConnectionHandle.java
org.jclouds.http.httpnio.pool.NioHttpCommandConnectionPool.java
org.jclouds.http.httpnio.pool.NioHttpCommandExecutionHandler.java
org.jclouds.http.httpnio.pool.NioTransformingHttpCommandExecutorService.java
org.jclouds.http.httpnio.util.NioHttpUtils.java
org.jclouds.http.pool.ConnectionPoolTransformingHttpCommandExecutorService.java
org.jclouds.http.pool.HttpCommandConnectionHandle.java
org.jclouds.http.pool.HttpCommandConnectionPool.java
org.jclouds.http.pool.config.ConnectionPoolCommandExecutorServiceModule.java
org.jclouds.logging.log4j.Log4JLogger.java
org.jclouds.logging.log4j.config.Log4JLoggingModule.java
org.jclouds.ssh.jsch.JschSshClient.java
org.jclouds.ssh.jsch.config.JschSshClientModule.java