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