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 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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.models.FederationModel;
  26. import com.gitblit.models.FederationProposal;
  27. import com.gitblit.models.FederationSet;
  28. import com.gitblit.models.FeedModel;
  29. import com.gitblit.models.RepositoryModel;
  30. import com.gitblit.models.ServerSettings;
  31. import com.gitblit.models.ServerStatus;
  32. import com.gitblit.models.UserModel;
  33. import com.google.gson.reflect.TypeToken;
  34. /**
  35. * Utility methods for rpc calls.
  36. *
  37. * @author James Moger
  38. *
  39. */
  40. public class RpcUtils {
  41. public static final Type NAMES_TYPE = new TypeToken<Collection<String>>() {
  42. }.getType();
  43. public static final Type SETTINGS_TYPE = new TypeToken<Map<String, String>>() {
  44. }.getType();
  45. private static final Type REPOSITORIES_TYPE = new TypeToken<Map<String, RepositoryModel>>() {
  46. }.getType();
  47. private static final Type USERS_TYPE = new TypeToken<Collection<UserModel>>() {
  48. }.getType();
  49. private static final Type REGISTRATIONS_TYPE = new TypeToken<Collection<FederationModel>>() {
  50. }.getType();
  51. private static final Type PROPOSALS_TYPE = new TypeToken<Collection<FederationProposal>>() {
  52. }.getType();
  53. private static final Type SETS_TYPE = new TypeToken<Collection<FederationSet>>() {
  54. }.getType();
  55. private static final Type BRANCHES_TYPE = new TypeToken<Map<String, Collection<String>>>() {
  56. }.getType();
  57. /**
  58. *
  59. * @param remoteURL
  60. * the url of the remote gitblit instance
  61. * @param req
  62. * the rpc request type
  63. * @return
  64. */
  65. public static String asLink(String remoteURL, RpcRequest req) {
  66. return asLink(remoteURL, req, null);
  67. }
  68. /**
  69. *
  70. * @param remoteURL
  71. * the url of the remote gitblit instance
  72. * @param req
  73. * the rpc request type
  74. * @param name
  75. * the name of the actionable object
  76. * @return
  77. */
  78. public static String asLink(String remoteURL, RpcRequest req, String name) {
  79. if (remoteURL.length() > 0 && remoteURL.charAt(remoteURL.length() - 1) == '/') {
  80. remoteURL = remoteURL.substring(0, remoteURL.length() - 1);
  81. }
  82. if (req == null) {
  83. req = RpcRequest.LIST_REPOSITORIES;
  84. }
  85. return remoteURL + Constants.RPC_PATH + "?req=" + req.name().toLowerCase()
  86. + (name == null ? "" : ("&name=" + name));
  87. }
  88. /**
  89. * Retrieves a map of the repositories at the remote gitblit instance keyed
  90. * by the repository clone url.
  91. *
  92. * @param serverUrl
  93. * @param account
  94. * @param password
  95. * @return a map of cloneable repositories
  96. * @throws IOException
  97. */
  98. public static Map<String, RepositoryModel> getRepositories(String serverUrl, String account,
  99. char[] password) throws IOException {
  100. String url = asLink(serverUrl, RpcRequest.LIST_REPOSITORIES);
  101. Map<String, RepositoryModel> models = JsonUtils.retrieveJson(url, REPOSITORIES_TYPE,
  102. account, password);
  103. return models;
  104. }
  105. /**
  106. * Tries to pull the gitblit user accounts from the remote gitblit instance.
  107. *
  108. * @param serverUrl
  109. * @param account
  110. * @param password
  111. * @return a collection of UserModel objects
  112. * @throws IOException
  113. */
  114. public static List<UserModel> getUsers(String serverUrl, String account, char[] password)
  115. throws IOException {
  116. String url = asLink(serverUrl, RpcRequest.LIST_USERS);
  117. Collection<UserModel> models = JsonUtils.retrieveJson(url, USERS_TYPE, account, password);
  118. List<UserModel> list = new ArrayList<UserModel>(models);
  119. return list;
  120. }
  121. /**
  122. * Create a repository on the Gitblit server.
  123. *
  124. * @param repository
  125. * @param serverUrl
  126. * @param account
  127. * @param password
  128. * @return true if the action succeeded
  129. * @throws IOException
  130. */
  131. public static boolean createRepository(RepositoryModel repository, String serverUrl,
  132. String account, char[] password) throws IOException {
  133. // ensure repository name ends with .git
  134. if (!repository.name.endsWith(".git")) {
  135. repository.name += ".git";
  136. }
  137. return doAction(RpcRequest.CREATE_REPOSITORY, null, repository, serverUrl, account,
  138. password);
  139. }
  140. /**
  141. * Send a revised version of the repository model to the Gitblit server.
  142. *
  143. * @param repository
  144. * @param serverUrl
  145. * @param account
  146. * @param password
  147. * @return true if the action succeeded
  148. * @throws IOException
  149. */
  150. public static boolean updateRepository(String repositoryName, RepositoryModel repository,
  151. String serverUrl, String account, char[] password) throws IOException {
  152. return doAction(RpcRequest.EDIT_REPOSITORY, repositoryName, repository, serverUrl, account,
  153. password);
  154. }
  155. /**
  156. * Delete a repository from the Gitblit server.
  157. *
  158. * @param repository
  159. * @param serverUrl
  160. * @param account
  161. * @param password
  162. * @return true if the action succeeded
  163. * @throws IOException
  164. */
  165. public static boolean deleteRepository(RepositoryModel repository, String serverUrl,
  166. String account, char[] password) throws IOException {
  167. return doAction(RpcRequest.DELETE_REPOSITORY, null, repository, serverUrl, account,
  168. password);
  169. }
  170. /**
  171. * Create a user on the Gitblit server.
  172. *
  173. * @param user
  174. * @param serverUrl
  175. * @param account
  176. * @param password
  177. * @return true if the action succeeded
  178. * @throws IOException
  179. */
  180. public static boolean createUser(UserModel user, String serverUrl, String account,
  181. char[] password) throws IOException {
  182. return doAction(RpcRequest.CREATE_USER, null, user, serverUrl, account, password);
  183. }
  184. /**
  185. * Send a revised version of the user model to the Gitblit server.
  186. *
  187. * @param user
  188. * @param serverUrl
  189. * @param account
  190. * @param password
  191. * @return true if the action succeeded
  192. * @throws IOException
  193. */
  194. public static boolean updateUser(String username, UserModel user, String serverUrl,
  195. String account, char[] password) throws IOException {
  196. return doAction(RpcRequest.EDIT_USER, username, user, serverUrl, account, password);
  197. }
  198. /**
  199. * Deletes a user from the Gitblit server.
  200. *
  201. * @param user
  202. * @param serverUrl
  203. * @param account
  204. * @param password
  205. * @return true if the action succeeded
  206. * @throws IOException
  207. */
  208. public static boolean deleteUser(UserModel user, String serverUrl, String account,
  209. char[] password) throws IOException {
  210. return doAction(RpcRequest.DELETE_USER, null, user, serverUrl, account, password);
  211. }
  212. /**
  213. * Retrieves the list of users that can access the specified repository.
  214. *
  215. * @param repository
  216. * @param serverUrl
  217. * @param account
  218. * @param password
  219. * @return list of members
  220. * @throws IOException
  221. */
  222. public static List<String> getRepositoryMembers(RepositoryModel repository, String serverUrl,
  223. String account, char[] password) throws IOException {
  224. String url = asLink(serverUrl, RpcRequest.LIST_REPOSITORY_MEMBERS, repository.name);
  225. Collection<String> list = JsonUtils.retrieveJson(url, NAMES_TYPE, account, password);
  226. return new ArrayList<String>(list);
  227. }
  228. /**
  229. * Sets the repository membership list.
  230. *
  231. * @param repository
  232. * @param memberships
  233. * @param serverUrl
  234. * @param account
  235. * @param password
  236. * @return true if the action succeeded
  237. * @throws IOException
  238. */
  239. public static boolean setRepositoryMembers(RepositoryModel repository,
  240. List<String> memberships, String serverUrl, String account, char[] password)
  241. throws IOException {
  242. return doAction(RpcRequest.SET_REPOSITORY_MEMBERS, repository.name, memberships, serverUrl,
  243. account, password);
  244. }
  245. /**
  246. * Retrieves the list of federation registrations. These are the list of
  247. * registrations that this Gitblit instance is pulling from.
  248. *
  249. * @param serverUrl
  250. * @param account
  251. * @param password
  252. * @return a collection of FederationRegistration objects
  253. * @throws IOException
  254. */
  255. public static List<FederationModel> getFederationRegistrations(String serverUrl,
  256. String account, char[] password) throws IOException {
  257. String url = asLink(serverUrl, RpcRequest.LIST_FEDERATION_REGISTRATIONS);
  258. Collection<FederationModel> registrations = JsonUtils.retrieveJson(url, REGISTRATIONS_TYPE,
  259. account, password);
  260. List<FederationModel> list = new ArrayList<FederationModel>(registrations);
  261. return list;
  262. }
  263. /**
  264. * Retrieves the list of federation result registrations. These are the
  265. * results reported back to this Gitblit instance from a federation client.
  266. *
  267. * @param serverUrl
  268. * @param account
  269. * @param password
  270. * @return a collection of FederationRegistration objects
  271. * @throws IOException
  272. */
  273. public static List<FederationModel> getFederationResultRegistrations(String serverUrl,
  274. String account, char[] password) throws IOException {
  275. String url = asLink(serverUrl, RpcRequest.LIST_FEDERATION_RESULTS);
  276. Collection<FederationModel> registrations = JsonUtils.retrieveJson(url, REGISTRATIONS_TYPE,
  277. account, password);
  278. List<FederationModel> list = new ArrayList<FederationModel>(registrations);
  279. return list;
  280. }
  281. /**
  282. * Retrieves the list of federation proposals.
  283. *
  284. * @param serverUrl
  285. * @param account
  286. * @param password
  287. * @return a collection of FederationProposal objects
  288. * @throws IOException
  289. */
  290. public static List<FederationProposal> getFederationProposals(String serverUrl, String account,
  291. char[] password) throws IOException {
  292. String url = asLink(serverUrl, RpcRequest.LIST_FEDERATION_PROPOSALS);
  293. Collection<FederationProposal> proposals = JsonUtils.retrieveJson(url, PROPOSALS_TYPE,
  294. account, password);
  295. List<FederationProposal> list = new ArrayList<FederationProposal>(proposals);
  296. return list;
  297. }
  298. /**
  299. * Retrieves the list of federation repository sets.
  300. *
  301. * @param serverUrl
  302. * @param account
  303. * @param password
  304. * @return a collection of FederationSet objects
  305. * @throws IOException
  306. */
  307. public static List<FederationSet> getFederationSets(String serverUrl, String account,
  308. char[] password) throws IOException {
  309. String url = asLink(serverUrl, RpcRequest.LIST_FEDERATION_SETS);
  310. Collection<FederationSet> sets = JsonUtils.retrieveJson(url, SETS_TYPE, account, password);
  311. List<FederationSet> list = new ArrayList<FederationSet>(sets);
  312. return list;
  313. }
  314. /**
  315. * Retrieves the settings of the Gitblit server.
  316. *
  317. * @param serverUrl
  318. * @param account
  319. * @param password
  320. * @return an Settings object
  321. * @throws IOException
  322. */
  323. public static ServerSettings getSettings(String serverUrl, String account, char[] password)
  324. throws IOException {
  325. String url = asLink(serverUrl, RpcRequest.LIST_SETTINGS);
  326. ServerSettings settings = JsonUtils.retrieveJson(url, ServerSettings.class, account,
  327. password);
  328. return settings;
  329. }
  330. /**
  331. * Update the settings on the Gitblit server.
  332. *
  333. * @param settings
  334. * the settings to update
  335. * @param serverUrl
  336. * @param account
  337. * @param password
  338. * @return true if the action succeeded
  339. * @throws IOException
  340. */
  341. public static boolean updateSettings(Map<String, String> settings, String serverUrl,
  342. String account, char[] password) throws IOException {
  343. return doAction(RpcRequest.EDIT_SETTINGS, null, settings, serverUrl, account, password);
  344. }
  345. /**
  346. * Retrieves the server status object.
  347. *
  348. * @param serverUrl
  349. * @param account
  350. * @param password
  351. * @return an ServerStatus object
  352. * @throws IOException
  353. */
  354. public static ServerStatus getStatus(String serverUrl, String account, char[] password)
  355. throws IOException {
  356. String url = asLink(serverUrl, RpcRequest.LIST_STATUS);
  357. ServerStatus status = JsonUtils.retrieveJson(url, ServerStatus.class, account, password);
  358. return status;
  359. }
  360. /**
  361. * Retrieves a map of local branches in the Gitblit server keyed by
  362. * repository.
  363. *
  364. * @param serverUrl
  365. * @param account
  366. * @param password
  367. * @return
  368. * @throws IOException
  369. */
  370. public static Map<String, Collection<String>> getBranches(String serverUrl, String account,
  371. char[] password) throws IOException {
  372. String url = asLink(serverUrl, RpcRequest.LIST_BRANCHES);
  373. Map<String, Collection<String>> branches = JsonUtils.retrieveJson(url, BRANCHES_TYPE,
  374. account, password);
  375. return branches;
  376. }
  377. /**
  378. * Retrieves a list of available branch feeds in the Gitblit server.
  379. *
  380. * @param serverUrl
  381. * @param account
  382. * @param password
  383. * @return
  384. * @throws IOException
  385. */
  386. public static List<FeedModel> getBranchFeeds(String serverUrl, String account, char[] password)
  387. throws IOException {
  388. List<FeedModel> feeds = new ArrayList<FeedModel>();
  389. Map<String, Collection<String>> allBranches = getBranches(serverUrl, account, password);
  390. for (Map.Entry<String, Collection<String>> entry : allBranches.entrySet()) {
  391. for (String branch : entry.getValue()) {
  392. FeedModel feed = new FeedModel();
  393. feed.repository = entry.getKey();
  394. feed.branch = branch;
  395. feeds.add(feed);
  396. }
  397. }
  398. return feeds;
  399. }
  400. /**
  401. * Do the specified administrative action on the Gitblit server.
  402. *
  403. * @param request
  404. * @param name
  405. * the name of the object (may be null)
  406. * @param object
  407. * @param serverUrl
  408. * @param account
  409. * @param password
  410. * @return true if the action succeeded
  411. * @throws IOException
  412. */
  413. protected static boolean doAction(RpcRequest request, String name, Object object,
  414. String serverUrl, String account, char[] password) throws IOException {
  415. String url = asLink(serverUrl, request, name);
  416. String json = JsonUtils.toJsonString(object);
  417. int resultCode = JsonUtils.sendJsonString(url, json, account, password);
  418. return resultCode == 200;
  419. }
  420. }