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;
|
c75304
|
33 |
import com.gitblit.models.RefModel;
|
93f0b1
|
34 |
import com.gitblit.models.RepositoryModel;
|
284a7b
|
35 |
import com.gitblit.models.ServerSettings;
|
93f0b1
|
36 |
import com.gitblit.models.UserModel;
|
JM |
37 |
import com.gitblit.utils.HttpUtils;
|
c75304
|
38 |
import com.gitblit.utils.JGitUtils;
|
31abc2
|
39 |
import com.gitblit.utils.RpcUtils;
|
93f0b1
|
40 |
|
JM |
41 |
/**
|
|
42 |
* Handles remote procedure calls.
|
|
43 |
*
|
|
44 |
* @author James Moger
|
|
45 |
*
|
|
46 |
*/
|
|
47 |
public class RpcServlet extends JsonServlet {
|
|
48 |
|
|
49 |
private static final long serialVersionUID = 1L;
|
|
50 |
|
|
51 |
public RpcServlet() {
|
|
52 |
super();
|
|
53 |
}
|
|
54 |
|
|
55 |
/**
|
|
56 |
* Processes an rpc request.
|
|
57 |
*
|
|
58 |
* @param request
|
|
59 |
* @param response
|
|
60 |
* @throws javax.servlet.ServletException
|
|
61 |
* @throws java.io.IOException
|
|
62 |
*/
|
|
63 |
@Override
|
ca9d0f
|
64 |
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
|
JM |
65 |
throws ServletException, IOException {
|
93f0b1
|
66 |
RpcRequest reqType = RpcRequest.fromName(request.getParameter("req"));
|
31abc2
|
67 |
String objectName = request.getParameter("name");
|
93f0b1
|
68 |
logger.info(MessageFormat.format("Rpc {0} request from {1}", reqType,
|
JM |
69 |
request.getRemoteAddr()));
|
284a7b
|
70 |
|
JM |
71 |
boolean allowAdmin = GitBlit.getBoolean(Keys.web.enableRpcAdministration, false);
|
93f0b1
|
72 |
|
ca9d0f
|
73 |
UserModel user = (UserModel) request.getUserPrincipal();
|
93f0b1
|
74 |
|
JM |
75 |
Object result = null;
|
|
76 |
if (RpcRequest.LIST_REPOSITORIES.equals(reqType)) {
|
|
77 |
// Determine the Gitblit clone url
|
|
78 |
String gitblitUrl = HttpUtils.getGitblitURL(request);
|
|
79 |
StringBuilder sb = new StringBuilder();
|
|
80 |
sb.append(gitblitUrl);
|
|
81 |
sb.append(Constants.GIT_PATH);
|
|
82 |
sb.append("{0}");
|
|
83 |
String cloneUrl = sb.toString();
|
|
84 |
|
ca9d0f
|
85 |
// list repositories
|
93f0b1
|
86 |
List<RepositoryModel> list = GitBlit.self().getRepositoryModels(user);
|
JM |
87 |
Map<String, RepositoryModel> repositories = new HashMap<String, RepositoryModel>();
|
|
88 |
for (RepositoryModel model : list) {
|
|
89 |
String url = MessageFormat.format(cloneUrl, model.name);
|
|
90 |
repositories.put(url, model);
|
|
91 |
}
|
|
92 |
result = repositories;
|
c75304
|
93 |
} else if (RpcRequest.LIST_BRANCHES.equals(reqType)) {
|
17820f
|
94 |
// list all local branches in all repositories accessible to user
|
JM |
95 |
Map<String, List<String>> localBranches = new HashMap<String, List<String>>();
|
c75304
|
96 |
List<RepositoryModel> models = GitBlit.self().getRepositoryModels(user);
|
JM |
97 |
for (RepositoryModel model : models) {
|
|
98 |
if (!model.hasCommits) {
|
|
99 |
// skip empty repository
|
|
100 |
continue;
|
|
101 |
}
|
17820f
|
102 |
// get local branches
|
c75304
|
103 |
Repository repository = GitBlit.self().getRepository(model.name);
|
JM |
104 |
List<RefModel> refs = JGitUtils.getLocalBranches(repository, false, -1);
|
17820f
|
105 |
if (model.showRemoteBranches) {
|
JM |
106 |
// add remote branches if repository displays them
|
|
107 |
refs.addAll(JGitUtils.getRemoteBranches(repository, false, -1));
|
|
108 |
}
|
c75304
|
109 |
if (refs.size() > 0) {
|
JM |
110 |
List<String> branches = new ArrayList<String>();
|
|
111 |
for (RefModel ref : refs) {
|
|
112 |
branches.add(ref.getName());
|
|
113 |
}
|
17820f
|
114 |
localBranches.put(model.name, branches);
|
c75304
|
115 |
}
|
JM |
116 |
repository.close();
|
|
117 |
}
|
17820f
|
118 |
result = localBranches;
|
93f0b1
|
119 |
} else if (RpcRequest.LIST_USERS.equals(reqType)) {
|
JM |
120 |
// list users
|
|
121 |
List<String> names = GitBlit.self().getAllUsernames();
|
|
122 |
List<UserModel> users = new ArrayList<UserModel>();
|
|
123 |
for (String name : names) {
|
|
124 |
users.add(GitBlit.self().getUserModel(name));
|
|
125 |
}
|
|
126 |
result = users;
|
31abc2
|
127 |
} else if (RpcRequest.CREATE_REPOSITORY.equals(reqType)) {
|
JM |
128 |
// create repository
|
|
129 |
RepositoryModel model = deserialize(request, response, RepositoryModel.class);
|
b2fde8
|
130 |
try {
|
JM |
131 |
GitBlit.self().updateRepositoryModel(model.name, model, true);
|
|
132 |
} catch (GitBlitException e) {
|
|
133 |
response.setStatus(failureCode);
|
|
134 |
}
|
31abc2
|
135 |
} else if (RpcRequest.EDIT_REPOSITORY.equals(reqType)) {
|
JM |
136 |
// edit repository
|
|
137 |
RepositoryModel model = deserialize(request, response, RepositoryModel.class);
|
bcc616
|
138 |
// name specifies original repository name in event of rename
|
31abc2
|
139 |
String repoName = objectName;
|
JM |
140 |
if (repoName == null) {
|
|
141 |
repoName = model.name;
|
|
142 |
}
|
b2fde8
|
143 |
try {
|
JM |
144 |
GitBlit.self().updateRepositoryModel(repoName, model, false);
|
|
145 |
} catch (GitBlitException e) {
|
|
146 |
response.setStatus(failureCode);
|
|
147 |
}
|
31abc2
|
148 |
} else if (RpcRequest.DELETE_REPOSITORY.equals(reqType)) {
|
JM |
149 |
// delete repository
|
|
150 |
RepositoryModel model = deserialize(request, response, RepositoryModel.class);
|
|
151 |
GitBlit.self().deleteRepositoryModel(model);
|
|
152 |
} else if (RpcRequest.CREATE_USER.equals(reqType)) {
|
|
153 |
// create user
|
|
154 |
UserModel model = deserialize(request, response, UserModel.class);
|
b2fde8
|
155 |
try {
|
JM |
156 |
GitBlit.self().updateUserModel(model.username, model, true);
|
|
157 |
} catch (GitBlitException e) {
|
|
158 |
response.setStatus(failureCode);
|
|
159 |
}
|
31abc2
|
160 |
} else if (RpcRequest.EDIT_USER.equals(reqType)) {
|
JM |
161 |
// edit user
|
|
162 |
UserModel model = deserialize(request, response, UserModel.class);
|
|
163 |
// name parameter specifies original user name in event of rename
|
|
164 |
String username = objectName;
|
|
165 |
if (username == null) {
|
|
166 |
username = model.username;
|
|
167 |
}
|
b2fde8
|
168 |
try {
|
JM |
169 |
GitBlit.self().updateUserModel(username, model, false);
|
|
170 |
} catch (GitBlitException e) {
|
|
171 |
response.setStatus(failureCode);
|
|
172 |
}
|
31abc2
|
173 |
} else if (RpcRequest.DELETE_USER.equals(reqType)) {
|
JM |
174 |
// delete user
|
|
175 |
UserModel model = deserialize(request, response, UserModel.class);
|
b2fde8
|
176 |
if (!GitBlit.self().deleteUser(model.username)) {
|
JM |
177 |
response.setStatus(failureCode);
|
|
178 |
}
|
31abc2
|
179 |
} else if (RpcRequest.LIST_REPOSITORY_MEMBERS.equals(reqType)) {
|
JM |
180 |
// get repository members
|
|
181 |
RepositoryModel model = GitBlit.self().getRepositoryModel(objectName);
|
|
182 |
result = GitBlit.self().getRepositoryUsers(model);
|
|
183 |
} else if (RpcRequest.SET_REPOSITORY_MEMBERS.equals(reqType)) {
|
|
184 |
// update repository access list
|
|
185 |
RepositoryModel model = GitBlit.self().getRepositoryModel(objectName);
|
|
186 |
Collection<String> names = deserialize(request, response, RpcUtils.NAMES_TYPE);
|
|
187 |
List<String> users = new ArrayList<String>(names);
|
|
188 |
if (!GitBlit.self().setRepositoryUsers(model, users)) {
|
b2fde8
|
189 |
response.setStatus(failureCode);
|
31abc2
|
190 |
}
|
JM |
191 |
} else if (RpcRequest.LIST_FEDERATION_REGISTRATIONS.equals(reqType)) {
|
|
192 |
// return the list of federation registrations
|
284a7b
|
193 |
if (allowAdmin) {
|
JM |
194 |
result = GitBlit.self().getFederationRegistrations();
|
|
195 |
} else {
|
|
196 |
response.sendError(notAllowedCode);
|
|
197 |
}
|
31abc2
|
198 |
} else if (RpcRequest.LIST_FEDERATION_RESULTS.equals(reqType)) {
|
JM |
199 |
// return the list of federation result registrations
|
284a7b
|
200 |
if (allowAdmin && GitBlit.canFederate()) {
|
31abc2
|
201 |
result = GitBlit.self().getFederationResultRegistrations();
|
JM |
202 |
} else {
|
b2fde8
|
203 |
response.sendError(notAllowedCode);
|
31abc2
|
204 |
}
|
JM |
205 |
} else if (RpcRequest.LIST_FEDERATION_PROPOSALS.equals(reqType)) {
|
|
206 |
// return the list of federation proposals
|
284a7b
|
207 |
if (allowAdmin && GitBlit.canFederate()) {
|
31abc2
|
208 |
result = GitBlit.self().getPendingFederationProposals();
|
JM |
209 |
} else {
|
b2fde8
|
210 |
response.sendError(notAllowedCode);
|
31abc2
|
211 |
}
|
JM |
212 |
} else if (RpcRequest.LIST_FEDERATION_SETS.equals(reqType)) {
|
|
213 |
// return the list of federation sets
|
284a7b
|
214 |
if (allowAdmin && GitBlit.canFederate()) {
|
31abc2
|
215 |
String gitblitUrl = HttpUtils.getGitblitURL(request);
|
JM |
216 |
result = GitBlit.self().getFederationSets(gitblitUrl);
|
|
217 |
} else {
|
b2fde8
|
218 |
response.sendError(notAllowedCode);
|
31abc2
|
219 |
}
|
da0269
|
220 |
} else if (RpcRequest.LIST_SETTINGS.equals(reqType)) {
|
JM |
221 |
// return the server's settings
|
284a7b
|
222 |
ServerSettings settings = GitBlit.self().getSettingsModel();
|
JM |
223 |
if (allowAdmin) {
|
|
224 |
// return all settings
|
|
225 |
result = settings;
|
d03aff
|
226 |
} else {
|
284a7b
|
227 |
// return management settings only
|
JM |
228 |
String[] keys = { Keys.realm.minPasswordLength, Keys.realm.passwordStorage,
|
|
229 |
Keys.federation.sets };
|
|
230 |
ServerSettings managementSettings = new ServerSettings();
|
|
231 |
for (String key : keys) {
|
|
232 |
managementSettings.add(settings.get(key));
|
|
233 |
}
|
|
234 |
result = managementSettings;
|
d03aff
|
235 |
}
|
JM |
236 |
} else if (RpcRequest.EDIT_SETTINGS.equals(reqType)) {
|
|
237 |
// update settings on the server
|
284a7b
|
238 |
if (allowAdmin) {
|
97a20e
|
239 |
Map<String, String> settings = deserialize(request, response,
|
d03aff
|
240 |
RpcUtils.SETTINGS_TYPE);
|
JM |
241 |
GitBlit.self().updateSettings(settings);
|
|
242 |
} else {
|
|
243 |
response.sendError(notAllowedCode);
|
|
244 |
}
|
84c1d5
|
245 |
} else if (RpcRequest.LIST_STATUS.equals(reqType)) {
|
b75734
|
246 |
// return the server's status information
|
284a7b
|
247 |
if (allowAdmin) {
|
486ee1
|
248 |
result = GitBlit.self().getStatus();
|
JM |
249 |
} else {
|
|
250 |
response.sendError(notAllowedCode);
|
|
251 |
}
|
93f0b1
|
252 |
}
|
JM |
253 |
|
|
254 |
// send the result of the request
|
|
255 |
serialize(response, result);
|
|
256 |
}
|
|
257 |
}
|