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
;
19 import java
.io
.PrintWriter
;
20 import java
.text
.SimpleDateFormat
;
21 import java
.util
.List
;
23 import java
.util
.Vector
;
25 import javax
.servlet
.http
.HttpServletRequest
;
26 import javax
.servlet
.http
.HttpServletResponse
;
28 import org
.apache
.commons
.io
.IOUtils
;
29 import org
.apache
.commons
.logging
.Log
;
30 import org
.apache
.commons
.logging
.LogFactory
;
31 import org
.argeo
.slc
.web
.ajaxplorer
.AjxpAction
;
32 import org
.argeo
.slc
.web
.ajaxplorer
.AjxpAnswer
;
33 import org
.argeo
.slc
.web
.ajaxplorer
.AjxpDriverException
;
34 import org
.tmatesoft
.svn
.core
.ISVNLogEntryHandler
;
35 import org
.tmatesoft
.svn
.core
.SVNException
;
36 import org
.tmatesoft
.svn
.core
.SVNLogEntry
;
37 import org
.tmatesoft
.svn
.core
.SVNLogEntryPath
;
38 import org
.tmatesoft
.svn
.core
.wc
.SVNRevision
;
40 public class SvnLogAction
implements AjxpAction
<SvnDriver
> {
41 private final SimpleDateFormat sdfIso
= new SimpleDateFormat(
42 "yyyy-MM-dd HH:mm:ss");
43 private final Log log
= LogFactory
.getLog(getClass());
45 public AjxpAnswer
execute(SvnDriver driver
, HttpServletRequest request
) {
46 String fileStr
= request
.getParameter("file");
47 log
.debug("Log file " + fileStr
);
48 if (fileStr
== null) {
49 throw new AjxpDriverException("A file needs to be provided.");
51 File file
= new File(driver
.getBasePath() + fileStr
);
52 return new SvnLogAnswer(driver
, file
);
55 protected class SvnLogAnswer
implements AjxpAnswer
{
56 private final SvnDriver driver
;
57 private final File file
;
59 public SvnLogAnswer(SvnDriver driver
, File file
) {
64 public void updateResponse(HttpServletResponse response
) {
65 PrintWriter writer
= null;
67 writer
= response
.getWriter();
68 writer
.append("<tree>");
69 writer
.append("<log>");
71 final List
<SVNLogEntry
> logEntries
= new Vector
<SVNLogEntry
>();
72 ISVNLogEntryHandler logHandler
= new ISVNLogEntryHandler() {
73 public void handleLogEntry(SVNLogEntry logEntry
)
75 logEntries
.add(logEntry
);
79 driver
.getManager().getLogClient().doLog(new File
[] { file
},
80 SVNRevision
.create(0), SVNRevision
.HEAD
, true, true,
83 for (int i
= logEntries
.size() - 1; i
>= 0; i
--) {
84 String xml
= logEntryAsXml(logEntries
.get(i
), file
);
85 if(log
.isTraceEnabled())
90 writer
.append("</log>");
91 writer
.append("</tree>");
92 } catch (Exception e
) {
93 throw new AjxpDriverException(
94 "Cannot retrieve log for " + file
, e
);
96 IOUtils
.closeQuietly(writer
);
102 protected String
logEntryAsXml(SVNLogEntry entry
, File file
) {
103 StringBuffer buf
= new StringBuffer();
104 buf
.append("<logentry");
105 buf
.append(" revision=\"").append(entry
.getRevision()).append("\"");
106 buf
.append(" is_file=\"").append(file
.isDirectory() ?
"0" : "1")
110 buf
.append("<author>").append(entry
.getAuthor()).append("</author>");
111 buf
.append("<date>").append(sdfIso
.format(entry
.getDate())).append(
114 buf
.append("<paths>");
115 Map
<Object
, SVNLogEntryPath
> paths
= entry
.getChangedPaths();
116 for (SVNLogEntryPath path
: paths
.values()) {
117 buf
.append("<path>").append(path
.getPath()).append("</path>");
119 buf
.append("</paths>");
121 buf
.append("<msg>").append(entry
.getMessage()).append("</msg>");
123 buf
.append("</logentry>");
124 return buf
.toString();