James Moger
2016-01-25 252dc07d7f85cc344b5919bb7c6166ef84b2102e
commit | author | age
f13c4c 1 /*
JM 2  * Copyright 2011 gitblit.com.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
7bf6e1 16 package com.gitblit.servlet;
9197d3 17
6d874a 18 import java.io.IOException;
JM 19 import java.text.MessageFormat;
20 import java.text.ParseException;
9197d3 21 import java.util.Date;
JM 22
cdb2fe 23 import com.google.inject.Inject;
JM 24 import com.google.inject.Singleton;
46f33f 25
6d874a 26 import javax.servlet.ServletException;
1b34b0 27 import javax.servlet.http.HttpServlet;
9197d3 28 import javax.servlet.http.HttpServletResponse;
JM 29
30 import org.eclipse.jgit.lib.Repository;
31 import org.eclipse.jgit.revwalk.RevCommit;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
7bf6e1 35 import com.gitblit.Constants;
JM 36 import com.gitblit.IStoredSettings;
37 import com.gitblit.Keys;
46f33f 38 import com.gitblit.manager.IFilestoreManager;
db4f6b 39 import com.gitblit.manager.IRepositoryManager;
59b817 40 import com.gitblit.utils.CompressionUtils;
9197d3 41 import com.gitblit.utils.JGitUtils;
6d874a 42 import com.gitblit.utils.MarkdownUtils;
9197d3 43 import com.gitblit.utils.StringUtils;
JM 44
892570 45 /**
JM 46  * Streams out a zip file from the specified repository for any tree path at any
47  * revision.
699e71 48  *
892570 49  * @author James Moger
699e71 50  *
892570 51  */
1b34b0 52 @Singleton
JM 53 public class DownloadZipServlet extends HttpServlet {
9197d3 54
JM 55     private static final long serialVersionUID = 1L;
56
2a7306 57     private transient Logger logger = LoggerFactory.getLogger(DownloadZipServlet.class);
cacf8b 58
65d5bb 59     private IStoredSettings settings;
cacf8b 60
65d5bb 61     private IRepositoryManager repositoryManager;
46f33f 62     
PM 63     private IFilestoreManager filestoreManager;
699e71 64
59b817 65     public static enum Format {
JM 66         zip(".zip"), tar(".tar"), gz(".tar.gz"), xz(".tar.xz"), bzip2(".tar.bzip2");
699e71 67
59b817 68         public final String extension;
699e71 69
59b817 70         Format(String ext) {
JM 71             this.extension = ext;
72         }
699e71 73
59b817 74         public static Format fromName(String name) {
JM 75             for (Format format : values()) {
76                 if (format.name().equalsIgnoreCase(name)) {
77                     return format;
78                 }
79             }
80             return zip;
81         }
82     }
9197d3 83
1b34b0 84     @Inject
46f33f 85     public DownloadZipServlet(IStoredSettings settings, IRepositoryManager repositoryManager, IFilestoreManager filestoreManager) {
1b34b0 86         this.settings = settings;
JM 87         this.repositoryManager = repositoryManager;
46f33f 88         this.filestoreManager = filestoreManager;
9197d3 89     }
JM 90
892570 91     /**
JM 92      * Returns an url to this servlet for the specified parameters.
699e71 93      *
892570 94      * @param baseURL
JM 95      * @param repository
96      * @param objectId
97      * @param path
59b817 98      * @param format
892570 99      * @return an url
JM 100      */
59b817 101     public static String asLink(String baseURL, String repository, String objectId, String path, Format format) {
8c9a20 102         if (baseURL.length() > 0 && baseURL.charAt(baseURL.length() - 1) == '/') {
c22722 103             baseURL = baseURL.substring(0, baseURL.length() - 1);
JM 104         }
5450d0 105         return baseURL + Constants.ZIP_PATH + "?r=" + repository
2a7306 106                 + (path == null ? "" : ("&p=" + path))
59b817 107                 + (objectId == null ? "" : ("&h=" + objectId))
JM 108                 + (format == null ? "" : ("&format=" + format.name()));
9197d3 109     }
JM 110
892570 111     /**
78753b 112      * Creates a zip stream from the repository of the requested data.
699e71 113      *
892570 114      * @param request
JM 115      * @param response
116      * @throws javax.servlet.ServletException
117      * @throws java.io.IOException
118      */
2a7306 119     private void processRequest(javax.servlet.http.HttpServletRequest request,
JM 120             javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException,
121             java.io.IOException {
db4f6b 122         if (!settings.getBoolean(Keys.web.allowZipDownloads, true)) {
9197d3 123             logger.warn("Zip downloads are disabled");
JM 124             response.sendError(HttpServletResponse.SC_FORBIDDEN);
125             return;
126         }
699e71 127
59b817 128         Format format = Format.zip;
9197d3 129         String repository = request.getParameter("r");
JM 130         String basePath = request.getParameter("p");
131         String objectId = request.getParameter("h");
59b817 132         String f = request.getParameter("format");
JM 133         if (!StringUtils.isEmpty(f)) {
134             format = Format.fromName(f);
135         }
699e71 136
9197d3 137         try {
JM 138             String name = repository;
139             if (name.indexOf('/') > -1) {
140                 name = name.substring(name.lastIndexOf('/') + 1);
141             }
59b817 142             name = StringUtils.stripDotGit(name);
9197d3 143
JM 144             if (!StringUtils.isEmpty(basePath)) {
145                 name += "-" + basePath.replace('/', '_');
146             }
147             if (!StringUtils.isEmpty(objectId)) {
148                 name += "-" + objectId;
149             }
699e71 150
db4f6b 151             Repository r = repositoryManager.getRepository(repository);
6d874a 152             if (r == null) {
db4f6b 153                 if (repositoryManager.isCollectingGarbage(repository)) {
e92c6d 154                     error(response, MessageFormat.format("# Error\nGitblit is busy collecting garbage in {0}", repository));
JM 155                     return;
156                 } else {
157                     error(response, MessageFormat.format("# Error\nFailed to find repository {0}", repository));
158                     return;
159                 }
6d874a 160             }
9197d3 161             RevCommit commit = JGitUtils.getCommit(r, objectId);
6d874a 162             if (commit == null) {
JM 163                 error(response, MessageFormat.format("# Error\nFailed to find commit {0}", objectId));
164                 r.close();
165                 return;
166             }
9197d3 167             Date date = JGitUtils.getCommitDate(commit);
6d874a 168
9197d3 169             String contentType = "application/octet-stream";
JM 170             response.setContentType(contentType + "; charset=" + response.getCharacterEncoding());
59b817 171             response.setHeader("Content-Disposition", "attachment; filename=\"" + name + format.extension + "\"");
9197d3 172             response.setDateHeader("Last-Modified", date.getTime());
JM 173             response.setHeader("Cache-Control", "no-cache");
174             response.setHeader("Pragma", "no-cache");
175             response.setDateHeader("Expires", 0);
176
46f33f 177             
9197d3 178             try {
59b817 179                 switch (format) {
JM 180                 case zip:
46f33f 181                     CompressionUtils.zip(r, filestoreManager, basePath, objectId, response.getOutputStream());
59b817 182                     break;
JM 183                 case tar:
46f33f 184                     CompressionUtils.tar(r, filestoreManager, basePath, objectId, response.getOutputStream());
59b817 185                     break;
JM 186                 case gz:
46f33f 187                     CompressionUtils.gz(r, filestoreManager, basePath, objectId, response.getOutputStream());
59b817 188                     break;
JM 189                 case xz:
46f33f 190                     CompressionUtils.xz(r, filestoreManager, basePath, objectId, response.getOutputStream());
59b817 191                     break;
JM 192                 case bzip2:
46f33f 193                     CompressionUtils.bzip2(r, filestoreManager, basePath, objectId, response.getOutputStream());
59b817 194                     break;
JM 195                 }
699e71 196
9197d3 197                 response.flushBuffer();
52fc94 198             } catch (IOException t) {
9a23de 199                 String message = t.getMessage() == null ? "" : t.getMessage().toLowerCase();
JM 200                 if (message.contains("reset") || message.contains("broken pipe")) {
201                     logger.error("Client aborted zip download: " + message);
202                 } else {
699e71 203                     logger.error("Failed to write attachment to client", t);
9a23de 204                 }
9197d3 205             } catch (Throwable t) {
JM 206                 logger.error("Failed to write attachment to client", t);
207             }
6d874a 208
JM 209             // close the repository
210             r.close();
9197d3 211         } catch (Throwable t) {
JM 212             logger.error("Failed to write attachment to client", t);
213         }
214     }
2a7306 215
6d874a 216     private void error(HttpServletResponse response, String mkd) throws ServletException,
JM 217             IOException, ParseException {
218         String content = MarkdownUtils.transformMarkdown(mkd);
219         response.setContentType("text/html; charset=" + Constants.ENCODING);
220         response.getWriter().write(content);
221     }
222
2a7306 223     @Override
JM 224     protected void doPost(javax.servlet.http.HttpServletRequest request,
225             javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException,
226             java.io.IOException {
227         processRequest(request, response);
228     }
229
230     @Override
231     protected void doGet(javax.servlet.http.HttpServletRequest request,
232             javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException,
233             java.io.IOException {
234         processRequest(request, response);
235     }
9197d3 236 }