James Moger
2011-05-28 28d6b2a860740557bf93dd0f9a48d059379ed696
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  */
608ece 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.WebPage;
24 import org.apache.wicket.markup.html.basic.Label;
25 import org.eclipse.jgit.lib.Repository;
26 import org.eclipse.jgit.revwalk.RevCommit;
27
fc948c 28 import com.gitblit.GitBlit;
155bf7 29 import com.gitblit.Keys;
608ece 30 import com.gitblit.utils.JGitUtils;
JM 31 import com.gitblit.wicket.WicketUtils;
32
33 public class RawPage extends WebPage {
34
35     public RawPage(PageParameters params) {
36         super(params);
37
38         if (!params.containsKey("r")) {
39             error("Repository not specified!");
40             redirectToInterceptPage(new RepositoriesPage());
41         }
42         final String repositoryName = WicketUtils.getRepositoryName(params);
155bf7 43         final String objectId = WicketUtils.getObject(params);
608ece 44         final String blobPath = WicketUtils.getPath(params);
JM 45
f5d0ad 46         Repository r = GitBlit.self().getRepository(repositoryName);
608ece 47         if (r == null) {
JM 48             error("Can not load repository " + repositoryName);
49             redirectToInterceptPage(new RepositoriesPage());
50             return;
51         }
52
53         RevCommit commit = JGitUtils.getCommit(r, objectId);
54
55         String extension = null;
56         if (blobPath.lastIndexOf('.') > -1) {
57             extension = blobPath.substring(blobPath.lastIndexOf('.') + 1);
58         }
59
60         // Map the extensions to types
61         Map<String, Integer> map = new HashMap<String, Integer>();
2a7306 62         for (String ext : GitBlit.getStrings(Keys.web.imageExtensions)) {
608ece 63             map.put(ext.toLowerCase(), 2);
JM 64         }
2a7306 65         for (String ext : GitBlit.getStrings(Keys.web.binaryExtensions)) {
608ece 66             map.put(ext.toLowerCase(), 3);
JM 67         }
68
69         if (extension != null) {
70             int type = 0;
71             if (map.containsKey(extension)) {
72                 type = map.get(extension);
73             }
74             Component c = null;
75             switch (type) {
76             case 2:
77                 // TODO image blobs
78                 c = new Label("rawText", "Image File");
79                 break;
80             case 3:
81                 // TODO binary blobs
82                 c = new Label("rawText", "Binary File");
83                 break;
84             default:
85                 // plain text
86                 c = new Label("rawText", JGitUtils.getRawContentAsString(r, commit, blobPath));
87                 WicketUtils.setCssClass(c, "plainprint");
88             }
89             add(c);
90         } else {
91             // plain text
2a7306 92             Label blobLabel = new Label("rawText", JGitUtils.getRawContentAsString(r, commit,
JM 93                     blobPath));
608ece 94             WicketUtils.setCssClass(blobLabel, "plainprint");
JM 95             add(blobLabel);
96         }
97         r.close();
155bf7 98     }
608ece 99 }