This is an automated email from the git hooks/post-receive script. New commit to branch feature/GIT in repository scmwebeditor. See http://git.nuiton.org/scmwebeditor.git commit 3ad1bbc18dace56a26daf5158627a1046351b1e2 Author: Hugo PIGEON <hpigeon@codelutin.com> Date: Mon May 4 14:43:28 2015 +0200 Added DTOs for the commit --- .../org/nuiton/scmwebeditor/GitConnection.java | 70 +++++++++++------- .../org/nuiton/scmwebeditor/ScmConnection.java | 14 ++-- .../org/nuiton/scmwebeditor/SvnConnection.java | 82 ++++++++++++---------- .../actions/ScmWebEditorCommitAction.java | 51 +++++++++++++- .../org/nuiton/scmwebeditor/dto/CommitDto.java | 71 +++++++++++++++++++ .../nuiton/scmwebeditor/dto/CommitResultDto.java | 79 +++++++++++++++++++++ 6 files changed, 295 insertions(+), 72 deletions(-) diff --git a/src/main/java/org/nuiton/scmwebeditor/GitConnection.java b/src/main/java/org/nuiton/scmwebeditor/GitConnection.java index 9b8213e..19d66d3 100644 --- a/src/main/java/org/nuiton/scmwebeditor/GitConnection.java +++ b/src/main/java/org/nuiton/scmwebeditor/GitConnection.java @@ -45,6 +45,9 @@ import org.nuiton.scmwebeditor.actions.ScmWebEditorCommitAction; import org.nuiton.scmwebeditor.actions.UploadAction; import org.nuiton.scmwebeditor.dto.BrowseDto; import org.nuiton.scmwebeditor.dto.BrowseResultDto; +import org.nuiton.scmwebeditor.dto.CommitDto; +import org.nuiton.scmwebeditor.dto.CommitResultDto; +import sun.security.x509.CRLDistributionPointsExtension; import javax.naming.AuthenticationException; import java.io.File; @@ -304,30 +307,35 @@ public class GitConnection implements ScmConnection { @Override - public String commit(ScmWebEditorCommitAction action) { + public CommitResultDto commit(CommitDto dto) { + + CommitResultDto resultDto = new CommitResultDto(); try { - cloneRepository(action.getUsername(), action.getPw()); + cloneRepository(dto.getUsername(), dto.getPassword()); } catch (ScmNotFoundException e) { if (log.isErrorEnabled()) { log.error("Error while cloning the repository", e); } - return action.SUCCESS; + resultDto.setError(CommitResultDto.ERROR); + return resultDto; } catch (IOException e) { if (log.isErrorEnabled()) { log.error("Error while cloning the repository", e); } - return action.SUCCESS; + resultDto.setError(CommitResultDto.ERROR); + return resultDto; } catch (AuthenticationException e) { if (log.isErrorEnabled()) { log.error("Error while cloning the repository", e); } - return action.LOGIN; + resultDto.setError(CommitResultDto.AUTH_ERROR); + return resultDto; } if (log.isDebugEnabled()) { @@ -336,39 +344,43 @@ public class GitConnection implements ScmConnection { File localFile = new File(localDirectory.getAbsolutePath() + "/" + fileName); - action.setLastText(action.getNewText()); + resultDto.setLastText(dto.getNewText()); try { String originalText = FileUtils.readFileToString(localFile); - action.setOrigText(originalText); + resultDto.setOrigText(originalText); } catch (FileNotFoundException e) { log.error("Can not find the local file", e); - return action.ERROR; + resultDto.setError(CommitResultDto.ERROR); + return resultDto; } catch (IOException e) { log.error("Can not open the local file", e); - return action.ERROR; + resultDto.setError(CommitResultDto.ERROR); + return resultDto; } // authentication - if (action.getUsername() == null || action.getPw() == null) { - return action.LOGIN; + if (dto.getUsername() == null || dto.getPassword() == null) { + resultDto.setError(CommitResultDto.AUTH_ERROR); + return resultDto; } - CredentialsProvider credentials = new UsernamePasswordCredentialsProvider(action.getUsername(), action.getPw()); + CredentialsProvider credentials = new UsernamePasswordCredentialsProvider(dto.getUsername(), dto.getPassword()); // applying the changes on the local file try { - FileUtils.writeStringToFile(localFile, action.getNewText()); + FileUtils.writeStringToFile(localFile, dto.getNewText()); } catch (IOException e) { if (log.isErrorEnabled()) { log.error("Can not modify the local file", e); } - return action.ERROR; + resultDto.setError(CommitResultDto.ERROR); + return resultDto; } try { @@ -381,8 +393,8 @@ public class GitConnection implements ScmConnection { CommitCommand commit = git.commit(); commit.setAll(true); - commit.setAuthor(action.getUsername(), "unknown"); - commit.setMessage(action.getCommitMessage()); + commit.setAuthor(dto.getUsername(), "unknown"); + commit.setMessage("From scmwebeditor -- " + dto.getCommitMessage()); try { commit.call(); @@ -391,7 +403,8 @@ public class GitConnection implements ScmConnection { log.error("Can not commit", e); } - return action.ERROR; + resultDto.setError(CommitResultDto.ERROR); + return resultDto; } if (log.isDebugEnabled()) { @@ -410,47 +423,54 @@ public class GitConnection implements ScmConnection { log.error("Can not push : the Git repository has no HEAD reference", e); } - return action.ERROR; + resultDto.setError(CommitResultDto.ERROR); + return resultDto; } catch (UnmergedPathsException e) { if (log.isErrorEnabled()) { log.error("Can not push : conflicts found (unmerged paths)", e); } - return action.ERROR; + resultDto.setError(CommitResultDto.ERROR); + return resultDto; } catch (ConcurrentRefUpdateException e) { if (log.isErrorEnabled()) { log.error("Can not push : someone else is updating the HEAD or the branch", e); } - return action.ERROR; + resultDto.setError(CommitResultDto.ERROR); + return resultDto; } catch (WrongRepositoryStateException e) { if (log.isErrorEnabled()) { log.error("Can not push : the repository is not in the right state", e); } - return action.ERROR; + resultDto.setError(CommitResultDto.ERROR); + return resultDto; } catch (RejectCommitException e) { if (log.isErrorEnabled()) { log.error("Can not push : commit rejected", e); } - return action.ERROR; + resultDto.setError(CommitResultDto.ERROR); + return resultDto; } catch (GitAPIException e) { if (log.isErrorEnabled()) { log.error("Can not push", e); } - return action.LOGIN; + resultDto.setError(CommitResultDto.AUTH_ERROR); + return resultDto; } } catch (IOException e) { if (log.isErrorEnabled()) { log.error("Can not open git local repository : " + localDirectory.getAbsolutePath(), e); - return action.ERROR; + resultDto.setError(CommitResultDto.ERROR); + return resultDto; } } - return action.SUCCESS; + return resultDto; } @Override diff --git a/src/main/java/org/nuiton/scmwebeditor/ScmConnection.java b/src/main/java/org/nuiton/scmwebeditor/ScmConnection.java index 1eb4a3b..505c817 100644 --- a/src/main/java/org/nuiton/scmwebeditor/ScmConnection.java +++ b/src/main/java/org/nuiton/scmwebeditor/ScmConnection.java @@ -5,6 +5,8 @@ import org.nuiton.scmwebeditor.actions.BrowseAction; import org.nuiton.scmwebeditor.actions.UploadAction; import org.nuiton.scmwebeditor.dto.BrowseDto; import org.nuiton.scmwebeditor.dto.BrowseResultDto; +import org.nuiton.scmwebeditor.dto.CommitDto; +import org.nuiton.scmwebeditor.dto.CommitResultDto; import javax.naming.AuthenticationException; @@ -23,16 +25,10 @@ public interface ScmConnection { /** * Makes a commit of the changed made to the edited file - * @param action the ScmWebEditorCommitAction which contains the parameters (repository's address, username, - * password, new file content...) and which will contain the result (new revision number, - * information message...) - * @return ScmWebEditorCommitAction.ERROR if an error occurred - * ScmWebEditorCommitAction.LOGIN if the authentication failed - * ScmWebEditorCommitAction.ERROR_PATH if it is impossible to reach the repository - * ScmWebEditorCommitAction.FILE_MODIFY if the local file has been modified by another program - * ScmWebEditorCommitAction.SUCCESS if the commit has been done without any problem + * @param dto the DTO which contains all the parameters + * @return a DTO which contains all the results */ - public String commit(ScmWebEditorCommitAction action); + public CommitResultDto commit(CommitDto dto); /** diff --git a/src/main/java/org/nuiton/scmwebeditor/SvnConnection.java b/src/main/java/org/nuiton/scmwebeditor/SvnConnection.java index 8156666..c9fd7b3 100644 --- a/src/main/java/org/nuiton/scmwebeditor/SvnConnection.java +++ b/src/main/java/org/nuiton/scmwebeditor/SvnConnection.java @@ -30,6 +30,8 @@ import org.nuiton.scmwebeditor.actions.ScmWebEditorCommitAction; import org.nuiton.scmwebeditor.actions.UploadAction; import org.nuiton.scmwebeditor.dto.BrowseDto; import org.nuiton.scmwebeditor.dto.BrowseResultDto; +import org.nuiton.scmwebeditor.dto.CommitDto; +import org.nuiton.scmwebeditor.dto.CommitResultDto; import org.nuiton.util.FileUtil; import org.tmatesoft.svn.core.*; import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager; @@ -239,9 +241,11 @@ public class SvnConnection implements ScmConnection { @Override - public String commit(ScmWebEditorCommitAction action) { + public CommitResultDto commit(CommitDto dto) { - updateAuthentication(action.getUsername(), action.getPw()); + CommitResultDto resultDto = new CommitResultDto(); + + updateAuthentication(dto.getUsername(), dto.getPassword()); if (log.isDebugEnabled()) { log.debug("Entering SVN commit"); @@ -253,7 +257,9 @@ public class SvnConnection implements ScmConnection { if (log.isErrorEnabled()) { log.error("Can't create checkoutDir", e1); } - return action.ERROR; + + resultDto.setError(CommitResultDto.ERROR); + return resultDto; } @@ -261,7 +267,6 @@ public class SvnConnection implements ScmConnection { try { checkout(checkoutdir); } catch (SVNAuthenticationException authexep) { - action.getRequest().setAttribute(action.getParameterAddress(), action.getAddress()); // if svn authentication failed user is redirected on login page if (log.isDebugEnabled()) { @@ -270,11 +275,8 @@ public class SvnConnection implements ScmConnection { // we delete the temporary directory delTempDirectory(checkoutdir); - action.setUsername(null); - action.setPw(null); - action.getScmSession().delScmUser(getRepositoryId()); - - return action.LOGIN; + resultDto.setError(CommitResultDto.AUTH_ERROR); + return resultDto; } catch (SVNException e) { if (log.isErrorEnabled()) { @@ -282,19 +284,21 @@ public class SvnConnection implements ScmConnection { } // deleting the temporary directory delTempDirectory(checkoutdir); - return action.ERROR_PATH; + + resultDto.setError(CommitResultDto.ERROR_PATH); + return resultDto; } File checkOutFile = new File(checkoutdir, fileName); - action.setLastText(action.getNewText()); + resultDto.setLastText(dto.getNewText()); try { String originalText = FileUtils.readFileToString(checkOutFile); - action.setOrigText(originalText); + resultDto.setOrigText(originalText); } catch (FileNotFoundException ee) { @@ -303,7 +307,8 @@ public class SvnConnection implements ScmConnection { */ delTempDirectory(checkoutdir); - return action.ERROR; + resultDto.setError(CommitResultDto.ERROR); + return resultDto; } catch (IOException e) { log.error("Can't find the checkout file", e); } @@ -312,25 +317,27 @@ public class SvnConnection implements ScmConnection { /* * Diff */ - if (!action.getForce()) { + if (!dto.isForce()) { try { - if (isDifferent(action.getOrigText())) { - ByteArrayOutputStream differents = getDiff(action.getNewText()); + if (isDifferent(resultDto.getOrigText())) { + ByteArrayOutputStream differents = getDiff(dto.getNewText()); if (differents.size() > 0) { - action.setDiff(differents.toString()); + resultDto.setDiff(differents.toString()); - String diff = action.getDiff(); + String diff = resultDto.getDiff(); - action.setDiff(diff.substring(diff.indexOf("@@"))); + resultDto.setDiff(diff.substring(diff.indexOf("@@"))); delTempDirectory(checkoutdir); try { - action.setHeadCommiter(getHeadcommiter(action.getAddress(), action.getUsername(), action.getPw())); + resultDto.setHeadCommiter(getHeadcommiter(dto.getAddress(), dto.getUsername(), dto.getPassword())); } catch (SVNException e) { log.error("Can't get head commiter", e); } - return action.FILE_MODIFY; + + resultDto.setError(CommitResultDto.FILE_MODIFY); + return resultDto; } } @@ -352,10 +359,12 @@ public class SvnConnection implements ScmConnection { try { - FileUtil.writeString(pathToFile, action.getNewText(), "UTF-8"); + FileUtil.writeString(pathToFile, dto.getNewText(), "UTF-8"); } catch (IOException e1) { delTempDirectory(checkoutdir); - return action.ERROR; + + resultDto.setError(CommitResultDto.ERROR); + return resultDto; } @@ -367,7 +376,7 @@ public class SvnConnection implements ScmConnection { if (log.isDebugEnabled()) { log.debug("Try to commit"); } - commitClient.doCommit(tabFile, false, "From scmwebeditor -- " + action.getCommitMessage(), null, null, false, false, SVNDepth.FILES); + commitClient.doCommit(tabFile, false, "From scmwebeditor -- " + dto.getCommitMessage(), null, null, false, false, SVNDepth.FILES); } catch (SVNAuthenticationException authexep) { if (log.isErrorEnabled()) { log.error("AUTH FAIL", authexep); @@ -375,23 +384,24 @@ public class SvnConnection implements ScmConnection { // if authentication failed edition page is reloaded form user's relogin - action.setOrigText(action.getNewText()); - action.setBadLogin(true); + resultDto.setOrigText(dto.getNewText()); // deleting temporary directory delTempDirectory(checkoutdir); - action.setUsername(null); - action.setPw(null); - // deleting the session value - action.getScmSession().delScmUser(getRepositoryId()); - return action.LOGIN; + dto.setUsername(null); + dto.setPassword(null); + + resultDto.setError(CommitResultDto.AUTH_ERROR); + return resultDto; } catch (SVNException e) { if (log.isErrorEnabled()) { log.error("SVN FAIL", e); } // deleting temporary directory delTempDirectory(checkoutdir); - return action.ERROR; + + resultDto.setError(CommitResultDto.ERROR); + return resultDto; } @@ -407,19 +417,19 @@ public class SvnConnection implements ScmConnection { delTempDirectory(checkoutdir); if (log.isInfoEnabled()) { - log.info(action.getUsername() + " with IP " + action.getRequest().getRemoteAddr() + " commit the file " - + action.getAddress() + " with message : " + action.getCommitMessage()); + log.info(dto.getUsername() + " commit the file " + dto.getAddress() + " with message : " + + dto.getCommitMessage()); } try { - action.setNumRevision(getHeadRevisionNumber(action.getAddress(), action.getUsername(), action.getPw())); + resultDto.setNumRevision(getHeadRevisionNumber(dto.getAddress(), dto.getUsername(), dto.getPassword())); } catch (AuthenticationException e) { if (log.isErrorEnabled()) { log.error("Auth fail", e); } } - return action.SUCCESS; + return resultDto; } diff --git a/src/main/java/org/nuiton/scmwebeditor/actions/ScmWebEditorCommitAction.java b/src/main/java/org/nuiton/scmwebeditor/actions/ScmWebEditorCommitAction.java index 37409e1..795bd0b 100644 --- a/src/main/java/org/nuiton/scmwebeditor/actions/ScmWebEditorCommitAction.java +++ b/src/main/java/org/nuiton/scmwebeditor/actions/ScmWebEditorCommitAction.java @@ -31,6 +31,8 @@ import org.dom4j.Document; import org.nuiton.jrst.JRST; import org.nuiton.jrst.legacy.JRSTReader; import org.nuiton.scmwebeditor.*; +import org.nuiton.scmwebeditor.dto.CommitDto; +import org.nuiton.scmwebeditor.dto.CommitResultDto; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; @@ -340,9 +342,54 @@ public class ScmWebEditorCommitAction extends AbstractScmWebEditorAction impleme String returnCode; - returnCode = scmConn.commit(this); + CommitDto dto = new CommitDto(); + dto.setUsername(username); + dto.setPassword(pw); + dto.setNewText(newText); + dto.setCommitMessage(commitMessage); + dto.setForce(force); + dto.setAddress(address); - return returnCode; + CommitResultDto resultDto = scmConn.commit(dto); + + if (resultDto.getLastText() != null) { + lastText = resultDto.getLastText(); + } + if (resultDto.getOrigText() != null) { + origText = resultDto.getOrigText(); + } + if (resultDto.getDiff() != null) { + diff = resultDto.getDiff(); + } + if (resultDto.getDiff() != null) { + headCommiter = resultDto.getHeadCommiter(); + } + if (resultDto.getNumRevision() != null) { + numRevision = resultDto.getNumRevision(); + } + + + if (resultDto.getError() != null) { + + String error = resultDto.getError(); + + if (error.equals(CommitResultDto.ERROR_PATH)) { + return ERROR_PATH; + } else if (error.equals(CommitResultDto.AUTH_ERROR)) { + request.setAttribute(getParameterAddress(), address); + getScmSession().delScmUser(scmConn.getRepositoryId()); + username = null; + pw = null; + + return LOGIN; + } else if (error.equals(CommitResultDto.FILE_MODIFY)) { + return FILE_MODIFY; + } else { + return ERROR; + } + } + + return SUCCESS; } diff --git a/src/main/java/org/nuiton/scmwebeditor/dto/CommitDto.java b/src/main/java/org/nuiton/scmwebeditor/dto/CommitDto.java new file mode 100644 index 0000000..2044d99 --- /dev/null +++ b/src/main/java/org/nuiton/scmwebeditor/dto/CommitDto.java @@ -0,0 +1,71 @@ +package org.nuiton.scmwebeditor.dto; + +public class CommitDto { + + /** the username used to connect to the SCM */ + protected String username; + + /** the password used to connect to the SCM */ + protected String password; + + /** the new text to insert */ + protected String newText; + + /** the message to describe the commit */ + protected String commitMessage; + + /** set to true for a commit even if there is no difference with the previous version */ + protected boolean force; + + /** the file to commit's address */ + protected String address; + + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public String getNewText() { + return newText; + } + + public void setNewText(String newText) { + this.newText = newText; + } + + public String getCommitMessage() { + return commitMessage; + } + + public void setCommitMessage(String commitMessage) { + this.commitMessage = commitMessage; + } + + public boolean isForce() { + return force; + } + + public void setForce(boolean force) { + this.force = force; + } + + public String getAddress() { + return address; + } + + public void setAddress(String address) { + this.address = address; + } +} diff --git a/src/main/java/org/nuiton/scmwebeditor/dto/CommitResultDto.java b/src/main/java/org/nuiton/scmwebeditor/dto/CommitResultDto.java new file mode 100644 index 0000000..5132bc0 --- /dev/null +++ b/src/main/java/org/nuiton/scmwebeditor/dto/CommitResultDto.java @@ -0,0 +1,79 @@ +package org.nuiton.scmwebeditor.dto; + +public class CommitResultDto { + + public static String ERROR = "error"; + + public static String ERROR_PATH = "error path"; + + public static String AUTH_ERROR = "auth error"; + + public static String FILE_MODIFY = "file modify"; + + /** gives a message about the error if one occured */ + protected String error; + + /** the last read text */ + protected String lastText; + + /** the original text */ + protected String origText; + + /** the differences between the current version and the last one */ + protected String diff; + + /** the commiter of the head version */ + protected String headCommiter; + + /** the number of the revision */ + protected String numRevision; + + + public String getError() { + return error; + } + + public void setError(String error) { + this.error = error; + } + + public String getLastText() { + return lastText; + } + + public void setLastText(String lastText) { + this.lastText = lastText; + } + + public String getOrigText() { + return origText; + } + + public void setOrigText(String origText) { + this.origText = origText; + } + + public String getDiff() { + return diff; + } + + public void setDiff(String diff) { + this.diff = diff; + } + + public String getHeadCommiter() { + return headCommiter; + } + + public void setHeadCommiter(String headCommiter) { + this.headCommiter = headCommiter; + } + + public String getNumRevision() { + return numRevision; + } + + public void setNumRevision(String numRevision) { + this.numRevision = numRevision; + } +} -- To stop receiving notification emails like this one, please contact nuiton.org SCM administrator <admin+scm@nuiton.org>.