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 |
}
|
17820f
|
113 |
// get local branches
|
c75304
|
114 |
Repository repository = GitBlit.self().getRepository(model.name);
|
JM |
115 |
List<RefModel> refs = JGitUtils.getLocalBranches(repository, false, -1);
|
17820f
|
116 |
if (model.showRemoteBranches) {
|
JM |
117 |
// add remote branches if repository displays them
|
|
118 |
refs.addAll(JGitUtils.getRemoteBranches(repository, false, -1));
|
|
119 |
}
|
c75304
|
120 |
if (refs.size() > 0) {
|
JM |
121 |
List<String> branches = new ArrayList<String>();
|
|
122 |
for (RefModel ref : refs) {
|
|
123 |
branches.add(ref.getName());
|
|
124 |
}
|
17820f
|
125 |
localBranches.put(model.name, branches);
|
c75304
|
126 |
}
|
JM |
127 |
repository.close();
|
|
128 |
}
|
17820f
|
129 |
result = localBranches;
|
93f0b1
|
130 |
} else if (RpcRequest.LIST_USERS.equals(reqType)) {
|
JM |
131 |
// list users
|
|
132 |
List<String> names = GitBlit.self().getAllUsernames();
|
|
133 |
List<UserModel> users = new ArrayList<UserModel>();
|
|
134 |
for (String name : names) {
|
|
135 |
users.add(GitBlit.self().getUserModel(name));
|
|
136 |
}
|
|
137 |
result = users;
|
f08aab
|
138 |
} else if (RpcRequest.LIST_TEAMS.equals(reqType)) {
|
JM |
139 |
// list teams
|
|
140 |
List<String> names = GitBlit.self().getAllTeamnames();
|
|
141 |
List<TeamModel> teams = new ArrayList<TeamModel>();
|
|
142 |
for (String name : names) {
|
|
143 |
teams.add(GitBlit.self().getTeamModel(name));
|
|
144 |
}
|
|
145 |
result = teams;
|
31abc2
|
146 |
} else if (RpcRequest.CREATE_REPOSITORY.equals(reqType)) {
|
JM |
147 |
// create repository
|
|
148 |
RepositoryModel model = deserialize(request, response, RepositoryModel.class);
|
b2fde8
|
149 |
try {
|
JM |
150 |
GitBlit.self().updateRepositoryModel(model.name, model, true);
|
|
151 |
} catch (GitBlitException e) {
|
|
152 |
response.setStatus(failureCode);
|
|
153 |
}
|
31abc2
|
154 |
} else if (RpcRequest.EDIT_REPOSITORY.equals(reqType)) {
|
JM |
155 |
// edit repository
|
|
156 |
RepositoryModel model = deserialize(request, response, RepositoryModel.class);
|
bcc616
|
157 |
// name specifies original repository name in event of rename
|
31abc2
|
158 |
String repoName = objectName;
|
JM |
159 |
if (repoName == null) {
|
|
160 |
repoName = model.name;
|
|
161 |
}
|
b2fde8
|
162 |
try {
|
JM |
163 |
GitBlit.self().updateRepositoryModel(repoName, model, false);
|
|
164 |
} catch (GitBlitException e) {
|
|
165 |
response.setStatus(failureCode);
|
|
166 |
}
|
31abc2
|
167 |
} else if (RpcRequest.DELETE_REPOSITORY.equals(reqType)) {
|
JM |
168 |
// delete repository
|
|
169 |
RepositoryModel model = deserialize(request, response, RepositoryModel.class);
|
|
170 |
GitBlit.self().deleteRepositoryModel(model);
|
|
171 |
} else if (RpcRequest.CREATE_USER.equals(reqType)) {
|
|
172 |
// create user
|
|
173 |
UserModel model = deserialize(request, response, UserModel.class);
|
b2fde8
|
174 |
try {
|
JM |
175 |
GitBlit.self().updateUserModel(model.username, model, true);
|
|
176 |
} catch (GitBlitException e) {
|
|
177 |
response.setStatus(failureCode);
|
|
178 |
}
|
31abc2
|
179 |
} else if (RpcRequest.EDIT_USER.equals(reqType)) {
|
JM |
180 |
// edit user
|
|
181 |
UserModel model = deserialize(request, response, UserModel.class);
|
|
182 |
// name parameter specifies original user name in event of rename
|
|
183 |
String username = objectName;
|
|
184 |
if (username == null) {
|
|
185 |
username = model.username;
|
|
186 |
}
|
b2fde8
|
187 |
try {
|
JM |
188 |
GitBlit.self().updateUserModel(username, model, false);
|
|
189 |
} catch (GitBlitException e) {
|
|
190 |
response.setStatus(failureCode);
|
|
191 |
}
|
31abc2
|
192 |
} else if (RpcRequest.DELETE_USER.equals(reqType)) {
|
JM |
193 |
// delete user
|
|
194 |
UserModel model = deserialize(request, response, UserModel.class);
|
b2fde8
|
195 |
if (!GitBlit.self().deleteUser(model.username)) {
|
JM |
196 |
response.setStatus(failureCode);
|
|
197 |
}
|
f08aab
|
198 |
} else if (RpcRequest.CREATE_TEAM.equals(reqType)) {
|
JM |
199 |
// create team
|
|
200 |
TeamModel model = deserialize(request, response, TeamModel.class);
|
|
201 |
try {
|
|
202 |
GitBlit.self().updateTeamModel(model.name, model, true);
|
|
203 |
} catch (GitBlitException e) {
|
|
204 |
response.setStatus(failureCode);
|
|
205 |
}
|
|
206 |
} else if (RpcRequest.EDIT_TEAM.equals(reqType)) {
|
|
207 |
// edit team
|
|
208 |
TeamModel model = deserialize(request, response, TeamModel.class);
|
|
209 |
// name parameter specifies original team name in event of rename
|
|
210 |
String teamname = objectName;
|
|
211 |
if (teamname == null) {
|
|
212 |
teamname = model.name;
|
|
213 |
}
|
|
214 |
try {
|
|
215 |
GitBlit.self().updateTeamModel(teamname, model, false);
|
|
216 |
} catch (GitBlitException e) {
|
|
217 |
response.setStatus(failureCode);
|
|
218 |
}
|
|
219 |
} else if (RpcRequest.DELETE_TEAM.equals(reqType)) {
|
|
220 |
// delete team
|
|
221 |
TeamModel model = deserialize(request, response, TeamModel.class);
|
|
222 |
if (!GitBlit.self().deleteTeam(model.name)) {
|
|
223 |
response.setStatus(failureCode);
|
|
224 |
}
|
31abc2
|
225 |
} else if (RpcRequest.LIST_REPOSITORY_MEMBERS.equals(reqType)) {
|
JM |
226 |
// get repository members
|
|
227 |
RepositoryModel model = GitBlit.self().getRepositoryModel(objectName);
|
|
228 |
result = GitBlit.self().getRepositoryUsers(model);
|
|
229 |
} else if (RpcRequest.SET_REPOSITORY_MEMBERS.equals(reqType)) {
|
822dfe
|
230 |
// rejected since 1.2.0
|
JM |
231 |
response.setStatus(failureCode);
|
|
232 |
} else if (RpcRequest.LIST_REPOSITORY_MEMBER_PERMISSIONS.equals(reqType)) {
|
|
233 |
// get repository member permissions
|
31abc2
|
234 |
RepositoryModel model = GitBlit.self().getRepositoryModel(objectName);
|
822dfe
|
235 |
result = GitBlit.self().getUserAccessPermissions(model);
|
JM |
236 |
} else if (RpcRequest.SET_REPOSITORY_MEMBER_PERMISSIONS.equals(reqType)) {
|
|
237 |
// set the repository permissions for the specified users
|
|
238 |
RepositoryModel model = GitBlit.self().getRepositoryModel(objectName);
|
|
239 |
Collection<RegistrantAccessPermission> permissions = deserialize(request, response, RpcUtils.REGISTRANT_PERMISSIONS_TYPE);
|
|
240 |
result = GitBlit.self().setUserAccessPermissions(model, permissions);
|
f08aab
|
241 |
} else if (RpcRequest.LIST_REPOSITORY_TEAMS.equals(reqType)) {
|
JM |
242 |
// get repository teams
|
|
243 |
RepositoryModel model = GitBlit.self().getRepositoryModel(objectName);
|
|
244 |
result = GitBlit.self().getRepositoryTeams(model);
|
|
245 |
} else if (RpcRequest.SET_REPOSITORY_TEAMS.equals(reqType)) {
|
822dfe
|
246 |
// rejected since 1.2.0
|
JM |
247 |
response.setStatus(failureCode);
|
|
248 |
} else if (RpcRequest.LIST_REPOSITORY_TEAM_PERMISSIONS.equals(reqType)) {
|
|
249 |
// get repository team permissions
|
f08aab
|
250 |
RepositoryModel model = GitBlit.self().getRepositoryModel(objectName);
|
822dfe
|
251 |
result = GitBlit.self().getTeamAccessPermissions(model);
|
JM |
252 |
} else if (RpcRequest.SET_REPOSITORY_TEAM_PERMISSIONS.equals(reqType)) {
|
|
253 |
// set the repository permissions for the specified teams
|
|
254 |
RepositoryModel model = GitBlit.self().getRepositoryModel(objectName);
|
|
255 |
Collection<RegistrantAccessPermission> permissions = deserialize(request, response, RpcUtils.REGISTRANT_PERMISSIONS_TYPE);
|
|
256 |
result = GitBlit.self().setTeamAccessPermissions(model, permissions);
|
31abc2
|
257 |
} else if (RpcRequest.LIST_FEDERATION_REGISTRATIONS.equals(reqType)) {
|
JM |
258 |
// return the list of federation registrations
|
284a7b
|
259 |
if (allowAdmin) {
|
JM |
260 |
result = GitBlit.self().getFederationRegistrations();
|
|
261 |
} else {
|
|
262 |
response.sendError(notAllowedCode);
|
|
263 |
}
|
31abc2
|
264 |
} else if (RpcRequest.LIST_FEDERATION_RESULTS.equals(reqType)) {
|
JM |
265 |
// return the list of federation result registrations
|
284a7b
|
266 |
if (allowAdmin && GitBlit.canFederate()) {
|
31abc2
|
267 |
result = GitBlit.self().getFederationResultRegistrations();
|
JM |
268 |
} else {
|
b2fde8
|
269 |
response.sendError(notAllowedCode);
|
31abc2
|
270 |
}
|
JM |
271 |
} else if (RpcRequest.LIST_FEDERATION_PROPOSALS.equals(reqType)) {
|
|
272 |
// return the list of federation proposals
|
284a7b
|
273 |
if (allowAdmin && GitBlit.canFederate()) {
|
31abc2
|
274 |
result = GitBlit.self().getPendingFederationProposals();
|
JM |
275 |
} else {
|
b2fde8
|
276 |
response.sendError(notAllowedCode);
|
31abc2
|
277 |
}
|
JM |
278 |
} else if (RpcRequest.LIST_FEDERATION_SETS.equals(reqType)) {
|
|
279 |
// return the list of federation sets
|
284a7b
|
280 |
if (allowAdmin && GitBlit.canFederate()) {
|
31abc2
|
281 |
String gitblitUrl = HttpUtils.getGitblitURL(request);
|
JM |
282 |
result = GitBlit.self().getFederationSets(gitblitUrl);
|
|
283 |
} else {
|
b2fde8
|
284 |
response.sendError(notAllowedCode);
|
31abc2
|
285 |
}
|
da0269
|
286 |
} else if (RpcRequest.LIST_SETTINGS.equals(reqType)) {
|
JM |
287 |
// return the server's settings
|
97d3af
|
288 |
ServerSettings settings = GitBlit.self().getSettingsModel();
|
284a7b
|
289 |
if (allowAdmin) {
|
JM |
290 |
// return all settings
|
|
291 |
result = settings;
|
d03aff
|
292 |
} else {
|
ec5a88
|
293 |
// anonymous users get a few settings to allow browser launching
|
JM |
294 |
List<String> keys = new ArrayList<String>();
|
|
295 |
keys.add(Keys.web.siteName);
|
|
296 |
keys.add(Keys.web.mountParameters);
|
9bdb91
|
297 |
keys.add(Keys.web.syndicationEntries);
|
f08aab
|
298 |
|
ec5a88
|
299 |
if (allowManagement) {
|
JM |
300 |
// keys necessary for repository and/or user management
|
|
301 |
keys.add(Keys.realm.minPasswordLength);
|
|
302 |
keys.add(Keys.realm.passwordStorage);
|
|
303 |
keys.add(Keys.federation.sets);
|
|
304 |
}
|
|
305 |
// build the settings
|
97d3af
|
306 |
ServerSettings managementSettings = new ServerSettings();
|
284a7b
|
307 |
for (String key : keys) {
|
JM |
308 |
managementSettings.add(settings.get(key));
|
|
309 |
}
|
4bd203
|
310 |
if (allowManagement) {
|
97d3af
|
311 |
managementSettings.pushScripts = settings.pushScripts;
|
4bd203
|
312 |
}
|
284a7b
|
313 |
result = managementSettings;
|
d03aff
|
314 |
}
|
JM |
315 |
} else if (RpcRequest.EDIT_SETTINGS.equals(reqType)) {
|
|
316 |
// update settings on the server
|
284a7b
|
317 |
if (allowAdmin) {
|
97a20e
|
318 |
Map<String, String> settings = deserialize(request, response,
|
d03aff
|
319 |
RpcUtils.SETTINGS_TYPE);
|
JM |
320 |
GitBlit.self().updateSettings(settings);
|
|
321 |
} else {
|
|
322 |
response.sendError(notAllowedCode);
|
|
323 |
}
|
84c1d5
|
324 |
} else if (RpcRequest.LIST_STATUS.equals(reqType)) {
|
b75734
|
325 |
// return the server's status information
|
284a7b
|
326 |
if (allowAdmin) {
|
486ee1
|
327 |
result = GitBlit.self().getStatus();
|
JM |
328 |
} else {
|
|
329 |
response.sendError(notAllowedCode);
|
|
330 |
}
|
fee060
|
331 |
} else if (RpcRequest.CLEAR_REPOSITORY_CACHE.equals(reqType)) {
|
JM |
332 |
// clear the repository list cache
|
4ebaf6
|
333 |
if (allowManagement) {
|
fee060
|
334 |
GitBlit.self().resetRepositoryListCache();
|
JM |
335 |
} else {
|
|
336 |
response.sendError(notAllowedCode);
|
|
337 |
}
|
93f0b1
|
338 |
}
|
JM |
339 |
|
|
340 |
// send the result of the request
|
|
341 |
serialize(response, result);
|
|
342 |
}
|
|
343 |
}
|