Paul Martin
2016-03-28 33f3580f8f4499ac9cd4812872c94fcdd81a5829
commit | author | age
dc7c2f 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  */
16 package com.gitblit.wicket.pages;
17
18 import java.util.List;
19
20 import org.apache.wicket.PageParameters;
21 import org.apache.wicket.markup.html.basic.Label;
22 import org.apache.wicket.markup.html.link.BookmarkablePageLink;
ff17f7 23 import org.apache.wicket.markup.html.link.ExternalLink;
8a71a1 24 import org.apache.wicket.markup.html.panel.Fragment;
dc7c2f 25 import org.eclipse.jgit.lib.Repository;
JM 26 import org.eclipse.jgit.revwalk.RevCommit;
27
713d27 28 import com.gitblit.models.UserModel;
ff17f7 29 import com.gitblit.servlet.RawServlet;
fef234 30 import com.gitblit.utils.BugtraqProcessor;
dc7c2f 31 import com.gitblit.utils.JGitUtils;
JM 32 import com.gitblit.utils.StringUtils;
33 import com.gitblit.wicket.CacheControl;
713d27 34 import com.gitblit.wicket.GitBlitWebSession;
dc7c2f 35 import com.gitblit.wicket.CacheControl.LastModified;
JM 36 import com.gitblit.wicket.MarkupProcessor;
37 import com.gitblit.wicket.MarkupProcessor.MarkupDocument;
8a71a1 38 import com.gitblit.wicket.MarkupProcessor.MarkupSyntax;
dc7c2f 39 import com.gitblit.wicket.WicketUtils;
JM 40
41 @CacheControl(LastModified.BOOT)
42 public class DocPage extends RepositoryPage {
43
44     public DocPage(PageParameters params) {
45         super(params);
46
47         final String path = WicketUtils.getPath(params).replace("%2f", "/").replace("%2F", "/");
11a173 48         MarkupProcessor processor = new MarkupProcessor(app().settings(), app().xssFilter());
713d27 49         UserModel currentUser = (GitBlitWebSession.get().getUser() != null) ? GitBlitWebSession.get().getUser() : UserModel.ANONYMOUS;
PM 50         final boolean userCanEdit = currentUser.canEdit(getRepositoryModel());
51         
dc7c2f 52         Repository r = getRepository();
JM 53         RevCommit commit = JGitUtils.getCommit(r, objectId);
99d0d4 54         String [] encodings = getEncodings();
dc7c2f 55
JM 56         // Read raw markup content and transform it to html
57         String documentPath = path;
58         String markupText = JGitUtils.getStringContent(r, commit.getTree(), path, encodings);
59
60         // Hunt for document
61         if (StringUtils.isEmpty(markupText)) {
62             String name = StringUtils.stripFileExtension(path);
63
e13171 64             List<String> docExtensions = processor.getAllExtensions();
dc7c2f 65             for (String ext : docExtensions) {
JM 66                 String checkName = name + "." + ext;
67                 markupText = JGitUtils.getStringContent(r, commit.getTree(), checkName, encodings);
68                 if (!StringUtils.isEmpty(markupText)) {
69                     // found it
70                     documentPath = path;
71                     break;
72                 }
73             }
74         }
75
b635f4 76         if (markupText == null) {
JM 77             markupText = "";
78         }
79
fef234 80         BugtraqProcessor bugtraq = new BugtraqProcessor(app().settings());
JM 81         markupText = bugtraq.processText(getRepository(), repositoryName, markupText);
82
8a71a1 83         Fragment fragment;
JM 84         MarkupDocument markupDoc = processor.parse(repositoryName, getBestCommitId(commit), documentPath, markupText);
85         if (MarkupSyntax.PLAIN.equals(markupDoc.syntax)) {
86             fragment = new Fragment("doc", "plainContent", this);
87         } else {
88             fragment = new Fragment("doc", "markupContent", this);
89         }
90
dc7c2f 91         // document page links
275496 92         fragment.add(new BookmarkablePageLink<Void>("editLink", EditFilePage.class,
713d27 93                 WicketUtils.newPathParameter(repositoryName, objectId, documentPath))
PM 94                 .setEnabled(userCanEdit));
8a71a1 95         fragment.add(new BookmarkablePageLink<Void>("blameLink", BlamePage.class,
dc7c2f 96                 WicketUtils.newPathParameter(repositoryName, objectId, documentPath)));
8a71a1 97         fragment.add(new BookmarkablePageLink<Void>("historyLink", HistoryPage.class,
dc7c2f 98                 WicketUtils.newPathParameter(repositoryName, objectId, documentPath)));
ff17f7 99         String rawUrl = RawServlet.asLink(getContextUrl(), repositoryName, objectId, documentPath);
JM 100         fragment.add(new ExternalLink("rawLink", rawUrl));
dc7c2f 101
8a71a1 102         fragment.add(new Label("content", markupDoc.html).setEscapeModelStrings(false));
JM 103         add(fragment);
dc7c2f 104     }
JM 105
106     @Override
107     protected String getPageName() {
108         return getString("gb.docs");
109     }
110
111     @Override
5d5e55 112     protected boolean isCommitPage() {
JM 113         return true;
114     }
115
116     @Override
dc7c2f 117     protected Class<? extends BasePage> getRepoNavPageClass() {
JM 118         return DocsPage.class;
119     }
120 }