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