| Ronald Bhuleskar | 999a71d | 2021-11-19 15:24:51 -0800 | [diff] [blame] | 1 | // Copyright 2021 Google LLC. 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 | |
| 17 | import static com.google.gitiles.FormatType.HTML; |
| 18 | |
| 19 | import java.util.Optional; |
| 20 | import javax.servlet.http.HttpServletRequest; |
| 21 | import org.eclipse.jgit.lib.Repository; |
| 22 | |
| 23 | /** |
| 24 | * Utility that provides information to replace the URL string that contains a branch name to a new |
| Sven Selberg | dff134a | 2022-01-10 14:36:15 +0100 | [diff] [blame] | 25 | * branch name. The updated branch mapping is provided by {@code BranchRedirect#getRedirectBranch} |
| 26 | * method. If it should update the branch then it is the caller's responsibility to update the URL |
| 27 | * with updated branch name as redirect. |
| Ronald Bhuleskar | 999a71d | 2021-11-19 15:24:51 -0800 | [diff] [blame] | 28 | * |
| 29 | * <p>This implementation does not provide a branch redirect mapping. Hence, including this as-is |
| 30 | * would be a no-op. To make this effective {@code BranchRedirect#getRedirectBranch} needs to be |
| 31 | * overridden that provides a mapping to the requested repo/branch. |
| 32 | */ |
| 33 | public class BranchRedirect { |
| 34 | |
| 35 | static final BranchRedirect EMPTY = new BranchRedirect(); |
| 36 | |
| 37 | /** |
| 38 | * Provides an extendable interface that can be used to provide implementation for determining |
| 39 | * redirect branch |
| 40 | * |
| 41 | * @param repo Repository |
| 42 | * @param sourceBranch full branch name eg. refs/heads/master |
| 43 | * @return Returns the branch that should be redirected to on a given repo. {@code |
| 44 | * Optional.empty()} means no redirect. |
| 45 | */ |
| 46 | protected Optional<String> getRedirectBranch(Repository repo, String sourceBranch) { |
| 47 | return Optional.empty(); |
| 48 | } |
| 49 | |
| 50 | static boolean isForAutomation(HttpServletRequest req) { |
| 51 | FormatType formatType = FormatType.getFormatType(req).orElse(HTML); |
| 52 | switch (formatType) { |
| 53 | case HTML: |
| 54 | case DEFAULT: |
| 55 | return false; |
| 56 | case JSON: |
| 57 | case TEXT: |
| 58 | default: |
| 59 | return true; |
| 60 | } |
| 61 | } |
| 62 | } |