| | |
| | | import java.util.HashMap;
|
| | | import java.util.List;
|
| | | import java.util.Map;
|
| | | import java.util.Properties;
|
| | |
|
| | | import javax.servlet.ServletException;
|
| | | import javax.servlet.http.HttpServletRequest;
|
| | | import javax.servlet.http.HttpServletResponse;
|
| | |
|
| | | import org.eclipse.jgit.lib.Repository;
|
| | |
|
| | | import com.gitblit.Constants.RpcRequest;
|
| | | import com.gitblit.models.RefModel;
|
| | | import com.gitblit.models.RepositoryModel;
|
| | | import com.gitblit.models.ServerSettings;
|
| | | import com.gitblit.models.TeamModel;
|
| | | import com.gitblit.models.UserModel;
|
| | | import com.gitblit.utils.HttpUtils;
|
| | | import com.gitblit.utils.JGitUtils;
|
| | | import com.gitblit.utils.RpcUtils;
|
| | |
|
| | | /**
|
| | |
| | | public class RpcServlet extends JsonServlet {
|
| | |
|
| | | private static final long serialVersionUID = 1L;
|
| | |
|
| | | public static final int PROTOCOL_VERSION = 4;
|
| | |
|
| | | public RpcServlet() {
|
| | | super();
|
| | |
| | |
|
| | | UserModel user = (UserModel) request.getUserPrincipal();
|
| | |
|
| | | boolean allowManagement = user != null && user.canAdmin
|
| | | && GitBlit.getBoolean(Keys.web.enableRpcManagement, false);
|
| | |
|
| | | boolean allowAdmin = user != null && user.canAdmin
|
| | | && GitBlit.getBoolean(Keys.web.enableRpcAdministration, false);
|
| | |
|
| | | Object result = null;
|
| | | if (RpcRequest.LIST_REPOSITORIES.equals(reqType)) {
|
| | | if (RpcRequest.GET_PROTOCOL.equals(reqType)) {
|
| | | // Return the protocol version
|
| | | result = PROTOCOL_VERSION;
|
| | | } else if (RpcRequest.LIST_REPOSITORIES.equals(reqType)) {
|
| | | // Determine the Gitblit clone url
|
| | | String gitblitUrl = HttpUtils.getGitblitURL(request);
|
| | | StringBuilder sb = new StringBuilder();
|
| | |
| | | repositories.put(url, model);
|
| | | }
|
| | | result = repositories;
|
| | | } else if (RpcRequest.LIST_BRANCHES.equals(reqType)) {
|
| | | // list all local branches in all repositories accessible to user
|
| | | Map<String, List<String>> localBranches = new HashMap<String, List<String>>();
|
| | | List<RepositoryModel> models = GitBlit.self().getRepositoryModels(user);
|
| | | for (RepositoryModel model : models) {
|
| | | if (!model.hasCommits) {
|
| | | // skip empty repository
|
| | | continue;
|
| | | }
|
| | | // get local branches
|
| | | Repository repository = GitBlit.self().getRepository(model.name);
|
| | | List<RefModel> refs = JGitUtils.getLocalBranches(repository, false, -1);
|
| | | if (model.showRemoteBranches) {
|
| | | // add remote branches if repository displays them
|
| | | refs.addAll(JGitUtils.getRemoteBranches(repository, false, -1));
|
| | | }
|
| | | if (refs.size() > 0) {
|
| | | List<String> branches = new ArrayList<String>();
|
| | | for (RefModel ref : refs) {
|
| | | branches.add(ref.getName());
|
| | | }
|
| | | localBranches.put(model.name, branches);
|
| | | }
|
| | | repository.close();
|
| | | }
|
| | | result = localBranches;
|
| | | } else if (RpcRequest.LIST_USERS.equals(reqType)) {
|
| | | // list users
|
| | | List<String> names = GitBlit.self().getAllUsernames();
|
| | |
| | | users.add(GitBlit.self().getUserModel(name));
|
| | | }
|
| | | result = users;
|
| | | } else if (RpcRequest.LIST_TEAMS.equals(reqType)) {
|
| | | // list teams
|
| | | List<String> names = GitBlit.self().getAllTeamnames();
|
| | | List<TeamModel> teams = new ArrayList<TeamModel>();
|
| | | for (String name : names) {
|
| | | teams.add(GitBlit.self().getTeamModel(name));
|
| | | }
|
| | | result = teams;
|
| | | } else if (RpcRequest.CREATE_REPOSITORY.equals(reqType)) {
|
| | | // create repository
|
| | | RepositoryModel model = deserialize(request, response, RepositoryModel.class);
|
| | | GitBlit.self().updateRepositoryModel(model.name, model, true);
|
| | | try {
|
| | | GitBlit.self().updateRepositoryModel(model.name, model, true);
|
| | | } catch (GitBlitException e) {
|
| | | response.setStatus(failureCode);
|
| | | }
|
| | | } else if (RpcRequest.EDIT_REPOSITORY.equals(reqType)) {
|
| | | // edit repository
|
| | | RepositoryModel model = deserialize(request, response, RepositoryModel.class);
|
| | |
| | | if (repoName == null) {
|
| | | repoName = model.name;
|
| | | }
|
| | | GitBlit.self().updateRepositoryModel(repoName, model, false);
|
| | | try {
|
| | | GitBlit.self().updateRepositoryModel(repoName, model, false);
|
| | | } catch (GitBlitException e) {
|
| | | response.setStatus(failureCode);
|
| | | }
|
| | | } else if (RpcRequest.DELETE_REPOSITORY.equals(reqType)) {
|
| | | // delete repository
|
| | | RepositoryModel model = deserialize(request, response, RepositoryModel.class);
|
| | |
| | | } else if (RpcRequest.CREATE_USER.equals(reqType)) {
|
| | | // create user
|
| | | UserModel model = deserialize(request, response, UserModel.class);
|
| | | GitBlit.self().updateUserModel(model.username, model, true);
|
| | | try {
|
| | | GitBlit.self().updateUserModel(model.username, model, true);
|
| | | } catch (GitBlitException e) {
|
| | | response.setStatus(failureCode);
|
| | | }
|
| | | } else if (RpcRequest.EDIT_USER.equals(reqType)) {
|
| | | // edit user
|
| | | UserModel model = deserialize(request, response, UserModel.class);
|
| | |
| | | if (username == null) {
|
| | | username = model.username;
|
| | | }
|
| | | GitBlit.self().updateUserModel(username, model, false);
|
| | | try {
|
| | | GitBlit.self().updateUserModel(username, model, false);
|
| | | } catch (GitBlitException e) {
|
| | | response.setStatus(failureCode);
|
| | | }
|
| | | } else if (RpcRequest.DELETE_USER.equals(reqType)) {
|
| | | // delete user
|
| | | UserModel model = deserialize(request, response, UserModel.class);
|
| | | GitBlit.self().deleteUser(model.username);
|
| | | if (!GitBlit.self().deleteUser(model.username)) {
|
| | | response.setStatus(failureCode);
|
| | | }
|
| | | } else if (RpcRequest.CREATE_TEAM.equals(reqType)) {
|
| | | // create team
|
| | | TeamModel model = deserialize(request, response, TeamModel.class);
|
| | | try {
|
| | | GitBlit.self().updateTeamModel(model.name, model, true);
|
| | | } catch (GitBlitException e) {
|
| | | response.setStatus(failureCode);
|
| | | }
|
| | | } else if (RpcRequest.EDIT_TEAM.equals(reqType)) {
|
| | | // edit team
|
| | | TeamModel model = deserialize(request, response, TeamModel.class);
|
| | | // name parameter specifies original team name in event of rename
|
| | | String teamname = objectName;
|
| | | if (teamname == null) {
|
| | | teamname = model.name;
|
| | | }
|
| | | try {
|
| | | GitBlit.self().updateTeamModel(teamname, model, false);
|
| | | } catch (GitBlitException e) {
|
| | | response.setStatus(failureCode);
|
| | | }
|
| | | } else if (RpcRequest.DELETE_TEAM.equals(reqType)) {
|
| | | // delete team
|
| | | TeamModel model = deserialize(request, response, TeamModel.class);
|
| | | if (!GitBlit.self().deleteTeam(model.name)) {
|
| | | response.setStatus(failureCode);
|
| | | }
|
| | | } else if (RpcRequest.LIST_REPOSITORY_MEMBERS.equals(reqType)) {
|
| | | // get repository members
|
| | | RepositoryModel model = GitBlit.self().getRepositoryModel(objectName);
|
| | |
| | | Collection<String> names = deserialize(request, response, RpcUtils.NAMES_TYPE);
|
| | | List<String> users = new ArrayList<String>(names);
|
| | | if (!GitBlit.self().setRepositoryUsers(model, users)) {
|
| | | response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
|
| | | response.setStatus(failureCode);
|
| | | }
|
| | | } else if (RpcRequest.LIST_REPOSITORY_TEAMS.equals(reqType)) {
|
| | | // get repository teams
|
| | | RepositoryModel model = GitBlit.self().getRepositoryModel(objectName);
|
| | | result = GitBlit.self().getRepositoryTeams(model);
|
| | | } else if (RpcRequest.SET_REPOSITORY_TEAMS.equals(reqType)) {
|
| | | // update repository team access list
|
| | | RepositoryModel model = GitBlit.self().getRepositoryModel(objectName);
|
| | | Collection<String> names = deserialize(request, response, RpcUtils.NAMES_TYPE);
|
| | | List<String> teams = new ArrayList<String>(names);
|
| | | if (!GitBlit.self().setRepositoryTeams(model, teams)) {
|
| | | response.setStatus(failureCode);
|
| | | }
|
| | | } else if (RpcRequest.LIST_FEDERATION_REGISTRATIONS.equals(reqType)) {
|
| | | // return the list of federation registrations
|
| | | result = GitBlit.self().getFederationRegistrations();
|
| | | if (allowAdmin) {
|
| | | result = GitBlit.self().getFederationRegistrations();
|
| | | } else {
|
| | | response.sendError(notAllowedCode);
|
| | | }
|
| | | } else if (RpcRequest.LIST_FEDERATION_RESULTS.equals(reqType)) {
|
| | | // return the list of federation result registrations
|
| | | if (GitBlit.canFederate()) {
|
| | | if (allowAdmin && GitBlit.canFederate()) {
|
| | | result = GitBlit.self().getFederationResultRegistrations();
|
| | | } else {
|
| | | response.sendError(HttpServletResponse.SC_FORBIDDEN);
|
| | | response.sendError(notAllowedCode);
|
| | | }
|
| | | } else if (RpcRequest.LIST_FEDERATION_PROPOSALS.equals(reqType)) {
|
| | | // return the list of federation proposals
|
| | | if (GitBlit.canFederate()) {
|
| | | if (allowAdmin && GitBlit.canFederate()) {
|
| | | result = GitBlit.self().getPendingFederationProposals();
|
| | | } else {
|
| | | response.sendError(HttpServletResponse.SC_FORBIDDEN);
|
| | | response.sendError(notAllowedCode);
|
| | | }
|
| | | } else if (RpcRequest.LIST_FEDERATION_SETS.equals(reqType)) {
|
| | | // return the list of federation sets
|
| | | if (GitBlit.canFederate()) {
|
| | | if (allowAdmin && GitBlit.canFederate()) {
|
| | | String gitblitUrl = HttpUtils.getGitblitURL(request);
|
| | | result = GitBlit.self().getFederationSets(gitblitUrl);
|
| | | } else {
|
| | | response.sendError(HttpServletResponse.SC_FORBIDDEN);
|
| | | response.sendError(notAllowedCode);
|
| | | }
|
| | | } else if (RpcRequest.LIST_SETTINGS.equals(reqType)) {
|
| | | // return the server's settings
|
| | | Properties settings = new Properties(); |
| | | List<String> keys = GitBlit.getAllKeys(null);
|
| | | for (String key:keys) {
|
| | | String value = GitBlit.getString(key, null);
|
| | | if (value != null) {
|
| | | settings.put(key, value);
|
| | | ServerSettings settings = GitBlit.self().getSettingsModel();
|
| | | if (allowAdmin) {
|
| | | // return all settings
|
| | | result = settings;
|
| | | } else {
|
| | | // anonymous users get a few settings to allow browser launching
|
| | | List<String> keys = new ArrayList<String>();
|
| | | keys.add(Keys.web.siteName);
|
| | | keys.add(Keys.web.mountParameters);
|
| | | keys.add(Keys.web.syndicationEntries);
|
| | |
|
| | | if (allowManagement) {
|
| | | // keys necessary for repository and/or user management
|
| | | keys.add(Keys.realm.minPasswordLength);
|
| | | keys.add(Keys.realm.passwordStorage);
|
| | | keys.add(Keys.federation.sets);
|
| | | }
|
| | | // build the settings
|
| | | ServerSettings managementSettings = new ServerSettings();
|
| | | for (String key : keys) {
|
| | | managementSettings.add(settings.get(key));
|
| | | }
|
| | | if (allowManagement) {
|
| | | managementSettings.pushScripts = settings.pushScripts;
|
| | | }
|
| | | result = managementSettings;
|
| | | }
|
| | | result = settings;
|
| | | } else if (RpcRequest.EDIT_SETTINGS.equals(reqType)) {
|
| | | // update settings on the server
|
| | | if (allowAdmin) {
|
| | | Map<String, String> settings = deserialize(request, response,
|
| | | RpcUtils.SETTINGS_TYPE);
|
| | | GitBlit.self().updateSettings(settings);
|
| | | } else {
|
| | | response.sendError(notAllowedCode);
|
| | | }
|
| | | } else if (RpcRequest.LIST_STATUS.equals(reqType)) {
|
| | | // return the server's status information
|
| | | if (allowAdmin) {
|
| | | result = GitBlit.self().getStatus();
|
| | | } else {
|
| | | response.sendError(notAllowedCode);
|
| | | }
|
| | | } else if (RpcRequest.CLEAR_REPOSITORY_CACHE.equals(reqType)) {
|
| | | // clear the repository list cache
|
| | | if (allowManagement) {
|
| | | GitBlit.self().resetRepositoryListCache();
|
| | | } else {
|
| | | response.sendError(notAllowedCode);
|
| | | }
|
| | | }
|
| | |
|
| | | // send the result of the request
|