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