commit | author | age
|
93f0b1
|
1 |
/*
|
JM |
2 |
* Copyright 2011 gitblit.com.
|
|
3 |
*
|
|
4 |
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5 |
* you may not use this file except in compliance with the License.
|
|
6 |
* You may obtain a copy of the License at
|
|
7 |
*
|
|
8 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9 |
*
|
|
10 |
* Unless required by applicable law or agreed to in writing, software
|
|
11 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13 |
* See the License for the specific language governing permissions and
|
|
14 |
* limitations under the License.
|
|
15 |
*/
|
|
16 |
package com.gitblit;
|
|
17 |
|
ca9d0f
|
18 |
import java.io.IOException;
|
93f0b1
|
19 |
import java.text.MessageFormat;
|
JM |
20 |
import java.util.ArrayList;
|
31abc2
|
21 |
import java.util.Collection;
|
93f0b1
|
22 |
import java.util.HashMap;
|
JM |
23 |
import java.util.List;
|
|
24 |
import java.util.Map;
|
|
25 |
|
ca9d0f
|
26 |
import javax.servlet.ServletException;
|
JM |
27 |
import javax.servlet.http.HttpServletRequest;
|
93f0b1
|
28 |
import javax.servlet.http.HttpServletResponse;
|
JM |
29 |
|
c75304
|
30 |
import org.eclipse.jgit.lib.Repository;
|
JM |
31 |
|
93f0b1
|
32 |
import com.gitblit.Constants.RpcRequest;
|
822dfe
|
33 |
import com.gitblit.models.RegistrantAccessPermission;
|
c75304
|
34 |
import com.gitblit.models.RefModel;
|
93f0b1
|
35 |
import com.gitblit.models.RepositoryModel;
|
284a7b
|
36 |
import com.gitblit.models.ServerSettings;
|
f08aab
|
37 |
import com.gitblit.models.TeamModel;
|
93f0b1
|
38 |
import com.gitblit.models.UserModel;
|
JM |
39 |
import com.gitblit.utils.HttpUtils;
|
c75304
|
40 |
import com.gitblit.utils.JGitUtils;
|
31abc2
|
41 |
import com.gitblit.utils.RpcUtils;
|
93f0b1
|
42 |
|
JM |
43 |
/**
|
|
44 |
* Handles remote procedure calls.
|
|
45 |
*
|
|
46 |
* @author James Moger
|
|
47 |
*
|
|
48 |
*/
|
|
49 |
public class RpcServlet extends JsonServlet {
|
|
50 |
|
|
51 |
private static final long serialVersionUID = 1L;
|
f08aab
|
52 |
|
822dfe
|
53 |
public static final int PROTOCOL_VERSION = 5;
|
93f0b1
|
54 |
|
JM |
55 |
public RpcServlet() {
|
|
56 |
super();
|
|
57 |
}
|
|
58 |
|
|
59 |
/**
|
|
60 |
* Processes an rpc request.
|
|
61 |
*
|
|
62 |
* @param request
|
|
63 |
* @param response
|
|
64 |
* @throws javax.servlet.ServletException
|
|
65 |
* @throws java.io.IOException
|
|
66 |
*/
|
|
67 |
@Override
|
ca9d0f
|
68 |
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
|
JM |
69 |
throws ServletException, IOException {
|
93f0b1
|
70 |
RpcRequest reqType = RpcRequest.fromName(request.getParameter("req"));
|
31abc2
|
71 |
String objectName = request.getParameter("name");
|
93f0b1
|
72 |
logger.info(MessageFormat.format("Rpc {0} request from {1}", reqType,
|
JM |
73 |
request.getRemoteAddr()));
|
284a7b
|
74 |
|
ca9d0f
|
75 |
UserModel user = (UserModel) request.getUserPrincipal();
|
ec5a88
|
76 |
|
7f7051
|
77 |
boolean allowManagement = user != null && user.canAdmin()
|
ec5a88
|
78 |
&& GitBlit.getBoolean(Keys.web.enableRpcManagement, false);
|
JM |
79 |
|
7f7051
|
80 |
boolean allowAdmin = user != null && user.canAdmin()
|
ec5a88
|
81 |
&& GitBlit.getBoolean(Keys.web.enableRpcAdministration, false);
|
93f0b1
|
82 |
|
JM |
83 |
Object result = null;
|
f08aab
|
84 |
if (RpcRequest.GET_PROTOCOL.equals(reqType)) {
|
JM |
85 |
// Return the protocol version
|
|
86 |
result = PROTOCOL_VERSION;
|
|
87 |
} else if (RpcRequest.LIST_REPOSITORIES.equals(reqType)) {
|
93f0b1
|
88 |
// Determine the Gitblit clone url
|
JM |
89 |
String gitblitUrl = HttpUtils.getGitblitURL(request);
|
|
90 |
StringBuilder sb = new StringBuilder();
|
|
91 |
sb.append(gitblitUrl);
|
|
92 |
sb.append(Constants.GIT_PATH);
|
|
93 |
sb.append("{0}");
|
|
94 |
String cloneUrl = sb.toString();
|
|
95 |
|
ca9d0f
|
96 |
// list repositories
|
93f0b1
|
97 |
List<RepositoryModel> list = GitBlit.self().getRepositoryModels(user);
|
JM |
98 |
Map<String, RepositoryModel> repositories = new HashMap<String, RepositoryModel>();
|
|
99 |
for (RepositoryModel model : list) {
|
|
100 |
String url = MessageFormat.format(cloneUrl, model.name);
|
|
101 |
repositories.put(url, model);
|
|
102 |
}
|
|
103 |
result = repositories;
|
c75304
|
104 |
} else if (RpcRequest.LIST_BRANCHES.equals(reqType)) {
|
17820f
|
105 |
// list all local branches in all repositories accessible to user
|
JM |
106 |
Map<String, List<String>> localBranches = new HashMap<String, List<String>>();
|
c75304
|
107 |
List<RepositoryModel> models = GitBlit.self().getRepositoryModels(user);
|
JM |
108 |
for (RepositoryModel model : models) {
|
|
109 |
if (!model.hasCommits) {
|
|
110 |
// skip empty repository
|
|
111 |
continue;
|
|
112 |
}
|
e92c6d
|
113 |
if (model.isCollectingGarbage) {
|
JM |
114 |
// skip garbage collecting repository
|
|
115 |
logger.warn(MessageFormat.format("Temporarily excluding {0} from RPC, busy collecting garbage", model.name));
|
|
116 |
continue;
|
|
117 |
}
|
17820f
|
118 |
// get local branches
|
c75304
|
119 |
Repository repository = GitBlit.self().getRepository(model.name);
|
JM |
120 |
List<RefModel> refs = JGitUtils.getLocalBranches(repository, false, -1);
|
17820f
|
121 |
if (model.showRemoteBranches) {
|
JM |
122 |
// add remote branches if repository displays them
|
|
123 |
refs.addAll(JGitUtils.getRemoteBranches(repository, false, -1));
|
|
124 |
}
|
c75304
|
125 |
if (refs.size() > 0) {
|
JM |
126 |
List<String> branches = new ArrayList<String>();
|
|
127 |
for (RefModel ref : refs) {
|
|
128 |
branches.add(ref.getName());
|
|
129 |
}
|
17820f
|
130 |
localBranches.put(model.name, branches);
|
c75304
|
131 |
}
|
JM |
132 |
repository.close();
|
|
133 |
}
|
17820f
|
134 |
result = localBranches;
|
93f0b1
|
135 |
} else if (RpcRequest.LIST_USERS.equals(reqType)) {
|
JM |
136 |
// list users
|
|
137 |
List<String> names = GitBlit.self().getAllUsernames();
|
|
138 |
List<UserModel> users = new ArrayList<UserModel>();
|
|
139 |
for (String name : names) {
|
|
140 |
users.add(GitBlit.self().getUserModel(name));
|
|
141 |
}
|
|
142 |
result = users;
|
f08aab
|
143 |
} else if (RpcRequest.LIST_TEAMS.equals(reqType)) {
|
JM |
144 |
// list teams
|
|
145 |
List<String> names = GitBlit.self().getAllTeamnames();
|
|
146 |
List<TeamModel> teams = new ArrayList<TeamModel>();
|
|
147 |
for (String name : names) {
|
|
148 |
teams.add(GitBlit.self().getTeamModel(name));
|
|
149 |
}
|
|
150 |
result = teams;
|
31abc2
|
151 |
} else if (RpcRequest.CREATE_REPOSITORY.equals(reqType)) {
|
JM |
152 |
// create repository
|
|
153 |
RepositoryModel model = deserialize(request, response, RepositoryModel.class);
|
b2fde8
|
154 |
try {
|
JM |
155 |
GitBlit.self().updateRepositoryModel(model.name, model, true);
|
|
156 |
} catch (GitBlitException e) {
|
|
157 |
response.setStatus(failureCode);
|
|
158 |
}
|
31abc2
|
159 |
} else if (RpcRequest.EDIT_REPOSITORY.equals(reqType)) {
|
JM |
160 |
// edit repository
|
|
161 |
RepositoryModel model = deserialize(request, response, RepositoryModel.class);
|
bcc616
|
162 |
// name specifies original repository name in event of rename
|
31abc2
|
163 |
String repoName = objectName;
|
JM |
164 |
if (repoName == null) {
|
|
165 |
repoName = model.name;
|
|
166 |
}
|
b2fde8
|
167 |
try {
|
JM |
168 |
GitBlit.self().updateRepositoryModel(repoName, model, false);
|
|
169 |
} catch (GitBlitException e) {
|
|
170 |
response.setStatus(failureCode);
|
|
171 |
}
|
31abc2
|
172 |
} else if (RpcRequest.DELETE_REPOSITORY.equals(reqType)) {
|
JM |
173 |
// delete repository
|
|
174 |
RepositoryModel model = deserialize(request, response, RepositoryModel.class);
|
|
175 |
GitBlit.self().deleteRepositoryModel(model);
|
|
176 |
} else if (RpcRequest.CREATE_USER.equals(reqType)) {
|
|
177 |
// create user
|
|
178 |
UserModel model = deserialize(request, response, UserModel.class);
|
b2fde8
|
179 |
try {
|
JM |
180 |
GitBlit.self().updateUserModel(model.username, model, true);
|
|
181 |
} catch (GitBlitException e) {
|
|
182 |
response.setStatus(failureCode);
|
|
183 |
}
|
31abc2
|
184 |
} else if (RpcRequest.EDIT_USER.equals(reqType)) {
|
JM |
185 |
// edit user
|
|
186 |
UserModel model = deserialize(request, response, UserModel.class);
|
|
187 |
// name parameter specifies original user name in event of rename
|
|
188 |
String username = objectName;
|
|
189 |
if (username == null) {
|
|
190 |
username = model.username;
|
|
191 |
}
|
b2fde8
|
192 |
try {
|
JM |
193 |
GitBlit.self().updateUserModel(username, model, false);
|
|
194 |
} catch (GitBlitException e) {
|
|
195 |
response.setStatus(failureCode);
|
|
196 |
}
|
31abc2
|
197 |
} else if (RpcRequest.DELETE_USER.equals(reqType)) {
|
JM |
198 |
// delete user
|
|
199 |
UserModel model = deserialize(request, response, UserModel.class);
|
b2fde8
|
200 |
if (!GitBlit.self().deleteUser(model.username)) {
|
JM |
201 |
response.setStatus(failureCode);
|
|
202 |
}
|
f08aab
|
203 |
} else if (RpcRequest.CREATE_TEAM.equals(reqType)) {
|
JM |
204 |
// create team
|
|
205 |
TeamModel model = deserialize(request, response, TeamModel.class);
|
|
206 |
try {
|
|
207 |
GitBlit.self().updateTeamModel(model.name, model, true);
|
|
208 |
} catch (GitBlitException e) {
|
|
209 |
response.setStatus(failureCode);
|
|
210 |
}
|
|
211 |
} else if (RpcRequest.EDIT_TEAM.equals(reqType)) {
|
|
212 |
// edit team
|
|
213 |
TeamModel model = deserialize(request, response, TeamModel.class);
|
|
214 |
// name parameter specifies original team name in event of rename
|
|
215 |
String teamname = objectName;
|
|
216 |
if (teamname == null) {
|
|
217 |
teamname = model.name;
|
|
218 |
}
|
|
219 |
try {
|
|
220 |
GitBlit.self().updateTeamModel(teamname, model, false);
|
|
221 |
} catch (GitBlitException e) {
|
|
222 |
response.setStatus(failureCode);
|
|
223 |
}
|
|
224 |
} else if (RpcRequest.DELETE_TEAM.equals(reqType)) {
|
|
225 |
// delete team
|
|
226 |
TeamModel model = deserialize(request, response, TeamModel.class);
|
|
227 |
if (!GitBlit.self().deleteTeam(model.name)) {
|
|
228 |
response.setStatus(failureCode);
|
|
229 |
}
|
31abc2
|
230 |
} else if (RpcRequest.LIST_REPOSITORY_MEMBERS.equals(reqType)) {
|
JM |
231 |
// get repository members
|
|
232 |
RepositoryModel model = GitBlit.self().getRepositoryModel(objectName);
|
|
233 |
result = GitBlit.self().getRepositoryUsers(model);
|
|
234 |
} else if (RpcRequest.SET_REPOSITORY_MEMBERS.equals(reqType)) {
|
822dfe
|
235 |
// rejected since 1.2.0
|
JM |
236 |
response.setStatus(failureCode);
|
|
237 |
} else if (RpcRequest.LIST_REPOSITORY_MEMBER_PERMISSIONS.equals(reqType)) {
|
|
238 |
// get repository member permissions
|
31abc2
|
239 |
RepositoryModel model = GitBlit.self().getRepositoryModel(objectName);
|
822dfe
|
240 |
result = GitBlit.self().getUserAccessPermissions(model);
|
JM |
241 |
} else if (RpcRequest.SET_REPOSITORY_MEMBER_PERMISSIONS.equals(reqType)) {
|
|
242 |
// set the repository permissions for the specified users
|
|
243 |
RepositoryModel model = GitBlit.self().getRepositoryModel(objectName);
|
|
244 |
Collection<RegistrantAccessPermission> permissions = deserialize(request, response, RpcUtils.REGISTRANT_PERMISSIONS_TYPE);
|
|
245 |
result = GitBlit.self().setUserAccessPermissions(model, permissions);
|
f08aab
|
246 |
} else if (RpcRequest.LIST_REPOSITORY_TEAMS.equals(reqType)) {
|
JM |
247 |
// get repository teams
|
|
248 |
RepositoryModel model = GitBlit.self().getRepositoryModel(objectName);
|
|
249 |
result = GitBlit.self().getRepositoryTeams(model);
|
|
250 |
} else if (RpcRequest.SET_REPOSITORY_TEAMS.equals(reqType)) {
|
822dfe
|
251 |
// rejected since 1.2.0
|
JM |
252 |
response.setStatus(failureCode);
|
|
253 |
} else if (RpcRequest.LIST_REPOSITORY_TEAM_PERMISSIONS.equals(reqType)) {
|
|
254 |
// get repository team permissions
|
f08aab
|
255 |
RepositoryModel model = GitBlit.self().getRepositoryModel(objectName);
|
822dfe
|
256 |
result = GitBlit.self().getTeamAccessPermissions(model);
|
JM |
257 |
} else if (RpcRequest.SET_REPOSITORY_TEAM_PERMISSIONS.equals(reqType)) {
|
|
258 |
// set the repository permissions for the specified teams
|
|
259 |
RepositoryModel model = GitBlit.self().getRepositoryModel(objectName);
|
|
260 |
Collection<RegistrantAccessPermission> permissions = deserialize(request, response, RpcUtils.REGISTRANT_PERMISSIONS_TYPE);
|
|
261 |
result = GitBlit.self().setTeamAccessPermissions(model, permissions);
|
31abc2
|
262 |
} else if (RpcRequest.LIST_FEDERATION_REGISTRATIONS.equals(reqType)) {
|
JM |
263 |
// return the list of federation registrations
|
284a7b
|
264 |
if (allowAdmin) {
|
JM |
265 |
result = GitBlit.self().getFederationRegistrations();
|
|
266 |
} else {
|
|
267 |
response.sendError(notAllowedCode);
|
|
268 |
}
|
31abc2
|
269 |
} else if (RpcRequest.LIST_FEDERATION_RESULTS.equals(reqType)) {
|
JM |
270 |
// return the list of federation result registrations
|
284a7b
|
271 |
if (allowAdmin && GitBlit.canFederate()) {
|
31abc2
|
272 |
result = GitBlit.self().getFederationResultRegistrations();
|
JM |
273 |
} else {
|
b2fde8
|
274 |
response.sendError(notAllowedCode);
|
31abc2
|
275 |
}
|
JM |
276 |
} else if (RpcRequest.LIST_FEDERATION_PROPOSALS.equals(reqType)) {
|
|
277 |
// return the list of federation proposals
|
284a7b
|
278 |
if (allowAdmin && GitBlit.canFederate()) {
|
31abc2
|
279 |
result = GitBlit.self().getPendingFederationProposals();
|
JM |
280 |
} else {
|
b2fde8
|
281 |
response.sendError(notAllowedCode);
|
31abc2
|
282 |
}
|
JM |
283 |
} else if (RpcRequest.LIST_FEDERATION_SETS.equals(reqType)) {
|
|
284 |
// return the list of federation sets
|
284a7b
|
285 |
if (allowAdmin && GitBlit.canFederate()) {
|
31abc2
|
286 |
String gitblitUrl = HttpUtils.getGitblitURL(request);
|
JM |
287 |
result = GitBlit.self().getFederationSets(gitblitUrl);
|
|
288 |
} else {
|
b2fde8
|
289 |
response.sendError(notAllowedCode);
|
31abc2
|
290 |
}
|
da0269
|
291 |
} else if (RpcRequest.LIST_SETTINGS.equals(reqType)) {
|
JM |
292 |
// return the server's settings
|
97d3af
|
293 |
ServerSettings settings = GitBlit.self().getSettingsModel();
|
284a7b
|
294 |
if (allowAdmin) {
|
JM |
295 |
// return all settings
|
|
296 |
result = settings;
|
d03aff
|
297 |
} else {
|
ec5a88
|
298 |
// anonymous users get a few settings to allow browser launching
|
JM |
299 |
List<String> keys = new ArrayList<String>();
|
|
300 |
keys.add(Keys.web.siteName);
|
|
301 |
keys.add(Keys.web.mountParameters);
|
9bdb91
|
302 |
keys.add(Keys.web.syndicationEntries);
|
f08aab
|
303 |
|
ec5a88
|
304 |
if (allowManagement) {
|
JM |
305 |
// keys necessary for repository and/or user management
|
|
306 |
keys.add(Keys.realm.minPasswordLength);
|
|
307 |
keys.add(Keys.realm.passwordStorage);
|
|
308 |
keys.add(Keys.federation.sets);
|
|
309 |
}
|
|
310 |
// build the settings
|
97d3af
|
311 |
ServerSettings managementSettings = new ServerSettings();
|
284a7b
|
312 |
for (String key : keys) {
|
JM |
313 |
managementSettings.add(settings.get(key));
|
|
314 |
}
|
4bd203
|
315 |
if (allowManagement) {
|
97d3af
|
316 |
managementSettings.pushScripts = settings.pushScripts;
|
4bd203
|
317 |
}
|
284a7b
|
318 |
result = managementSettings;
|
d03aff
|
319 |
}
|
JM |
320 |
} else if (RpcRequest.EDIT_SETTINGS.equals(reqType)) {
|
|
321 |
// update settings on the server
|
284a7b
|
322 |
if (allowAdmin) {
|
97a20e
|
323 |
Map<String, String> settings = deserialize(request, response,
|
d03aff
|
324 |
RpcUtils.SETTINGS_TYPE);
|
JM |
325 |
GitBlit.self().updateSettings(settings);
|
|
326 |
} else {
|
|
327 |
response.sendError(notAllowedCode);
|
|
328 |
}
|
84c1d5
|
329 |
} else if (RpcRequest.LIST_STATUS.equals(reqType)) {
|
b75734
|
330 |
// return the server's status information
|
284a7b
|
331 |
if (allowAdmin) {
|
486ee1
|
332 |
result = GitBlit.self().getStatus();
|
JM |
333 |
} else {
|
|
334 |
response.sendError(notAllowedCode);
|
|
335 |
}
|
fee060
|
336 |
} else if (RpcRequest.CLEAR_REPOSITORY_CACHE.equals(reqType)) {
|
JM |
337 |
// clear the repository list cache
|
4ebaf6
|
338 |
if (allowManagement) {
|
fee060
|
339 |
GitBlit.self().resetRepositoryListCache();
|
JM |
340 |
} else {
|
|
341 |
response.sendError(notAllowedCode);
|
|
342 |
}
|
93f0b1
|
343 |
}
|
JM |
344 |
|
|
345 |
// send the result of the request
|
|
346 |
serialize(response, result);
|
|
347 |
}
|
|
348 |
}
|