From dd9ae71bc1cb13b90dcc8d9689550eb7dfe7d035 Mon Sep 17 00:00:00 2001 From: James Moger <james.moger@gitblit.com> Date: Sun, 25 Sep 2011 09:45:45 -0400 Subject: [PATCH] Revised federation proposal mechanism. Added SendProposalPage. --- src/com/gitblit/GitBlit.java | 80 ++++++++++++++++++++++++++++++++++++++++ 1 files changed, 80 insertions(+), 0 deletions(-) diff --git a/src/com/gitblit/GitBlit.java b/src/com/gitblit/GitBlit.java index 3e974b3..f3ad363 100644 --- a/src/com/gitblit/GitBlit.java +++ b/src/com/gitblit/GitBlit.java @@ -1102,6 +1102,86 @@ } /** + * Get repositories for the specified token. + * + * @param gitblitUrl + * the base url of this gitblit instance + * @param token + * the federation token + * @return a map of <cloneurl, RepositoryModel> + */ + public Map<String, RepositoryModel> getRepositories(String gitblitUrl, String token) { + Map<String, String> federationSets = new HashMap<String, String>(); + for (String set : getStrings(Keys.federation.sets)) { + federationSets.put(getFederationToken(set), set); + } + + // Determine the Gitblit clone url + StringBuilder sb = new StringBuilder(); + sb.append(gitblitUrl); + sb.append(Constants.GIT_PATH); + sb.append("{0}"); + String cloneUrl = sb.toString(); + + // Retrieve all available repositories + UserModel user = new UserModel(Constants.FEDERATION_USER); + user.canAdmin = true; + List<RepositoryModel> list = getRepositoryModels(user); + + // create the [cloneurl, repositoryModel] map + Map<String, RepositoryModel> repositories = new HashMap<String, RepositoryModel>(); + for (RepositoryModel model : list) { + // by default, setup the url for THIS repository + String url = MessageFormat.format(cloneUrl, model.name); + switch (model.federationStrategy) { + case EXCLUDE: + // skip this repository + continue; + case FEDERATE_ORIGIN: + // federate the origin, if it is defined + if (!StringUtils.isEmpty(model.origin)) { + url = model.origin; + } + break; + } + + if (federationSets.containsKey(token)) { + // include repositories only for federation set + String set = federationSets.get(token); + if (model.federationSets.contains(set)) { + repositories.put(url, model); + } + } else { + // standard federation token for ALL + repositories.put(url, model); + } + } + return repositories; + } + + /** + * Creates a proposal from the token. + * + * @param gitblitUrl + * the url of this Gitblit instance + * @param token + * @return a potential proposal + */ + public FederationProposal createFederationProposal(String gitblitUrl, String token) { + FederationToken tokenType = FederationToken.REPOSITORIES; + for (FederationToken type : FederationToken.values()) { + if (token.equals(getFederationToken(type))) { + tokenType = type; + break; + } + } + Map<String, RepositoryModel> repositories = getRepositories(gitblitUrl, token); + FederationProposal proposal = new FederationProposal(gitblitUrl, tokenType, token, + repositories); + return proposal; + } + + /** * Returns the proposal identified by the supplied token. * * @param token -- Gitblit v1.9.1