* @param path path in the svn repo
* @param revision revision of the file
* @return found {@link FileEntry}, null if not found
*/
public FileEntry findOne(User user, String path, SVNRevision revision) {
final FileEntry script = new FileEntry();
SVNClientManager svnClientManager = null;
ByteArrayOutputStream outputStream = null;
try {
svnClientManager = getSVNClientManager();
SVNURL userRepoUrl = SVNURL.fromFile(getUserRepoDirectory(user));
if (userRepoUrl == null) {
return null;
}
SVNRepository repo = svnClientManager.createRepository(userRepoUrl, true);
SVNNodeKind nodeKind = repo.checkPath(path, -1);
if (nodeKind == SVNNodeKind.NONE) {
return null;
}
outputStream = new ByteArrayOutputStream();
SVNProperties fileProperty = new SVNProperties();
// Get File.
repo.getFile(path, revision.getNumber(), fileProperty, outputStream);
SVNDirEntry lastRevisionedEntry = repo.info(path, -1);
long lastRevisionNumber = (lastRevisionedEntry == null) ? -1 : lastRevisionedEntry.getRevision();
String revisionStr = fileProperty.getStringValue(SVNProperty.REVISION);
long revisionNumber = Long.parseLong(revisionStr);
SVNDirEntry info = repo.info(path, revisionNumber);
byte[] byteArray = outputStream.toByteArray();
script.setPath(path);
for (String name : fileProperty.nameSet()) {
script.getProperties().put(name, fileProperty.getStringValue(name));
}
script.setFileType(FileType.getFileTypeByExtension(FilenameUtils.getExtension(script.getFileName())));
if (script.getFileType().isEditable()) {
String autoDetectedEncoding = EncodingUtils.detectEncoding(byteArray, "UTF-8");
script.setContent(new String(byteArray, autoDetectedEncoding));
script.setEncoding(autoDetectedEncoding);
script.setContentBytes(byteArray);
} else {
script.setContentBytes(byteArray);
}
script.setDescription(info.getCommitMessage());
script.setRevision(revisionNumber);
script.setLastRevision(lastRevisionNumber);
script.setCreatedUser(user);
} catch (Exception e) {
LOG.error("Error while fetching a file from SVN {}", user.getUserId() + "_" + path, e);
return null;
} finally {
closeSVNClientManagerQuietly(svnClientManager);