lemval
2012-01-31 1c30dad2115fc513791d8a5b292ad0f7d7b85749
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.utils;
17
31abc2 18 import java.io.IOException;
93f0b1 19 import java.lang.reflect.Type;
31abc2 20 import java.util.ArrayList;
93f0b1 21 import java.util.Collection;
31abc2 22 import java.util.List;
93f0b1 23 import java.util.Map;
JM 24
25 import com.gitblit.Constants;
26 import com.gitblit.Constants.RpcRequest;
f08aab 27 import com.gitblit.GitBlitException.UnknownRequestException;
31abc2 28 import com.gitblit.models.FederationModel;
JM 29 import com.gitblit.models.FederationProposal;
30 import com.gitblit.models.FederationSet;
17820f 31 import com.gitblit.models.FeedModel;
93f0b1 32 import com.gitblit.models.RepositoryModel;
84c1d5 33 import com.gitblit.models.ServerSettings;
d03aff 34 import com.gitblit.models.ServerStatus;
f08aab 35 import com.gitblit.models.TeamModel;
93f0b1 36 import com.gitblit.models.UserModel;
JM 37 import com.google.gson.reflect.TypeToken;
38
39 /**
40  * Utility methods for rpc calls.
41  * 
42  * @author James Moger
43  * 
44  */
45 public class RpcUtils {
46
31abc2 47     public static final Type NAMES_TYPE = new TypeToken<Collection<String>>() {
d03aff 48     }.getType();
JM 49
97a20e 50     public static final Type SETTINGS_TYPE = new TypeToken<Map<String, String>>() {
93f0b1 51     }.getType();
JM 52
31abc2 53     private static final Type REPOSITORIES_TYPE = new TypeToken<Map<String, RepositoryModel>>() {
JM 54     }.getType();
55
56     private static final Type USERS_TYPE = new TypeToken<Collection<UserModel>>() {
f08aab 57     }.getType();
JM 58
59     private static final Type TEAMS_TYPE = new TypeToken<Collection<TeamModel>>() {
31abc2 60     }.getType();
JM 61
62     private static final Type REGISTRATIONS_TYPE = new TypeToken<Collection<FederationModel>>() {
63     }.getType();
64
65     private static final Type PROPOSALS_TYPE = new TypeToken<Collection<FederationProposal>>() {
66     }.getType();
67
68     private static final Type SETS_TYPE = new TypeToken<Collection<FederationSet>>() {
93f0b1 69     }.getType();
JM 70
c75304 71     private static final Type BRANCHES_TYPE = new TypeToken<Map<String, Collection<String>>>() {
JM 72     }.getType();
73
93f0b1 74     /**
JM 75      * 
76      * @param remoteURL
77      *            the url of the remote gitblit instance
78      * @param req
79      *            the rpc request type
80      * @return
81      */
82     public static String asLink(String remoteURL, RpcRequest req) {
31abc2 83         return asLink(remoteURL, req, null);
JM 84     }
85
86     /**
87      * 
88      * @param remoteURL
89      *            the url of the remote gitblit instance
90      * @param req
91      *            the rpc request type
92      * @param name
93      *            the name of the actionable object
94      * @return
95      */
96     public static String asLink(String remoteURL, RpcRequest req, String name) {
93f0b1 97         if (remoteURL.length() > 0 && remoteURL.charAt(remoteURL.length() - 1) == '/') {
JM 98             remoteURL = remoteURL.substring(0, remoteURL.length() - 1);
99         }
100         if (req == null) {
101             req = RpcRequest.LIST_REPOSITORIES;
102         }
31abc2 103         return remoteURL + Constants.RPC_PATH + "?req=" + req.name().toLowerCase()
f08aab 104                 + (name == null ? "" : ("&name=" + StringUtils.encodeURL(name)));
JM 105     }
106
107     /**
108      * Returns the version of the RPC protocol on the server.
109      * 
110      * @param serverUrl
111      * @param account
112      * @param password
113      * @return the protocol version
114      * @throws IOException
115      */
116     public static int getProtocolVersion(String serverUrl, String account, char[] password)
117             throws IOException {
118         String url = asLink(serverUrl, RpcRequest.GET_PROTOCOL);
119         int protocol = 1;
120         try {
121             protocol = JsonUtils.retrieveJson(url, Integer.class, account, password);
122         } catch (UnknownRequestException e) {
123             // v0.7.0 (protocol 1) did not have this request type 
124         }
125         return protocol;
93f0b1 126     }
31abc2 127
93f0b1 128     /**
JM 129      * Retrieves a map of the repositories at the remote gitblit instance keyed
130      * by the repository clone url.
131      * 
132      * @param serverUrl
31abc2 133      * @param account
JM 134      * @param password
93f0b1 135      * @return a map of cloneable repositories
31abc2 136      * @throws IOException
93f0b1 137      */
31abc2 138     public static Map<String, RepositoryModel> getRepositories(String serverUrl, String account,
JM 139             char[] password) throws IOException {
93f0b1 140         String url = asLink(serverUrl, RpcRequest.LIST_REPOSITORIES);
31abc2 141         Map<String, RepositoryModel> models = JsonUtils.retrieveJson(url, REPOSITORIES_TYPE,
JM 142                 account, password);
93f0b1 143         return models;
JM 144     }
145
146     /**
147      * Tries to pull the gitblit user accounts from the remote gitblit instance.
148      * 
149      * @param serverUrl
31abc2 150      * @param account
JM 151      * @param password
93f0b1 152      * @return a collection of UserModel objects
31abc2 153      * @throws IOException
93f0b1 154      */
31abc2 155     public static List<UserModel> getUsers(String serverUrl, String account, char[] password)
JM 156             throws IOException {
93f0b1 157         String url = asLink(serverUrl, RpcRequest.LIST_USERS);
31abc2 158         Collection<UserModel> models = JsonUtils.retrieveJson(url, USERS_TYPE, account, password);
JM 159         List<UserModel> list = new ArrayList<UserModel>(models);
f08aab 160         return list;
JM 161     }
162
163     /**
164      * Tries to pull the gitblit team definitions from the remote gitblit
165      * instance.
166      * 
167      * @param serverUrl
168      * @param account
169      * @param password
170      * @return a collection of UserModel objects
171      * @throws IOException
172      */
173     public static List<TeamModel> getTeams(String serverUrl, String account, char[] password)
174             throws IOException {
175         String url = asLink(serverUrl, RpcRequest.LIST_TEAMS);
176         Collection<TeamModel> models = JsonUtils.retrieveJson(url, TEAMS_TYPE, account, password);
177         List<TeamModel> list = new ArrayList<TeamModel>(models);
31abc2 178         return list;
JM 179     }
180
181     /**
182      * Create a repository on the Gitblit server.
183      * 
184      * @param repository
185      * @param serverUrl
186      * @param account
187      * @param password
188      * @return true if the action succeeded
189      * @throws IOException
190      */
191     public static boolean createRepository(RepositoryModel repository, String serverUrl,
192             String account, char[] password) throws IOException {
773bb6 193         // ensure repository name ends with .git
JM 194         if (!repository.name.endsWith(".git")) {
195             repository.name += ".git";
196         }
31abc2 197         return doAction(RpcRequest.CREATE_REPOSITORY, null, repository, serverUrl, account,
JM 198                 password);
199
200     }
201
202     /**
203      * Send a revised version of the repository model to the Gitblit server.
204      * 
205      * @param repository
206      * @param serverUrl
207      * @param account
208      * @param password
209      * @return true if the action succeeded
210      * @throws IOException
211      */
212     public static boolean updateRepository(String repositoryName, RepositoryModel repository,
213             String serverUrl, String account, char[] password) throws IOException {
214         return doAction(RpcRequest.EDIT_REPOSITORY, repositoryName, repository, serverUrl, account,
215                 password);
216     }
217
218     /**
219      * Delete a repository from the Gitblit server.
220      * 
221      * @param repository
222      * @param serverUrl
223      * @param account
224      * @param password
225      * @return true if the action succeeded
226      * @throws IOException
227      */
228     public static boolean deleteRepository(RepositoryModel repository, String serverUrl,
229             String account, char[] password) throws IOException {
230         return doAction(RpcRequest.DELETE_REPOSITORY, null, repository, serverUrl, account,
231                 password);
232
233     }
234
235     /**
236      * Create a user on the Gitblit server.
237      * 
238      * @param user
239      * @param serverUrl
240      * @param account
241      * @param password
242      * @return true if the action succeeded
243      * @throws IOException
244      */
245     public static boolean createUser(UserModel user, String serverUrl, String account,
246             char[] password) throws IOException {
247         return doAction(RpcRequest.CREATE_USER, null, user, serverUrl, account, password);
248
249     }
250
251     /**
252      * Send a revised version of the user model to the Gitblit server.
253      * 
254      * @param user
255      * @param serverUrl
256      * @param account
257      * @param password
258      * @return true if the action succeeded
259      * @throws IOException
260      */
261     public static boolean updateUser(String username, UserModel user, String serverUrl,
262             String account, char[] password) throws IOException {
263         return doAction(RpcRequest.EDIT_USER, username, user, serverUrl, account, password);
264
265     }
266
267     /**
268      * Deletes a user from the Gitblit server.
269      * 
270      * @param user
271      * @param serverUrl
272      * @param account
273      * @param password
274      * @return true if the action succeeded
275      * @throws IOException
276      */
277     public static boolean deleteUser(UserModel user, String serverUrl, String account,
278             char[] password) throws IOException {
279         return doAction(RpcRequest.DELETE_USER, null, user, serverUrl, account, password);
280     }
281
282     /**
f08aab 283      * Create a team on the Gitblit server.
JM 284      * 
285      * @param team
286      * @param serverUrl
287      * @param account
288      * @param password
289      * @return true if the action succeeded
290      * @throws IOException
291      */
292     public static boolean createTeam(TeamModel team, String serverUrl, String account,
293             char[] password) throws IOException {
294         return doAction(RpcRequest.CREATE_TEAM, null, team, serverUrl, account, password);
295
296     }
297
298     /**
299      * Send a revised version of the team model to the Gitblit server.
300      * 
301      * @param team
302      * @param serverUrl
303      * @param account
304      * @param password
305      * @return true if the action succeeded
306      * @throws IOException
307      */
308     public static boolean updateTeam(String teamname, TeamModel team, String serverUrl,
309             String account, char[] password) throws IOException {
310         return doAction(RpcRequest.EDIT_TEAM, teamname, team, serverUrl, account, password);
311
312     }
313
314     /**
315      * Deletes a team from the Gitblit server.
316      * 
317      * @param team
318      * @param serverUrl
319      * @param account
320      * @param password
321      * @return true if the action succeeded
322      * @throws IOException
323      */
324     public static boolean deleteTeam(TeamModel team, String serverUrl, String account,
325             char[] password) throws IOException {
326         return doAction(RpcRequest.DELETE_TEAM, null, team, serverUrl, account, password);
327     }
328
329     /**
31abc2 330      * Retrieves the list of users that can access the specified repository.
JM 331      * 
332      * @param repository
333      * @param serverUrl
334      * @param account
335      * @param password
336      * @return list of members
337      * @throws IOException
338      */
339     public static List<String> getRepositoryMembers(RepositoryModel repository, String serverUrl,
340             String account, char[] password) throws IOException {
341         String url = asLink(serverUrl, RpcRequest.LIST_REPOSITORY_MEMBERS, repository.name);
342         Collection<String> list = JsonUtils.retrieveJson(url, NAMES_TYPE, account, password);
343         return new ArrayList<String>(list);
344     }
345
346     /**
f08aab 347      * Sets the repository user membership list.
31abc2 348      * 
JM 349      * @param repository
350      * @param memberships
351      * @param serverUrl
352      * @param account
353      * @param password
354      * @return true if the action succeeded
355      * @throws IOException
356      */
357     public static boolean setRepositoryMembers(RepositoryModel repository,
358             List<String> memberships, String serverUrl, String account, char[] password)
359             throws IOException {
360         return doAction(RpcRequest.SET_REPOSITORY_MEMBERS, repository.name, memberships, serverUrl,
361                 account, password);
362     }
363
364     /**
f08aab 365      * Retrieves the list of teams that can access the specified repository.
JM 366      * 
367      * @param repository
368      * @param serverUrl
369      * @param account
370      * @param password
371      * @return list of teams
372      * @throws IOException
373      */
374     public static List<String> getRepositoryTeams(RepositoryModel repository, String serverUrl,
375             String account, char[] password) throws IOException {
376         String url = asLink(serverUrl, RpcRequest.LIST_REPOSITORY_TEAMS, repository.name);
377         Collection<String> list = JsonUtils.retrieveJson(url, NAMES_TYPE, account, password);
378         return new ArrayList<String>(list);
379     }
380
381     /**
382      * Sets the repository team membership list.
383      * 
384      * @param repository
385      * @param teams
386      * @param serverUrl
387      * @param account
388      * @param password
389      * @return true if the action succeeded
390      * @throws IOException
391      */
392     public static boolean setRepositoryTeams(RepositoryModel repository,
393             List<String> teams, String serverUrl, String account, char[] password)
394             throws IOException {
395         return doAction(RpcRequest.SET_REPOSITORY_TEAMS, repository.name, teams, serverUrl,
396                 account, password);
397     }
398
399     /**
31abc2 400      * Retrieves the list of federation registrations. These are the list of
JM 401      * registrations that this Gitblit instance is pulling from.
402      * 
403      * @param serverUrl
404      * @param account
405      * @param password
406      * @return a collection of FederationRegistration objects
407      * @throws IOException
408      */
409     public static List<FederationModel> getFederationRegistrations(String serverUrl,
410             String account, char[] password) throws IOException {
411         String url = asLink(serverUrl, RpcRequest.LIST_FEDERATION_REGISTRATIONS);
412         Collection<FederationModel> registrations = JsonUtils.retrieveJson(url, REGISTRATIONS_TYPE,
413                 account, password);
414         List<FederationModel> list = new ArrayList<FederationModel>(registrations);
415         return list;
416     }
417
418     /**
419      * Retrieves the list of federation result registrations. These are the
420      * results reported back to this Gitblit instance from a federation client.
421      * 
422      * @param serverUrl
423      * @param account
424      * @param password
425      * @return a collection of FederationRegistration objects
426      * @throws IOException
427      */
428     public static List<FederationModel> getFederationResultRegistrations(String serverUrl,
429             String account, char[] password) throws IOException {
430         String url = asLink(serverUrl, RpcRequest.LIST_FEDERATION_RESULTS);
431         Collection<FederationModel> registrations = JsonUtils.retrieveJson(url, REGISTRATIONS_TYPE,
432                 account, password);
433         List<FederationModel> list = new ArrayList<FederationModel>(registrations);
434         return list;
435     }
da0269 436
31abc2 437     /**
JM 438      * Retrieves the list of federation proposals.
439      * 
440      * @param serverUrl
441      * @param account
442      * @param password
443      * @return a collection of FederationProposal objects
444      * @throws IOException
445      */
da0269 446     public static List<FederationProposal> getFederationProposals(String serverUrl, String account,
JM 447             char[] password) throws IOException {
31abc2 448         String url = asLink(serverUrl, RpcRequest.LIST_FEDERATION_PROPOSALS);
JM 449         Collection<FederationProposal> proposals = JsonUtils.retrieveJson(url, PROPOSALS_TYPE,
450                 account, password);
451         List<FederationProposal> list = new ArrayList<FederationProposal>(proposals);
452         return list;
453     }
da0269 454
31abc2 455     /**
JM 456      * Retrieves the list of federation repository sets.
457      * 
458      * @param serverUrl
459      * @param account
460      * @param password
461      * @return a collection of FederationSet objects
462      * @throws IOException
463      */
da0269 464     public static List<FederationSet> getFederationSets(String serverUrl, String account,
JM 465             char[] password) throws IOException {
31abc2 466         String url = asLink(serverUrl, RpcRequest.LIST_FEDERATION_SETS);
da0269 467         Collection<FederationSet> sets = JsonUtils.retrieveJson(url, SETS_TYPE, account, password);
31abc2 468         List<FederationSet> list = new ArrayList<FederationSet>(sets);
JM 469         return list;
da0269 470     }
JM 471
472     /**
473      * Retrieves the settings of the Gitblit server.
474      * 
475      * @param serverUrl
476      * @param account
477      * @param password
84c1d5 478      * @return an Settings object
da0269 479      * @throws IOException
JM 480      */
84c1d5 481     public static ServerSettings getSettings(String serverUrl, String account, char[] password)
JM 482             throws IOException {
da0269 483         String url = asLink(serverUrl, RpcRequest.LIST_SETTINGS);
d03aff 484         ServerSettings settings = JsonUtils.retrieveJson(url, ServerSettings.class, account,
JM 485                 password);
da0269 486         return settings;
b75734 487     }
JM 488
489     /**
d03aff 490      * Update the settings on the Gitblit server.
JM 491      * 
492      * @param settings
493      *            the settings to update
494      * @param serverUrl
495      * @param account
496      * @param password
497      * @return true if the action succeeded
498      * @throws IOException
499      */
500     public static boolean updateSettings(Map<String, String> settings, String serverUrl,
501             String account, char[] password) throws IOException {
502         return doAction(RpcRequest.EDIT_SETTINGS, null, settings, serverUrl, account, password);
503
504     }
505
506     /**
b75734 507      * Retrieves the server status object.
JM 508      * 
509      * @param serverUrl
510      * @param account
511      * @param password
512      * @return an ServerStatus object
513      * @throws IOException
514      */
515     public static ServerStatus getStatus(String serverUrl, String account, char[] password)
516             throws IOException {
84c1d5 517         String url = asLink(serverUrl, RpcRequest.LIST_STATUS);
b75734 518         ServerStatus status = JsonUtils.retrieveJson(url, ServerStatus.class, account, password);
JM 519         return status;
31abc2 520     }
JM 521
522     /**
17820f 523      * Retrieves a map of local branches in the Gitblit server keyed by
c75304 524      * repository.
JM 525      * 
526      * @param serverUrl
527      * @param account
528      * @param password
529      * @return
530      * @throws IOException
531      */
17820f 532     public static Map<String, Collection<String>> getBranches(String serverUrl, String account,
JM 533             char[] password) throws IOException {
c75304 534         String url = asLink(serverUrl, RpcRequest.LIST_BRANCHES);
17820f 535         Map<String, Collection<String>> branches = JsonUtils.retrieveJson(url, BRANCHES_TYPE,
JM 536                 account, password);
537         return branches;
538     }
539
540     /**
541      * Retrieves a list of available branch feeds in the Gitblit server.
542      * 
543      * @param serverUrl
544      * @param account
545      * @param password
546      * @return
547      * @throws IOException
548      */
549     public static List<FeedModel> getBranchFeeds(String serverUrl, String account, char[] password)
550             throws IOException {
551         List<FeedModel> feeds = new ArrayList<FeedModel>();
552         Map<String, Collection<String>> allBranches = getBranches(serverUrl, account, password);
553         for (Map.Entry<String, Collection<String>> entry : allBranches.entrySet()) {
554             for (String branch : entry.getValue()) {
555                 FeedModel feed = new FeedModel();
556                 feed.repository = entry.getKey();
557                 feed.branch = branch;
558                 feeds.add(feed);
559             }
560         }
561         return feeds;
c75304 562     }
JM 563
564     /**
31abc2 565      * Do the specified administrative action on the Gitblit server.
JM 566      * 
567      * @param request
568      * @param name
569      *            the name of the object (may be null)
570      * @param object
571      * @param serverUrl
572      * @param account
573      * @param password
574      * @return true if the action succeeded
575      * @throws IOException
576      */
577     protected static boolean doAction(RpcRequest request, String name, Object object,
578             String serverUrl, String account, char[] password) throws IOException {
579         String url = asLink(serverUrl, request, name);
580         String json = JsonUtils.toJsonString(object);
581         int resultCode = JsonUtils.sendJsonString(url, json, account, password);
582         return resultCode == 200;
da0269 583     }
93f0b1 584 }