| Dave Borowitz | ba9c118 | 2013-03-13 14:16:43 -0700 | [diff] [blame] | 1 | // Copyright 2013 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 | 5427146 | 2013-11-11 11:43:11 -0800 | [diff] [blame] | 17 | import com.google.common.base.Joiner; |
| 18 | import com.google.common.collect.ImmutableMap; |
| Masaya Suzuki | 5cecb86 | 2019-03-25 17:35:44 -0700 | [diff] [blame] | 19 | import com.google.gitiles.GitilesRequestFailureException.FailureReason; |
| Dave Borowitz | 5427146 | 2013-11-11 11:43:11 -0800 | [diff] [blame] | 20 | import com.google.gson.reflect.TypeToken; |
| Dave Borowitz | 3b744b1 | 2016-08-19 16:11:10 -0400 | [diff] [blame] | 21 | import java.io.IOException; |
| 22 | import java.io.Writer; |
| 23 | import java.util.List; |
| 24 | import java.util.Map; |
| 25 | import javax.servlet.http.HttpServletRequest; |
| 26 | import javax.servlet.http.HttpServletResponse; |
| Dave Borowitz | ba9c118 | 2013-03-13 14:16:43 -0700 | [diff] [blame] | 27 | import org.eclipse.jgit.api.Git; |
| 28 | import org.eclipse.jgit.api.NameRevCommand; |
| 29 | import org.eclipse.jgit.api.errors.GitAPIException; |
| 30 | import org.eclipse.jgit.errors.AmbiguousObjectException; |
| 31 | import org.eclipse.jgit.errors.RevisionSyntaxException; |
| 32 | import org.eclipse.jgit.http.server.ServletUtils; |
| 33 | import org.eclipse.jgit.lib.Constants; |
| 34 | import org.eclipse.jgit.lib.ObjectId; |
| 35 | import org.eclipse.jgit.lib.Repository; |
| 36 | |
| Dave Borowitz | ba9c118 | 2013-03-13 14:16:43 -0700 | [diff] [blame] | 37 | /** Serves an API result describing an object. */ |
| 38 | public class DescribeServlet extends BaseServlet { |
| 39 | private static final long serialVersionUID = 1L; |
| 40 | |
| 41 | private static final String ALL_PARAM = "all"; |
| 42 | private static final String CONTAINS_PARAM = "contains"; |
| 43 | private static final String TAGS_PARAM = "tags"; |
| 44 | |
| 45 | private static boolean getBooleanParam(GitilesView view, String name) { |
| 46 | List<String> values = view.getParameters().get(name); |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 47 | return !values.isEmpty() && (values.get(0).isEmpty() || values.get(0).equals("1")); |
| Dave Borowitz | ba9c118 | 2013-03-13 14:16:43 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| Dave Borowitz | 8d6d687 | 2014-03-16 15:18:14 -0700 | [diff] [blame] | 50 | protected DescribeServlet(GitilesAccess.Factory accessFactory) { |
| 51 | super(null, accessFactory); |
| Dave Borowitz | ba9c118 | 2013-03-13 14:16:43 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | @Override |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 55 | protected void doGetText(HttpServletRequest req, HttpServletResponse res) throws IOException { |
| Dave Borowitz | ba9c118 | 2013-03-13 14:16:43 -0700 | [diff] [blame] | 56 | String name = describe(ServletUtils.getRepository(req), ViewFilter.getView(req), req, res); |
| 57 | if (name == null) { |
| 58 | return; |
| 59 | } |
| David Pursehouse | c3e772a | 2016-06-15 21:49:35 +0900 | [diff] [blame] | 60 | try (Writer out = startRenderText(req, res)) { |
| 61 | out.write(RefServlet.sanitizeRefForText(name)); |
| 62 | } |
| Dave Borowitz | ba9c118 | 2013-03-13 14:16:43 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | @Override |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 66 | protected void doGetJson(HttpServletRequest req, HttpServletResponse res) throws IOException { |
| Dave Borowitz | ba9c118 | 2013-03-13 14:16:43 -0700 | [diff] [blame] | 67 | String name = describe(ServletUtils.getRepository(req), ViewFilter.getView(req), req, res); |
| 68 | if (name == null) { |
| 69 | return; |
| 70 | } |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 71 | renderJson( |
| 72 | req, |
| 73 | res, |
| Dave Borowitz | ba9c118 | 2013-03-13 14:16:43 -0700 | [diff] [blame] | 74 | ImmutableMap.of(ViewFilter.getView(req).getPathPart(), name), |
| 75 | new TypeToken<Map<String, String>>() {}.getType()); |
| 76 | } |
| 77 | |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 78 | private ObjectId resolve( |
| 79 | Repository repo, GitilesView view, HttpServletRequest req, HttpServletResponse res) |
| 80 | throws IOException { |
| Dave Borowitz | ba9c118 | 2013-03-13 14:16:43 -0700 | [diff] [blame] | 81 | String rev = view.getPathPart(); |
| 82 | try { |
| 83 | return repo.resolve(rev); |
| 84 | } catch (RevisionSyntaxException e) { |
| Masaya Suzuki | 5cecb86 | 2019-03-25 17:35:44 -0700 | [diff] [blame] | 85 | throw new GitilesRequestFailureException(FailureReason.INCORECT_PARAMETER) |
| 86 | .withPublicErrorMessage( |
| 87 | "Invalid revision syntax: %s", RefServlet.sanitizeRefForText(rev)); |
| Dave Borowitz | ba9c118 | 2013-03-13 14:16:43 -0700 | [diff] [blame] | 88 | } catch (AmbiguousObjectException e) { |
| Masaya Suzuki | 5cecb86 | 2019-03-25 17:35:44 -0700 | [diff] [blame] | 89 | throw new GitilesRequestFailureException(FailureReason.AMBIGUOUS_OBJECT) |
| 90 | .withPublicErrorMessage( |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 91 | "Ambiguous short SHA-1 %s (%s)", |
| Masaya Suzuki | 5cecb86 | 2019-03-25 17:35:44 -0700 | [diff] [blame] | 92 | e.getAbbreviatedObjectId(), Joiner.on(", ").join(e.getCandidates())); |
| Dave Borowitz | ba9c118 | 2013-03-13 14:16:43 -0700 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 96 | private String describe( |
| 97 | Repository repo, GitilesView view, HttpServletRequest req, HttpServletResponse res) |
| 98 | throws IOException { |
| Dave Borowitz | ba9c118 | 2013-03-13 14:16:43 -0700 | [diff] [blame] | 99 | if (!getBooleanParam(view, CONTAINS_PARAM)) { |
| Masaya Suzuki | 5cecb86 | 2019-03-25 17:35:44 -0700 | [diff] [blame] | 100 | throw new GitilesRequestFailureException(FailureReason.INCORECT_PARAMETER); |
| Dave Borowitz | ba9c118 | 2013-03-13 14:16:43 -0700 | [diff] [blame] | 101 | } |
| 102 | ObjectId id = resolve(repo, view, req, res); |
| 103 | if (id == null) { |
| 104 | return null; |
| 105 | } |
| Dave Borowitz | ba9c118 | 2013-03-13 14:16:43 -0700 | [diff] [blame] | 106 | String name; |
| Dave Borowitz | c35311d | 2015-08-10 11:21:51 -0400 | [diff] [blame] | 107 | try (Git git = new Git(repo)) { |
| 108 | NameRevCommand cmd = nameRevCommand(git, id, req, res); |
| 109 | if (cmd == null) { |
| 110 | return null; |
| 111 | } |
| Dave Borowitz | ba9c118 | 2013-03-13 14:16:43 -0700 | [diff] [blame] | 112 | name = cmd.call().get(id); |
| 113 | } catch (GitAPIException e) { |
| 114 | throw new IOException(e); |
| 115 | } |
| 116 | if (name == null) { |
| Masaya Suzuki | 5cecb86 | 2019-03-25 17:35:44 -0700 | [diff] [blame] | 117 | throw new GitilesRequestFailureException(FailureReason.OBJECT_NOT_FOUND); |
| Dave Borowitz | ba9c118 | 2013-03-13 14:16:43 -0700 | [diff] [blame] | 118 | } |
| 119 | return name; |
| 120 | } |
| 121 | |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 122 | private NameRevCommand nameRevCommand( |
| 123 | Git git, ObjectId id, HttpServletRequest req, HttpServletResponse res) throws IOException { |
| Dave Borowitz | ba9c118 | 2013-03-13 14:16:43 -0700 | [diff] [blame] | 124 | GitilesView view = ViewFilter.getView(req); |
| Dave Borowitz | c35311d | 2015-08-10 11:21:51 -0400 | [diff] [blame] | 125 | NameRevCommand cmd = git.nameRev(); |
| Dave Borowitz | ba9c118 | 2013-03-13 14:16:43 -0700 | [diff] [blame] | 126 | boolean all = getBooleanParam(view, ALL_PARAM); |
| 127 | boolean tags = getBooleanParam(view, TAGS_PARAM); |
| 128 | if (all && tags) { |
| Masaya Suzuki | 5cecb86 | 2019-03-25 17:35:44 -0700 | [diff] [blame] | 129 | throw new GitilesRequestFailureException(FailureReason.UNSUPPORTED_REVISION_NAMES) |
| 130 | .withPublicErrorMessage("Cannot specify both \"all\" and \"tags\""); |
| Dave Borowitz | ba9c118 | 2013-03-13 14:16:43 -0700 | [diff] [blame] | 131 | } |
| 132 | if (all) { |
| 133 | cmd.addPrefix(Constants.R_REFS); |
| 134 | } else if (tags) { |
| 135 | cmd.addPrefix(Constants.R_TAGS); |
| 136 | } else { |
| 137 | cmd.addAnnotatedTags(); |
| 138 | } |
| 139 | cmd.add(id); |
| 140 | return cmd; |
| 141 | } |
| 142 | } |