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.

RepositoryUrlPanel.java 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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.wicket.panels;
  17. import java.io.Serializable;
  18. import java.text.MessageFormat;
  19. import java.util.ArrayList;
  20. import java.util.List;
  21. import org.apache.wicket.Component;
  22. import org.apache.wicket.Localizer;
  23. import org.apache.wicket.RequestCycle;
  24. import org.apache.wicket.markup.html.basic.Label;
  25. import org.apache.wicket.markup.repeater.Item;
  26. import org.apache.wicket.markup.repeater.data.DataView;
  27. import org.apache.wicket.markup.repeater.data.ListDataProvider;
  28. import org.apache.wicket.protocol.http.WebRequest;
  29. import com.gitblit.Constants;
  30. import com.gitblit.Constants.AccessPermission;
  31. import com.gitblit.Constants.AccessRestrictionType;
  32. import com.gitblit.GitBlit;
  33. import com.gitblit.Keys;
  34. import com.gitblit.SparkleShareInviteServlet;
  35. import com.gitblit.models.RepositoryModel;
  36. import com.gitblit.models.UserModel;
  37. import com.gitblit.utils.StringUtils;
  38. import com.gitblit.wicket.GitBlitWebSession;
  39. import com.gitblit.wicket.WicketUtils;
  40. /**
  41. * Smart repository url panel which can display multiple Gitblit repository urls
  42. * and also supports 3rd party app clone links.
  43. *
  44. * @author James Moger
  45. *
  46. */
  47. public class RepositoryUrlPanel extends BasePanel {
  48. private static final long serialVersionUID = 1L;
  49. private final String primaryUrl;
  50. public RepositoryUrlPanel(String wicketId, boolean onlyPrimary, UserModel user,
  51. RepositoryModel repository, Localizer localizer, Component owner) {
  52. super(wicketId);
  53. if (user == null) {
  54. user = UserModel.ANONYMOUS;
  55. }
  56. List<String> repositoryUrls = new ArrayList<String>();
  57. AccessPermission accessPermission = null;
  58. if (GitBlit.getBoolean(Keys.git.enableGitServlet, true)) {
  59. accessPermission = user.getRepositoryPermission(repository).permission;
  60. repositoryUrls.add(getRepositoryUrl(repository));
  61. }
  62. repositoryUrls.addAll(GitBlit.self().getOtherCloneUrls(repository.name, UserModel.ANONYMOUS.equals(user) ? "" : user.username));
  63. primaryUrl = repositoryUrls.size() == 0 ? "" : repositoryUrls.remove(0);
  64. add(new DetailedRepositoryUrlPanel("repositoryPrimaryUrl", localizer, owner, repository.name, primaryUrl, accessPermission));
  65. if (!onlyPrimary) {
  66. Component gitDaemonUrlPanel = createGitDaemonUrlPanel("repositoryGitDaemonUrl", user, repository);
  67. if (!StringUtils.isEmpty(primaryUrl) && gitDaemonUrlPanel instanceof DetailedRepositoryUrlPanel) {
  68. WicketUtils.setCssStyle(gitDaemonUrlPanel, "padding-top: 10px");
  69. }
  70. add(gitDaemonUrlPanel);
  71. } else {
  72. add(new Label("repositoryGitDaemonUrl").setVisible(false));
  73. }
  74. final List<AppCloneLink> cloneLinks = new ArrayList<AppCloneLink>();
  75. if (user.canClone(repository) && GitBlit.getBoolean(Keys.web.allowAppCloneLinks, true)) {
  76. // universal app clone urls
  77. // cloneLinks.add(new AppCloneLink(localizer.getString("gb.cloneWithSmartGit", owner),
  78. // MessageFormat.format("smartgit://cloneRepo/{0}", primaryUrl),
  79. // "Syntevo SmartGit\u2122"));
  80. if (isWindows()) {
  81. // Windows client app clone urls
  82. cloneLinks.add(new AppCloneLink(localizer.getString("gb.cloneWithSourceTree", owner),
  83. MessageFormat.format("sourcetree://cloneRepo/{0}", primaryUrl),
  84. "Atlassian SourceTree\u2122"));
  85. // cloneLinks.add(new AppCloneLink(
  86. // MessageFormat.format(localizer.getString("gb.cloneWithGitHub", owner), "Windows"),
  87. // MessageFormat.format("github-windows://openRepo/{0}", primaryUrl)));
  88. } else if (isMac()) {
  89. // Mac client app clone urls
  90. cloneLinks.add(new AppCloneLink(localizer.getString("gb.cloneWithSourceTree", owner),
  91. MessageFormat.format("sourcetree://cloneRepo/{0}", primaryUrl),
  92. "Atlassian SourceTree\u2122"));
  93. // cloneLinks.add(new AppCloneLink(
  94. // MessageFormat.format(localizer.getString("gb.cloneWithGitHub", owner), "Mac"),
  95. // MessageFormat.format("github-mac://openRepo/{0}", primaryUrl)));
  96. }
  97. // sparkleshare invite url
  98. String sparkleshareUrl = getSparkleShareInviteUrl(user, repository);
  99. if (!StringUtils.isEmpty(sparkleshareUrl)) {
  100. cloneLinks.add(new AppCloneLink(localizer.getString("gb.cloneWithSparkleShare", owner),
  101. sparkleshareUrl, "SparkleShare \u2122", "icon-star"));
  102. }
  103. }
  104. // app clone links
  105. ListDataProvider<AppCloneLink> appLinks = new ListDataProvider<AppCloneLink>(cloneLinks);
  106. DataView<AppCloneLink> appCloneLinks = new DataView<AppCloneLink>("appCloneLink", appLinks) {
  107. private static final long serialVersionUID = 1L;
  108. int count;
  109. public void populateItem(final Item<AppCloneLink> item) {
  110. final AppCloneLink appLink = item.getModelObject();
  111. item.add(new Label("icon", MessageFormat.format("<i class=\"{0}\"></i>", appLink.icon)).setEscapeModelStrings(false));
  112. LinkPanel linkPanel = new LinkPanel("link", null, appLink.name, appLink.url);
  113. if (!StringUtils.isEmpty(appLink.tooltip)) {
  114. WicketUtils.setHtmlTooltip(linkPanel, appLink.tooltip);
  115. }
  116. item.add(linkPanel);
  117. item.add(new Label("separator", "|").setVisible(count < (cloneLinks.size() - 1)));
  118. count++;
  119. }
  120. };
  121. add(appCloneLinks);
  122. }
  123. public String getPrimaryUrl() {
  124. return primaryUrl;
  125. }
  126. protected String getRepositoryUrl(RepositoryModel repository) {
  127. StringBuilder sb = new StringBuilder();
  128. sb.append(WicketUtils.getGitblitURL(RequestCycle.get().getRequest()));
  129. sb.append(Constants.GIT_PATH);
  130. sb.append(repository.name);
  131. // inject username into repository url if authentication is required
  132. if (repository.accessRestriction.exceeds(AccessRestrictionType.NONE)
  133. && GitBlitWebSession.get().isLoggedIn()) {
  134. String username = GitBlitWebSession.get().getUsername();
  135. sb.insert(sb.indexOf("://") + 3, username + "@");
  136. }
  137. return sb.toString();
  138. }
  139. protected Component createGitDaemonUrlPanel(String wicketId, UserModel user, RepositoryModel repository) {
  140. int gitDaemonPort = GitBlit.getInteger(Keys.git.daemonPort, 0);
  141. if (gitDaemonPort > 0 && user.canClone(repository)) {
  142. String servername = ((WebRequest) getRequest()).getHttpServletRequest().getServerName();
  143. String gitDaemonUrl;
  144. if (gitDaemonPort == 9418) {
  145. // standard port
  146. gitDaemonUrl = MessageFormat.format("git://{0}/{1}", servername, repository.name);
  147. } else {
  148. // non-standard port
  149. gitDaemonUrl = MessageFormat.format("git://{0}:{1,number,0}/{2}", servername, gitDaemonPort, repository.name);
  150. }
  151. AccessPermission gitDaemonPermission = user.getRepositoryPermission(repository).permission;;
  152. if (gitDaemonPermission.atLeast(AccessPermission.CLONE)) {
  153. if (repository.accessRestriction.atLeast(AccessRestrictionType.CLONE)) {
  154. // can not authenticate clone via anonymous git protocol
  155. gitDaemonPermission = AccessPermission.NONE;
  156. } else if (repository.accessRestriction.atLeast(AccessRestrictionType.PUSH)) {
  157. // can not authenticate push via anonymous git protocol
  158. gitDaemonPermission = AccessPermission.CLONE;
  159. } else {
  160. // normal user permission
  161. }
  162. }
  163. if (AccessPermission.NONE.equals(gitDaemonPermission)) {
  164. // repository prohibits all anonymous access
  165. return new Label(wicketId).setVisible(false);
  166. } else {
  167. // repository allows some form of anonymous access
  168. return new DetailedRepositoryUrlPanel(wicketId, getLocalizer(), this, repository.name, gitDaemonUrl, gitDaemonPermission);
  169. }
  170. } else {
  171. // git daemon is not running
  172. return new Label(wicketId).setVisible(false);
  173. }
  174. }
  175. protected String getSparkleShareInviteUrl(UserModel user, RepositoryModel repository) {
  176. if (repository.isBare && repository.isSparkleshared()) {
  177. String username = null;
  178. if (UserModel.ANONYMOUS != user) {
  179. username = user.username;
  180. }
  181. if (GitBlit.getBoolean(Keys.git.enableGitServlet, true) || (GitBlit.getInteger(Keys.git.daemonPort, 0) > 0)) {
  182. // Gitblit as server
  183. // ensure user can rewind
  184. if (user.canRewindRef(repository)) {
  185. String baseURL = WicketUtils.getGitblitURL(RequestCycle.get().getRequest());
  186. return SparkleShareInviteServlet.asLink(baseURL, repository.name, username);
  187. }
  188. } else {
  189. // Gitblit as viewer, assume RW+ permission
  190. String baseURL = WicketUtils.getGitblitURL(RequestCycle.get().getRequest());
  191. return SparkleShareInviteServlet.asLink(baseURL, repository.name, username);
  192. }
  193. }
  194. return null;
  195. }
  196. static class AppCloneLink implements Serializable {
  197. private static final long serialVersionUID = 1L;
  198. final String name;
  199. final String url;
  200. final String tooltip;
  201. final String icon;
  202. public AppCloneLink(String name, String url, String tooltip) {
  203. this(name, url, tooltip, "icon-download");
  204. }
  205. public AppCloneLink(String name, String url, String tooltip, String icon) {
  206. this.name = name;
  207. this.url = url;
  208. this.tooltip = tooltip;
  209. this.icon = icon;
  210. }
  211. }
  212. }