2 * Copyright (C) 2007-2012 Mathieu Baudier
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.slc
.web
.ajaxplorer
.svn
;
18 import javax
.servlet
.ServletOutputStream
;
19 import javax
.servlet
.http
.HttpServletRequest
;
20 import javax
.servlet
.http
.HttpServletResponse
;
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
;
29 public class SvnDownloadAction
implements AjxpAction
<SvnDriver
> {
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
;
38 String revStr
= request
.getParameter("rev");
39 Long rev
= Long
.parseLong(revStr
);
40 return new SvnDownloadAnswer(driver
, path
, rev
);
43 public class SvnDownloadAnswer
implements AjxpAnswer
{
44 private final SvnDriver driver
;
45 private final String path
;
46 private final Long rev
;
48 public SvnDownloadAnswer(SvnDriver driver
, String path
, Long rev
) {
54 public void updateResponse(HttpServletResponse response
) {
55 ServletOutputStream out
= null;
57 FileDownloadAction
.setDefaultDownloadHeaders(response
,
59 response
.setHeader("AjaXplorer-SvnFileName", getFileName());
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
);
68 IOUtils
.closeQuietly(out
);
72 protected String
getFileName() {
73 int lastIndexSlash
= path
.lastIndexOf('/');
74 final String origFileName
;
75 if (lastIndexSlash
!= -1) {
76 origFileName
= path
.substring(lastIndexSlash
+ 1);
81 int lastIndexPoint
= origFileName
.lastIndexOf('.');
82 String prefix
= origFileName
.substring(0, lastIndexPoint
);
83 String ext
= origFileName
.substring(lastIndexPoint
);
84 return prefix
+ "-" + rev
+ ext
;