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 |
|
e4f49a
|
18 |
import java.io.IOException;
|
608ece
|
19 |
import java.util.HashMap;
|
JM |
20 |
import java.util.Map;
|
|
21 |
|
e4f49a
|
22 |
import org.apache.wicket.IRequestTarget;
|
608ece
|
23 |
import org.apache.wicket.PageParameters;
|
e4f49a
|
24 |
import org.apache.wicket.RequestCycle;
|
608ece
|
25 |
import org.apache.wicket.markup.html.WebPage;
|
e4f49a
|
26 |
import org.apache.wicket.protocol.http.WebResponse;
|
608ece
|
27 |
import org.eclipse.jgit.lib.Repository;
|
JM |
28 |
import org.eclipse.jgit.revwalk.RevCommit;
|
e4f49a
|
29 |
import org.slf4j.Logger;
|
JM |
30 |
import org.slf4j.LoggerFactory;
|
608ece
|
31 |
|
fc948c
|
32 |
import com.gitblit.GitBlit;
|
155bf7
|
33 |
import com.gitblit.Keys;
|
608ece
|
34 |
import com.gitblit.utils.JGitUtils;
|
4ab184
|
35 |
import com.gitblit.utils.StringUtils;
|
608ece
|
36 |
import com.gitblit.wicket.WicketUtils;
|
JM |
37 |
|
|
38 |
public class RawPage extends WebPage {
|
|
39 |
|
e4f49a
|
40 |
private final Logger logger = LoggerFactory.getLogger(getClass().getSimpleName());
|
JM |
41 |
|
|
42 |
public RawPage(final PageParameters params) {
|
608ece
|
43 |
super(params);
|
JM |
44 |
|
|
45 |
if (!params.containsKey("r")) {
|
6caa93
|
46 |
error(getString("gb.repositoryNotSpecified"));
|
608ece
|
47 |
redirectToInterceptPage(new RepositoriesPage());
|
JM |
48 |
}
|
|
49 |
|
e4f49a
|
50 |
getRequestCycle().setRequestTarget(new IRequestTarget() {
|
JM |
51 |
@Override
|
|
52 |
public void detach(RequestCycle requestCycle) {
|
4ab184
|
53 |
}
|
JM |
54 |
|
e4f49a
|
55 |
@Override
|
JM |
56 |
public void respond(RequestCycle requestCycle) {
|
|
57 |
WebResponse response = (WebResponse) requestCycle.getResponse();
|
4ab184
|
58 |
|
e4f49a
|
59 |
final String repositoryName = WicketUtils.getRepositoryName(params);
|
JM |
60 |
final String objectId = WicketUtils.getObject(params);
|
|
61 |
final String blobPath = WicketUtils.getPath(params);
|
|
62 |
String[] encodings = GitBlit.getEncodings();
|
|
63 |
|
|
64 |
Repository r = GitBlit.self().getRepository(repositoryName);
|
|
65 |
if (r == null) {
|
|
66 |
error(getString("gb.canNotLoadRepository") + " " + repositoryName);
|
|
67 |
redirectToInterceptPage(new RepositoriesPage());
|
|
68 |
return;
|
4ab184
|
69 |
}
|
e4f49a
|
70 |
|
JM |
71 |
if (StringUtils.isEmpty(blobPath)) {
|
|
72 |
// objectid referenced raw view
|
|
73 |
byte [] binary = JGitUtils.getByteContent(r, objectId);
|
|
74 |
response.setContentType("application/octet-stream");
|
|
75 |
response.setContentLength(binary.length);
|
|
76 |
try {
|
|
77 |
response.getOutputStream().write(binary);
|
|
78 |
} catch (Exception e) {
|
|
79 |
logger.error("Failed to write binary response", e);
|
|
80 |
}
|
|
81 |
} else {
|
|
82 |
// standard raw blob view
|
|
83 |
RevCommit commit = JGitUtils.getCommit(r, objectId);
|
|
84 |
|
|
85 |
String filename = blobPath;
|
|
86 |
if (blobPath.indexOf('/') > -1) {
|
|
87 |
filename = blobPath.substring(blobPath.lastIndexOf('/') + 1);
|
|
88 |
}
|
|
89 |
|
|
90 |
String extension = null;
|
|
91 |
if (blobPath.lastIndexOf('.') > -1) {
|
|
92 |
extension = blobPath.substring(blobPath.lastIndexOf('.') + 1);
|
|
93 |
}
|
|
94 |
|
|
95 |
// Map the extensions to types
|
|
96 |
Map<String, Integer> map = new HashMap<String, Integer>();
|
|
97 |
for (String ext : GitBlit.getStrings(Keys.web.imageExtensions)) {
|
|
98 |
map.put(ext.toLowerCase(), 2);
|
|
99 |
}
|
|
100 |
for (String ext : GitBlit.getStrings(Keys.web.binaryExtensions)) {
|
|
101 |
map.put(ext.toLowerCase(), 3);
|
|
102 |
}
|
|
103 |
|
|
104 |
if (extension != null) {
|
|
105 |
int type = 0;
|
|
106 |
if (map.containsKey(extension)) {
|
|
107 |
type = map.get(extension);
|
|
108 |
}
|
|
109 |
switch (type) {
|
|
110 |
case 2:
|
|
111 |
// image blobs
|
|
112 |
byte[] image = JGitUtils.getByteContent(r, commit.getTree(), blobPath);
|
|
113 |
response.setContentType("image/" + extension.toLowerCase());
|
|
114 |
response.setContentLength(image.length);
|
|
115 |
try {
|
|
116 |
response.getOutputStream().write(image);
|
|
117 |
} catch (IOException e) {
|
|
118 |
logger.error("Failed to write image response", e);
|
|
119 |
}
|
|
120 |
break;
|
|
121 |
case 3:
|
|
122 |
// binary blobs (download)
|
|
123 |
byte[] binary = JGitUtils.getByteContent(r, commit.getTree(), blobPath);
|
|
124 |
response.setContentLength(binary.length);
|
|
125 |
response.setContentType("application/octet-stream");
|
|
126 |
response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
|
|
127 |
try {
|
|
128 |
response.getOutputStream().write(binary);
|
|
129 |
} catch (IOException e) {
|
|
130 |
logger.error("Failed to write binary response", e);
|
|
131 |
}
|
|
132 |
break;
|
|
133 |
default:
|
|
134 |
// plain text
|
|
135 |
String content = JGitUtils.getStringContent(r, commit.getTree(),
|
|
136 |
blobPath, encodings);
|
|
137 |
response.setContentType("text/plain; charset=UTF-8");
|
|
138 |
try {
|
|
139 |
response.getOutputStream().write(content.getBytes("UTF-8"));
|
|
140 |
} catch (Exception e) {
|
|
141 |
logger.error("Failed to write text response", e);
|
|
142 |
}
|
|
143 |
}
|
|
144 |
|
|
145 |
} else {
|
|
146 |
// plain text
|
|
147 |
String content = JGitUtils.getStringContent(r, commit.getTree(), blobPath,
|
|
148 |
encodings);
|
|
149 |
response.setContentType("text/plain; charset=UTF-8");
|
|
150 |
try {
|
|
151 |
response.getOutputStream().write(content.getBytes("UTF-8"));
|
|
152 |
} catch (Exception e) {
|
|
153 |
logger.error("Failed to write text response", e);
|
|
154 |
}
|
|
155 |
}
|
4ab184
|
156 |
}
|
e4f49a
|
157 |
r.close();
|
4ab184
|
158 |
}
|
e4f49a
|
159 |
});
|
155bf7
|
160 |
}
|
608ece
|
161 |
}
|