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.

RepositoriesPage.java 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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.io.File;
  18. import java.io.FileReader;
  19. import java.io.InputStream;
  20. import java.io.InputStreamReader;
  21. import java.text.MessageFormat;
  22. import org.apache.wicket.Component;
  23. import org.apache.wicket.markup.html.basic.Label;
  24. import org.apache.wicket.resource.ContextRelativeResource;
  25. import com.gitblit.GitBlit;
  26. import com.gitblit.Keys;
  27. import com.gitblit.utils.MarkdownUtils;
  28. import com.gitblit.utils.StringUtils;
  29. import com.gitblit.wicket.GitBlitWebSession;
  30. import com.gitblit.wicket.WicketUtils;
  31. import com.gitblit.wicket.panels.RepositoriesPanel;
  32. public class RepositoriesPage extends RootPage {
  33. public RepositoriesPage() {
  34. super();
  35. setupPage("", "");
  36. // check to see if we should display a login message
  37. boolean authenticateView = GitBlit.getBoolean(Keys.web.authenticateViewPages, true);
  38. if (authenticateView && !GitBlitWebSession.get().isLoggedIn()) {
  39. String messageSource = GitBlit.getString(Keys.web.loginMessage, "gitblit");
  40. String message = readMarkdown(messageSource, "login.mkd");
  41. Component repositoriesMessage = new Label("repositoriesMessage", message);
  42. add(repositoriesMessage.setEscapeModelStrings(false));
  43. add(new Label("repositoriesPanel"));
  44. return;
  45. }
  46. // Load the markdown welcome message
  47. String messageSource = GitBlit.getString(Keys.web.repositoriesMessage, "gitblit");
  48. String message = readMarkdown(messageSource, "welcome.mkd");
  49. Component repositoriesMessage = new Label("repositoriesMessage", message)
  50. .setEscapeModelStrings(false).setVisible(message.length() > 0);
  51. add(repositoriesMessage);
  52. RepositoriesPanel repositories = new RepositoriesPanel("repositoriesPanel", showAdmin,
  53. null, getAccessRestrictions());
  54. // push the panel down if we are hiding the admin controls and the
  55. // welcome message
  56. if (!showAdmin && !repositoriesMessage.isVisible()) {
  57. WicketUtils.setCssStyle(repositories, "padding-top:5px;");
  58. }
  59. add(repositories);
  60. }
  61. private String readMarkdown(String messageSource, String resource) {
  62. String message = "";
  63. if (messageSource.equalsIgnoreCase("gitblit")) {
  64. // Read default message
  65. message = readDefaultMarkdown(resource);
  66. } else {
  67. // Read user-supplied message
  68. if (!StringUtils.isEmpty(messageSource)) {
  69. File file = new File(messageSource);
  70. if (file.exists()) {
  71. try {
  72. FileReader reader = new FileReader(file);
  73. message = MarkdownUtils.transformMarkdown(reader);
  74. } catch (Throwable t) {
  75. message = "Failed to read " + file;
  76. warn(message, t);
  77. }
  78. } else {
  79. message = messageSource + " is not a valid file.";
  80. }
  81. }
  82. }
  83. return message;
  84. }
  85. private String readDefaultMarkdown(String file) {
  86. String message;
  87. try {
  88. ContextRelativeResource res = WicketUtils.getResource(file);
  89. InputStream is = res.getResourceStream().getInputStream();
  90. InputStreamReader reader = new InputStreamReader(is);
  91. message = MarkdownUtils.transformMarkdown(reader);
  92. reader.close();
  93. } catch (Throwable t) {
  94. message = MessageFormat.format("Failed to read default message from {0}!", file);
  95. error(message, t, false);
  96. }
  97. return message;
  98. }
  99. }