From 14cbbeaf61ce67c1ef5a6445515f3134dd5690ce Mon Sep 17 00:00:00 2001 From: James Moger Date: Fri, 29 Nov 2013 20:06:09 -0500 Subject: [PATCH] New canonical base url setting for repository urls and notifications Change-Id: I86a1eb61c2351d1cf3f6b3becf00f2a36d5c8ed6 --- src/main/distrib/data/gitblit.properties | 6 ++++++ .../java/com/gitblit/git/GitblitReceivePack.java | 6 ------ .../com/gitblit/git/GitblitReceivePackFactory.java | 10 ++++++---- .../java/com/gitblit/manager/GitblitManager.java | 6 +++++- .../java/com/gitblit/servlet/FederationServlet.java | 12 +++++++++--- src/main/java/com/gitblit/servlet/RpcServlet.java | 10 ++++++++-- .../java/com/gitblit/servlet/SyndicationServlet.java | 5 ++++- 7 files changed, 38 insertions(+), 17 deletions(-) diff --git a/src/main/distrib/data/gitblit.properties b/src/main/distrib/data/gitblit.properties index edfa1c4c..482a8353 100644 --- a/src/main/distrib/data/gitblit.properties +++ b/src/main/distrib/data/gitblit.properties @@ -612,6 +612,12 @@ realm.minPasswordLength = 5 # SINCE 0.5.0 web.siteName = +# The canonical url of your Gitblit server to bs used in email notifications. +# e.g. web.canonicalUrl = https://demo-gitblit.rhcloud.com +# +# SINCE 1.4.0 +web.canonicalUrl = + # You may specify a different logo image for the header but it must be 120x45px. # If the specified file does not exist, the default Gitblit logo will be used. # diff --git a/src/main/java/com/gitblit/git/GitblitReceivePack.java b/src/main/java/com/gitblit/git/GitblitReceivePack.java index e5c0ea45..c3734656 100644 --- a/src/main/java/com/gitblit/git/GitblitReceivePack.java +++ b/src/main/java/com/gitblit/git/GitblitReceivePack.java @@ -85,8 +85,6 @@ public class GitblitReceivePack extends ReceivePack implements PreReceiveHook, P protected String gitblitUrl; - protected String repositoryUrl; - protected GroovyScriptEngine gse; private final IStoredSettings settings; @@ -390,10 +388,6 @@ public class GitblitReceivePack extends ReceivePack implements PreReceiveHook, P this.gitblitUrl = url; } - protected void setRepositoryUrl(String url) { - this.repositoryUrl = url; - } - protected void sendRejection(final ReceiveCommand cmd, final String why, Object... objects) { String text; if (ArrayUtils.isEmpty(objects)) { diff --git a/src/main/java/com/gitblit/git/GitblitReceivePackFactory.java b/src/main/java/com/gitblit/git/GitblitReceivePackFactory.java index bdf9b1ad..b8b49bcd 100644 --- a/src/main/java/com/gitblit/git/GitblitReceivePackFactory.java +++ b/src/main/java/com/gitblit/git/GitblitReceivePackFactory.java @@ -63,7 +63,6 @@ public class GitblitReceivePackFactory implements ReceivePackFactory { String repositoryName = ""; String origin = ""; String gitblitUrl = ""; - String repositoryUrl = ""; int timeout = 0; if (req instanceof HttpServletRequest) { @@ -72,7 +71,6 @@ public class GitblitReceivePackFactory implements ReceivePackFactory { repositoryName = request.getAttribute("gitblitRepositoryName").toString(); origin = request.getRemoteHost(); gitblitUrl = HttpUtils.getGitblitURL(request); - repositoryUrl = request.getRequestURI(); // determine pushing user String username = request.getRemoteUser(); @@ -98,11 +96,15 @@ public class GitblitReceivePackFactory implements ReceivePackFactory { throw new ServiceNotEnabledException(); } + String url = settings.getString(Keys.web.canonicalUrl, null); + if (StringUtils.isEmpty(url)) { + url = gitblitUrl; + } + final RepositoryModel repository = gitblit.getRepositoryModel(repositoryName); final GitblitReceivePack rp = new GitblitReceivePack(gitblit, db, repository, user); - rp.setGitblitUrl(gitblitUrl); - rp.setRepositoryUrl(repositoryUrl); + rp.setGitblitUrl(url); rp.setRefLogIdent(new PersonIdent(user.username, user.username + "@" + origin)); rp.setTimeout(timeout); diff --git a/src/main/java/com/gitblit/manager/GitblitManager.java b/src/main/java/com/gitblit/manager/GitblitManager.java index 75b258a5..7e788361 100644 --- a/src/main/java/com/gitblit/manager/GitblitManager.java +++ b/src/main/java/com/gitblit/manager/GitblitManager.java @@ -337,8 +337,12 @@ public class GitblitManager implements IGitblit { } protected String getRepositoryUrl(HttpServletRequest request, String username, RepositoryModel repository) { + String gitblitUrl = settings.getString(Keys.web.canonicalUrl, null); + if (StringUtils.isEmpty(gitblitUrl)) { + gitblitUrl = HttpUtils.getGitblitURL(request); + } StringBuilder sb = new StringBuilder(); - sb.append(HttpUtils.getGitblitURL(request)); + sb.append(gitblitUrl); sb.append(Constants.R_PATH); sb.append(repository.name); diff --git a/src/main/java/com/gitblit/servlet/FederationServlet.java b/src/main/java/com/gitblit/servlet/FederationServlet.java index 372292de..8dbf0e16 100644 --- a/src/main/java/com/gitblit/servlet/FederationServlet.java +++ b/src/main/java/com/gitblit/servlet/FederationServlet.java @@ -138,8 +138,11 @@ public class FederationServlet extends JsonServlet { return; } - String url = HttpUtils.getGitblitURL(request); - federationManager.submitFederationProposal(proposal, url); + String gitblitUrl = settings.getString(Keys.web.canonicalUrl, null); + if (StringUtils.isEmpty(gitblitUrl)) { + gitblitUrl = HttpUtils.getGitblitURL(request); + } + federationManager.submitFederationProposal(proposal, gitblitUrl); logger.info(MessageFormat.format( "Submitted {0} federation proposal to pull {1} repositories from {2}", proposal.tokenType.name(), proposal.repositories.size(), proposal.url)); @@ -185,7 +188,10 @@ public class FederationServlet extends JsonServlet { Object result = null; if (FederationRequest.PULL_REPOSITORIES.equals(reqType)) { - String gitblitUrl = HttpUtils.getGitblitURL(request); + String gitblitUrl = settings.getString(Keys.web.canonicalUrl, null); + if (StringUtils.isEmpty(gitblitUrl)) { + gitblitUrl = HttpUtils.getGitblitURL(request); + } result = federationManager.getRepositories(gitblitUrl, token); } else { if (FederationRequest.PULL_SETTINGS.equals(reqType)) { diff --git a/src/main/java/com/gitblit/servlet/RpcServlet.java b/src/main/java/com/gitblit/servlet/RpcServlet.java index e7b3ed2c..28f0d5bf 100644 --- a/src/main/java/com/gitblit/servlet/RpcServlet.java +++ b/src/main/java/com/gitblit/servlet/RpcServlet.java @@ -101,7 +101,10 @@ public class RpcServlet extends JsonServlet { result = PROTOCOL_VERSION; } else if (RpcRequest.LIST_REPOSITORIES.equals(reqType)) { // Determine the Gitblit clone url - String gitblitUrl = HttpUtils.getGitblitURL(request); + String gitblitUrl = settings.getString(Keys.web.canonicalUrl, null); + if (StringUtils.isEmpty(gitblitUrl)) { + gitblitUrl = HttpUtils.getGitblitURL(request); + } StringBuilder sb = new StringBuilder(); sb.append(gitblitUrl); sb.append(Constants.R_PATH); @@ -320,7 +323,10 @@ public class RpcServlet extends JsonServlet { } else if (RpcRequest.LIST_FEDERATION_SETS.equals(reqType)) { // return the list of federation sets if (allowAdmin && gitblit.canFederate()) { - String gitblitUrl = HttpUtils.getGitblitURL(request); + String gitblitUrl = settings.getString(Keys.web.canonicalUrl, null); + if (StringUtils.isEmpty(gitblitUrl)) { + gitblitUrl = HttpUtils.getGitblitURL(request); + } result = gitblit.getFederationSets(gitblitUrl); } else { response.sendError(notAllowedCode); diff --git a/src/main/java/com/gitblit/servlet/SyndicationServlet.java b/src/main/java/com/gitblit/servlet/SyndicationServlet.java index 8acd0192..66ca4a19 100644 --- a/src/main/java/com/gitblit/servlet/SyndicationServlet.java +++ b/src/main/java/com/gitblit/servlet/SyndicationServlet.java @@ -222,7 +222,10 @@ public class SyndicationServlet extends DaggerServlet { // parameterized parameters urlPattern = "{0}/commit/?r={1}&h={2}"; } - String gitblitUrl = HttpUtils.getGitblitURL(request); + String gitblitUrl = settings.getString(Keys.web.canonicalUrl, null); + if (StringUtils.isEmpty(gitblitUrl)) { + gitblitUrl = HttpUtils.getGitblitURL(request); + } char fsc = settings.getChar(Keys.web.forwardSlashCharacter, '/'); List entries = new ArrayList(); -- 2.39.5