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