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