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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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.FileInputStream;
  19. import java.io.InputStream;
  20. import java.io.InputStreamReader;
  21. import java.text.MessageFormat;
  22. import java.util.ArrayList;
  23. import java.util.List;
  24. import org.apache.wicket.Component;
  25. import org.apache.wicket.PageParameters;
  26. import org.apache.wicket.markup.html.basic.Label;
  27. import org.eclipse.jgit.lib.Constants;
  28. import com.gitblit.Keys;
  29. import com.gitblit.models.RepositoryModel;
  30. import com.gitblit.utils.MarkdownUtils;
  31. import com.gitblit.utils.StringUtils;
  32. import com.gitblit.wicket.CacheControl;
  33. import com.gitblit.wicket.CacheControl.LastModified;
  34. import com.gitblit.wicket.GitBlitWebSession;
  35. import com.gitblit.wicket.PageRegistration;
  36. import com.gitblit.wicket.PageRegistration.DropDownMenuItem;
  37. import com.gitblit.wicket.PageRegistration.DropDownMenuRegistration;
  38. import com.gitblit.wicket.WicketUtils;
  39. import com.gitblit.wicket.panels.RepositoriesPanel;
  40. @CacheControl(LastModified.ACTIVITY)
  41. public class RepositoriesPage extends RootPage {
  42. public RepositoriesPage() {
  43. super();
  44. setup(null);
  45. }
  46. public RepositoriesPage(PageParameters params) {
  47. super(params);
  48. setup(params);
  49. }
  50. @Override
  51. protected boolean reusePageParameters() {
  52. return true;
  53. }
  54. private void setup(PageParameters params) {
  55. setupPage("", "");
  56. // check to see if we should display a login message
  57. boolean authenticateView = app().settings().getBoolean(Keys.web.authenticateViewPages, true);
  58. if (authenticateView && !GitBlitWebSession.get().isLoggedIn()) {
  59. String messageSource = app().settings().getString(Keys.web.loginMessage, "gitblit");
  60. String message = readMarkdown(messageSource, "login.mkd");
  61. Component repositoriesMessage = new Label("repositoriesMessage", message);
  62. add(repositoriesMessage.setEscapeModelStrings(false));
  63. add(new Label("repositoriesPanel"));
  64. return;
  65. }
  66. // Load the markdown welcome message
  67. String messageSource = app().settings().getString(Keys.web.repositoriesMessage, "gitblit");
  68. String message = readMarkdown(messageSource, "welcome.mkd");
  69. Component repositoriesMessage = new Label("repositoriesMessage", message)
  70. .setEscapeModelStrings(false).setVisible(message.length() > 0);
  71. add(repositoriesMessage);
  72. List<RepositoryModel> repositories = getRepositories(params);
  73. RepositoriesPanel repositoriesPanel = new RepositoriesPanel("repositoriesPanel", showAdmin,
  74. true, repositories, true, getAccessRestrictions());
  75. // push the panel down if we are hiding the admin controls and the
  76. // welcome message
  77. if (!showAdmin && !repositoriesMessage.isVisible()) {
  78. WicketUtils.setCssStyle(repositoriesPanel, "padding-top:5px;");
  79. }
  80. add(repositoriesPanel);
  81. }
  82. @Override
  83. protected void addDropDownMenus(List<PageRegistration> pages) {
  84. PageParameters params = getPageParameters();
  85. DropDownMenuRegistration menu = new DropDownMenuRegistration("gb.filters",
  86. RepositoriesPage.class);
  87. // preserve time filter option on repository choices
  88. menu.menuItems.addAll(getRepositoryFilterItems(params));
  89. // preserve repository filter option on time choices
  90. menu.menuItems.addAll(getTimeFilterItems(params));
  91. if (menu.menuItems.size() > 0) {
  92. // Reset Filter
  93. menu.menuItems.add(new DropDownMenuItem(getString("gb.reset"), null, null));
  94. }
  95. pages.add(menu);
  96. }
  97. private String readMarkdown(String messageSource, String resource) {
  98. String message = "";
  99. if (messageSource.equalsIgnoreCase("gitblit")) {
  100. // Read default message
  101. message = readDefaultMarkdown(resource);
  102. } else {
  103. // Read user-supplied message
  104. if (!StringUtils.isEmpty(messageSource)) {
  105. File file = app().runtime().getFileOrFolder(messageSource);
  106. if (file.exists()) {
  107. try {
  108. FileInputStream fis = new FileInputStream(file);
  109. InputStreamReader reader = new InputStreamReader(fis,
  110. Constants.CHARACTER_ENCODING);
  111. message = MarkdownUtils.transformMarkdown(reader);
  112. reader.close();
  113. } catch (Throwable t) {
  114. message = getString("gb.failedToRead") + " " + file;
  115. warn(message, t);
  116. }
  117. } else {
  118. message = messageSource + " " + getString("gb.isNotValidFile");
  119. }
  120. }
  121. }
  122. return message;
  123. }
  124. private String readDefaultMarkdown(String file) {
  125. String base = file.substring(0, file.lastIndexOf('.'));
  126. String ext = file.substring(file.lastIndexOf('.'));
  127. String lc = getLanguageCode();
  128. String cc = getCountryCode();
  129. // try to read file_en-us.ext, file_en.ext, file.ext
  130. List<String> files = new ArrayList<String>();
  131. if (!StringUtils.isEmpty(lc)) {
  132. if (!StringUtils.isEmpty(cc)) {
  133. files.add(base + "_" + lc + "-" + cc + ext);
  134. files.add(base + "_" + lc + "_" + cc.toUpperCase() + ext);
  135. }
  136. files.add(base + "_" + lc + ext);
  137. }
  138. files.add(file);
  139. for (String name : files) {
  140. String message;
  141. InputStreamReader reader = null;
  142. try {
  143. InputStream is = getClass().getResourceAsStream("/" + name);
  144. if (is == null) {
  145. continue;
  146. }
  147. reader = new InputStreamReader(is, Constants.CHARACTER_ENCODING);
  148. message = MarkdownUtils.transformMarkdown(reader);
  149. reader.close();
  150. return message;
  151. } catch (Throwable t) {
  152. message = MessageFormat.format(getString("gb.failedToReadMessage"), file);
  153. error(message, t, false);
  154. return message;
  155. } finally {
  156. if (reader != null) {
  157. try {
  158. reader.close();
  159. } catch (Exception e) {
  160. }
  161. }
  162. }
  163. }
  164. return MessageFormat.format(getString("gb.failedToReadMessage"), file);
  165. }
  166. }