1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
|
/*
* Copyright 2012 gitblit.com.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.gitblit.servlet;
import java.io.IOException;
import java.text.MessageFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jgit.lib.FileMode;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevTree;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.gitblit.Constants;
import com.gitblit.IStoredSettings;
import com.gitblit.Keys;
import com.gitblit.dagger.DaggerServlet;
import com.gitblit.manager.IRepositoryManager;
import com.gitblit.models.PathModel;
import com.gitblit.models.RefModel;
import com.gitblit.utils.ArrayUtils;
import com.gitblit.utils.ByteFormat;
import com.gitblit.utils.JGitUtils;
import com.gitblit.utils.MarkdownUtils;
import com.gitblit.utils.StringUtils;
import com.gitblit.wicket.MarkupProcessor;
import com.gitblit.wicket.MarkupProcessor.MarkupDocument;
import dagger.ObjectGraph;
/**
* Serves the content of a gh-pages branch.
*
* @author James Moger
*
*/
public class PagesServlet extends DaggerServlet {
private static final long serialVersionUID = 1L;
private transient Logger logger = LoggerFactory.getLogger(PagesServlet.class);
private IStoredSettings settings;
private IRepositoryManager repositoryManager;
@Override
protected void inject(ObjectGraph dagger) {
this.settings = dagger.get(IStoredSettings.class);
this.repositoryManager = dagger.get(IRepositoryManager.class);
}
/**
* Returns an url to this servlet for the specified parameters.
*
* @param baseURL
* @param repository
* @param path
* @return an url
*/
public static String asLink(String baseURL, String repository, String path) {
if (baseURL.length() > 0 && baseURL.charAt(baseURL.length() - 1) == '/') {
baseURL = baseURL.substring(0, baseURL.length() - 1);
}
return baseURL + Constants.PAGES + repository + "/" + (path == null ? "" : ("/" + path));
}
/**
* Retrieves the specified resource from the gh-pages branch of the
* repository.
*
* @param request
* @param response
* @throws javax.servlet.ServletException
* @throws java.io.IOException
*/
private void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String path = request.getPathInfo();
if (path.toLowerCase().endsWith(".git")) {
// forward to url with trailing /
// this is important for relative pages links
response.sendRedirect(request.getServletPath() + path + "/");
return;
}
if (path.charAt(0) == '/') {
// strip leading /
path = path.substring(1);
}
// determine repository and resource from url
String repository = "";
String resource = "";
Repository r = null;
int offset = 0;
while (r == null) {
int slash = path.indexOf('/', offset);
if (slash == -1) {
repository = path;
} else {
repository = path.substring(0, slash);
}
r = repositoryManager.getRepository(repository, false);
offset = slash + 1;
if (offset > 0) {
resource = path.substring(offset);
}
if (repository.equals(path)) {
// either only repository in url or no repository found
break;
}
}
ServletContext context = request.getSession().getServletContext();
try {
if (r == null) {
// repository not found!
String mkd = MessageFormat.format(
"# Error\nSorry, no valid **repository** specified in this url: {0}!",
repository);
error(response, mkd);
return;
}
// retrieve the content from the repository
RefModel pages = JGitUtils.getPagesBranch(r);
RevCommit commit = JGitUtils.getCommit(r, pages.getObjectId().getName());
if (commit == null) {
// branch not found!
String mkd = MessageFormat.format(
"# Error\nSorry, the repository {0} does not have a **gh-pages** branch!",
repository);
error(response, mkd);
return;
}
MarkupProcessor processor = new MarkupProcessor(settings);
String [] encodings = settings.getStrings(Keys.web.blobEncodings).toArray(new String[0]);
RevTree tree = commit.getTree();
String res = resource;
if (res.endsWith("/")) {
res = res.substring(0, res.length() - 1);
}
List<PathModel> pathEntries = JGitUtils.getFilesInPath(r, res, commit);
byte[] content = null;
if (pathEntries.isEmpty()) {
// not a path, a specific resource
try {
String contentType = context.getMimeType(res);
if (contentType == null) {
contentType = "text/plain";
}
if (contentType.startsWith("text")) {
content = JGitUtils.getStringContent(r, tree, res, encodings).getBytes(
Constants.ENCODING);
} else {
content = JGitUtils.getByteContent(r, tree, res, false);
}
response.setContentType(contentType);
} catch (Exception e) {
}
} else {
// path request
if (!request.getPathInfo().endsWith("/")) {
// redirect to trailing '/' url
response.sendRedirect(request.getServletPath() + request.getPathInfo() + "/");
return;
}
Map<String, String> names = new TreeMap<String, String>();
for (PathModel entry : pathEntries) {
names.put(entry.name.toLowerCase(), entry.name);
}
List<String> extensions = new ArrayList<String>();
extensions.add("html");
extensions.add("htm");
extensions.addAll(processor.getMarkupExtensions());
for (String ext : extensions) {
String key = "index." + ext;
if (names.containsKey(key)) {
String fileName = names.get(key);
String fullPath = fileName;
if (!res.isEmpty()) {
fullPath = res + "/" + fileName;
}
String stringContent = JGitUtils.getStringContent(r, tree, fullPath, encodings);
if (stringContent == null) {
continue;
}
content = stringContent.getBytes(Constants.ENCODING);
if (content != null) {
res = fullPath;
// assume text/html unless the servlet container
// overrides
response.setContentType("text/html; charset=" + Constants.ENCODING);
break;
}
}
}
}
// no content, document list or custom 404 page
if (ArrayUtils.isEmpty(content)) {
if (pathEntries.isEmpty()) {
// 404
String custom404 = JGitUtils.getStringContent(r, tree, "404.html", encodings);
if (!StringUtils.isEmpty(custom404)) {
content = custom404.getBytes(Constants.ENCODING);
}
// still no content
if (ArrayUtils.isEmpty(content)) {
String str = MessageFormat.format(
"# Error\nSorry, the requested resource **{0}** was not found.",
resource);
content = MarkdownUtils.transformMarkdown(str).getBytes(Constants.ENCODING);
}
try {
// output the content
logger.warn("Pages 404: " + resource);
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
response.getOutputStream().write(content);
response.flushBuffer();
} catch (Throwable t) {
logger.error("Failed to write page to client", t);
}
} else {
// document list
response.setContentType("text/html");
response.getWriter().append("<style>table th, table td { min-width: 150px; text-align: left; }</style>");
response.getWriter().append("<table>");
response.getWriter().append("<thead><tr><th>path</th><th>mode</th><th>size</th></tr>");
response.getWriter().append("</thead>");
response.getWriter().append("<tbody>");
String pattern = "<tr><td><a href=\"{0}/{1}\">{1}</a></td><td>{2}</td><td>{3}</td></tr>";
final ByteFormat byteFormat = new ByteFormat();
if (!pathEntries.isEmpty()) {
if (pathEntries.get(0).path.indexOf('/') > -1) {
// we are in a subdirectory, add parent directory link
pathEntries.add(0, new PathModel("..", resource + "/..", 0, FileMode.TREE.getBits(), null, null));
}
}
String basePath = request.getServletPath() + request.getPathInfo();
if (basePath.charAt(basePath.length() - 1) == '/') {
// strip trailing slash
basePath = basePath.substring(0, basePath.length() - 1);
}
for (PathModel entry : pathEntries) {
response.getWriter().append(MessageFormat.format(pattern, basePath, entry.name,
JGitUtils.getPermissionsFromMode(entry.mode), byteFormat.format(entry.size)));
}
response.getWriter().append("</tbody>");
response.getWriter().append("</table>");
}
return;
}
// check to see if we should transform markup files
String ext = StringUtils.getFileExtension(resource);
if (processor.getMarkupExtensions().contains(ext)) {
String markup = new String(content, Constants.ENCODING);
MarkupDocument markupDoc = processor.parse(repository, commit.getName(), resource, markup);
content = markupDoc.html.getBytes("UTF-8");
response.setContentType("text/html; charset=" + Constants.ENCODING);
}
try {
// output the content
response.setHeader("Cache-Control", "public, max-age=3600, must-revalidate");
response.setDateHeader("Last-Modified", JGitUtils.getCommitDate(commit).getTime());
response.getOutputStream().write(content);
response.flushBuffer();
} catch (Throwable t) {
logger.error("Failed to write page to client", t);
}
} catch (Throwable t) {
logger.error("Failed to write page to client", t);
} finally {
r.close();
}
}
private void error(HttpServletResponse response, String mkd) throws ServletException,
IOException, ParseException {
String content = MarkdownUtils.transformMarkdown(mkd);
response.setContentType("text/html; charset=" + Constants.ENCODING);
response.getWriter().write(content);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
}
|