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 |
*/
|
9197d3
|
16 |
package com.gitblit;
|
JM |
17 |
|
|
18 |
import java.util.Date;
|
|
19 |
|
|
20 |
import javax.servlet.http.HttpServlet;
|
|
21 |
import javax.servlet.http.HttpServletResponse;
|
|
22 |
|
|
23 |
import org.eclipse.jgit.lib.Repository;
|
|
24 |
import org.eclipse.jgit.revwalk.RevCommit;
|
|
25 |
import org.slf4j.Logger;
|
|
26 |
import org.slf4j.LoggerFactory;
|
|
27 |
|
|
28 |
import com.gitblit.utils.JGitUtils;
|
|
29 |
import com.gitblit.utils.StringUtils;
|
|
30 |
|
892570
|
31 |
/**
|
JM |
32 |
* Streams out a zip file from the specified repository for any tree path at any
|
|
33 |
* revision.
|
|
34 |
*
|
|
35 |
* @author James Moger
|
|
36 |
*
|
|
37 |
*/
|
9197d3
|
38 |
public class DownloadZipServlet extends HttpServlet {
|
JM |
39 |
|
|
40 |
private static final long serialVersionUID = 1L;
|
|
41 |
|
2a7306
|
42 |
private transient Logger logger = LoggerFactory.getLogger(DownloadZipServlet.class);
|
9197d3
|
43 |
|
JM |
44 |
public DownloadZipServlet() {
|
|
45 |
super();
|
|
46 |
}
|
|
47 |
|
892570
|
48 |
/**
|
JM |
49 |
* Returns an url to this servlet for the specified parameters.
|
|
50 |
*
|
|
51 |
* @param baseURL
|
|
52 |
* @param repository
|
|
53 |
* @param objectId
|
|
54 |
* @param path
|
|
55 |
* @return an url
|
|
56 |
*/
|
2a7306
|
57 |
public static String asLink(String baseURL, String repository, String objectId, String path) {
|
8c9a20
|
58 |
if (baseURL.length() > 0 && baseURL.charAt(baseURL.length() - 1) == '/') {
|
c22722
|
59 |
baseURL = baseURL.substring(0, baseURL.length() - 1);
|
JM |
60 |
}
|
5450d0
|
61 |
return baseURL + Constants.ZIP_PATH + "?r=" + repository
|
2a7306
|
62 |
+ (path == null ? "" : ("&p=" + path))
|
JM |
63 |
+ (objectId == null ? "" : ("&h=" + objectId));
|
9197d3
|
64 |
}
|
JM |
65 |
|
892570
|
66 |
/**
|
78753b
|
67 |
* Creates a zip stream from the repository of the requested data.
|
892570
|
68 |
*
|
JM |
69 |
* @param request
|
|
70 |
* @param response
|
|
71 |
* @throws javax.servlet.ServletException
|
|
72 |
* @throws java.io.IOException
|
|
73 |
*/
|
2a7306
|
74 |
private void processRequest(javax.servlet.http.HttpServletRequest request,
|
JM |
75 |
javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException,
|
|
76 |
java.io.IOException {
|
|
77 |
if (!GitBlit.getBoolean(Keys.web.allowZipDownloads, true)) {
|
9197d3
|
78 |
logger.warn("Zip downloads are disabled");
|
JM |
79 |
response.sendError(HttpServletResponse.SC_FORBIDDEN);
|
|
80 |
return;
|
|
81 |
}
|
78753b
|
82 |
|
9197d3
|
83 |
String repository = request.getParameter("r");
|
JM |
84 |
String basePath = request.getParameter("p");
|
|
85 |
String objectId = request.getParameter("h");
|
|
86 |
|
|
87 |
try {
|
|
88 |
String name = repository;
|
|
89 |
if (name.indexOf('/') > -1) {
|
|
90 |
name = name.substring(name.lastIndexOf('/') + 1);
|
|
91 |
}
|
|
92 |
|
|
93 |
if (!StringUtils.isEmpty(basePath)) {
|
|
94 |
name += "-" + basePath.replace('/', '_');
|
|
95 |
}
|
|
96 |
if (!StringUtils.isEmpty(objectId)) {
|
|
97 |
name += "-" + objectId;
|
|
98 |
}
|
|
99 |
|
|
100 |
Repository r = GitBlit.self().getRepository(repository);
|
|
101 |
RevCommit commit = JGitUtils.getCommit(r, objectId);
|
|
102 |
Date date = JGitUtils.getCommitDate(commit);
|
|
103 |
String contentType = "application/octet-stream";
|
|
104 |
response.setContentType(contentType + "; charset=" + response.getCharacterEncoding());
|
2a7306
|
105 |
response.setHeader("Content-Disposition", "attachment; filename=\"" + name + ".zip"
|
JM |
106 |
+ "\"");
|
9197d3
|
107 |
response.setDateHeader("Last-Modified", date.getTime());
|
JM |
108 |
response.setHeader("Cache-Control", "no-cache");
|
|
109 |
response.setHeader("Pragma", "no-cache");
|
|
110 |
response.setDateHeader("Expires", 0);
|
|
111 |
|
|
112 |
try {
|
|
113 |
JGitUtils.zip(r, basePath, objectId, response.getOutputStream());
|
|
114 |
response.flushBuffer();
|
|
115 |
} catch (Throwable t) {
|
|
116 |
logger.error("Failed to write attachment to client", t);
|
|
117 |
}
|
|
118 |
} catch (Throwable t) {
|
|
119 |
logger.error("Failed to write attachment to client", t);
|
|
120 |
}
|
|
121 |
}
|
2a7306
|
122 |
|
JM |
123 |
@Override
|
|
124 |
protected void doPost(javax.servlet.http.HttpServletRequest request,
|
|
125 |
javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException,
|
|
126 |
java.io.IOException {
|
|
127 |
processRequest(request, response);
|
|
128 |
}
|
|
129 |
|
|
130 |
@Override
|
|
131 |
protected void doGet(javax.servlet.http.HttpServletRequest request,
|
|
132 |
javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException,
|
|
133 |
java.io.IOException {
|
|
134 |
processRequest(request, response);
|
|
135 |
}
|
9197d3
|
136 |
}
|