This repository was archived by the owner on Mar 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 184
Expand file tree
/
Copy pathRepositoryFilter.java
More file actions
63 lines (56 loc) · 2.59 KB
/
Copy pathRepositoryFilter.java
File metadata and controls
63 lines (56 loc) · 2.59 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
// 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;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.gitiles.ViewFilter.getRegexGroup;
import static org.eclipse.jgit.http.server.ServletUtils.ATTRIBUTE_REPOSITORY;
import com.google.gitiles.GitilesRequestFailureException.FailureReason;
import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jgit.errors.RepositoryNotFoundException;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.transport.resolver.RepositoryResolver;
import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException;
import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
class RepositoryFilter extends AbstractHttpFilter {
private final RepositoryResolver<HttpServletRequest> resolver;
RepositoryFilter(RepositoryResolver<HttpServletRequest> resolver) {
this.resolver = checkNotNull(resolver, "resolver");
}
@Override
public void doFilter(HttpServletRequest req, HttpServletResponse res, FilterChain chain)
throws IOException, ServletException {
try {
String repo = ViewFilter.trimLeadingSlash(getRegexGroup(req, 1));
try (Repository git = resolver.open(req, repo)) {
req.setAttribute(ATTRIBUTE_REPOSITORY, git);
chain.doFilter(req, res);
} catch (RepositoryNotFoundException e) {
// Drop through the rest of the chain. ViewFilter will pass this
// to HostIndexServlet which will attempt to list repositories
// or send SC_NOT_FOUND there.
chain.doFilter(req, res);
} finally {
req.removeAttribute(ATTRIBUTE_REPOSITORY);
}
} catch (ServiceNotEnabledException e) {
throw new GitilesRequestFailureException(FailureReason.SERVICE_NOT_ENABLED, e);
} catch (ServiceNotAuthorizedException e) {
throw new GitilesRequestFailureException(FailureReason.NOT_AUTHORIZED, e);
}
}
}