James Moger
2012-02-09 5cc40c89abafdcccbc8a5b2cf3890780ccff908e
commit | author | age
f13c4c 1 /*
JM 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  */
5fe7df 16 package com.gitblit.wicket.pages;
JM 17
c3f4f1 18 import java.io.File;
JM 19 import java.io.FileReader;
20 import java.io.InputStream;
21 import java.io.InputStreamReader;
8c5d72 22 import java.text.MessageFormat;
cb57ec 23 import java.util.List;
5fe7df 24
31bcbe 25 import org.apache.wicket.Application;
c3f4f1 26 import org.apache.wicket.Component;
cb57ec 27 import org.apache.wicket.PageParameters;
5fe7df 28 import org.apache.wicket.markup.html.basic.Label;
c3f4f1 29 import org.apache.wicket.resource.ContextRelativeResource;
5fe7df 30
fc948c 31 import com.gitblit.GitBlit;
155bf7 32 import com.gitblit.Keys;
cb57ec 33 import com.gitblit.models.RepositoryModel;
cf9550 34 import com.gitblit.utils.MarkdownUtils;
c3f4f1 35 import com.gitblit.utils.StringUtils;
8c5d72 36 import com.gitblit.wicket.GitBlitWebSession;
31bcbe 37 import com.gitblit.wicket.PageRegistration;
bc57cd 38 import com.gitblit.wicket.PageRegistration.DropDownMenuItem;
31bcbe 39 import com.gitblit.wicket.PageRegistration.DropDownMenuRegistration;
5fe7df 40 import com.gitblit.wicket.WicketUtils;
a4d249 41 import com.gitblit.wicket.panels.RepositoriesPanel;
5fe7df 42
a7571b 43 public class RepositoriesPage extends RootPage {
5fe7df 44
JM 45     public RepositoriesPage() {
94750e 46         super();
cb57ec 47         setup(null);
JM 48     }
dfb889 49
cb57ec 50     public RepositoriesPage(PageParameters params) {
JM 51         super(params);
52         setup(params);
bc57cd 53     }
JM 54
55     @Override
56     protected boolean reusePageParameters() {
57         return true;
cb57ec 58     }
JM 59
60     private void setup(PageParameters params) {
61         setupPage("", "");
8c5d72 62         // check to see if we should display a login message
JM 63         boolean authenticateView = GitBlit.getBoolean(Keys.web.authenticateViewPages, true);
64         if (authenticateView && !GitBlitWebSession.get().isLoggedIn()) {
65             String messageSource = GitBlit.getString(Keys.web.loginMessage, "gitblit");
66             String message = readMarkdown(messageSource, "login.mkd");
67             Component repositoriesMessage = new Label("repositoriesMessage", message);
68             add(repositoriesMessage.setEscapeModelStrings(false));
69             add(new Label("repositoriesPanel"));
70             return;
71         }
72
c3f4f1 73         // Load the markdown welcome message
2a7306 74         String messageSource = GitBlit.getString(Keys.web.repositoriesMessage, "gitblit");
8c5d72 75         String message = readMarkdown(messageSource, "welcome.mkd");
JM 76         Component repositoriesMessage = new Label("repositoriesMessage", message)
77                 .setEscapeModelStrings(false).setVisible(message.length() > 0);
78         add(repositoriesMessage);
cb57ec 79
JM 80         List<RepositoryModel> repositories = getRepositories(params);
81
82         RepositoriesPanel repositoriesPanel = new RepositoriesPanel("repositoriesPanel", showAdmin,
83                 repositories, true, getAccessRestrictions());
8c5d72 84         // push the panel down if we are hiding the admin controls and the
JM 85         // welcome message
86         if (!showAdmin && !repositoriesMessage.isVisible()) {
cb57ec 87             WicketUtils.setCssStyle(repositoriesPanel, "padding-top:5px;");
8c5d72 88         }
cb57ec 89         add(repositoriesPanel);
8c5d72 90     }
JM 91
31bcbe 92     @Override
JM 93     protected void addDropDownMenus(List<PageRegistration> pages) {
e4ffeb 94         PageParameters params = getPageParameters();
bc57cd 95
JM 96         DropDownMenuRegistration menu = new DropDownMenuRegistration("gb.filters",
97                 RepositoriesPage.class);
98         // preserve time filter option on repository choices
e4ffeb 99         menu.menuItems.addAll(getRepositoryFilterItems(params));
JM 100
bc57cd 101         // preserve repository filter option on time choices
e4ffeb 102         menu.menuItems.addAll(getTimeFilterItems(params));
JM 103
bc57cd 104         if (menu.menuItems.size() > 0) {
e4ffeb 105             // Reset Filter
bc57cd 106             menu.menuItems.add(new DropDownMenuItem(getString("gb.reset"), null, null));
JM 107         }
108
31bcbe 109         pages.add(menu);
JM 110     }
111
8c5d72 112     private String readMarkdown(String messageSource, String resource) {
94750e 113         String message = "";
c3f4f1 114         if (messageSource.equalsIgnoreCase("gitblit")) {
8c5d72 115             // Read default message
JM 116             message = readDefaultMarkdown(resource);
c3f4f1 117         } else {
8c5d72 118             // Read user-supplied message
c3f4f1 119             if (!StringUtils.isEmpty(messageSource)) {
JM 120                 File file = new File(messageSource);
121                 if (file.exists()) {
122                     try {
123                         FileReader reader = new FileReader(file);
cf9550 124                         message = MarkdownUtils.transformMarkdown(reader);
c3f4f1 125                     } catch (Throwable t) {
JM 126                         message = "Failed to read " + file;
5450d0 127                         warn(message, t);
c3f4f1 128                     }
JM 129                 } else {
130                     message = messageSource + " is not a valid file.";
131                 }
132             }
133         }
8c5d72 134         return message;
JM 135     }
136
137     private String readDefaultMarkdown(String file) {
138         String message;
139         try {
140             ContextRelativeResource res = WicketUtils.getResource(file);
141             InputStream is = res.getResourceStream().getInputStream();
142             InputStreamReader reader = new InputStreamReader(is);
143             message = MarkdownUtils.transformMarkdown(reader);
144             reader.close();
145         } catch (Throwable t) {
146             message = MessageFormat.format("Failed to read default message from {0}!", file);
147             error(message, t, false);
94750e 148         }
8c5d72 149         return message;
00afd7 150     }
bc57cd 151
31bcbe 152     @Override
JM 153     protected void onBeforeRender() {
154         if (GitBlit.isDebugMode()) {
155             // strip Wicket tags in debug mode for jQuery DOM traversal
156             Application.get().getMarkupSettings().setStripWicketTags(true);
157         }
158         super.onBeforeRender();
159     }
bc57cd 160
31bcbe 161     @Override
JM 162     protected void onAfterRender() {
163         if (GitBlit.isDebugMode()) {
164             // restore Wicket debug tags
165             Application.get().getMarkupSettings().setStripWicketTags(false);
166         }
167         super.onAfterRender();
168     }
5fe7df 169 }