Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 9th, 2012  |  syntax: Java  |  size: 9.05 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. package com.gobbler;
  2.  
  3. import helpers.rest.RestHelper;
  4.  
  5. import java.io.IOException;
  6. import java.io.UnsupportedEncodingException;
  7. import java.lang.reflect.Array;
  8. import java.lang.reflect.Type;
  9. import java.util.ArrayList;
  10. import java.util.Collection;
  11. import java.util.Collections;
  12. import java.util.HashMap;
  13. import java.util.Map;
  14. import java.util.regex.Matcher;
  15. import java.util.regex.Pattern;
  16.  
  17. import org.apache.http.HttpResponse;
  18. import org.apache.http.client.HttpClient;
  19. import org.apache.http.client.methods.HttpPost;
  20. import org.apache.http.entity.StringEntity;
  21. import org.apache.http.impl.client.DefaultHttpClient;
  22. import org.json.JSONArray;
  23. import org.json.JSONException;
  24. import org.json.JSONObject;
  25.  
  26. import android.util.Log;
  27.  
  28. import com.gobbler.synchronization.AccountQuota;
  29. import com.gobbler.synchronization.File;
  30. import com.gobbler.synchronization.Machine;
  31. import com.gobbler.synchronization.MergeError;
  32. import com.gobbler.synchronization.Project;
  33. import com.gobbler.synchronization.Transfer;
  34. import com.gobbler.synchronization.User;
  35. import com.gobbler.synchronization.UserData;
  36. import com.gobbler.synchronization.Volume;
  37.  
  38. import com.google.gson.Gson;
  39. import com.google.gson.reflect.TypeToken;
  40.  
  41. public class GobblerClient {
  42.  
  43.         private static GobblerClient gobblerClient = null;
  44.  
  45.         private String client_key;
  46.         private Collection<AccountQuota> accountQuotas;
  47.         private Collection<Machine> machines;
  48.         private Collection<Project> projects;
  49.         private Collection<Transfer> transfers;
  50.         private Collection<User> users;
  51.         private Collection<UserData> userDatas;
  52.         private Collection<Volume> volumes;
  53.         private Collection<Folder> folders;
  54.  
  55.         public GobblerClient(String client_key) {
  56.                 this.client_key = client_key;
  57.         }
  58.        
  59.         public static GobblerClient initializeGobblerClient(String client_key) {
  60.                 if (client_key == null) {
  61.                         throw new RuntimeException("getGobblerClient must have a non-null clientKey");
  62.                 }
  63.                 if (gobblerClient == null) {
  64.                         gobblerClient = new GobblerClient(client_key);
  65.                 }
  66.                 return gobblerClient;
  67.         }
  68.  
  69.         public static GobblerClient getGobblerClient() {
  70.                 if (gobblerClient == null)
  71.                         throw new RuntimeException(
  72.                                         "getGobblerClient cannot return if GobblerClient has not been initialized");
  73.                 return gobblerClient;
  74.         }
  75.  
  76.         public String getJsonData(String method_type) {
  77.                 String jsonString = "{ \"objects\" : [] }";
  78.                 String url = String.format("http://api.gobbler.com/client_%s/sync_ask",
  79.                                 method_type);
  80.                 String response = GobblerRestClient.post(url, jsonString, this.client_key);
  81.                 JSONObject jsonObject = GobblerRestClient.convertToJSON(response);
  82.  
  83.                 String updates = null;
  84.                 try {
  85.                         updates = jsonObject.getJSONArray("updates").toString();
  86.                 } catch (JSONException e) {
  87.                         throw new ServerLoginError("Response from Gobbler server is unparseble");
  88.                 }
  89.  
  90.                 Log.d("getJsonData", updates);
  91.                 return updates;
  92.         }
  93.  
  94.         /*
  95.          * Below we have some number of methods that all do more or less the same
  96.          * thing. An attempt was made to abstract these using generics, but due to
  97.          * Java type erasure, it is impossible to such an abstract method. See:
  98.          * http://
  99.          * stackoverflow.com/questions/10356140/how-can-i-use-google-gson-to-deserialize
  100.          * -a-json-array-into-a-a-collection-of-a-g/10359955#10359955
  101.          */
  102.  
  103.         /*
  104.          * Fetch the users's account quotas.
  105.          */
  106.         public Collection<AccountQuota> getAccountQuotas() {
  107.                 if (this.accountQuotas != null)
  108.                         return this.accountQuotas;
  109.                
  110.                 String json = getJsonData("account_quota"); // TODO: Need to ensure that is
  111.                                                                                                                                                                                                 // a list, ie [1, 2, 3, 4]
  112.                 Gson gson = new Gson();
  113.                 // straight from gson docs. not sure what this does, but its apparently
  114.                 // unavoidable
  115.                 Type collectionType = new TypeToken<Collection<AccountQuota>>() {
  116.                 }.getType();
  117.                 return gson.fromJson(json, collectionType);
  118.         }
  119.  
  120.         /*
  121.          * Fetch the users's machines.
  122.          */
  123.         public Collection<Machine> getMachines() {
  124.                 if (this.machines != null)
  125.                         return this.machines;
  126.                
  127.                 String json = getJsonData("machine");
  128.                 Gson gson = new Gson();
  129.                 // straight from gson docs. not sure what this does, but its apparently
  130.                 // unavoidable
  131.                 Type collectionType = new TypeToken<Collection<Machine>>() {
  132.                 }.getType();
  133.                 return gson.fromJson(json, collectionType);
  134.         }
  135.  
  136.         /*
  137.          * Fetch the user's projects.
  138.          */
  139.         public Collection<Project> getProjects() {
  140.                 if (this.projects != null)
  141.                         return this.projects;
  142.                
  143.                 String json = getJsonData("project");
  144.                 Gson gson = new Gson();
  145.                 // straight from gson docs. not sure what this does, but its apparently
  146.                 // unavoidable
  147.                 Type collectionType = new TypeToken<Collection<Project>>() {
  148.                 }.getType();
  149.                 return gson.fromJson(json, collectionType);
  150.         }
  151.  
  152.         /*
  153.          * Fetch the users's transfers.
  154.          */
  155.         public Collection<Transfer> getTransfers() {
  156.                 if (this.transfers != null)
  157.                         return this.transfers;
  158.  
  159.                 String json = getJsonData("transfer");
  160.                 Gson gson = new Gson();
  161.                 // straight from gson docs. not sure what this does, but its apparently
  162.                 // unavoidable
  163.                 Type collectionType = new TypeToken<Collection<Transfer>>() {
  164.                 }.getType();
  165.                 return gson.fromJson(json, collectionType);
  166.         }
  167.  
  168.         /*
  169.          *
  170.          */
  171.         public Collection<User> getUsers() {
  172.                 if (this.users != null)
  173.                         return this.users;
  174.                
  175.                 String json = getJsonData("user");
  176.                 Gson gson = new Gson();
  177.                 // straight from gson docs. not sure what this does, but its apparently
  178.                 // unavoidable
  179.                 Type collectionType = new TypeToken<Collection<User>>() {
  180.                 }.getType();
  181.                 return gson.fromJson(json, collectionType);
  182.         }
  183.  
  184.         /*
  185.          * Fetch the users's user datas.
  186.          */
  187.         public Collection<UserData> getUserDatas() {
  188.                 if (this.userDatas != null)
  189.                         return this.userDatas;
  190.  
  191.                 String json = getJsonData("user_data");
  192.                 Gson gson = new Gson();
  193.                 // straight from gson docs. not sure what this does, but its apparently
  194.                 // unavoidable
  195.                 Type collectionType = new TypeToken<Collection<UserData>>() {
  196.                 }.getType();
  197.                 return gson.fromJson(json, collectionType);
  198.         }
  199.  
  200.         /*
  201.          * Fetch the users's volumes.
  202.          */
  203.         public Collection<Volume> getVolumes() {
  204.                 if (this.volumes != null)
  205.                         return this.volumes;
  206.  
  207.                 String json = getJsonData("volume");
  208.                 Gson gson = new Gson();
  209.                 // straight from gson docs. not sure what this does, but its apparently
  210.                 // unavoidable
  211.                 Type collectionType = new TypeToken<Collection<Volume>>() {
  212.                 }.getType();
  213.                 return gson.fromJson(json, collectionType);
  214.         }
  215.        
  216.         /*
  217.          * Get the root of a file system.
  218.          */
  219.         private File getRoot(Collection<File> files) {
  220.                 File root = null;
  221.                 for (File file : files) {
  222.                         if (file.isRoot()) {
  223.                                 root = file;
  224.                         }
  225.                 }
  226.                 assert root != null;
  227.                 return root;
  228.         }
  229.        
  230.         /*
  231.          * Given a list of files, arrange them into a tree.
  232.          */
  233.         public File buildFileSystem(Collection<File> files) throws MergeError {
  234.                 File root = getRoot(files);
  235.                 files.remove(root);
  236.                
  237.                 // Sort each of the files by number of ancestors to ensure that during each merge, the parent of the file already exists.
  238.                 // If a parent doesn't exist, then a MergeError is thrown.
  239.                 ArrayList<File> fileArray = new ArrayList<File>(files);
  240.                 Collections.sort(fileArray);
  241.                 for (File file : files) {
  242.                         root.merge(file.getAncestorNames(), file);
  243.                 }
  244.                
  245.                 return root;
  246.         }
  247.        
  248.         class Folder {
  249.                 private String volumeGuid;
  250.                 private String path;
  251.                 private String name;
  252.  
  253.                 public Folder(String volumeGuid, String path) {
  254.                         this.volumeGuid = volumeGuid;
  255.                         this.path = path;
  256.                         this.name = parseNameFromPath(path);
  257.                 }
  258.  
  259.                 public String getVolume() {
  260.                         return this.volumeGuid;
  261.                 }
  262.  
  263.                 public String getPath() {
  264.                         return this.path;
  265.                 }
  266.                
  267.                 public String getName() {
  268.                         return this.name;
  269.                 }
  270.                
  271.                 public String parseNameFromPath(String path) {
  272.                         Pattern p1 = Pattern.compile("([^\\/]+?)$");
  273.                         Matcher m1 = p1.matcher(path);
  274.                         String intermediaryString = m1.group(0);
  275.                        
  276.                         Pattern p2 = Pattern.compile("^[^.]+");
  277.                         Matcher m2 = p2.matcher(intermediaryString);
  278.                         return m2.group(0);
  279.                 }
  280.         }
  281.  
  282.        
  283.         public Collection<Folder> getFolders() {
  284.                 if (this.folders != null)
  285.                         return this.folders;
  286.                
  287.                 String jsonString = "{ \"objects\" : [] }";
  288.                 String url = "http://api.gobbler.com/client_catalog/sync_ask_folder";
  289.                 String response = GobblerRestClient.post(url, jsonString, client_key);
  290.                 JSONObject jsonObject = GobblerRestClient.convertToJSON(response);
  291.  
  292.                 JSONArray projects = null;
  293.                 try {
  294.                         projects = jsonObject.getJSONArray("updates");
  295.                 } catch (JSONException e) {
  296.                         throw new ServerLoginError("Response from Gobbler server is unparseble");
  297.                 }
  298.                
  299.                 folders = new ArrayList<Folder>();
  300.                 for (int i = 0; i < projects.length(); i++) {
  301.                         JSONObject project = null;
  302.                         try {
  303.                                 project = projects.getJSONObject(i);
  304.                         } catch (JSONException e) {
  305.                                 e.printStackTrace();
  306.                         }
  307.  
  308.                         String volumeGuid = null, path = null;
  309.                         try {
  310.                                 volumeGuid = project.getString("volume_guid");
  311.                                 path = project.getString("path");
  312.                         } catch (JSONException e) {
  313.                                 // TODO Auto-generated catch block
  314.                                 e.printStackTrace();
  315.                         }
  316.  
  317.                         Folder volumePath = new Folder(volumeGuid, path);
  318.                         folders.add(volumePath);
  319.                 }
  320.                 return this.folders;
  321.         }
  322.        
  323.        
  324. }