You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RpcUtils.java 21KB

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