| | |
| | | import java.util.List;
|
| | | import java.util.Map;
|
| | |
|
| | | import javax.inject.Inject;
|
| | | import javax.inject.Singleton;
|
| | | import com.google.inject.Inject;
|
| | | import com.google.inject.Singleton;
|
| | | import javax.servlet.ServletException;
|
| | | import javax.servlet.http.HttpServletRequest;
|
| | | import javax.servlet.http.HttpServletResponse;
|
| | |
| | | * Handles remote procedure calls.
|
| | | *
|
| | | * @author James Moger
|
| | | *
|
| | | */
|
| | | @Singleton
|
| | | public class RpcServlet extends JsonServlet {
|
| | |
|
| | | private static final long serialVersionUID = 1L;
|
| | |
|
| | | public static final int PROTOCOL_VERSION = 6;
|
| | | public static final int PROTOCOL_VERSION = 8;
|
| | |
|
| | | private final IStoredSettings settings;
|
| | | private IStoredSettings settings;
|
| | |
|
| | | private final IGitblit gitblit;
|
| | | private IGitblit gitblit;
|
| | |
|
| | | @Inject
|
| | | public RpcServlet(IGitblit gitblit) {
|
| | |
|
| | | super();
|
| | |
|
| | | this.settings = gitblit.getSettings();
|
| | | public RpcServlet(IStoredSettings settings, IGitblit gitblit) {
|
| | | this.settings = settings;
|
| | | this.gitblit = gitblit;
|
| | | }
|
| | |
|
| | |
| | | * @throws java.io.IOException
|
| | | */
|
| | | @Override
|
| | | protected void processRequest(HttpServletRequest request, HttpServletResponse response)
|
| | | throws ServletException, IOException {
|
| | | protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException,
|
| | | IOException {
|
| | | RpcRequest reqType = RpcRequest.fromName(request.getParameter("req"));
|
| | | String objectName = request.getParameter("name");
|
| | | logger.info(MessageFormat.format("Rpc {0} request from {1}", reqType,
|
| | | request.getRemoteAddr()));
|
| | | logger.info(MessageFormat.format("Rpc {0} request from {1}", reqType, request.getRemoteAddr()));
|
| | |
|
| | | UserModel user = (UserModel) request.getUserPrincipal();
|
| | |
|
| | |
| | | result = PROTOCOL_VERSION;
|
| | | } else if (RpcRequest.LIST_REPOSITORIES.equals(reqType)) {
|
| | | // Determine the Gitblit clone url
|
| | | String gitblitUrl = HttpUtils.getGitblitURL(request);
|
| | | String gitblitUrl = settings.getString(Keys.web.canonicalUrl, null);
|
| | | if (StringUtils.isEmpty(gitblitUrl)) {
|
| | | gitblitUrl = HttpUtils.getGitblitURL(request);
|
| | | }
|
| | | StringBuilder sb = new StringBuilder();
|
| | | sb.append(gitblitUrl);
|
| | | sb.append(Constants.R_PATH);
|
| | |
| | | }
|
| | | if (model.isCollectingGarbage) {
|
| | | // skip garbage collecting repository
|
| | | logger.warn(MessageFormat.format("Temporarily excluding {0} from RPC, busy collecting garbage", model.name));
|
| | | logger.warn(MessageFormat.format("Temporarily excluding {0} from RPC, busy collecting garbage",
|
| | | model.name));
|
| | | continue;
|
| | | }
|
| | | // get local branches
|
| | |
| | | } catch (GitBlitException e) {
|
| | | response.setStatus(failureCode);
|
| | | }
|
| | | } else if (RpcRequest.FORK_REPOSITORY.equals(reqType)) {
|
| | | // fork repository
|
| | | RepositoryModel origin = gitblit.getRepositoryModel(objectName);
|
| | | if (origin == null) {
|
| | | // failed to find repository, error is logged by the repository
|
| | | // manager
|
| | | response.setStatus(failureCode);
|
| | | } else {
|
| | | if (user == null || !user.canFork(origin)) {
|
| | | logger.error("User {} is not permitted to fork '{}'!", user == null ? "anonymous" : user.username,
|
| | | objectName);
|
| | | response.setStatus(failureCode);
|
| | | } else {
|
| | | try {
|
| | | // fork the origin
|
| | | RepositoryModel fork = gitblit.fork(origin, user);
|
| | | if (fork == null) {
|
| | | logger.error("Failed to fork repository '{}'!", objectName);
|
| | | response.setStatus(failureCode);
|
| | | } else {
|
| | | logger.info("User {} has forked '{}'!", user.username, objectName);
|
| | | }
|
| | | } catch (GitBlitException e) {
|
| | | response.setStatus(failureCode);
|
| | | }
|
| | | }
|
| | | }
|
| | | } else if (RpcRequest.EDIT_REPOSITORY.equals(reqType)) {
|
| | | // edit repository
|
| | | RepositoryModel model = deserialize(request, response, RepositoryModel.class);
|
| | |
| | | // create user
|
| | | UserModel model = deserialize(request, response, UserModel.class);
|
| | | try {
|
| | | gitblit.updateUserModel(model.username, model, true);
|
| | | gitblit.addUser(model);
|
| | | } catch (GitBlitException e) {
|
| | | response.setStatus(failureCode);
|
| | | }
|
| | |
| | | username = model.username;
|
| | | }
|
| | | try {
|
| | | gitblit.updateUserModel(username, model, false);
|
| | | gitblit.reviseUser(username, model);
|
| | | } catch (GitBlitException e) {
|
| | | response.setStatus(failureCode);
|
| | | }
|
| | |
| | | // create team
|
| | | TeamModel model = deserialize(request, response, TeamModel.class);
|
| | | try {
|
| | | gitblit.updateTeamModel(model.name, model, true);
|
| | | gitblit.addTeam(model);
|
| | | } catch (GitBlitException e) {
|
| | | response.setStatus(failureCode);
|
| | | }
|
| | |
| | | teamname = model.name;
|
| | | }
|
| | | try {
|
| | | gitblit.updateTeamModel(teamname, model, false);
|
| | | gitblit.reviseTeam(teamname, model);
|
| | | } catch (GitBlitException e) {
|
| | | response.setStatus(failureCode);
|
| | | }
|
| | |
| | | } else if (RpcRequest.SET_REPOSITORY_MEMBER_PERMISSIONS.equals(reqType)) {
|
| | | // set the repository permissions for the specified users
|
| | | RepositoryModel model = gitblit.getRepositoryModel(objectName);
|
| | | Collection<RegistrantAccessPermission> permissions = deserialize(request, response, RpcUtils.REGISTRANT_PERMISSIONS_TYPE);
|
| | | Collection<RegistrantAccessPermission> permissions = deserialize(request, response,
|
| | | RpcUtils.REGISTRANT_PERMISSIONS_TYPE);
|
| | | result = gitblit.setUserAccessPermissions(model, permissions);
|
| | | } else if (RpcRequest.LIST_REPOSITORY_TEAMS.equals(reqType)) {
|
| | | // get repository teams
|
| | |
| | | } else if (RpcRequest.SET_REPOSITORY_TEAM_PERMISSIONS.equals(reqType)) {
|
| | | // set the repository permissions for the specified teams
|
| | | RepositoryModel model = gitblit.getRepositoryModel(objectName);
|
| | | Collection<RegistrantAccessPermission> permissions = deserialize(request, response, RpcUtils.REGISTRANT_PERMISSIONS_TYPE);
|
| | | Collection<RegistrantAccessPermission> permissions = deserialize(request, response,
|
| | | RpcUtils.REGISTRANT_PERMISSIONS_TYPE);
|
| | | result = gitblit.setTeamAccessPermissions(model, permissions);
|
| | | } else if (RpcRequest.LIST_FEDERATION_REGISTRATIONS.equals(reqType)) {
|
| | | // return the list of federation registrations
|
| | |
| | | } else if (RpcRequest.LIST_FEDERATION_SETS.equals(reqType)) {
|
| | | // return the list of federation sets
|
| | | if (allowAdmin && gitblit.canFederate()) {
|
| | | String gitblitUrl = HttpUtils.getGitblitURL(request);
|
| | | String gitblitUrl = settings.getString(Keys.web.canonicalUrl, null);
|
| | | if (StringUtils.isEmpty(gitblitUrl)) {
|
| | | gitblitUrl = HttpUtils.getGitblitURL(request);
|
| | | }
|
| | | result = gitblit.getFederationSets(gitblitUrl);
|
| | | } else {
|
| | | response.sendError(notAllowedCode);
|
| | |
| | | } else if (RpcRequest.EDIT_SETTINGS.equals(reqType)) {
|
| | | // update settings on the server
|
| | | if (allowAdmin) {
|
| | | Map<String, String> map = deserialize(request, response,
|
| | | RpcUtils.SETTINGS_TYPE);
|
| | | Map<String, String> map = deserialize(request, response, RpcUtils.SETTINGS_TYPE);
|
| | | gitblit.updateSettings(map);
|
| | | } else {
|
| | | response.sendError(notAllowedCode);
|
| | |
| | | } else {
|
| | | response.sendError(notAllowedCode);
|
| | | }
|
| | | } else if (RpcRequest.REINDEX_TICKETS.equals(reqType)) {
|
| | | if (allowManagement) {
|
| | | if (StringUtils.isEmpty(objectName)) {
|
| | | // reindex all tickets
|
| | | gitblit.getTicketService().reindex();
|
| | | } else {
|
| | | // reindex tickets in a specific repository
|
| | | RepositoryModel model = gitblit.getRepositoryModel(objectName);
|
| | | gitblit.getTicketService().reindex(model);
|
| | | }
|
| | | } else {
|
| | | response.sendError(notAllowedCode);
|
| | | }
|
| | | }
|
| | |
|
| | | // send the result of the request
|