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