| 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; |
| 18 | |
| 19 | import com.google.common.annotations.VisibleForTesting; |
| 20 | import com.google.common.base.Strings; |
| 21 | import com.google.common.collect.ImmutableMap; |
| 22 | import com.google.common.collect.Lists; |
| 23 | |
| 24 | import org.eclipse.jgit.http.server.ServletUtils; |
| 25 | import org.eclipse.jgit.lib.Constants; |
| 26 | import org.eclipse.jgit.lib.Ref; |
| 27 | import org.eclipse.jgit.lib.RefComparator; |
| 28 | import org.eclipse.jgit.lib.RefDatabase; |
| 29 | |
| 30 | import java.io.IOException; |
| 31 | import java.util.Collection; |
| 32 | import java.util.List; |
| 33 | import java.util.Map; |
| 34 | |
| 35 | import javax.servlet.http.HttpServletRequest; |
| 36 | import javax.servlet.http.HttpServletResponse; |
| 37 | |
| 38 | /** Serves the index page for a repository, if accessed directly by a browser. */ |
| 39 | public class RepositoryIndexServlet extends BaseServlet { |
| 40 | private final GitilesAccess.Factory accessFactory; |
| 41 | |
| 42 | public RepositoryIndexServlet(Renderer renderer, GitilesAccess.Factory accessFactory) { |
| 43 | super(renderer); |
| 44 | this.accessFactory = checkNotNull(accessFactory, "accessFactory"); |
| 45 | } |
| 46 | |
| 47 | @Override |
| 48 | protected void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException { |
| 49 | render(req, res, "gitiles.repositoryIndex", buildData(req)); |
| 50 | } |
| 51 | |
| 52 | @VisibleForTesting |
| 53 | Map<String, ?> buildData(HttpServletRequest req) throws IOException { |
| 54 | RepositoryDescription desc = accessFactory.forRequest(req).getRepositoryDescription(); |
| 55 | return ImmutableMap.of( |
| 56 | "cloneUrl", desc.cloneUrl, |
| 57 | "description", Strings.nullToEmpty(desc.description), |
| 58 | "branches", getRefs(req, Constants.R_HEADS), |
| 59 | "tags", getRefs(req, Constants.R_TAGS)); |
| 60 | } |
| 61 | |
| 62 | private List<Map<String, String>> getRefs(HttpServletRequest req, String prefix) |
| 63 | throws IOException { |
| 64 | RefDatabase refdb = ServletUtils.getRepository(req).getRefDatabase(); |
| 65 | String repoName = ViewFilter.getView(req).getRepositoryName(); |
| 66 | Collection<Ref> refs = RefComparator.sort(refdb.getRefs(prefix).values()); |
| 67 | List<Map<String, String>> result = Lists.newArrayListWithCapacity(refs.size()); |
| 68 | |
| 69 | for (Ref ref : refs) { |
| 70 | String name = ref.getName().substring(prefix.length()); |
| 71 | boolean needPrefix = !ref.getName().equals(refdb.getRef(name).getName()); |
| 72 | result.add(ImmutableMap.of( |
| 73 | "url", GitilesView.log().copyFrom(req).setRevision( |
| 74 | Revision.unpeeled(needPrefix ? ref.getName() : name, ref.getObjectId())).toUrl(), |
| 75 | "name", name)); |
| 76 | } |
| 77 | |
| 78 | return result; |
| 79 | } |
| 80 | } |