-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathDocServlet.java
More file actions
298 lines (263 loc) · 9.96 KB
/
DocServlet.java
File metadata and controls
298 lines (263 loc) · 9.96 KB
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
// Copyright 2015 Google Inc. All Rights Reserved.
//
// 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.google.gitiles.doc;
import static javax.servlet.http.HttpServletResponse.SC_NOT_MODIFIED;
import static org.eclipse.jgit.lib.Constants.OBJ_BLOB;
import static org.eclipse.jgit.lib.FileMode.TYPE_FILE;
import static org.eclipse.jgit.lib.FileMode.TYPE_MASK;
import static org.eclipse.jgit.lib.FileMode.TYPE_TREE;
import com.google.common.base.MoreObjects;
import com.google.common.base.Strings;
import com.google.common.base.Throwables;
import com.google.common.hash.Hasher;
import com.google.common.hash.Hashing;
import com.google.common.net.HttpHeaders;
import com.google.gitiles.BaseServlet;
import com.google.gitiles.GitilesAccess;
import com.google.gitiles.GitilesRequestFailureException;
import com.google.gitiles.GitilesRequestFailureException.FailureReason;
import com.google.gitiles.GitilesView;
import com.google.gitiles.Renderer;
import com.google.gitiles.ViewFilter;
import com.google.gitiles.doc.html.StreamHtmlBuilder;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Nullable;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.commonmark.node.Node;
import org.eclipse.jgit.errors.IncorrectObjectTypeException;
import org.eclipse.jgit.errors.LargeObjectException;
import org.eclipse.jgit.http.server.ServletUtils;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.ObjectReader;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevTree;
import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.treewalk.TreeWalk;
public class DocServlet extends BaseServlet {
private static final long serialVersionUID = 1L;
private static final String INDEX_MD = "index.md";
private static final String NAVBAR_MD = "navbar.md";
private static final String SOY_FILE = "Doc.soy";
private static final String SOY_TEMPLATE = "com.google.gitiles.templates.Doc.markdownDoc";
// Generation of ETag logic. Bump this only if DocServlet logic changes
// significantly enough to impact cached pages. Soy template and source
// files are automatically hashed as part of the ETag.
private static final int ETAG_GEN = 5;
private final HtmlSanitizer.Factory htmlSanitizer;
public DocServlet(GitilesAccess.Factory accessFactory, Renderer renderer) {
this(accessFactory, renderer, HtmlSanitizer.DISABLED_FACTORY);
}
public DocServlet(
GitilesAccess.Factory accessFactory, Renderer renderer, HtmlSanitizer.Factory htmlSanitizer) {
super(renderer, accessFactory);
this.htmlSanitizer = htmlSanitizer;
}
@Override
protected void doGetHtml(HttpServletRequest req, HttpServletResponse res) throws IOException {
MarkdownConfig cfg = MarkdownConfig.get(getAccess(req).getConfig());
if (!cfg.render) {
throw new GitilesRequestFailureException(FailureReason.MARKDOWN_NOT_ENABLED);
}
GitilesView view = ViewFilter.getView(req);
Repository repo = ServletUtils.getRepository(req);
try (RevWalk rw = new RevWalk(repo)) {
ObjectReader reader = rw.getObjectReader();
String path = view.getPathPart();
RevTree root;
try {
root = rw.parseTree(view.getRevision().getId());
} catch (IncorrectObjectTypeException e) {
throw new GitilesRequestFailureException(FailureReason.INCORRECT_OBJECT_TYPE, e);
}
MarkdownFile srcmd = findFile(rw, root, path);
if (srcmd == null) {
throw new GitilesRequestFailureException(FailureReason.OBJECT_NOT_FOUND);
}
MarkdownFile navmd = findNavbar(rw, root, path);
String curEtag = etag(srcmd, navmd);
if (etagMatch(req, curEtag)) {
res.setStatus(SC_NOT_MODIFIED);
return;
}
view = view.toBuilder().setPathPart(srcmd.path).build();
try {
srcmd.read(reader, cfg);
if (navmd != null) {
navmd.read(reader, cfg);
}
} catch (LargeObjectException.ExceedsLimit e) {
fileTooBig(res, view);
return;
}
MarkdownToHtml.Builder fmt =
MarkdownToHtml.builder()
.setConfig(cfg)
.setGitilesView(view)
.setRequestUri(req.getRequestURI())
.setReader(reader)
.setRootTree(root)
.setHtmlSanitizer(htmlSanitizer.create(req));
Navbar navbar = createNavbar(cfg, fmt, navmd);
res.setHeader(HttpHeaders.ETAG, curEtag);
showDoc(req, res, view, fmt, navbar, srcmd);
}
}
private static boolean etagMatch(HttpServletRequest req, String etag) {
String reqEtag = req.getHeader(HttpHeaders.IF_NONE_MATCH);
return reqEtag != null && reqEtag.equals(etag);
}
private String etag(MarkdownFile srcmd, @Nullable MarkdownFile navmd) {
byte[] b = new byte[Constants.OBJECT_ID_LENGTH];
Hasher h = Hashing.murmur3_128().newHasher();
h.putInt(ETAG_GEN);
renderer.getTemplateHash(SOY_FILE).writeBytesTo(b, 0, b.length);
h.putBytes(b);
if (navmd != null) {
navmd.id.copyRawTo(b, 0);
h.putBytes(b);
}
srcmd.id.copyRawTo(b, 0);
h.putBytes(b);
return h.hash().toString();
}
private @Nullable MarkdownFile findNavbar(RevWalk rw, RevTree root, String path)
throws IOException {
if (!Strings.isNullOrEmpty(path)) {
// Traverse up the path until we find a NAVBAR_MD.
StringBuilder pathRemaining = new StringBuilder(path);
while (pathRemaining.length() > 0) {
int lastPathSeparatorIndex = pathRemaining.lastIndexOf("/");
pathRemaining.setLength(lastPathSeparatorIndex + 1);
MarkdownFile navmd = findFile(rw, root, pathRemaining.toString() + NAVBAR_MD);
if (navmd != null) {
return navmd;
}
pathRemaining.setLength(Math.max(lastPathSeparatorIndex, 0));
}
return null;
}
return findFile(rw, root, NAVBAR_MD);
}
private void showDoc(
HttpServletRequest req,
HttpServletResponse res,
GitilesView view,
MarkdownToHtml.Builder fmt,
Navbar navbar,
MarkdownFile srcFile)
throws IOException {
Map<String, Object> data = new HashMap<>();
data.putAll(navbar.toSoyData());
MarkdownConfig cfg = navbar.getConfig();
Node doc = GitilesMarkdown.parse(cfg, srcFile.consumeContent());
data.put("pageTitle", pageTitle(doc, srcFile));
if (view.getType() != GitilesView.Type.ROOTED_DOC) {
data.put("sourceUrl", GitilesView.show().copyFrom(view).toUrl());
data.put("logUrl", GitilesView.log().copyFrom(view).toUrl());
data.put("blameUrl", GitilesView.blame().copyFrom(view).toUrl());
}
if (cfg.analyticsId != null) {
data.put("analyticsId", cfg.analyticsId);
}
try (OutputStream out = startRenderCompressedStreamingHtml(req, res, SOY_TEMPLATE, data)) {
Writer w = newWriter(out, res);
fmt.setConfig(cfg)
.setFilePath(srcFile.path)
.build()
.renderToHtml(new StreamHtmlBuilder(w), doc);
w.flush();
} catch (RuntimeIOException e) {
Throwables.throwIfInstanceOf(e.getCause(), IOException.class);
throw e;
}
}
private static Navbar createNavbar(
MarkdownConfig cfg, MarkdownToHtml.Builder fmt, @Nullable MarkdownFile navFile) {
Navbar navbar = new Navbar().setConfig(cfg);
if (navFile != null) {
navbar
.setFormatter(fmt.setFilePath(navFile.path).build())
.setMarkdown(navFile.consumeContent());
}
return navbar;
}
private static String pageTitle(Node doc, MarkdownFile srcFile) {
String title = MarkdownUtil.getTitle(doc);
return MoreObjects.firstNonNull(title, srcFile.path);
}
private static @Nullable MarkdownFile findFile(RevWalk rw, RevTree root, String path)
throws IOException {
if (Strings.isNullOrEmpty(path)) {
path = INDEX_MD;
}
ObjectReader reader = rw.getObjectReader();
try (TreeWalk tw = TreeWalk.forPath(reader, path, root)) {
if (tw == null) {
return null;
}
if ((tw.getRawMode(0) & TYPE_MASK) == TYPE_TREE) {
if (findIndexFile(tw)) {
path = tw.getPathString();
} else {
return null;
}
}
if ((tw.getRawMode(0) & TYPE_MASK) == TYPE_FILE) {
if (!path.endsWith(".md")) {
return null;
}
return new MarkdownFile(path, tw.getObjectId(0));
}
return null;
}
}
private static boolean findIndexFile(TreeWalk tw) throws IOException {
tw.enterSubtree();
while (tw.next()) {
if ((tw.getRawMode(0) & TYPE_MASK) == TYPE_FILE && INDEX_MD.equals(tw.getNameString())) {
return true;
}
}
return false;
}
private static void fileTooBig(HttpServletResponse res, GitilesView view) throws IOException {
if (view.getType() == GitilesView.Type.ROOTED_DOC) {
throw new GitilesRequestFailureException(FailureReason.OBJECT_TOO_LARGE);
}
res.sendRedirect(GitilesView.show().copyFrom(view).toUrl());
}
private static class MarkdownFile {
final String path;
final ObjectId id;
byte[] content;
MarkdownFile(String path, ObjectId id) {
this.path = path;
this.id = id;
}
void read(ObjectReader reader, MarkdownConfig cfg) throws IOException {
content = reader.open(id, OBJ_BLOB).getCachedBytes(cfg.inputLimit);
}
byte[] consumeContent() {
byte[] c = content;
content = null;
return c;
}
}
}