]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.server/src/main/java/org/argeo/slc/web/ajaxplorer/svn/SvnDownloadAction.java
Introduce Ajaxplorer support in SLC
[gpl/argeo-slc.git] / runtime / org.argeo.slc.server / src / main / java / org / argeo / slc / web / ajaxplorer / svn / SvnDownloadAction.java
1 package org.argeo.slc.web.ajaxplorer.svn;
2
3 import javax.servlet.ServletOutputStream;
4 import javax.servlet.http.HttpServletRequest;
5 import javax.servlet.http.HttpServletResponse;
6
7 import org.apache.commons.io.IOUtils;
8 import org.argeo.slc.web.ajaxplorer.AjxpAction;
9 import org.argeo.slc.web.ajaxplorer.AjxpAnswer;
10 import org.argeo.slc.web.ajaxplorer.AjxpDriverException;
11 import org.argeo.slc.web.ajaxplorer.file.FileDownloadAction;
12 import org.tmatesoft.svn.core.io.SVNRepository;
13
14 public class SvnDownloadAction implements AjxpAction<SvnDriver> {
15
16 public AjxpAnswer execute(SvnDriver driver, HttpServletRequest request) {
17 String path = request.getParameter("file");
18 if (path.charAt(path.length() - 1) == '/') {
19 // probably a directory
20 return AjxpAnswer.DO_NOTHING;
21 }
22
23 String revStr = request.getParameter("rev");
24 Long rev = Long.parseLong(revStr);
25 return new SvnDownloadAnswer(driver, path, rev);
26 }
27
28 public class SvnDownloadAnswer implements AjxpAnswer {
29 private final SvnDriver driver;
30 private final String path;
31 private final Long rev;
32
33 public SvnDownloadAnswer(SvnDriver driver, String path, Long rev) {
34 this.driver = driver;
35 this.path = path;
36 this.rev = rev;
37 }
38
39 public void updateResponse(HttpServletResponse response) {
40 ServletOutputStream out = null;
41 try {
42 FileDownloadAction.setDefaultDownloadHeaders(response,
43 getFileName(), null);
44 response.setHeader("AjaXplorer-SvnFileName", getFileName());
45
46 SVNRepository repository = driver.getRepository();
47 out = response.getOutputStream();
48 repository.getFile(path, rev, null, out);
49 } catch (Exception e) {
50 throw new AjxpDriverException("Cannot download revision " + rev
51 + " of path " + path, e);
52 } finally {
53 IOUtils.closeQuietly(out);
54 }
55 }
56
57 protected String getFileName() {
58 int lastIndexSlash = path.lastIndexOf('/');
59 final String origFileName;
60 if (lastIndexSlash != -1) {
61 origFileName = path.substring(lastIndexSlash + 1);
62 } else {
63 origFileName = path;
64 }
65
66 int lastIndexPoint = origFileName.lastIndexOf('.');
67 String prefix = origFileName.substring(0, lastIndexPoint);
68 String ext = origFileName.substring(lastIndexPoint);
69 return prefix + "-" + rev + ext;
70 }
71 }
72 }