From 31abc26dd0354bc2dafe27c011c2e54934a89486 Mon Sep 17 00:00:00 2001
From: James Moger <james.moger@gitblit.com>
Date: Sun, 02 Oct 2011 15:37:24 -0400
Subject: [PATCH] Fairly complete json rpc interface to view/control Gitblit data objects.
---
src/com/gitblit/utils/RpcUtils.java | 290 +++++++++++++++++++++++++++++++++++++++++++++++++++++++--
1 files changed, 279 insertions(+), 11 deletions(-)
diff --git a/src/com/gitblit/utils/RpcUtils.java b/src/com/gitblit/utils/RpcUtils.java
index 919c7bb..715ecb5 100644
--- a/src/com/gitblit/utils/RpcUtils.java
+++ b/src/com/gitblit/utils/RpcUtils.java
@@ -15,12 +15,18 @@
*/
package com.gitblit.utils;
+import java.io.IOException;
import java.lang.reflect.Type;
+import java.util.ArrayList;
import java.util.Collection;
+import java.util.List;
import java.util.Map;
import com.gitblit.Constants;
import com.gitblit.Constants.RpcRequest;
+import com.gitblit.models.FederationModel;
+import com.gitblit.models.FederationProposal;
+import com.gitblit.models.FederationSet;
import com.gitblit.models.RepositoryModel;
import com.gitblit.models.UserModel;
import com.google.gson.reflect.TypeToken;
@@ -33,10 +39,22 @@
*/
public class RpcUtils {
- public static final Type REPOSITORIES_TYPE = new TypeToken<Map<String, RepositoryModel>>() {
+ public static final Type NAMES_TYPE = new TypeToken<Collection<String>>() {
}.getType();
- public static final Type USERS_TYPE = new TypeToken<Collection<UserModel>>() {
+ private static final Type REPOSITORIES_TYPE = new TypeToken<Map<String, RepositoryModel>>() {
+ }.getType();
+
+ private static final Type USERS_TYPE = new TypeToken<Collection<UserModel>>() {
+ }.getType();
+
+ private static final Type REGISTRATIONS_TYPE = new TypeToken<Collection<FederationModel>>() {
+ }.getType();
+
+ private static final Type PROPOSALS_TYPE = new TypeToken<Collection<FederationProposal>>() {
+ }.getType();
+
+ private static final Type SETS_TYPE = new TypeToken<Collection<FederationSet>>() {
}.getType();
/**
@@ -48,26 +66,45 @@
* @return
*/
public static String asLink(String remoteURL, RpcRequest req) {
+ return asLink(remoteURL, req, null);
+ }
+
+ /**
+ *
+ * @param remoteURL
+ * the url of the remote gitblit instance
+ * @param req
+ * the rpc request type
+ * @param name
+ * the name of the actionable object
+ * @return
+ */
+ public static String asLink(String remoteURL, RpcRequest req, String name) {
if (remoteURL.length() > 0 && remoteURL.charAt(remoteURL.length() - 1) == '/') {
remoteURL = remoteURL.substring(0, remoteURL.length() - 1);
}
if (req == null) {
req = RpcRequest.LIST_REPOSITORIES;
}
- return remoteURL + Constants.RPC_PATH + "?req=" + req.name().toLowerCase();
+ return remoteURL + Constants.RPC_PATH + "?req=" + req.name().toLowerCase()
+ + (name == null ? "" : ("&name=" + name));
}
-
+
/**
* Retrieves a map of the repositories at the remote gitblit instance keyed
* by the repository clone url.
*
* @param serverUrl
+ * @param account
+ * @param password
* @return a map of cloneable repositories
- * @throws Exception
+ * @throws IOException
*/
- public static Map<String, RepositoryModel> getRepositories(String serverUrl) throws Exception {
+ public static Map<String, RepositoryModel> getRepositories(String serverUrl, String account,
+ char[] password) throws IOException {
String url = asLink(serverUrl, RpcRequest.LIST_REPOSITORIES);
- Map<String, RepositoryModel> models = JsonUtils.retrieveJson(url, REPOSITORIES_TYPE);
+ Map<String, RepositoryModel> models = JsonUtils.retrieveJson(url, REPOSITORIES_TYPE,
+ account, password);
return models;
}
@@ -75,12 +112,243 @@
* Tries to pull the gitblit user accounts from the remote gitblit instance.
*
* @param serverUrl
+ * @param account
+ * @param password
* @return a collection of UserModel objects
- * @throws Exception
+ * @throws IOException
*/
- public static Collection<UserModel> getUsers(String serverUrl) throws Exception {
+ public static List<UserModel> getUsers(String serverUrl, String account, char[] password)
+ throws IOException {
String url = asLink(serverUrl, RpcRequest.LIST_USERS);
- Collection<UserModel> models = JsonUtils.retrieveJson(url, USERS_TYPE);
- return models;
+ Collection<UserModel> models = JsonUtils.retrieveJson(url, USERS_TYPE, account, password);
+ List<UserModel> list = new ArrayList<UserModel>(models);
+ return list;
+ }
+
+ /**
+ * Create a repository on the Gitblit server.
+ *
+ * @param repository
+ * @param serverUrl
+ * @param account
+ * @param password
+ * @return true if the action succeeded
+ * @throws IOException
+ */
+ public static boolean createRepository(RepositoryModel repository, String serverUrl,
+ String account, char[] password) throws IOException {
+ return doAction(RpcRequest.CREATE_REPOSITORY, null, repository, serverUrl, account,
+ password);
+
+ }
+
+ /**
+ * Send a revised version of the repository model to the Gitblit server.
+ *
+ * @param repository
+ * @param serverUrl
+ * @param account
+ * @param password
+ * @return true if the action succeeded
+ * @throws IOException
+ */
+ public static boolean updateRepository(String repositoryName, RepositoryModel repository,
+ String serverUrl, String account, char[] password) throws IOException {
+ return doAction(RpcRequest.EDIT_REPOSITORY, repositoryName, repository, serverUrl, account,
+ password);
+ }
+
+ /**
+ * Delete a repository from the Gitblit server.
+ *
+ * @param repository
+ * @param serverUrl
+ * @param account
+ * @param password
+ * @return true if the action succeeded
+ * @throws IOException
+ */
+ public static boolean deleteRepository(RepositoryModel repository, String serverUrl,
+ String account, char[] password) throws IOException {
+ return doAction(RpcRequest.DELETE_REPOSITORY, null, repository, serverUrl, account,
+ password);
+
+ }
+
+ /**
+ * Create a user on the Gitblit server.
+ *
+ * @param user
+ * @param serverUrl
+ * @param account
+ * @param password
+ * @return true if the action succeeded
+ * @throws IOException
+ */
+ public static boolean createUser(UserModel user, String serverUrl, String account,
+ char[] password) throws IOException {
+ return doAction(RpcRequest.CREATE_USER, null, user, serverUrl, account, password);
+
+ }
+
+ /**
+ * Send a revised version of the user model to the Gitblit server.
+ *
+ * @param user
+ * @param serverUrl
+ * @param account
+ * @param password
+ * @return true if the action succeeded
+ * @throws IOException
+ */
+ public static boolean updateUser(String username, UserModel user, String serverUrl,
+ String account, char[] password) throws IOException {
+ return doAction(RpcRequest.EDIT_USER, username, user, serverUrl, account, password);
+
+ }
+
+ /**
+ * Deletes a user from the Gitblit server.
+ *
+ * @param user
+ * @param serverUrl
+ * @param account
+ * @param password
+ * @return true if the action succeeded
+ * @throws IOException
+ */
+ public static boolean deleteUser(UserModel user, String serverUrl, String account,
+ char[] password) throws IOException {
+ return doAction(RpcRequest.DELETE_USER, null, user, serverUrl, account, password);
+ }
+
+ /**
+ * Retrieves the list of users that can access the specified repository.
+ *
+ * @param repository
+ * @param serverUrl
+ * @param account
+ * @param password
+ * @return list of members
+ * @throws IOException
+ */
+ public static List<String> getRepositoryMembers(RepositoryModel repository, String serverUrl,
+ String account, char[] password) throws IOException {
+ String url = asLink(serverUrl, RpcRequest.LIST_REPOSITORY_MEMBERS, repository.name);
+ Collection<String> list = JsonUtils.retrieveJson(url, NAMES_TYPE, account, password);
+ return new ArrayList<String>(list);
+ }
+
+ /**
+ * Sets the repository membership list.
+ *
+ * @param repository
+ * @param memberships
+ * @param serverUrl
+ * @param account
+ * @param password
+ * @return true if the action succeeded
+ * @throws IOException
+ */
+ public static boolean setRepositoryMembers(RepositoryModel repository,
+ List<String> memberships, String serverUrl, String account, char[] password)
+ throws IOException {
+ return doAction(RpcRequest.SET_REPOSITORY_MEMBERS, repository.name, memberships, serverUrl,
+ account, password);
+ }
+
+ /**
+ * Retrieves the list of federation registrations. These are the list of
+ * registrations that this Gitblit instance is pulling from.
+ *
+ * @param serverUrl
+ * @param account
+ * @param password
+ * @return a collection of FederationRegistration objects
+ * @throws IOException
+ */
+ public static List<FederationModel> getFederationRegistrations(String serverUrl,
+ String account, char[] password) throws IOException {
+ String url = asLink(serverUrl, RpcRequest.LIST_FEDERATION_REGISTRATIONS);
+ Collection<FederationModel> registrations = JsonUtils.retrieveJson(url, REGISTRATIONS_TYPE,
+ account, password);
+ List<FederationModel> list = new ArrayList<FederationModel>(registrations);
+ return list;
+ }
+
+ /**
+ * Retrieves the list of federation result registrations. These are the
+ * results reported back to this Gitblit instance from a federation client.
+ *
+ * @param serverUrl
+ * @param account
+ * @param password
+ * @return a collection of FederationRegistration objects
+ * @throws IOException
+ */
+ public static List<FederationModel> getFederationResultRegistrations(String serverUrl,
+ String account, char[] password) throws IOException {
+ String url = asLink(serverUrl, RpcRequest.LIST_FEDERATION_RESULTS);
+ Collection<FederationModel> registrations = JsonUtils.retrieveJson(url, REGISTRATIONS_TYPE,
+ account, password);
+ List<FederationModel> list = new ArrayList<FederationModel>(registrations);
+ return list;
+ }
+
+ /**
+ * Retrieves the list of federation proposals.
+ *
+ * @param serverUrl
+ * @param account
+ * @param password
+ * @return a collection of FederationProposal objects
+ * @throws IOException
+ */
+ public static List<FederationProposal> getFederationProposals(String serverUrl,
+ String account, char[] password) throws IOException {
+ String url = asLink(serverUrl, RpcRequest.LIST_FEDERATION_PROPOSALS);
+ Collection<FederationProposal> proposals = JsonUtils.retrieveJson(url, PROPOSALS_TYPE,
+ account, password);
+ List<FederationProposal> list = new ArrayList<FederationProposal>(proposals);
+ return list;
+ }
+
+ /**
+ * Retrieves the list of federation repository sets.
+ *
+ * @param serverUrl
+ * @param account
+ * @param password
+ * @return a collection of FederationSet objects
+ * @throws IOException
+ */
+ public static List<FederationSet> getFederationSets(String serverUrl,
+ String account, char[] password) throws IOException {
+ String url = asLink(serverUrl, RpcRequest.LIST_FEDERATION_SETS);
+ Collection<FederationSet> sets = JsonUtils.retrieveJson(url, SETS_TYPE,
+ account, password);
+ List<FederationSet> list = new ArrayList<FederationSet>(sets);
+ return list;
+ }
+
+ /**
+ * Do the specified administrative action on the Gitblit server.
+ *
+ * @param request
+ * @param name
+ * the name of the object (may be null)
+ * @param object
+ * @param serverUrl
+ * @param account
+ * @param password
+ * @return true if the action succeeded
+ * @throws IOException
+ */
+ protected static boolean doAction(RpcRequest request, String name, Object object,
+ String serverUrl, String account, char[] password) throws IOException {
+ String url = asLink(serverUrl, request, name);
+ String json = JsonUtils.toJsonString(object);
+ int resultCode = JsonUtils.sendJsonString(url, json, account, password);
+ return resultCode == 200;
}
}
--
Gitblit v1.9.1