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