blob: 3f034558fd49dd0ad578c5785b0cf942b754f1e9 [file] [log] [blame]
Dave Borowitz6d9bc5a2013-06-19 09:12:52 -07001// Copyright 2013 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
15package com.google.gitiles;
16
17import static javax.servlet.http.HttpServletResponse.SC_NOT_FOUND;
18import static javax.servlet.http.HttpServletResponse.SC_OK;
19
Dave Borowitzfc417942014-04-09 07:54:42 -070020import com.google.common.base.Strings;
Dave Borowitz3b744b12016-08-19 16:11:10 -040021import java.io.IOException;
David Pursehouse7a7f5472016-10-14 09:59:20 +090022import java.util.Optional;
Dave Borowitz3b744b12016-08-19 16:11:10 -040023import javax.servlet.ServletException;
24import javax.servlet.http.HttpServletRequest;
25import javax.servlet.http.HttpServletResponse;
Dave Borowitz6d9bc5a2013-06-19 09:12:52 -070026import org.eclipse.jgit.api.ArchiveCommand;
27import org.eclipse.jgit.api.errors.GitAPIException;
Dave Borowitz6d9bc5a2013-06-19 09:12:52 -070028import org.eclipse.jgit.errors.IncorrectObjectTypeException;
29import org.eclipse.jgit.http.server.ServletUtils;
Dave Borowitz5051e672013-11-11 11:09:40 -080030import org.eclipse.jgit.lib.FileMode;
31import org.eclipse.jgit.lib.ObjectId;
Dave Borowitz6d9bc5a2013-06-19 09:12:52 -070032import org.eclipse.jgit.lib.Repository;
Dave Borowitz5051e672013-11-11 11:09:40 -080033import org.eclipse.jgit.revwalk.RevTree;
Dave Borowitz6d9bc5a2013-06-19 09:12:52 -070034import org.eclipse.jgit.revwalk.RevWalk;
Dave Borowitz5051e672013-11-11 11:09:40 -080035import org.eclipse.jgit.treewalk.TreeWalk;
Dave Borowitz6d9bc5a2013-06-19 09:12:52 -070036
Dave Borowitz6d9bc5a2013-06-19 09:12:52 -070037public class ArchiveServlet extends BaseServlet {
38 private static final long serialVersionUID = 1L;
39
Dave Borowitzded109a2014-03-03 15:25:39 -050040 public ArchiveServlet(GitilesAccess.Factory accessFactory) {
Dave Borowitz8d6d6872014-03-16 15:18:14 -070041 super(null, accessFactory);
Dave Borowitz6d9bc5a2013-06-19 09:12:52 -070042 }
43
44 @Override
45 protected void doGet(HttpServletRequest req, HttpServletResponse res)
46 throws IOException, ServletException {
47 GitilesView view = ViewFilter.getView(req);
48 Revision rev = view.getRevision();
49 Repository repo = ServletUtils.getRepository(req);
50
Dave Borowitz5051e672013-11-11 11:09:40 -080051 ObjectId treeId = getTree(view, repo, rev);
52 if (treeId.equals(ObjectId.zeroId())) {
Dave Borowitz6d9bc5a2013-06-19 09:12:52 -070053 res.sendError(SC_NOT_FOUND);
54 return;
Dave Borowitz6d9bc5a2013-06-19 09:12:52 -070055 }
56
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +020057 Optional<ArchiveFormat> format =
58 ArchiveFormat.byExtension(view.getExtension(), getAccess(req).getConfig());
Dave Borowitzded109a2014-03-03 15:25:39 -050059 if (!format.isPresent()) {
60 res.setStatus(SC_NOT_FOUND);
61 return;
62 }
Dave Borowitz6d9bc5a2013-06-19 09:12:52 -070063 String filename = getFilename(view, rev, view.getExtension());
Andrew Bonventrecc3418b2016-12-01 20:18:37 -080064 setDownloadHeaders(req, res, filename, format.get().getMimeType());
Dave Borowitz6d9bc5a2013-06-19 09:12:52 -070065 res.setStatus(SC_OK);
66
67 try {
68 new ArchiveCommand(repo)
Dave Borowitz36eb26d2014-04-19 16:42:32 -070069 .setFormat(format.get().getRegisteredName())
Dave Borowitz5051e672013-11-11 11:09:40 -080070 .setTree(treeId)
Dave Borowitz6d9bc5a2013-06-19 09:12:52 -070071 .setOutputStream(res.getOutputStream())
72 .call();
73 } catch (GitAPIException e) {
74 throw new IOException(e);
75 }
76 }
77
Dave Borowitz5051e672013-11-11 11:09:40 -080078 private ObjectId getTree(GitilesView view, Repository repo, Revision rev) throws IOException {
Shawn Pearceb5ad0a02015-05-24 20:33:17 -070079 try (RevWalk rw = new RevWalk(repo)) {
Dave Borowitz5051e672013-11-11 11:09:40 -080080 RevTree tree = rw.parseTree(rev.getId());
Dave Borowitzfc417942014-04-09 07:54:42 -070081 if (Strings.isNullOrEmpty(view.getPathPart())) {
Dave Borowitz5051e672013-11-11 11:09:40 -080082 return tree;
83 }
84 TreeWalk tw = TreeWalk.forPath(rw.getObjectReader(), view.getPathPart(), tree);
Dave Borowitzfc417942014-04-09 07:54:42 -070085 if (tw == null || tw.getFileMode(0) != FileMode.TREE) {
Dave Borowitz5051e672013-11-11 11:09:40 -080086 return ObjectId.zeroId();
87 }
88 return tw.getObjectId(0);
89 } catch (IncorrectObjectTypeException e) {
90 return ObjectId.zeroId();
Dave Borowitz5051e672013-11-11 11:09:40 -080091 }
92 }
93
Dave Borowitz6d9bc5a2013-06-19 09:12:52 -070094 private String getFilename(GitilesView view, Revision rev, String ext) {
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +020095 StringBuilder sb =
96 new StringBuilder()
97 .append(PathUtil.basename(view.getRepositoryName()))
98 .append('-')
99 .append(rev.getName());
Dave Borowitz5051e672013-11-11 11:09:40 -0800100 if (view.getPathPart() != null) {
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +0200101 sb.append('-').append(view.getPathPart().replace('/', '-'));
Dave Borowitz5051e672013-11-11 11:09:40 -0800102 }
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +0200103 return sb.append(ext).toString();
Dave Borowitz6d9bc5a2013-06-19 09:12:52 -0700104 }
105}