This is an automated email from the git hooks/post-receive script. New commit to branch develop in repository scmwebeditor. See http://git.nuiton.org/scmwebeditor.git commit 5d1a1325a77027d5cd1c89bd3f7408d4f048f47a Author: Hugo PIGEON <hpigeon@codelutin.com> Date: Tue Jun 2 15:07:00 2015 +0200 Code refactoring to make the GitConnection and SvnConnection classes shorter --- .../org/nuiton/scmwebeditor/git/GitConnection.java | 985 +++------------------ .../nuiton/scmwebeditor/git/GitFileManager.java | 943 ++++++++++++++++++++ .../org/nuiton/scmwebeditor/git/GitProvider.java | 25 +- .../org/nuiton/scmwebeditor/api/ScmConnection.java | 40 - .../{ScmConnection.java => ScmFileManager.java} | 65 +- .../org/nuiton/scmwebeditor/api/ScmProvider.java | 7 + .../org/nuiton/scmwebeditor/svn/SvnConnection.java | 479 +--------- .../nuiton/scmwebeditor/svn/SvnFileManager.java | 495 +++++++++++ .../org/nuiton/scmwebeditor/svn/SvnProvider.java | 25 +- .../uiweb/actions/CreateDirectoryAction.java | 4 +- .../scmwebeditor/uiweb/actions/MoveFileAction.java | 4 +- .../uiweb/actions/RemoveDirectoryAction.java | 4 +- .../uiweb/actions/RemoveFileAction.java | 4 +- .../uiweb/actions/UploadFileAction.java | 4 +- .../src/main/resources/scmwebeditor.properties | 2 +- 15 files changed, 1657 insertions(+), 1429 deletions(-) diff --git a/swe-git/src/main/java/org/nuiton/scmwebeditor/git/GitConnection.java b/swe-git/src/main/java/org/nuiton/scmwebeditor/git/GitConnection.java index 92a5e3a..aadf497 100644 --- a/swe-git/src/main/java/org/nuiton/scmwebeditor/git/GitConnection.java +++ b/swe-git/src/main/java/org/nuiton/scmwebeditor/git/GitConnection.java @@ -39,8 +39,11 @@ import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider; import org.eclipse.jgit.treewalk.TreeWalk; import org.nuiton.scmwebeditor.api.RepositoryNotFoundException; import org.nuiton.scmwebeditor.api.ScmConnection; -import org.nuiton.scmwebeditor.api.dto.*; -import org.nuiton.scmwebeditor.api.dto.result.*; +import org.nuiton.scmwebeditor.api.dto.BrowseDto; +import org.nuiton.scmwebeditor.api.dto.CommitDto; +import org.nuiton.scmwebeditor.api.dto.result.BrowseResultDto; +import org.nuiton.scmwebeditor.api.dto.result.CommitResultDto; +import org.nuiton.scmwebeditor.api.dto.result.RemoveFileResultDto; import javax.naming.AuthenticationException; import java.io.File; @@ -78,9 +81,23 @@ public class GitConnection implements ScmConnection { protected static final String DEFAULT_BRANCH = "master"; + public Repository getGitRepo() { return gitRepo; } + + public void setGitRepo(Repository gitRepo) { this.gitRepo = gitRepo; } + public File getLocalDirectory() { return localDirectory; } - public Repository getGitRepo() { return gitRepo; } + public void setLocalDirectory(File localDirectory) { this.localDirectory = localDirectory; } + + public String getAddressGit() { return addressGit; } + + public void setAddressGit(String addressGit) { this.addressGit = addressGit; } + + public void setFileName(String fileName) { this.fileName = fileName; } + + public String getPathToLocalRepos() { return pathToLocalRepos; } + + public void setPathToLocalRepos(String pathToLocalRepos) { this.pathToLocalRepos = pathToLocalRepos; } /** * Creates a new connection to a Git repository @@ -428,942 +445,169 @@ public class GitConnection implements ScmConnection { } @Override - public UploadFileResultDto uploadFile(UploadFileDto dto) { - - UploadFileResultDto resultDto = new UploadFileResultDto(); + public File getFileContent(String path, String username, String password) throws AuthenticationException { // getting the last version of the repository try { - updateRepository(dto.getUsername(), dto.getPassword()); + updateRepository(username, password); } catch (RepositoryNotFoundException e) { if (log.isErrorEnabled()) { log.error("Error while cloning or pulling the repository", e); } - - resultDto.setError(UploadFileResultDto.ERROR); - return resultDto; - } catch (IOException e) { if (log.isErrorEnabled()) { log.error("Error while cloning or pulling the repository", e); } - resultDto.setError(UploadFileResultDto.ERROR); - return resultDto; - - } catch (AuthenticationException e) { - - if (log.isErrorEnabled()) { - log.error("Error while cloning or pulling the repository", e); - } - resultDto.setError(UploadFileResultDto.AUTH_ERROR); - return resultDto; - } - - resultDto.setFileRoot(addressGit); - resultDto.setScmRoot(addressGit); - - // if there is no file to upload, we get back to the upload form - if (dto.getUpload() == null) { - resultDto.setError(UploadFileResultDto.REDIRECT); - return resultDto; - } - - if (log.isDebugEnabled()) { - log.debug("FileName : " + dto.getUploadFileName()); - log.debug("ContentType : " + dto.getUploadContentType()); - } - - // Writing the file to the local directory - String pathOnRepo = dto.getScmPath(); - String path; - - if (pathOnRepo.length() > addressGit.length()) { - pathOnRepo = pathOnRepo.substring(addressGit.length() + 1); - path = pathOnRepo + File.separator + dto.getUploadFileName(); - } else { - path = dto.getUploadFileName(); - } - - File file = new File(localDirectory.getAbsolutePath() + File.separator + path); - - try { - FileUtils.copyFile(dto.getUpload(), file); - } catch (IOException e) { - if (log.isErrorEnabled()) { - log.error("Can't copy the file to the local directory", e); - } - - resultDto.setError(UploadFileResultDto.ERROR); - return resultDto; - } - - // authentication - if (dto.getUsername() == null) { - dto.setUsername("anonymous"); - } - - if (dto.getPassword() == null) { - dto.setPassword("anonymous"); } - CredentialsProvider credentials = new UsernamePasswordCredentialsProvider(dto.getUsername(), dto.getPassword()); - - try { - Git git = Git.open(localDirectory); - - // add - AddCommand add = git.add(); - add.addFilepattern(path); - - try { - add.call(); - } catch (GitAPIException e) { - if (log.isErrorEnabled()) { - log.error("Can not add new files", e); - } - } - - // commit - try { - doCommit(git, dto.getUsername(), "unknown", "From scmwebeditor -- add the file : " + dto.getUploadFileName()); - } catch (GitAPIException e) { - if (log.isErrorEnabled()) { - log.error("Can not commit", e); - } - - resultDto.setError(UploadFileResultDto.ERROR); - return resultDto; - } - - // push - if (log.isDebugEnabled()) { - log.debug("Preparing push"); - } - - PushCommand push = git.push(); - push.setRemote(addressGit); - push.setCredentialsProvider(credentials); - - try { - push.call(); - } catch (GitAPIException e) { - - file.delete(); - - handlePushException(e); - - if (e.getMessage().endsWith("not authorized")) { - resultDto.setError(RemoveFileResultDto.AUTH_ERROR); - } else { - resultDto.setError(RemoveFileResultDto.ERROR); - } - - return resultDto; - } - } catch (IOException e) { - if (log.isErrorEnabled()) { - log.error("Can not open git local repository : " + localDirectory.getAbsolutePath(), e); - } + File fileToEdit = new File(localDirectory.getAbsolutePath() + File.separator + fileName); - file.delete(); - resultDto.setError(UploadFileResultDto.ERROR); - return resultDto; + if (!fileToEdit.exists()) { + throw new IllegalArgumentException("There is no entry at '" + path + "'."); } - return resultDto; + return fileToEdit; } @Override - public RemoveFileResultDto removeFile(RemoveFileDto dto) { - - RemoveFileResultDto resultDto = new RemoveFileResultDto(); + public String getHeadRevisionNumber(String path, String username, String password) throws AuthenticationException { // getting the last version of the repository try { - updateRepository(dto.getUsername(), dto.getPassword()); + updateRepository(username, password); } catch (RepositoryNotFoundException e) { if (log.isErrorEnabled()) { log.error("Error while cloning or pulling the repository", e); } - - resultDto.setError(RemoveFileResultDto.ERROR); - return resultDto; - } catch (IOException e) { if (log.isErrorEnabled()) { log.error("Error while cloning or pulling the repository", e); } - resultDto.setError(RemoveFileResultDto.ERROR); - return resultDto; + } - } catch (AuthenticationException e) { + String headRevision = null; - if (log.isErrorEnabled()) { - log.error("Error while cloning or pulling the repository", e); - } - resultDto.setError(RemoveFileResultDto.AUTH_ERROR); - return resultDto; + try { + ObjectId commitId = gitRepo.resolve(Constants.HEAD); + headRevision = commitId.getName(); + } catch (IOException e) { + e.printStackTrace(); } - resultDto.setFileRoot(addressGit); - resultDto.setScmRoot(addressGit); + return headRevision; + } + @Override + public String getRepositoryId() { + return addressGit; + } - // if there is no file to remove, we get back to the remove form - if (dto.getScmPath() == null) { - resultDto.setError(RemoveFileResultDto.REDIRECT); - return resultDto; - } else if (dto.getScmPath().equals("")) { - resultDto.setError(RemoveFileResultDto.REDIRECT); - return resultDto; - } else if (dto.getScmPath().equals(addressGit)) { - resultDto.setError(RemoveFileResultDto.REDIRECT); - return resultDto; - } + @Override + public String getFileName() { + return fileName; + } - if (log.isDebugEnabled()) { - log.debug("FileName : " + dto.getScmPath()); - } + @Override + public String getImagePath(String address, String repositoryRoot) { - // Removing the file from the local directory - String pathOnRepo = dto.getScmPath(); + String path = localDirectory.getAbsolutePath() + address.replace(repositoryRoot, ""); + path.replaceAll("/", File.separator); - if (pathOnRepo.length() > addressGit.length()) { - pathOnRepo = pathOnRepo.replace(addressGit + "/", ""); - } + return path; + } - File file = new File(localDirectory.getAbsolutePath() + File.separator + pathOnRepo); - // authentication - if (dto.getUsername() == null) { - dto.setUsername("anonymous"); - } + /** + * 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 { - if (dto.getPassword() == null) { - dto.setPassword("anonymous"); - } + Git git = Git.open(localDirectory); - CredentialsProvider credentials = new UsernamePasswordCredentialsProvider(dto.getUsername(), dto.getPassword()); + CheckoutCommand checkout = git.checkout(); + checkout.setCreateBranch(true); + checkout.setName(branchName); + checkout.setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK); + checkout.setStartPoint("origin/" + branchName); try { - Git git = Git.open(localDirectory); - - // removing the file - RmCommand rm = git.rm(); - rm.addFilepattern(pathOnRepo); - try { - rm.call(); - } catch (GitAPIException e) { - if (log.isErrorEnabled()) { - log.error("Can not remove Git file " + pathOnRepo, e); - } + checkout.call(); + } catch (GitAPIException e) { - resultDto.setError(RemoveFileResultDto.ERROR); - return resultDto; - } - file.delete(); + // if we could not create a new local branch, it may be because it already exists + checkout = git.checkout(); + checkout.setName(branchName); - // commit try { - doCommit(git, dto.getUsername(), "unknown", "From scmwebeditor -- remove the file : " + pathOnRepo); - } catch (GitAPIException e) { + checkout.call(); + } catch (GitAPIException e1) { if (log.isErrorEnabled()) { - log.error("Can not commit", e); + log.error("Can not checkout branch " + branchName, e1); } - - resultDto.setError(RemoveFileResultDto.ERROR); - return resultDto; + throw new IOException("Can not checkout branch " + branchName, e1); } + } + } - // push - if (log.isDebugEnabled()) { - log.debug("Preparing push"); - } - PushCommand push = git.push(); - push.setRemote(addressGit); - push.setCredentialsProvider(credentials); + /** + * Updates the repository to the last version by a clone or a pull command + * @param username the username to use to connect to the repository + * @param password the password to use to connect to the repository + * @throws RepositoryNotFoundException if the repository is not found + * @throws IOException if it is not possible to reach the repository + * @throws AuthenticationException if there is a problem during the authentication process + */ + public void updateRepository(String username, String password) + throws RepositoryNotFoundException, IOException, AuthenticationException { - try { - push.call(); - } catch (GitAPIException e) { + // Cloning the remote repository to a local directory + String hashedAddress = addressGit; - handlePushException(e); + String hashResult = hash(addressGit, "SHA-512"); - try { - cloneRepository(credentials); - } catch (AuthenticationException e1) { - if (log.isErrorEnabled()) { - log.error("Can not clone repository at address " + addressGit); - } - } catch (RepositoryNotFoundException e1) { - if (log.isErrorEnabled()) { - log.error("Can not clone repository at address " + addressGit); - } - } + if (hashResult != null) { + hashedAddress = hashResult; + } - if (e.getMessage().endsWith("not authorized")) { - resultDto.setError(RemoveFileResultDto.AUTH_ERROR); - } else { - resultDto.setError(RemoveFileResultDto.ERROR); - } + localDirectory = new File(pathToLocalRepos + File.separator + hashedAddress); - return resultDto; - } - } catch (IOException e) { - if (log.isErrorEnabled()) { - log.error("Can not open git local repository : " + localDirectory.getAbsolutePath(), e); - } + CredentialsProvider credentials = null; - resultDto.setError(RemoveFileResultDto.ERROR); - return resultDto; + if (username != null && password != null) { + credentials = new UsernamePasswordCredentialsProvider(username, password); } - return resultDto; - } - - @Override - public CreateDirectoryResultDto createDirectory(CreateDirectoryDto dto) { + if (!localDirectory.exists()) { - CreateDirectoryResultDto resultDto = new CreateDirectoryResultDto(); + cloneRepository(credentials); - // getting the last version of the repository - try { - updateRepository(dto.getUsername(), dto.getPassword()); - } catch (RepositoryNotFoundException e) { + } else { - if (log.isErrorEnabled()) { - log.error("Error while cloning or pulling the repository", e); - } + Git git = Git.open(localDirectory); - resultDto.setError(UploadFileResultDto.ERROR); - return resultDto; + PullCommand pull = git.pull(); + pull.setCredentialsProvider(credentials); - } catch (IOException e) { + try { + pull.call(); + } catch (InvalidRemoteException e) { + if (log.isErrorEnabled()) { + log.error("Can't pull the remote repository", e); + } + cloneRepository(credentials); - if (log.isErrorEnabled()) { - log.error("Error while cloning or pulling the repository", e); - } - resultDto.setError(UploadFileResultDto.ERROR); - return resultDto; - - } catch (AuthenticationException e) { - - if (log.isErrorEnabled()) { - log.error("Error while cloning or pulling the repository", e); - } - resultDto.setError(UploadFileResultDto.AUTH_ERROR); - return resultDto; - } - - resultDto.setFileRoot(addressGit); - resultDto.setScmRoot(addressGit); - - // if the name of the new directory is empty we get back to the create directory form - if (dto.getDirectoryName() == null) { - - resultDto.setError(UploadFileResultDto.REDIRECT); - return resultDto; - } - - if (dto.getDirectoryName().equals("")) { - - resultDto.setError(UploadFileResultDto.REDIRECT); - return resultDto; - } - - // Creating the new directory in the local directory - String pathOnRepo = dto.getParentDirectory(); - - if (pathOnRepo.length() > addressGit.length()) { - pathOnRepo = pathOnRepo.substring(addressGit.length() + 1); - } else { - pathOnRepo = ""; - } - - File file = new File(localDirectory.getAbsolutePath() + File.separator + pathOnRepo + - File.separator + dto.getDirectoryName()); - file.mkdir(); - File emptyFile = new File(file.getAbsolutePath() + File.separator + ".gitignore"); - - try { - emptyFile.createNewFile(); - FileUtils.writeStringToFile(emptyFile, " "); - } catch (IOException e) { - if (log.isErrorEnabled()) { - log.error("Can not create file '" + emptyFile.getAbsolutePath() + "'"); - } - } - - // authentication - if (dto.getUsername() == null) { - dto.setUsername("anonymous"); - } - - if (dto.getPassword() == null) { - dto.setPassword("anonymous"); - } - - CredentialsProvider credentials = new UsernamePasswordCredentialsProvider(dto.getUsername(), dto.getPassword()); - - try { - Git git = Git.open(localDirectory); - - // add - AddCommand add = git.add(); - String toAdd; - - if (pathOnRepo.equals("")) { - toAdd = dto.getDirectoryName() + "/.gitignore"; - } else { - toAdd = pathOnRepo + "/" + dto.getDirectoryName() + "/.gitignore"; - } - - add.addFilepattern(toAdd); - - try { - add.call(); - } catch (GitAPIException e) { - if (log.isErrorEnabled()) { - log.error("Can not add new files", e); - } - } - - // commit - try { - doCommit(git, dto.getUsername(), "unknown", "From scmwebeditor -- create the directory : " + dto.getDirectoryName()); - } catch (GitAPIException e) { - if (log.isErrorEnabled()) { - log.error("Can not commit", e); - } - - resultDto.setError(UploadFileResultDto.ERROR); - return resultDto; - } - - // push - if (log.isDebugEnabled()) { - log.debug("Preparing push"); - } - - PushCommand push = git.push(); - push.setRemote(addressGit); - push.setCredentialsProvider(credentials); - - try { - push.call(); - } catch (GitAPIException e) { - - emptyFile.delete(); - file.delete(); - - handlePushException(e); - - if (e.getMessage().endsWith("not authorized")) { - resultDto.setError(RemoveFileResultDto.AUTH_ERROR); - } else { - resultDto.setError(RemoveFileResultDto.ERROR); - } - - return resultDto; - } - } catch (IOException e) { - if (log.isErrorEnabled()) { - log.error("Can not open git local repository : " + localDirectory.getAbsolutePath(), e); - } - - emptyFile.delete(); - file.delete(); - resultDto.setError(UploadFileResultDto.ERROR); - return resultDto; - } - - return resultDto; - } - - @Override - public RemoveDirectoryResultDto removeDirectory(RemoveDirectoryDto dto) { - - RemoveDirectoryResultDto resultDto = new RemoveDirectoryResultDto(); - - // getting the last version of the repository - try { - updateRepository(dto.getUsername(), dto.getPassword()); - } catch (RepositoryNotFoundException e) { - - if (log.isErrorEnabled()) { - log.error("Error while cloning or pulling the repository", e); - } - - resultDto.setError(RemoveFileResultDto.ERROR); - return resultDto; - - } catch (IOException e) { - - if (log.isErrorEnabled()) { - log.error("Error while cloning or pulling the repository", e); - } - resultDto.setError(RemoveFileResultDto.ERROR); - return resultDto; - - } catch (AuthenticationException e) { - - if (log.isErrorEnabled()) { - log.error("Error while cloning or pulling the repository", e); - } - resultDto.setError(RemoveFileResultDto.AUTH_ERROR); - return resultDto; - } - - resultDto.setFileRoot(addressGit); - resultDto.setScmRoot(addressGit); - - - // if there is no file to remove, we get back to the remove form - if (dto.getDirectoryToRemove() == null) { - resultDto.setError(RemoveFileResultDto.REDIRECT); - return resultDto; - } - if (dto.getDirectoryToRemove().equals("") || dto.getDirectoryToRemove().equals(addressGit)) { - resultDto.setError(RemoveFileResultDto.REDIRECT); - return resultDto; - } - - // Removing the directory from the local directory - String pathOnRepo = dto.getDirectoryToRemove(); - - if (pathOnRepo.length() > addressGit.length()) { - pathOnRepo = pathOnRepo.replace(addressGit + "/", ""); - } - - File directory = new File(localDirectory.getAbsolutePath() + File.separator + pathOnRepo); - - // authentication - if (dto.getUsername() == null) { - dto.setUsername("anonymous"); - } - - if (dto.getPassword() == null) { - dto.setPassword("anonymous"); - } - - CredentialsProvider credentials = new UsernamePasswordCredentialsProvider(dto.getUsername(), dto.getPassword()); - - try { - Git git = Git.open(localDirectory); - - // removing the directory - RmCommand rm = git.rm(); - rm.addFilepattern(pathOnRepo); - try { - rm.call(); - } catch (GitAPIException e) { - if (log.isErrorEnabled()) { - log.error("Can not execute Git remove " + pathOnRepo, e); - } - - resultDto.setError(RemoveFileResultDto.ERROR); - return resultDto; - } - directory.delete(); - - // commit - try { - doCommit(git, dto.getUsername(), "unknown", "From scmwebeditor -- remove the directory : " + pathOnRepo); - } catch (GitAPIException e) { - if (log.isErrorEnabled()) { - log.error("Can not commit", e); - } - - resultDto.setError(RemoveFileResultDto.ERROR); - return resultDto; - } - - // push - if (log.isDebugEnabled()) { - log.debug("Preparing push"); - } - - PushCommand push = git.push(); - push.setRemote(addressGit); - push.setCredentialsProvider(credentials); - - try { - push.call(); - } catch (GitAPIException e) { - - handlePushException(e); - - try { - cloneRepository(credentials); - } catch (AuthenticationException e1) { - if (log.isErrorEnabled()) { - log.error("Can not clone repository at address " + addressGit); - } - } catch (RepositoryNotFoundException e1) { - if (log.isErrorEnabled()) { - log.error("Can not clone repository at address " + addressGit); - } - } - - if (e.getMessage().endsWith("not authorized")) { - resultDto.setError(RemoveFileResultDto.AUTH_ERROR); - } else { - resultDto.setError(RemoveFileResultDto.ERROR); - } - - return resultDto; - } - } catch (IOException e) { - if (log.isErrorEnabled()) { - log.error("Can not open git local repository : " + localDirectory.getAbsolutePath(), e); - } - - resultDto.setError(RemoveFileResultDto.ERROR); - return resultDto; - } - - return resultDto; - } - - @Override - public MoveFileResultDto moveFile(MoveFileDto dto) { - - MoveFileResultDto resultDto = new MoveFileResultDto(); - - // getting the last version of the repository - try { - updateRepository(dto.getUsername(), dto.getPassword()); - } catch (RepositoryNotFoundException e) { - - if (log.isErrorEnabled()) { - log.error("Error while cloning or pulling the repository", e); - } - - resultDto.setError(RemoveFileResultDto.ERROR); - return resultDto; - - } catch (IOException e) { - - if (log.isErrorEnabled()) { - log.error("Error while cloning or pulling the repository", e); - } - resultDto.setError(RemoveFileResultDto.ERROR); - return resultDto; - - } catch (AuthenticationException e) { - - if (log.isErrorEnabled()) { - log.error("Error while cloning or pulling the repository", e); - } - resultDto.setError(RemoveFileResultDto.AUTH_ERROR); - return resultDto; - } - - resultDto.setFileRoot(addressGit); - resultDto.setScmRoot(addressGit); - - // if the name of the file to move is empty we get back to the move a file form - if (dto.getFileToMove() == null) { - resultDto.setError(MoveFileResultDto.REDIRECT); - return resultDto; - } - - if (dto.getFileToMove().equals("") || dto.getFileToMove().equals(resultDto.getFileRoot())) { - resultDto.setError(MoveFileResultDto.REDIRECT); - return resultDto; - } - - // if the name of the destination directory is empty we get back to the move a file form - if (dto.getDestinationDirectory() == null) { - resultDto.setError(MoveFileResultDto.REDIRECT); - return resultDto; - } - - if (dto.getDestinationDirectory().equals("")) { - resultDto.setError(MoveFileResultDto.REDIRECT); - return resultDto; - } - - // Moving the file on the local directory - String pathOnRepoFile = dto.getFileToMove(); - - if (pathOnRepoFile.length() > addressGit.length()) { - pathOnRepoFile = pathOnRepoFile.replace(addressGit + "/", ""); - } - - String pathOnRepoDirectory = dto.getDestinationDirectory(); - - if (pathOnRepoDirectory.length() > addressGit.length()) { - pathOnRepoDirectory = pathOnRepoDirectory.replace(addressGit + "/", ""); - } - - String sourceFileName = pathOnRepoFile.substring(pathOnRepoFile.lastIndexOf('/') + 1); - - File sourceFile = new File(localDirectory.getAbsolutePath() + File.separator + pathOnRepoFile); - File destFile = new File(localDirectory.getAbsolutePath() + File.separator + pathOnRepoDirectory - + File.separator + sourceFileName); - - try { - FileUtils.moveFile(sourceFile, destFile); - } catch (IOException e) { - if (log.isErrorEnabled()) { - log.error("Can not move file " + sourceFile.getAbsolutePath() + " to " + destFile.getAbsolutePath(), e); - } - return resultDto; - } - - // authentication - if (dto.getUsername() == null) { - dto.setUsername("anonymous"); - } - - if (dto.getPassword() == null) { - dto.setPassword("anonymous"); - } - - CredentialsProvider credentials = new UsernamePasswordCredentialsProvider(dto.getUsername(), dto.getPassword()); - - try { - Git git = Git.open(localDirectory); - - // adding the moved file - AddCommand add = git.add(); - add.addFilepattern(pathOnRepoDirectory + "/" + sourceFileName); - try { - add.call(); - } catch (GitAPIException e) { - if (log.isErrorEnabled()) { - log.error("Can not execute Git add " + pathOnRepoDirectory + "/" + sourceFileName, e); - } - - resultDto.setError(MoveFileResultDto.ERROR); - return resultDto; - } - - // commit - try { - doCommit(git, dto.getUsername(), "unknown", "From scmwebeditor -- move the file : " + pathOnRepoFile + - " to : " + pathOnRepoDirectory); - } catch (GitAPIException e) { - if (log.isErrorEnabled()) { - log.error("Can not commit", e); - } - - resultDto.setError(MoveFileResultDto.ERROR); - return resultDto; - } - - // push - if (log.isDebugEnabled()) { - log.debug("Preparing push"); - } - - PushCommand push = git.push(); - push.setRemote(addressGit); - push.setCredentialsProvider(credentials); - - try { - push.call(); - } catch (GitAPIException e) { - - handlePushException(e); - - try { - cloneRepository(credentials); - } catch (AuthenticationException e1) { - if (log.isErrorEnabled()) { - log.error("Can not clone repository at address " + addressGit); - } - } catch (RepositoryNotFoundException e1) { - if (log.isErrorEnabled()) { - log.error("Can not clone repository at address " + addressGit); - } - } - - if (e.getMessage().endsWith("not authorized")) { - resultDto.setError(MoveFileResultDto.AUTH_ERROR); - } else { - resultDto.setError(MoveFileResultDto.ERROR); - } - - return resultDto; - } - } catch (IOException e) { - if (log.isErrorEnabled()) { - log.error("Can not open git local repository : " + localDirectory.getAbsolutePath(), e); - } - - resultDto.setError(MoveFileResultDto.ERROR); - return resultDto; - } - - return resultDto; - } - - - @Override - public File getFileContent(String path, String username, String password) throws AuthenticationException { - - // getting the last version of the repository - try { - updateRepository(username, password); - } catch (RepositoryNotFoundException e) { - - if (log.isErrorEnabled()) { - log.error("Error while cloning or pulling the repository", e); - } - } catch (IOException e) { - - if (log.isErrorEnabled()) { - log.error("Error while cloning or pulling the repository", e); - } - } - - File fileToEdit = new File(localDirectory.getAbsolutePath() + File.separator + fileName); - - if (!fileToEdit.exists()) { - throw new IllegalArgumentException("There is no entry at '" + path + "'."); - } - - return fileToEdit; - } - - @Override - public String getHeadRevisionNumber(String path, String username, String password) throws AuthenticationException { - - // getting the last version of the repository - try { - updateRepository(username, password); - } catch (RepositoryNotFoundException e) { - - if (log.isErrorEnabled()) { - log.error("Error while cloning or pulling the repository", e); - } - } catch (IOException e) { - - if (log.isErrorEnabled()) { - log.error("Error while cloning or pulling the repository", e); - } - } - - String headRevision = null; - - try { - ObjectId commitId = gitRepo.resolve(Constants.HEAD); - headRevision = commitId.getName(); - } catch (IOException e) { - e.printStackTrace(); - } - - return headRevision; - } - - @Override - public String getRepositoryId() { - return addressGit; - } - - @Override - public String getFileName() { - return fileName; - } - - @Override - public String getImagePath(String address, String repositoryRoot) { - - String path = localDirectory.getAbsolutePath() + address.replace(repositoryRoot, ""); - path.replaceAll("/", File.separator); - - return path; - } - - - /** - * 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); - } - throw new IOException("Can not checkout branch " + branchName, e1); - } - } - } - - - /** - * Updates the repository to the last version by a clone or a pull command - * @param username the username to use to connect to the repository - * @param password the password to use to connect to the repository - * @throws RepositoryNotFoundException if the repository is not found - * @throws IOException if it is not possible to reach the repository - * @throws AuthenticationException if there is a problem during the authentication process - */ - public void updateRepository(String username, String password) - throws RepositoryNotFoundException, IOException, AuthenticationException { - - // Cloning the remote repository to a local directory - String hashedAddress = addressGit; - - String hashResult = hash(addressGit, "SHA-512"); - - if (hashResult != null) { - hashedAddress = hashResult; - } - - localDirectory = new File(pathToLocalRepos + File.separator + hashedAddress); - - CredentialsProvider credentials = null; - - if (username != null && password != null) { - credentials = new UsernamePasswordCredentialsProvider(username, password); - } - - if (!localDirectory.exists()) { - - cloneRepository(credentials); - - } else { - - Git git = Git.open(localDirectory); - - PullCommand pull = git.pull(); - pull.setCredentialsProvider(credentials); - - try { - pull.call(); - } catch (InvalidRemoteException e) { - if (log.isErrorEnabled()) { - log.error("Can't pull the remote repository", e); - } - cloneRepository(credentials); + } catch (JGitInternalException e) { + if (log.isErrorEnabled()) { + log.error("Can't pull the remote repository", e); + } + cloneRepository(credentials); } catch (TransportException e) { if (log.isErrorEnabled()) { @@ -1380,11 +624,6 @@ public class GitConnection implements ScmConnection { log.error("Can't pull the remote repository", e); } cloneRepository(credentials); - } catch (JGitInternalException e) { - if (log.isErrorEnabled()) { - log.error("Can't pull the remote repository", e); - } - cloneRepository(credentials); } if (log.isDebugEnabled()) { diff --git a/swe-git/src/main/java/org/nuiton/scmwebeditor/git/GitFileManager.java b/swe-git/src/main/java/org/nuiton/scmwebeditor/git/GitFileManager.java new file mode 100644 index 0000000..edeebc8 --- /dev/null +++ b/swe-git/src/main/java/org/nuiton/scmwebeditor/git/GitFileManager.java @@ -0,0 +1,943 @@ +/* + * #%L + * ScmWebEditor + * %% + * Copyright (C) 2009 - 2015 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.git; + +import org.apache.commons.io.FileUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.eclipse.jgit.api.*; +import org.eclipse.jgit.api.errors.GitAPIException; +import org.eclipse.jgit.api.errors.InvalidRemoteException; +import org.eclipse.jgit.api.errors.JGitInternalException; +import org.eclipse.jgit.api.errors.TransportException; +import org.eclipse.jgit.storage.file.FileRepositoryBuilder; +import org.eclipse.jgit.transport.CredentialsProvider; +import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider; +import org.nuiton.scmwebeditor.api.RepositoryNotFoundException; +import org.nuiton.scmwebeditor.api.ScmFileManager; +import org.nuiton.scmwebeditor.api.dto.*; +import org.nuiton.scmwebeditor.api.dto.result.*; + +import javax.naming.AuthenticationException; +import java.io.File; +import java.io.IOException; + +/** + * Implementation of the Git's features related to file management + */ +public class GitFileManager implements ScmFileManager { + + private static final Log log = LogFactory.getLog(GitFileManager.class); + + /** the connection to the Git repository */ + GitConnection connection; + + /** + * Creates a new file manager for Git repositories + * @param connection the connection to the Git repository + * @throws IOException if the repository can not be reached + */ + public GitFileManager(GitConnection connection) throws IOException { + + this.connection = connection; + } + + @Override + public UploadFileResultDto uploadFile(UploadFileDto dto) { + + UploadFileResultDto resultDto = new UploadFileResultDto(); + + // getting the last version of the repository + try { + updateRepository(dto.getUsername(), dto.getPassword()); + } catch (RepositoryNotFoundException e) { + + if (log.isErrorEnabled()) { + log.error("Error while cloning or pulling the repository", e); + } + + resultDto.setError(UploadFileResultDto.ERROR); + return resultDto; + } catch (IOException e) { + + if (log.isErrorEnabled()) { + log.error("Error while cloning or pulling the repository", e); + } + + resultDto.setError(UploadFileResultDto.ERROR); + return resultDto; + } catch (AuthenticationException e) { + + if (log.isErrorEnabled()) { + log.error("Error while cloning or pulling the repository", e); + } + resultDto.setError(UploadFileResultDto.AUTH_ERROR); + return resultDto; + } + + resultDto.setFileRoot(connection.getAddressGit()); + resultDto.setScmRoot(connection.getAddressGit()); + + // if there is no file to upload, we get back to the upload form + if (dto.getUpload() == null) { + resultDto.setError(UploadFileResultDto.REDIRECT); + return resultDto; + } + + if (log.isDebugEnabled()) { + log.debug("FileName : " + dto.getUploadFileName()); + log.debug("ContentType : " + dto.getUploadContentType()); + } + + // Writing the file to the local directory + String pathOnRepo = dto.getScmPath(); + String path; + + if (pathOnRepo.length() > connection.getAddressGit().length()) { + pathOnRepo = pathOnRepo.substring(connection.getAddressGit().length() + 1); + path = pathOnRepo + File.separator + dto.getUploadFileName(); + } else { + path = dto.getUploadFileName(); + } + + File file = new File(connection.getLocalDirectory().getAbsolutePath() + File.separator + path); + + try { + FileUtils.copyFile(dto.getUpload(), file); + } catch (IOException e) { + if (log.isErrorEnabled()) { + log.error("Can't copy the file to the local directory", e); + } + + resultDto.setError(UploadFileResultDto.ERROR); + return resultDto; + } + + // authentication + if (dto.getUsername() == null) { + dto.setUsername("anonymous"); + } + + if (dto.getPassword() == null) { + dto.setPassword("anonymous"); + } + + CredentialsProvider credentials = new UsernamePasswordCredentialsProvider(dto.getUsername(), dto.getPassword()); + + try { + Git git = Git.open(connection.getLocalDirectory()); + + // add + AddCommand add = git.add(); + add.addFilepattern(path); + + try { + add.call(); + } catch (GitAPIException e) { + if (log.isErrorEnabled()) { + log.error("Can not add new files", e); + } + } + + // commit + try { + connection.doCommit(git, dto.getUsername(), "unknown", "From scmwebeditor -- add the file : " + + dto.getUploadFileName()); + } catch (GitAPIException e) { + if (log.isErrorEnabled()) { + log.error("Can not commit", e); + } + + resultDto.setError(UploadFileResultDto.ERROR); + return resultDto; + } + + // push + if (log.isDebugEnabled()) { + log.debug("Preparing push"); + } + + PushCommand push = git.push(); + push.setRemote(connection.getAddressGit()); + push.setCredentialsProvider(credentials); + + try { + push.call(); + } catch (GitAPIException e) { + + file.delete(); + + connection.handlePushException(e); + + if (e.getMessage().endsWith("not authorized")) { + resultDto.setError(RemoveFileResultDto.AUTH_ERROR); + } else { + resultDto.setError(RemoveFileResultDto.ERROR); + } + + return resultDto; + } + } catch (IOException e) { + if (log.isErrorEnabled()) { + log.error("Can not open git local repository : " + connection.getLocalDirectory().getAbsolutePath(), e); + } + + file.delete(); + resultDto.setError(UploadFileResultDto.ERROR); + return resultDto; + } + + return resultDto; + } + + @Override + public RemoveFileResultDto removeFile(RemoveFileDto dto) { + + RemoveFileResultDto resultDto = new RemoveFileResultDto(); + + // getting the last version of the repository + try { + updateRepository(dto.getUsername(), dto.getPassword()); + } catch (RepositoryNotFoundException e) { + + if (log.isErrorEnabled()) { + log.error("Error while cloning or pulling the repository", e); + } + + resultDto.setError(RemoveFileResultDto.ERROR); + return resultDto; + + } catch (IOException e) { + + if (log.isErrorEnabled()) { + log.error("Error while cloning or pulling the repository", e); + } + + resultDto.setError(RemoveFileResultDto.ERROR); + return resultDto; + + } catch (AuthenticationException e) { + + if (log.isErrorEnabled()) { + log.error("Error while cloning or pulling the repository", e); + } + resultDto.setError(RemoveFileResultDto.AUTH_ERROR); + return resultDto; + } + + resultDto.setFileRoot(connection.getAddressGit()); + resultDto.setScmRoot(connection.getAddressGit()); + + + // if there is no file to remove, we get back to the remove form + if (dto.getScmPath() == null) { + resultDto.setError(RemoveFileResultDto.REDIRECT); + return resultDto; + } else if (dto.getScmPath().equals("") || dto.getScmPath().equals(connection.getAddressGit())) { + resultDto.setError(RemoveFileResultDto.REDIRECT); + return resultDto; + } + + if (log.isDebugEnabled()) { + log.debug("FileName : " + dto.getScmPath()); + } + + // Removing the file from the local directory + String pathOnRepo = dto.getScmPath(); + + if (pathOnRepo.length() > connection.getAddressGit().length()) { + pathOnRepo = pathOnRepo.replace(connection.getAddressGit() + "/", ""); + } + + File file = new File(connection.getLocalDirectory().getAbsolutePath() + File.separator + pathOnRepo); + + // authentication + if (dto.getUsername() == null) { + dto.setUsername("anonymous"); + } + + if (dto.getPassword() == null) { + dto.setPassword("anonymous"); + } + + CredentialsProvider credentials = new UsernamePasswordCredentialsProvider(dto.getUsername(), dto.getPassword()); + + try { + Git git = Git.open(connection.getLocalDirectory()); + + // removing the file + RmCommand rm = git.rm(); + rm.addFilepattern(pathOnRepo); + try { + rm.call(); + } catch (GitAPIException e) { + if (log.isErrorEnabled()) { + log.error("Can not remove Git file " + pathOnRepo, e); + } + + resultDto.setError(RemoveFileResultDto.ERROR); + return resultDto; + } + file.delete(); + + // commit + try { + connection.doCommit(git, dto.getUsername(), "unknown", "From scmwebeditor -- remove the file : " + pathOnRepo); + } catch (GitAPIException e) { + if (log.isErrorEnabled()) { + log.error("Can not commit", e); + } + + resultDto.setError(RemoveFileResultDto.ERROR); + return resultDto; + } + + // push + if (log.isDebugEnabled()) { + log.debug("Preparing push"); + } + + PushCommand push = git.push(); + push.setRemote(connection.getAddressGit()); + push.setCredentialsProvider(credentials); + + try { + push.call(); + } catch (GitAPIException e) { + + connection.handlePushException(e); + + try { + connection.cloneRepository(credentials); + } catch (AuthenticationException e1) { + if (log.isErrorEnabled()) { + log.error("Can not clone repository at address " + connection.getAddressGit()); + } + resultDto.setError(RemoveFileResultDto.AUTH_ERROR); + } catch (RepositoryNotFoundException e1) { + if (log.isErrorEnabled()) { + log.error("Can not clone repository at address " + connection.getAddressGit()); + } + resultDto.setError(RemoveFileResultDto.ERROR); + } + + if (e.getMessage().endsWith("not authorized")) { + resultDto.setError(RemoveFileResultDto.AUTH_ERROR); + } else { + resultDto.setError(RemoveFileResultDto.ERROR); + } + + return resultDto; + } + } catch (IOException e) { + if (log.isErrorEnabled()) { + log.error("Can not open git local repository : " + connection.getLocalDirectory().getAbsolutePath(), e); + } + + resultDto.setError(RemoveFileResultDto.ERROR); + return resultDto; + } + + return resultDto; + } + + @Override + public CreateDirectoryResultDto createDirectory(CreateDirectoryDto dto) { + + CreateDirectoryResultDto resultDto = new CreateDirectoryResultDto(); + + // getting the last version of the repository + try { + updateRepository(dto.getUsername(), dto.getPassword()); + } catch (RepositoryNotFoundException e) { + + if (log.isErrorEnabled()) { + log.error("Error while cloning or pulling the repository", e); + } + + resultDto.setError(UploadFileResultDto.ERROR); + return resultDto; + + } catch (IOException e) { + + if (log.isErrorEnabled()) { + log.error("Error while cloning or pulling the repository", e); + } + + resultDto.setError(UploadFileResultDto.ERROR); + return resultDto; + + } catch (AuthenticationException e) { + + if (log.isErrorEnabled()) { + log.error("Error while cloning or pulling the repository", e); + } + resultDto.setError(UploadFileResultDto.AUTH_ERROR); + return resultDto; + } + + resultDto.setFileRoot(connection.getAddressGit()); + resultDto.setScmRoot(connection.getAddressGit()); + + // if the name of the new directory is empty we get back to the create directory form + if (dto.getDirectoryName() == null) { + + resultDto.setError(UploadFileResultDto.REDIRECT); + return resultDto; + } + + if (dto.getDirectoryName().equals("")) { + + resultDto.setError(UploadFileResultDto.REDIRECT); + return resultDto; + } + + // Creating the new directory in the local directory + String pathOnRepo = dto.getParentDirectory(); + + if (pathOnRepo.length() > connection.getAddressGit().length()) { + pathOnRepo = pathOnRepo.substring(connection.getAddressGit().length() + 1); + } else { + pathOnRepo = ""; + } + + File file = new File(connection.getLocalDirectory().getAbsolutePath() + File.separator + pathOnRepo + + File.separator + dto.getDirectoryName()); + file.mkdir(); + File emptyFile = new File(file.getAbsolutePath() + File.separator + ".gitignore"); + + try { + emptyFile.createNewFile(); + FileUtils.writeStringToFile(emptyFile, " "); + } catch (IOException e) { + if (log.isErrorEnabled()) { + log.error("Can not create file '" + emptyFile.getAbsolutePath() + "'"); + } + } + + // authentication + if (dto.getUsername() == null) { + dto.setUsername("anonymous"); + } + + if (dto.getPassword() == null) { + dto.setPassword("anonymous"); + } + + CredentialsProvider credentials = new UsernamePasswordCredentialsProvider(dto.getUsername(), dto.getPassword()); + + try { + Git git = Git.open(connection.getLocalDirectory()); + + // add + AddCommand add = git.add(); + String toAdd; + + if (pathOnRepo.equals("")) { + toAdd = dto.getDirectoryName() + "/.gitignore"; + } else { + toAdd = pathOnRepo + "/" + dto.getDirectoryName() + "/.gitignore"; + } + + add.addFilepattern(toAdd); + + try { + add.call(); + } catch (GitAPIException e) { + if (log.isErrorEnabled()) { + log.error("Can not add new files", e); + } + } + + // commit + try { + connection.doCommit(git, dto.getUsername(), "unknown", "From scmwebeditor -- create the directory : " + dto.getDirectoryName()); + } catch (GitAPIException e) { + if (log.isErrorEnabled()) { + log.error("Can not commit", e); + } + + resultDto.setError(UploadFileResultDto.ERROR); + return resultDto; + } + + // push + if (log.isDebugEnabled()) { + log.debug("Preparing push"); + } + + PushCommand push = git.push(); + push.setRemote(connection.getAddressGit()); + push.setCredentialsProvider(credentials); + + try { + push.call(); + } catch (GitAPIException e) { + + emptyFile.delete(); + file.delete(); + + connection.handlePushException(e); + + if (e.getMessage().endsWith("not authorized")) { + resultDto.setError(RemoveFileResultDto.AUTH_ERROR); + } else { + resultDto.setError(RemoveFileResultDto.ERROR); + } + + return resultDto; + } + } catch (IOException e) { + if (log.isErrorEnabled()) { + log.error("Can not open git local repository : " + connection.getLocalDirectory().getAbsolutePath(), e); + } + + emptyFile.delete(); + file.delete(); + resultDto.setError(UploadFileResultDto.ERROR); + return resultDto; + } + + return resultDto; + } + + @Override + public RemoveDirectoryResultDto removeDirectory(RemoveDirectoryDto dto) { + + RemoveDirectoryResultDto resultDto = new RemoveDirectoryResultDto(); + + // getting the last version of the repository + try { + updateRepository(dto.getUsername(), dto.getPassword()); + } catch (RepositoryNotFoundException e) { + + if (log.isErrorEnabled()) { + log.error("Error while cloning or pulling the repository", e); + } + + resultDto.setError(RemoveFileResultDto.ERROR); + return resultDto; + + } catch (IOException e) { + + if (log.isErrorEnabled()) { + log.error("Error while cloning or pulling the repository", e); + } + + resultDto.setError(RemoveFileResultDto.ERROR); + return resultDto; + + } catch (AuthenticationException e) { + + if (log.isErrorEnabled()) { + log.error("Error while cloning or pulling the repository", e); + } + resultDto.setError(RemoveFileResultDto.AUTH_ERROR); + return resultDto; + } + + resultDto.setFileRoot(connection.getAddressGit()); + resultDto.setScmRoot(connection.getAddressGit()); + + // if there is no file to remove, we get back to the remove form + if (dto.getDirectoryToRemove() == null) { + resultDto.setError(RemoveFileResultDto.REDIRECT); + return resultDto; + } + if (dto.getDirectoryToRemove().equals("") || dto.getDirectoryToRemove().equals(connection.getAddressGit())) { + resultDto.setError(RemoveFileResultDto.REDIRECT); + return resultDto; + } + + // Removing the directory from the local directory + String pathOnRepo = dto.getDirectoryToRemove(); + + if (pathOnRepo.length() > connection.getAddressGit().length()) { + pathOnRepo = pathOnRepo.replace(connection.getAddressGit() + "/", ""); + } + + File directory = new File(connection.getLocalDirectory().getAbsolutePath() + File.separator + pathOnRepo); + + // authentication + if (dto.getUsername() == null) { + dto.setUsername("anonymous"); + } + + if (dto.getPassword() == null) { + dto.setPassword("anonymous"); + } + + CredentialsProvider credentials = new UsernamePasswordCredentialsProvider(dto.getUsername(), dto.getPassword()); + + try { + Git git = Git.open(connection.getLocalDirectory()); + + // removing the directory + RmCommand rm = git.rm(); + rm.addFilepattern(pathOnRepo); + try { + rm.call(); + } catch (GitAPIException e) { + if (log.isErrorEnabled()) { + log.error("Can not execute Git remove " + pathOnRepo, e); + } + + resultDto.setError(RemoveFileResultDto.ERROR); + return resultDto; + } + directory.delete(); + + // commit + try { + connection.doCommit(git, dto.getUsername(), "unknown", "From scmwebeditor -- remove the directory : " + pathOnRepo); + } catch (GitAPIException e) { + if (log.isErrorEnabled()) { + log.error("Can not commit", e); + } + + resultDto.setError(RemoveFileResultDto.ERROR); + return resultDto; + } + + // push + if (log.isDebugEnabled()) { + log.debug("Preparing push"); + } + + PushCommand push = git.push(); + push.setRemote(connection.getAddressGit()); + push.setCredentialsProvider(credentials); + + try { + push.call(); + } catch (GitAPIException e) { + + connection.handlePushException(e); + + try { + connection.cloneRepository(credentials); + } catch (AuthenticationException e1) { + if (log.isErrorEnabled()) { + log.error("Can not clone repository at address " + connection.getAddressGit()); + } + resultDto.setError(RemoveFileResultDto.AUTH_ERROR); + } catch (RepositoryNotFoundException e1) { + if (log.isErrorEnabled()) { + log.error("Can not clone repository at address " + connection.getAddressGit()); + } + resultDto.setError(RemoveFileResultDto.ERROR); + } + + if (e.getMessage().endsWith("not authorized")) { + resultDto.setError(RemoveFileResultDto.AUTH_ERROR); + } else { + resultDto.setError(RemoveFileResultDto.ERROR); + } + + return resultDto; + } + } catch (IOException e) { + if (log.isErrorEnabled()) { + log.error("Can not open git local repository : " + connection.getLocalDirectory().getAbsolutePath(), e); + } + + resultDto.setError(RemoveFileResultDto.ERROR); + return resultDto; + } + + return resultDto; + } + + @Override + public MoveFileResultDto moveFile(MoveFileDto dto) { + + MoveFileResultDto resultDto = new MoveFileResultDto(); + + // getting the last version of the repository + try { + updateRepository(dto.getUsername(), dto.getPassword()); + } catch (RepositoryNotFoundException e) { + + if (log.isErrorEnabled()) { + log.error("Error while cloning or pulling the repository", e); + } + + resultDto.setError(RemoveFileResultDto.ERROR); + return resultDto; + + } catch (IOException e) { + + if (log.isErrorEnabled()) { + log.error("Error while cloning or pulling the repository", e); + } + + resultDto.setError(RemoveFileResultDto.ERROR); + return resultDto; + + } catch (AuthenticationException e) { + + if (log.isErrorEnabled()) { + log.error("Error while cloning or pulling the repository", e); + } + resultDto.setError(RemoveFileResultDto.AUTH_ERROR); + return resultDto; + } + + resultDto.setFileRoot(connection.getAddressGit()); + resultDto.setScmRoot(connection.getAddressGit()); + + // if the name of the file to move is empty we get back to the move a file form + if (dto.getFileToMove() == null) { + resultDto.setError(MoveFileResultDto.REDIRECT); + return resultDto; + } + + if (dto.getFileToMove().equals("") || dto.getFileToMove().equals(resultDto.getFileRoot())) { + resultDto.setError(MoveFileResultDto.REDIRECT); + return resultDto; + } + + // if the name of the destination directory is empty we get back to the move a file form + if (dto.getDestinationDirectory() == null) { + resultDto.setError(MoveFileResultDto.REDIRECT); + return resultDto; + } + + if (dto.getDestinationDirectory().equals("")) { + resultDto.setError(MoveFileResultDto.REDIRECT); + return resultDto; + } + + // Moving the file on the local directory + String pathOnRepoFile = dto.getFileToMove(); + + if (pathOnRepoFile.length() > connection.getAddressGit().length()) { + pathOnRepoFile = pathOnRepoFile.replace(connection.getAddressGit() + "/", ""); + } + + String pathOnRepoDirectory = dto.getDestinationDirectory(); + + if (pathOnRepoDirectory.length() > connection.getAddressGit().length()) { + pathOnRepoDirectory = pathOnRepoDirectory.replace(connection.getAddressGit() + "/", ""); + } + + String sourceFileName = pathOnRepoFile.substring(pathOnRepoFile.lastIndexOf('/') + 1); + + File sourceFile = new File(connection.getLocalDirectory().getAbsolutePath() + File.separator + pathOnRepoFile); + File destFile = new File(connection.getLocalDirectory().getAbsolutePath() + File.separator + pathOnRepoDirectory + + File.separator + sourceFileName); + + try { + FileUtils.moveFile(sourceFile, destFile); + } catch (IOException e) { + if (log.isErrorEnabled()) { + log.error("Can not move file " + sourceFile.getAbsolutePath() + " to " + destFile.getAbsolutePath(), e); + } + return resultDto; + } + + // authentication + if (dto.getUsername() == null) { + dto.setUsername("anonymous"); + } + + if (dto.getPassword() == null) { + dto.setPassword("anonymous"); + } + + CredentialsProvider credentials = new UsernamePasswordCredentialsProvider(dto.getUsername(), dto.getPassword()); + + try { + Git git = Git.open(connection.getLocalDirectory()); + + // adding the moved file + AddCommand add = git.add(); + add.addFilepattern(pathOnRepoDirectory + "/" + sourceFileName); + try { + add.call(); + } catch (GitAPIException e) { + if (log.isErrorEnabled()) { + log.error("Can not execute Git add " + pathOnRepoDirectory + "/" + sourceFileName, e); + } + + resultDto.setError(MoveFileResultDto.ERROR); + return resultDto; + } + + // commit + try { + connection.doCommit(git, dto.getUsername(), "unknown", "From scmwebeditor -- move the file : " + pathOnRepoFile + + " to : " + pathOnRepoDirectory); + } catch (GitAPIException e) { + if (log.isErrorEnabled()) { + log.error("Can not commit", e); + } + + resultDto.setError(MoveFileResultDto.ERROR); + return resultDto; + } + + // push + if (log.isDebugEnabled()) { + log.debug("Preparing push"); + } + + PushCommand push = git.push(); + push.setRemote(connection.getAddressGit()); + push.setCredentialsProvider(credentials); + + try { + push.call(); + } catch (GitAPIException e) { + + connection.handlePushException(e); + + try { + connection.cloneRepository(credentials); + } catch (AuthenticationException e1) { + if (log.isErrorEnabled()) { + log.error("Can not clone repository at address " + connection.getAddressGit()); + } + resultDto.setError(MoveFileResultDto.AUTH_ERROR); + } catch (RepositoryNotFoundException e1) { + if (log.isErrorEnabled()) { + log.error("Can not clone repository at address " + connection.getAddressGit()); + } + resultDto.setError(MoveFileResultDto.ERROR); + } + + if (e.getMessage().endsWith("not authorized")) { + resultDto.setError(MoveFileResultDto.AUTH_ERROR); + } else { + resultDto.setError(MoveFileResultDto.ERROR); + } + + return resultDto; + } + } catch (IOException e) { + if (log.isErrorEnabled()) { + log.error("Can not open git local repository : " + connection.getLocalDirectory().getAbsolutePath(), e); + } + + resultDto.setError(MoveFileResultDto.ERROR); + return resultDto; + } + + return resultDto; + } + + + /** + * Updates the repository to the last version by a clone or a pull command + * @param username the username to use to connect to the repository + * @param password the password to use to connect to the repository + * @throws RepositoryNotFoundException if the repository is not found + * @throws IOException if it is not possible to reach the repository + * @throws AuthenticationException if there is a problem during the authentication process + */ + public void updateRepository(String username, String password) + throws RepositoryNotFoundException, IOException, AuthenticationException { + + // Cloning the remote repository to a local directory + String hashedAddress = connection.getAddressGit(); + + String hashResult = connection.hash(connection.getAddressGit(), "SHA-512"); + + if (hashResult != null) { + hashedAddress = hashResult; + } + + connection.setLocalDirectory(new File(connection.getPathToLocalRepos() + File.separator + hashedAddress)); + + CredentialsProvider credentials = null; + + if (username != null && password != null) { + credentials = new UsernamePasswordCredentialsProvider(username, password); + } + + if (!connection.getLocalDirectory().exists()) { + + connection.cloneRepository(credentials); + + } else { + + Git git = Git.open(connection.getLocalDirectory()); + + PullCommand pull = git.pull(); + pull.setCredentialsProvider(credentials); + + try { + pull.call(); + } catch (InvalidRemoteException e) { + if (log.isErrorEnabled()) { + log.error("Can't pull the remote repository", e); + } + connection.cloneRepository(credentials); + + } catch (JGitInternalException e) { + if (log.isErrorEnabled()) { + log.error("Can't pull the remote repository", e); + } + connection.cloneRepository(credentials); + + } catch (TransportException e) { + if (log.isErrorEnabled()) { + log.error("Can't pull the remote repository: " + connection.getAddressGit(), e); + } + + if (e.getMessage().endsWith("500 Internal Server Error")) { + throw new AuthenticationException("Can not pull the Git repository: auth failed"); + } else { + connection.cloneRepository(credentials); + } + } catch (GitAPIException e) { + if (log.isErrorEnabled()) { + log.error("Can't pull the remote repository", e); + } + connection.cloneRepository(credentials); + } + + if (log.isDebugEnabled()) { + log.debug("Pulled repository " + connection.getAddressGit()); + } + } + + + if (log.isDebugEnabled()) { + log.debug("Connection to local repository"); + } + + // Connection to the local repository + File gitFile = new File(connection.getLocalDirectory().getAbsolutePath() + File.separator + ".git"); + FileRepositoryBuilder gitRepoBuilder = new FileRepositoryBuilder(); + gitRepoBuilder.setGitDir(gitFile); + connection.setGitRepo(gitRepoBuilder.build()); + + if (!connection.getGitRepo().getObjectDatabase().exists()) { + + if (log.isErrorEnabled()) { + log.error("The repository at address " + connection.getAddressGit() + " doesn't exist"); + throw new RepositoryNotFoundException("The repository at address " + connection.getAddressGit() + " doesn't exist"); + } + } + } +} \ No newline at end of file diff --git a/swe-git/src/main/java/org/nuiton/scmwebeditor/git/GitProvider.java b/swe-git/src/main/java/org/nuiton/scmwebeditor/git/GitProvider.java index fefa5fc..d57a520 100644 --- a/swe-git/src/main/java/org/nuiton/scmwebeditor/git/GitProvider.java +++ b/swe-git/src/main/java/org/nuiton/scmwebeditor/git/GitProvider.java @@ -31,10 +31,7 @@ import org.eclipse.jgit.revwalk.RevCommit; import org.eclipse.jgit.revwalk.RevWalk; import org.eclipse.jgit.transport.CredentialsProvider; import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider; -import org.nuiton.scmwebeditor.api.OperationNotSupportedException; -import org.nuiton.scmwebeditor.api.RepositoryNotFoundException; -import org.nuiton.scmwebeditor.api.ScmConnection; -import org.nuiton.scmwebeditor.api.ScmProvider; +import org.nuiton.scmwebeditor.api.*; import org.nuiton.scmwebeditor.api.dto.CreateBranchDto; import org.nuiton.scmwebeditor.api.dto.result.AbstractResultDto; @@ -273,6 +270,26 @@ public class GitProvider implements ScmProvider { } @Override + public ScmFileManager getFileManager(ScmConnection connection) { + + GitFileManager fileManager = null; + + if (connection instanceof GitConnection) { + try { + fileManager = new GitFileManager((GitConnection) connection); + } catch (IOException e) { + if (log.isErrorEnabled()) { + log.error("Can not connect to Git repository", e); + } + } + } else { + throw new SweInternalException("Can not get Git file manager for a non-Git connection"); + } + + return fileManager; + } + + @Override public boolean addressSeemsCompatible(String address) { if (address.contains(".git") || address.startsWith("git://") || address.contains("git.")) { diff --git a/swe-scm-api/src/main/java/org/nuiton/scmwebeditor/api/ScmConnection.java b/swe-scm-api/src/main/java/org/nuiton/scmwebeditor/api/ScmConnection.java index 542f256..b0f23f5 100644 --- a/swe-scm-api/src/main/java/org/nuiton/scmwebeditor/api/ScmConnection.java +++ b/swe-scm-api/src/main/java/org/nuiton/scmwebeditor/api/ScmConnection.java @@ -49,46 +49,6 @@ public interface ScmConnection { /** - * Uploads a file to the repository as a new file - * @param dto the DTO which contains all the parameters - * @return a DTO which contains all the results - */ - UploadFileResultDto uploadFile(UploadFileDto dto); - - - /** - * Removes a file from the repository - * @param dto the DTO which contains all the parameters - * @return a DTO which contains all the results - */ - RemoveFileResultDto removeFile(RemoveFileDto dto); - - - /** - * Creates a new directory on the repository - * @param dto the DTO which contains all the parameters - * @return a DTO which contains all the results - */ - CreateDirectoryResultDto createDirectory(CreateDirectoryDto dto); - - - /** - * Removes a directory from the repository - * @param dto the DTO which contains all the parameters - * @return a DTO which contains all the results - */ - RemoveDirectoryResultDto removeDirectory(RemoveDirectoryDto dto); - - - /** - * Moves a file to a directory in the repository - * @param dto the DTO which contains all the parameters - * @return a DTo which contains all the results - */ - MoveFileResultDto moveFile(MoveFileDto dto); - - - /** * 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 diff --git a/swe-scm-api/src/main/java/org/nuiton/scmwebeditor/api/ScmConnection.java b/swe-scm-api/src/main/java/org/nuiton/scmwebeditor/api/ScmFileManager.java similarity index 50% copy from swe-scm-api/src/main/java/org/nuiton/scmwebeditor/api/ScmConnection.java copy to swe-scm-api/src/main/java/org/nuiton/scmwebeditor/api/ScmFileManager.java index 542f256..693d550 100644 --- a/swe-scm-api/src/main/java/org/nuiton/scmwebeditor/api/ScmConnection.java +++ b/swe-scm-api/src/main/java/org/nuiton/scmwebeditor/api/ScmFileManager.java @@ -28,25 +28,9 @@ import javax.naming.AuthenticationException; import java.io.File; /** - * An interface which gives the SCMs main features + * An interface which gives the SCMs features relative to file management */ -public interface ScmConnection { - - /** - * Searches the repository's files to make a list of them - * @param dto the DTO which contains all the parameters - * @return a DTO which contains all the results - */ - BrowseResultDto browse(BrowseDto dto); - - - /** - * Makes a commit of the changed made to the edited file - * @param dto the DTO which contains all the parameters - * @return a DTO which contains all the results - */ - CommitResultDto commit(CommitDto dto); - +public interface ScmFileManager { /** * Uploads a file to the repository as a new file @@ -86,49 +70,4 @@ public interface ScmConnection { * @return a DTo which contains all the results */ MoveFileResultDto moveFile(MoveFileDto dto); - - - /** - * 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 - * @throws AuthenticationException if there is a problem during the authentication process - */ - File getFileContent(String path, String username, String password) throws AuthenticationException; - - - /** - * 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 - * @throws AuthenticationException if there is a problem during the authentication process - */ - String getHeadRevisionNumber(String path, String username, String password) throws AuthenticationException; - - - /** - * Gives the repository's unique identifier - * @return the repository's unique identifier - */ - String getRepositoryId(); - - - /** - * Gives the name of the edited file - * @return the name of the edited file - */ - String getFileName(); - - - /** - * Gives the path to use to display an image on a web page - * @param address the full address of the image on the repository - * @param repositoryRoot the address of the repository's root - * @return the path to use to display an image on a web page - */ - String getImagePath(String address, String repositoryRoot); } diff --git a/swe-scm-api/src/main/java/org/nuiton/scmwebeditor/api/ScmProvider.java b/swe-scm-api/src/main/java/org/nuiton/scmwebeditor/api/ScmProvider.java index 08976e6..489d4d4 100644 --- a/swe-scm-api/src/main/java/org/nuiton/scmwebeditor/api/ScmProvider.java +++ b/swe-scm-api/src/main/java/org/nuiton/scmwebeditor/api/ScmProvider.java @@ -91,6 +91,13 @@ public interface ScmProvider { ScmConnection getConnection(String address, String pathToLocalRepos); /** + * Gives the file manager for the given connection + * @param connection the connection to the repository + * @return the file manager for the repository + */ + ScmFileManager getFileManager(ScmConnection connection); + + /** * Tells whether the given address seems compatible with the SCM * @param address the repository's address * @return true if the repository seems to be compatible with the SCM diff --git a/swe-svn/src/main/java/org/nuiton/scmwebeditor/svn/SvnConnection.java b/swe-svn/src/main/java/org/nuiton/scmwebeditor/svn/SvnConnection.java index b4da898..47df65d 100644 --- a/swe-svn/src/main/java/org/nuiton/scmwebeditor/svn/SvnConnection.java +++ b/swe-svn/src/main/java/org/nuiton/scmwebeditor/svn/SvnConnection.java @@ -25,8 +25,10 @@ import org.apache.commons.io.FileUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.nuiton.scmwebeditor.api.ScmConnection; -import org.nuiton.scmwebeditor.api.dto.*; -import org.nuiton.scmwebeditor.api.dto.result.*; +import org.nuiton.scmwebeditor.api.dto.BrowseDto; +import org.nuiton.scmwebeditor.api.dto.CommitDto; +import org.nuiton.scmwebeditor.api.dto.result.BrowseResultDto; +import org.nuiton.scmwebeditor.api.dto.result.CommitResultDto; import org.nuiton.util.FileUtil; import org.tmatesoft.svn.core.*; import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager; @@ -77,6 +79,42 @@ public class SvnConnection implements ScmConnection { protected SVNClientManager manager; + public String getAddressSvn() { return addressSvn; } + + public void setAddressSvn(String addressSvn) { this.addressSvn = addressSvn; } + + public String getSvnPath() { return svnPath; } + + public void setSvnPath(String svnPath) { this.svnPath = svnPath; } + + public void setFileName(String fileName) { this.fileName = fileName; } + + public File getCheckoutdir() { return checkoutdir; } + + public void setCheckoutdir(File checkoutdir) { this.checkoutdir = checkoutdir; } + + public SVNURL getRemoteUrl() { return remoteUrl; } + + public void setRemoteUrl(SVNURL remoteUrl) { this.remoteUrl = remoteUrl; } + + public ISVNAuthenticationManager getAuthManager() { return authManager; } + + public void setAuthManager( + ISVNAuthenticationManager authManager) { this.authManager = authManager; } + + public DefaultSVNOptions getSvnOption() { return svnOption; } + + public void setSvnOption(DefaultSVNOptions svnOption) { this.svnOption = svnOption; } + + public String getPathToLocalRepos() { return pathToLocalRepos; } + + public void setPathToLocalRepos(String pathToLocalRepos) { this.pathToLocalRepos = pathToLocalRepos; } + + public SVNClientManager getManager() { return manager; } + + public void setManager(SVNClientManager manager) { this.manager = manager; } + + public SvnConnection(String address, String pathToLocalRepos) throws SVNException { if(log.isDebugEnabled()) { @@ -413,443 +451,6 @@ public class SvnConnection implements ScmConnection { @Override - public UploadFileResultDto uploadFile(UploadFileDto dto) { - - UploadFileResultDto resultDto = new UploadFileResultDto(); - - if (dto.getUsername() == null) { - dto.setUsername("anonymous"); - } - - if (dto.getPassword() == null) { - dto.setPassword("anonymous"); - } - - resultDto.setScmRoot(getSvnRoot(dto.getUsername(), dto.getPassword())); - - if (resultDto.getScmRoot() == null) { - resultDto.setScmRoot(svnPath); - } - - if (svnPath.endsWith("/")) { - resultDto.setFileRoot(svnPath.substring(0, svnPath.lastIndexOf('/'))); - } else { - resultDto.setFileRoot(svnPath); - } - - updateAuthentication(dto.getUsername(), dto.getPassword()); - - try { - testConnection(); - } catch (SVNException e) { - if (log.isDebugEnabled()) { - log.debug("Test connection fail", e); - } - - resultDto.setError(UploadFileResultDto.CONNECTION_FAILED); - return resultDto; - } - - - // if there is no file to upload we get back to the upload form - if (dto.getUpload() == null) { - - resultDto.setError(UploadFileResultDto.REDIRECT); - return resultDto; - } - - if (log.isDebugEnabled()) { - log.debug("FileName : " + dto.getUploadFileName()); - log.debug("ContentType : " + dto.getUploadContentType()); - } - - SVNCommitClient commitClient = new SVNCommitClient(manager, svnOption); - - try { - commitClient.doImport(dto.getUpload(), SVNURL.parseURIEncoded(dto.getScmPath() + "/" + dto.getUploadFileName()), - "From scmwebeditor -- add the file : " + dto.getUploadFileName(), null, false, false, SVNDepth.EMPTY); - } catch (SVNAuthenticationException authexep) { - - if (log.isErrorEnabled()) { - log.error("authentication fail", authexep); - } - resultDto.setError(UploadFileResultDto.AUTH_ERROR); - - return resultDto; - - } catch (SVNException e) { - - if (log.isErrorEnabled()) { - log.error("Error SVN import", e); - } - resultDto.setError(UploadFileResultDto.ERROR); - - return resultDto; - - } - - return resultDto; - } - - @Override - public RemoveFileResultDto removeFile(RemoveFileDto dto) { - - RemoveFileResultDto resultDto = new RemoveFileResultDto(); - - if (dto.getUsername() == null) { - dto.setUsername("anonymous"); - } - - if (dto.getPassword() == null) { - dto.setPassword("anonymous"); - } - - resultDto.setScmRoot(getSvnRoot(dto.getUsername(), dto.getPassword())); - - if (resultDto.getScmRoot() == null) { - resultDto.setScmRoot(svnPath); - } - - if (svnPath.endsWith("/")) { - resultDto.setFileRoot(svnPath.substring(0, svnPath.lastIndexOf('/'))); - } else { - resultDto.setFileRoot(svnPath); - } - - updateAuthentication(dto.getUsername(), dto.getPassword()); - - try { - testConnection(); - } catch (SVNException e) { - if (log.isDebugEnabled()) { - log.debug("Test connection fail", e); - } - - resultDto.setError(RemoveFileResultDto.CONNECTION_FAILED); - return resultDto; - } - - - // if there is no file to remove we get back to the remove form - if (dto.getScmPath() == null) { - - resultDto.setError(RemoveFileResultDto.REDIRECT); - return resultDto; - } - - SVNCommitClient commitClient = new SVNCommitClient(manager, svnOption); - - try { - SVNURL scmPathTab[] = new SVNURL[1]; - scmPathTab[0] = SVNURL.parseURIEncoded(dto.getScmPath()); - commitClient.doDelete(scmPathTab, "From scmwebeditor -- remove the file : " + dto.getScmPath()); - } catch (SVNAuthenticationException authexep) { - - if (log.isErrorEnabled()) { - log.error("authentication fail", authexep); - } - resultDto.setError(RemoveFileResultDto.AUTH_ERROR); - - return resultDto; - - } catch (SVNException e) { - - if (log.isErrorEnabled()) { - log.error("Error SVN import", e); - } - resultDto.setError(RemoveFileResultDto.ERROR); - - return resultDto; - - } - - return resultDto; - } - - @Override - public CreateDirectoryResultDto createDirectory(CreateDirectoryDto dto) { - - CreateDirectoryResultDto resultDto = new CreateDirectoryResultDto(); - - if (dto.getUsername() == null) { - dto.setUsername("anonymous"); - } - - if (dto.getPassword() == null) { - dto.setPassword("anonymous"); - } - - resultDto.setScmRoot(getSvnRoot(dto.getUsername(), dto.getPassword())); - - if (resultDto.getScmRoot() == null) { - resultDto.setScmRoot(svnPath); - } - - if (svnPath.endsWith("/")) { - resultDto.setFileRoot(svnPath.substring(0, svnPath.lastIndexOf('/'))); - } else { - resultDto.setFileRoot(svnPath); - } - - updateAuthentication(dto.getUsername(), dto.getPassword()); - - try { - testConnection(); - } catch (SVNException e) { - if (log.isDebugEnabled()) { - log.debug("Test connection fail", e); - } - - resultDto.setError(CreateDirectoryResultDto.CONNECTION_FAILED); - return resultDto; - } - - - // if the name of the new directory is empty we get back to the create directory form - if (dto.getDirectoryName() == null) { - - resultDto.setError(CreateDirectoryResultDto.REDIRECT); - return resultDto; - } - - if (dto.getDirectoryName().equals("")) { - - resultDto.setError(CreateDirectoryResultDto.REDIRECT); - return resultDto; - } - - SVNCommitClient commitClient = new SVNCommitClient(manager, svnOption); - - try { - SVNURL[] urls = new SVNURL[1]; - urls[0] = SVNURL.parseURIEncoded(dto.getParentDirectory() + "/" + dto.getDirectoryName()); - - commitClient.doMkDir(urls, "From scmwebeditor -- create the directory : " + dto.getDirectoryName()); - } catch (SVNAuthenticationException authexep) { - - if (log.isErrorEnabled()) { - log.error("authentication fail", authexep); - } - resultDto.setError(CreateDirectoryResultDto.AUTH_ERROR); - - return resultDto; - - } catch (SVNException e) { - - if (log.isErrorEnabled()) { - log.error("Error SVN import", e); - } - resultDto.setError(CreateDirectoryResultDto.ERROR); - - return resultDto; - - } - - return resultDto; - } - - @Override - public RemoveDirectoryResultDto removeDirectory(RemoveDirectoryDto dto) { - - RemoveDirectoryResultDto resultDto = new RemoveDirectoryResultDto(); - - if (dto.getUsername() == null) { - dto.setUsername("anonymous"); - } - - if (dto.getPassword() == null) { - dto.setPassword("anonymous"); - } - - resultDto.setScmRoot(getSvnRoot(dto.getUsername(), dto.getPassword())); - - if (resultDto.getScmRoot() == null) { - resultDto.setScmRoot(svnPath); - } - - if (svnPath.endsWith("/")) { - resultDto.setFileRoot(svnPath.substring(0, svnPath.lastIndexOf('/'))); - } else { - resultDto.setFileRoot(svnPath); - } - - updateAuthentication(dto.getUsername(), dto.getPassword()); - - try { - testConnection(); - } catch (SVNException e) { - if (log.isDebugEnabled()) { - log.debug("Test connection fail", e); - } - - resultDto.setError(RemoveDirectoryResultDto.CONNECTION_FAILED); - return resultDto; - } - - - // if the name of the directory to remove is empty we get back to the remove directory form - if (dto.getDirectoryToRemove() == null) { - - resultDto.setError(RemoveDirectoryResultDto.REDIRECT); - return resultDto; - } - - if (dto.getDirectoryToRemove().equals("") || dto.getDirectoryToRemove().equals(resultDto.getFileRoot())) { - - resultDto.setError(RemoveDirectoryResultDto.REDIRECT); - return resultDto; - } - - SVNCommitClient commitClient = new SVNCommitClient(manager, svnOption); - - try { - SVNURL[] urls = new SVNURL[1]; - urls[0] = SVNURL.parseURIEncoded(dto.getDirectoryToRemove()); - - commitClient.doDelete(urls, "From scmwebeditor -- remove the directory : " + dto.getDirectoryToRemove()); - } catch (SVNAuthenticationException authexep) { - - if (log.isErrorEnabled()) { - log.error("authentication fail", authexep); - } - resultDto.setError(RemoveDirectoryResultDto.AUTH_ERROR); - - return resultDto; - - } catch (SVNException e) { - - if (log.isErrorEnabled()) { - log.error("Error SVN import", e); - } - resultDto.setError(RemoveDirectoryResultDto.ERROR); - - return resultDto; - - } - - return resultDto; - } - - @Override - public MoveFileResultDto moveFile(MoveFileDto dto) { - - MoveFileResultDto resultDto = new MoveFileResultDto(); - - if (dto.getUsername() == null) { - dto.setUsername("anonymous"); - } - - if (dto.getPassword() == null) { - dto.setPassword("anonymous"); - } - - resultDto.setScmRoot(getSvnRoot(dto.getUsername(), dto.getPassword())); - - if (resultDto.getScmRoot() == null) { - resultDto.setScmRoot(svnPath); - } - - if (svnPath.endsWith("/")) { - resultDto.setFileRoot(svnPath.substring(0, svnPath.lastIndexOf('/'))); - } else { - resultDto.setFileRoot(svnPath); - } - - updateAuthentication(dto.getUsername(), dto.getPassword()); - - try { - testConnection(); - } catch (SVNException e) { - if (log.isDebugEnabled()) { - log.debug("Test connection fail", e); - } - - resultDto.setError(MoveFileResultDto.CONNECTION_FAILED); - return resultDto; - } - - // if the name of the file to move is empty we get back to the move a file form - if (dto.getFileToMove() == null) { - resultDto.setError(MoveFileResultDto.REDIRECT); - return resultDto; - } - - if (dto.getFileToMove().equals("") || dto.getFileToMove().equals(resultDto.getFileRoot())) { - resultDto.setError(MoveFileResultDto.REDIRECT); - return resultDto; - } - - // if the name of the destination directory is empty we get back to the move a file form - if (dto.getDestinationDirectory() == null) { - resultDto.setError(MoveFileResultDto.REDIRECT); - return resultDto; - } - - if (dto.getDestinationDirectory().equals("")) { - resultDto.setError(MoveFileResultDto.REDIRECT); - return resultDto; - } - - // getting thr URLs - - String fileName = dto.getFileToMove().substring(dto.getFileToMove().lastIndexOf('/') + 1); - - SVNURL sourceUrl; - - try { - sourceUrl = SVNURL.parseURIEncoded(dto.getFileToMove()); - } catch (SVNException e) { - if (log.isErrorEnabled()) { - log.error("Can not get source file URL " + dto.getFileToMove(), e); - } - return resultDto; - } - - SVNURL destUrl; - - try { - destUrl = SVNURL.parseURIEncoded(dto.getDestinationDirectory() + "/" + fileName); - } catch (SVNException e) { - if (log.isErrorEnabled()) { - log.error("Can not get destination file URL " + dto.getDestinationDirectory() + "/" + fileName, e); - } - return resultDto; - } - - SVNCopySource[] sourceFileTab = new SVNCopySource[1]; - sourceFileTab[0] = new SVNCopySource(SVNRevision.HEAD, SVNRevision.HEAD, sourceUrl); - - // moving the file - SVNCopyClient copyClient = new SVNCopyClient(manager, svnOption); - - try { - copyClient.doCopy(sourceFileTab, destUrl, true, false, true, "From scmwebeditor -- move the file : " - + sourceUrl.getPath() + " to : " + destUrl.getPath(), null); - } catch (SVNAuthenticationException authexep) { - - if (log.isErrorEnabled()) { - log.error("authentication fail", authexep); - } - resultDto.setError(MoveFileResultDto.AUTH_ERROR); - - return resultDto; - - } catch (SVNException e) { - - if (log.isErrorEnabled()) { - log.error("Error SVN import", e); - } - resultDto.setError(MoveFileResultDto.ERROR); - - return resultDto; - - } - - return resultDto; - } - - - @Override public File getFileContent(String path, String username, String password) throws AuthenticationException { String url = path.substring(0, path.lastIndexOf("/")); diff --git a/swe-svn/src/main/java/org/nuiton/scmwebeditor/svn/SvnFileManager.java b/swe-svn/src/main/java/org/nuiton/scmwebeditor/svn/SvnFileManager.java new file mode 100644 index 0000000..a4fc1d3 --- /dev/null +++ b/swe-svn/src/main/java/org/nuiton/scmwebeditor/svn/SvnFileManager.java @@ -0,0 +1,495 @@ +/* + * #%L + * ScmWebEditor + * %% + * Copyright (C) 2009 - 2015 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.svn; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.nuiton.scmwebeditor.api.ScmFileManager; +import org.nuiton.scmwebeditor.api.dto.*; +import org.nuiton.scmwebeditor.api.dto.result.*; +import org.tmatesoft.svn.core.SVNAuthenticationException; +import org.tmatesoft.svn.core.SVNDepth; +import org.tmatesoft.svn.core.SVNException; +import org.tmatesoft.svn.core.SVNURL; +import org.tmatesoft.svn.core.wc.SVNCommitClient; +import org.tmatesoft.svn.core.wc.SVNCopyClient; +import org.tmatesoft.svn.core.wc.SVNCopySource; +import org.tmatesoft.svn.core.wc.SVNRevision; + +/** + * Implementation of the SVN's features related to file management + */ +public class SvnFileManager implements ScmFileManager { + + + private static final Log log = LogFactory.getLog(SvnFileManager.class); + + /** the connection to the Git repository */ + SvnConnection connection; + + + public SvnFileManager(SvnConnection connection) throws SVNException { + + if(log.isDebugEnabled()) { + log.debug("SVN repository"); + } + + this.connection = connection; + } + + + @Override + public UploadFileResultDto uploadFile(UploadFileDto dto) { + + UploadFileResultDto resultDto = new UploadFileResultDto(); + + if (dto.getUsername() == null) { + dto.setUsername("anonymous"); + } + + if (dto.getPassword() == null) { + dto.setPassword("anonymous"); + } + + resultDto.setScmRoot(connection.getSvnRoot(dto.getUsername(), dto.getPassword())); + + if (resultDto.getScmRoot() == null) { + resultDto.setScmRoot(connection.getSvnPath()); + } + + if (connection.getSvnPath().endsWith("/")) { + resultDto.setFileRoot(connection.getSvnPath().substring(0, connection.getSvnPath().lastIndexOf('/'))); + } else { + resultDto.setFileRoot(connection.getSvnPath()); + } + + connection.updateAuthentication(dto.getUsername(), dto.getPassword()); + + try { + connection.testConnection(); + } catch (SVNException e) { + if (log.isDebugEnabled()) { + log.debug("Test connection fail", e); + } + + resultDto.setError(UploadFileResultDto.CONNECTION_FAILED); + return resultDto; + } + + + // if there is no file to upload we get back to the upload form + if (dto.getUpload() == null) { + + resultDto.setError(UploadFileResultDto.REDIRECT); + return resultDto; + } + + if (log.isDebugEnabled()) { + log.debug("FileName : " + dto.getUploadFileName()); + log.debug("ContentType : " + dto.getUploadContentType()); + } + + SVNCommitClient commitClient = new SVNCommitClient(connection.getManager(), connection.getSvnOption()); + + try { + commitClient.doImport(dto.getUpload(), SVNURL.parseURIEncoded(dto.getScmPath() + "/" + dto.getUploadFileName()), + "From scmwebeditor -- add the file : " + dto.getUploadFileName(), null, false, false, SVNDepth.EMPTY); + } catch (SVNAuthenticationException authexep) { + + if (log.isErrorEnabled()) { + log.error("authentication fail", authexep); + } + resultDto.setError(UploadFileResultDto.AUTH_ERROR); + + return resultDto; + + } catch (SVNException e) { + + if (log.isErrorEnabled()) { + log.error("Error SVN import", e); + } + resultDto.setError(UploadFileResultDto.ERROR); + + return resultDto; + + } + + return resultDto; + } + + @Override + public RemoveFileResultDto removeFile(RemoveFileDto dto) { + + RemoveFileResultDto resultDto = new RemoveFileResultDto(); + + if (dto.getUsername() == null) { + dto.setUsername("anonymous"); + } + + if (dto.getPassword() == null) { + dto.setPassword("anonymous"); + } + + resultDto.setScmRoot(connection.getSvnRoot(dto.getUsername(), dto.getPassword())); + + if (resultDto.getScmRoot() == null) { + resultDto.setScmRoot(connection.getSvnPath()); + } + + if (connection.getSvnPath().endsWith("/")) { + resultDto.setFileRoot(connection.getSvnPath().substring(0, connection.getSvnPath().lastIndexOf('/'))); + } else { + resultDto.setFileRoot(connection.getSvnPath()); + } + + connection.updateAuthentication(dto.getUsername(), dto.getPassword()); + + try { + connection.testConnection(); + } catch (SVNException e) { + if (log.isDebugEnabled()) { + log.debug("Test connection fail", e); + } + + resultDto.setError(RemoveFileResultDto.CONNECTION_FAILED); + return resultDto; + } + + + // if there is no file to remove we get back to the remove form + if (dto.getScmPath() == null) { + + resultDto.setError(RemoveFileResultDto.REDIRECT); + return resultDto; + } + + SVNCommitClient commitClient = new SVNCommitClient(connection.getManager(), connection.getSvnOption()); + + try { + SVNURL scmPathTab[] = new SVNURL[1]; + scmPathTab[0] = SVNURL.parseURIEncoded(dto.getScmPath()); + commitClient.doDelete(scmPathTab, "From scmwebeditor -- remove the file : " + dto.getScmPath()); + } catch (SVNAuthenticationException authexep) { + + if (log.isErrorEnabled()) { + log.error("authentication fail", authexep); + } + resultDto.setError(RemoveFileResultDto.AUTH_ERROR); + + return resultDto; + + } catch (SVNException e) { + + if (log.isErrorEnabled()) { + log.error("Error SVN import", e); + } + resultDto.setError(RemoveFileResultDto.ERROR); + + return resultDto; + + } + + return resultDto; + } + + @Override + public CreateDirectoryResultDto createDirectory(CreateDirectoryDto dto) { + + CreateDirectoryResultDto resultDto = new CreateDirectoryResultDto(); + + if (dto.getUsername() == null) { + dto.setUsername("anonymous"); + } + + if (dto.getPassword() == null) { + dto.setPassword("anonymous"); + } + + resultDto.setScmRoot(connection.getSvnRoot(dto.getUsername(), dto.getPassword())); + + if (resultDto.getScmRoot() == null) { + resultDto.setScmRoot(connection.getSvnPath()); + } + + if (connection.getSvnPath().endsWith("/")) { + resultDto.setFileRoot(connection.getSvnPath().substring(0, connection.getSvnPath().lastIndexOf('/'))); + } else { + resultDto.setFileRoot(connection.getSvnPath()); + } + + connection.updateAuthentication(dto.getUsername(), dto.getPassword()); + + try { + connection.testConnection(); + } catch (SVNException e) { + if (log.isDebugEnabled()) { + log.debug("Test connection fail", e); + } + + resultDto.setError(CreateDirectoryResultDto.CONNECTION_FAILED); + return resultDto; + } + + + // if the name of the new directory is empty we get back to the create directory form + if (dto.getDirectoryName() == null) { + + resultDto.setError(CreateDirectoryResultDto.REDIRECT); + return resultDto; + } + + if (dto.getDirectoryName().equals("")) { + + resultDto.setError(CreateDirectoryResultDto.REDIRECT); + return resultDto; + } + + SVNCommitClient commitClient = new SVNCommitClient(connection.getManager(), connection.getSvnOption()); + + try { + SVNURL[] urls = new SVNURL[1]; + urls[0] = SVNURL.parseURIEncoded(dto.getParentDirectory() + "/" + dto.getDirectoryName()); + + commitClient.doMkDir(urls, "From scmwebeditor -- create the directory : " + dto.getDirectoryName()); + } catch (SVNAuthenticationException authexep) { + + if (log.isErrorEnabled()) { + log.error("authentication fail", authexep); + } + resultDto.setError(CreateDirectoryResultDto.AUTH_ERROR); + + return resultDto; + + } catch (SVNException e) { + + if (log.isErrorEnabled()) { + log.error("Error SVN import", e); + } + resultDto.setError(CreateDirectoryResultDto.ERROR); + + return resultDto; + + } + + return resultDto; + } + + @Override + public RemoveDirectoryResultDto removeDirectory(RemoveDirectoryDto dto) { + + RemoveDirectoryResultDto resultDto = new RemoveDirectoryResultDto(); + + if (dto.getUsername() == null) { + dto.setUsername("anonymous"); + } + + if (dto.getPassword() == null) { + dto.setPassword("anonymous"); + } + + resultDto.setScmRoot(connection.getSvnRoot(dto.getUsername(), dto.getPassword())); + + if (resultDto.getScmRoot() == null) { + resultDto.setScmRoot(connection.getSvnPath()); + } + + if (connection.getSvnPath().endsWith("/")) { + resultDto.setFileRoot(connection.getSvnPath().substring(0, connection.getSvnPath().lastIndexOf('/'))); + } else { + resultDto.setFileRoot(connection.getSvnPath()); + } + + connection.updateAuthentication(dto.getUsername(), dto.getPassword()); + + try { + connection.testConnection(); + } catch (SVNException e) { + if (log.isDebugEnabled()) { + log.debug("Test connection fail", e); + } + + resultDto.setError(RemoveDirectoryResultDto.CONNECTION_FAILED); + return resultDto; + } + + + // if the name of the directory to remove is empty we get back to the remove directory form + if (dto.getDirectoryToRemove() == null) { + + resultDto.setError(RemoveDirectoryResultDto.REDIRECT); + return resultDto; + } + + if (dto.getDirectoryToRemove().equals("") || dto.getDirectoryToRemove().equals(resultDto.getFileRoot())) { + + resultDto.setError(RemoveDirectoryResultDto.REDIRECT); + return resultDto; + } + + SVNCommitClient commitClient = new SVNCommitClient(connection.getManager(), connection.getSvnOption()); + + try { + SVNURL[] urls = new SVNURL[1]; + urls[0] = SVNURL.parseURIEncoded(dto.getDirectoryToRemove()); + + commitClient.doDelete(urls, "From scmwebeditor -- remove the directory : " + dto.getDirectoryToRemove()); + } catch (SVNAuthenticationException authexep) { + + if (log.isErrorEnabled()) { + log.error("authentication fail", authexep); + } + resultDto.setError(RemoveDirectoryResultDto.AUTH_ERROR); + + return resultDto; + + } catch (SVNException e) { + + if (log.isErrorEnabled()) { + log.error("Error SVN import", e); + } + resultDto.setError(RemoveDirectoryResultDto.ERROR); + + return resultDto; + + } + + return resultDto; + } + + @Override + public MoveFileResultDto moveFile(MoveFileDto dto) { + + MoveFileResultDto resultDto = new MoveFileResultDto(); + + if (dto.getUsername() == null) { + dto.setUsername("anonymous"); + } + + if (dto.getPassword() == null) { + dto.setPassword("anonymous"); + } + + resultDto.setScmRoot(connection.getSvnRoot(dto.getUsername(), dto.getPassword())); + + if (resultDto.getScmRoot() == null) { + resultDto.setScmRoot(connection.getSvnPath()); + } + + if (connection.getSvnPath().endsWith("/")) { + resultDto.setFileRoot(connection.getSvnPath().substring(0, connection.getSvnPath().lastIndexOf('/'))); + } else { + resultDto.setFileRoot(connection.getSvnPath()); + } + + connection.updateAuthentication(dto.getUsername(), dto.getPassword()); + + try { + connection.testConnection(); + } catch (SVNException e) { + if (log.isDebugEnabled()) { + log.debug("Test connection fail", e); + } + + resultDto.setError(MoveFileResultDto.CONNECTION_FAILED); + return resultDto; + } + + // if the name of the file to move is empty we get back to the move a file form + if (dto.getFileToMove() == null) { + resultDto.setError(MoveFileResultDto.REDIRECT); + return resultDto; + } + + if (dto.getFileToMove().equals("") || dto.getFileToMove().equals(resultDto.getFileRoot())) { + resultDto.setError(MoveFileResultDto.REDIRECT); + return resultDto; + } + + // if the name of the destination directory is empty we get back to the move a file form + if (dto.getDestinationDirectory() == null) { + resultDto.setError(MoveFileResultDto.REDIRECT); + return resultDto; + } + + if (dto.getDestinationDirectory().equals("")) { + resultDto.setError(MoveFileResultDto.REDIRECT); + return resultDto; + } + + // getting thr URLs + + String fileName = dto.getFileToMove().substring(dto.getFileToMove().lastIndexOf('/') + 1); + + SVNURL sourceUrl; + + try { + sourceUrl = SVNURL.parseURIEncoded(dto.getFileToMove()); + } catch (SVNException e) { + if (log.isErrorEnabled()) { + log.error("Can not get source file URL " + dto.getFileToMove(), e); + } + return resultDto; + } + + SVNURL destUrl; + + try { + destUrl = SVNURL.parseURIEncoded(dto.getDestinationDirectory() + "/" + fileName); + } catch (SVNException e) { + if (log.isErrorEnabled()) { + log.error("Can not get destination file URL " + dto.getDestinationDirectory() + "/" + fileName, e); + } + return resultDto; + } + + SVNCopySource[] sourceFileTab = new SVNCopySource[1]; + sourceFileTab[0] = new SVNCopySource(SVNRevision.HEAD, SVNRevision.HEAD, sourceUrl); + + // moving the file + SVNCopyClient copyClient = new SVNCopyClient(connection.getManager(), connection.getSvnOption()); + + try { + copyClient.doCopy(sourceFileTab, destUrl, true, false, true, "From scmwebeditor -- move the file : " + + sourceUrl.getPath() + " to : " + destUrl.getPath(), null); + } catch (SVNAuthenticationException authexep) { + + if (log.isErrorEnabled()) { + log.error("authentication fail", authexep); + } + resultDto.setError(MoveFileResultDto.AUTH_ERROR); + + return resultDto; + + } catch (SVNException e) { + + if (log.isErrorEnabled()) { + log.error("Error SVN import", e); + } + resultDto.setError(MoveFileResultDto.ERROR); + + return resultDto; + + } + + return resultDto; + } +} \ No newline at end of file diff --git a/swe-svn/src/main/java/org/nuiton/scmwebeditor/svn/SvnProvider.java b/swe-svn/src/main/java/org/nuiton/scmwebeditor/svn/SvnProvider.java index 52e73f3..a02061d 100644 --- a/swe-svn/src/main/java/org/nuiton/scmwebeditor/svn/SvnProvider.java +++ b/swe-svn/src/main/java/org/nuiton/scmwebeditor/svn/SvnProvider.java @@ -24,10 +24,7 @@ package org.nuiton.scmwebeditor.svn; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.nuiton.scmwebeditor.api.OperationNotSupportedException; -import org.nuiton.scmwebeditor.api.RepositoryNotFoundException; -import org.nuiton.scmwebeditor.api.ScmConnection; -import org.nuiton.scmwebeditor.api.ScmProvider; +import org.nuiton.scmwebeditor.api.*; import org.nuiton.scmwebeditor.api.dto.CreateBranchDto; import org.tmatesoft.svn.core.SVNException; import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory; @@ -102,6 +99,26 @@ public class SvnProvider implements ScmProvider { } @Override + public ScmFileManager getFileManager(ScmConnection connection) { + + SvnFileManager fileManager = null; + + if (connection instanceof SvnConnection) { + try { + fileManager = new SvnFileManager((SvnConnection) connection); + } catch (SVNException e) { + if (log.isErrorEnabled()) { + log.error("Can not connect to SVN repository", e); + } + } + } else { + throw new SweInternalException("Can not get SVN file manager for a non-SVN connection"); + } + + return fileManager; + } + + @Override public boolean addressSeemsCompatible(String address) { if (address.startsWith("svn://") || address.contains("svn.") || address.contains("subversion.")) { diff --git a/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/CreateDirectoryAction.java b/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/CreateDirectoryAction.java index d551e24..4f0cbb1 100644 --- a/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/CreateDirectoryAction.java +++ b/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/CreateDirectoryAction.java @@ -25,6 +25,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.struts2.interceptor.ServletRequestAware; import org.nuiton.scmwebeditor.api.ScmConnection; +import org.nuiton.scmwebeditor.api.ScmFileManager; import org.nuiton.scmwebeditor.api.ScmProvider; import org.nuiton.scmwebeditor.api.dto.CreateDirectoryDto; import org.nuiton.scmwebeditor.api.dto.result.CreateDirectoryResultDto; @@ -146,6 +147,7 @@ public class CreateDirectoryAction extends AbstractScmWebEditorAction implements ScmProvider provider = ScmWebEditorConfig.getProvider(scmType); ScmConnection scmConn = provider.getConnection(address, pathToLocalRepos); + ScmFileManager scmFileManager = provider.getFileManager(scmConn); // if the repository is not protected for writing, we get its UUID if (address.endsWith("/")) { @@ -168,7 +170,7 @@ public class CreateDirectoryAction extends AbstractScmWebEditorAction implements dto.setDirectoryName(directoryName); dto.setParentDirectory(parentDirectory); - CreateDirectoryResultDto resultDto = scmConn.createDirectory(dto); + CreateDirectoryResultDto resultDto = scmFileManager.createDirectory(dto); if (resultDto.getScmRoot() != null) { scmRoot = resultDto.getScmRoot(); diff --git a/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/MoveFileAction.java b/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/MoveFileAction.java index 54dee46..80c2a31 100644 --- a/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/MoveFileAction.java +++ b/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/MoveFileAction.java @@ -25,6 +25,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.struts2.interceptor.ServletRequestAware; import org.nuiton.scmwebeditor.api.ScmConnection; +import org.nuiton.scmwebeditor.api.ScmFileManager; import org.nuiton.scmwebeditor.api.ScmProvider; import org.nuiton.scmwebeditor.api.dto.MoveFileDto; import org.nuiton.scmwebeditor.api.dto.result.MoveFileResultDto; @@ -154,6 +155,7 @@ public class MoveFileAction extends AbstractScmWebEditorAction implements Servle ScmProvider provider = ScmWebEditorConfig.getProvider(scmType); ScmConnection scmConn = provider.getConnection(address, pathToLocalRepos); + ScmFileManager scmFileManager = provider.getFileManager(scmConn); // if the repository is not protected for writing, we get its UUID if (address.endsWith("/")) { @@ -177,7 +179,7 @@ public class MoveFileAction extends AbstractScmWebEditorAction implements Servle dto.setFileToMove(fileToMove); dto.setDestinationDirectory(destinationDirectory); - MoveFileResultDto resultDto = scmConn.moveFile(dto); + MoveFileResultDto resultDto = scmFileManager.moveFile(dto); if (resultDto.getScmRoot() != null) { scmRoot = resultDto.getScmRoot(); diff --git a/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/RemoveDirectoryAction.java b/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/RemoveDirectoryAction.java index f16b8f7..1d3b2ee 100644 --- a/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/RemoveDirectoryAction.java +++ b/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/RemoveDirectoryAction.java @@ -25,6 +25,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.struts2.interceptor.ServletRequestAware; import org.nuiton.scmwebeditor.api.ScmConnection; +import org.nuiton.scmwebeditor.api.ScmFileManager; import org.nuiton.scmwebeditor.api.ScmProvider; import org.nuiton.scmwebeditor.api.dto.RemoveDirectoryDto; import org.nuiton.scmwebeditor.api.dto.result.RemoveDirectoryResultDto; @@ -139,6 +140,7 @@ public class RemoveDirectoryAction extends AbstractScmWebEditorAction implements ScmProvider provider = ScmWebEditorConfig.getProvider(scmType); ScmConnection scmConn = provider.getConnection(address, pathToLocalRepos); + ScmFileManager scmFileManager = provider.getFileManager(scmConn); // if the repository is not protected for writing, we get its UUID if (address.endsWith("/")) { @@ -160,7 +162,7 @@ public class RemoveDirectoryAction extends AbstractScmWebEditorAction implements dto.setPassword(pw); dto.setDirectoryToRemove(directoryToRemove); - RemoveDirectoryResultDto resultDto = scmConn.removeDirectory(dto); + RemoveDirectoryResultDto resultDto = scmFileManager.removeDirectory(dto); if (resultDto.getScmRoot() != null) { scmRoot = resultDto.getScmRoot(); diff --git a/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/RemoveFileAction.java b/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/RemoveFileAction.java index fdad038..1e5a3e3 100644 --- a/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/RemoveFileAction.java +++ b/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/RemoveFileAction.java @@ -25,6 +25,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.struts2.interceptor.ServletRequestAware; import org.nuiton.scmwebeditor.api.ScmConnection; +import org.nuiton.scmwebeditor.api.ScmFileManager; import org.nuiton.scmwebeditor.api.ScmProvider; import org.nuiton.scmwebeditor.api.dto.RemoveFileDto; import org.nuiton.scmwebeditor.api.dto.result.RemoveFileResultDto; @@ -139,6 +140,7 @@ public class RemoveFileAction extends AbstractScmWebEditorAction implements Serv ScmProvider provider = ScmWebEditorConfig.getProvider(scmType); ScmConnection scmConn = provider.getConnection(address, pathToLocalRepos); + ScmFileManager scmFileManager = provider.getFileManager(scmConn); // if the repository is not protected for writing, we get its UUID if (address.endsWith("/")) { @@ -160,7 +162,7 @@ public class RemoveFileAction extends AbstractScmWebEditorAction implements Serv dto.setPassword(pw); dto.setScmPath(scmPath); - RemoveFileResultDto resultDto = scmConn.removeFile(dto); + RemoveFileResultDto resultDto = scmFileManager.removeFile(dto); if (resultDto.getScmRoot() != null) { scmRoot = resultDto.getScmRoot(); diff --git a/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/UploadFileAction.java b/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/UploadFileAction.java index fb4688b..e3a393c 100644 --- a/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/UploadFileAction.java +++ b/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/UploadFileAction.java @@ -25,6 +25,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.struts2.interceptor.ServletRequestAware; import org.nuiton.scmwebeditor.api.ScmConnection; +import org.nuiton.scmwebeditor.api.ScmFileManager; import org.nuiton.scmwebeditor.api.ScmProvider; import org.nuiton.scmwebeditor.api.dto.UploadFileDto; import org.nuiton.scmwebeditor.api.dto.result.UploadFileResultDto; @@ -175,6 +176,7 @@ public class UploadFileAction extends AbstractScmWebEditorAction implements Serv ScmProvider provider = ScmWebEditorConfig.getProvider(scmType); ScmConnection scmConn = provider.getConnection(address, pathToLocalRepos); + ScmFileManager scmFileManager = provider.getFileManager(scmConn); // if the repository is not protected for writing, we get its UUID if (address.endsWith("/")) { @@ -199,7 +201,7 @@ public class UploadFileAction extends AbstractScmWebEditorAction implements Serv dto.setUploadContentType(uploadContentType); dto.setScmPath(scmPath); - UploadFileResultDto resultDto = scmConn.uploadFile(dto); + UploadFileResultDto resultDto = scmFileManager.uploadFile(dto); if (resultDto.getFileRoot() != null) { fileRoot = resultDto.getFileRoot(); diff --git a/swe-ui-web/src/main/resources/scmwebeditor.properties b/swe-ui-web/src/main/resources/scmwebeditor.properties index 151b2e1..bd65c9a 100644 --- a/swe-ui-web/src/main/resources/scmwebeditor.properties +++ b/swe-ui-web/src/main/resources/scmwebeditor.properties @@ -22,7 +22,7 @@ swe.editableFiles=text,xml,javascript,sh,x-tex,x-java swe.cookiePrivateKey=ZvcCyhfRTVZoQz3B/IpYdw== swe.localRepositoriesPath=/var/local/swe -swe.enableAutoSave=true +swe.enableAutoSave=false # the auto save interval is set in milliseconds swe.autoSaveInterval=300000 swe.provider.SVN=org.nuiton.scmwebeditor.svn.SvnProvider -- To stop receiving notification emails like this one, please contact nuiton.org SCM administrator <admin+scm@nuiton.org>.