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.

RawPage.java 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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.util.HashMap;
  18. import java.util.Map;
  19. import org.apache.wicket.Component;
  20. import org.apache.wicket.PageParameters;
  21. import org.apache.wicket.markup.html.WebPage;
  22. import org.apache.wicket.markup.html.basic.Label;
  23. import org.eclipse.jgit.lib.Repository;
  24. import org.eclipse.jgit.revwalk.RevCommit;
  25. import com.gitblit.GitBlit;
  26. import com.gitblit.Keys;
  27. import com.gitblit.utils.JGitUtils;
  28. import com.gitblit.wicket.WicketUtils;
  29. public class RawPage extends WebPage {
  30. public RawPage(PageParameters params) {
  31. super(params);
  32. if (!params.containsKey("r")) {
  33. error("Repository not specified!");
  34. redirectToInterceptPage(new RepositoriesPage());
  35. }
  36. final String repositoryName = WicketUtils.getRepositoryName(params);
  37. final String objectId = WicketUtils.getObject(params);
  38. final String blobPath = WicketUtils.getPath(params);
  39. Repository r = GitBlit.self().getRepository(repositoryName);
  40. if (r == null) {
  41. error("Can not load repository " + repositoryName);
  42. redirectToInterceptPage(new RepositoriesPage());
  43. return;
  44. }
  45. RevCommit commit = JGitUtils.getCommit(r, objectId);
  46. String extension = null;
  47. if (blobPath.lastIndexOf('.') > -1) {
  48. extension = blobPath.substring(blobPath.lastIndexOf('.') + 1);
  49. }
  50. // Map the extensions to types
  51. Map<String, Integer> map = new HashMap<String, Integer>();
  52. for (String ext : GitBlit.self().settings().getStrings(Keys.web.imageExtensions)) {
  53. map.put(ext.toLowerCase(), 2);
  54. }
  55. for (String ext : GitBlit.self().settings().getStrings(Keys.web.binaryExtensions)) {
  56. map.put(ext.toLowerCase(), 3);
  57. }
  58. if (extension != null) {
  59. int type = 0;
  60. if (map.containsKey(extension)) {
  61. type = map.get(extension);
  62. }
  63. Component c = null;
  64. switch (type) {
  65. case 2:
  66. // TODO image blobs
  67. c = new Label("rawText", "Image File");
  68. break;
  69. case 3:
  70. // TODO binary blobs
  71. c = new Label("rawText", "Binary File");
  72. break;
  73. default:
  74. // plain text
  75. c = new Label("rawText", JGitUtils.getRawContentAsString(r, commit, blobPath));
  76. WicketUtils.setCssClass(c, "plainprint");
  77. }
  78. add(c);
  79. } else {
  80. // plain text
  81. Label blobLabel = new Label("rawText", JGitUtils.getRawContentAsString(r, commit, blobPath));
  82. WicketUtils.setCssClass(blobLabel, "plainprint");
  83. add(blobLabel);
  84. }
  85. r.close();
  86. }
  87. }