| 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 | |
| 17 | import static com.google.common.base.Preconditions.checkNotNull; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 18 | |
| 19 | import com.google.common.base.Strings; |
| David Pursehouse | 6740b3e | 2019-07-17 11:06:08 +0900 | [diff] [blame] | 20 | import com.google.common.collect.ImmutableList; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 21 | import com.google.common.collect.ImmutableMap; |
| 22 | import com.google.common.collect.Sets; |
| Masaya Suzuki | 5cecb86 | 2019-03-25 17:35:44 -0700 | [diff] [blame] | 23 | import com.google.gitiles.GitilesRequestFailureException.FailureReason; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 24 | import com.google.gson.reflect.TypeToken; |
| David Pursehouse | f35b06c | 2016-08-20 11:10:42 +0900 | [diff] [blame] | 25 | import com.google.template.soy.data.restricted.NullData; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 26 | import java.io.IOException; |
| Dave Borowitz | 673d198 | 2014-05-02 12:30:49 -0700 | [diff] [blame] | 27 | import java.io.Writer; |
| Dave Borowitz | 2705893 | 2014-12-03 15:44:46 -0800 | [diff] [blame] | 28 | import java.util.Collections; |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 29 | import java.util.LinkedHashMap; |
| 30 | import java.util.List; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 31 | import java.util.Map; |
| David Pursehouse | 7a7f547 | 2016-10-14 09:59:20 +0900 | [diff] [blame] | 32 | import java.util.Optional; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 33 | import java.util.Set; |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 34 | import javax.annotation.Nullable; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 35 | import javax.servlet.http.HttpServletRequest; |
| 36 | import javax.servlet.http.HttpServletResponse; |
| Dave Borowitz | 3b744b1 | 2016-08-19 16:11:10 -0400 | [diff] [blame] | 37 | import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException; |
| 38 | import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 39 | |
| 40 | /** Serves the top level index page for a Gitiles host. */ |
| 41 | public class HostIndexServlet extends BaseServlet { |
| Chad Horohoe | ad23f14 | 2012-11-12 09:45:39 -0800 | [diff] [blame] | 42 | private static final long serialVersionUID = 1L; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 43 | |
| 44 | protected final GitilesUrls urls; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 45 | |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 46 | public HostIndexServlet( |
| 47 | GitilesAccess.Factory accessFactory, Renderer renderer, GitilesUrls urls) { |
| Dave Borowitz | 8d6d687 | 2014-03-16 15:18:14 -0700 | [diff] [blame] | 48 | super(renderer, accessFactory); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 49 | this.urls = checkNotNull(urls, "urls"); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 52 | private Map<String, RepositoryDescription> list( |
| David Pursehouse | 3484ab8 | 2019-05-13 06:55:07 +0200 | [diff] [blame] | 53 | HttpServletRequest req, String prefix, Set<String> branches) throws IOException { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 54 | Map<String, RepositoryDescription> descs; |
| 55 | try { |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 56 | descs = getAccess(req).listRepositories(prefix, branches); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 57 | } catch (ServiceNotEnabledException e) { |
| Masaya Suzuki | 5cecb86 | 2019-03-25 17:35:44 -0700 | [diff] [blame] | 58 | throw new GitilesRequestFailureException(FailureReason.SERVICE_NOT_ENABLED, e); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 59 | } catch (ServiceNotAuthorizedException e) { |
| Masaya Suzuki | 5cecb86 | 2019-03-25 17:35:44 -0700 | [diff] [blame] | 60 | throw new GitilesRequestFailureException(FailureReason.NOT_AUTHORIZED, e); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 61 | } |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 62 | if (prefix != null && descs.isEmpty()) { |
| Masaya Suzuki | 5cecb86 | 2019-03-25 17:35:44 -0700 | [diff] [blame] | 63 | throw new GitilesRequestFailureException(FailureReason.REPOSITORY_NOT_FOUND); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 64 | } |
| Dave Borowitz | b1c628f | 2013-01-11 11:28:20 -0800 | [diff] [blame] | 65 | return descs; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| David Pursehouse | 6740b3e | 2019-07-17 11:06:08 +0900 | [diff] [blame] | 68 | private Map<String, Object> toMapData( |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 69 | RepositoryDescription desc, @Nullable String prefix, GitilesView view) { |
| David Pursehouse | 6740b3e | 2019-07-17 11:06:08 +0900 | [diff] [blame] | 70 | return ImmutableMap.<String, Object>builder() |
| 71 | .put("name", stripPrefix(prefix, desc.name)) |
| 72 | .put("description", Strings.nullToEmpty(desc.description)) |
| 73 | .put( |
| 74 | "url", |
| 75 | GitilesView.repositoryIndex().copyFrom(view).setRepositoryName(desc.name).toUrl()) |
| 76 | .build(); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| Dave Borowitz | b1c628f | 2013-01-11 11:28:20 -0800 | [diff] [blame] | 79 | @Override |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 80 | protected void doHead(HttpServletRequest req, HttpServletResponse res) throws IOException { |
| Shawn Pearce | 10e68e6 | 2016-01-02 09:37:58 -0800 | [diff] [blame] | 81 | Optional<FormatType> format = getFormat(req); |
| 82 | if (!format.isPresent()) { |
| Masaya Suzuki | 5cecb86 | 2019-03-25 17:35:44 -0700 | [diff] [blame] | 83 | throw new GitilesRequestFailureException(FailureReason.UNSUPPORTED_RESPONSE_FORMAT); |
| Shawn Pearce | 10e68e6 | 2016-01-02 09:37:58 -0800 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | GitilesView view = ViewFilter.getView(req); |
| 87 | String prefix = view.getRepositoryPrefix(); |
| 88 | if (prefix != null) { |
| David Pursehouse | 3484ab8 | 2019-05-13 06:55:07 +0200 | [diff] [blame] | 89 | Map<String, RepositoryDescription> descs = list(req, prefix, Collections.emptySet()); |
| Shawn Pearce | 10e68e6 | 2016-01-02 09:37:58 -0800 | [diff] [blame] | 90 | if (descs == null) { |
| 91 | return; |
| 92 | } |
| 93 | } |
| 94 | switch (format.get()) { |
| 95 | case HTML: |
| 96 | case JSON: |
| 97 | case TEXT: |
| 98 | res.setStatus(HttpServletResponse.SC_OK); |
| 99 | res.setContentType(format.get().getMimeType()); |
| 100 | break; |
| David Pursehouse | cb91aaf | 2016-06-15 22:05:24 +0900 | [diff] [blame] | 101 | case DEFAULT: |
| Shawn Pearce | 10e68e6 | 2016-01-02 09:37:58 -0800 | [diff] [blame] | 102 | default: |
| Masaya Suzuki | 5cecb86 | 2019-03-25 17:35:44 -0700 | [diff] [blame] | 103 | throw new GitilesRequestFailureException(FailureReason.UNSUPPORTED_RESPONSE_FORMAT); |
| Shawn Pearce | 10e68e6 | 2016-01-02 09:37:58 -0800 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | |
| 107 | @Override |
| Dave Borowitz | b1c628f | 2013-01-11 11:28:20 -0800 | [diff] [blame] | 108 | protected void doGetHtml(HttpServletRequest req, HttpServletResponse res) throws IOException { |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 109 | GitilesView view = ViewFilter.getView(req); |
| 110 | String prefix = view.getRepositoryPrefix(); |
| David Pursehouse | 3484ab8 | 2019-05-13 06:55:07 +0200 | [diff] [blame] | 111 | Map<String, RepositoryDescription> descs = list(req, prefix, parseShowBranch(req)); |
| Dave Borowitz | b1c628f | 2013-01-11 11:28:20 -0800 | [diff] [blame] | 112 | if (descs == null) { |
| 113 | return; |
| 114 | } |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 115 | |
| David Pursehouse | 6740b3e | 2019-07-17 11:06:08 +0900 | [diff] [blame] | 116 | ImmutableList.Builder<Map<String, Object>> repos = ImmutableList.builder(); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 117 | for (RepositoryDescription desc : descs.values()) { |
| David Pursehouse | 969bfc8 | 2015-12-08 15:11:17 +0900 | [diff] [blame] | 118 | if (prefix == null || desc.name.startsWith(prefix)) { |
| David Pursehouse | 6740b3e | 2019-07-17 11:06:08 +0900 | [diff] [blame] | 119 | repos.add(toMapData(desc, prefix, view)); |
| David Pursehouse | 969bfc8 | 2015-12-08 15:11:17 +0900 | [diff] [blame] | 120 | } |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 123 | String hostName = urls.getHostName(req); |
| 124 | List<Map<String, String>> breadcrumbs = null; |
| 125 | if (prefix != null) { |
| 126 | hostName = hostName + '/' + prefix; |
| 127 | breadcrumbs = view.getBreadcrumbs(); |
| 128 | } |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 129 | renderHtml( |
| 130 | req, |
| 131 | res, |
| 132 | "gitiles.hostIndex", |
| 133 | ImmutableMap.of( |
| 134 | "hostName", |
| 135 | hostName, |
| 136 | "breadcrumbs", |
| David Pursehouse | 6740b3e | 2019-07-17 11:06:08 +0900 | [diff] [blame] | 137 | breadcrumbs != null ? breadcrumbs : NullData.INSTANCE, |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 138 | "prefix", |
| 139 | prefix != null ? prefix + '/' : "", |
| 140 | "repositories", |
| David Pursehouse | 6740b3e | 2019-07-17 11:06:08 +0900 | [diff] [blame] | 141 | repos.build())); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| Dave Borowitz | b1c628f | 2013-01-11 11:28:20 -0800 | [diff] [blame] | 144 | @Override |
| 145 | protected void doGetText(HttpServletRequest req, HttpServletResponse res) throws IOException { |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 146 | String prefix = ViewFilter.getView(req).getRepositoryPrefix(); |
| Dave Borowitz | b1c628f | 2013-01-11 11:28:20 -0800 | [diff] [blame] | 147 | Set<String> branches = parseShowBranch(req); |
| David Pursehouse | 3484ab8 | 2019-05-13 06:55:07 +0200 | [diff] [blame] | 148 | Map<String, RepositoryDescription> descs = list(req, prefix, branches); |
| Dave Borowitz | b1c628f | 2013-01-11 11:28:20 -0800 | [diff] [blame] | 149 | if (descs == null) { |
| 150 | return; |
| 151 | } |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 152 | |
| David Pursehouse | c3e772a | 2016-06-15 21:49:35 +0900 | [diff] [blame] | 153 | try (Writer writer = startRenderText(req, res)) { |
| 154 | for (RepositoryDescription repo : descs.values()) { |
| 155 | for (String name : branches) { |
| 156 | String ref = repo.branches.get(name); |
| 157 | if (ref == null) { |
| 158 | // Print stub (forty '-' symbols) |
| 159 | ref = "----------------------------------------"; |
| 160 | } |
| 161 | writer.write(ref); |
| 162 | writer.write(' '); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 163 | } |
| David Pursehouse | fa84572 | 2016-10-04 16:26:17 +0900 | [diff] [blame] | 164 | writer.write(GitilesUrls.escapeName(stripPrefix(prefix, repo.name))); |
| David Pursehouse | c3e772a | 2016-06-15 21:49:35 +0900 | [diff] [blame] | 165 | writer.write('\n'); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 166 | } |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 167 | } |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| Dave Borowitz | b1c628f | 2013-01-11 11:28:20 -0800 | [diff] [blame] | 170 | @Override |
| 171 | protected void doGetJson(HttpServletRequest req, HttpServletResponse res) throws IOException { |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 172 | String prefix = ViewFilter.getView(req).getRepositoryPrefix(); |
| David Pursehouse | 3484ab8 | 2019-05-13 06:55:07 +0200 | [diff] [blame] | 173 | Map<String, RepositoryDescription> descs = list(req, prefix, parseShowBranch(req)); |
| Dave Borowitz | b1c628f | 2013-01-11 11:28:20 -0800 | [diff] [blame] | 174 | if (descs == null) { |
| 175 | return; |
| 176 | } |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 177 | if (prefix != null) { |
| 178 | Map<String, RepositoryDescription> r = new LinkedHashMap<>(); |
| 179 | for (Map.Entry<String, RepositoryDescription> e : descs.entrySet()) { |
| 180 | r.put(stripPrefix(prefix, e.getKey()), e.getValue()); |
| 181 | } |
| 182 | descs = r; |
| 183 | } |
| Dave Borowitz | b1c628f | 2013-01-11 11:28:20 -0800 | [diff] [blame] | 184 | renderJson(req, res, descs, new TypeToken<Map<String, RepositoryDescription>>() {}.getType()); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 185 | } |
| 186 | |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 187 | private static String stripPrefix(@Nullable String prefix, String name) { |
| David Pursehouse | 969bfc8 | 2015-12-08 15:11:17 +0900 | [diff] [blame] | 188 | if (prefix != null && name.startsWith(prefix)) { |
| 189 | return name.substring(prefix.length() + 1); |
| 190 | } |
| 191 | return name; |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 192 | } |
| 193 | |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 194 | private static Set<String> parseShowBranch(HttpServletRequest req) { |
| 195 | // Roughly match Gerrit Code Review's /projects/ API by supporting |
| 196 | // both show-branch and b as query parameters. |
| 197 | Set<String> branches = Sets.newLinkedHashSet(); |
| 198 | String[] values = req.getParameterValues("show-branch"); |
| 199 | if (values != null) { |
| Dave Borowitz | 2705893 | 2014-12-03 15:44:46 -0800 | [diff] [blame] | 200 | Collections.addAll(branches, values); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 201 | } |
| 202 | values = req.getParameterValues("b"); |
| 203 | if (values != null) { |
| Dave Borowitz | 2705893 | 2014-12-03 15:44:46 -0800 | [diff] [blame] | 204 | Collections.addAll(branches, values); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 205 | } |
| 206 | return branches; |
| 207 | } |
| 208 | } |