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
18 import java.util.HashMap;
19 import java.util.Map;
20
21 import org.apache.wicket.Component;
22 import org.apache.wicket.PageParameters;
23 import org.apache.wicket.markup.html.basic.Label;
e4f49a 24 import org.apache.wicket.markup.html.image.Image;
608ece 25 import org.apache.wicket.markup.html.link.BookmarkablePageLink;
JM 26 import org.eclipse.jgit.lib.Constants;
5fe7df 27 import org.eclipse.jgit.lib.Repository;
JM 28 import org.eclipse.jgit.revwalk.RevCommit;
29
87cc1e 30 import com.gitblit.GitBlit;
155bf7 31 import com.gitblit.Keys;
5fe7df 32 import com.gitblit.utils.JGitUtils;
4ab184 33 import com.gitblit.utils.StringUtils;
e4f49a 34 import com.gitblit.wicket.ExternalImage;
5fe7df 35 import com.gitblit.wicket.WicketUtils;
c1c3c6 36 import com.gitblit.wicket.panels.CommitHeaderPanel;
5fe7df 37 import com.gitblit.wicket.panels.PathBreadcrumbsPanel;
JM 38
39 public class BlobPage extends RepositoryPage {
40
41     public BlobPage(PageParameters params) {
cebf45 42         super(params);
5fe7df 43
4ab184 44         Repository r = getRepository();
7d35e2 45         final String blobPath = WicketUtils.getPath(params);
ae9e15 46         String [] encodings = GitBlit.getEncodings();
JM 47         
4ab184 48         if (StringUtils.isEmpty(blobPath)) {
JM 49             // blob by objectid
2a7306 50
716745 51             add(new BookmarkablePageLink<Void>("blameLink", BlamePage.class,
008322 52                     WicketUtils.newPathParameter(repositoryName, objectId, blobPath))
JM 53                     .setEnabled(false));
4ab184 54             add(new BookmarkablePageLink<Void>("historyLink", HistoryPage.class).setEnabled(false));
JM 55             add(new BookmarkablePageLink<Void>("rawLink", RawPage.class,
56                     WicketUtils.newPathParameter(repositoryName, objectId, blobPath)));
57             add(new BookmarkablePageLink<Void>("headLink", BlobPage.class).setEnabled(false));
58             add(new CommitHeaderPanel("commitHeader", objectId));
59             add(new PathBreadcrumbsPanel("breadcrumbs", repositoryName, blobPath, objectId));
ae9e15 60             Component c = new Label("blobText", JGitUtils.getStringContent(r, objectId, encodings));
4ab184 61             WicketUtils.setCssClass(c, "plainprint");
5fe7df 62             add(c);
JM 63         } else {
4ab184 64             // standard blob view
JM 65             String extension = null;
66             if (blobPath.lastIndexOf('.') > -1) {
67                 extension = blobPath.substring(blobPath.lastIndexOf('.') + 1).toLowerCase();
68             }
69
70             // see if we should redirect to the markdown page
71             for (String ext : GitBlit.getStrings(Keys.web.markdownExtensions)) {
72                 if (ext.equals(extension)) {
73                     setResponsePage(MarkdownPage.class, params);
74                     return;
75                 }
76             }
77
78             // manually get commit because it can be null
79             RevCommit commit = JGitUtils.getCommit(r, objectId);
80
81             // blob page links
716745 82             add(new BookmarkablePageLink<Void>("blameLink", BlamePage.class,
JM 83                     WicketUtils.newPathParameter(repositoryName, objectId, blobPath)));
4ab184 84             add(new BookmarkablePageLink<Void>("historyLink", HistoryPage.class,
JM 85                     WicketUtils.newPathParameter(repositoryName, objectId, blobPath)));
86             add(new BookmarkablePageLink<Void>("rawLink", RawPage.class,
87                     WicketUtils.newPathParameter(repositoryName, objectId, blobPath)));
88             add(new BookmarkablePageLink<Void>("headLink", BlobPage.class,
89                     WicketUtils.newPathParameter(repositoryName, Constants.HEAD, blobPath)));
90
91             add(new CommitHeaderPanel("commitHeader", repositoryName, commit));
92
93             add(new PathBreadcrumbsPanel("breadcrumbs", repositoryName, blobPath, objectId));
94
95             // Map the extensions to types
96             Map<String, Integer> map = new HashMap<String, Integer>();
97             for (String ext : GitBlit.getStrings(Keys.web.prettyPrintExtensions)) {
98                 map.put(ext.toLowerCase(), 1);
99             }
100             for (String ext : GitBlit.getStrings(Keys.web.imageExtensions)) {
101                 map.put(ext.toLowerCase(), 2);
102             }
103             for (String ext : GitBlit.getStrings(Keys.web.binaryExtensions)) {
104                 map.put(ext.toLowerCase(), 3);
105             }
106
107             if (extension != null) {
108                 int type = 0;
109                 if (map.containsKey(extension)) {
110                     type = map.get(extension);
111                 }
112                 Component c = null;
e4f49a 113                 Component i = null;
4ab184 114                 switch (type) {
JM 115                 case 1:
116                     // PrettyPrint blob text
117                     c = new Label("blobText", JGitUtils.getStringContent(r, commit.getTree(),
ae9e15 118                             blobPath, encodings));
4ab184 119                     WicketUtils.setCssClass(c, "prettyprint linenums");
e4f49a 120                     i = new Image("blobImage").setVisible(false);
4ab184 121                     break;
JM 122                 case 2:
e4f49a 123                     // image blobs
JM 124                     c = new Label("blobText").setVisible(false);
125                     i = new ExternalImage("blobImage", urlFor(RawPage.class, WicketUtils.newPathParameter(repositoryName, objectId, blobPath)).toString());
4ab184 126                     break;
JM 127                 case 3:
e4f49a 128                     // binary blobs
4ab184 129                     c = new Label("blobText", "Binary File");
e4f49a 130                     i = new Image("blobImage").setVisible(false);
4ab184 131                     break;
JM 132                 default:
133                     // plain text
134                     c = new Label("blobText", JGitUtils.getStringContent(r, commit.getTree(),
ae9e15 135                             blobPath, encodings));
4ab184 136                     WicketUtils.setCssClass(c, "plainprint");
e4f49a 137                     i = new Image("blobImage").setVisible(false);
4ab184 138                 }
JM 139                 add(c);
e4f49a 140                 add(i);
4ab184 141             } else {
JM 142                 // plain text
143                 Label blobLabel = new Label("blobText", JGitUtils.getStringContent(r,
ae9e15 144                         commit.getTree(), blobPath, encodings));
4ab184 145                 WicketUtils.setCssClass(blobLabel, "plainprint");
JM 146                 add(blobLabel);
e4f49a 147                 add(new Image("blobImage").setVisible(false));
4ab184 148             }
5fe7df 149         }
JM 150     }
155bf7 151
cebf45 152     @Override
JM 153     protected String getPageName() {
1e47ab 154         return getString("gb.view");
cebf45 155     }
5fe7df 156 }