Android Open Source - android-rackspacecloud Container 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   w w w.  j  a  va 2 s.c  om
import java.io.IOException;
import java.io.StringReader;
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.HttpPost;
import org.apache.http.client.methods.HttpPut;
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.text.Editable;
import android.util.Log;

import com.rackspace.cloud.files.api.client.parsers.ContainerXMLParser;
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 ContainerManager extends EntityManager {
  private Context context;
  
  public ContainerManager(Context context) {
    this.context = context;
  }

  public HttpBundle create(Editable editable) throws CloudServersException {
    HttpResponse resp = null;
    CustomHttpClient httpclient = new CustomHttpClient(context);
    
    String url = getSafeURL(Account.getAccount().getStorageUrl(), editable.toString());
    HttpPut put = new HttpPut(url);
    
    put.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
    httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class);

    HttpBundle bundle = new HttpBundle();
    bundle.setCurlRequest(put);
    
    try {
      resp = httpclient.execute(put);
      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 ArrayList<Container> createCDNList(boolean detail) throws CloudServersException {
    
    CustomHttpClient httpclient = new CustomHttpClient(context);
    HttpGet get = new HttpGet(Account.getAccount().getCdnManagementUrl()+"?format=xml");
    ArrayList<Container> cdnContainers = new ArrayList<Container>();
    
    get.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
    
    try {      
      HttpResponse resp = httpclient.execute(get);        
        BasicResponseHandler responseHandler = new BasicResponseHandler();
        String body = responseHandler.handleResponse(resp);
        
        if (resp.getStatusLine().getStatusCode() == 200) {          
          ContainerXMLParser cdnContainerXMLParser = new ContainerXMLParser();
          SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
          XMLReader xmlReader = saxParser.getXMLReader();
          xmlReader.setContentHandler(cdnContainerXMLParser);
              
          xmlReader.parse(new InputSource(new StringReader(body)));
          cdnContainers = cdnContainerXMLParser.getContainers();          
        } 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 cdnContainers;
  }

  
  public HttpBundle enable(String container, String ttl, String logRet)
      throws CloudServersException {
    HttpResponse resp = null;
    CustomHttpClient httpclient = new CustomHttpClient(context);
    String url = getSafeURL(Account.getAccount().getCdnManagementUrl(), container);
    HttpPut put = new HttpPut(url);

    put.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
    put.addHeader("X-TTL", ttl);
    put.addHeader("X-Log-Retention", logRet);
    Log.v("cdn manager", ttl + container + logRet);
    httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class);

    HttpBundle bundle = new HttpBundle();
    bundle.setCurlRequest(put);
    
    try {
      resp = httpclient.execute(put);
      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 disable(String container, String cdn, String ttl, String logRet)
  throws CloudServersException {
       HttpResponse resp = null;
       CustomHttpClient httpclient = new CustomHttpClient(context);
      String url = getSafeURL(Account.getAccount().getCdnManagementUrl(), container);
         HttpPost post = new HttpPost(url);
         
         post.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
           post.addHeader("X-TTL", ttl);
           post.addHeader("X-Log-Retention", logRet);
           post.addHeader("X-CDN-Enabled", cdn);
           httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class);

           HttpBundle bundle = new HttpBundle();
        bundle.setCurlRequest(post);
           
           try {
             resp = httpclient.execute(post);
             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 delete(String string) throws CloudServersException {
    HttpResponse resp = null;
    CustomHttpClient httpclient = new CustomHttpClient(context);
    String url = getSafeURL(Account.getAccount().getStorageUrl(), string);
    HttpDelete put = new HttpDelete(url);
    
    put.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
    httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class);

    HttpBundle bundle = new HttpBundle();
    bundle.setCurlRequest(put);
    
    try {
      resp = httpclient.execute(put);
      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 ArrayList<Container> createList(boolean detail)
      throws CloudServersException {

    CustomHttpClient httpclient = new CustomHttpClient(context);
    HttpGet get = new HttpGet(Account.getAccount().getStorageUrl() + "?format=xml");
    ArrayList<Container> containers = new ArrayList<Container>();

    get.addHeader("X-Storage-Token", Account.getAccount().getStorageToken());
    get.addHeader("Content-Type", "application/xml");

    try {
      HttpResponse resp = httpclient.execute(get);
      BasicResponseHandler responseHandler = new BasicResponseHandler();
      String body = responseHandler.handleResponse(resp);

      if (resp.getStatusLine().getStatusCode() == 200
          || resp.getStatusLine().getStatusCode() == 203) {
        ContainerXMLParser containerXMLParser = new ContainerXMLParser();
        SAXParser saxParser = SAXParserFactory.newInstance()
            .newSAXParser();
        XMLReader xmlReader = saxParser.getXMLReader();
        xmlReader.setContentHandler(containerXMLParser);

        xmlReader.parse(new InputSource(new StringReader(body)));
        containers = containerXMLParser.getContainers();
      } 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 containers;
  }
  
  private String getSafeURL(String badURL, String name){
    URI uri = null;
    try {
      uri = new URI("https", badURL.substring(8), "/" + name.toString()+"/", "");
    } 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()-1);
  }

}




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