James Moger
2011-11-11 d65f712ea3d8941f4b9145c0630c30c20af80d13
src/com/gitblit/utils/RpcUtils.java
@@ -21,15 +21,16 @@
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import com.gitblit.Constants;
import com.gitblit.Constants.RpcRequest;
import com.gitblit.IStoredSettings;
import com.gitblit.models.FederationModel;
import com.gitblit.models.FederationProposal;
import com.gitblit.models.FederationSet;
import com.gitblit.models.FeedModel;
import com.gitblit.models.RepositoryModel;
import com.gitblit.models.ServerSettings;
import com.gitblit.models.ServerStatus;
import com.gitblit.models.UserModel;
import com.google.gson.reflect.TypeToken;
@@ -42,6 +43,9 @@
public class RpcUtils {
   public static final Type NAMES_TYPE = new TypeToken<Collection<String>>() {
   }.getType();
   public static final Type SETTINGS_TYPE = new TypeToken<Map<String, String>>() {
   }.getType();
   private static final Type REPOSITORIES_TYPE = new TypeToken<Map<String, RepositoryModel>>() {
@@ -57,6 +61,9 @@
   }.getType();
   private static final Type SETS_TYPE = new TypeToken<Collection<FederationSet>>() {
   }.getType();
   private static final Type BRANCHES_TYPE = new TypeToken<Map<String, Collection<String>>>() {
   }.getType();
   /**
@@ -139,6 +146,10 @@
    */
   public static boolean createRepository(RepositoryModel repository, String serverUrl,
         String account, char[] password) throws IOException {
      // ensure repository name ends with .git
      if (!repository.name.endsWith(".git")) {
         repository.name += ".git";
      }
      return doAction(RpcRequest.CREATE_REPOSITORY, null, repository, serverUrl, account,
            password);
@@ -338,15 +349,90 @@
    * @param serverUrl
    * @param account
    * @param password
    * @return an IStoredSettings object
    * @return an Settings object
    * @throws IOException
    */
   public static IStoredSettings getSettings(String serverUrl, String account, char[] password)
   public static ServerSettings getSettings(String serverUrl, String account, char[] password)
         throws IOException {
      String url = asLink(serverUrl, RpcRequest.LIST_SETTINGS);
      Properties props = JsonUtils.retrieveJson(url, Properties.class, account, password);
      RpcSettings settings = new RpcSettings(props);
      ServerSettings settings = JsonUtils.retrieveJson(url, ServerSettings.class, account,
            password);
      return settings;
   }
   /**
    * Update the settings on the Gitblit server.
    *
    * @param settings
    *            the settings to update
    * @param serverUrl
    * @param account
    * @param password
    * @return true if the action succeeded
    * @throws IOException
    */
   public static boolean updateSettings(Map<String, String> settings, String serverUrl,
         String account, char[] password) throws IOException {
      return doAction(RpcRequest.EDIT_SETTINGS, null, settings, serverUrl, account, password);
   }
   /**
    * Retrieves the server status object.
    *
    * @param serverUrl
    * @param account
    * @param password
    * @return an ServerStatus object
    * @throws IOException
    */
   public static ServerStatus getStatus(String serverUrl, String account, char[] password)
         throws IOException {
      String url = asLink(serverUrl, RpcRequest.LIST_STATUS);
      ServerStatus status = JsonUtils.retrieveJson(url, ServerStatus.class, account, password);
      return status;
   }
   /**
    * Retrieves a map of local branches in the Gitblit server keyed by
    * repository.
    *
    * @param serverUrl
    * @param account
    * @param password
    * @return
    * @throws IOException
    */
   public static Map<String, Collection<String>> getBranches(String serverUrl, String account,
         char[] password) throws IOException {
      String url = asLink(serverUrl, RpcRequest.LIST_BRANCHES);
      Map<String, Collection<String>> branches = JsonUtils.retrieveJson(url, BRANCHES_TYPE,
            account, password);
      return branches;
   }
   /**
    * Retrieves a list of available branch feeds in the Gitblit server.
    *
    * @param serverUrl
    * @param account
    * @param password
    * @return
    * @throws IOException
    */
   public static List<FeedModel> getBranchFeeds(String serverUrl, String account, char[] password)
         throws IOException {
      List<FeedModel> feeds = new ArrayList<FeedModel>();
      Map<String, Collection<String>> allBranches = getBranches(serverUrl, account, password);
      for (Map.Entry<String, Collection<String>> entry : allBranches.entrySet()) {
         for (String branch : entry.getValue()) {
            FeedModel feed = new FeedModel();
            feed.repository = entry.getKey();
            feed.branch = branch;
            feeds.add(feed);
         }
      }
      return feeds;
   }
   /**
@@ -368,32 +454,5 @@
      String json = JsonUtils.toJsonString(object);
      int resultCode = JsonUtils.sendJsonString(url, json, account, password);
      return resultCode == 200;
   }
   /**
    * Settings implementation that wraps a retrieved properties instance. This
    * class is used for RPC communication.
    *
    * @author James Moger
    *
    */
   private static class RpcSettings extends IStoredSettings {
      private final Properties properties = new Properties();
      public RpcSettings(Properties props) {
         super(RpcSettings.class);
         properties.putAll(props);
      }
      @Override
      protected Properties read() {
         return properties;
      }
      @Override
      public String toString() {
         return "RpcSettings";
      }
   }
}