]> git.argeo.org Git - gpl/argeo-slc.git/blob - legacy/runtime/org.argeo.slc.server/src/main/java/org/argeo/slc/web/ajaxplorer/svn/SvnLogAction.java
Restructure SLC
[gpl/argeo-slc.git] / legacy / runtime / org.argeo.slc.server / src / main / java / org / argeo / slc / web / ajaxplorer / svn / SvnLogAction.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 java.io.File;
19 import java.io.PrintWriter;
20 import java.text.SimpleDateFormat;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.Vector;
24
25 import javax.servlet.http.HttpServletRequest;
26 import javax.servlet.http.HttpServletResponse;
27
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;
39
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());
44
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.");
50 }
51 File file = new File(driver.getBasePath() + fileStr);
52 return new SvnLogAnswer(driver, file);
53 }
54
55 protected class SvnLogAnswer implements AjxpAnswer {
56 private final SvnDriver driver;
57 private final File file;
58
59 public SvnLogAnswer(SvnDriver driver, File file) {
60 this.driver = driver;
61 this.file = file;
62 }
63
64 public void updateResponse(HttpServletResponse response) {
65 PrintWriter writer = null;
66 try {
67 writer = response.getWriter();
68 writer.append("<tree>");
69 writer.append("<log>");
70
71 final List<SVNLogEntry> logEntries = new Vector<SVNLogEntry>();
72 ISVNLogEntryHandler logHandler = new ISVNLogEntryHandler() {
73 public void handleLogEntry(SVNLogEntry logEntry)
74 throws SVNException {
75 logEntries.add(logEntry);
76 }
77 };
78
79 driver.getManager().getLogClient().doLog(new File[] { file },
80 SVNRevision.create(0), SVNRevision.HEAD, true, true,
81 100, logHandler);
82
83 for (int i = logEntries.size() - 1; i >= 0; i--) {
84 String xml = logEntryAsXml(logEntries.get(i), file);
85 if(log.isTraceEnabled())
86 log.trace(xml);
87 writer.append(xml);
88 }
89
90 writer.append("</log>");
91 writer.append("</tree>");
92 } catch (Exception e) {
93 throw new AjxpDriverException(
94 "Cannot retrieve log for " + file, e);
95 } finally {
96 IOUtils.closeQuietly(writer);
97 }
98 }
99
100 }
101
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")
107 .append("\"");
108 buf.append(">");
109
110 buf.append("<author>").append(entry.getAuthor()).append("</author>");
111 buf.append("<date>").append(sdfIso.format(entry.getDate())).append(
112 "</date>");
113
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>");
118 }
119 buf.append("</paths>");
120
121 buf.append("<msg>").append(entry.getMessage()).append("</msg>");
122
123 buf.append("</logentry>");
124 return buf.toString();
125 }
126
127 }