branch feature/GIT updated (89f9466 -> bf4a193)
This is an automated email from the git hooks/post-receive script. New change to branch feature/GIT in repository scmwebeditor. See http://git.nuiton.org/scmwebeditor.git from 89f9466 Remove code-mirror new bf4a193 Now using an interface instead of the abstract class for the SCMs (ScmConnection) and the local Git repositories are stored in a folder which the name is a hash of their URL The 1 revisions listed above as "new" are entirely new to this repository and will be described in separate emails. The revisions listed as "adds" were already present in the repository and have only been added to this reference. Detailed log of new commits: commit bf4a193f766d7fe0b7ba85bf6972ddba61f7805c Author: InternationalKoder <international_koder@yahoo.com> Date: Thu Apr 23 17:33:17 2015 +0200 Now using an interface instead of the abstract class for the SCMs (ScmConnection) and the local Git repositories are stored in a folder which the name is a hash of their URL Summary of changes: .../org/nuiton/scmwebeditor/GitConnection.java | 301 +++++---- .../org/nuiton/scmwebeditor/ScmConnection.java | 117 ++-- .../nuiton/scmwebeditor/ScmConnectionFactory.java | 2 +- .../scmwebeditor/ScmConnectionInterface.java | 84 --- .../java/org/nuiton/scmwebeditor/ScmSession.java | 2 +- .../scmwebeditor/ScmWebEditorBaseAction.java | 35 +- .../org/nuiton/scmwebeditor/SvnConnection.java | 677 +++++++++++++-------- .../nuiton/scmwebeditor/actions/LogoutAction.java | 17 +- .../nuiton/scmwebeditor/actions/ResetAction.java | 57 +- .../actions/ScmWebEditorCommitAction.java | 19 +- .../actions/ScmWebEditorMainAction.java | 48 +- .../nuiton/scmwebeditor/actions/SearchAction.java | 7 +- .../nuiton/scmwebeditor/actions/UploadAction.java | 223 +------ 13 files changed, 731 insertions(+), 858 deletions(-) delete mode 100644 src/main/java/org/nuiton/scmwebeditor/ScmConnectionInterface.java -- To stop receiving notification emails like this one, please contact nuiton.org SCM administrator <admin+scm@nuiton.org>.
This is an automated email from the git hooks/post-receive script. New commit to branch feature/GIT in repository scmwebeditor. See http://git.nuiton.org/scmwebeditor.git commit bf4a193f766d7fe0b7ba85bf6972ddba61f7805c Author: InternationalKoder <international_koder@yahoo.com> Date: Thu Apr 23 17:33:17 2015 +0200 Now using an interface instead of the abstract class for the SCMs (ScmConnection) and the local Git repositories are stored in a folder which the name is a hash of their URL --- .../org/nuiton/scmwebeditor/GitConnection.java | 301 +++++---- .../org/nuiton/scmwebeditor/ScmConnection.java | 117 ++-- .../nuiton/scmwebeditor/ScmConnectionFactory.java | 2 +- .../scmwebeditor/ScmConnectionInterface.java | 84 --- .../java/org/nuiton/scmwebeditor/ScmSession.java | 2 +- .../scmwebeditor/ScmWebEditorBaseAction.java | 35 +- .../org/nuiton/scmwebeditor/SvnConnection.java | 677 +++++++++++++-------- .../nuiton/scmwebeditor/actions/LogoutAction.java | 17 +- .../nuiton/scmwebeditor/actions/ResetAction.java | 57 +- .../actions/ScmWebEditorCommitAction.java | 19 +- .../actions/ScmWebEditorMainAction.java | 48 +- .../nuiton/scmwebeditor/actions/SearchAction.java | 7 +- .../nuiton/scmwebeditor/actions/UploadAction.java | 223 +------ 13 files changed, 731 insertions(+), 858 deletions(-) diff --git a/src/main/java/org/nuiton/scmwebeditor/GitConnection.java b/src/main/java/org/nuiton/scmwebeditor/GitConnection.java index 6299fac..184f432 100644 --- a/src/main/java/org/nuiton/scmwebeditor/GitConnection.java +++ b/src/main/java/org/nuiton/scmwebeditor/GitConnection.java @@ -22,9 +22,11 @@ package org.nuiton.scmwebeditor; import com.google.common.collect.Lists; +import com.jcraft.jsch.HASH; import org.apache.commons.io.FileUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.apache.shiro.crypto.hash.Hash; import org.eclipse.jgit.api.*; import org.eclipse.jgit.api.errors.GitAPIException; import org.eclipse.jgit.diff.DiffEntry; @@ -37,15 +39,16 @@ import org.eclipse.jgit.revwalk.RevTree; import org.eclipse.jgit.revwalk.RevWalk; import org.eclipse.jgit.storage.file.FileRepositoryBuilder; import org.eclipse.jgit.treewalk.TreeWalk; -import org.nuiton.scmwebeditor.actions.ScmWebEditorCommitAction; -import org.nuiton.scmwebeditor.actions.SearchAction; +import org.nuiton.scmwebeditor.actions.*; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; import java.util.*; -public class GitConnection extends ScmConnection { +public class GitConnection implements ScmConnection { private static final Log log = LogFactory.getLog(GitConnection.class); @@ -55,37 +58,54 @@ public class GitConnection extends ScmConnection { /** the local directory where the repository has been cloned to */ protected File localDirectory; - protected static final String MASTER_BRANCH = "master"; - - public Repository getGitRepo() { return gitRepo; } - - public String getScmPath() { return scmPath; } - - public File getLocalDirectory() { return localDirectory; } + /** path to the directory of the file to edit */ + protected String addressGit; - @Override - public String getFullFileName() { - - String fullFileName = localDirectory + "/" + fileName; - return fullFileName; - } + /** the name of the file to edit */ + protected String fileName; + protected static final String MASTER_BRANCH = "master"; public GitConnection(String address, String sessionId) throws IOException { - super(address.substring(0, address.indexOf(".git") + 4)); - if(log.isDebugEnabled()) { log.debug("Git repository"); } - scmPath = addressScm; - fileName = address.substring(address.indexOf(".git") + 4); + if (address.contains(".git")) { + addressGit = address.substring(0, address.indexOf(".git") + 4); + fileName = address.substring(address.indexOf(".git") + 4); + } else { + addressGit = address; + fileName = address.substring(address.lastIndexOf("/") + 1); + } // Cloning the remote repository to a local directory String localReposPath = ScmWebEditorConfig.getLocalRepositoriesPath(); - localDirectory = new File(localReposPath + "/" + sessionId); + String hashedAddress = addressGit; + + try { + MessageDigest md = MessageDigest.getInstance("SHA-512"); + md.update(addressGit.getBytes()); + byte byteData[] = md.digest(); + + StringBuffer sb = new StringBuffer(); + for (int i = 0 ; i < byteData.length ; i++) { + String hexByte = Integer.toString((byteData[i] & 0xff) + 0x100, 16); + hexByte = hexByte.substring(1); + sb.append(hexByte); + } + + hashedAddress = sb.toString(); + if (log.isDebugEnabled()) { + log.debug("hashed address " + addressGit + " : " + hashedAddress); + } + } catch (NoSuchAlgorithmException e) { + log.error("Can not hash the repository address : the algorithm does not exist", e); + } + + localDirectory = new File(localReposPath + "/" + sessionId + "/" + hashedAddress); // We don't need to clone the repository if we already have the last version boolean cloneNeeded = false; @@ -114,14 +134,14 @@ public class GitConnection extends ScmConnection { } } catch (GitAPIException e) { if (log.isErrorEnabled()) { - log.error("The repository at address " + addressScm + " doesn't exist", e); + log.error("The repository at address " + addressGit + " doesn't exist", e); } } } if (cloneNeeded) { CloneCommand clone = Git.cloneRepository(); - clone.setURI(addressScm); + clone.setURI(addressGit); clone.setDirectory(localDirectory); try { clone.call(); @@ -141,7 +161,7 @@ public class GitConnection extends ScmConnection { if (!gitRepo.getObjectDatabase().exists()) { if (log.isErrorEnabled()) { - log.error("The repository at address " + addressScm + " doesn't exist"); + log.error("The repository at address " + addressGit + " doesn't exist"); } } } @@ -266,112 +286,14 @@ public class GitConnection extends ScmConnection { } - /** - * Gives a list of a remote repository's branches - * @param address the URL to the remote repository - * @return a list of the repository's branches - */ - public static List<String> getBranches(String address) { - - List<String> branches = new ArrayList<String>(); - - LsRemoteCommand lsRemote = new LsRemoteCommand(null); - lsRemote.setRemote(address); - lsRemote.setTags(false); - lsRemote.setHeads(true); - - try { - Collection<Ref> lsRemoteResult = lsRemote.call(); - branches = new ArrayList<String>(); - - for (Ref branch : lsRemoteResult) { - - // we only take the name of the branch, not "refs/heads/" - String name = branch.getName(); - name = name.substring(name.indexOf("/") + 1); - name = name.substring(name.indexOf("/") + 1); - - branches.add(name); - } - } catch (GitAPIException e) { - log.error("The repository at address " + address + " doesn't exist", e); - } - - return branches; - } - - - @Override - public String getUUID() { - return null; - } - - - @Override - public String getHeadRevision(String login, String password) throws IOException { - - String dir = localDirectory.getAbsolutePath(); - File fileToEdit = new File(dir + "/" + fileName); - - String origText = FileUtils.readFileToString(fileToEdit); - - return origText; - } - @Override - public String getHeadNumberRevision(String login, String password) throws IOException, GitAPIException { - - Git git = Git.open(localDirectory); - DescribeCommand describeCommand = git.describe(); - - String headRevision = describeCommand.call(); - - return headRevision; - } - - - /** - * Changing for another branch - * @param branchName the new branch's name - * @throws IOException if reaching the repository is not possible - */ - public void changeBranch(String branchName) throws IOException { - - Git git = Git.open(localDirectory); - - CheckoutCommand checkout = git.checkout(); - checkout.setCreateBranch(true); - checkout.setName(branchName); - checkout.setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK); - checkout.setStartPoint("origin/" + branchName); - - try { - checkout.call(); - } catch (GitAPIException e) { - - // if we could not create a new local branch, it may be because it already exists - checkout = git.checkout(); - checkout.setName(branchName); - - try { - checkout.call(); - } catch (GitAPIException e1) { - if (log.isErrorEnabled()) { - log.error("Can not checkout branch " + branchName, e1); - } - } - } - } - - @Override - public String commit(ScmWebEditorCommitAction action, String login, String password, - String address, String commitMessage) { + public String commit(ScmWebEditorCommitAction action) { if (log.isDebugEnabled()) { log.debug("Entering Git commit"); } - File localFile = new File(getFullFileName()); + File localFile = new File(localDirectory.getAbsolutePath() + "/" + fileName); action.setLastText(action.getNewText()); @@ -433,7 +355,7 @@ public class GitConnection extends ScmConnection { // push PushCommand push = git.push(); - push.setRemote(addressScm); + push.setRemote(addressGit); try { push.call(); @@ -454,4 +376,133 @@ public class GitConnection extends ScmConnection { return action.SUCCESS; } + + @Override + public String uploadFile(UploadAction action) { + return action.ERROR; + } + + + @Override + public String getFileContent(String path, String username, String password) { + + File fileToEdit = new File(localDirectory.getAbsolutePath() + "/" + fileName); + + String origText = null; + try { + origText = FileUtils.readFileToString(fileToEdit); + } catch (IOException e) { + if (log.isErrorEnabled()) { + log.error("Can not read file " + fileToEdit, e); + } + } + + return origText; + } + + @Override + public String getHeadRevisionNumber(String path, String username, String password) { + + Git git = null; + + try { + git = Git.open(localDirectory); + } catch (IOException e) { + if (log.isErrorEnabled()) { + log.error("Can not open local directory " + localDirectory.getAbsolutePath(), e); + } + } + + DescribeCommand describeCommand = git.describe(); + + String headRevision = null; + try { + headRevision = describeCommand.call(); + } catch (GitAPIException e) { + if (log.isErrorEnabled()) { + log.error("Can not use the Git describe command", e); + } + } + + return headRevision; + } + + @Override + public String getRepositoryId() { + return addressGit; + } + + @Override + public String getFileName() { + return fileName; + } + + + /** + * Gives a list of a remote repository's branches + * @param address the URL to the remote repository + * @return a list of the repository's branches + */ + public static List<String> getBranches(String address) { + + List<String> branches = new ArrayList<String>(); + + LsRemoteCommand lsRemote = new LsRemoteCommand(null); + lsRemote.setRemote(address); + lsRemote.setTags(false); + lsRemote.setHeads(true); + + try { + Collection<Ref> lsRemoteResult = lsRemote.call(); + branches = new ArrayList<String>(); + + for (Ref branch : lsRemoteResult) { + + // we only take the name of the branch, not "refs/heads/" + String name = branch.getName(); + name = name.substring(name.indexOf("/") + 1); + name = name.substring(name.indexOf("/") + 1); + + branches.add(name); + } + } catch (GitAPIException e) { + log.error("The repository at address " + address + " doesn't exist", e); + } + + return branches; + } + + + /** + * Changing for another branch + * @param branchName the new branch's name + * @throws IOException if reaching the repository is not possible + */ + public void changeBranch(String branchName) throws IOException { + + Git git = Git.open(localDirectory); + + CheckoutCommand checkout = git.checkout(); + checkout.setCreateBranch(true); + checkout.setName(branchName); + checkout.setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK); + checkout.setStartPoint("origin/" + branchName); + + try { + checkout.call(); + } catch (GitAPIException e) { + + // if we could not create a new local branch, it may be because it already exists + checkout = git.checkout(); + checkout.setName(branchName); + + try { + checkout.call(); + } catch (GitAPIException e1) { + if (log.isErrorEnabled()) { + log.error("Can not checkout branch " + branchName, e1); + } + } + } + } } diff --git a/src/main/java/org/nuiton/scmwebeditor/ScmConnection.java b/src/main/java/org/nuiton/scmwebeditor/ScmConnection.java index c454f05..5134f03 100644 --- a/src/main/java/org/nuiton/scmwebeditor/ScmConnection.java +++ b/src/main/java/org/nuiton/scmwebeditor/ScmConnection.java @@ -1,88 +1,81 @@ -/* - * #%L - * ScmWebEditor - * %% - * Copyright (C) 2009 - 2014 CodeLutin - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public - * License along with this program. If not, see - * <http://www.gnu.org/licenses/lgpl-3.0.html>. - * #L% - */ package org.nuiton.scmwebeditor; -import org.nuiton.scmwebeditor.actions.ScmWebEditorCommitAction; -import org.nuiton.scmwebeditor.actions.SearchAction; - -public abstract class ScmConnection { - - /** full scm path */ - protected String addressScm; - - /** name of the file to edit */ - protected String fileName; +import org.nuiton.scmwebeditor.actions.*; - /** path to the file to edit's directory */ - protected String scmPath; +import javax.naming.AuthenticationException; - public String getFileName() { return fileName; } - - /** the full name which can be used to open the file */ - public abstract String getFullFileName(); +/** + * An interface which the SCM classes have to implement + */ +public interface ScmConnection { + /** + * Searches the repository's files to make a list of them + * @param action the SearchAction which contains the parameters (repository's address, username, password...) + * and which will contain the result (a list of TreeNode for example) + * @return SearchAction.AUTH_ERROR if there has been a problem during the authentication + * SearchAction.ROOT if the result is the root of the repository + * SearchAction.SUCCESS otherwise + */ + public String search(SearchAction action); - public ScmConnection(String address) { - addressScm = address; - } + /** + * Makes a commit of the changed made to the edited file + * @param action the ScmWebEditorCommitAction which contains the parameters (repository's address, username, + * password, new file content...) and which will contain the result (new revision number, + * information message...) + * @return ScmWebEditorCommitAction.ERROR if an error occurred + * ScmWebEditorCommitAction.LOGIN if the authentication failed + * ScmWebEditorCommitAction.ERROR_PATH if it is impossible to reach the repository + * ScmWebEditorCommitAction.FILE_MODIFY if the local file has been modified by another program + * ScmWebEditorCommitAction.SUCCESS if the commit has been done without any problem + */ + public String commit(ScmWebEditorCommitAction action); /** - * Makes a list of the repository's files - * @param action the action which was called for the search - * @return a code which will be interpreted in struts.xml + * Uploads a file to the repository as a new file + * @param action the UploadAction which contains the parameters (repository's address, file to upload...) + * @return UploadAction.ERROR if an error occurred + * UploadAction.REDIRECT if there is no file to upload + * UploadAction.LOGIN if the authentication failed + * UploadAction.SUCCESS if the upload has been done without any problem */ - public abstract String search(SearchAction action); + public String uploadFile(UploadAction action); + /** - * @return the repository's UUID or null if the repository doesn't have one + * Gives the content of a file as a String + * @param path the path to the file to get the content from + * @param username the user's login for the SCM + * @param password the user's password for the SCM + * @return a String which contains the file's content */ - public abstract String getUUID(); + public String getFileContent(String path, String username, String password) throws AuthenticationException; - public abstract String getScmPath(); /** - * Gives the edited file's content for its head revision - * @param login the username to login with - * @param password the password to login with - * @return the edited file's content in a String - * @throws Exception when an error occurs while getting the content + * Gives the number of the head revision + * @param path the path to the SCM + * @param username the user's login for the SCM + * @param password the user's password for the SCM + * @return a String which contains the head revision's number */ - public abstract String getHeadRevision(String login, String password) throws Exception; + public String getHeadRevisionNumber(String path, String username, String password) throws AuthenticationException; + /** - * @param login the username to login with - * @param password the password to login with - * @return the number of the head revision + * Gives the repository's unique identifier + * @return the repository's unique identifier */ - public abstract String getHeadNumberRevision(String login, String password) throws Exception; + public String getRepositoryId(); + /** - * commits the changes - * @param action the action which was called for the commit - * @return the return code + * Gives the name of the edited file + * @return the name of the edited file */ - public abstract String commit(ScmWebEditorCommitAction action, String login, String password, - String address, String commitMessage); + public String getFileName(); } diff --git a/src/main/java/org/nuiton/scmwebeditor/ScmConnectionFactory.java b/src/main/java/org/nuiton/scmwebeditor/ScmConnectionFactory.java index d1c5937..ab370cb 100644 --- a/src/main/java/org/nuiton/scmwebeditor/ScmConnectionFactory.java +++ b/src/main/java/org/nuiton/scmwebeditor/ScmConnectionFactory.java @@ -23,7 +23,7 @@ public class ScmConnectionFactory { ScmConnection scmConn = null; - if(address.endsWith(".git")) { + if(address.contains(".git")) { try { scmConn = new GitConnection(address, sessionId); diff --git a/src/main/java/org/nuiton/scmwebeditor/ScmConnectionInterface.java b/src/main/java/org/nuiton/scmwebeditor/ScmConnectionInterface.java deleted file mode 100644 index 50bf2c2..0000000 --- a/src/main/java/org/nuiton/scmwebeditor/ScmConnectionInterface.java +++ /dev/null @@ -1,84 +0,0 @@ -package org.nuiton.scmwebeditor; - -import org.nuiton.scmwebeditor.actions.*; - -/** - * An interface which the SCM classes have to implement - */ -public interface ScmConnectionInterface { - - /** - * Searches the repository's files to make a list of them - * @param action the SearchAction which contains the parameters (repository's address, username, password...) - * and which will contain the result (a list of TreeNode for example) - * @return SearchAction.AUTH_ERROR if there has been a problem during the authentication - * SearchAction.ROOT if the result is the root of the repository - * SearchAction.SUCCESS otherwise - */ - public String search(SearchAction action); - - - /** - * Makes a commit of the changed made to the edited file - * @param action the ScmWebEditorCommitAction which contains the parameters (repository's address, username, - * password, new file content...) and which will contain the result (new revision number, - * information message...) - * @return ScmWebEditorCommitAction.ERROR if an error occurred - * ScmWebEditorCommitAction.LOGIN if the authentication failed - * ScmWebEditorCommitAction.ERROR_PATH if it is impossible to reach the repository - * ScmWebEditorCommitAction.FILE_MODIFY if the local file has been modified by another program - * ScmWebEditorCommitAction.SUCCESS if the commit has been done without any problem - */ - public String commit(ScmWebEditorCommitAction action); - - - /** - * Cancels all the changes made on the edited file by getting its last revision - * @param action the ResetAction which contains the parameters (repository's address, username, password...) - * and which will contain the result (the last revision's content and the revision number) - * @return ResetAction.AUTH_ERROR if the authentication failed - * ResetAction.ERROR_PATH if it is impossible to reach the repository - * ResetAction.SUCCESS if the reset has been done without any problem - */ - public String reset(ResetAction action); - - - /** - * Logs the user out of the current SCM - * @param action the LogoutAction which contains the parameters (repository's address, servlet request, SCM session...) - * @return LogoutAction.SUCCESS if the logout has been done without any problem - */ - public String logout(LogoutAction action); - - - /** - * Uploads a file to the repository as a new file - * @param action the UploadAction which contains the parameters (repository's address, file to upload...) - * @return UploadAction.ERROR if an error occurred - * UploadAction.REDIRECT if there is no file to upload - * UploadAction.LOGIN if the authentication failed - * UploadAction.SUCCESS if the upload has been done without any problem - */ - public String uploadFile(UploadAction action); - - - /** - * Gives the content of a file as a String - * @param path the path to the file to get the content from - * @param username the user's login for the SCM - * @param password the user's password for the SCM - * @return a String which contains the file's content - */ - public String getFileContent(String path, String username, String password); - - - /** - * Gives the number of the head revision - * @param path the path to the SCM - * @param username the user's login for the SCM - * @param password the user's password for the SCM - * @return a String which contains the head revision's number - */ - public String getHeadRevisionNumber(String path, String username, String password); - -} diff --git a/src/main/java/org/nuiton/scmwebeditor/ScmSession.java b/src/main/java/org/nuiton/scmwebeditor/ScmSession.java index cd9773f..947ed96 100644 --- a/src/main/java/org/nuiton/scmwebeditor/ScmSession.java +++ b/src/main/java/org/nuiton/scmwebeditor/ScmSession.java @@ -64,7 +64,7 @@ public class ScmSession { public String getUUID(String address) { SvnConnection svnConnection = new SvnConnection(address); - return svnConnection.getUUID(); + return svnConnection.getRepositoryId(); } diff --git a/src/main/java/org/nuiton/scmwebeditor/ScmWebEditorBaseAction.java b/src/main/java/org/nuiton/scmwebeditor/ScmWebEditorBaseAction.java index 970942b..ebd0359 100644 --- a/src/main/java/org/nuiton/scmwebeditor/ScmWebEditorBaseAction.java +++ b/src/main/java/org/nuiton/scmwebeditor/ScmWebEditorBaseAction.java @@ -223,38 +223,6 @@ public class ScmWebEditorBaseAction extends BaseAction implements ServletRequest } } - - /** - * @param address - * @param login - * @param password - * @return - * @throws SVNException - * @throws IllegalArgumentException - */ - public String getHeadRevision(String address, String login, String password) throws Exception, IllegalArgumentException { - - HttpSession session = request.getSession(); - String sessionId = session.getId(); - ScmConnection scmConn = ScmConnectionFactory.createScmConnection(address, sessionId); - - String lastRevision = scmConn.getHeadRevision(login, password); - - return lastRevision; - } - - - public String getHeadNumberRevision(String address, String login, String password) throws Exception { - - HttpSession session = request.getSession(); - String sessionId = session.getId(); - ScmConnection scmConn = ScmConnectionFactory.createScmConnection(address, sessionId); - - String headRevision = scmConn.getHeadNumberRevision(login, password); - - return headRevision; - } - public String getHeadcommiter(String address, String login, String password) throws SVNException { ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager(login, password); @@ -299,7 +267,7 @@ public class ScmWebEditorBaseAction extends BaseAction implements ServletRequest parser.parse(is, contenthandler, metadata); result = metadata.get(Metadata.CONTENT_TYPE); if (log.isDebugEnabled()) { - log.debug("Mine type of " + file.getName() + " is : " + result); + log.debug("Mime type of " + file.getName() + " is : " + result); } } catch (SAXException e) { @@ -317,6 +285,7 @@ public class ScmWebEditorBaseAction extends BaseAction implements ServletRequest } protected String getMimeType(String content, String filename) throws IOException { + InputStream is = new ByteArrayInputStream(content.getBytes()); String result = null; try { diff --git a/src/main/java/org/nuiton/scmwebeditor/SvnConnection.java b/src/main/java/org/nuiton/scmwebeditor/SvnConnection.java index b694338..db53321 100644 --- a/src/main/java/org/nuiton/scmwebeditor/SvnConnection.java +++ b/src/main/java/org/nuiton/scmwebeditor/SvnConnection.java @@ -27,6 +27,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.nuiton.scmwebeditor.actions.ScmWebEditorCommitAction; import org.nuiton.scmwebeditor.actions.SearchAction; +import org.nuiton.scmwebeditor.actions.UploadAction; import org.nuiton.util.FileUtil; import org.tmatesoft.svn.core.*; import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager; @@ -36,16 +37,26 @@ import org.tmatesoft.svn.core.io.SVNRepository; import org.tmatesoft.svn.core.io.SVNRepositoryFactory; import org.tmatesoft.svn.core.wc.*; +import javax.naming.AuthenticationException; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; -public class SvnConnection extends ScmConnection { +public class SvnConnection implements ScmConnection { private static final Log log = LogFactory.getLog(SvnConnection.class); + /** full svn path */ + protected String addressSvn; + + /** svn path without fileName */ + protected String svnPath; + + /** fileName of modif file */ + protected String fileName; + /** Temp directory for checkout */ protected File checkoutdir; @@ -63,73 +74,21 @@ public class SvnConnection extends ScmConnection { */ protected SVNClientManager manager; - - protected String repositoryId; - - - public ISVNAuthenticationManager getAuthManager() { - return authManager; - } - - public void setAuthManager(ISVNAuthenticationManager authManager) { - this.authManager = authManager; - } - - public File getCheckoutdir() { - return checkoutdir; - } - - public void createCheckoutdir() throws IOException { - checkoutdir = FileUtil.createTempDirectory("scm_", ""); - } - - public void setCheckoutdir(File checkoutdir) { this.checkoutdir = checkoutdir; } - - public SVNClientManager getManager() { - return manager; - } - - public void setManager(SVNClientManager manager) { - this.manager = manager; - } - - public SVNURL getRemoteUrl() { - return remoteUrl; - } - - public void setRemoteUrl(SVNURL remoteUrl) { - this.remoteUrl = remoteUrl; - } - - public DefaultSVNOptions getSvnOption() { - return svnOption; - } - - public void setSvnOption(DefaultSVNOptions svnOption) { this.svnOption = svnOption; } - - public String getScmPath() { return addressScm.substring(0, addressScm.lastIndexOf("/")); } - - public String getRepositoryId() { return repositoryId; } - - public void setRepositoryId(String repositoryId) { this.repositoryId = repositoryId; } - - @Override - public String getFullFileName() { return fileName; } + public File getCheckoutdir() { return checkoutdir; } public SvnConnection(String address) throws StringIndexOutOfBoundsException { - super(address); - if(log.isDebugEnabled()) { log.debug("SVN repository"); } - scmPath = address.substring(0, address.lastIndexOf("/")); + svnPath = address.substring(0, address.lastIndexOf("/")); fileName = address.substring(address.lastIndexOf("/") + 1); + addressSvn = address; try { - remoteUrl = SVNURL.parseURIEncoded(scmPath); + remoteUrl = SVNURL.parseURIEncoded(svnPath); } catch (SVNException e) { if (log.isErrorEnabled()) { log.error("Can't parse svnPath", e); @@ -142,107 +101,6 @@ public class SvnConnection extends ScmConnection { manager = SVNClientManager.newInstance(svnOption, authManager); } - public void updateAuthentication(String login, String password) { - setAuthManager(SVNWCUtil.createDefaultAuthenticationManager(login, password)); - setManager(SVNClientManager.newInstance(getSvnOption(), getAuthManager())); - } - - /** @return */ - public String getUUID() { - String repositoryUUID; - try { - SVNRepository repository = SVNRepositoryFactory.create(SVNURL.parseURIDecoded(addressScm)); - ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager(); - repository.setAuthenticationManager(authManager); - - repositoryUUID = repository.getRepositoryUUID(true); - } catch (SVNException e) { - if (log.isDebugEnabled()) { - log.debug("Can't get UUID"); - } - return null; - } - - return repositoryUUID; - } - - /** @return */ - public String getSvnRoot() { - String repositoryRoot; - try { - SVNRepository repository = SVNRepositoryFactory.create(SVNURL.parseURIDecoded(addressScm)); - ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager(); - repository.setAuthenticationManager(authManager); - - repositoryRoot = repository.getRepositoryRoot(true).toString(); - } catch (SVNException e) { - if (log.isDebugEnabled()) { - log.debug("Can't get SvnRoot"); - } - return null; - } - - return repositoryRoot; - } - - - public void testConnection() throws SVNException { - SVNRepository repository = SVNRepositoryFactory.create(SVNURL.parseURIDecoded(addressScm)); - repository.setAuthenticationManager(authManager); - - - repository.testConnection(); - - - } - - - public boolean isDifferent(String text) throws IOException { - File pathToFile = new File(checkoutdir, getFileName()); - - SVNDiffClient diffClient = new SVNDiffClient(getManager(), getSvnOption()); - - FileUtils.writeStringToFile(pathToFile, text, "UTF-8"); - - ByteArrayOutputStream diff = new ByteArrayOutputStream(); - - try { - diffClient.doDiff(pathToFile, SVNRevision.UNDEFINED, SVNRevision.WORKING, SVNRevision.HEAD, SVNDepth.INFINITY, true, diff, null); - } catch (SVNException e) { - log.error("Diff fail", e); - return false; - } - - if (diff.size() > 0) { - return true; - } else { - return false; - } - - - } - - public ByteArrayOutputStream getDiff(String text) throws IOException { - File pathToFile = new File(checkoutdir, getFileName()); - - SVNDiffClient diffClient = new SVNDiffClient(getManager(), getSvnOption()); - - FileUtils.writeStringToFile(pathToFile, text, "UTF-8"); - - ByteArrayOutputStream diff = new ByteArrayOutputStream(); - - try { - diffClient.doDiff(pathToFile, SVNRevision.UNDEFINED, SVNRevision.WORKING, SVNRevision.HEAD, SVNDepth.INFINITY, true, diff, null); - } catch (SVNException e) { - log.error("Diff fail", e); - } - - - return diff; - - - } - @Override public String search(SearchAction action) { @@ -355,77 +213,9 @@ public class SvnConnection extends ScmConnection { @Override - public String getHeadRevision(String login, String password) throws SVNException { - - String lastRevision; - - String url = getScmPath(); - String file = getFileName(); - - SVNRepository repository = SVNRepositoryFactory.create(SVNURL.parseURIEncoded(url)); - ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager(login, password); - repository.setAuthenticationManager(authManager); - - SVNNodeKind nodeKind = repository.checkPath(file, -1); - - - if (nodeKind == SVNNodeKind.NONE) { - if (log.isErrorEnabled()) { - log.error("There is no entry at '" + url + "'."); - } - throw new IllegalArgumentException("There is no entry at '" + url + "'."); - } else if (nodeKind == SVNNodeKind.DIR) { - if (log.isErrorEnabled()) { - log.error("The entry at '" + url + "' is a file while a directory was expected."); - } - throw new IllegalArgumentException("The entry at '" + url + "' is a file while a directory was expected."); - } - - - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - - SVNProperties fileProperties = new SVNProperties(); - - repository.getFile(file, -1, fileProperties, baos); - - fileProperties.getStringValue(SVNProperty.REVISION); - - - lastRevision = baos.toString(); - - try { - baos.close(); - } catch (IOException e) { - if (log.isDebugEnabled()) { - log.debug("Can't close stream", e); - } - } - - return lastRevision; - } - - - @Override - public String getHeadNumberRevision(String login, String password) throws SVNException { - - ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager(login, password); - - DefaultSVNOptions svnOption = new DefaultSVNOptions(); - svnOption.setPropertyValue(SVNProperty.EOL_STYLE, SVNProperty.EOL_STYLE_LF); - - SVNWCClient wcClient = new SVNWCClient(authManager, svnOption); - - SVNInfo info = wcClient.doInfo(SVNURL.parseURIEncoded(addressScm), SVNRevision.HEAD, SVNRevision.HEAD); - - String headRevision = info.getRevision().toString(); - - return headRevision; - } + public String commit(ScmWebEditorCommitAction action) { - - @Override - public String commit(ScmWebEditorCommitAction action, String login, String password, - String address, String commitMessage) { + updateAuthentication(action.getUsername(), action.getPw()); if (log.isDebugEnabled()) { log.debug("Entering SVN commit"); @@ -445,18 +235,18 @@ public class SvnConnection extends ScmConnection { try { checkout(checkoutdir); } catch (SVNAuthenticationException authexep) { - action.getRequest().setAttribute(action.PARAMETER_ADDRESS, address); + action.getRequest().setAttribute(action.PARAMETER_ADDRESS, action.getAddress()); // if svn authentication failed user is redirected on login page if (log.isDebugEnabled()) { - log.debug("Private SCM on reading " + getRemoteUrl()); + log.debug("Private SCM on reading " + remoteUrl); } // we delete the temporary directory delTempDirectory(checkoutdir); action.setUsername(null); action.setPw(null); - action.getScmSession().delScmUser(getUUID()); + action.getScmSession().delScmUser(getRepositoryId()); return action.LOGIN; @@ -470,7 +260,7 @@ public class SvnConnection extends ScmConnection { } - File checkOutFile = new File(checkoutdir, getFileName()); + File checkOutFile = new File(checkoutdir, fileName); action.setLastText(action.getNewText()); @@ -510,7 +300,7 @@ public class SvnConnection extends ScmConnection { action.setDiff(diff.substring(diff.indexOf("@@"))); delTempDirectory(checkoutdir); try { - action.setHeadCommiter(action.getHeadcommiter(address, login, password)); + action.setHeadCommiter(action.getHeadcommiter(action.getAddress(), action.getUsername(), action.getPw())); } catch (SVNException e) { log.error("Can't get head commiter", e); } @@ -552,7 +342,7 @@ public class SvnConnection extends ScmConnection { if (log.isDebugEnabled()) { log.debug("Try to commit"); } - commitClient.doCommit(tabFile, false, "From scmwebeditor -- " + commitMessage, null, null, false, false, SVNDepth.FILES); + commitClient.doCommit(tabFile, false, "From scmwebeditor -- " + action.getCommitMessage(), null, null, false, false, SVNDepth.FILES); } catch (SVNAuthenticationException authexep) { if (log.isErrorEnabled()) { log.error("AUTH FAIL", authexep); @@ -568,7 +358,7 @@ public class SvnConnection extends ScmConnection { action.setUsername(null); action.setPw(null); // deleting the session value - action.getScmSession().delScmUser(getUUID()); + action.getScmSession().delScmUser(getRepositoryId()); return action.LOGIN; } catch (SVNException e) { if (log.isErrorEnabled()) { @@ -592,22 +382,418 @@ public class SvnConnection extends ScmConnection { delTempDirectory(checkoutdir); if (log.isInfoEnabled()) { - log.info(login + " with IP " + action.getRequest().getRemoteAddr() + " commit the file " - + address + " with message : " + commitMessage); + log.info(action.getUsername() + " with IP " + action.getRequest().getRemoteAddr() + " commit the file " + + action.getAddress() + " with message : " + action.getCommitMessage()); + } + + try { + action.setNumRevision(getHeadRevisionNumber(action.getAddress(), action.getUsername(), action.getPw())); + } catch (AuthenticationException e) { + if (log.isErrorEnabled()) { + log.error("Auth fail", e); + } + } + + return action.SUCCESS; + } + + + @Override + public String uploadFile(UploadAction action) { + + action.setSvnRoot(getSvnRoot()); + action.setFileRoot(svnPath); + + + if (action.getSvnRoot() == null) { + action.setSvnRoot(action.getFileRoot()); + } + + + //Si le repo n'est pas protege en ecriture on recupere sont UUID + String repositoryUUID = getRepositoryId(); + if (repositoryUUID == null) { + repositoryUUID = action.getAddress(); + } + + + if (action.getUsername() == null && action.getPw() == null) { + if (action.getScmSession().getUsername(repositoryUUID) != null && + action.getScmSession().getPassword(repositoryUUID) != null) { + //On recupère les identifiants en session + action.setUsername(action.getScmSession().getUsername(repositoryUUID)); + action.setPw(action.getScmSession().getPassword(repositoryUUID)); + } else { + action.setUsername(null); + action.setPw(null); + } + } else { + action.getScmSession().addScmUser(repositoryUUID, action.getUsername(), action.getPw()); } + + updateAuthentication(action.getUsername(), action.getPw()); + try { - action.setNumRevision(action.getHeadNumberRevision(address, login, password)); + testConnection(); } catch (SVNException e) { - action.setNumRevision(null); - } catch (Exception e) { if (log.isDebugEnabled()) { - log.debug("Unknown error", e); + log.debug("Test connection fail", e); } - action.setNumRevision(null); + action.getScmSession().delScmUser(repositoryUUID); + action.setUsername(null); + action.setPw(null); } - return action.SUCCESS; + + //Si il n'y a pas de fichier à uploader on retourne sur le formulaire d'upload + if (action.getUpload() == null) { + return UploadAction.REDIRECT; + } + + action.setBadLogin(false); + action.setError(false); + + if (log.isDebugEnabled()) { + log.debug("FileName : " + action.getUploadFileName()); + log.debug("ContentType : " + action.getUploadContentType()); + } + + /* + * Checkout process + */ + SVNUpdateClient upclient = new SVNUpdateClient(manager, svnOption); + + File checkoutDir = null; + try { + createCheckoutdir(); + } catch (IOException e1) { + if (log.isErrorEnabled()) { + log.error("Can't create checkoutDir", e1); + } + action.setError(true); + return UploadAction.ERROR; + } + + + try { + if (log.isDebugEnabled()) { + log.debug("Do Checkout of " + action.getSvnRoot()); + } + upclient.doCheckout(SVNURL.parseURIEncoded(action.getSvnRoot()), checkoutDir, + SVNRevision.HEAD, SVNRevision.HEAD, SVNDepth.INFINITY, false); + } catch (SVNAuthenticationException authexep) { + + // if svn authentication failed user is redirected on login page + if (log.isDebugEnabled()) { + log.debug("Private SCM on reading " + remoteUrl); + } + //On supprime le repertoire temporaire + delTempDirectory(checkoutDir); + //redirect to a login page + action.setError(true); + return UploadAction.ERROR; + + } catch (SVNException e) { + //Suppression du repertoire temporaire + delTempDirectory(checkoutDir); + action.setError(true); + return UploadAction.ERROR; + } + + + //On test si le chemin se termine par un / si non, on l'ajoute + if (!svnPath.endsWith("/")) { + svnPath += "/"; + } + + + //Copy file in checkourdir + + String checkoutPath = checkoutDir.getAbsolutePath(); + + File file = new File(checkoutPath + svnPath.replace(action.getSvnRoot(), ""), action.getUploadFileName()); + + try { + FileUtils.copyFile(action.getUpload(), file); + } catch (IOException e) { + action.setError(true); + if (log.isErrorEnabled()) { + log.error("Can't copy the file to the checkout directory", e); + } + return UploadAction.ERROR; + } + + + //Commit process + + + try { + + if (log.isDebugEnabled()) { + log.debug("leSvnPath : " + file.toString()); + } + + //On ajoute le repertoire + manager.getWCClient().doAdd(file, false, false, false, SVNDepth.EMPTY, false, true); + if (log.isDebugEnabled()) { + log.debug("Add success !"); + } + //On ajoute le fichier aux fichiers versionnés + + } catch (SVNException e) { + if (log.isErrorEnabled()) { + log.error("Erreur SVN Add", e); + } + action.setError(true); + //Suppression du repertoire temporaire + delTempDirectory(checkoutDir); + return UploadAction.ERROR; + } + + File[] checkoutDirTab = new File[1]; + checkoutDirTab[0] = checkoutDir; + + try { + manager.getCommitClient().doCommit(checkoutDirTab, false, "From scmwebeditor -- add the file : " + + action.getUploadFileName(), null, null, false, true, SVNDepth.INFINITY); + + if (log.isDebugEnabled()) { + log.debug("Commit success !"); + } + + } catch (SVNAuthenticationException authexep) { + if (log.isErrorEnabled()) { + log.error("authentification fail"); + } + action.setBadLogin(true); + //Suppression du repertoire temporaire + delTempDirectory(checkoutDir); + action.getScmSession().delScmUser(repositoryUUID); + return UploadAction.LOGIN; + } catch (SVNException e) { + if (log.isErrorEnabled()) { + log.error("Erreur SVN commit", e); + } + action.setError(true); + //Suppression du repertoire temporaire + delTempDirectory(checkoutDir); + return UploadAction.ERROR; + } + + + //Suppression du repertoire temporaire + delTempDirectory(checkoutDir); + + if (log.isDebugEnabled()) { + log.debug("File upload successful"); + } + + if (log.isInfoEnabled()) { + log.info(action.getUsername() + " with IP " + action.getRequest().getRemoteAddr() + " add the file " + + action.getUploadFileName() + " on the repository."); + } + return UploadAction.SUCCESS; + } + + + @Override + public String getFileContent(String path, String username, String password) throws AuthenticationException { + + String url = path.substring(0, path.lastIndexOf("/")); + String file = path.substring(path.lastIndexOf("/") + 1); + + updateAuthentication(username, password); + + String lastRevision = null; + SVNRepository repository = null; + + try { + repository = SVNRepositoryFactory.create(SVNURL.parseURIEncoded(url)); + + ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager(username, password); + repository.setAuthenticationManager(authManager); + + SVNNodeKind nodeKind = repository.checkPath(file, -1); + + + if (nodeKind == SVNNodeKind.NONE) { + if (log.isErrorEnabled()) { + log.error("There is no entry at '" + url + "'."); + } + throw new IllegalArgumentException("There is no entry at '" + url + "'."); + } else if (nodeKind == SVNNodeKind.DIR) { + if (log.isErrorEnabled()) { + log.error("The entry at '" + url + "' is a file while a directory was expected."); + } + throw new IllegalArgumentException("The entry at '" + url + "' is a file while a directory was expected."); + } + + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + + SVNProperties fileProperties = new SVNProperties(); + repository.getFile(file, -1, fileProperties, baos); + fileProperties.getStringValue(SVNProperty.REVISION); + + lastRevision = baos.toString(); + + try { + baos.close(); + } catch (IOException e) { + if (log.isDebugEnabled()) { + log.debug("Can't close stream", e); + } + } + } catch (SVNAuthenticationException e) { + throw new AuthenticationException("Auth fail"); + } catch (SVNException e) { + if (log.isErrorEnabled()) { + log.error("Can not get file content from SVN repository", e); + } + } + + return lastRevision; + } + + + @Override + public String getHeadRevisionNumber(String path, String username, String password) throws AuthenticationException { + + if (log.isDebugEnabled()) { + log.debug("headRevisionNumber expected " + addressSvn + " ; got " + path); + } + + ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager(username, password); + + DefaultSVNOptions svnOption = new DefaultSVNOptions(); + svnOption.setPropertyValue(SVNProperty.EOL_STYLE, SVNProperty.EOL_STYLE_LF); + + SVNWCClient wcClient = new SVNWCClient(authManager, svnOption); + + SVNInfo info = null; + + try { + info = wcClient.doInfo(SVNURL.parseURIEncoded(path), SVNRevision.HEAD, SVNRevision.HEAD); + } catch (SVNAuthenticationException e) { + throw new AuthenticationException("Auth fail"); + } catch (SVNException e) { + if (log.isErrorEnabled()) { + log.error("Can not get info from SVN repository", e); + } + } + + String headRevision = info.getRevision().toString(); + + return headRevision; + } + + + public String getRepositoryId() { + String repositoryUUID; + try { + SVNRepository repository = SVNRepositoryFactory.create(SVNURL.parseURIDecoded(addressSvn)); + ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager(); + repository.setAuthenticationManager(authManager); + + repositoryUUID = repository.getRepositoryUUID(true); + } catch (SVNException e) { + if (log.isDebugEnabled()) { + log.debug("Can't get UUID"); + } + return null; + } + + return repositoryUUID; + } + + + @Override + public String getFileName() { + return fileName; + } + + + public void updateAuthentication(String login, String password) { + authManager = SVNWCUtil.createDefaultAuthenticationManager(login, password); + manager = SVNClientManager.newInstance(svnOption, authManager); + } + + /** @return */ + public String getSvnRoot() { + String repositoryRoot; + try { + SVNRepository repository = SVNRepositoryFactory.create(SVNURL.parseURIDecoded(addressSvn)); + ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager(); + repository.setAuthenticationManager(authManager); + + repositoryRoot = repository.getRepositoryRoot(true).toString(); + } catch (SVNException e) { + if (log.isDebugEnabled()) { + log.debug("Can't get SvnRoot"); + } + return null; + } + + return repositoryRoot; + } + + + public void testConnection() throws SVNException { + SVNRepository repository = SVNRepositoryFactory.create(SVNURL.parseURIDecoded(addressSvn)); + repository.setAuthenticationManager(authManager); + + + repository.testConnection(); + + + } + + + public boolean isDifferent(String text) throws IOException { + File pathToFile = new File(checkoutdir, fileName); + + SVNDiffClient diffClient = new SVNDiffClient(manager, svnOption); + + FileUtils.writeStringToFile(pathToFile, text, "UTF-8"); + + ByteArrayOutputStream diff = new ByteArrayOutputStream(); + + try { + diffClient.doDiff(pathToFile, SVNRevision.UNDEFINED, SVNRevision.WORKING, SVNRevision.HEAD, SVNDepth.INFINITY, true, diff, null); + } catch (SVNException e) { + log.error("Diff fail", e); + return false; + } + + if (diff.size() > 0) { + return true; + } else { + return false; + } + + + } + + public ByteArrayOutputStream getDiff(String text) throws IOException { + File pathToFile = new File(checkoutdir, fileName); + + SVNDiffClient diffClient = new SVNDiffClient(manager, svnOption); + + FileUtils.writeStringToFile(pathToFile, text, "UTF-8"); + + ByteArrayOutputStream diff = new ByteArrayOutputStream(); + + try { + diffClient.doDiff(pathToFile, SVNRevision.UNDEFINED, SVNRevision.WORKING, SVNRevision.HEAD, SVNDepth.INFINITY, true, diff, null); + } catch (SVNException e) { + log.error("Diff fail", e); + } + + + return diff; + + } @@ -655,6 +841,11 @@ public class SvnConnection extends ScmConnection { } + public void createCheckoutdir() throws IOException { + checkoutdir = FileUtil.createTempDirectory("scm_", ""); + } + + /** * Use to delete the checkout temp directory * diff --git a/src/main/java/org/nuiton/scmwebeditor/actions/LogoutAction.java b/src/main/java/org/nuiton/scmwebeditor/actions/LogoutAction.java index 526af94..112ed7d 100644 --- a/src/main/java/org/nuiton/scmwebeditor/actions/LogoutAction.java +++ b/src/main/java/org/nuiton/scmwebeditor/actions/LogoutAction.java @@ -25,6 +25,8 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.struts2.interceptor.ServletRequestAware; import org.apache.struts2.interceptor.ServletResponseAware; +import org.nuiton.scmwebeditor.ScmConnection; +import org.nuiton.scmwebeditor.ScmConnectionFactory; import org.nuiton.scmwebeditor.ScmWebEditorBaseAction; import org.nuiton.scmwebeditor.SvnConnection; @@ -65,17 +67,18 @@ public class LogoutAction extends ScmWebEditorBaseAction implements ServletReque public String execute() { - SvnConnection svnConn = new SvnConnection(address); + String sessionId = request.getSession().getId(); + ScmConnection scmConn = ScmConnectionFactory.createScmConnection(address, sessionId); - //Si le repo n'est pas protege en ecriture on recupere sont UUID - String repositoryUUID = svnConn.getUUID(); - if (repositoryUUID == null) { - repositoryUUID = address; + // getting the repository unique identifier if it is possible + String repositoryId = scmConn.getRepositoryId(); + if (repositoryId == null) { + repositoryId = address; } //suppression des cookies pour ce dépot for (Cookie c : request.getCookies()) { - if (c.getName().equals(repositoryUUID)) { + if (c.getName().equals(repositoryId)) { c.setMaxAge(0);//On supprime le cookie response.addCookie(c); if (log.isDebugEnabled()) { @@ -85,7 +88,7 @@ public class LogoutAction extends ScmWebEditorBaseAction implements ServletReque } //Suppression des identifiants stockés en session - getScmSession().delScmUser(repositoryUUID); + getScmSession().delScmUser(repositoryId); return SUCCESS; } diff --git a/src/main/java/org/nuiton/scmwebeditor/actions/ResetAction.java b/src/main/java/org/nuiton/scmwebeditor/actions/ResetAction.java index e4fc659..6becee3 100644 --- a/src/main/java/org/nuiton/scmwebeditor/actions/ResetAction.java +++ b/src/main/java/org/nuiton/scmwebeditor/actions/ResetAction.java @@ -23,11 +23,16 @@ package org.nuiton.scmwebeditor.actions; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.nuiton.scmwebeditor.ScmConnection; +import org.nuiton.scmwebeditor.ScmConnectionFactory; import org.nuiton.scmwebeditor.ScmWebEditorBaseAction; import org.nuiton.scmwebeditor.SvnConnection; import org.tmatesoft.svn.core.SVNAuthenticationException; import org.tmatesoft.svn.core.SVNException; +import javax.naming.AuthenticationException; +import javax.servlet.http.Cookie; + public class ResetAction extends ScmWebEditorBaseAction { private static final long serialVersionUID = -1154924826535371319L; @@ -73,19 +78,20 @@ public class ResetAction extends ScmWebEditorBaseAction { public String execute() { - SvnConnection svnConn = new SvnConnection(address); + String sessionId = request.getSession().getId(); + ScmConnection scmConn = ScmConnectionFactory.createScmConnection(address, sessionId); - //Si le repo n'est pas protege en ecriture on recupere sont UUID - String repositoryUUID = svnConn.getUUID(); - if (repositoryUUID == null) { - repositoryUUID = address; + // getting the repository unique identifier if it is possible + String repositoryId = scmConn.getRepositoryId(); + if (repositoryId == null) { + repositoryId = address; } if (username == null && pw == null) { - if (getScmSession().getUsername(repositoryUUID) != null && getScmSession().getPassword(repositoryUUID) != null) { - //On recupère les identifiants en session - username = getScmSession().getUsername(repositoryUUID); - pw = getScmSession().getPassword(repositoryUUID); + if (getScmSession().getUsername(repositoryId) != null && getScmSession().getPassword(repositoryId) != null) { + // getting the authntication information from the session + username = getScmSession().getUsername(repositoryId); + pw = getScmSession().getPassword(repositoryId); } else { username = null; pw = null; @@ -93,41 +99,14 @@ public class ResetAction extends ScmWebEditorBaseAction { } try { - - lastRevision = getHeadRevision(address, username, pw); - - numRevision = getHeadNumberRevision(address, username, pw); - - - } catch (SVNAuthenticationException authexep) { + lastRevision = scmConn.getFileContent(address, username, pw); + numRevision = scmConn.getHeadRevisionNumber(address, username, pw); + } catch (AuthenticationException e) { if (log.isErrorEnabled()) { log.error("AUTH FAIL"); } error = AUTH_ERROR; return AUTH_ERROR; - } catch (SVNException e1) { - if (log.isErrorEnabled()) { - log.error("Can't reach the svn repository"); - } - error = ERROR_PATH; - numRevision = null; - return ERROR_PATH; - } catch (StringIndexOutOfBoundsException e) { - if (log.isErrorEnabled()) { - log.error("Can't reach the svn repository"); - } - error = ERROR_PATH; - return ERROR_PATH; - } catch (IllegalArgumentException e) { - if (log.isErrorEnabled()) { - log.error("Problem with file path", e); - } - error = ERROR_PATH; - return ERROR_PATH; - } catch (Exception e) { - if (log.isErrorEnabled()) { - log.error("Unknown error", e); - } } return SUCCESS; diff --git a/src/main/java/org/nuiton/scmwebeditor/actions/ScmWebEditorCommitAction.java b/src/main/java/org/nuiton/scmwebeditor/actions/ScmWebEditorCommitAction.java index 609c994..f0d87be 100644 --- a/src/main/java/org/nuiton/scmwebeditor/actions/ScmWebEditorCommitAction.java +++ b/src/main/java/org/nuiton/scmwebeditor/actions/ScmWebEditorCommitAction.java @@ -29,19 +29,11 @@ import org.apache.struts2.interceptor.ServletRequestAware; import org.apache.struts2.interceptor.ServletResponseAware; import org.nuiton.jrst.JRST; import org.nuiton.scmwebeditor.*; -import org.nuiton.util.FileUtil; -import org.tmatesoft.svn.core.SVNAuthenticationException; -import org.tmatesoft.svn.core.SVNDepth; -import org.tmatesoft.svn.core.SVNException; -import org.tmatesoft.svn.core.wc.SVNCommitClient; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; -import java.io.ByteArrayOutputStream; -import java.io.File; -import java.io.IOException; public class ScmWebEditorCommitAction extends ScmWebEditorBaseAction implements ServletRequestAware, ServletResponseAware { @@ -253,7 +245,7 @@ public class ScmWebEditorCommitAction extends ScmWebEditorBaseAction implements String password = getPw(); // if the repository is not protected for writing, we get its UUID - String repositoryUUID = scmConn.getUUID(); + String repositoryUUID = scmConn.getRepositoryId(); if (repositoryUUID == null) { repositoryUUID = address; } @@ -311,15 +303,12 @@ public class ScmWebEditorCommitAction extends ScmWebEditorBaseAction implements getScmSession().addScmUser(repositoryUUID, login, password); } - - // FIXME gérer l'identification avec Git - if (scmConn instanceof SvnConnection) { - ((SvnConnection) scmConn).updateAuthentication(login, password); - } + username = login; + pw = password; String returnCode; - returnCode = scmConn.commit(this, login, password, address, commitMessage); + returnCode = scmConn.commit(this); return returnCode; } diff --git a/src/main/java/org/nuiton/scmwebeditor/actions/ScmWebEditorMainAction.java b/src/main/java/org/nuiton/scmwebeditor/actions/ScmWebEditorMainAction.java index 1b35aa6..5b4369f 100644 --- a/src/main/java/org/nuiton/scmwebeditor/actions/ScmWebEditorMainAction.java +++ b/src/main/java/org/nuiton/scmwebeditor/actions/ScmWebEditorMainAction.java @@ -31,6 +31,7 @@ import org.nuiton.scmwebeditor.*; import org.tmatesoft.svn.core.SVNAuthenticationException; import org.tmatesoft.svn.core.SVNException; +import javax.naming.AuthenticationException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; @@ -243,10 +244,10 @@ public class ScmWebEditorMainAction extends ScmWebEditorBaseAction implements Se format = scmConn.getFileName().substring(scmConn.getFileName().lastIndexOf(".") + 1); - String originalText; + String originalText = ""; //Si le repo n'est pas protege en ecriture on recupere sont UUID - String repositoryUUID = scmConn.getUUID(); + String repositoryUUID = scmConn.getRepositoryId(); if (repositoryUUID == null) { repositoryUUID = address; } @@ -314,26 +315,20 @@ public class ScmWebEditorMainAction extends ScmWebEditorBaseAction implements Se getScmSession().addScmUser(repositoryUUID, username, pw); } - // FIXME gestion de l'authentification sous Git - if (scmConn instanceof SvnConnection) { - ((SvnConnection) scmConn).updateAuthentication(username, pw); - } - /* * Recuperation du fichier et de sa revision */ - try { - originalText = getHeadRevision(address, username, pw); - numRevision = getHeadNumberRevision(address, username, pw); - - } catch (SVNAuthenticationException authexep) { + try { + originalText = scmConn.getFileContent(address, username, pw); + numRevision = scmConn.getHeadRevisionNumber(address, username, pw); + } catch (AuthenticationException e) { request.setAttribute(PARAMETER_ADDRESS, address); // if svn authentication failed user is redirected on login page if (log.isDebugEnabled()) { - log.debug("Auth Fail ", authexep); + log.debug("Auth Fail ", e); } //suppression des cookies pour ce dépot @@ -350,37 +345,12 @@ public class ScmWebEditorMainAction extends ScmWebEditorBaseAction implements Se getScmSession().delScmUser(repositoryUUID); //redirect to a login page return LOGIN; - - } catch (SVNException e) { - request.setAttribute("projectUrl", projectUrl); - - if (log.isErrorEnabled()) { - log.error("SVN error ", e); - } - return ERROR_PATH; - } catch (IllegalArgumentException e) { - request.setAttribute("projectUrl", projectUrl); - - if (log.isDebugEnabled()) { - log.debug("SVN error debug", e); - } - return ERROR_PATH; - } catch (IOException e) { - if (log.isDebugEnabled()) { - log.debug("Can't read local file", e); - } - return ERROR_PATH; - } catch (Exception e) { - if (log.isDebugEnabled()) { - log.debug("Unknown error", e); - } - return ERROR_PATH; } mimeType = null; try { - mimeType = getMimeType(originalText, scmConn.getFullFileName()); + mimeType = getMimeType(originalText, scmConn.getFileName()); } catch (IOException e) { if (log.isErrorEnabled()) { log.error("Can't get MimeType, problem when reading file", e); diff --git a/src/main/java/org/nuiton/scmwebeditor/actions/SearchAction.java b/src/main/java/org/nuiton/scmwebeditor/actions/SearchAction.java index b0f8dc2..7c05340 100644 --- a/src/main/java/org/nuiton/scmwebeditor/actions/SearchAction.java +++ b/src/main/java/org/nuiton/scmwebeditor/actions/SearchAction.java @@ -24,15 +24,16 @@ package org.nuiton.scmwebeditor.actions; import com.jgeppert.struts2.jquery.tree.result.TreeNode; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.nuiton.scmwebeditor.*; +import org.nuiton.scmwebeditor.GitConnection; +import org.nuiton.scmwebeditor.ScmConnection; +import org.nuiton.scmwebeditor.ScmConnectionFactory; +import org.nuiton.scmwebeditor.ScmWebEditorBaseAction; import org.tmatesoft.svn.core.SVNDirEntry; import org.tmatesoft.svn.core.SVNException; import org.tmatesoft.svn.core.SVNNodeKind; import org.tmatesoft.svn.core.io.SVNRepository; -import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; -import java.io.IOException; import java.util.*; import java.util.Map.Entry; diff --git a/src/main/java/org/nuiton/scmwebeditor/actions/UploadAction.java b/src/main/java/org/nuiton/scmwebeditor/actions/UploadAction.java index e1d1215..1659b83 100644 --- a/src/main/java/org/nuiton/scmwebeditor/actions/UploadAction.java +++ b/src/main/java/org/nuiton/scmwebeditor/actions/UploadAction.java @@ -25,6 +25,8 @@ import org.apache.commons.io.FileUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.struts2.interceptor.ServletRequestAware; +import org.nuiton.scmwebeditor.ScmConnection; +import org.nuiton.scmwebeditor.ScmConnectionFactory; import org.nuiton.scmwebeditor.ScmWebEditorBaseAction; import org.nuiton.scmwebeditor.SvnConnection; import org.tmatesoft.svn.core.SVNAuthenticationException; @@ -143,215 +145,24 @@ public class UploadAction extends ScmWebEditorBaseAction implements ServletReque return fileRoot; } + public void setSvnRoot(String svnRoot) { this.svnRoot = svnRoot; } + + public void setFileRoot(String fileRoot) { this.fileRoot = fileRoot; } + + public void setBadLogin(boolean badLogin) { this.badLogin = badLogin; } + + public void setError(boolean error) { this.error = error; } + + public HttpServletRequest getRequest() { return request; } + public String execute() { - SvnConnection svnConnection = new SvnConnection(address); - svnRoot = svnConnection.getSvnRoot(); - - fileRoot = svnConnection.getScmPath(); - - if (svnRoot == null) { - svnRoot = fileRoot; - } - - - SvnConnection svnConn; - - try { - svnConn = new SvnConnection(address); - } catch (StringIndexOutOfBoundsException e) { - if (log.isErrorEnabled()) { - log.error("error when creating SvnSession in UploadAction", e); - } - error = true; - return ERROR; - } - - - //Si le repo n'est pas protege en ecriture on recupere sont UUID - String repositoryUUID = svnConn.getUUID(); - if (repositoryUUID == null) { - repositoryUUID = address; - } - - - if (username == null && pw == null) { - if (getScmSession().getUsername(repositoryUUID) != null && getScmSession().getPassword(repositoryUUID) != null) { - //On recupère les identifiants en session - username = getScmSession().getUsername(repositoryUUID); - pw = getScmSession().getPassword(repositoryUUID); - } else { - username = null; - pw = null; - } - } else { - getScmSession().addScmUser(repositoryUUID, username, pw); - } - - - svnConn.updateAuthentication(username, pw); + String sessionId = request.getSession().getId(); + ScmConnection scmConn = ScmConnectionFactory.createScmConnection(address, sessionId); + + String returnCode = scmConn.uploadFile(this); - try { - svnConn.testConnection(); - } catch (SVNException e) { - if (log.isDebugEnabled()) { - log.debug("Test connection fail", e); - } - getScmSession().delScmUser(repositoryUUID); - username = null; - pw = null; - } - - - //Si il n'y a pas de fichier à uploader on retourne sur le formulaire d'upload - if (upload == null) { - return REDIRECT; - } - - badLogin = false; - error = false; - - if (log.isDebugEnabled()) { - log.debug("FileName : " + uploadFileName); - log.debug("ContentType : " + uploadContentType); - } - - /* - * Checkout process - */ - SVNUpdateClient upclient = new SVNUpdateClient(svnConn.getManager(), svnConn.getSvnOption()); - - File checkoutDir; - try { - svnConn.createCheckoutdir(); - checkoutDir = svnConn.getCheckoutdir(); - } catch (IOException e1) { - if (log.isErrorEnabled()) { - log.error("Can't create checkoutDir", e1); - } - error = true; - return ERROR; - } - - - try { - if (log.isDebugEnabled()) { - log.debug("Do Checkout of " + svnRoot); - } - upclient.doCheckout(SVNURL.parseURIEncoded(svnRoot), checkoutDir, - SVNRevision.HEAD, SVNRevision.HEAD, SVNDepth.INFINITY, false); - } catch (SVNAuthenticationException authexep) { - - // if svn authentication failed user is redirected on login page - if (log.isDebugEnabled()) { - log.debug("Private SCM on reading " + svnConn.getRemoteUrl()); - } - //On supprime le repertoire temporaire - svnConn.delTempDirectory(checkoutDir); - //redirect to a login page - error = true; - return ERROR; - - } catch (SVNException e) { - //Suppression du repertoire temporaire - svnConn.delTempDirectory(checkoutDir); - error = true; - return ERROR; - } - - - //On test si le chemin se termine par un / si non, on l'ajoute - if (!svnPath.endsWith("/")) { - svnPath += "/"; - } - - - //Copy file in checkourdir - - String checkoutPath = checkoutDir.getAbsolutePath(); - - File file = new File(checkoutPath + svnPath.replace(svnRoot, ""), uploadFileName); - - try { - FileUtils.copyFile(upload, file); - } catch (IOException e) { - error = true; - if (log.isErrorEnabled()) { - log.error("Can't copy the file to the checkout directory", e); - } - return ERROR; - } - - - //Commit process - - SVNClientManager manager = svnConn.getManager(); - - - try { - - if (log.isDebugEnabled()) { - log.debug("leSvnPath : " + file.toString()); - } - - //On ajoute le repertoire - manager.getWCClient().doAdd(file, false, false, false, SVNDepth.EMPTY, false, true); - if (log.isDebugEnabled()) { - log.debug("Add success !"); - } - //On ajoute le fichier aux fichiers versionnés - - } catch (SVNException e) { - if (log.isErrorEnabled()) { - log.error("Erreur SVN Add", e); - } - error = true; - //Suppression du repertoire temporaire - svnConn.delTempDirectory(checkoutDir); - return ERROR; - } - - File[] checkoutDirTab = new File[1]; - checkoutDirTab[0] = checkoutDir; - - try { - manager.getCommitClient().doCommit(checkoutDirTab, false, "From scmwebeditor -- add the file : " + uploadFileName, null, null, false, true, SVNDepth.INFINITY); - - if (log.isDebugEnabled()) { - log.debug("Commit success !"); - } - - } catch (SVNAuthenticationException authexep) { - if (log.isErrorEnabled()) { - log.error("authentification fail"); - } - badLogin = true; - //Suppression du repertoire temporaire - svnConn.delTempDirectory(checkoutDir); - getScmSession().delScmUser(repositoryUUID); - return LOGIN; - } catch (SVNException e) { - if (log.isErrorEnabled()) { - log.error("Erreur SVN commit", e); - } - error = true; - //Suppression du repertoire temporaire - svnConn.delTempDirectory(checkoutDir); - return ERROR; - } - - - //Suppression du repertoire temporaire - svnConn.delTempDirectory(checkoutDir); - - if (log.isDebugEnabled()) { - log.debug("File upload successful"); - } - - if (log.isInfoEnabled()) { - log.info(username + " with IP " + request.getRemoteAddr() + " add the file " + getUploadFileName() + " on the repository."); - } - return SUCCESS; + return returnCode; } @Override -- To stop receiving notification emails like this one, please contact nuiton.org SCM administrator <admin+scm@nuiton.org>.
participants (1)
-
nuiton.org scm