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 |
|
|
30 |
import com.gitblit.Constants.RpcRequest;
|
|
31 |
import com.gitblit.models.RepositoryModel;
|
|
32 |
import com.gitblit.models.UserModel;
|
|
33 |
import com.gitblit.utils.HttpUtils;
|
31abc2
|
34 |
import com.gitblit.utils.RpcUtils;
|
93f0b1
|
35 |
|
JM |
36 |
/**
|
|
37 |
* Handles remote procedure calls.
|
|
38 |
*
|
|
39 |
* @author James Moger
|
|
40 |
*
|
|
41 |
*/
|
|
42 |
public class RpcServlet extends JsonServlet {
|
|
43 |
|
|
44 |
private static final long serialVersionUID = 1L;
|
|
45 |
|
|
46 |
public RpcServlet() {
|
|
47 |
super();
|
|
48 |
}
|
|
49 |
|
|
50 |
/**
|
|
51 |
* Processes an rpc request.
|
|
52 |
*
|
|
53 |
* @param request
|
|
54 |
* @param response
|
|
55 |
* @throws javax.servlet.ServletException
|
|
56 |
* @throws java.io.IOException
|
|
57 |
*/
|
|
58 |
@Override
|
ca9d0f
|
59 |
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
|
JM |
60 |
throws ServletException, IOException {
|
93f0b1
|
61 |
RpcRequest reqType = RpcRequest.fromName(request.getParameter("req"));
|
31abc2
|
62 |
String objectName = request.getParameter("name");
|
93f0b1
|
63 |
logger.info(MessageFormat.format("Rpc {0} request from {1}", reqType,
|
JM |
64 |
request.getRemoteAddr()));
|
|
65 |
|
ca9d0f
|
66 |
UserModel user = (UserModel) request.getUserPrincipal();
|
93f0b1
|
67 |
|
JM |
68 |
Object result = null;
|
|
69 |
if (RpcRequest.LIST_REPOSITORIES.equals(reqType)) {
|
|
70 |
// Determine the Gitblit clone url
|
|
71 |
String gitblitUrl = HttpUtils.getGitblitURL(request);
|
|
72 |
StringBuilder sb = new StringBuilder();
|
|
73 |
sb.append(gitblitUrl);
|
|
74 |
sb.append(Constants.GIT_PATH);
|
|
75 |
sb.append("{0}");
|
|
76 |
String cloneUrl = sb.toString();
|
|
77 |
|
ca9d0f
|
78 |
// list repositories
|
93f0b1
|
79 |
List<RepositoryModel> list = GitBlit.self().getRepositoryModels(user);
|
JM |
80 |
Map<String, RepositoryModel> repositories = new HashMap<String, RepositoryModel>();
|
|
81 |
for (RepositoryModel model : list) {
|
|
82 |
String url = MessageFormat.format(cloneUrl, model.name);
|
|
83 |
repositories.put(url, model);
|
|
84 |
}
|
|
85 |
result = repositories;
|
|
86 |
} else if (RpcRequest.LIST_USERS.equals(reqType)) {
|
|
87 |
// list users
|
|
88 |
List<String> names = GitBlit.self().getAllUsernames();
|
|
89 |
List<UserModel> users = new ArrayList<UserModel>();
|
|
90 |
for (String name : names) {
|
|
91 |
users.add(GitBlit.self().getUserModel(name));
|
|
92 |
}
|
|
93 |
result = users;
|
31abc2
|
94 |
} else if (RpcRequest.CREATE_REPOSITORY.equals(reqType)) {
|
JM |
95 |
// create repository
|
|
96 |
RepositoryModel model = deserialize(request, response, RepositoryModel.class);
|
b2fde8
|
97 |
try {
|
JM |
98 |
GitBlit.self().updateRepositoryModel(model.name, model, true);
|
|
99 |
} catch (GitBlitException e) {
|
|
100 |
response.setStatus(failureCode);
|
|
101 |
}
|
31abc2
|
102 |
} else if (RpcRequest.EDIT_REPOSITORY.equals(reqType)) {
|
JM |
103 |
// edit repository
|
|
104 |
RepositoryModel model = deserialize(request, response, RepositoryModel.class);
|
bcc616
|
105 |
// name specifies original repository name in event of rename
|
31abc2
|
106 |
String repoName = objectName;
|
JM |
107 |
if (repoName == null) {
|
|
108 |
repoName = model.name;
|
|
109 |
}
|
b2fde8
|
110 |
try {
|
JM |
111 |
GitBlit.self().updateRepositoryModel(repoName, model, false);
|
|
112 |
} catch (GitBlitException e) {
|
|
113 |
response.setStatus(failureCode);
|
|
114 |
}
|
31abc2
|
115 |
} else if (RpcRequest.DELETE_REPOSITORY.equals(reqType)) {
|
JM |
116 |
// delete repository
|
|
117 |
RepositoryModel model = deserialize(request, response, RepositoryModel.class);
|
|
118 |
GitBlit.self().deleteRepositoryModel(model);
|
|
119 |
} else if (RpcRequest.CREATE_USER.equals(reqType)) {
|
|
120 |
// create user
|
|
121 |
UserModel model = deserialize(request, response, UserModel.class);
|
b2fde8
|
122 |
try {
|
JM |
123 |
GitBlit.self().updateUserModel(model.username, model, true);
|
|
124 |
} catch (GitBlitException e) {
|
|
125 |
response.setStatus(failureCode);
|
|
126 |
}
|
31abc2
|
127 |
} else if (RpcRequest.EDIT_USER.equals(reqType)) {
|
JM |
128 |
// edit user
|
|
129 |
UserModel model = deserialize(request, response, UserModel.class);
|
|
130 |
// name parameter specifies original user name in event of rename
|
|
131 |
String username = objectName;
|
|
132 |
if (username == null) {
|
|
133 |
username = model.username;
|
|
134 |
}
|
b2fde8
|
135 |
try {
|
JM |
136 |
GitBlit.self().updateUserModel(username, model, false);
|
|
137 |
} catch (GitBlitException e) {
|
|
138 |
response.setStatus(failureCode);
|
|
139 |
}
|
31abc2
|
140 |
} else if (RpcRequest.DELETE_USER.equals(reqType)) {
|
JM |
141 |
// delete user
|
|
142 |
UserModel model = deserialize(request, response, UserModel.class);
|
b2fde8
|
143 |
if (!GitBlit.self().deleteUser(model.username)) {
|
JM |
144 |
response.setStatus(failureCode);
|
|
145 |
}
|
31abc2
|
146 |
} else if (RpcRequest.LIST_REPOSITORY_MEMBERS.equals(reqType)) {
|
JM |
147 |
// get repository members
|
|
148 |
RepositoryModel model = GitBlit.self().getRepositoryModel(objectName);
|
|
149 |
result = GitBlit.self().getRepositoryUsers(model);
|
|
150 |
} else if (RpcRequest.SET_REPOSITORY_MEMBERS.equals(reqType)) {
|
|
151 |
// update repository access list
|
|
152 |
RepositoryModel model = GitBlit.self().getRepositoryModel(objectName);
|
|
153 |
Collection<String> names = deserialize(request, response, RpcUtils.NAMES_TYPE);
|
|
154 |
List<String> users = new ArrayList<String>(names);
|
|
155 |
if (!GitBlit.self().setRepositoryUsers(model, users)) {
|
b2fde8
|
156 |
response.setStatus(failureCode);
|
31abc2
|
157 |
}
|
JM |
158 |
} else if (RpcRequest.LIST_FEDERATION_REGISTRATIONS.equals(reqType)) {
|
|
159 |
// return the list of federation registrations
|
|
160 |
result = GitBlit.self().getFederationRegistrations();
|
|
161 |
} else if (RpcRequest.LIST_FEDERATION_RESULTS.equals(reqType)) {
|
|
162 |
// return the list of federation result registrations
|
|
163 |
if (GitBlit.canFederate()) {
|
|
164 |
result = GitBlit.self().getFederationResultRegistrations();
|
|
165 |
} else {
|
b2fde8
|
166 |
response.sendError(notAllowedCode);
|
31abc2
|
167 |
}
|
JM |
168 |
} else if (RpcRequest.LIST_FEDERATION_PROPOSALS.equals(reqType)) {
|
|
169 |
// return the list of federation proposals
|
|
170 |
if (GitBlit.canFederate()) {
|
|
171 |
result = GitBlit.self().getPendingFederationProposals();
|
|
172 |
} else {
|
b2fde8
|
173 |
response.sendError(notAllowedCode);
|
31abc2
|
174 |
}
|
JM |
175 |
} else if (RpcRequest.LIST_FEDERATION_SETS.equals(reqType)) {
|
|
176 |
// return the list of federation sets
|
|
177 |
if (GitBlit.canFederate()) {
|
|
178 |
String gitblitUrl = HttpUtils.getGitblitURL(request);
|
|
179 |
result = GitBlit.self().getFederationSets(gitblitUrl);
|
|
180 |
} else {
|
b2fde8
|
181 |
response.sendError(notAllowedCode);
|
31abc2
|
182 |
}
|
da0269
|
183 |
} else if (RpcRequest.LIST_SETTINGS.equals(reqType)) {
|
JM |
184 |
// return the server's settings
|
b75734
|
185 |
result = GitBlit.self().getSettingModels();
|
JM |
186 |
} else if (RpcRequest.LIST_SERVER_STATUS.equals(reqType)) {
|
|
187 |
// return the server's status information
|
|
188 |
result = GitBlit.self().getStatus();
|
93f0b1
|
189 |
}
|
JM |
190 |
|
|
191 |
// send the result of the request
|
|
192 |
serialize(response, result);
|
|
193 |
}
|
|
194 |
}
|