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.

BlobPage.java 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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.pages;
  17. import java.text.MessageFormat;
  18. import java.util.HashMap;
  19. import java.util.Map;
  20. import org.apache.wicket.Component;
  21. import org.apache.wicket.PageParameters;
  22. import org.apache.wicket.RedirectException;
  23. import org.apache.wicket.markup.html.basic.Label;
  24. import org.apache.wicket.markup.html.image.Image;
  25. import org.apache.wicket.markup.html.link.BookmarkablePageLink;
  26. import org.apache.wicket.markup.html.link.ExternalLink;
  27. import org.eclipse.jgit.lib.Repository;
  28. import org.eclipse.jgit.revwalk.RevCommit;
  29. import com.gitblit.Keys;
  30. import com.gitblit.servlet.RawServlet;
  31. import com.gitblit.utils.JGitUtils;
  32. import com.gitblit.utils.StringUtils;
  33. import com.gitblit.wicket.CacheControl;
  34. import com.gitblit.wicket.CacheControl.LastModified;
  35. import com.gitblit.wicket.ExternalImage;
  36. import com.gitblit.wicket.MarkupProcessor;
  37. import com.gitblit.wicket.WicketUtils;
  38. import com.gitblit.wicket.panels.CommitHeaderPanel;
  39. import com.gitblit.wicket.panels.PathBreadcrumbsPanel;
  40. @CacheControl(LastModified.BOOT)
  41. public class BlobPage extends RepositoryPage {
  42. protected String fileExtension;
  43. public BlobPage(PageParameters params) {
  44. super(params);
  45. Repository r = getRepository();
  46. final String blobPath = WicketUtils.getPath(params);
  47. String [] encodings = getEncodings();
  48. if (StringUtils.isEmpty(objectId) && StringUtils.isEmpty(blobPath)) {
  49. throw new RedirectException(TreePage.class, WicketUtils.newRepositoryParameter(repositoryName));
  50. }
  51. if (StringUtils.isEmpty(blobPath)) {
  52. // blob by objectid
  53. add(new BookmarkablePageLink<Void>("blameLink", BlamePage.class,
  54. WicketUtils.newPathParameter(repositoryName, objectId, blobPath))
  55. .setEnabled(false));
  56. add(new BookmarkablePageLink<Void>("historyLink", HistoryPage.class).setEnabled(false));
  57. String rawUrl = RawServlet.asLink(getContextUrl(), repositoryName, objectId, blobPath);
  58. add(new ExternalLink("rawLink", rawUrl));
  59. add(new CommitHeaderPanel("commitHeader", objectId));
  60. add(new PathBreadcrumbsPanel("breadcrumbs", repositoryName, blobPath, objectId));
  61. Component c = new Label("blobText", JGitUtils.getStringContent(r, objectId, encodings));
  62. WicketUtils.setCssClass(c, "plainprint");
  63. add(c);
  64. } else {
  65. // standard blob view
  66. String extension = null;
  67. if (blobPath.lastIndexOf('.') > -1) {
  68. extension = blobPath.substring(blobPath.lastIndexOf('.') + 1).toLowerCase();
  69. }
  70. // see if we should redirect to the doc page
  71. MarkupProcessor processor = new MarkupProcessor(app().settings(), app().xssFilter());
  72. for (String ext : processor.getMarkupExtensions()) {
  73. if (ext.equals(extension)) {
  74. setResponsePage(DocPage.class, params);
  75. return;
  76. }
  77. }
  78. RevCommit commit = getCommit();
  79. // blob page links
  80. add(new BookmarkablePageLink<Void>("blameLink", BlamePage.class,
  81. WicketUtils.newPathParameter(repositoryName, objectId, blobPath)));
  82. add(new BookmarkablePageLink<Void>("historyLink", HistoryPage.class,
  83. WicketUtils.newPathParameter(repositoryName, objectId, blobPath)));
  84. String rawUrl = RawServlet.asLink(getContextUrl(), repositoryName, objectId, blobPath);
  85. add(new ExternalLink("rawLink", rawUrl));
  86. add(new CommitHeaderPanel("commitHeader", repositoryName, commit));
  87. add(new PathBreadcrumbsPanel("breadcrumbs", repositoryName, blobPath, objectId));
  88. // Map the extensions to types
  89. Map<String, Integer> map = new HashMap<String, Integer>();
  90. for (String ext : app().settings().getStrings(Keys.web.prettyPrintExtensions)) {
  91. map.put(ext.toLowerCase(), 1);
  92. }
  93. for (String ext : app().settings().getStrings(Keys.web.imageExtensions)) {
  94. map.put(ext.toLowerCase(), 2);
  95. }
  96. for (String ext : app().settings().getStrings(Keys.web.binaryExtensions)) {
  97. map.put(ext.toLowerCase(), 3);
  98. }
  99. if (extension != null) {
  100. int type = 0;
  101. if (map.containsKey(extension)) {
  102. type = map.get(extension);
  103. }
  104. switch (type) {
  105. case 2:
  106. // image blobs
  107. add(new Label("blobText").setVisible(false));
  108. add(new ExternalImage("blobImage", rawUrl));
  109. break;
  110. case 3:
  111. // binary blobs
  112. add(new Label("blobText", "Binary File"));
  113. add(new Image("blobImage").setVisible(false));
  114. break;
  115. default:
  116. // plain text
  117. String source = JGitUtils.getStringContent(r, commit.getTree(), blobPath, encodings);
  118. String table;
  119. if (source == null) {
  120. table = missingBlob(blobPath, commit);
  121. } else {
  122. table = generateSourceView(source, extension, type == 1);
  123. }
  124. add(new Label("blobText", table).setEscapeModelStrings(false));
  125. add(new Image("blobImage").setVisible(false));
  126. fileExtension = extension;
  127. }
  128. } else {
  129. // plain text
  130. String source = JGitUtils.getStringContent(r, commit.getTree(), blobPath, encodings);
  131. String table;
  132. if (source == null) {
  133. table = missingBlob(blobPath, commit);
  134. } else {
  135. table = generateSourceView(source, null, false);
  136. }
  137. add(new Label("blobText", table).setEscapeModelStrings(false));
  138. add(new Image("blobImage").setVisible(false));
  139. }
  140. }
  141. }
  142. protected String missingBlob(String blobPath, RevCommit commit) {
  143. StringBuilder sb = new StringBuilder();
  144. sb.append("<div class=\"alert alert-error\">");
  145. String pattern = getString("gb.doesNotExistInTree").replace("{0}", "<b>{0}</b>").replace("{1}", "<b>{1}</b>");
  146. sb.append(MessageFormat.format(pattern, blobPath, commit.getTree().getId().getName()));
  147. sb.append("</div>");
  148. return sb.toString();
  149. }
  150. protected String generateSourceView(String source, String extension, boolean prettyPrint) {
  151. String [] lines = source.split("\n");
  152. StringBuilder sb = new StringBuilder();
  153. sb.append("<!-- start blob table -->");
  154. sb.append("<table width=\"100%\"><tbody><tr>");
  155. // nums column
  156. sb.append("<!-- start nums column -->");
  157. sb.append("<td id=\"nums\">");
  158. sb.append("<pre>");
  159. String numPattern = "<span id=\"L{0}\" class=\"jump\"></span><a href=\"#L{0}\">{0}</a>\n";
  160. for (int i = 0; i < lines.length; i++) {
  161. sb.append(MessageFormat.format(numPattern, "" + (i + 1)));
  162. }
  163. sb.append("</pre>");
  164. sb.append("<!-- end nums column -->");
  165. sb.append("</td>");
  166. sb.append("<!-- start lines column -->");
  167. sb.append("<td id=\"lines\">");
  168. sb.append("<div class=\"sourceview\">");
  169. if (prettyPrint) {
  170. sb.append("<pre class=\"prettyprint lang-" + extension + "\">");
  171. } else {
  172. sb.append("<pre class=\"plainprint\">");
  173. }
  174. lines = StringUtils.escapeForHtml(source, true).split("\n");
  175. sb.append("<table width=\"100%\"><tbody>");
  176. String linePattern = "<tr class=\"{0}\"><td><div><span class=\"line\">{1}</span></div>\r</tr>";
  177. for (int i = 0; i < lines.length; i++) {
  178. String line = lines[i].replace('\r', ' ');
  179. String cssClass = (i % 2 == 0) ? "even" : "odd";
  180. if (StringUtils.isEmpty(line.trim())) {
  181. line = "&nbsp;";
  182. }
  183. sb.append(MessageFormat.format(linePattern, cssClass, line, "" + (i + 1)));
  184. }
  185. sb.append("</tbody></table></pre>");
  186. sb.append("</pre>");
  187. sb.append("</div>");
  188. sb.append("</td>");
  189. sb.append("<!-- end lines column -->");
  190. sb.append("</tr></tbody></table>");
  191. sb.append("<!-- end blob table -->");
  192. return sb.toString();
  193. }
  194. @Override
  195. protected String getPageName() {
  196. return getString("gb.view");
  197. }
  198. @Override
  199. protected Class<? extends BasePage> getRepoNavPageClass() {
  200. return TreePage.class;
  201. }
  202. }