| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 1 | // Copyright 2012 Google Inc. All Rights Reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package com.google.gitiles; |
| 16 | |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 17 | import java.io.IOException; |
| 18 | import java.util.Map; |
| Shawn Pearce | db394cc | 2017-07-01 11:45:20 -0700 | [diff] [blame] | 19 | import java.util.Optional; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 20 | import java.util.Set; |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 21 | import javax.annotation.Nullable; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 22 | import javax.servlet.http.HttpServletRequest; |
| Dave Borowitz | 3b744b1 | 2016-08-19 16:11:10 -0400 | [diff] [blame] | 23 | import org.eclipse.jgit.lib.Config; |
| 24 | import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException; |
| 25 | import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 26 | |
| 27 | /** |
| 28 | * Git storage interface for Gitiles. |
| Dave Borowitz | 40255d5 | 2016-08-19 16:16:22 -0400 | [diff] [blame] | 29 | * |
| 30 | * <p>Each instance is associated with a single end-user request, which implicitly includes |
| 31 | * information about the host and repository. |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 32 | */ |
| 33 | public interface GitilesAccess { |
| Shawn Pearce | db394cc | 2017-07-01 11:45:20 -0700 | [diff] [blame] | 34 | /** Access for the current request, if it has been initialized. */ |
| 35 | public static Optional<GitilesAccess> getAccess(HttpServletRequest req) { |
| 36 | return Optional.ofNullable((GitilesAccess) req.getAttribute(GitilesAccess.class.getName())); |
| 37 | } |
| 38 | |
| 39 | /** Access for the current request. */ |
| 40 | public static GitilesAccess getAccess(HttpServletRequest req, Factory factory) { |
| 41 | GitilesAccess access = getAccess(req).orElse(null); |
| 42 | if (access == null) { |
| 43 | access = factory.forRequest(req); |
| 44 | req.setAttribute(GitilesAccess.class.getName(), access); |
| 45 | } |
| 46 | return access; |
| 47 | } |
| 48 | |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 49 | /** Factory for per-request access. */ |
| 50 | public interface Factory { |
| David Pursehouse | e3d3ec8 | 2016-06-15 22:10:48 +0900 | [diff] [blame] | 51 | GitilesAccess forRequest(HttpServletRequest req); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | /** |
| 55 | * List repositories on the host. |
| 56 | * |
| Dave Borowitz | 40255d5 | 2016-08-19 16:16:22 -0400 | [diff] [blame] | 57 | * @param prefix repository base path to list. Trailing "/" is implicitly added if missing. Null |
| 58 | * or empty string will match all repositories. |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 59 | * @param branches branches to list along with each repository. |
| 60 | * @return map of repository names to descriptions. |
| Dave Borowitz | 40255d5 | 2016-08-19 16:16:22 -0400 | [diff] [blame] | 61 | * @throws ServiceNotEnabledException to trigger an HTTP 403 Forbidden (matching behavior in |
| 62 | * {@link org.eclipse.jgit.http.server.RepositoryFilter}). |
| 63 | * @throws ServiceNotAuthorizedException to trigger an HTTP 401 Unauthorized (matching behavior in |
| 64 | * {@link org.eclipse.jgit.http.server.RepositoryFilter}). |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 65 | * @throws IOException if an error occurred. |
| 66 | */ |
| Dave Borowitz | 40255d5 | 2016-08-19 16:16:22 -0400 | [diff] [blame] | 67 | Map<String, RepositoryDescription> listRepositories(@Nullable String prefix, Set<String> branches) |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 68 | throws ServiceNotEnabledException, ServiceNotAuthorizedException, IOException; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 69 | |
| 70 | /** |
| Dave Borowitz | 40255d5 | 2016-08-19 16:16:22 -0400 | [diff] [blame] | 71 | * @return an opaque object that uniquely identifies the end-user making the request, and supports |
| 72 | * {@link Object#equals(Object)} and {@link Object#hashCode()}. Never null. |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 73 | */ |
| David Pursehouse | e3d3ec8 | 2016-06-15 22:10:48 +0900 | [diff] [blame] | 74 | Object getUserKey(); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 75 | |
| 76 | /** @return the repository name associated with the request. */ |
| David Pursehouse | e3d3ec8 | 2016-06-15 22:10:48 +0900 | [diff] [blame] | 77 | String getRepositoryName(); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 78 | |
| 79 | /** |
| 80 | * @return the description attached to the repository of this request. |
| Dave Borowitz | 40255d5 | 2016-08-19 16:16:22 -0400 | [diff] [blame] | 81 | * @throws IOException an error occurred reading the description string from the repository. |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 82 | */ |
| David Pursehouse | e3d3ec8 | 2016-06-15 22:10:48 +0900 | [diff] [blame] | 83 | RepositoryDescription getRepositoryDescription() throws IOException; |
| Dave Borowitz | ded109a | 2014-03-03 15:25:39 -0500 | [diff] [blame] | 84 | |
| 85 | /** |
| 86 | * @return configuration to apply to the host/repository for this request. |
| 87 | * @throws IOException an error occurred reading the configuration. |
| 88 | */ |
| David Pursehouse | e3d3ec8 | 2016-06-15 22:10:48 +0900 | [diff] [blame] | 89 | Config getConfig() throws IOException; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 90 | } |