-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathViewFilter.java
More file actions
336 lines (299 loc) · 11.8 KB
/
ViewFilter.java
File metadata and controls
336 lines (299 loc) · 11.8 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
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 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;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.eclipse.jgit.http.server.ServletUtils.ATTRIBUTE_REPOSITORY;
import com.google.common.base.Strings;
import com.google.gitiles.GitilesRequestFailureException.FailureReason;
import java.io.IOException;
import java.util.Map;
import javax.annotation.Nullable;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jgit.http.server.ServletUtils;
import org.eclipse.jgit.http.server.glue.WrappedRequest;
/** Filter to parse URLs and convert them to {@link GitilesView}s. */
public class ViewFilter extends AbstractHttpFilter {
// TODO(dborowitz): Make this public in JGit (or implement getRegexGroup
// upstream).
private static final String REGEX_GROUPS_ATTRIBUTE =
"org.eclipse.jgit.http.server.glue.MetaServlet.serveRegex";
private static final String VIEW_ATTRIBUTE = ViewFilter.class.getName() + "/View";
private static final String CMD_ARCHIVE = "+archive";
private static final String CMD_AUTO = "+";
private static final String CMD_BLAME = "+blame";
private static final String CMD_DESCRIBE = "+describe";
private static final String CMD_DIFF = "+diff";
private static final String CMD_LOG = "+log";
private static final String CMD_REFS = "+refs";
private static final String CMD_SHOW = "+show";
private static final String CMD_DOC = "+doc";
public static GitilesView getView(HttpServletRequest req) {
return (GitilesView) req.getAttribute(VIEW_ATTRIBUTE);
}
static String getRegexGroup(HttpServletRequest req, int groupId) {
WrappedRequest[] groups = (WrappedRequest[]) req.getAttribute(REGEX_GROUPS_ATTRIBUTE);
return checkNotNull(groups)[groupId].getPathInfo();
}
static void setView(HttpServletRequest req, GitilesView view) {
req.setAttribute(VIEW_ATTRIBUTE, view);
}
static void removeView(HttpServletRequest req) {
req.removeAttribute(VIEW_ATTRIBUTE);
}
static String trimLeadingSlash(String str) {
return checkLeadingSlash(str).substring(1);
}
private static String checkLeadingSlash(String str) {
checkArgument(str.startsWith("/"), "expected string starting with a slash: %s", str);
return str;
}
private static boolean isEmptyOrSlash(String path) {
return path.isEmpty() || path.equals("/");
}
private static boolean hasRepository(HttpServletRequest req) {
return req.getAttribute(ATTRIBUTE_REPOSITORY) != null;
}
private final GitilesUrls urls;
private final GitilesAccess.Factory accessFactory;
private final VisibilityCache visibilityCache;
private final BranchRedirect branchRedirect;
public ViewFilter(
GitilesAccess.Factory accessFactory,
GitilesUrls urls,
VisibilityCache visibilityCache,
BranchRedirect branchRedirect) {
this.urls = checkNotNull(urls, "urls");
this.accessFactory = checkNotNull(accessFactory, "accessFactory");
this.visibilityCache = checkNotNull(visibilityCache, "visibilityCache");
this.branchRedirect = checkNotNull(branchRedirect, "branchRedirect");
}
@Override
public void doFilter(HttpServletRequest req, HttpServletResponse res, FilterChain chain)
throws IOException, ServletException {
GitilesView.Builder view = parse(req);
if (view == null) {
throw new GitilesRequestFailureException(FailureReason.CANNOT_PARSE_GITILES_VIEW);
}
Map<String, String[]> params = req.getParameterMap();
view.setHostName(urls.getHostName(req))
.setServletPath(req.getContextPath() + req.getServletPath())
.putAllParams(params);
if (normalize(view, res)) {
return;
}
setView(req, view.build());
try {
chain.doFilter(req, res);
} finally {
removeView(req);
}
}
private boolean normalize(GitilesView.Builder view, HttpServletResponse res) throws IOException {
if (!Revision.isNull(view.getOldRevision())) {
return false;
}
Revision r = view.getRevision();
Revision nr = Revision.normalizeParentExpressions(r);
if (!r.equals(nr)) {
res.sendRedirect(view.setRevision(nr).toUrl());
return true;
}
return false;
}
private @Nullable GitilesView.Builder parse(HttpServletRequest req) throws IOException {
String repoName = trimLeadingSlash(getRegexGroup(req, 1));
if (repoName.isEmpty()) {
return GitilesView.hostIndex();
}
String command = getRegexGroup(req, 2);
String path = getRegexGroup(req, 3);
if (command.isEmpty()) {
return parseNoCommand(req, repoName);
}
if (!hasRepository(req)) {
return null; // no repository? return null to 404.
}
if (command.equals(CMD_ARCHIVE)) {
return parseArchiveCommand(req, repoName, path);
} else if (command.equals(CMD_AUTO)) {
return parseAutoCommand(req, repoName, path);
} else if (command.equals(CMD_BLAME)) {
return parseBlameCommand(req, repoName, path);
} else if (command.equals(CMD_DESCRIBE)) {
return parseDescribeCommand(repoName, path);
} else if (command.equals(CMD_DIFF)) {
return parseDiffCommand(req, repoName, path);
} else if (command.equals(CMD_LOG)) {
return parseLogCommand(req, repoName, path);
} else if (command.equals(CMD_REFS)) {
return parseRefsCommand(repoName, path);
} else if (command.equals(CMD_SHOW)) {
return parseShowCommand(req, repoName, path);
} else if (command.equals(CMD_DOC)) {
return parseDocCommand(req, repoName, path);
} else {
return null;
}
}
private GitilesView.Builder parseNoCommand(HttpServletRequest req, String repoName) {
if (!hasRepository(req)) {
return GitilesView.hostIndex().setRepositoryPrefix(repoName);
}
return GitilesView.repositoryIndex().setRepositoryName(repoName);
}
private @Nullable GitilesView.Builder parseArchiveCommand(
HttpServletRequest req, String repoName, String path) throws IOException {
String ext = null;
for (String e : ArchiveFormat.allExtensions()) {
if (path.endsWith(e)) {
path = path.substring(0, path.length() - e.length());
ext = e;
break;
}
}
if (ext == null || path.endsWith("/")) {
return null;
}
RevisionParser.Result result = parseRevision(req, path);
if (result.getOldRevision() != null) {
return null;
}
return GitilesView.archive()
.setRepositoryName(repoName)
.setRevision(result.getRevision())
.setPathPart(Strings.emptyToNull(result.getPath()))
.setExtension(ext);
}
private @Nullable GitilesView.Builder parseAutoCommand(
HttpServletRequest req, String repoName, String path) throws IOException {
// Note: if you change the mapping for +, make sure to change
// GitilesView.toUrl() correspondingly.
if (path.isEmpty()) {
return null;
}
RevisionParser.Result result = parseRevision(req, path);
if (result.getOldRevision() != null) {
return parseDiffCommand(repoName, result);
}
GitilesView.Builder b = parseShowCommand(repoName, result);
if (b != null && b.getPathPart() != null && b.getPathPart().endsWith(".md")) {
return GitilesView.doc()
.setRepositoryName(repoName)
.setRevision(result.getRevision())
.setPathPart(result.getPath());
}
return b;
}
private @Nullable GitilesView.Builder parseBlameCommand(
HttpServletRequest req, String repoName, String path) throws IOException {
if (path.isEmpty()) {
return null;
}
RevisionParser.Result result = parseRevision(req, path);
if (result.getOldRevision() != null || result.getPath().isEmpty()) {
return null;
}
return GitilesView.blame()
.setRepositoryName(repoName)
.setRevision(result.getRevision())
.setPathPart(result.getPath());
}
private @Nullable GitilesView.Builder parseDescribeCommand(String repoName, String path) {
if (isEmptyOrSlash(path)) {
return null;
}
return GitilesView.describe().setRepositoryName(repoName).setPathPart(path);
}
private GitilesView.Builder parseDiffCommand(HttpServletRequest req, String repoName, String path)
throws IOException {
return parseDiffCommand(repoName, parseRevision(req, path));
}
private @Nullable GitilesView.Builder parseDiffCommand(
String repoName, RevisionParser.Result result) {
return GitilesView.diff()
.setRepositoryName(repoName)
.setRevision(result.getRevision())
.setOldRevision(result.getOldRevision())
.setPathPart(result.getPath());
}
private @Nullable GitilesView.Builder parseLogCommand(
HttpServletRequest req, String repoName, String path) throws IOException {
if (isEmptyOrSlash(path)) {
return GitilesView.log().setRepositoryName(repoName);
}
RevisionParser.Result result = parseRevision(req, path);
return GitilesView.log()
.setRepositoryName(repoName)
.setRevision(result.getRevision())
.setOldRevision(result.getOldRevision())
.setPathPart(result.getPath());
}
private GitilesView.Builder parseRefsCommand(String repoName, String path) {
return GitilesView.refs().setRepositoryName(repoName).setPathPart(path);
}
private GitilesView.Builder parseShowCommand(HttpServletRequest req, String repoName, String path)
throws IOException {
return parseShowCommand(repoName, parseRevision(req, path));
}
private @Nullable GitilesView.Builder parseShowCommand(
String repoName, RevisionParser.Result result) {
if (result.getOldRevision() != null) {
return null;
}
if (result.getPath().isEmpty()) {
return GitilesView.revision().setRepositoryName(repoName).setRevision(result.getRevision());
}
return GitilesView.path()
.setRepositoryName(repoName)
.setRevision(result.getRevision())
.setPathPart(result.getPath());
}
private GitilesView.Builder parseDocCommand(HttpServletRequest req, String repoName, String path)
throws IOException {
return parseDocCommand(repoName, parseRevision(req, path));
}
private @Nullable GitilesView.Builder parseDocCommand(
String repoName, RevisionParser.Result result) {
if (result.getOldRevision() != null) {
return null;
}
GitilesView.Builder b =
GitilesView.doc().setRepositoryName(repoName).setRevision(result.getRevision());
if (!result.getPath().isEmpty()) {
b.setPathPart(result.getPath());
}
return b;
}
private RevisionParser.Result parseRevision(HttpServletRequest req, String path)
throws IOException {
RevisionParser revParser =
new RevisionParser(
ServletUtils.getRepository(req),
accessFactory.forRequest(req),
visibilityCache,
getBranchRedirect(req));
RevisionParser.Result rev = revParser.parse(checkLeadingSlash(path));
if (rev == null) {
throw new GitilesRequestFailureException(FailureReason.OBJECT_NOT_FOUND);
}
return rev;
}
private BranchRedirect getBranchRedirect(HttpServletRequest req) {
return BranchRedirect.isForAutomation(req) ? BranchRedirect.EMPTY : branchRedirect;
}
}