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