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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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.html.image.ContextImage;
  26. import org.apache.wicket.markup.html.panel.Fragment;
  27. import org.apache.wicket.markup.repeater.Item;
  28. import org.apache.wicket.markup.repeater.data.DataView;
  29. import org.apache.wicket.markup.repeater.data.ListDataProvider;
  30. import org.apache.wicket.protocol.http.WebRequest;
  31. import org.apache.wicket.protocol.http.request.WebClientInfo;
  32. import com.gitblit.Constants;
  33. import com.gitblit.Constants.AccessPermission;
  34. import com.gitblit.Constants.AccessRestrictionType;
  35. import com.gitblit.GitBlit;
  36. import com.gitblit.Keys;
  37. import com.gitblit.SparkleShareInviteServlet;
  38. import com.gitblit.models.GitClientApplication;
  39. import com.gitblit.models.RepositoryModel;
  40. import com.gitblit.models.UserModel;
  41. import com.gitblit.utils.StringUtils;
  42. import com.gitblit.wicket.GitBlitWebSession;
  43. import com.gitblit.wicket.WicketUtils;
  44. /**
  45. * Smart repository url panel which can display multiple Gitblit repository urls
  46. * and also supports 3rd party app clone links.
  47. *
  48. * @author James Moger
  49. *
  50. */
  51. public class RepositoryUrlPanel extends BasePanel {
  52. private static final long serialVersionUID = 1L;
  53. private final RepoUrl primaryUrl;
  54. public RepositoryUrlPanel(String wicketId, boolean onlyPrimary, UserModel user,
  55. final RepositoryModel repository, Localizer localizer, Component owner) {
  56. super(wicketId);
  57. if (user == null) {
  58. user = UserModel.ANONYMOUS;
  59. }
  60. List<RepoUrl> repositoryUrls = new ArrayList<RepoUrl>();
  61. // http/https url
  62. if (GitBlit.getBoolean(Keys.git.enableGitServlet, true)) {
  63. AccessPermission permission = user.getRepositoryPermission(repository).permission;
  64. if (permission.exceeds(AccessPermission.NONE)) {
  65. repositoryUrls.add(new RepoUrl(getRepositoryUrl(repository), permission));
  66. }
  67. }
  68. // git daemon url
  69. String gitDaemonUrl = getGitDaemonUrl(user, repository);
  70. if (!StringUtils.isEmpty(gitDaemonUrl)) {
  71. AccessPermission permission = getGitDaemonAccessPermission(user, repository);
  72. if (permission.exceeds(AccessPermission.NONE)) {
  73. repositoryUrls.add(new RepoUrl(gitDaemonUrl, permission));
  74. }
  75. }
  76. // add all other urls
  77. for (String url : GitBlit.self().getOtherCloneUrls(repository.name, UserModel.ANONYMOUS.equals(user) ? "" : user.username)) {
  78. repositoryUrls.add(new RepoUrl(url, null));
  79. }
  80. // grab primary url from the top of the list
  81. primaryUrl = repositoryUrls.size() == 0 ? null : repositoryUrls.get(0);
  82. add(new DetailedRepositoryUrlPanel("repositoryPrimaryUrl", localizer, owner,
  83. repository.name, primaryUrl == null ? "" : primaryUrl.url,
  84. primaryUrl == null ? null : primaryUrl.permission));
  85. if (onlyPrimary) {
  86. // only displaying the primary url
  87. add(new Label("urlMenus").setVisible(false));
  88. return;
  89. }
  90. final String clonePattern = localizer.getString("gb.cloneUrl", owner);
  91. final String visitSitePattern = localizer.getString("gb.visitSite", owner);
  92. GitClientApplication URLS = new GitClientApplication();
  93. URLS.name = "URLs";
  94. URLS.command = "{0}";
  95. URLS.attribution = "Repository URLs";
  96. URLS.isApplication = false;
  97. URLS.isActive = true;
  98. GitClientApplication GIT = new GitClientApplication();
  99. GIT.name = "Git";
  100. GIT.command = "git clone {0}";
  101. GIT.productUrl = "http://git-scm.org";
  102. GIT.attribution = "Git Syntax";
  103. GIT.isApplication = false;
  104. GIT.isActive = true;
  105. final List<GitClientApplication> clientApps = new ArrayList<GitClientApplication>();
  106. clientApps.add(URLS);
  107. clientApps.add(GIT);
  108. final String userAgent = ((WebClientInfo) GitBlitWebSession.get().getClientInfo()).getUserAgent();
  109. boolean allowAppLinks = GitBlit.getBoolean(Keys.web.allowAppCloneLinks, true);
  110. if (user.canClone(repository)) {
  111. for (GitClientApplication app : GitBlit.self().getClientApplications()) {
  112. if (app.isActive && app.allowsPlatform(userAgent) && (!app.isApplication || (app.isApplication && allowAppLinks))) {
  113. clientApps.add(app);
  114. }
  115. }
  116. // sparkleshare invite url
  117. String sparkleshareUrl = getSparkleShareInviteUrl(user, repository);
  118. if (!StringUtils.isEmpty(sparkleshareUrl) && allowAppLinks) {
  119. GitClientApplication link = new GitClientApplication();
  120. link.name = "SparkleShare";
  121. link.cloneUrl = sparkleshareUrl;
  122. link.attribution = "SparkleShare\u2122";
  123. link.platforms = new String [] { "windows", "macintosh", "linux" };
  124. link.productUrl = "http://sparkleshare.org";
  125. link.isApplication = true;
  126. link.isActive = true;
  127. clientApps.add(link);
  128. }
  129. }
  130. final ListDataProvider<RepoUrl> repoUrls = new ListDataProvider<RepoUrl>(repositoryUrls);
  131. // app clone links
  132. ListDataProvider<GitClientApplication> appLinks = new ListDataProvider<GitClientApplication>(clientApps);
  133. DataView<GitClientApplication> urlMenus = new DataView<GitClientApplication>("urlMenus", appLinks) {
  134. private static final long serialVersionUID = 1L;
  135. public void populateItem(final Item<GitClientApplication> item) {
  136. final GitClientApplication cloneLink = item.getModelObject();
  137. item.add(new Label("productName", cloneLink.name));
  138. // a nested repeater for all repo links
  139. DataView<RepoUrl> repoLinks = new DataView<RepoUrl>("repoLinks", repoUrls) {
  140. private static final long serialVersionUID = 1L;
  141. public void populateItem(final Item<RepoUrl> repoLinkItem) {
  142. RepoUrl repoUrl = repoLinkItem.getModelObject();
  143. if (!StringUtils.isEmpty(cloneLink.cloneUrl)) {
  144. // custom registered url
  145. Fragment fragment = new Fragment("repoLink", "linkFragment", this);
  146. String name;
  147. if (repoUrl.permission != null) {
  148. name = MessageFormat.format("{0} ({1})", repoUrl.url, repoUrl.permission);
  149. } else {
  150. name = repoUrl.url;
  151. }
  152. String url = MessageFormat.format(cloneLink.cloneUrl, repoUrl);
  153. fragment.add(new LinkPanel("content", null, MessageFormat.format(clonePattern, name), url));
  154. repoLinkItem.add(fragment);
  155. String tooltip = getProtocolPermissionDescription(repository, repoUrl);
  156. WicketUtils.setHtmlTooltip(fragment, tooltip);
  157. } else if (!StringUtils.isEmpty(cloneLink.command)) {
  158. // command-line
  159. Fragment fragment = new Fragment("repoLink", "commandFragment", this);
  160. WicketUtils.setCssClass(fragment, "repositoryUrlMenuItem");
  161. String command = MessageFormat.format(cloneLink.command, repoUrl);
  162. fragment.add(new Label("content", command));
  163. repoLinkItem.add(fragment);
  164. String tooltip = getProtocolPermissionDescription(repository, repoUrl);
  165. WicketUtils.setHtmlTooltip(fragment, tooltip);
  166. // copy function for command
  167. if (GitBlit.getBoolean(Keys.web.allowFlashCopyToClipboard, true)) {
  168. // clippy: flash-based copy & paste
  169. Fragment copyFragment = new Fragment("copyFunction", "clippyPanel", this);
  170. String baseUrl = WicketUtils.getGitblitURL(getRequest());
  171. ShockWaveComponent clippy = new ShockWaveComponent("clippy", baseUrl + "/clippy.swf");
  172. clippy.setValue("flashVars", "text=" + StringUtils.encodeURL(command));
  173. copyFragment.add(clippy);
  174. fragment.add(copyFragment);
  175. } else {
  176. // javascript: manual copy & paste with modal browser prompt dialog
  177. Fragment copyFragment = new Fragment("copyFunction", "jsPanel", this);
  178. ContextImage img = WicketUtils.newImage("copyIcon", "clippy.png");
  179. img.add(new JavascriptTextPrompt("onclick", "Copy to Clipboard (Ctrl+C, Enter)", command));
  180. copyFragment.add(img);
  181. fragment.add(copyFragment);
  182. }
  183. }
  184. }};
  185. item.add(repoLinks);
  186. item.add(new Label("productAttribution", cloneLink.attribution));
  187. if (!StringUtils.isEmpty(cloneLink.productUrl)) {
  188. LinkPanel productlinkPanel = new LinkPanel("productLink", null,
  189. MessageFormat.format(visitSitePattern, cloneLink.name), cloneLink.productUrl, true);
  190. item.add(productlinkPanel);
  191. } else {
  192. item.add(new Label("productLink").setVisible(false));
  193. }
  194. }
  195. };
  196. add(urlMenus);
  197. }
  198. public String getPrimaryUrl() {
  199. return primaryUrl == null ? "" : primaryUrl.url;
  200. }
  201. protected String getRepositoryUrl(RepositoryModel repository) {
  202. StringBuilder sb = new StringBuilder();
  203. sb.append(WicketUtils.getGitblitURL(RequestCycle.get().getRequest()));
  204. sb.append(Constants.GIT_PATH);
  205. sb.append(repository.name);
  206. // inject username into repository url if authentication is required
  207. if (repository.accessRestriction.exceeds(AccessRestrictionType.NONE)
  208. && GitBlitWebSession.get().isLoggedIn()) {
  209. String username = GitBlitWebSession.get().getUsername();
  210. sb.insert(sb.indexOf("://") + 3, username + "@");
  211. }
  212. return sb.toString();
  213. }
  214. protected String getGitDaemonUrl(UserModel user, RepositoryModel repository) {
  215. int gitDaemonPort = GitBlit.getInteger(Keys.git.daemonPort, 0);
  216. if (gitDaemonPort > 0 && user.canClone(repository)) {
  217. String servername = ((WebRequest) getRequest()).getHttpServletRequest().getServerName();
  218. String gitDaemonUrl;
  219. if (gitDaemonPort == 9418) {
  220. // standard port
  221. gitDaemonUrl = MessageFormat.format("git://{0}/{1}", servername, repository.name);
  222. } else {
  223. // non-standard port
  224. gitDaemonUrl = MessageFormat.format("git://{0}:{1,number,0}/{2}", servername, gitDaemonPort, repository.name);
  225. }
  226. return gitDaemonUrl;
  227. }
  228. return null;
  229. }
  230. protected AccessPermission getGitDaemonAccessPermission(UserModel user, RepositoryModel repository) {
  231. int gitDaemonPort = GitBlit.getInteger(Keys.git.daemonPort, 0);
  232. if (gitDaemonPort > 0 && user.canClone(repository)) {
  233. AccessPermission gitDaemonPermission = user.getRepositoryPermission(repository).permission;;
  234. if (gitDaemonPermission.atLeast(AccessPermission.CLONE)) {
  235. if (repository.accessRestriction.atLeast(AccessRestrictionType.CLONE)) {
  236. // can not authenticate clone via anonymous git protocol
  237. gitDaemonPermission = AccessPermission.NONE;
  238. } else if (repository.accessRestriction.atLeast(AccessRestrictionType.PUSH)) {
  239. // can not authenticate push via anonymous git protocol
  240. gitDaemonPermission = AccessPermission.CLONE;
  241. } else {
  242. // normal user permission
  243. }
  244. }
  245. return gitDaemonPermission;
  246. }
  247. return AccessPermission.NONE;
  248. }
  249. protected String getSparkleShareInviteUrl(UserModel user, RepositoryModel repository) {
  250. if (repository.isBare && repository.isSparkleshared()) {
  251. String username = null;
  252. if (UserModel.ANONYMOUS != user) {
  253. username = user.username;
  254. }
  255. if (GitBlit.getBoolean(Keys.git.enableGitServlet, true) || (GitBlit.getInteger(Keys.git.daemonPort, 0) > 0)) {
  256. // Gitblit as server
  257. // ensure user can rewind
  258. if (user.canRewindRef(repository)) {
  259. String baseURL = WicketUtils.getGitblitURL(RequestCycle.get().getRequest());
  260. return SparkleShareInviteServlet.asLink(baseURL, repository.name, username);
  261. }
  262. } else {
  263. // Gitblit as viewer, assume RW+ permission
  264. String baseURL = WicketUtils.getGitblitURL(RequestCycle.get().getRequest());
  265. return SparkleShareInviteServlet.asLink(baseURL, repository.name, username);
  266. }
  267. }
  268. return null;
  269. }
  270. protected String getProtocolPermissionDescription(RepositoryModel repository, RepoUrl repoUrl) {
  271. String protocol = repoUrl.url.substring(0, repoUrl.url.indexOf("://"));
  272. String note;
  273. if (repoUrl.permission == null) {
  274. note = MessageFormat.format(getString("gb.externalPermissions"), protocol, repository.name);
  275. } else {
  276. note = null;
  277. String key;
  278. switch (repoUrl.permission) {
  279. case OWNER:
  280. case REWIND:
  281. key = "gb.rewindPermission";
  282. break;
  283. case DELETE:
  284. key = "gb.deletePermission";
  285. break;
  286. case CREATE:
  287. key = "gb.createPermission";
  288. break;
  289. case PUSH:
  290. key = "gb.pushPermission";
  291. break;
  292. case CLONE:
  293. key = "gb.clonePermission";
  294. break;
  295. default:
  296. key = null;
  297. note = getString("gb.viewAccess");
  298. break;
  299. }
  300. if (note == null) {
  301. String pattern = getString(key);
  302. String description = MessageFormat.format(pattern, repoUrl.permission.toString());
  303. String permissionPattern = getString("gb.yourProtocolPermissionIs");
  304. note = MessageFormat.format(permissionPattern, protocol.toUpperCase(), repository, description);
  305. }
  306. }
  307. return note;
  308. }
  309. private class RepoUrl implements Serializable {
  310. private static final long serialVersionUID = 1L;
  311. final String url;
  312. final AccessPermission permission;
  313. RepoUrl(String url, AccessPermission permission) {
  314. this.url = url;
  315. this.permission = permission;
  316. }
  317. @Override
  318. public String toString() {
  319. return url;
  320. }
  321. }
  322. }