branch feature/3879 created (now 412769c)
This is an automated email from the git hooks/post-receive script. New change to branch feature/3879 in repository scmwebeditor. See http://git.nuiton.org/scmwebeditor.git at 412769c Application dans le module web + nettoyage de code This branch includes the following new commits: new 20f0add PRemière passe pour un peu factoriser le code (mais il reste encore pas mal à faire : utiliser un objet pour représenter un authentification plutôt que d'utiliser des paramètres login-password) new 3866ee7 Introduction de ScmAuthentication et utilisation de le module API new 33603cb Application dans le module svn new db92125 Application dans le module git new 412769c Application dans le module web + nettoyage de code The 5 revisions listed above as "new" are entirely new to this repository and will be described in separate emails. The revisions listed as "adds" were already present in the repository and have only been added to this reference. Detailed log of new commits: commit 412769c139ed3ba5574ff6286f5284bcdb358aa2 Author: Tony CHEMIT <chemit@codelutin.com> Date: Sun Feb 14 19:29:11 2016 +0100 Application dans le module web + nettoyage de code commit db92125b07c8459179e67db655b040f051ba5bdc Author: Tony CHEMIT <chemit@codelutin.com> Date: Sun Feb 14 19:28:56 2016 +0100 Application dans le module git commit 33603cba14c5a237d8c5aca379b2770c76669b0e Author: Tony CHEMIT <chemit@codelutin.com> Date: Sun Feb 14 19:28:49 2016 +0100 Application dans le module svn commit 3866ee7bd7f9c5319f885a6db451acdad2d4e2e5 Author: Tony CHEMIT <chemit@codelutin.com> Date: Sun Feb 14 19:28:32 2016 +0100 Introduction de ScmAuthentication et utilisation de le module API commit 20f0add006bb17e4a323dc5a3c5989f64f46a763 Author: Tony CHEMIT <chemit@codelutin.com> Date: Sun Feb 14 17:16:34 2016 +0100 PRemière passe pour un peu factoriser le code (mais il reste encore pas mal à faire : utiliser un objet pour représenter un authentification plutôt que d'utiliser des paramètres login-password) -- To stop receiving notification emails like this one, please contact nuiton.org SCM administrator <admin+scm@nuiton.org>.
This is an automated email from the git hooks/post-receive script. New commit to branch feature/3879 in repository scmwebeditor. See http://git.nuiton.org/scmwebeditor.git commit 20f0add006bb17e4a323dc5a3c5989f64f46a763 Author: Tony CHEMIT <chemit@codelutin.com> Date: Sun Feb 14 17:16:34 2016 +0100 PRemière passe pour un peu factoriser le code (mais il reste encore pas mal à faire : utiliser un objet pour représenter un authentification plutôt que d'utiliser des paramètres login-password) --- .../org/nuiton/scmwebeditor/uiweb/ScmSession.java | 8 +- .../uiweb/actions/AbstractScmWebEditorAction.java | 69 ++++- .../AbstractScmWebEditorWithAddressAction.java | 331 +++++++++++++++++++++ .../scmwebeditor/uiweb/actions/BrowseAction.java | 124 ++------ .../uiweb/actions/CreateBranchAction.java | 83 ++---- .../uiweb/actions/CreateDirectoryAction.java | 94 ++---- .../uiweb/actions/DownloadFileAction.java | 47 ++- .../scmwebeditor/uiweb/actions/EditAction.java | 117 +------- .../scmwebeditor/uiweb/actions/GetImageAction.java | 19 +- .../uiweb/actions/ListBranchesAction.java | 51 +--- .../scmwebeditor/uiweb/actions/LogoutAction.java | 50 +--- .../scmwebeditor/uiweb/actions/MoveFileAction.java | 91 +----- .../scmwebeditor/uiweb/actions/PreviewAction.java | 13 +- .../uiweb/actions/RemoveDirectoryAction.java | 91 +----- .../uiweb/actions/RemoveFileAction.java | 92 +----- .../scmwebeditor/uiweb/actions/ResetAction.java | 51 +--- .../scmwebeditor/uiweb/actions/SaveAction.java | 6 +- .../uiweb/actions/ScmWebEditorCommitAction.java | 144 +-------- .../uiweb/actions/ScmWebEditorMainAction.java | 54 +--- .../uiweb/actions/UploadFileAction.java | 84 +----- .../uiweb/actions/ViewDiffsAction.java | 106 +------ .../uiweb/actions/ViewImageAction.java | 123 ++------ .../uiweb/actions/ViewRevisionAction.java | 107 +------ 23 files changed, 659 insertions(+), 1296 deletions(-) diff --git a/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/ScmSession.java b/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/ScmSession.java index ce2ccda..e92d119 100644 --- a/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/ScmSession.java +++ b/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/ScmSession.java @@ -21,6 +21,8 @@ */ package org.nuiton.scmwebeditor.uiweb; +import org.apache.shiro.authc.UsernamePasswordToken; + import java.util.HashMap; import java.util.Map; @@ -53,6 +55,10 @@ public class ScmSession { scmUsers.put(address, new ScmUser(login, password)); } + public void addScmUser(String repositoryUUID, UsernamePasswordToken usernamePasswordToken) { + scmUsers.put(repositoryUUID, new ScmUser(usernamePasswordToken.getUsername(), new String(usernamePasswordToken.getPassword()))); + } + /** * Removes a user from the list of users * @param address the repository's address @@ -75,6 +81,7 @@ public class ScmSession { } + /** * Gives the password related to the given repository * @param url the repository's address @@ -88,5 +95,4 @@ public class ScmSession { } } - } diff --git a/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/AbstractScmWebEditorAction.java b/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/AbstractScmWebEditorAction.java index 8e2d845..09fd105 100644 --- a/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/AbstractScmWebEditorAction.java +++ b/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/AbstractScmWebEditorAction.java @@ -22,24 +22,43 @@ package org.nuiton.scmwebeditor.uiweb.actions; import com.opensymphony.xwork2.ActionContext; -import info.monitorenter.cpdetector.io.*; +import info.monitorenter.cpdetector.io.ASCIIDetector; +import info.monitorenter.cpdetector.io.ByteOrderMarkDetector; +import info.monitorenter.cpdetector.io.CodepageDetectorProxy; +import info.monitorenter.cpdetector.io.JChardetFacade; +import info.monitorenter.cpdetector.io.ParsingDetector; import org.apache.commons.io.IOUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.struts2.interceptor.ServletRequestAware; +import org.apache.struts2.interceptor.ServletResponseAware; import org.apache.tika.exception.TikaException; import org.apache.tika.metadata.Metadata; import org.apache.tika.parser.AutoDetectParser; import org.apache.tika.sax.BodyContentHandler; +import org.nuiton.scmwebeditor.api.ScmConnection; import org.nuiton.scmwebeditor.uiweb.ScmSession; import org.nuiton.scmwebeditor.uiweb.ScmWebEditorConfig; import org.nuiton.web.struts2.BaseAction; import org.xml.sax.SAXException; +import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; -import java.io.*; +import javax.servlet.http.HttpServletResponse; +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.io.Reader; +import java.io.Writer; import java.net.MalformedURLException; import java.nio.charset.Charset; +import java.text.Normalizer; import java.util.Arrays; import java.util.Map; import java.util.Properties; @@ -47,7 +66,7 @@ import java.util.Properties; /** * Base for all the other actions */ -public abstract class AbstractScmWebEditorAction extends BaseAction implements ServletRequestAware { +public abstract class AbstractScmWebEditorAction extends BaseAction implements ServletRequestAware, ServletResponseAware { private static final Log log = LogFactory.getLog(AbstractScmWebEditorAction.class); @@ -64,6 +83,9 @@ public abstract class AbstractScmWebEditorAction extends BaseAction implements S /** the HTTP request received by the server */ protected transient HttpServletRequest request; + /** the HTTP response to send to the client */ + protected transient HttpServletResponse response; + /** the name of the SCM to use for the given repository */ protected String scmType; @@ -355,6 +377,30 @@ public abstract class AbstractScmWebEditorAction extends BaseAction implements S return scmSession; } + protected String getRepositoryUUID(ScmConnection scmConnection, String address) { + + String repositoryUUID = scmConnection.getRepositoryId(); + if (repositoryUUID == null) { + repositoryUUID = address.replace(' ', '_'); + repositoryUUID = Normalizer.normalize(repositoryUUID, Normalizer.Form.NFD).replaceAll("[\u0300-\u036F]", ""); + } + + return repositoryUUID; + + } + + protected void removeRepositoryCookie(String repositoryUUID) { + for (Cookie c : request.getCookies()) { + if (c.getName().equals(repositoryUUID)) { + c.setMaxAge(0);//On supprime le cookie + response.addCookie(c); + if (log.isDebugEnabled()) { + log.debug("Cookie supprimé"); + } + } + } + } + /** * Reads the information in the session to give the username and the password for a given repository if necessary * @param repositoryUUID the ID of the repository to connect to @@ -399,6 +445,16 @@ public abstract class AbstractScmWebEditorAction extends BaseAction implements S return usernamePw; } + protected void removeRepositoryFromCache(String repositoryUUID) { + + // deleting the cookies for this repository + removeRepositoryCookie(repositoryUUID); + + // remove it from user session + getScmSession().delScmUser(repositoryUUID); + + } + public String getUsername(String url) { return getScmSession().getUsername(url); } @@ -410,9 +466,12 @@ public abstract class AbstractScmWebEditorAction extends BaseAction implements S public String getSweVersion() { return ScmWebEditorConfig.getVersion(); } @Override - public void setServletRequest(HttpServletRequest request) { + public final void setServletRequest(HttpServletRequest request) { this.request = request; } - + @Override + public final void setServletResponse(HttpServletResponse response) { + this.response = response; + } } diff --git a/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/AbstractScmWebEditorWithAddressAction.java b/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/AbstractScmWebEditorWithAddressAction.java new file mode 100644 index 0000000..0cb136c --- /dev/null +++ b/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/AbstractScmWebEditorWithAddressAction.java @@ -0,0 +1,331 @@ +package org.nuiton.scmwebeditor.uiweb.actions; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.shiro.authc.UsernamePasswordToken; +import org.apache.shiro.codec.Base64; +import org.apache.shiro.crypto.BlowfishCipherService; +import org.nuiton.scmwebeditor.api.ScmConnection; +import org.nuiton.scmwebeditor.api.ScmFileManager; +import org.nuiton.scmwebeditor.api.ScmProvider; +import org.nuiton.scmwebeditor.uiweb.ScmWebEditorConfig; + +import javax.servlet.http.Cookie; +import javax.servlet.http.HttpSession; +import java.io.File; +import java.io.UnsupportedEncodingException; +import java.util.Optional; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * Created on 13/02/16. + * + * @author Tony Chemit - chemit@codelutin.com + */ +public class AbstractScmWebEditorWithAddressAction extends AbstractScmWebEditorAction { + + /** Logger. */ + private static final Log log = LogFactory.getLog(AbstractScmWebEditorWithAddressAction.class); + + /** the username to use to connect to the repository */ + private String username; + + /** the password to use to connect to the repository */ + private String pw; + + /** the repository's address */ + private String address; + + private ScmAuthenticatedUrl authenticatedUrl; + + private ScmProvider scmProvider; + + private String sessionId; + + private String pathToLocalRepos; + + private ScmConnection scmConnection; + + private ScmFileManager scmFileManager; + + protected ScmFileManager getScmFileManager() { + if (scmFileManager == null) { + scmFileManager = getScmProvider().getFileManager(getScmConnection()); + } + return scmFileManager; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getPw() { + return pw; + } + + public void setPw(String pw) { + this.pw = pw; + } + + public String getAddress() { + return address; + } + + public void setAddress(String address) { + this.address = address; + } + + protected void initAddress() { + + if (address.endsWith("/")) { + address = address.substring(0, address.lastIndexOf('/')); + } + + authenticatedUrl = getAuthenticatedUrl(address, username, pw); + + address = authenticatedUrl.getUrl(); + username = authenticatedUrl.getUsername(); + pw = authenticatedUrl.getPw(); + + } + + protected String getRepositoryUUID() { + return getRepositoryUUID(getScmConnection(), address); + } + + protected ScmProvider getScmProvider() { + if (scmProvider == null) { + scmProvider = ScmWebEditorConfig.getProvider(scmType); + } + return scmProvider; + } + + protected String getSessionId() { + if (sessionId == null) { + HttpSession session = request.getSession(); + sessionId = session.getId(); + } + return sessionId; + } + + protected String getPathToLocalRepos() { + if (pathToLocalRepos == null) { + String sessionId = getSessionId(); + pathToLocalRepos = ScmWebEditorConfig.getLocalRepositoriesPath() + File.separator + sessionId; + } + return pathToLocalRepos; + } + + protected ScmConnection getScmConnection() { + if (scmConnection == null) { + String pathToLocalRepos = getPathToLocalRepos(); + + scmConnection = getScmProvider().getConnection(address, pathToLocalRepos); + } + return scmConnection; + } + + protected Optional<UsernamePasswordToken> initAuthenticationInformations(String repositoryUUID, boolean readCookie, boolean saveCookie) { + + UsernamePasswordToken token = null; + + if (readCookie) { + + String usernamepwCookie = null; + + for (Cookie c : request.getCookies()) { + if (c.getName().equals(repositoryUUID)) + usernamepwCookie = c.getValue(); + } + + if (usernamepwCookie != null) { + + BlowfishCipherService bf = new BlowfishCipherService(); + + byte[] privateKey = Base64.decode(ScmWebEditorConfig.getKey()); + + String usernameDecode = null; + try { + usernameDecode = new String(bf.decrypt(Base64.decode(usernamepwCookie), privateKey).getBytes(), "UTF-8"); + } catch (UnsupportedEncodingException e) { + if (log.isErrorEnabled()) { + log.error("Can not create a String with UTF-8 encoding"); + } + } + + if (usernameDecode != null) { + String[] resCookie = usernameDecode.split(","); + if (resCookie.length == 2) { + token = new UsernamePasswordToken(resCookie[0], resCookie[1]); + + } + } + } + + + } + + if (saveCookie && token !=null) { + + BlowfishCipherService bf = new BlowfishCipherService(); + + byte[] privateKey = Base64.decode(ScmWebEditorConfig.getKey()); + + Cookie authCookie = null; + + try { + authCookie = new Cookie(repositoryUUID, bf.encrypt((token.getUsername() + "," + new String(token.getPassword())).getBytes("UTF-8"), privateKey).toBase64()); + } catch (UnsupportedEncodingException e) { + if (log.isErrorEnabled()) { + log.error("Can not get bytes from UTF-8 encoding"); + } + } + + if (authCookie != null) { + authCookie.setMaxAge(60 * 60 * 24 * 365); + response.addCookie(authCookie); + } + + } + + String[] usernamePw = getUsernamePwFromSession(repositoryUUID); + username = usernamePw[0]; + pw = usernamePw[1]; + + return Optional.ofNullable(token); + + } + + protected void storeUsernamePasswordToCookie(String repositoryUUID) { + + BlowfishCipherService bf = new BlowfishCipherService(); + + byte[] privateKey = Base64.decode(ScmWebEditorConfig.getKey()); + + if (username != null && pw != null) { + + Cookie authCookie = null; + + try { + authCookie = new Cookie(repositoryUUID, bf.encrypt((username + "," + pw).getBytes("UTF-8"), privateKey).toBase64()); + } catch (UnsupportedEncodingException e) { + if (log.isErrorEnabled()) { + log.error("Can not get bytes from UTF-8 encoding"); + } + } + + if (authCookie != null) { + authCookie.setMaxAge(60 * 60 * 24 * 365); + response.addCookie(authCookie); + } + } + } + + /** + * Reads the information in the session to give the username and the password for a given repository if necessary + * @param repositoryUUID the ID of the repository to connect to + * @return the username to use at position [0] ; the password to use at position [1] + */ + protected String[] getUsernamePwFromSession(String repositoryUUID) { + + String[] usernamePw = new String[2]; + usernamePw[0] = username; + usernamePw[1] = pw; + + if (username == null || pw == null) { + + String login = getScmSession().getUsername(repositoryUUID); + String password = getScmSession().getPassword(repositoryUUID); + + if (login != null && password != null) { + + // getting the authentication information in session + usernamePw[0] = login; + usernamePw[1] = password; + } + } else { + if (username.equals("") || pw.equals("")) { + + String login = getScmSession().getUsername(repositoryUUID); + String password = getScmSession().getPassword(repositoryUUID); + + if (login != null && password != null) { + + // getting the authentication information in session + usernamePw[0] = login; + usernamePw[1] = password; + } + } else { + getScmSession().addScmUser(repositoryUUID, username, pw); + } + } + + return usernamePw; + } + + protected void resetAuthentication() { + username = null; + pw= null; + } + + protected static final Pattern AUTHENTICATED_URL_PATTERN = Pattern.compile("(http[s]://)([^:]+):([^@]+)@(.+)"); + + public static ScmAuthenticatedUrl getAuthenticatedUrl(String url, String username, String pw) { + + Matcher matcher = AUTHENTICATED_URL_PATTERN.matcher(url); + if (matcher.matches()) { + + username = matcher.group(2); + pw = matcher.group(3); + url = matcher.group(1) + matcher.group(4); + } else { + + if (username == null) { + username = "anonymous"; + } + if (pw == null) { + pw = "anonymous"; + } + + } + + return new ScmAuthenticatedUrl(url, username, pw); + } + + public static class ScmAuthenticatedUrl { + + private final String url; + private final String username; + private final String pw; + + + public ScmAuthenticatedUrl(String url, String username, String pw) { + this.url = url; + this.username = username; + this.pw = pw; + } + + public String getUrl() { + return url; + } + + public String getUsername() { + return username; + } + + public String getPw() { + return pw; + } + + public boolean isAuthenticated() { + return !username.equals("anonymous") && !username.equals("") && !pw.equals("anonymous") && !pw.equals(""); + } + + } + +} diff --git a/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/BrowseAction.java b/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/BrowseAction.java index 4cbe3b9..df692f2 100644 --- a/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/BrowseAction.java +++ b/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/BrowseAction.java @@ -24,29 +24,20 @@ package org.nuiton.scmwebeditor.uiweb.actions; import com.jgeppert.struts2.jquery.tree.result.TreeNode; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.apache.shiro.codec.Base64; -import org.apache.shiro.crypto.BlowfishCipherService; -import org.apache.struts2.interceptor.ServletResponseAware; +import org.apache.shiro.authc.UsernamePasswordToken; import org.nuiton.scmwebeditor.api.ScmConnection; -import org.nuiton.scmwebeditor.api.ScmProvider; import org.nuiton.scmwebeditor.api.dto.BrowseDto; import org.nuiton.scmwebeditor.api.dto.result.BrowseResultDto; -import org.nuiton.scmwebeditor.uiweb.ScmWebEditorConfig; - -import javax.servlet.http.Cookie; -import javax.servlet.http.HttpServletResponse; -import javax.servlet.http.HttpSession; -import java.io.File; -import java.io.UnsupportedEncodingException; -import java.text.Normalizer; + import java.util.ArrayList; import java.util.List; import java.util.Map; +import java.util.Optional; /** * Allows to browse through a repository */ -public class BrowseAction extends AbstractScmWebEditorAction implements ServletResponseAware { +public class BrowseAction extends AbstractScmWebEditorWithAddressAction { private static final long serialVersionUID = 4432027215087932750L; @@ -54,15 +45,6 @@ public class BrowseAction extends AbstractScmWebEditorAction implements ServletR public static final String ROOT = "root"; - /** the repository's address */ - protected String address; - - /** the username to connect with */ - protected String username; - - /** the password to connect with */ - protected String pw; - /** equals true when an error occured */ protected boolean error; @@ -81,26 +63,10 @@ public class BrowseAction extends AbstractScmWebEditorAction implements ServletR /** equals true if the SCM is able to use branches */ protected boolean scmSupportsBranches; - /** the HTTP response to send to the client */ - protected transient HttpServletResponse response; - - public boolean getError() { return error; } public void setError(boolean error) { this.error = error; } - public String getAddress() { return address; } - - public void setAddress(String address) { this.address = address; } - - public String getUsername() { return username; } - - public void setUsername(String username) { this.username = username; } - - public String getPw() { return pw; } - - public void setPw(String pw) { this.pw = pw; } - public String getId() { return id; } public String getHeadBranchName() { return headBranchName; } @@ -119,38 +85,24 @@ public class BrowseAction extends AbstractScmWebEditorAction implements ServletR public void setId(String id) { this.id = id; } - - /** - * Execution of the browse action - * @return a code interpreted in the file struts.xml - */ + @Override public String execute() { + if (log.isDebugEnabled()) { log.debug("Enter in browse action"); } - if (address.endsWith("/")) { - address = address.substring(0, address.length() - 1); - } - - if (username == null) { - username = "anonymous"; - } - if (pw == null) { - pw = "anonymous"; - } + initAddress(); // connection to the repository - HttpSession session = request.getSession(); - String sessionId = session.getId(); + ScmConnection scmConn = getScmConnection(); - String pathToLocalRepos = ScmWebEditorConfig.getLocalRepositoriesPath() + File.separator + sessionId; + String repositoryUUID = getRepositoryUUID(); - ScmProvider provider = ScmWebEditorConfig.getProvider(scmType); - ScmConnection scmConn = provider.getConnection(address, pathToLocalRepos); + Optional<UsernamePasswordToken> optionalAuthentication = initAuthenticationInformations(repositoryUUID, false, true); - scmSupportsBranches = provider.supportsBranches(); + scmSupportsBranches = getScmProvider().supportsBranches(); if (scmConn == null) { error = true; @@ -159,10 +111,16 @@ public class BrowseAction extends AbstractScmWebEditorAction implements ServletR // putting all the parameters into a DTO BrowseDto dto = new BrowseDto(); - dto.setUsername(username); - dto.setPassword(pw); + dto.setAddress(getAddress()); + + boolean withAuthentication = optionalAuthentication.isPresent(); + if (withAuthentication) { + + dto.setUsername(optionalAuthentication.get().getUsername()); + dto.setPassword(new String(optionalAuthentication.get().getPassword())); + } + dto.setId(id); - dto.setAddress(address); dto.setSelectedBranch(selectedBranch); BrowseResultDto resultDto = scmConn.browse(dto); @@ -240,50 +198,20 @@ public class BrowseAction extends AbstractScmWebEditorAction implements ServletR } } - // if the repository is not protected for writing, we get its UUID - String repositoryUUID = scmConn.getRepositoryId(); - if (repositoryUUID == null) { - repositoryUUID = address.replace(' ', '_'); - repositoryUUID = Normalizer.normalize(repositoryUUID, Normalizer.Form.NFD).replaceAll("[\u0300-\u036F]", ""); - } - - if (username != null && pw != null) { - if (!username.equals("anonymous") && !username.equals("") && !pw.equals("anonymous") && !pw.equals("")) { - - BlowfishCipherService bf = new BlowfishCipherService(); - byte[] privateKey = Base64.decode(ScmWebEditorConfig.getKey()); - - Cookie authCookie = null; - try { - authCookie = new Cookie(repositoryUUID, bf.encrypt((username + "," + pw).getBytes("UTF-8"), privateKey).toBase64()); - } catch (UnsupportedEncodingException e) { - if (log.isErrorEnabled()) { - log.error("Can not get bytes from UTF-8 encoding"); - } - } - if (authCookie != null) { - authCookie.setMaxAge(60 * 60 * 24 * 365); - response.addCookie(authCookie); - } + if (withAuthentication) { - if (log.isDebugEnabled()) { - log.debug("addscmuser uuid == " + repositoryUUID); - } + storeUsernamePasswordToCookie(repositoryUUID); - getScmSession().addScmUser(repositoryUUID, username, pw); + if (log.isDebugEnabled()) { + log.debug("addscmuser uuid == " + repositoryUUID); } + + getScmSession().addScmUser(repositoryUUID, optionalAuthentication.get()); } return SUCCESS; } - - @Override - public void setServletResponse(HttpServletResponse response) { - this.response = response; - } - - } diff --git a/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/CreateBranchAction.java b/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/CreateBranchAction.java index 29e86d7..75ccc51 100644 --- a/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/CreateBranchAction.java +++ b/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/CreateBranchAction.java @@ -23,40 +23,26 @@ package org.nuiton.scmwebeditor.uiweb.actions; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.apache.struts2.interceptor.ServletRequestAware; +import org.apache.shiro.authc.UsernamePasswordToken; 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.dto.CreateBranchDto; import org.nuiton.scmwebeditor.api.dto.result.AbstractResultDto; -import org.nuiton.scmwebeditor.uiweb.ScmWebEditorConfig; import javax.naming.AuthenticationException; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpSession; -import java.io.File; -import java.text.Normalizer; import java.util.List; +import java.util.Optional; /** * Creates a new branch on the repository */ -public class CreateBranchAction extends AbstractScmWebEditorAction implements ServletRequestAware { +public class CreateBranchAction extends AbstractScmWebEditorWithAddressAction { private static final Log log = LogFactory.getLog(CreateBranchAction.class); public static final String REDIRECT = "redirect"; - /** the username to use to connect to the repository */ - protected String username; - - /** the password to use to connect to the repository */ - protected String pw; - - /** the repository's address */ - protected String address; - /** equals true if there is a problem during the authentication process */ protected boolean badLogin; @@ -72,19 +58,6 @@ public class CreateBranchAction extends AbstractScmWebEditorAction implements Se /** the name of the new branch */ protected String newBranchName; - - public String getUsername() { return username; } - - public void setUsername(String username) { this.username = username; } - - public String getPw() { return pw; } - - public void setPw(String pw) { this.pw = pw; } - - public String getAddress() { return address; } - - public void setAddress(String address) { this.address = address; } - public boolean isBadLogin() { return badLogin; } public void setBadLogin(boolean badLogin) { this.badLogin = badLogin; } @@ -105,34 +78,23 @@ public class CreateBranchAction extends AbstractScmWebEditorAction implements Se public void setNewBranchName(String newBranchName) { this.newBranchName = newBranchName; } - /** - * Execution of the create branch action - * @return a code interpreted in the file struts.xml - */ + @Override public String execute() { - HttpSession session = request.getSession(); - String sessionId = session.getId(); - String pathToLocalRepos = ScmWebEditorConfig.getLocalRepositoriesPath() + File.separator + sessionId; + initAddress(); - ScmProvider provider = ScmWebEditorConfig.getProvider(scmType); + String repositoryUUID = getRepositoryUUID(); - ScmConnection scmConn = provider.getConnection(address, pathToLocalRepos); + Optional<UsernamePasswordToken> optionalAuthentication = initAuthenticationInformations(repositoryUUID, false, false); - // if the repository is not protected for writing, we get its UUID - if (address.endsWith("/")) { - address = address.substring(0, address.lastIndexOf('/')); - } + boolean withAuthentication = optionalAuthentication.isPresent(); - String repositoryUUID = scmConn.getRepositoryId(); - if (repositoryUUID == null) { - repositoryUUID = address.replace(' ', '_'); - repositoryUUID = Normalizer.normalize(repositoryUUID, Normalizer.Form.NFD).replaceAll("[\u0300-\u036F]", ""); - } + String username = withAuthentication ? optionalAuthentication.get().getUsername() : null; + String pw = withAuthentication ? new String(optionalAuthentication.get().getPassword()) : null; + + ScmProvider provider = getScmProvider(); - String[] usernamePw = getUsernamePwFromSession(repositoryUUID, username, pw); - username = usernamePw[0]; - pw = usernamePw[1]; + String address = getAddress(); try { branches = provider.listBranches(address, username, pw); @@ -153,9 +115,11 @@ public class CreateBranchAction extends AbstractScmWebEditorAction implements Se CreateBranchDto dto = new CreateBranchDto(); dto.setAddress(address); dto.setNewBranchName(newBranchName); - dto.setUsername(username); - dto.setPassword(pw); - dto.setPathToLocalRepos(pathToLocalRepos); + if (withAuthentication) { + dto.setUsername(username); + dto.setPassword(pw); + } + dto.setPathToLocalRepos(getPathToLocalRepos()); dto.setSelectedBranch(selectedBranch); String createBranchError; @@ -173,8 +137,8 @@ public class CreateBranchAction extends AbstractScmWebEditorAction implements Se log.error("Authentication problem", e); } badLogin = true; - username = null; - pw = null; + resetAuthentication(); + return LOGIN; } catch (RepositoryNotFoundException e) { if (log.isErrorEnabled()) { @@ -187,8 +151,7 @@ public class CreateBranchAction extends AbstractScmWebEditorAction implements Se if (createBranchError != null) { if (createBranchError.equals(AbstractResultDto.AUTH_ERROR)) { badLogin = true; - username = null; - pw = null; + resetAuthentication(); return LOGIN; } else { error = true; @@ -200,8 +163,4 @@ public class CreateBranchAction extends AbstractScmWebEditorAction implements Se return SUCCESS; } - @Override - public void setServletRequest(HttpServletRequest request) { - this.request = request; - } } 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 ee303c6..1ce08ed 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 @@ -21,36 +21,24 @@ */ package org.nuiton.scmwebeditor.uiweb.actions; +import org.apache.shiro.authc.UsernamePasswordToken; 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; -import org.nuiton.scmwebeditor.uiweb.ScmWebEditorConfig; -import javax.servlet.http.HttpSession; -import java.io.File; -import java.text.Normalizer; +import java.util.Optional; /** * Creates a new directory in the repository */ -public class CreateDirectoryAction extends AbstractScmWebEditorAction implements ServletRequestAware { +public class CreateDirectoryAction extends AbstractScmWebEditorWithAddressAction implements ServletRequestAware { private static final long serialVersionUID = 4244339447567114412L; public static final String REDIRECT = "redirect"; - /** the username to use to connect to the repository */ - protected String username; - - /** the password to use to connect to the repository */ - protected String pw; - - /** the repository's address */ - protected String address; - /** the URL the root of the repository */ protected String scmRoot; @@ -69,31 +57,6 @@ public class CreateDirectoryAction extends AbstractScmWebEditorAction implements /** the full path of the root */ protected String fileRoot; - - public String getUsername() { - return username; - } - - public void setUsername(String username) { - this.username = username; - } - - public String getPw() { - return pw; - } - - public void setPw(String pw) { - this.pw = pw; - } - - public void setAddress(String address) { - this.address = address; - } - - public String getAddress() { - return address; - } - public boolean isBadLogin() { return badLogin; } @@ -124,42 +87,26 @@ public class CreateDirectoryAction extends AbstractScmWebEditorAction implements public void setParentDirectory(String parentDirectory) { this.parentDirectory = parentDirectory; } - /** - * Execution of the create directory action - * @return a code interpreted in the file struts.xml - */ + @Override public String execute() { - HttpSession session = request.getSession(); - String sessionId = session.getId(); - String pathToLocalRepos = ScmWebEditorConfig.getLocalRepositoriesPath() + File.separator + sessionId; + initAddress(); - ScmProvider provider = ScmWebEditorConfig.getProvider(scmType); + String repositoryUUID = getRepositoryUUID(); - ScmConnection scmConn = provider.getConnection(address, pathToLocalRepos); - ScmFileManager scmFileManager = provider.getFileManager(scmConn); + Optional<UsernamePasswordToken> optionalAuthentication = initAuthenticationInformations(repositoryUUID, false, false); - // if the repository is not protected for writing, we get its UUID - if (address.endsWith("/")) { - address = address.substring(0, address.lastIndexOf('/')); - } - - String repositoryUUID = scmConn.getRepositoryId(); - if (repositoryUUID == null) { - repositoryUUID = address.replace(' ', '_'); - repositoryUUID = Normalizer.normalize(repositoryUUID, Normalizer.Form.NFD).replaceAll("[\u0300-\u036F]", ""); - } - - String[] usernamePw = getUsernamePwFromSession(repositoryUUID, username, pw); - username = usernamePw[0]; - pw = usernamePw[1]; + boolean withAuthentication = optionalAuthentication.isPresent(); CreateDirectoryDto dto = new CreateDirectoryDto(); - dto.setUsername(username); - dto.setPassword(pw); + if (withAuthentication) { + dto.setUsername(optionalAuthentication.get().getUsername()); + dto.setPassword(new String(optionalAuthentication.get().getPassword())); + } dto.setDirectoryName(directoryName); dto.setParentDirectory(parentDirectory); + ScmFileManager scmFileManager = getScmFileManager(); CreateDirectoryResultDto resultDto = scmFileManager.createDirectory(dto); if (resultDto.getScmRoot() != null) { @@ -169,11 +116,8 @@ public class CreateDirectoryAction extends AbstractScmWebEditorAction implements fileRoot = resultDto.getFileRoot(); } - if (username != null && pw != null) { - if (username.equals("") && pw.equals("")) { - username = null; - pw = null; - } + if (withAuthentication) { + resetAuthentication(); } @@ -182,19 +126,19 @@ public class CreateDirectoryAction extends AbstractScmWebEditorAction implements String errorMessage = resultDto.getError(); error = true; + ScmConnection scmConn = getScmConnection(); + if (errorMessage.equals(CreateDirectoryResultDto.CONNECTION_FAILED)) { getScmSession().delScmUser(scmConn.getRepositoryId()); - username = null; - pw = null; + resetAuthentication(); return ERROR; } else if (errorMessage.equals(CreateDirectoryResultDto.AUTH_ERROR)) { badLogin = true; - username = null; - pw = null; + resetAuthentication(); getScmSession().delScmUser(scmConn.getRepositoryId()); return LOGIN; diff --git a/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/DownloadFileAction.java b/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/DownloadFileAction.java index 685497d..498cce9 100644 --- a/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/DownloadFileAction.java +++ b/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/DownloadFileAction.java @@ -23,58 +23,49 @@ package org.nuiton.scmwebeditor.uiweb.actions; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.apache.shiro.authc.UsernamePasswordToken; import org.nuiton.scmwebeditor.api.ScmConnection; -import org.nuiton.scmwebeditor.api.ScmProvider; -import org.nuiton.scmwebeditor.uiweb.ScmWebEditorConfig; import javax.servlet.ServletOutputStream; -import javax.servlet.http.HttpSession; -import java.io.*; -import java.text.Normalizer; +import java.io.BufferedInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.Optional; /** * Allows to download the file at the given path */ -public class DownloadFileAction extends ScmWebEditorMainAction { +public class DownloadFileAction extends AbstractScmWebEditorWithAddressAction { private static final Log log = LogFactory.getLog(DownloadFileAction.class); /** the path to the root of the repository */ protected String repositoryRoot; - public String getRepositoryRoot() { return repositoryRoot; } public void setRepositoryRoot(String repositoryRoot) { this.repositoryRoot = repositoryRoot; } - - /** - * Execution of the download file action - * @return a code interpreted in the file struts.xml - */ + @Override public String execute() { - HttpSession session = request.getSession(); - String sessionId = session.getId(); - String pathToLocalRepos = ScmWebEditorConfig.getLocalRepositoriesPath() + File.separator + sessionId; + initAddress(); - ScmProvider provider = ScmWebEditorConfig.getProvider(scmType); - ScmConnection scmConn = provider.getConnection(address, pathToLocalRepos); + String repositoryUUID = getRepositoryUUID(); - // getting the authentication information - // if the repository is not protected for writing, we get its UUID - String repositoryUUID = scmConn.getRepositoryId(); - if (repositoryUUID == null) { - repositoryUUID = address.replace(' ', '_'); - repositoryUUID = Normalizer.normalize(repositoryUUID, Normalizer.Form.NFD).replaceAll("[\u0300-\u036F]", ""); - } + Optional<UsernamePasswordToken> optionalAuthentication = initAuthenticationInformations(repositoryUUID, false, false); + boolean withAuthentication = optionalAuthentication.isPresent(); + + String username = withAuthentication ? optionalAuthentication.get().getUsername() : null; + String pw = withAuthentication ? new String(optionalAuthentication.get().getPassword()) : null; + + ScmConnection scmConn = getScmConnection(); - String[] usernamePw = getUsernamePwFromSession(repositoryUUID, username, pw); - username = usernamePw[0]; - pw = usernamePw[1]; // getting the path to use to download the file - String filePath = scmConn.getFilePath(address, repositoryRoot, username, pw); + String filePath = scmConn.getFilePath(getAddress(), repositoryRoot, username, pw); String filename = filePath.substring(filePath.lastIndexOf('/') + 1); File fileToDownload = new File(filePath); diff --git a/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/EditAction.java b/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/EditAction.java index 7323cde..84e441c 100644 --- a/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/EditAction.java +++ b/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/EditAction.java @@ -24,8 +24,7 @@ package org.nuiton.scmwebeditor.uiweb.actions; import org.apache.commons.io.FileUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.apache.shiro.codec.Base64; -import org.apache.shiro.crypto.BlowfishCipherService; +import org.apache.shiro.authc.UsernamePasswordToken; import org.nuiton.scmwebeditor.api.OperationNotSupportedException; import org.nuiton.scmwebeditor.api.ScmConnection; import org.nuiton.scmwebeditor.api.ScmProvider; @@ -34,14 +33,11 @@ import org.nuiton.scmwebeditor.api.dto.result.AbstractResultDto; import org.nuiton.scmwebeditor.uiweb.ScmWebEditorConfig; import javax.naming.AuthenticationException; -import javax.servlet.http.Cookie; -import javax.servlet.http.HttpSession; import java.io.File; import java.io.IOException; -import java.io.UnsupportedEncodingException; -import java.text.Normalizer; import java.util.LinkedList; import java.util.Map; +import java.util.Optional; /** * Allows to edit a file @@ -120,21 +116,15 @@ public class EditAction extends ScmWebEditorMainAction { public void setFileDirectlyAccessible( boolean fileDirectlyAccessible) { this.fileDirectlyAccessible = fileDirectlyAccessible; } - /** - * Execution of the edit action - * @return a code interpreted in the file struts.xml - */ + @Override public String execute() { autoSaveInterval = ScmWebEditorConfig.getAutoSaveInterval(); - HttpSession session = request.getSession(); - String sessionId = session.getId(); + String pathToLocalRepos = getPathToLocalRepos(); - String pathToLocalRepos = ScmWebEditorConfig.getLocalRepositoriesPath() + File.separator + sessionId; - - ScmProvider provider = ScmWebEditorConfig.getProvider(scmType); - ScmConnection scmConn = provider.getConnection(address, pathToLocalRepos); + ScmProvider provider = getScmProvider(); + ScmConnection scmConn = getScmConnection(); fileDirectlyAccessible = provider.filesDirectlyAccessible(); @@ -155,92 +145,25 @@ public class EditAction extends ScmWebEditorMainAction { String originalText = ""; // if the repository is not protected, we get its UUID - String repositoryUUID = scmConn.getRepositoryId(); - if (repositoryUUID == null) { - repositoryUUID = address.replace(' ', '_'); - repositoryUUID = Normalizer.normalize(repositoryUUID, Normalizer.Form.NFD).replaceAll("[\u0300-\u036F]", ""); - } + String repositoryUUID = getRepositoryUUID(); + String address = getAddress(); if (repositoryRoot == null) { repositoryRoot = address.substring(0, address.lastIndexOf('/')); } else if (repositoryRoot.equals("")) { repositoryRoot = address.substring(0, address.lastIndexOf('/')); } - if (log.isDebugEnabled()) { - log.debug("Login : " + username); - } - - - /* - * Reading the cookie - */ - + Optional<UsernamePasswordToken> optionalAuthentication = initAuthenticationInformations(repositoryUUID, false, false); + boolean withAuthentication = optionalAuthentication.isPresent(); - String usernamepwCookie = null; - // read the cookies - - BlowfishCipherService bf = new BlowfishCipherService(); - - byte[] privateKey = Base64.decode(ScmWebEditorConfig.getKey()); - - if (request.getCookies() != null) { - for (Cookie c : request.getCookies()) { - if (c.getName().equals(repositoryUUID)) { - usernamepwCookie = c.getValue(); - } - } - } - - if (usernamepwCookie != null) { - - String usernameDecode = null; - try { - usernameDecode = new String(bf.decrypt(Base64.decode(usernamepwCookie), privateKey).getBytes(), "UTF-8"); - } catch (UnsupportedEncodingException e) { - if (log.isErrorEnabled()) { - log.error("Can not create a String with UTF-8 encoding"); - } - } - - if (usernameDecode != null) { - String[] resCookie = usernameDecode.split(","); - if (resCookie.length == 2) { - username = resCookie[0]; - pw = resCookie[1]; - } - } - } - - if (saveCookie) { - if (username != null && pw != null) { - - if (!username.equals("") && !pw.equals("")) { - - Cookie authCookie = null; - - try { - authCookie = new Cookie(repositoryUUID, bf.encrypt((username + "," + pw).getBytes("UTF-8"), - privateKey).toBase64()); - } catch (UnsupportedEncodingException e) { - if (log.isErrorEnabled()) { - log.error("Can not get a String from UTF-8 encoding"); - } - } - - if (authCookie != null) { - authCookie.setMaxAge(60 * 60 * 24 * 365); - response.addCookie(authCookie); - } - } - } + String username = withAuthentication ? optionalAuthentication.get().getUsername() : null; + String pw = withAuthentication ? new String(optionalAuthentication.get().getPassword()) : null; + if (log.isDebugEnabled()) { + log.debug("Login : " + username + " to repository: " + repositoryUUID); } - // authentication - String[] usernamePw = getUsernamePwFromSession(repositoryUUID, username, pw); - username = usernamePw[0]; - pw = usernamePw[1]; String name = username; String password = pw; @@ -305,18 +228,8 @@ public class EditAction extends ScmWebEditorMainAction { log.debug("Auth Fail ", e); } - // deleting the cookies for this repository - for (Cookie c : request.getCookies()) { - if (c.getName().equals(repositoryUUID)) { - c.setMaxAge(0);//On supprime le cookie - response.addCookie(c); - if (log.isDebugEnabled()) { - log.debug("Cookie supprimé"); - } - } - } + removeRepositoryFromCache(repositoryUUID); - getScmSession().delScmUser(repositoryUUID); //redirect to a login page return LOGIN; } catch (IOException e) { diff --git a/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/GetImageAction.java b/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/GetImageAction.java index dedb3d4..e64daaa 100644 --- a/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/GetImageAction.java +++ b/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/GetImageAction.java @@ -23,11 +23,8 @@ package org.nuiton.scmwebeditor.uiweb.actions; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.nuiton.scmwebeditor.uiweb.ScmWebEditorConfig; import javax.imageio.ImageIO; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpSession; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.File; @@ -50,10 +47,6 @@ public class GetImageAction extends ScmWebEditorMainAction { /** the path to the image */ protected String imagePath; - /** the HTTP request sent to the server */ - protected HttpServletRequest servletRequest; - - public byte[] getImageInByte() { return imageInByte; } public void setImageInByte(byte[] imageInByte) { @@ -69,18 +62,14 @@ public class GetImageAction extends ScmWebEditorMainAction { public void setImagePath(String imagePath) { this.imagePath = imagePath; } - + @Override public String execute() { return SUCCESS; } public byte[] getCustomImageInBytes() { - - HttpSession session = servletRequest.getSession(); - String sessionId = session.getId(); - - String pathToLocalRepos = ScmWebEditorConfig.getLocalRepositoriesPath() + File.separator + sessionId; + String pathToLocalRepos = getPathToLocalRepos(); if (imagePath.startsWith(pathToLocalRepos)) { @@ -140,8 +129,4 @@ public class GetImageAction extends ScmWebEditorMainAction { return type; } - @Override - public void setServletRequest(HttpServletRequest request) { - servletRequest = request; - } } diff --git a/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/ListBranchesAction.java b/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/ListBranchesAction.java index 8f102a6..e9e4540 100644 --- a/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/ListBranchesAction.java +++ b/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/ListBranchesAction.java @@ -23,39 +23,26 @@ package org.nuiton.scmwebeditor.uiweb.actions; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.apache.shiro.authc.UsernamePasswordToken; import org.nuiton.scmwebeditor.api.OperationNotSupportedException; import org.nuiton.scmwebeditor.api.ScmProvider; -import org.nuiton.scmwebeditor.uiweb.ScmWebEditorConfig; import java.util.List; +import java.util.Optional; /** * Gives a list of the existing branches on the repository */ -public class ListBranchesAction extends AbstractScmWebEditorAction { +public class ListBranchesAction extends AbstractScmWebEditorWithAddressAction { private static final Log log = LogFactory.getLog(ListBranchesAction.class); - /** the repository's address */ - protected String address; - /** equals true if the SCM is able to use branches */ protected boolean scmSupportsBranches; /** list of the existing branches on the repository */ protected List<String> branches; - /** the username to use to connect to the repository */ - protected String username; - - /** the password to use to connect to the repository */ - protected String pw; - - - public String getAddress() { return address; } - - public void setAddress(String address) { this.address = address; } - public boolean isScmSupportsBranches() { return scmSupportsBranches; } public void setScmSupportsBranches(boolean scmSupportsBranches) { this.scmSupportsBranches = scmSupportsBranches; } @@ -64,34 +51,24 @@ public class ListBranchesAction extends AbstractScmWebEditorAction { public void setBranches(List<String> branches) { this.branches = branches; } - public String getUsername() { return username; } - - public void setUsername(String username) { this.username = username; } - - public String getPw() { return pw; } - - public void setPw(String pw) { this.pw = pw; } - - - /** - * Execution of the list branches action - * @return a code interpreted in the file struts.xml - */ + @Override public String execute() { - if (username == null) { - username = "anonymous"; - } - if (pw == null) { - pw = "anonymous"; - } + initAddress(); + + String repositoryUUID = getRepositoryUUID(); - ScmProvider provider = ScmWebEditorConfig.getProvider(scmType); + Optional<UsernamePasswordToken> optionalAuthentication = initAuthenticationInformations(repositoryUUID, false, false); + boolean withAuthentication = optionalAuthentication.isPresent(); + String username = withAuthentication ? optionalAuthentication.get().getUsername() : null; + String pw = withAuthentication ? new String(optionalAuthentication.get().getPassword()) : null; + ScmProvider provider = getScmProvider(); scmSupportsBranches = provider.supportsBranches(); + try { - branches = provider.listBranches(address, username, pw); + branches = provider.listBranches(getAddress(), username, pw); } catch (OperationNotSupportedException e) { if (log.isDebugEnabled()) { log.debug("The SCM " + scmType + " does not support branches", e); diff --git a/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/LogoutAction.java b/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/LogoutAction.java index d0a9f5b..a4e1d40 100644 --- a/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/LogoutAction.java +++ b/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/LogoutAction.java @@ -23,22 +23,16 @@ package org.nuiton.scmwebeditor.uiweb.actions; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.apache.struts2.interceptor.ServletRequestAware; -import org.apache.struts2.interceptor.ServletResponseAware; import org.nuiton.scmwebeditor.api.ScmConnection; import org.nuiton.scmwebeditor.api.ScmProvider; import org.nuiton.scmwebeditor.uiweb.ScmWebEditorConfig; -import javax.servlet.http.Cookie; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; import java.io.File; -import java.text.Normalizer; /** * Ends the user session on the repository */ -public class LogoutAction extends AbstractScmWebEditorAction implements ServletRequestAware, ServletResponseAware { +public class LogoutAction extends AbstractScmWebEditorAction { private static final long serialVersionUID = 6937086747942656369L; @@ -50,13 +44,6 @@ public class LogoutAction extends AbstractScmWebEditorAction implements ServletR /** the URL to the repository's root */ protected String projectUrl; - /** the HTTP request sent to the server */ - protected transient HttpServletRequest request; - - /** the HTTP response to send to the client */ - protected transient HttpServletResponse response; - - public void setAddress(String address) { this.address = address; } public String getAddress() { return address; } @@ -65,11 +52,7 @@ public class LogoutAction extends AbstractScmWebEditorAction implements ServletR public void setProjectUrl(String projectUrl) { this.projectUrl = projectUrl; } - - /** - * Execution of the logout action - * @return a code interpreted in the file struts.xml - */ + @Override public String execute() { String sessionId = request.getSession().getId(); @@ -79,38 +62,15 @@ public class LogoutAction extends AbstractScmWebEditorAction implements ServletR ScmConnection scmConn = provider.getConnection(address, pathToLocalRepos); // getting the repository unique identifier if it is possible - String repositoryId = scmConn.getRepositoryId(); - if (repositoryId == null) { - repositoryId = address.replace(' ', '_'); - repositoryId = Normalizer.normalize(repositoryId, Normalizer.Form.NFD).replaceAll("[\u0300-\u036F]", ""); - } + String repositoryUUID = getRepositoryUUID(scmConn, address); // deleting the cookies for this repository - for (Cookie c : request.getCookies()) { - if (c.getName().equals(repositoryId)) { - c.setMaxAge(0);// deleting the cookie - response.addCookie(c); - if (log.isDebugEnabled()) { - log.debug("Cookie supprimé"); - } - } - } + removeRepositoryCookie(repositoryUUID); // deleting the authentication info in session - getScmSession().delScmUser(repositoryId); + getScmSession().delScmUser(repositoryUUID); return SUCCESS; } - - @Override - public void setServletRequest(HttpServletRequest request) { - this.request = request; - } - - @Override - public void setServletResponse(HttpServletResponse response) { - this.response = response; - } - } 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 368d26a..b2ff290 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 @@ -21,35 +21,23 @@ */ package org.nuiton.scmwebeditor.uiweb.actions; +import org.apache.shiro.authc.UsernamePasswordToken; 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; -import org.nuiton.scmwebeditor.uiweb.ScmWebEditorConfig; -import javax.servlet.http.HttpSession; -import java.io.File; -import java.text.Normalizer; +import java.util.Optional; /** * Moves a file in the repository */ -public class MoveFileAction extends AbstractScmWebEditorAction { +public class MoveFileAction extends AbstractScmWebEditorWithAddressAction { private static final long serialVersionUID = 4244339447567114412L; public static final String REDIRECT = "redirect"; - /** the username to use to connect to the repository */ - protected String username; - - /** the password to use to connect to the repository */ - protected String pw; - - /** the repository's address */ - protected String address; - /** the URL the root of the repository */ protected String scmRoot; @@ -71,31 +59,6 @@ public class MoveFileAction extends AbstractScmWebEditorAction { /** the full path of the destination directory */ protected String destinationDirectory; - - public String getUsername() { - return username; - } - - public void setUsername(String username) { - this.username = username; - } - - public String getPw() { - return pw; - } - - public void setPw(String pw) { - this.pw = pw; - } - - public void setAddress(String address) { - this.address = address; - } - - public String getAddress() { - return address; - } - public boolean isBadLogin() { return badLogin; } @@ -131,35 +94,17 @@ public class MoveFileAction extends AbstractScmWebEditorAction { public void setDestinationDirectory( String destinationDirectory) { this.destinationDirectory = destinationDirectory; } - /** - * Execution of the move a file action - * @return a code interpreted in the file struts.xml - */ + @Override public String execute() { - HttpSession session = request.getSession(); - String sessionId = session.getId(); - String pathToLocalRepos = ScmWebEditorConfig.getLocalRepositoriesPath() + File.separator + sessionId; - - 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("/")) { - address = address.substring(0, address.lastIndexOf('/')); - } + initAddress(); - String repositoryUUID = scmConn.getRepositoryId(); - if (repositoryUUID == null) { - repositoryUUID = address.replace(' ', '_'); - repositoryUUID = Normalizer.normalize(repositoryUUID, Normalizer.Form.NFD).replaceAll("[\u0300-\u036F]", ""); - } + String repositoryUUID = getRepositoryUUID(); - String[] usernamePw = getUsernamePwFromSession(repositoryUUID, username, pw); - username = usernamePw[0]; - pw = usernamePw[1]; + Optional<UsernamePasswordToken> optionalAuthentication = initAuthenticationInformations(repositoryUUID, false, false); + boolean withAuthentication = optionalAuthentication.isPresent(); + String username = withAuthentication ? optionalAuthentication.get().getUsername() : null; + String pw = withAuthentication ? new String(optionalAuthentication.get().getPassword()) : null; MoveFileDto dto = new MoveFileDto(); dto.setUsername(username); @@ -168,6 +113,7 @@ public class MoveFileAction extends AbstractScmWebEditorAction { dto.setFileToMove(fileToMove); dto.setDestinationDirectory(destinationDirectory); + ScmFileManager scmFileManager = getScmFileManager(); MoveFileResultDto resultDto = scmFileManager.moveFile(dto); if (resultDto.getScmRoot() != null) { @@ -177,32 +123,23 @@ public class MoveFileAction extends AbstractScmWebEditorAction { fileRoot = resultDto.getFileRoot(); } - if (username != null && pw != null) { - if (username.equals("") && pw.equals("")) { - username = null; - pw = null; - } - } - - if (resultDto.getError() != null) { + ScmConnection scmConn = getScmConnection(); String errorMessage = resultDto.getError(); error = true; if (errorMessage.equals(MoveFileResultDto.CONNECTION_FAILED)) { getScmSession().delScmUser(scmConn.getRepositoryId()); - username = null; - pw = null; + resetAuthentication(); return ERROR; } else if (errorMessage.equals(MoveFileResultDto.AUTH_ERROR)) { badLogin = true; - username = null; - pw = null; + resetAuthentication(); getScmSession().delScmUser(scmConn.getRepositoryId()); return LOGIN; diff --git a/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/PreviewAction.java b/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/PreviewAction.java index 0a6f933..9528920 100644 --- a/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/PreviewAction.java +++ b/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/PreviewAction.java @@ -24,27 +24,22 @@ package org.nuiton.scmwebeditor.uiweb.actions; import com.github.rjeschke.txtmark.Processor; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.apache.struts2.interceptor.ServletRequestAware; import org.dom4j.Document; import org.nuiton.jrst.JRST; import org.nuiton.jrst.legacy.JRSTReader; -import javax.servlet.http.HttpServletRequest; import java.io.StringReader; /** * Gives a preview of the edited RST or Markdown file */ -public class PreviewAction extends AbstractScmWebEditorAction implements ServletRequestAware { +public class PreviewAction extends AbstractScmWebEditorAction { /** serialVersionUID. */ private static final long serialVersionUID = -2388759298175611718L; private static final Log log = LogFactory.getLog(PreviewAction.class); - /** the HTTP request sent to the server */ - protected transient HttpServletRequest request; - /** the HTML code to display for the preview */ protected String htmlPreview; @@ -111,10 +106,4 @@ public class PreviewAction extends AbstractScmWebEditorAction implements Servlet return SUCCESS; } - @Override - public void setServletRequest(HttpServletRequest request) { - this.request = request; - } - - } 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 df2ae20..0c7ab44 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 @@ -21,35 +21,23 @@ */ package org.nuiton.scmwebeditor.uiweb.actions; +import org.apache.shiro.authc.UsernamePasswordToken; 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; -import org.nuiton.scmwebeditor.uiweb.ScmWebEditorConfig; -import javax.servlet.http.HttpSession; -import java.io.File; -import java.text.Normalizer; +import java.util.Optional; /** * Removes a directory on the repository */ -public class RemoveDirectoryAction extends AbstractScmWebEditorAction { +public class RemoveDirectoryAction extends AbstractScmWebEditorWithAddressAction { private static final long serialVersionUID = 4244339447567114412L; public static final String REDIRECT = "redirect"; - /** the username to use to connect to the repository */ - protected String username; - - /** the password to use to connect to the repository */ - protected String pw; - - /** the repository's address */ - protected String address; - /** the path to the directory to remove */ protected String directoryToRemove; @@ -65,31 +53,6 @@ public class RemoveDirectoryAction extends AbstractScmWebEditorAction { /** the full path of the root */ protected String fileRoot; - - public String getUsername() { - return username; - } - - public void setUsername(String username) { - this.username = username; - } - - public String getPw() { - return pw; - } - - public void setPw(String pw) { - this.pw = pw; - } - - public void setAddress(String address) { - this.address = address; - } - - public String getAddress() { - return address; - } - public boolean isBadLogin() { return badLogin; } @@ -116,41 +79,24 @@ public class RemoveDirectoryAction extends AbstractScmWebEditorAction { public void setScmRoot(String scmRoot) { this.scmRoot = scmRoot; } - /** - * Execution of the remove directory action - * @return a code interpreted in the file struts.xml - */ + @Override public String execute() { - HttpSession session = request.getSession(); - String sessionId = session.getId(); - String pathToLocalRepos = ScmWebEditorConfig.getLocalRepositoriesPath() + File.separator + sessionId; - - 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("/")) { - address = address.substring(0, address.lastIndexOf('/')); - } + initAddress(); - String repositoryUUID = scmConn.getRepositoryId(); - if (repositoryUUID == null) { - repositoryUUID = address.replace(' ', '_'); - repositoryUUID = Normalizer.normalize(repositoryUUID, Normalizer.Form.NFD).replaceAll("[\u0300-\u036F]", ""); - } + String repositoryUUID = getRepositoryUUID(); - String[] usernamePw = getUsernamePwFromSession(repositoryUUID, username, pw); - username = usernamePw[0]; - pw = usernamePw[1]; + Optional<UsernamePasswordToken> optionalAuthentication = initAuthenticationInformations(repositoryUUID, false, false); + boolean withAuthentication = optionalAuthentication.isPresent(); + String username = withAuthentication ? optionalAuthentication.get().getUsername() : null; + String pw = withAuthentication ? new String(optionalAuthentication.get().getPassword()) : null; RemoveDirectoryDto dto = new RemoveDirectoryDto(); dto.setUsername(username); dto.setPassword(pw); dto.setDirectoryToRemove(directoryToRemove); + ScmFileManager scmFileManager = getScmFileManager(); RemoveDirectoryResultDto resultDto = scmFileManager.removeDirectory(dto); if (resultDto.getScmRoot() != null) { @@ -160,32 +106,25 @@ public class RemoveDirectoryAction extends AbstractScmWebEditorAction { fileRoot = resultDto.getFileRoot(); } - if (username != null && pw != null) { - if (username.equals("") && pw.equals("")) { - username = null; - pw = null; - } - } - if (resultDto.getError() != null) { + ScmConnection scmConn = getScmConnection(); + String errorMessage = resultDto.getError(); error = true; if (errorMessage.equals(RemoveDirectoryResultDto.CONNECTION_FAILED)) { getScmSession().delScmUser(scmConn.getRepositoryId()); - username = null; - pw = null; + resetAuthentication(); return ERROR; } else if (errorMessage.equals(RemoveDirectoryResultDto.AUTH_ERROR)) { badLogin = true; - username = null; - pw = null; + resetAuthentication(); getScmSession().delScmUser(scmConn.getRepositoryId()); return LOGIN; 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 459cf95..08d4fef 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 @@ -21,35 +21,23 @@ */ package org.nuiton.scmwebeditor.uiweb.actions; +import org.apache.shiro.authc.UsernamePasswordToken; 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; -import org.nuiton.scmwebeditor.uiweb.ScmWebEditorConfig; -import javax.servlet.http.HttpSession; -import java.io.File; -import java.text.Normalizer; +import java.util.Optional; /** * Removes a file from the repository */ -public class RemoveFileAction extends AbstractScmWebEditorAction { +public class RemoveFileAction extends AbstractScmWebEditorWithAddressAction { private static final long serialVersionUID = 4244339447567114412L; public static final String REDIRECT = "redirect"; - /** the username to use to connect to the repository */ - protected String username; - - /** the password to use to connect to the repository */ - protected String pw; - - /** the repository's address */ - protected String address; - /** the URL the root of the repository */ protected String scmRoot; @@ -65,31 +53,6 @@ public class RemoveFileAction extends AbstractScmWebEditorAction { /** the full path of the root */ protected String fileRoot; - - public String getUsername() { - return username; - } - - public void setUsername(String username) { - this.username = username; - } - - public String getPw() { - return pw; - } - - public void setPw(String pw) { - this.pw = pw; - } - - public void setAddress(String address) { - this.address = address; - } - - public String getAddress() { - return address; - } - public boolean isBadLogin() { return badLogin; } @@ -116,41 +79,24 @@ public class RemoveFileAction extends AbstractScmWebEditorAction { public void setFileRoot(String fileRoot) { this.fileRoot = fileRoot; } - /** - * Execution of the remove action - * @return a code interpreted in the file struts.xml - */ + @Override public String execute() { - HttpSession session = request.getSession(); - String sessionId = session.getId(); - String pathToLocalRepos = ScmWebEditorConfig.getLocalRepositoriesPath() + File.separator + sessionId; - - ScmProvider provider = ScmWebEditorConfig.getProvider(scmType); + initAddress(); - ScmConnection scmConn = provider.getConnection(address, pathToLocalRepos); - ScmFileManager scmFileManager = provider.getFileManager(scmConn); + String repositoryUUID = getRepositoryUUID(); - // if the repository is not protected for writing, we get its UUID - if (address.endsWith("/")) { - address = address.substring(0, address.lastIndexOf('/')); - } - - String repositoryUUID = scmConn.getRepositoryId(); - if (repositoryUUID == null) { - repositoryUUID = address.replace(' ', '_'); - repositoryUUID = Normalizer.normalize(repositoryUUID, Normalizer.Form.NFD).replaceAll("[\u0300-\u036F]", ""); - } - - String[] usernamePw = getUsernamePwFromSession(repositoryUUID, username, pw); - username = usernamePw[0]; - pw = usernamePw[1]; + Optional<UsernamePasswordToken> optionalAuthentication = initAuthenticationInformations(repositoryUUID, false, false); + boolean withAuthentication = optionalAuthentication.isPresent(); + String username = withAuthentication ? optionalAuthentication.get().getUsername() : null; + String pw = withAuthentication ? new String(optionalAuthentication.get().getPassword()) : null; RemoveFileDto dto = new RemoveFileDto(); dto.setUsername(username); dto.setPassword(pw); dto.setScmPath(scmPath); + ScmFileManager scmFileManager = getScmFileManager(); RemoveFileResultDto resultDto = scmFileManager.removeFile(dto); if (resultDto.getScmRoot() != null) { @@ -160,32 +106,24 @@ public class RemoveFileAction extends AbstractScmWebEditorAction { fileRoot = resultDto.getFileRoot(); } - if (username != null && pw != null) { - if (username.equals("") && pw.equals("")) { - username = null; - pw = null; - } - } - - if (resultDto.getError() != null) { + ScmConnection scmConn = getScmConnection(); + String errorMessage = resultDto.getError(); error = true; if (errorMessage.equals(RemoveFileResultDto.CONNECTION_FAILED)) { getScmSession().delScmUser(scmConn.getRepositoryId()); - username = null; - pw = null; + resetAuthentication(); return ERROR; } else if (errorMessage.equals(RemoveFileResultDto.AUTH_ERROR)) { badLogin = true; - username = null; - pw = null; + resetAuthentication(); getScmSession().delScmUser(scmConn.getRepositoryId()); return LOGIN; diff --git a/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/ResetAction.java b/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/ResetAction.java index 51390f3..6f6fd5b 100644 --- a/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/ResetAction.java +++ b/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/ResetAction.java @@ -24,19 +24,18 @@ package org.nuiton.scmwebeditor.uiweb.actions; import org.apache.commons.io.FileUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.apache.shiro.authc.UsernamePasswordToken; import org.nuiton.scmwebeditor.api.ScmConnection; -import org.nuiton.scmwebeditor.api.ScmProvider; -import org.nuiton.scmwebeditor.uiweb.ScmWebEditorConfig; import javax.naming.AuthenticationException; import java.io.File; import java.io.IOException; -import java.text.Normalizer; +import java.util.Optional; /** * Deletes all the changes made to the edited file */ -public class ResetAction extends AbstractScmWebEditorAction { +public class ResetAction extends AbstractScmWebEditorWithAddressAction { private static final long serialVersionUID = -1154924826535371319L; @@ -48,54 +47,31 @@ public class ResetAction extends AbstractScmWebEditorAction { /** the number of the current revision */ protected String numRevision; - /** the repository's address */ - protected String address; - - /** the username to use to connect to the repository */ - protected String username; - - /** the password to use to connect to the repository */ - protected String pw; - /** information about an error if one occurs */ protected String error; - public String getLastRevision() { return lastRevision; } public String getNumRevision() { return numRevision; } - public void setAddress(String address) { this.address = address; } - - public void setUsername(String username) { this.username = username; } - - public void setPw(String pw) { this.pw = pw; } - public String getError() { return error; } - /** - * Execution of the reset action - * @return a code interpreted in the file struts.xml - */ + @Override public String execute() { - String sessionId = request.getSession().getId(); - String pathToLocalRepos = ScmWebEditorConfig.getLocalRepositoriesPath() + File.separator + sessionId; + initAddress(); - ScmProvider provider = ScmWebEditorConfig.getProvider(scmType); - ScmConnection scmConn = provider.getConnection(address, pathToLocalRepos); + String repositoryUUID = getRepositoryUUID(); - // getting the repository unique identifier if it is possible - String repositoryId = scmConn.getRepositoryId(); - if (repositoryId == null) { - repositoryId = address.replace(' ', '_'); - repositoryId = Normalizer.normalize(repositoryId, Normalizer.Form.NFD).replaceAll("[\u0300-\u036F]", ""); - } - String[] usernamePw = getUsernamePwFromSession(repositoryId, username, pw); - username = usernamePw[0]; - pw = usernamePw[1]; + Optional<UsernamePasswordToken> optionalAuthentication = initAuthenticationInformations(repositoryUUID, false, false); + boolean withAuthentication = optionalAuthentication.isPresent(); + String username = withAuthentication ? optionalAuthentication.get().getUsername() : null; + String pw = withAuthentication ? new String(optionalAuthentication.get().getPassword()) : null; + + ScmConnection scmConn = getScmConnection(); try { + String address = getAddress(); File originalFile = scmConn.getFileContent(address, username, pw); lastRevision = FileUtils.readFileToString(originalFile); numRevision = scmConn.getHeadRevisionNumber(address, username, pw); @@ -114,5 +90,4 @@ public class ResetAction extends AbstractScmWebEditorAction { return SUCCESS; } - } diff --git a/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/SaveAction.java b/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/SaveAction.java index f46d2f1..7d61d94 100644 --- a/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/SaveAction.java +++ b/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/SaveAction.java @@ -50,11 +50,7 @@ public class SaveAction extends ScmWebEditorCommitAction { public Date getDate() { return date; } - - /** - * Execution of the save action - * @return a code interpreted in the file struts.xml - */ + @Override public String execute() { if (log.isDebugEnabled()) { diff --git a/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/ScmWebEditorCommitAction.java b/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/ScmWebEditorCommitAction.java index 2f06b9d..33de9c1 100644 --- a/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/ScmWebEditorCommitAction.java +++ b/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/ScmWebEditorCommitAction.java @@ -23,32 +23,21 @@ package org.nuiton.scmwebeditor.uiweb.actions; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.apache.shiro.codec.Base64; -import org.apache.shiro.crypto.BlowfishCipherService; -import org.apache.struts2.interceptor.ServletRequestAware; -import org.apache.struts2.interceptor.ServletResponseAware; +import org.apache.shiro.authc.UsernamePasswordToken; import org.dom4j.Document; import org.nuiton.jrst.JRST; import org.nuiton.jrst.legacy.JRSTReader; import org.nuiton.scmwebeditor.api.ScmConnection; -import org.nuiton.scmwebeditor.api.ScmProvider; import org.nuiton.scmwebeditor.api.dto.CommitDto; import org.nuiton.scmwebeditor.api.dto.result.CommitResultDto; -import org.nuiton.scmwebeditor.uiweb.ScmWebEditorConfig; -import javax.servlet.http.Cookie; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import javax.servlet.http.HttpSession; -import java.io.File; import java.io.StringReader; -import java.io.UnsupportedEncodingException; -import java.text.Normalizer; +import java.util.Optional; /** * Commits all the changes to the remote repository */ -public class ScmWebEditorCommitAction extends AbstractScmWebEditorAction implements ServletRequestAware, ServletResponseAware { +public class ScmWebEditorCommitAction extends AbstractScmWebEditorWithAddressAction { public static final String FILE_MODIFY = "fileModify"; @@ -65,15 +54,6 @@ public class ScmWebEditorCommitAction extends AbstractScmWebEditorAction impleme /** the old file content */ protected String origText; - /** the username to use to connect to the repository */ - protected String username; - - /** the password to use to connect to the repository */ - protected String pw; - - /** the repository's address */ - protected String address; - /** the last revision's file content */ protected String lastText; @@ -107,12 +87,6 @@ public class ScmWebEditorCommitAction extends AbstractScmWebEditorAction impleme /** equals true if only a commit is requested, without a push */ protected boolean commitOnly; - /** the HTTP request sent to the server */ - protected transient HttpServletRequest request; - - /** the HTTP response to send to the client */ - protected transient HttpServletResponse response; - public String getCommitMessage() { return commitMessage; } public void setCommitMessage(String commitMessage) { this.commitMessage = commitMessage; } @@ -129,18 +103,6 @@ public class ScmWebEditorCommitAction extends AbstractScmWebEditorAction impleme public void setOrigText(String origText) { this.origText = origText; } - public String getUsername() { return username; } - - public void setUsername(String username) { this.username = username; } - - public String getPw() { return pw; } - - public void setPw(String pw) { this.pw = pw; } - - public String getAddress() { return address; } - - public void setAddress(String address) { this.address = address; } - public String getLastText() { return lastText; } public void setLastText(String lastText) { this.lastText = lastText; } @@ -169,8 +131,6 @@ public class ScmWebEditorCommitAction extends AbstractScmWebEditorAction impleme public void setMimeType(String mimeType) { this.mimeType = mimeType; } - public HttpServletRequest getRequest() { return request; } - public boolean getForce() { return force; } public void setForce(boolean force) { this.force = force; } @@ -208,13 +168,9 @@ public class ScmWebEditorCommitAction extends AbstractScmWebEditorAction impleme return false; } - } - /** - * Execution of the commit action - * @return a code interpreted in the file struts.xml - */ + @Override public String execute() { System.setProperty("file.encoding", "UTF-8"); @@ -228,81 +184,20 @@ public class ScmWebEditorCommitAction extends AbstractScmWebEditorAction impleme } // connection to the repository - HttpSession session = request.getSession(); - String sessionId = session.getId(); - String pathToLocalRepos = ScmWebEditorConfig.getLocalRepositoriesPath() + File.separator + sessionId; - - ScmProvider provider = ScmWebEditorConfig.getProvider(scmType); - ScmConnection scmConn = provider.getConnection(address, pathToLocalRepos); + ScmConnection scmConn = getScmConnection(); // if the repository is not protected for writing, we get its UUID - String repositoryUUID = scmConn.getRepositoryId(); - if (repositoryUUID == null) { - repositoryUUID = address.replace(' ', '_'); - repositoryUUID = Normalizer.normalize(repositoryUUID, Normalizer.Form.NFD).replaceAll("[\u0300-\u036F]", ""); - } - - - /* - * reading the cookie - */ - String usernamepwCookie = null; - // read the cookies - - BlowfishCipherService bf = new BlowfishCipherService(); - - byte[] privateKey = Base64.decode(ScmWebEditorConfig.getKey()); - - for (Cookie c : request.getCookies()) { - if (c.getName().equals(repositoryUUID)) - usernamepwCookie = c.getValue(); - } - - - if (usernamepwCookie != null) { + String repositoryUUID = getRepositoryUUID(); - String usernameDecode = null; - try { - usernameDecode = new String(bf.decrypt(Base64.decode(usernamepwCookie), privateKey).getBytes(), "UTF-8"); - } catch (UnsupportedEncodingException e) { - if (log.isErrorEnabled()) { - log.error("Can not create a String with UTF-8 encoding"); - } - } + Optional<UsernamePasswordToken> optionalAuthentication = initAuthenticationInformations(repositoryUUID, true, saveCookie); + boolean withAuthentication = optionalAuthentication.isPresent(); + String username = withAuthentication ? optionalAuthentication.get().getUsername() : null; + String pw = withAuthentication ? new String(optionalAuthentication.get().getPassword()) : null; - if (usernameDecode != null) { - String[] resCookie = usernameDecode.split(","); - if (resCookie.length == 2) { - username = resCookie[0]; - pw = resCookie[1]; - } - } + if (log.isDebugEnabled()) { + log.debug("Login : " + username + " to repository: " + repositoryUUID); } - if (saveCookie) { - if (username != null && pw != null) { - - Cookie authCookie = null; - - try { - authCookie = new Cookie(repositoryUUID, bf.encrypt((username + "," + pw).getBytes("UTF-8"), privateKey).toBase64()); - } catch (UnsupportedEncodingException e) { - if (log.isErrorEnabled()) { - log.error("Can not get bytes from UTF-8 encoding"); - } - } - - if (authCookie != null) { - authCookie.setMaxAge(60 * 60 * 24 * 365); - response.addCookie(authCookie); - } - } - - } - - String[] usernamePw = getUsernamePwFromSession(repositoryUUID, username, pw); - username = usernamePw[0]; - pw = usernamePw[1]; CommitDto dto = new CommitDto(); dto.setUsername(username); @@ -310,7 +205,7 @@ public class ScmWebEditorCommitAction extends AbstractScmWebEditorAction impleme dto.setNewText(newText); dto.setCommitMessage(commitMessage); dto.setForce(force); - dto.setAddress(address); + dto.setAddress(getAddress()); dto.setCommitOnly(commitOnly); CommitResultDto resultDto = scmConn.commit(dto); @@ -339,7 +234,7 @@ public class ScmWebEditorCommitAction extends AbstractScmWebEditorAction impleme if (error.equals(CommitResultDto.ERROR_PATH)) { return ERROR_PATH; } else if (error.equals(CommitResultDto.AUTH_ERROR)) { - request.setAttribute(getParameterAddress(), address); + request.setAttribute(getParameterAddress(), getAddress()); getScmSession().delScmUser(scmConn.getRepositoryId()); username = null; pw = null; @@ -355,15 +250,4 @@ public class ScmWebEditorCommitAction extends AbstractScmWebEditorAction impleme return SUCCESS; } - - @Override - public void setServletRequest(HttpServletRequest request) { - this.request = request; - } - - @Override - public void setServletResponse(HttpServletResponse response) { - this.response = response; - } - } diff --git a/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/ScmWebEditorMainAction.java b/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/ScmWebEditorMainAction.java index ffca302..7943130 100644 --- a/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/ScmWebEditorMainAction.java +++ b/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/ScmWebEditorMainAction.java @@ -21,19 +21,18 @@ */ package org.nuiton.scmwebeditor.uiweb.actions; +import com.google.common.base.Strings; import com.google.common.collect.Lists; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.apache.struts2.interceptor.ServletResponseAware; import org.nuiton.scmwebeditor.uiweb.ScmWebEditorConfig; -import javax.servlet.http.HttpServletResponse; import java.util.List; /** * The first action called when entering the website */ -public class ScmWebEditorMainAction extends AbstractScmWebEditorAction implements ServletResponseAware { +public class ScmWebEditorMainAction extends AbstractScmWebEditorWithAddressAction { private static final long serialVersionUID = 8361035067228171624L; @@ -44,9 +43,6 @@ public class ScmWebEditorMainAction extends AbstractScmWebEditorAction implement public static final String EDIT_PAGE = "editPage"; - /** the repository's address */ - protected String address; - /** the URL to the repository's root */ protected String projectUrl; @@ -68,15 +64,6 @@ public class ScmWebEditorMainAction extends AbstractScmWebEditorAction implement /** the number of the edited file's revision */ protected String numRevision; - /** the username to use to connect to the repository */ - protected String username; - - /** the password to use to connect to the repository */ - protected String pw; - - /** the HTTP response to send to the client */ - protected transient HttpServletResponse response; - /** the repository's unique identifier */ protected String repositoryId; @@ -93,30 +80,6 @@ public class ScmWebEditorMainAction extends AbstractScmWebEditorAction implement this.repositoryId = repositoryId; } - public String getUsername() { - return username; - } - - public void setUsername(String username) { - this.username = username; - } - - public String getPw() { - return pw; - } - - public void setPw(String pw) { - this.pw = pw; - } - - public String getAddress() { - return address; - } - - public void setAddress(String address) { - this.address = address; - } - public String getFormat() { return format; } @@ -156,17 +119,13 @@ public class ScmWebEditorMainAction extends AbstractScmWebEditorAction implement * @return true if the parameters are empty */ private boolean testParameters() { - if (address == null || address.length() == 0) { + if (Strings.isNullOrEmpty(getAddress())) { return true; } else { return false; } } - /** - * Execution of the main action - * @return a code interpreted in the file struts.xml - */ @Override public String execute() { @@ -193,11 +152,4 @@ public class ScmWebEditorMainAction extends AbstractScmWebEditorAction implement } - - @Override - public void setServletResponse(HttpServletResponse response) { - this.response = response; - } - - } 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 30a8279..08ebc59 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 @@ -21,21 +21,18 @@ */ package org.nuiton.scmwebeditor.uiweb.actions; +import org.apache.shiro.authc.UsernamePasswordToken; 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; -import org.nuiton.scmwebeditor.uiweb.ScmWebEditorConfig; -import javax.servlet.http.HttpSession; import java.io.File; -import java.text.Normalizer; +import java.util.Optional; /** * Uploads a file from the client to the repository */ -public class UploadFileAction extends AbstractScmWebEditorAction { +public class UploadFileAction extends AbstractScmWebEditorWithAddressAction { private static final long serialVersionUID = 4244339447567114412L; @@ -50,15 +47,6 @@ public class UploadFileAction extends AbstractScmWebEditorAction { /** the type of the file to upload */ protected String uploadContentType; - /** the username to use to connect to the repository */ - protected String username; - - /** the password to use to connect to the repository */ - protected String pw; - - /** the repository's address */ - protected String address; - /** the URL the root of the repository */ protected String scmRoot; @@ -99,30 +87,6 @@ public class UploadFileAction extends AbstractScmWebEditorAction { this.uploadFileName = uploadFileName; } - public String getUsername() { - return username; - } - - public void setUsername(String username) { - this.username = username; - } - - public String getPw() { - return pw; - } - - public void setPw(String pw) { - this.pw = pw; - } - - public void setAddress(String address) { - this.address = address; - } - - public String getAddress() { - return address; - } - public boolean isBadLogin() { return badLogin; } @@ -151,36 +115,17 @@ public class UploadFileAction extends AbstractScmWebEditorAction { public String getScmPath() { return scmPath; } - - /** - * Execution of the upload action - * @return a code interpreted in the file struts.xml - */ + @Override public String execute() { - HttpSession session = request.getSession(); - String sessionId = session.getId(); - String pathToLocalRepos = ScmWebEditorConfig.getLocalRepositoriesPath() + File.separator + sessionId; + initAddress(); - 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("/")) { - address = address.substring(0, address.lastIndexOf('/')); - } - - String repositoryUUID = scmConn.getRepositoryId(); - if (repositoryUUID == null) { - repositoryUUID = address.replace(' ', '_'); - repositoryUUID = Normalizer.normalize(repositoryUUID, Normalizer.Form.NFD).replaceAll("[\u0300-\u036F]", ""); - } + String repositoryUUID = getRepositoryUUID(); - String[] usernamePw = getUsernamePwFromSession(repositoryUUID, username, pw); - username = usernamePw[0]; - pw = usernamePw[1]; + Optional<UsernamePasswordToken> optionalAuthentication = initAuthenticationInformations(repositoryUUID, false, false); + boolean withAuthentication = optionalAuthentication.isPresent(); + String username = withAuthentication ? optionalAuthentication.get().getUsername() : null; + String pw = withAuthentication ? new String(optionalAuthentication.get().getPassword()) : null; UploadFileDto dto = new UploadFileDto(); dto.setUsername(username); @@ -190,7 +135,7 @@ public class UploadFileAction extends AbstractScmWebEditorAction { dto.setUploadContentType(uploadContentType); dto.setScmPath(scmPath); - UploadFileResultDto resultDto = scmFileManager.uploadFile(dto); + UploadFileResultDto resultDto = getScmFileManager().uploadFile(dto); if (resultDto.getFileRoot() != null) { fileRoot = resultDto.getFileRoot(); @@ -201,22 +146,21 @@ public class UploadFileAction extends AbstractScmWebEditorAction { if (resultDto.getError() != null) { + ScmConnection scmConn = getScmConnection(); String errorMessage = resultDto.getError(); error = true; if (errorMessage.equals(UploadFileResultDto.CONNECTION_FAILED)) { getScmSession().delScmUser(scmConn.getRepositoryId()); - username = null; - pw = null; + resetAuthentication(); return ERROR; } else if (errorMessage.equals(UploadFileResultDto.AUTH_ERROR)) { badLogin = true; - username = null; - pw = null; + resetAuthentication(); getScmSession().delScmUser(scmConn.getRepositoryId()); return LOGIN; diff --git a/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/ViewDiffsAction.java b/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/ViewDiffsAction.java index ac8cbd1..4ba180a 100644 --- a/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/ViewDiffsAction.java +++ b/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/ViewDiffsAction.java @@ -24,19 +24,13 @@ package org.nuiton.scmwebeditor.uiweb.actions; import org.apache.commons.io.FileUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.apache.shiro.codec.Base64; -import org.apache.shiro.crypto.BlowfishCipherService; +import org.apache.shiro.authc.UsernamePasswordToken; import org.nuiton.scmwebeditor.api.ScmConnection; -import org.nuiton.scmwebeditor.api.ScmProvider; -import org.nuiton.scmwebeditor.uiweb.ScmWebEditorConfig; import javax.naming.AuthenticationException; -import javax.servlet.http.Cookie; -import javax.servlet.http.HttpSession; import java.io.File; import java.io.IOException; -import java.io.UnsupportedEncodingException; -import java.text.Normalizer; +import java.util.Optional; /** * Allows to view the history of a file @@ -74,90 +68,23 @@ public class ViewDiffsAction extends ScmWebEditorMainAction { public void setError(String error) { this.error = error; } - /** - * Execution of the view history action - * @return a code interpreted in the file struts.xml - */ + @Override public String execute() { - HttpSession session = request.getSession(); - String sessionId = session.getId(); - error = null; - String pathToLocalRepos = ScmWebEditorConfig.getLocalRepositoriesPath() + File.separator + sessionId; - - ScmProvider provider = ScmWebEditorConfig.getProvider(scmType); - ScmConnection scmConn = provider.getConnection(address, pathToLocalRepos); + String repositoryUUID = getRepositoryUUID(); - // if the repository is not protected, we get its UUID - String repositoryUUID = scmConn.getRepositoryId(); - if (repositoryUUID == null) { - repositoryUUID = address.replace(' ', '_'); - repositoryUUID = Normalizer.normalize(repositoryUUID, Normalizer.Form.NFD).replaceAll("[\u0300-\u036F]", ""); - } + Optional<UsernamePasswordToken> optionalAuthentication = initAuthenticationInformations(repositoryUUID, true, saveCookie); + boolean withAuthentication = optionalAuthentication.isPresent(); + String username = withAuthentication ? optionalAuthentication.get().getUsername() : null; + String pw = withAuthentication ? new String(optionalAuthentication.get().getPassword()) : null; if (log.isDebugEnabled()) { - log.debug("Login : " + username); - } - - - /* - * Reading the cookie - */ - - - String usernamepwCookie = null; - // read the cookies - - BlowfishCipherService bf = new BlowfishCipherService(); - - byte[] privateKey = Base64.decode(ScmWebEditorConfig.getKey()); - - if (request.getCookies() != null) { - for (Cookie c : request.getCookies()) { - if (c.getName().equals(repositoryUUID)) { - usernamepwCookie = c.getValue(); - } - } + log.debug("Login : " + username + " to repository: " + repositoryUUID); } - if (usernamepwCookie != null) { - - String usernameDecode = null; - try { - usernameDecode = new String(bf.decrypt(Base64.decode(usernamepwCookie), privateKey).getBytes(), "UTF-8"); - } catch (UnsupportedEncodingException e) { - if (log.isErrorEnabled()) { - log.error("Can not create a String with UTF-8 encoding"); - } - } - - if (usernameDecode != null) { - String[] resCookie = usernameDecode.split(","); - if (resCookie.length == 2) { - username = resCookie[0]; - pw = resCookie[1]; - } - } - } - - if (saveCookie) { - if (username != null && pw != null) { - - if (!username.equals("") && !pw.equals("")) { - Cookie authCookie = new Cookie(repositoryUUID, bf.encrypt((username + "," + pw).getBytes(), privateKey).toBase64()); - authCookie.setMaxAge(60 * 60 * 24 * 365); - response.addCookie(authCookie); - } - } - - } - - // authentication - String[] usernamePw = getUsernamePwFromSession(repositoryUUID, username, pw); - username = usernamePw[0]; - pw = usernamePw[1]; + initAuthenticationInformations(repositoryUUID, true, saveCookie); String name = username; String password = pw; @@ -169,12 +96,15 @@ public class ViewDiffsAction extends ScmWebEditorMainAction { password = "anonymous"; } + String address = getAddress(); /* * Getting the differences */ try { + + ScmConnection scmConn = getScmConnection(); File tempFile = scmConn.getDiffs(address, name, password, revision1, revision2); if (tempFile != null) { @@ -192,15 +122,7 @@ public class ViewDiffsAction extends ScmWebEditorMainAction { } // deleting the cookies for this repository - for (Cookie c : request.getCookies()) { - if (c.getName().equals(repositoryUUID)) { - c.setMaxAge(0);//On supprime le cookie - response.addCookie(c); - if (log.isDebugEnabled()) { - log.debug("Cookie supprimé"); - } - } - } + removeRepositoryCookie(repositoryUUID); getScmSession().delScmUser(repositoryUUID); error = LOGIN; diff --git a/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/ViewImageAction.java b/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/ViewImageAction.java index 6e43da9..8dbf5c7 100644 --- a/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/ViewImageAction.java +++ b/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/ViewImageAction.java @@ -23,22 +23,16 @@ package org.nuiton.scmwebeditor.uiweb.actions; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.apache.shiro.codec.Base64; -import org.apache.shiro.crypto.BlowfishCipherService; +import org.apache.shiro.authc.UsernamePasswordToken; import org.nuiton.scmwebeditor.api.OperationNotSupportedException; import org.nuiton.scmwebeditor.api.ScmConnection; import org.nuiton.scmwebeditor.api.ScmProvider; import org.nuiton.scmwebeditor.api.dto.result.AbstractResultDto; -import org.nuiton.scmwebeditor.uiweb.ScmWebEditorConfig; import javax.naming.AuthenticationException; -import javax.servlet.http.Cookie; -import javax.servlet.http.HttpSession; -import java.io.File; -import java.io.UnsupportedEncodingException; -import java.text.Normalizer; import java.util.Arrays; import java.util.List; +import java.util.Optional; /** * Allows to view an image @@ -88,35 +82,25 @@ public class ViewImageAction extends ScmWebEditorMainAction { public void setFilesDirectlyAccessible( boolean filesDirectlyAccessible) { this.filesDirectlyAccessible = filesDirectlyAccessible; } - /** - * Execution of the view image action - * @return a code interpreted in the file struts.xml - */ + @Override public String execute() { + String address = getAddress(); format = address.substring(address.lastIndexOf('.') + 1).toLowerCase(); if (!SUPPORTED_IMAGE_FORMATS.contains(format)) { return ERROR_PATH; } - HttpSession session = request.getSession(); - String sessionId = session.getId(); + String pathToLocalRepos = getPathToLocalRepos(); - String pathToLocalRepos = ScmWebEditorConfig.getLocalRepositoriesPath() + File.separator + sessionId; - - ScmProvider provider = ScmWebEditorConfig.getProvider(scmType); - ScmConnection scmConn = provider.getConnection(address, pathToLocalRepos); + ScmProvider provider = getScmProvider(); scmSupportsBranches = provider.supportsBranches(); filesDirectlyAccessible = provider.filesDirectlyAccessible(); // if the repository is not protected, we get its UUID - String repositoryUUID = scmConn.getRepositoryId(); - if (repositoryUUID == null) { - repositoryUUID = address.replace(' ', '_'); - repositoryUUID = Normalizer.normalize(repositoryUUID, Normalizer.Form.NFD).replaceAll("[\u0300-\u036F]", ""); - } + String repositoryUUID = getRepositoryUUID(); if (repositoryRoot == null) { repositoryRoot = address.substring(0, address.lastIndexOf('/')); @@ -124,80 +108,17 @@ public class ViewImageAction extends ScmWebEditorMainAction { repositoryRoot = address.substring(0, address.lastIndexOf('/')); } - if (log.isDebugEnabled()) { - log.debug("Login : " + username); - } - - - /* - * Reading the cookie - */ - - - String usernamepwCookie = null; - // read the cookies - - BlowfishCipherService bf = new BlowfishCipherService(); - - byte[] privateKey = Base64.decode(ScmWebEditorConfig.getKey()); - - if (request.getCookies() != null) { - for (Cookie c : request.getCookies()) { - if (c.getName().equals(repositoryUUID)) { - usernamepwCookie = c.getValue(); - } - } - } - - if (usernamepwCookie != null) { - String usernameDecode = null; - try { - usernameDecode = new String(bf.decrypt(Base64.decode(usernamepwCookie), privateKey).getBytes(), "UTF-8"); - } catch (UnsupportedEncodingException e) { - if (log.isErrorEnabled()) { - log.error("Can not create a String with UTF-8 encoding"); - } - } - - if (usernameDecode != null) { - String[] resCookie = usernameDecode.split(","); - if (resCookie.length == 2) { - username = resCookie[0]; - pw = resCookie[1]; - } - } - } - - if (saveCookie) { - if (username != null && pw != null) { - - if (!username.equals("") && !pw.equals("")) { - - Cookie authCookie = null; - - try { - authCookie = new Cookie(repositoryUUID, bf.encrypt((username + "," + pw).getBytes("UTF-8"), privateKey).toBase64()); - } catch (UnsupportedEncodingException e) { - if (log.isErrorEnabled()) { - log.error("Can not get a String from UTF-8 encoding"); - } - } - - if (authCookie != null) { - authCookie.setMaxAge(60 * 60 * 24 * 365); - response.addCookie(authCookie); - } - } - } + // authentication + Optional<UsernamePasswordToken> optionalAuthentication = initAuthenticationInformations(repositoryUUID, true, saveCookie); + boolean withAuthentication = optionalAuthentication.isPresent(); + String username = withAuthentication ? optionalAuthentication.get().getUsername() : null; + String pw = withAuthentication ? new String(optionalAuthentication.get().getPassword()) : null; + if (log.isDebugEnabled()) { + log.debug("Login : " + username + " to " + repositoryUUID); } - // authentication - String[] usernamePw = getUsernamePwFromSession(repositoryUUID, username, pw); - username = usernamePw[0]; - pw = usernamePw[1]; - String name = username; String password = pw; @@ -233,6 +154,8 @@ public class ViewImageAction extends ScmWebEditorMainAction { * Getting the file's revision */ + ScmConnection scmConn = provider.getConnection(address, pathToLocalRepos); + try { numRevision = scmConn.getHeadRevisionNumber(address, name, password); } catch (AuthenticationException e) { @@ -243,18 +166,8 @@ public class ViewImageAction extends ScmWebEditorMainAction { log.debug("Auth Fail ", e); } - // deleting the cookies for this repository - for (Cookie c : request.getCookies()) { - if (c.getName().equals(repositoryUUID)) { - c.setMaxAge(0);//On supprime le cookie - response.addCookie(c); - if (log.isDebugEnabled()) { - log.debug("Cookie supprimé"); - } - } - } + removeRepositoryFromCache(repositoryUUID); - getScmSession().delScmUser(repositoryUUID); //redirect to a login page return LOGIN; } catch (IllegalArgumentException e) { @@ -266,7 +179,6 @@ public class ViewImageAction extends ScmWebEditorMainAction { imagePath = scmConn.getFilePath(address, repositoryRoot, username, pw); - if (log.isInfoEnabled()) { log.info("IP client : " + request.getRemoteAddr() + " , get file : " + address + ". File's mimetype : " + mimeType); @@ -274,4 +186,5 @@ public class ViewImageAction extends ScmWebEditorMainAction { return VIEW_IMAGE; } + } diff --git a/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/ViewRevisionAction.java b/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/ViewRevisionAction.java index 0f4bf55..1c19d6b 100644 --- a/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/ViewRevisionAction.java +++ b/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/ViewRevisionAction.java @@ -24,19 +24,13 @@ package org.nuiton.scmwebeditor.uiweb.actions; import org.apache.commons.io.FileUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.apache.shiro.codec.Base64; -import org.apache.shiro.crypto.BlowfishCipherService; +import org.apache.shiro.authc.UsernamePasswordToken; import org.nuiton.scmwebeditor.api.ScmConnection; -import org.nuiton.scmwebeditor.api.ScmProvider; -import org.nuiton.scmwebeditor.uiweb.ScmWebEditorConfig; import javax.naming.AuthenticationException; -import javax.servlet.http.Cookie; -import javax.servlet.http.HttpSession; import java.io.File; import java.io.IOException; -import java.io.UnsupportedEncodingException; -import java.text.Normalizer; +import java.util.Optional; /** * Allows to view a file at the given revision @@ -81,90 +75,25 @@ public class ViewRevisionAction extends ScmWebEditorMainAction { public void setError(String error) { this.error = error; } - /** - * Execution of the view history action - * @return a code interpreted in the file struts.xml - */ + @Override public String execute() { - HttpSession session = request.getSession(); - String sessionId = session.getId(); - error = null; - String pathToLocalRepos = ScmWebEditorConfig.getLocalRepositoriesPath() + File.separator + sessionId; - - ScmProvider provider = ScmWebEditorConfig.getProvider(scmType); - ScmConnection scmConn = provider.getConnection(address, pathToLocalRepos); + ScmConnection scmConn = getScmConnection(); // if the repository is not protected, we get its UUID - String repositoryUUID = scmConn.getRepositoryId(); - if (repositoryUUID == null) { - repositoryUUID = address.replace(' ', '_'); - repositoryUUID = Normalizer.normalize(repositoryUUID, Normalizer.Form.NFD).replaceAll("[\u0300-\u036F]", ""); - } - - if (log.isDebugEnabled()) { - log.debug("Login : " + username); - } - - - /* - * Reading the cookie - */ - - - String usernamepwCookie = null; - // read the cookies - - BlowfishCipherService bf = new BlowfishCipherService(); - - byte[] privateKey = Base64.decode(ScmWebEditorConfig.getKey()); + String repositoryUUID = getRepositoryUUID(); - if (request.getCookies() != null) { - for (Cookie c : request.getCookies()) { - if (c.getName().equals(repositoryUUID)) { - usernamepwCookie = c.getValue(); - } - } - } - - if (usernamepwCookie != null) { - - String usernameDecode = null; - try { - usernameDecode = new String(bf.decrypt(Base64.decode(usernamepwCookie), privateKey).getBytes(), "UTF-8"); - } catch (UnsupportedEncodingException e) { - if (log.isErrorEnabled()) { - log.error("Can not create a String with UTF-8 encoding"); - } - } - - if (usernameDecode != null) { - String[] resCookie = usernameDecode.split(","); - if (resCookie.length == 2) { - username = resCookie[0]; - pw = resCookie[1]; - } - } - } - - if (saveCookie) { - if (username != null && pw != null) { - - if (!username.equals("") && !pw.equals("")) { - Cookie authCookie = new Cookie(repositoryUUID, bf.encrypt((username + "," + pw).getBytes(), privateKey).toBase64()); - authCookie.setMaxAge(60 * 60 * 24 * 365); - response.addCookie(authCookie); - } - } + Optional<UsernamePasswordToken> optionalAuthentication = initAuthenticationInformations(repositoryUUID, true, saveCookie); + boolean withAuthentication = optionalAuthentication.isPresent(); + String username = withAuthentication ? optionalAuthentication.get().getUsername() : null; + String pw = withAuthentication ? new String(optionalAuthentication.get().getPassword()) : null; + if (log.isDebugEnabled()) { + log.debug("Login : " + username + " to repository: " + repositoryUUID); } - // authentication - String[] usernamePw = getUsernamePwFromSession(repositoryUUID, username, pw); - username = usernamePw[0]; - pw = usernamePw[1]; String name = username; String password = pw; @@ -181,6 +110,7 @@ public class ViewRevisionAction extends ScmWebEditorMainAction { * Getting the file's revision */ + String address = getAddress(); try { String revision = revision1; @@ -204,18 +134,8 @@ public class ViewRevisionAction extends ScmWebEditorMainAction { log.debug("Auth Fail ", e); } - // deleting the cookies for this repository - for (Cookie c : request.getCookies()) { - if (c.getName().equals(repositoryUUID)) { - c.setMaxAge(0);//On supprime le cookie - response.addCookie(c); - if (log.isDebugEnabled()) { - log.debug("Cookie supprimé"); - } - } - } + removeRepositoryFromCache(repositoryUUID); - getScmSession().delScmUser(repositoryUUID); error = LOGIN; return LOGIN; } catch (IOException e) { @@ -228,4 +148,5 @@ public class ViewRevisionAction extends ScmWebEditorMainAction { return SUCCESS; } + } -- To stop receiving notification emails like this one, please contact nuiton.org SCM administrator <admin+scm@nuiton.org>.
This is an automated email from the git hooks/post-receive script. New commit to branch feature/3879 in repository scmwebeditor. See http://git.nuiton.org/scmwebeditor.git commit 3866ee7bd7f9c5319f885a6db451acdad2d4e2e5 Author: Tony CHEMIT <chemit@codelutin.com> Date: Sun Feb 14 19:28:32 2016 +0100 Introduction de ScmAuthentication et utilisation de le module API --- swe-scm-api/pom.xml | 8 ++ .../nuiton/scmwebeditor/api/ScmAuthentication.java | 41 ++++++++++ .../org/nuiton/scmwebeditor/api/ScmConnection.java | 92 ++++++++++------------ .../nuiton/scmwebeditor/api/ScmFileManager.java | 27 +++++-- .../org/nuiton/scmwebeditor/api/ScmProvider.java | 57 ++++++++------ .../org/nuiton/scmwebeditor/api/dto/BrowseDto.java | 31 +------- .../org/nuiton/scmwebeditor/api/dto/CommitDto.java | 35 ++------ .../scmwebeditor/api/dto/CreateBranchDto.java | 17 +--- .../scmwebeditor/api/dto/CreateDirectoryDto.java | 29 ++----- .../nuiton/scmwebeditor/api/dto/MoveFileDto.java | 25 +----- .../scmwebeditor/api/dto/RemoveDirectoryDto.java | 29 ++----- .../nuiton/scmwebeditor/api/dto/RemoveFileDto.java | 29 ++----- .../scmwebeditor/api/dto/RequestSupport.java | 28 +++++++ .../nuiton/scmwebeditor/api/dto/UploadFileDto.java | 25 +----- 14 files changed, 200 insertions(+), 273 deletions(-) diff --git a/swe-scm-api/pom.xml b/swe-scm-api/pom.xml index e87646e..6deb964 100644 --- a/swe-scm-api/pom.xml +++ b/swe-scm-api/pom.xml @@ -12,4 +12,12 @@ <name>ScmWebEditor SCM API</name> <description>This is the core module of ScmWebEditor.</description> + + <dependencies> + <dependency> + <groupId>com.google.guava</groupId> + <artifactId>guava</artifactId> + </dependency> + </dependencies> + </project> diff --git a/swe-scm-api/src/main/java/org/nuiton/scmwebeditor/api/ScmAuthentication.java b/swe-scm-api/src/main/java/org/nuiton/scmwebeditor/api/ScmAuthentication.java new file mode 100644 index 0000000..cafed09 --- /dev/null +++ b/swe-scm-api/src/main/java/org/nuiton/scmwebeditor/api/ScmAuthentication.java @@ -0,0 +1,41 @@ +package org.nuiton.scmwebeditor.api; + +import com.google.common.base.MoreObjects; + +/** + * A simple bean to contains login-password. + * + * Created on 14/02/16. + * + * @author Tony Chemit - chemit@codelutin.com + * @since 0.8 + */ +public class ScmAuthentication { + + public static final ScmAuthentication ANONYMOUS = new ScmAuthentication("anonymous", "anonymous".toCharArray()); + + protected final String username; + + protected final char[] password; + + public ScmAuthentication(String username, char[] password) { + this.username = username; + this.password = password; + } + + public String getUsername() { + return username; + } + + public char[] getPassword() { + return password; + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(this) + .add("username", username) + .add("password", "***") + .toString(); + } +} 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 ec86c13..ce322c1 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 @@ -29,6 +29,7 @@ import org.nuiton.scmwebeditor.api.dto.result.CommitResultDto; import javax.naming.AuthenticationException; import java.io.File; import java.util.Map; +import java.util.Optional; /** * An interface which gives the SCMs main features @@ -36,102 +37,95 @@ import java.util.Map; public interface ScmConnection { /** - * Searches the repository's files to make a list of them + * 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 + * 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); - /** - * Gives the content of a file - * @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 + * Gives the content of a file. + * + * @param path the path to the file to get the content from + * @param optionalScmAuthentication user's authentication informations * @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; - + File getFileContent(String path, Optional<ScmAuthentication> optionalScmAuthentication) 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 + * Gives the number of the head revision. + * + * @param path the path to the SCM + * @param optionalScmAuthentication user authentication informations * @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; - + String getHeadRevisionNumber(String path, Optional<ScmAuthentication> optionalScmAuthentication) throws AuthenticationException; /** - * Gives the repository's unique identifier + * Gives the repository's unique identifier. + * * @return the repository's unique identifier */ String getRepositoryId(); - /** - * Gives the name of the edited file + * Gives the name of the edited file. + * * @return the name of the edited file */ String getFileName(); - /** - * Gives the path to use to get a file from the repository - * @param address the full address of the file on the repository - * @param repositoryRoot the address of the repository's root - * @param username the user's login for the SCM - * @param password the user's password for the SCM + * Gives the path to use to get a file from the repository. + * + * @param address the full address of the file on the repository + * @param repositoryRoot the address of the repository's root + * @param optionalScmAuthentication user authentication informations * @return the path to use to get a file from the repository */ - String getFilePath(String address, String repositoryRoot, String username, String password); - + String getFilePath(String address, String repositoryRoot, Optional<ScmAuthentication> optionalScmAuthentication); /** - * Gives a list of the important revisions for the file at the given address - * @param address the file's address - * @param username the username to use to connect to the repository - * @param password the password to use to connect to the repository + * Gives a list of the important revisions for the file at the given address. + * + * @param address the file's address + * @param optionalScmAuthentication user authentication informations (may be null if no authentication is given) * @return a list of revisions for the file at the given address with their revision name * @throws AuthenticationException if there is a problem during the authentication process */ - Map<ScmRevision, String> getRevisions(String address, String username, String password) throws AuthenticationException; - + Map<ScmRevision, String> getRevisions(String address, Optional<ScmAuthentication> optionalScmAuthentication) throws AuthenticationException; /** - * Gives the content of a file at the specified revision - * @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 - * @param revision the revision ID to use to get the file content + * Gives the content of a file at the specified revision. + * + * @param path the path to the file to get the content from + * @param optionalScmAuthentication user authentication informations + * @param revision the revision ID to use to get the file content * @return a String which contains the file's content * @throws AuthenticationException if there is a problem during the authentication process */ - File getFileContentAtRevision(String path, String username, String password, String revision) - throws AuthenticationException; - + File getFileContentAtRevision(String path, Optional<ScmAuthentication> optionalScmAuthentication, String revision) throws AuthenticationException; /** - * Gives the content of a file at the specified revision - * @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 - * @param revision1 one of the revisions ID to compare - * @param revision2 the other revision ID to compare + * Gives the content of a file at the specified revision. + * + * @param path the path to the file to get the content from + * @param optionalScmAuthentication user authentication informations + * @param revision1 one of the revisions ID to compare + * @param revision2 the other revision ID to compare * @return a String which contains the file's content * @throws AuthenticationException if there is a problem during the authentication process */ - File getDiffs(String path, String username, String password, String revision1,String revision2) - throws AuthenticationException; + File getDiffs(String path, Optional<ScmAuthentication> optionalScmAuthentication, String revision1, String revision2) throws AuthenticationException; } diff --git a/swe-scm-api/src/main/java/org/nuiton/scmwebeditor/api/ScmFileManager.java b/swe-scm-api/src/main/java/org/nuiton/scmwebeditor/api/ScmFileManager.java index 7a5583d..f96bb7e 100644 --- a/swe-scm-api/src/main/java/org/nuiton/scmwebeditor/api/ScmFileManager.java +++ b/swe-scm-api/src/main/java/org/nuiton/scmwebeditor/api/ScmFileManager.java @@ -21,8 +21,16 @@ */ package org.nuiton.scmwebeditor.api; -import org.nuiton.scmwebeditor.api.dto.*; -import org.nuiton.scmwebeditor.api.dto.result.*; +import org.nuiton.scmwebeditor.api.dto.CreateDirectoryDto; +import org.nuiton.scmwebeditor.api.dto.MoveFileDto; +import org.nuiton.scmwebeditor.api.dto.RemoveDirectoryDto; +import org.nuiton.scmwebeditor.api.dto.RemoveFileDto; +import org.nuiton.scmwebeditor.api.dto.UploadFileDto; +import org.nuiton.scmwebeditor.api.dto.result.CreateDirectoryResultDto; +import org.nuiton.scmwebeditor.api.dto.result.MoveFileResultDto; +import org.nuiton.scmwebeditor.api.dto.result.RemoveDirectoryResultDto; +import org.nuiton.scmwebeditor.api.dto.result.RemoveFileResultDto; +import org.nuiton.scmwebeditor.api.dto.result.UploadFileResultDto; /** * An interface which gives the SCMs features relative to file management @@ -30,7 +38,8 @@ import org.nuiton.scmwebeditor.api.dto.result.*; public interface ScmFileManager { /** - * Uploads a file to the repository as a new file + * 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 */ @@ -38,7 +47,8 @@ public interface ScmFileManager { /** - * Removes a file from the repository + * Removes a file from the repository. + * * @param dto the DTO which contains all the parameters * @return a DTO which contains all the results */ @@ -46,7 +56,8 @@ public interface ScmFileManager { /** - * Creates a new directory on the repository + * Creates a new directory on the repository. + * * @param dto the DTO which contains all the parameters * @return a DTO which contains all the results */ @@ -54,7 +65,8 @@ public interface ScmFileManager { /** - * Removes a directory from the repository + * Removes a directory from the repository. + * * @param dto the DTO which contains all the parameters * @return a DTO which contains all the results */ @@ -62,7 +74,8 @@ public interface ScmFileManager { /** - * Moves a file to a directory in the repository + * 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 */ 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 988128e..8105ffb 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 @@ -25,6 +25,7 @@ import org.nuiton.scmwebeditor.api.dto.CreateBranchDto; import javax.naming.AuthenticationException; import java.util.List; +import java.util.Optional; /** * An interface which allows to know and to use the specific features of each SCM @@ -32,80 +33,86 @@ import java.util.List; public interface ScmProvider { /** - * Tells whether the SCM supports branches + * Tells whether the SCM supports branches. + * * @return true if the SCM supports branches */ boolean supportsBranches(); /** - * Makes a list of the repository's branches - * @param address the repository's address - * @param username the username used to connect to the SCM - * @param password the password used to connect to the SCM + * Makes a list of the repository's branches. + * + * @param address the repository's address + * @param optionalScmAuthentication user's authentication informations * @return a list of the repository's branches * @throws OperationNotSupportedException if the SCM doesn't support branches */ - List<String> listBranches(String address, String username, String password) throws OperationNotSupportedException; + List<String> listBranches(String address, Optional<ScmAuthentication> optionalScmAuthentication) throws OperationNotSupportedException; /** - * Allows to create a new branch on the repository + * Allows to create a new branch on the repository. + * * @param dto the DTO which contains all the parameters * @return an error code or null if there was no error during the process * @throws OperationNotSupportedException if the SCM doesn't support branches - * @throws AuthenticationException if there is an authentication problem - * @throws RepositoryNotFoundException if the repository could not be reached + * @throws AuthenticationException if there is an authentication problem + * @throws RepositoryNotFoundException if the repository could not be reached */ - String createBranch(CreateBranchDto dto) throws OperationNotSupportedException, - AuthenticationException, RepositoryNotFoundException; + String createBranch(CreateBranchDto dto) throws OperationNotSupportedException, AuthenticationException, RepositoryNotFoundException; /** - * Changes the working branch for the local repository - * @param branchName the new working branch - * @param pathToLocalRepos the path to the directory where the local repositories are stored - * @param username the username to use to connect to the repository - * @param password the password to use to connect to the repository + * Changes the working branch for the local repository. + * + * @param branchName the new working branch + * @param pathToLocalRepos the path to the directory where the local repositories are stored + * @param optionalScmAuthentication user's authentication informations * @return an error code or null if there was no error during the process * @throws OperationNotSupportedException if the SCM doesn't support branches */ - String changeBranch(String branchName, String pathToLocalRepos, String username, String password) - throws OperationNotSupportedException; + String changeBranch(String branchName, String pathToLocalRepos, Optional<ScmAuthentication> optionalScmAuthentication) throws OperationNotSupportedException; /** - * Tells whether the SCM allows to choose when the commits are pushed to the server + * Tells whether the SCM allows to choose when the commits are pushed to the server. + * * @return true if the SCM supports a command to push the commits to the server */ boolean supportsPush(); /** - * Tells whether the repositories' files are directly accessible by their address + * Tells whether the repositories' files are directly accessible by their address. + * * @return true if the files are directly accessible */ boolean filesDirectlyAccessible(); /** - * Gives the connection to the SCM - * @param address the repository's address + * Gives the connection to the SCM. + * + * @param address the repository's address * @param pathToLocalRepos the folder which will be used to store one client's repositories * @return the connection to the SCM */ ScmConnection getConnection(String address, String pathToLocalRepos); /** - * Gives the file manager for the given connection + * 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 + * 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 */ boolean addressSeemsCompatible(String address); /** - * Gives the name of the default branch if the SCM supports branches + * Gives the name of the default branch if the SCM supports branches. + * * @return the name of the default branch to use * @throws OperationNotSupportedException if the SCM doesn't support branches */ diff --git a/swe-scm-api/src/main/java/org/nuiton/scmwebeditor/api/dto/BrowseDto.java b/swe-scm-api/src/main/java/org/nuiton/scmwebeditor/api/dto/BrowseDto.java index ac9a208..b161080 100644 --- a/swe-scm-api/src/main/java/org/nuiton/scmwebeditor/api/dto/BrowseDto.java +++ b/swe-scm-api/src/main/java/org/nuiton/scmwebeditor/api/dto/BrowseDto.java @@ -23,40 +23,17 @@ package org.nuiton.scmwebeditor.api.dto; */ -public class BrowseDto { +public class BrowseDto extends RequestSupport { - /** the username used to connect to the SCM */ - protected String username; - - /** the password used to connect to the SCM */ - protected String password; - - /** the selected branch on the SCM */ + /** Selected branch on the SCM. */ protected String selectedBranch; - /** the SCM's identifier */ + /** SCM's identifier. */ protected String id; - /** the SCM's adddress */ + /** SCM's adddress. */ 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 getSelectedBranch() { return selectedBranch; } diff --git a/swe-scm-api/src/main/java/org/nuiton/scmwebeditor/api/dto/CommitDto.java b/swe-scm-api/src/main/java/org/nuiton/scmwebeditor/api/dto/CommitDto.java index 82d64da..8451b33 100644 --- a/swe-scm-api/src/main/java/org/nuiton/scmwebeditor/api/dto/CommitDto.java +++ b/swe-scm-api/src/main/java/org/nuiton/scmwebeditor/api/dto/CommitDto.java @@ -22,46 +22,23 @@ package org.nuiton.scmwebeditor.api.dto; * #L% */ -public class CommitDto { +public class CommitDto extends RequestSupport{ - /** 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 */ + /** New text to insert. */ protected String newText; - /** the message to describe the commit */ + /** Message to describe the commit. */ protected String commitMessage; - /** set to true for a commit even if there is no difference with the previous version */ + /** 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 */ + /** File to commit's address. */ protected String address; - /** set to true for a commit without a push */ + /** Set to true for a commit without a push. */ private boolean commitOnly; - - 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; } diff --git a/swe-scm-api/src/main/java/org/nuiton/scmwebeditor/api/dto/CreateBranchDto.java b/swe-scm-api/src/main/java/org/nuiton/scmwebeditor/api/dto/CreateBranchDto.java index ffd4a2b..3ae9ec4 100644 --- a/swe-scm-api/src/main/java/org/nuiton/scmwebeditor/api/dto/CreateBranchDto.java +++ b/swe-scm-api/src/main/java/org/nuiton/scmwebeditor/api/dto/CreateBranchDto.java @@ -21,7 +21,7 @@ */ package org.nuiton.scmwebeditor.api.dto; -public class CreateBranchDto { +public class CreateBranchDto extends RequestSupport{ /** the repository's address */ protected String address; @@ -32,16 +32,9 @@ public class CreateBranchDto { /** the name of the origin branch for the new one */ protected String selectedBranch; - /** the username to use to connect to the repository */ - protected String username; - - /** the password to use to connect to the repository */ - protected String password; - /** the folder which is used to store the repositories */ protected String pathToLocalRepos; - public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @@ -54,14 +47,6 @@ public class CreateBranchDto { public void setSelectedBranch(String selectedBranch) { this.selectedBranch = selectedBranch; } - 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 getPathToLocalRepos() { return pathToLocalRepos; } public void setPathToLocalRepos(String pathToLocalRepos) { this.pathToLocalRepos = pathToLocalRepos; } diff --git a/swe-scm-api/src/main/java/org/nuiton/scmwebeditor/api/dto/CreateDirectoryDto.java b/swe-scm-api/src/main/java/org/nuiton/scmwebeditor/api/dto/CreateDirectoryDto.java index 9f35f07..e2fec5e 100644 --- a/swe-scm-api/src/main/java/org/nuiton/scmwebeditor/api/dto/CreateDirectoryDto.java +++ b/swe-scm-api/src/main/java/org/nuiton/scmwebeditor/api/dto/CreateDirectoryDto.java @@ -22,13 +22,7 @@ package org.nuiton.scmwebeditor.api.dto; * #L% */ -public class CreateDirectoryDto { - - /** the username used to connect to the SCM */ - protected String username; - - /** the password used to connect to the SCM */ - protected String password; +public class CreateDirectoryDto extends RequestSupport{ /** the name of the directory to create */ protected String directoryName; @@ -36,27 +30,14 @@ public class CreateDirectoryDto { /** path to the repository */ protected String parentDirectory; - - public String getUsername() { - return username; - } - - public void setUsername(String username) { - this.username = username; + public String getDirectoryName() { + return directoryName; } - public String getPassword() { - return password; + public void setDirectoryName(String directoryName) { + this.directoryName = directoryName; } - public void setPassword(String password) { - this.password = password; - } - - public String getDirectoryName() { return directoryName; } - - public void setDirectoryName(String directoryName) { this.directoryName = directoryName; } - public String getParentDirectory() { return parentDirectory; } diff --git a/swe-scm-api/src/main/java/org/nuiton/scmwebeditor/api/dto/MoveFileDto.java b/swe-scm-api/src/main/java/org/nuiton/scmwebeditor/api/dto/MoveFileDto.java index f78d165..1ac2957 100644 --- a/swe-scm-api/src/main/java/org/nuiton/scmwebeditor/api/dto/MoveFileDto.java +++ b/swe-scm-api/src/main/java/org/nuiton/scmwebeditor/api/dto/MoveFileDto.java @@ -21,13 +21,7 @@ */ package org.nuiton.scmwebeditor.api.dto; -public class MoveFileDto { - - /** the username used to connect to the SCM */ - protected String username; - - /** the password used to connect to the SCM */ - protected String password; +public class MoveFileDto extends RequestSupport{ /** path to the repository */ protected String scmPath; @@ -38,23 +32,6 @@ public class MoveFileDto { /** path to the destination directory */ protected String destinationDirectory; - - 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 getScmPath() { return scmPath; } public void setScmPath(String scmPath) { this.scmPath = scmPath; } diff --git a/swe-scm-api/src/main/java/org/nuiton/scmwebeditor/api/dto/RemoveDirectoryDto.java b/swe-scm-api/src/main/java/org/nuiton/scmwebeditor/api/dto/RemoveDirectoryDto.java index a2d0844..16ebd8b 100644 --- a/swe-scm-api/src/main/java/org/nuiton/scmwebeditor/api/dto/RemoveDirectoryDto.java +++ b/swe-scm-api/src/main/java/org/nuiton/scmwebeditor/api/dto/RemoveDirectoryDto.java @@ -22,35 +22,16 @@ package org.nuiton.scmwebeditor.api.dto; * #L% */ -public class RemoveDirectoryDto { - - /** the username used to connect to the SCM */ - protected String username; - - /** the password used to connect to the SCM */ - protected String password; +public class RemoveDirectoryDto extends RequestSupport{ /** the path to the directory to remove */ protected String directoryToRemove; - - public String getUsername() { - return username; - } - - public void setUsername(String username) { - this.username = username; + public String getDirectoryToRemove() { + return directoryToRemove; } - public String getPassword() { - return password; + public void setDirectoryToRemove(String directoryToRemove) { + this.directoryToRemove = directoryToRemove; } - - public void setPassword(String password) { - this.password = password; - } - - public String getDirectoryToRemove() { return directoryToRemove; } - - public void setDirectoryToRemove(String directoryToRemove) { this.directoryToRemove = directoryToRemove; } } diff --git a/swe-scm-api/src/main/java/org/nuiton/scmwebeditor/api/dto/RemoveFileDto.java b/swe-scm-api/src/main/java/org/nuiton/scmwebeditor/api/dto/RemoveFileDto.java index 16697bc..b223dd9 100644 --- a/swe-scm-api/src/main/java/org/nuiton/scmwebeditor/api/dto/RemoveFileDto.java +++ b/swe-scm-api/src/main/java/org/nuiton/scmwebeditor/api/dto/RemoveFileDto.java @@ -21,35 +21,16 @@ */ package org.nuiton.scmwebeditor.api.dto; -public class RemoveFileDto { - - /** the username used to connect to the SCM */ - protected String username; - - /** the password used to connect to the SCM */ - protected String password; +public class RemoveFileDto extends RequestSupport{ /** path to the repository */ protected String scmPath; - - public String getUsername() { - return username; - } - - public void setUsername(String username) { - this.username = username; + public String getScmPath() { + return scmPath; } - public String getPassword() { - return password; + public void setScmPath(String scmPath) { + this.scmPath = scmPath; } - - public void setPassword(String password) { - this.password = password; - } - - public String getScmPath() { return scmPath; } - - public void setScmPath(String scmPath) { this.scmPath = scmPath; } } diff --git a/swe-scm-api/src/main/java/org/nuiton/scmwebeditor/api/dto/RequestSupport.java b/swe-scm-api/src/main/java/org/nuiton/scmwebeditor/api/dto/RequestSupport.java new file mode 100644 index 0000000..00d1ece --- /dev/null +++ b/swe-scm-api/src/main/java/org/nuiton/scmwebeditor/api/dto/RequestSupport.java @@ -0,0 +1,28 @@ +package org.nuiton.scmwebeditor.api.dto; + +import org.nuiton.scmwebeditor.api.ScmAuthentication; + +import java.util.Optional; + +/** + * Created on 14/02/16. + * + * @author Tony Chemit - chemit@codelutin.com + */ +public abstract class RequestSupport { + + /** user's authentication informations (or anonymous). */ + protected ScmAuthentication scmAuthentication; + + public final ScmAuthentication getScmAuthentication() { + return scmAuthentication; + } + + public final void setScmAuthentication(Optional<ScmAuthentication> optionalScmAuthentication) { + this.scmAuthentication = optionalScmAuthentication.orElse(ScmAuthentication.ANONYMOUS); + } + + public final String getUsername() { + return scmAuthentication.getUsername(); + } +} diff --git a/swe-scm-api/src/main/java/org/nuiton/scmwebeditor/api/dto/UploadFileDto.java b/swe-scm-api/src/main/java/org/nuiton/scmwebeditor/api/dto/UploadFileDto.java index 28abf07..500f7a7 100644 --- a/swe-scm-api/src/main/java/org/nuiton/scmwebeditor/api/dto/UploadFileDto.java +++ b/swe-scm-api/src/main/java/org/nuiton/scmwebeditor/api/dto/UploadFileDto.java @@ -24,13 +24,7 @@ package org.nuiton.scmwebeditor.api.dto; import java.io.File; -public class UploadFileDto { - - /** the username used to connect to the SCM */ - protected String username; - - /** the password used to connect to the SCM */ - protected String password; +public class UploadFileDto extends RequestSupport{ /** the file to upload */ protected File upload; @@ -44,23 +38,6 @@ public class UploadFileDto { /** path to the repository */ protected String scmPath; - - 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 File getUpload() { return upload; } -- To stop receiving notification emails like this one, please contact nuiton.org SCM administrator <admin+scm@nuiton.org>.
This is an automated email from the git hooks/post-receive script. New commit to branch feature/3879 in repository scmwebeditor. See http://git.nuiton.org/scmwebeditor.git commit 33603cba14c5a237d8c5aca379b2770c76669b0e Author: Tony CHEMIT <chemit@codelutin.com> Date: Sun Feb 14 19:28:49 2016 +0100 Application dans le module svn --- .../org/nuiton/scmwebeditor/svn/SvnConnection.java | 214 ++++++++++++--------- .../nuiton/scmwebeditor/svn/SvnFileManager.java | 76 +++----- .../org/nuiton/scmwebeditor/svn/SvnProvider.java | 29 ++- .../java/org/nuiton/scmwebeditor/svn/SvnUtil.java | 18 ++ 4 files changed, 173 insertions(+), 164 deletions(-) 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 b2a8451..252e59c 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 @@ -24,6 +24,7 @@ package org.nuiton.scmwebeditor.svn; import org.apache.commons.io.FileUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.nuiton.scmwebeditor.api.ScmAuthentication; import org.nuiton.scmwebeditor.api.ScmConnection; import org.nuiton.scmwebeditor.api.ScmRevision; import org.nuiton.scmwebeditor.api.dto.BrowseDto; @@ -31,7 +32,15 @@ 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.SVNAuthenticationException; +import org.tmatesoft.svn.core.SVNDepth; +import org.tmatesoft.svn.core.SVNDirEntry; +import org.tmatesoft.svn.core.SVNException; +import org.tmatesoft.svn.core.SVNNodeKind; +import org.tmatesoft.svn.core.SVNProperties; +import org.tmatesoft.svn.core.SVNProperty; +import org.tmatesoft.svn.core.SVNRevisionProperty; +import org.tmatesoft.svn.core.SVNURL; import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager; import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory; import org.tmatesoft.svn.core.internal.util.SVNEncodingUtil; @@ -39,16 +48,30 @@ import org.tmatesoft.svn.core.internal.wc.DefaultSVNOptions; import org.tmatesoft.svn.core.io.SVNFileRevision; import org.tmatesoft.svn.core.io.SVNRepository; import org.tmatesoft.svn.core.io.SVNRepositoryFactory; -import org.tmatesoft.svn.core.wc.*; +import org.tmatesoft.svn.core.wc.SVNClientManager; +import org.tmatesoft.svn.core.wc.SVNCommitClient; +import org.tmatesoft.svn.core.wc.SVNDiffClient; +import org.tmatesoft.svn.core.wc.SVNInfo; +import org.tmatesoft.svn.core.wc.SVNRevision; +import org.tmatesoft.svn.core.wc.SVNUpdateClient; +import org.tmatesoft.svn.core.wc.SVNWCClient; +import org.tmatesoft.svn.core.wc.SVNWCUtil; import javax.naming.AuthenticationException; -import java.io.*; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.io.Writer; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Collection; import java.util.Date; import java.util.Map; +import java.util.Optional; import java.util.TreeMap; /** @@ -87,24 +110,34 @@ public class SvnConnection implements ScmConnection { protected SVNClientManager manager; - public String getSvnPath() { return svnPath; } + public String getSvnPath() { + return svnPath; + } - public DefaultSVNOptions getSvnOption() { return svnOption; } + public DefaultSVNOptions getSvnOption() { + return svnOption; + } - public String getPathToLocalRepos() { return pathToLocalRepos; } + public String getPathToLocalRepos() { + return pathToLocalRepos; + } - public void setPathToLocalRepos(String pathToLocalRepos) { this.pathToLocalRepos = pathToLocalRepos; } + public void setPathToLocalRepos(String pathToLocalRepos) { + this.pathToLocalRepos = pathToLocalRepos; + } - public SVNClientManager getManager() { return manager; } + public SVNClientManager getManager() { + return manager; + } public SvnConnection(String address, String pathToLocalRepos) throws SVNException { - if(log.isDebugEnabled()) { + if (log.isDebugEnabled()) { log.debug("SVN repository"); } - if (address.lastIndexOf('/') != -1){ + if (address.lastIndexOf('/') != -1) { svnPath = address.substring(0, address.lastIndexOf('/')); fileName = address.substring(address.lastIndexOf('/') + 1); @@ -134,44 +167,44 @@ public class SvnConnection implements ScmConnection { SVNRepository repository; ISVNAuthenticationManager svnAuthManager; - String name = dto.getUsername(); - String password = dto.getPassword(); + switch (dto.getId()) { + case "": - if (dto.getId().equals("")) { - - try { - if (log.isDebugEnabled()) { - log.debug("Address svn : " + dto.getAddress()); - } + try { + if (log.isDebugEnabled()) { + log.debug("Address svn : " + dto.getAddress()); + } - String encodedUrl = SVNEncodingUtil.autoURIEncode(addressSvn); - repository = SVNRepositoryFactory.create(SVNURL.parseURIEncoded(encodedUrl)); - svnAuthManager = SVNWCUtil.createDefaultAuthenticationManager(name, password); - repository.setAuthenticationManager(svnAuthManager); + String encodedUrl = SVNEncodingUtil.autoURIEncode(addressSvn); + repository = SVNRepositoryFactory.create(SVNURL.parseURIEncoded(encodedUrl)); + svnAuthManager = SvnUtil.createAuthenticationManager(dto.getScmAuthentication()); + repository.setAuthenticationManager(svnAuthManager); - repository.testConnection(); + repository.testConnection(); - } catch (SVNAuthenticationException e) { - if (log.isErrorEnabled()) { - log.error("Can't access to the repository : Auth Problem", e); - } - resultDto.setError(BrowseResultDto.AUTH_ERROR); - return resultDto; - } catch (SVNException e) { - if (log.isErrorEnabled()) { - log.error("Can't access to the repository", e); + } catch (SVNAuthenticationException e) { + if (log.isErrorEnabled()) { + log.error("Can't access to the repository : Auth Problem", e); + } + resultDto.setError(BrowseResultDto.AUTH_ERROR); + return resultDto; + } catch (SVNException e) { + if (log.isErrorEnabled()) { + log.error("Can't access to the repository", e); + } + resultDto.setError(BrowseResultDto.ERROR); + return resultDto; } - resultDto.setError(BrowseResultDto.ERROR); - return resultDto; - } - resultDto.setError(BrowseResultDto.ROOT); - return resultDto; - } else if (dto.getId().equals("0")) { - url = dto.getAddress(); - } else { - url = dto.getId(); - dto.setAddress(dto.getId()); + resultDto.setError(BrowseResultDto.ROOT); + return resultDto; + case "0": + url = dto.getAddress(); + break; + default: + url = dto.getId(); + dto.setAddress(dto.getId()); + break; } @@ -179,7 +212,7 @@ public class SvnConnection implements ScmConnection { String encodedUrl = SVNEncodingUtil.autoURIEncode(url); repository = SVNRepositoryFactory.create(SVNURL.parseURIEncoded(encodedUrl)); - svnAuthManager = SVNWCUtil.createDefaultAuthenticationManager(name, password); + svnAuthManager = SvnUtil.createAuthenticationManager(dto.getScmAuthentication()); repository.setAuthenticationManager(svnAuthManager); if (log.isDebugEnabled()) { @@ -243,7 +276,7 @@ public class SvnConnection implements ScmConnection { CommitResultDto resultDto = new CommitResultDto(); - updateAuthentication(dto.getUsername(), dto.getPassword()); + updateAuthentication(dto.getScmAuthentication()); if (log.isDebugEnabled()) { log.debug("Entering SVN commit"); @@ -329,7 +362,7 @@ public class SvnConnection implements ScmConnection { resultDto.setDiff(diff.substring(diff.indexOf("@@"))); delTempDirectory(checkoutdir); try { - resultDto.setHeadCommiter(getHeadcommiter(dto.getAddress(), dto.getUsername(), dto.getPassword())); + resultDto.setHeadCommiter(getHeadcommiter(dto.getAddress(), Optional.of(dto.getScmAuthentication()))); } catch (SVNException e) { log.error("Can't get head commiter", e); } @@ -377,7 +410,7 @@ public class SvnConnection implements ScmConnection { log.debug("Try to commit"); } commitClient.doCommit(tabFile, false, "From scmwebeditor -- " + dto.getCommitMessage(), null, null, false, - false, SVNDepth.FILES); + false, SVNDepth.FILES); } catch (SVNAuthenticationException authexep) { if (log.isErrorEnabled()) { log.error("AUTH FAIL", authexep); @@ -389,8 +422,7 @@ public class SvnConnection implements ScmConnection { // deleting temporary directory delTempDirectory(checkoutdir); - dto.setUsername(null); - dto.setPassword(null); + dto.setScmAuthentication(null); resultDto.setError(CommitResultDto.AUTH_ERROR); return resultDto; @@ -418,12 +450,11 @@ public class SvnConnection implements ScmConnection { delTempDirectory(checkoutdir); if (log.isInfoEnabled()) { - log.info(dto.getUsername() + " commit the file " + dto.getAddress() + " with message : " - + dto.getCommitMessage()); + log.info(dto.getScmAuthentication() + " commit the file " + dto.getAddress() + " with message : " + dto.getCommitMessage()); } try { - resultDto.setNumRevision(getHeadRevisionNumber(dto.getAddress(), dto.getUsername(), dto.getPassword())); + resultDto.setNumRevision(getHeadRevisionNumber(dto.getAddress(), Optional.of(dto.getScmAuthentication()))); } catch (AuthenticationException e) { if (log.isErrorEnabled()) { log.error("Auth fail", e); @@ -435,22 +466,20 @@ public class SvnConnection implements ScmConnection { @Override - public File getFileContent(String path, String username, String password) throws AuthenticationException { - - File fileContent = getFileContentAtRevision(path, username, password, "-1"); + public File getFileContent(String path, Optional<ScmAuthentication> optionalScmAuthentication) throws AuthenticationException { - return fileContent; + return getFileContentAtRevision(path, optionalScmAuthentication, "-1"); } @Override - public String getHeadRevisionNumber(String path, String username, String password) throws AuthenticationException { + public String getHeadRevisionNumber(String path, Optional<ScmAuthentication> optionalScmAuthentication) throws AuthenticationException { if (log.isDebugEnabled()) { log.debug("headRevisionNumber expected " + addressSvn + " ; got " + path); } - ISVNAuthenticationManager svnAuthManager = SVNWCUtil.createDefaultAuthenticationManager(username, password); + ISVNAuthenticationManager svnAuthManager = SvnUtil.createAuthenticationManager(optionalScmAuthentication.orElse(ScmAuthentication.ANONYMOUS)); DefaultSVNOptions svnOptions = new DefaultSVNOptions(); svnOptions.setPropertyValue(SVNProperty.EOL_STYLE, SVNProperty.EOL_STYLE_LF); @@ -506,7 +535,7 @@ public class SvnConnection implements ScmConnection { } @Override - public String getFilePath(String address, String repositoryRoot, String username, String password) { + public String getFilePath(String address, String repositoryRoot, Optional<ScmAuthentication> optionalScmAuthentication) { String path = address; @@ -515,7 +544,7 @@ public class SvnConnection implements ScmConnection { File svgFile = null; try { - svgFile = getFileContent(address, username, password); + svgFile = getFileContent(address, optionalScmAuthentication); } catch (AuthenticationException e) { if (log.isErrorEnabled()) { log.error("Can not get file content for address " + address); @@ -531,16 +560,16 @@ public class SvnConnection implements ScmConnection { } @Override - public Map<ScmRevision, String> getRevisions(String address, String username, String password) throws AuthenticationException { + public Map<ScmRevision, String> getRevisions(String address, Optional<ScmAuthentication> optionalScmAuthentication) throws AuthenticationException { final int MAX_MESSAGE_LENGTH = 64; String url = address.substring(0, address.lastIndexOf('/')); String file = address.substring(address.lastIndexOf('/') + 1); - Map<ScmRevision, String> revisions = new TreeMap<ScmRevision, String>(); + Map<ScmRevision, String> revisions = new TreeMap<>(); - updateAuthentication(username, password); + updateAuthentication(optionalScmAuthentication.orElse(ScmAuthentication.ANONYMOUS)); SVNRepository repository; @@ -548,7 +577,7 @@ public class SvnConnection implements ScmConnection { // getting the revisions repository = SVNRepositoryFactory.create(SVNURL.parseURIEncoded(url)); - ISVNAuthenticationManager svnAuthManager = SVNWCUtil.createDefaultAuthenticationManager(username, password); + ISVNAuthenticationManager svnAuthManager = SvnUtil.createAuthenticationManager(optionalScmAuthentication.orElse(ScmAuthentication.ANONYMOUS)); repository.setAuthenticationManager(svnAuthManager); SVNNodeKind nodeKind = repository.checkPath(file, -1); @@ -617,8 +646,7 @@ public class SvnConnection implements ScmConnection { } @Override - public File getFileContentAtRevision(String path, String username, String password, - String revision) throws AuthenticationException { + public File getFileContentAtRevision(String path, Optional<ScmAuthentication> optionalScmAuthentication, String revision) throws AuthenticationException { String url = path.substring(0, path.lastIndexOf('/')); String file = path.substring(path.lastIndexOf('/') + 1); @@ -645,14 +673,14 @@ public class SvnConnection implements ScmConnection { } } - updateAuthentication(username, password); + updateAuthentication(optionalScmAuthentication.orElse(ScmAuthentication.ANONYMOUS)); SVNRepository repository; try { repository = SVNRepositoryFactory.create(SVNURL.parseURIEncoded(url)); - ISVNAuthenticationManager svnAuthManager = SVNWCUtil.createDefaultAuthenticationManager(username, password); + ISVNAuthenticationManager svnAuthManager = SvnUtil.createAuthenticationManager(optionalScmAuthentication.orElse(ScmAuthentication.ANONYMOUS)); repository.setAuthenticationManager(svnAuthManager); SVNNodeKind nodeKind = repository.checkPath(file, rev); @@ -709,8 +737,7 @@ public class SvnConnection implements ScmConnection { @Override - public File getDiffs(String path, String username, String password, - String revision1, String revision2) throws AuthenticationException { + public File getDiffs(String path, Optional<ScmAuthentication> optionalScmAuthentication, String revision1, String revision2) throws AuthenticationException { String url = path.substring(0, path.lastIndexOf('/')); String file = path.substring(path.lastIndexOf('/') + 1); @@ -738,14 +765,14 @@ public class SvnConnection implements ScmConnection { } } - updateAuthentication(username, password); + updateAuthentication(optionalScmAuthentication.orElse(ScmAuthentication.ANONYMOUS)); SVNRepository repository; try { repository = SVNRepositoryFactory.create(SVNURL.parseURIEncoded(url)); - ISVNAuthenticationManager svnAuthManager = SVNWCUtil.createDefaultAuthenticationManager(username, password); + ISVNAuthenticationManager svnAuthManager = SvnUtil.createAuthenticationManager(optionalScmAuthentication.orElse(ScmAuthentication.ANONYMOUS)); repository.setAuthenticationManager(svnAuthManager); SVNNodeKind nodeKind1 = repository.checkPath(file, rev1); @@ -810,12 +837,12 @@ public class SvnConnection implements ScmConnection { } - public String getSvnRoot(String username, String password) { + public String getSvnRoot(ScmAuthentication scmAuthentication) { String repositoryRoot; try { String encodedUrl = SVNEncodingUtil.autoURIEncode(addressSvn); SVNRepository repository = SVNRepositoryFactory.create(SVNURL.parseURIEncoded(encodedUrl)); - ISVNAuthenticationManager svnAuthManager = SVNWCUtil.createDefaultAuthenticationManager(username, password); + ISVNAuthenticationManager svnAuthManager = SvnUtil.createAuthenticationManager(scmAuthentication); repository.setAuthenticationManager(svnAuthManager); repositoryRoot = repository.getRepositoryRoot(true).toString(); @@ -830,8 +857,8 @@ public class SvnConnection implements ScmConnection { } - public void updateAuthentication(String login, String password) { - authManager = SVNWCUtil.createDefaultAuthenticationManager(login, password); + public void updateAuthentication(ScmAuthentication scmAuthentication) { + authManager = SvnUtil.createAuthenticationManager(scmAuthentication); manager = SVNClientManager.newInstance(svnOption, authManager); } @@ -850,9 +877,7 @@ public class SvnConnection implements ScmConnection { ByteArrayOutputStream diff = getDiff(text); - boolean positiveSize = (diff.size() > 0); - - return positiveSize; + return diff.size() > 0; } public ByteArrayOutputStream getDiff(String text) throws IOException { @@ -866,7 +891,7 @@ public class SvnConnection implements ScmConnection { try { diffClient.doDiff(pathToFile, SVNRevision.UNDEFINED, SVNRevision.WORKING, SVNRevision.HEAD, - SVNDepth.INFINITY, true, diff, null); + SVNDepth.INFINITY, true, diff, null); } catch (SVNException e) { log.error("Diff fail", e); } @@ -888,7 +913,7 @@ public class SvnConnection implements ScmConnection { } upclient.doCheckout(remoteUrl, checkoutdir, - SVNRevision.HEAD, SVNRevision.HEAD, SVNDepth.FILES, false); + SVNRevision.HEAD, SVNRevision.HEAD, SVNDepth.FILES, false); } @@ -907,7 +932,7 @@ public class SvnConnection implements ScmConnection { /** - * Use to delete the checkout temp directory + * Use to delete the checkout temp directory. * * @param checkoutdir The dir temp directory */ @@ -926,16 +951,16 @@ public class SvnConnection implements ScmConnection { /** - * Gives the name of the person who made the last commit - * @param address the repository's address - * @param login the username to use to connect to the repository - * @param password the password to use to connect to the repository + * Gives the name of the person who made the last commit. + * + * @param address the repository's address + * @param optionalScmAuthentication the username to use to connect to the repository * @return the name of the person who made the last commit * @throws SVNException if there is a problem while accessing the repository */ - public String getHeadcommiter(String address, String login, String password) throws SVNException { + public String getHeadcommiter(String address, Optional<ScmAuthentication> optionalScmAuthentication) throws SVNException { - ISVNAuthenticationManager svnAuthManager = SVNWCUtil.createDefaultAuthenticationManager(login, password); + ISVNAuthenticationManager svnAuthManager = SvnUtil.createAuthenticationManager(optionalScmAuthentication.orElse(ScmAuthentication.ANONYMOUS)); DefaultSVNOptions svnOptions = new DefaultSVNOptions(); svnOptions.setPropertyValue(SVNProperty.EOL_STYLE, SVNProperty.EOL_STYLE_LF); @@ -944,19 +969,17 @@ public class SvnConnection implements ScmConnection { SVNInfo info = wcClient.doInfo(SVNURL.parseURIEncoded(address), SVNRevision.HEAD, SVNRevision.HEAD); - String headAuthor = info.getAuthor(); - - - return headAuthor; + return info.getAuthor(); } /** - * Makes a list of the files and directories in the repository + * Makes a list of the files and directories in the repository. + * * @param repository the repository to use to fetch information - * @param path the path to the directory (in the repository) to make the list from - * @param address the repository's address - * @param resultDto the DTO which will contain the files and directories found + * @param path the path to the directory (in the repository) to make the list from + * @param address the repository's address + * @param resultDto the DTO which will contain the files and directories found */ public void listEntries(SVNRepository repository, String path, String address, BrowseResultDto resultDto) { @@ -992,6 +1015,7 @@ public class SvnConnection implements ScmConnection { /** * Gives a unique address for the given file name + * * @param fileName the name of the file * @return a unique address for the file name */ 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 index a4fc1d3..ccf7e89 100644 --- a/swe-svn/src/main/java/org/nuiton/scmwebeditor/svn/SvnFileManager.java +++ b/swe-svn/src/main/java/org/nuiton/scmwebeditor/svn/SvnFileManager.java @@ -24,8 +24,16 @@ 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.nuiton.scmwebeditor.api.dto.CreateDirectoryDto; +import org.nuiton.scmwebeditor.api.dto.MoveFileDto; +import org.nuiton.scmwebeditor.api.dto.RemoveDirectoryDto; +import org.nuiton.scmwebeditor.api.dto.RemoveFileDto; +import org.nuiton.scmwebeditor.api.dto.UploadFileDto; +import org.nuiton.scmwebeditor.api.dto.result.CreateDirectoryResultDto; +import org.nuiton.scmwebeditor.api.dto.result.MoveFileResultDto; +import org.nuiton.scmwebeditor.api.dto.result.RemoveDirectoryResultDto; +import org.nuiton.scmwebeditor.api.dto.result.RemoveFileResultDto; +import org.nuiton.scmwebeditor.api.dto.result.UploadFileResultDto; import org.tmatesoft.svn.core.SVNAuthenticationException; import org.tmatesoft.svn.core.SVNDepth; import org.tmatesoft.svn.core.SVNException; @@ -49,7 +57,7 @@ public class SvnFileManager implements ScmFileManager { public SvnFileManager(SvnConnection connection) throws SVNException { - if(log.isDebugEnabled()) { + if (log.isDebugEnabled()) { log.debug("SVN repository"); } @@ -62,15 +70,7 @@ public class SvnFileManager implements ScmFileManager { 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())); + resultDto.setScmRoot(connection.getSvnRoot(dto.getScmAuthentication())); if (resultDto.getScmRoot() == null) { resultDto.setScmRoot(connection.getSvnPath()); @@ -82,7 +82,7 @@ public class SvnFileManager implements ScmFileManager { resultDto.setFileRoot(connection.getSvnPath()); } - connection.updateAuthentication(dto.getUsername(), dto.getPassword()); + connection.updateAuthentication(dto.getScmAuthentication()); try { connection.testConnection(); @@ -112,7 +112,7 @@ public class SvnFileManager implements ScmFileManager { try { commitClient.doImport(dto.getUpload(), SVNURL.parseURIEncoded(dto.getScmPath() + "/" + dto.getUploadFileName()), - "From scmwebeditor -- add the file : " + dto.getUploadFileName(), null, false, false, SVNDepth.EMPTY); + "From scmwebeditor -- add the file : " + dto.getUploadFileName(), null, false, false, SVNDepth.EMPTY); } catch (SVNAuthenticationException authexep) { if (log.isErrorEnabled()) { @@ -141,15 +141,7 @@ public class SvnFileManager implements ScmFileManager { 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())); + resultDto.setScmRoot(connection.getSvnRoot(dto.getScmAuthentication())); if (resultDto.getScmRoot() == null) { resultDto.setScmRoot(connection.getSvnPath()); @@ -161,7 +153,7 @@ public class SvnFileManager implements ScmFileManager { resultDto.setFileRoot(connection.getSvnPath()); } - connection.updateAuthentication(dto.getUsername(), dto.getPassword()); + connection.updateAuthentication(dto.getScmAuthentication()); try { connection.testConnection(); @@ -216,15 +208,7 @@ public class SvnFileManager implements ScmFileManager { 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())); + resultDto.setScmRoot(connection.getSvnRoot(dto.getScmAuthentication())); if (resultDto.getScmRoot() == null) { resultDto.setScmRoot(connection.getSvnPath()); @@ -236,7 +220,7 @@ public class SvnFileManager implements ScmFileManager { resultDto.setFileRoot(connection.getSvnPath()); } - connection.updateAuthentication(dto.getUsername(), dto.getPassword()); + connection.updateAuthentication(dto.getScmAuthentication()); try { connection.testConnection(); @@ -298,15 +282,7 @@ public class SvnFileManager implements ScmFileManager { 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())); + resultDto.setScmRoot(connection.getSvnRoot(dto.getScmAuthentication())); if (resultDto.getScmRoot() == null) { resultDto.setScmRoot(connection.getSvnPath()); @@ -318,7 +294,7 @@ public class SvnFileManager implements ScmFileManager { resultDto.setFileRoot(connection.getSvnPath()); } - connection.updateAuthentication(dto.getUsername(), dto.getPassword()); + connection.updateAuthentication(dto.getScmAuthentication()); try { connection.testConnection(); @@ -380,15 +356,7 @@ public class SvnFileManager implements ScmFileManager { 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())); + resultDto.setScmRoot(connection.getSvnRoot(dto.getScmAuthentication())); if (resultDto.getScmRoot() == null) { resultDto.setScmRoot(connection.getSvnPath()); @@ -400,7 +368,7 @@ public class SvnFileManager implements ScmFileManager { resultDto.setFileRoot(connection.getSvnPath()); } - connection.updateAuthentication(dto.getUsername(), dto.getPassword()); + connection.updateAuthentication(dto.getScmAuthentication()); try { connection.testConnection(); 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 2b2ac79..ac3a070 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,7 +24,12 @@ package org.nuiton.scmwebeditor.svn; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.nuiton.scmwebeditor.api.*; +import org.nuiton.scmwebeditor.api.OperationNotSupportedException; +import org.nuiton.scmwebeditor.api.RepositoryNotFoundException; +import org.nuiton.scmwebeditor.api.ScmAuthentication; +import org.nuiton.scmwebeditor.api.ScmConnection; +import org.nuiton.scmwebeditor.api.ScmProvider; +import org.nuiton.scmwebeditor.api.SweInternalException; import org.nuiton.scmwebeditor.api.dto.CreateBranchDto; import org.tmatesoft.svn.core.SVNException; import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory; @@ -32,6 +37,7 @@ import org.tmatesoft.svn.core.internal.io.svn.SVNRepositoryFactoryImpl; import javax.naming.AuthenticationException; import java.util.List; +import java.util.Optional; /** * Implementation of the SVN's specific features @@ -56,19 +62,17 @@ public class SvnProvider implements ScmProvider { } @Override - public List<String> listBranches(String address, String username, String password) throws OperationNotSupportedException { + public List<String> listBranches(String address, Optional<ScmAuthentication> optionalScmAuthentication) throws OperationNotSupportedException { throw new OperationNotSupportedException("The 'list branches' operation is not available for SVN repositories"); } @Override - public String createBranch(CreateBranchDto dto) throws OperationNotSupportedException, - AuthenticationException, RepositoryNotFoundException { + public String createBranch(CreateBranchDto dto) throws OperationNotSupportedException, AuthenticationException, RepositoryNotFoundException { throw new OperationNotSupportedException("The 'create a branch' operation is not available for SVN repositories"); } @Override - public String changeBranch(String branchName, String pathToLocalRepos, String username, String password) - throws OperationNotSupportedException { + public String changeBranch(String branchName, String pathToLocalRepos, Optional<ScmAuthentication> optionalScmAuthentication) throws OperationNotSupportedException { throw new OperationNotSupportedException("The 'change branch' operation is not available for SVN repositories"); } @@ -83,7 +87,7 @@ public class SvnProvider implements ScmProvider { } @Override - public ScmConnection getConnection(String address, String pathToLocalRepos) { + public SvnConnection getConnection(String address, String pathToLocalRepos) { SvnConnection svnConn = null; @@ -99,7 +103,7 @@ public class SvnProvider implements ScmProvider { } @Override - public ScmFileManager getFileManager(ScmConnection connection) { + public SvnFileManager getFileManager(ScmConnection connection) { SvnFileManager fileManager = null; @@ -121,16 +125,11 @@ public class SvnProvider implements ScmProvider { @Override public boolean addressSeemsCompatible(String address) { - if (address.startsWith("svn://") || address.contains("svn.") || address.contains("subversion.")) { - return true; - } else { - return false; - } + return address.startsWith("svn://") || address.contains("svn.") || address.contains("subversion."); } @Override public String getDefaultBranchName() throws OperationNotSupportedException { - throw new OperationNotSupportedException("The 'get default branch name' operation is not available for " + - "SVN repositories"); + throw new OperationNotSupportedException("The 'get default branch name' operation is not available for SVN repositories"); } } diff --git a/swe-svn/src/main/java/org/nuiton/scmwebeditor/svn/SvnUtil.java b/swe-svn/src/main/java/org/nuiton/scmwebeditor/svn/SvnUtil.java new file mode 100644 index 0000000..908a73b --- /dev/null +++ b/swe-svn/src/main/java/org/nuiton/scmwebeditor/svn/SvnUtil.java @@ -0,0 +1,18 @@ +package org.nuiton.scmwebeditor.svn; + +import org.nuiton.scmwebeditor.api.ScmAuthentication; +import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager; +import org.tmatesoft.svn.core.wc.SVNWCUtil; + +/** + * Created on 14/02/16. + * + * @author Tony Chemit - chemit@codelutin.com + */ +public class SvnUtil { + + public static ISVNAuthenticationManager createAuthenticationManager(ScmAuthentication scmAuthentication) { + return SVNWCUtil.createDefaultAuthenticationManager(scmAuthentication.getUsername(), scmAuthentication.getPassword()); + } + +} -- To stop receiving notification emails like this one, please contact nuiton.org SCM administrator <admin+scm@nuiton.org>.
This is an automated email from the git hooks/post-receive script. New commit to branch feature/3879 in repository scmwebeditor. See http://git.nuiton.org/scmwebeditor.git commit db92125b07c8459179e67db655b040f051ba5bdc Author: Tony CHEMIT <chemit@codelutin.com> Date: Sun Feb 14 19:28:56 2016 +0100 Application dans le module git --- .../org/nuiton/scmwebeditor/git/GitConnection.java | 186 +++++++++++++-------- .../nuiton/scmwebeditor/git/GitFileManager.java | 122 +++++--------- .../org/nuiton/scmwebeditor/git/GitProvider.java | 82 +++------ .../java/org/nuiton/scmwebeditor/git/GitUtil.java | 17 ++ 4 files changed, 202 insertions(+), 205 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 f5ca173..6b594eb 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 @@ -25,21 +25,40 @@ import com.google.common.collect.Lists; 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.*; +import org.eclipse.jgit.api.CheckoutCommand; +import org.eclipse.jgit.api.CloneCommand; +import org.eclipse.jgit.api.CommitCommand; +import org.eclipse.jgit.api.CreateBranchCommand; +import org.eclipse.jgit.api.DiffCommand; +import org.eclipse.jgit.api.Git; +import org.eclipse.jgit.api.LogCommand; +import org.eclipse.jgit.api.PullCommand; +import org.eclipse.jgit.api.PushCommand; +import org.eclipse.jgit.api.errors.ConcurrentRefUpdateException; +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.NoHeadException; +import org.eclipse.jgit.api.errors.TransportException; +import org.eclipse.jgit.api.errors.UnmergedPathsException; +import org.eclipse.jgit.api.errors.WrongRepositoryStateException; import org.eclipse.jgit.diff.DiffEntry; import org.eclipse.jgit.diff.DiffFormatter; -import org.eclipse.jgit.lib.*; +import org.eclipse.jgit.lib.Constants; +import org.eclipse.jgit.lib.ObjectId; +import org.eclipse.jgit.lib.ObjectLoader; +import org.eclipse.jgit.lib.ObjectReader; +import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.revwalk.RevCommit; import org.eclipse.jgit.revwalk.RevTree; import org.eclipse.jgit.revwalk.RevWalk; import org.eclipse.jgit.storage.file.FileRepositoryBuilder; import org.eclipse.jgit.transport.CredentialsProvider; -import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider; import org.eclipse.jgit.treewalk.CanonicalTreeParser; import org.eclipse.jgit.treewalk.TreeWalk; import org.eclipse.jgit.treewalk.filter.PathFilter; import org.nuiton.scmwebeditor.api.RepositoryNotFoundException; +import org.nuiton.scmwebeditor.api.ScmAuthentication; import org.nuiton.scmwebeditor.api.ScmConnection; import org.nuiton.scmwebeditor.api.ScmRevision; import org.nuiton.scmwebeditor.api.dto.BrowseDto; @@ -49,12 +68,24 @@ import org.nuiton.scmwebeditor.api.dto.result.CommitResultDto; import org.nuiton.scmwebeditor.api.dto.result.RemoveFileResultDto; import javax.naming.AuthenticationException; -import java.io.*; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.text.DateFormat; import java.text.SimpleDateFormat; -import java.util.*; +import java.util.ArrayList; +import java.util.Date; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.TreeMap; /** * Implementation of the Git's main features @@ -84,29 +115,44 @@ public class GitConnection implements ScmConnection { protected final String REPOSITORY_EXTENSION = ".git"; - public Repository getGitRepo() { return gitRepo; } + public Repository getGitRepo() { + return gitRepo; + } - public void setGitRepo(Repository gitRepo) { this.gitRepo = gitRepo; } + public void setGitRepo(Repository gitRepo) { + this.gitRepo = gitRepo; + } - public File getLocalDirectory() { return localDirectory; } + public File getLocalDirectory() { + return localDirectory; + } - public void setLocalDirectory(File localDirectory) { this.localDirectory = localDirectory; } + public void setLocalDirectory(File localDirectory) { + this.localDirectory = localDirectory; + } - public String getAddressGit() { return addressGit; } + public String getAddressGit() { + return addressGit; + } - public String getPathToLocalRepos() { return pathToLocalRepos; } + public String getPathToLocalRepos() { + return pathToLocalRepos; + } - public void setPathToLocalRepos(String pathToLocalRepos) { this.pathToLocalRepos = pathToLocalRepos; } + public void setPathToLocalRepos(String pathToLocalRepos) { + this.pathToLocalRepos = pathToLocalRepos; + } /** * Creates a new connection to a Git repository - * @param address the address of the Git repository to connect to + * + * @param address the address of the Git repository to connect to * @param pathToLocalRepos the path to the local folder which will store the user's repositories * @throws IOException if the repository can not be reached */ public GitConnection(String address, String pathToLocalRepos) throws IOException { - if(log.isDebugEnabled()) { + if (log.isDebugEnabled()) { log.debug("Git repository"); } @@ -143,7 +189,7 @@ public class GitConnection implements ScmConnection { // getting the last version of the repository try { - updateRepository(dto.getUsername(), dto.getPassword()); + updateRepository(dto.getScmAuthentication()); } catch (RepositoryNotFoundException e) { if (log.isErrorEnabled()) { @@ -210,15 +256,18 @@ public class GitConnection implements ScmConnection { String address = dto.getAddress(); // if the id parameter is not given, we look for the root - if (id.equals("")) { + switch (id) { + case "": - resultDto.setError(BrowseResultDto.ROOT); - return resultDto; + resultDto.setError(BrowseResultDto.ROOT); + return resultDto; - } else if (id.equals("0")) { - url = address; - } else { - url = id; + case "0": + url = address; + break; + default: + url = id; + break; } // if the id parameter is given, we look for the specified directory @@ -302,9 +351,17 @@ public class GitConnection implements ScmConnection { CommitResultDto resultDto = new CommitResultDto(); + ScmAuthentication scmAuthentication = dto.getScmAuthentication(); + + // authentication + if (scmAuthentication == null) { + resultDto.setError(CommitResultDto.AUTH_ERROR); + return resultDto; + } + // getting the last version of the repository try { - updateRepository(dto.getUsername(), dto.getPassword()); + updateRepository(scmAuthentication); } catch (RepositoryNotFoundException e) { if (log.isErrorEnabled()) { @@ -340,13 +397,7 @@ public class GitConnection implements ScmConnection { return resultDto; } - // authentication - if (dto.getUsername() == null || dto.getPassword() == null) { - resultDto.setError(CommitResultDto.AUTH_ERROR); - return resultDto; - } - - CredentialsProvider credentials = new UsernamePasswordCredentialsProvider(dto.getUsername(), dto.getPassword()); + CredentialsProvider credentials = GitUtil.createCredentialsProvider(scmAuthentication); // applying the changes on the local file try { @@ -365,7 +416,7 @@ public class GitConnection implements ScmConnection { // commit try { - doCommit(git, dto.getUsername(), "unknown", "From scmwebeditor -- " + dto.getCommitMessage()); + doCommit(git, scmAuthentication.getUsername(), "unknown", "From scmwebeditor -- " + dto.getCommitMessage()); } catch (GitAPIException e) { if (log.isErrorEnabled()) { log.error("Can not commit", e); @@ -411,7 +462,7 @@ public class GitConnection implements ScmConnection { } try { - resultDto.setNumRevision(getHeadRevisionNumber(dto.getAddress(), dto.getUsername(), dto.getPassword())); + resultDto.setNumRevision(getHeadRevisionNumber(dto.getAddress(), Optional.of(scmAuthentication))); } catch (AuthenticationException e) { if (log.isErrorEnabled()) { log.error("Auth fail", e); @@ -422,11 +473,11 @@ public class GitConnection implements ScmConnection { } @Override - public File getFileContent(String path, String username, String password) throws AuthenticationException { + public File getFileContent(String path, Optional<ScmAuthentication> optionalScmAuthentication) throws AuthenticationException { // getting the last version of the repository try { - updateRepository(username, password); + updateRepository(optionalScmAuthentication.orElse(ScmAuthentication.ANONYMOUS)); } catch (RepositoryNotFoundException e) { if (log.isErrorEnabled()) { @@ -444,11 +495,11 @@ public class GitConnection implements ScmConnection { } @Override - public String getHeadRevisionNumber(String path, String username, String password) throws AuthenticationException { + public String getHeadRevisionNumber(String path, Optional<ScmAuthentication> optionalScmAuthentication) throws AuthenticationException { // getting the last version of the repository try { - updateRepository(username, password); + updateRepository(optionalScmAuthentication.orElse(ScmAuthentication.ANONYMOUS)); } catch (RepositoryNotFoundException e) { if (log.isErrorEnabled()) { @@ -481,10 +532,10 @@ public class GitConnection implements ScmConnection { } @Override - public String getFilePath(String address, String repositoryRoot, String username, String password) { + public String getFilePath(String address, String repositoryRoot, Optional<ScmAuthentication> optionalScmAuthentication) { try { - updateRepository(username, password); + updateRepository(optionalScmAuthentication.orElse(ScmAuthentication.ANONYMOUS)); } catch (RepositoryNotFoundException e) { if (log.isErrorEnabled()) { log.error("Error while cloning or pulling the repository", e); @@ -499,14 +550,14 @@ public class GitConnection implements ScmConnection { } @Override - public Map<ScmRevision, String> getRevisions(String address, String username, String password) { + public Map<ScmRevision, String> getRevisions(String address, Optional<ScmAuthentication> optionalScmAuthentication) { final int MAX_MESSAGE_LENGTH = 64; String pathOnRepo = address.replace(addressGit + "/", ""); - Map<ScmRevision, String> revisions = new TreeMap<ScmRevision, String>(); + Map<ScmRevision, String> revisions = new TreeMap<>(); try { - updateRepository(username, password); + updateRepository(optionalScmAuthentication.orElse(ScmAuthentication.ANONYMOUS)); } catch (RepositoryNotFoundException e) { if (log.isErrorEnabled()) { @@ -568,11 +619,11 @@ public class GitConnection implements ScmConnection { } @Override - public File getFileContentAtRevision(String path, String username, String password, + public File getFileContentAtRevision(String path, Optional<ScmAuthentication> optionalScmAuthentication, String revision) throws AuthenticationException { try { - updateRepository(username, password); + updateRepository(optionalScmAuthentication.orElse(ScmAuthentication.ANONYMOUS)); } catch (RepositoryNotFoundException e) { if (log.isErrorEnabled()) { @@ -626,11 +677,11 @@ public class GitConnection implements ScmConnection { } @Override - public File getDiffs(String path, String username, String password, String revision1, + public File getDiffs(String path, Optional<ScmAuthentication> optionalScmAuthentication, String revision1, String revision2) throws AuthenticationException { try { - updateRepository(username, password); + updateRepository(optionalScmAuthentication.orElse(ScmAuthentication.ANONYMOUS)); } catch (RepositoryNotFoundException e) { if (log.isErrorEnabled()) { @@ -673,7 +724,7 @@ public class GitConnection implements ScmConnection { DiffFormatter df = new DiffFormatter(baos); df.setRepository(gitRepo); - for(DiffEntry diff : diffs) { + for (DiffEntry diff : diffs) { df.format(diff); String diffText = baos.toString("UTF-8"); FileUtils.writeStringToFile(tempFile, diffText); @@ -683,7 +734,7 @@ public class GitConnection implements ScmConnection { } catch (IOException e) { if (log.isErrorEnabled()) { log.error("Error while getting file '" + pathOnRepo + "' content at revisions " + revision1 - + " and " + revision2, e); + + " and " + revision2, e); } } catch (GitAPIException e) { if (log.isErrorEnabled()) { @@ -697,6 +748,7 @@ public class GitConnection implements ScmConnection { /** * Changing for another branch + * * @param branchName the new branch's name * @throws IOException if reaching the repository is not possible */ @@ -738,11 +790,11 @@ public class GitConnection implements ScmConnection { /** * 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 + * + * @param scmAuthentication optional user authentication informations * @throws RepositoryNotFoundException if the repository is not found */ - public void updateRepository(String username, String password) throws RepositoryNotFoundException { + public void updateRepository(ScmAuthentication scmAuthentication) throws RepositoryNotFoundException { // Cloning the remote repository to a local directory String hashedAddress = addressGit; @@ -753,13 +805,9 @@ public class GitConnection implements ScmConnection { hashedAddress = hashResult; } - localDirectory = new File(pathToLocalRepos + File.separator + hashedAddress); + localDirectory = new File(pathToLocalRepos + File.separator + hashedAddress); - CredentialsProvider credentials = null; - - if (username != null && password != null) { - credentials = new UsernamePasswordCredentialsProvider(username, password); - } + CredentialsProvider credentials = GitUtil.createCredentialsProvider(scmAuthentication); try { if (!localDirectory.exists()) { @@ -775,13 +823,7 @@ public class GitConnection implements ScmConnection { try { pull.call(); - } catch (InvalidRemoteException e) { - if (log.isErrorEnabled()) { - log.error("Can't pull the remote repository", e); - } - cloneRepository(credentials); - - } catch (JGitInternalException e) { + } catch (InvalidRemoteException | JGitInternalException e) { if (log.isErrorEnabled()) { log.error("Can't pull the remote repository", e); } @@ -848,10 +890,11 @@ public class GitConnection implements ScmConnection { /** * Clones the repository (clone command) + * * @param credentials the username and 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 + * @throws IOException if it is not possible to reach the repository + * @throws AuthenticationException if there is a problem during the authentication process */ protected void cloneRepository(CredentialsProvider credentials) throws IOException, AuthenticationException, RepositoryNotFoundException { @@ -900,7 +943,8 @@ public class GitConnection implements ScmConnection { /** * Hashes a String with then given algorithms - * @param toHash the String to hash + * + * @param toHash the String to hash * @param algorithm the algorithm to use to hash the String * @return the hashed String */ @@ -940,9 +984,10 @@ public class GitConnection implements ScmConnection { /** * Calls the commit command - * @param git the git repository which will receive the commit - * @param authorName the name of the commit's author - * @param authorEmail the e-mail address of the author + * + * @param git the git repository which will receive the commit + * @param authorName the name of the commit's author + * @param authorEmail the e-mail address of the author * @param commitMessage the message that describes the commit * @throws GitAPIException if there is a problem during the commit process */ @@ -962,6 +1007,7 @@ public class GitConnection implements ScmConnection { /** * Handles the exception thrown by a push command + * * @param e the exception thrown by a push command */ protected void handlePushException(GitAPIException e) { 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 index 7205497..8431060 100644 --- a/swe-git/src/main/java/org/nuiton/scmwebeditor/git/GitFileManager.java +++ b/swe-git/src/main/java/org/nuiton/scmwebeditor/git/GitFileManager.java @@ -24,18 +24,30 @@ 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.AddCommand; +import org.eclipse.jgit.api.Git; +import org.eclipse.jgit.api.PullCommand; +import org.eclipse.jgit.api.PushCommand; +import org.eclipse.jgit.api.RmCommand; 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.ScmAuthentication; import org.nuiton.scmwebeditor.api.ScmFileManager; -import org.nuiton.scmwebeditor.api.dto.*; -import org.nuiton.scmwebeditor.api.dto.result.*; +import org.nuiton.scmwebeditor.api.dto.CreateDirectoryDto; +import org.nuiton.scmwebeditor.api.dto.MoveFileDto; +import org.nuiton.scmwebeditor.api.dto.RemoveDirectoryDto; +import org.nuiton.scmwebeditor.api.dto.RemoveFileDto; +import org.nuiton.scmwebeditor.api.dto.UploadFileDto; +import org.nuiton.scmwebeditor.api.dto.result.CreateDirectoryResultDto; +import org.nuiton.scmwebeditor.api.dto.result.MoveFileResultDto; +import org.nuiton.scmwebeditor.api.dto.result.RemoveDirectoryResultDto; +import org.nuiton.scmwebeditor.api.dto.result.RemoveFileResultDto; +import org.nuiton.scmwebeditor.api.dto.result.UploadFileResultDto; import javax.naming.AuthenticationException; import java.io.File; @@ -62,6 +74,7 @@ public class GitFileManager implements ScmFileManager { /** * 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 */ @@ -77,7 +90,7 @@ public class GitFileManager implements ScmFileManager { // getting the last version of the repository try { - updateRepository(dto.getUsername(), dto.getPassword()); + updateRepository(dto.getScmAuthentication()); } catch (RepositoryNotFoundException e) { if (log.isErrorEnabled()) { @@ -142,15 +155,7 @@ public class GitFileManager implements ScmFileManager { } // authentication - if (dto.getUsername() == null) { - dto.setUsername("anonymous"); - } - - if (dto.getPassword() == null) { - dto.setPassword("anonymous"); - } - - CredentialsProvider credentials = new UsernamePasswordCredentialsProvider(dto.getUsername(), dto.getPassword()); + CredentialsProvider credentials = GitUtil.createCredentialsProvider(dto.getScmAuthentication()); try { Git git = Git.open(connection.getLocalDirectory()); @@ -169,7 +174,7 @@ public class GitFileManager implements ScmFileManager { // commit try { - connection.doCommit(git, dto.getUsername(), "unknown", "From scmwebeditor -- add the file : " + connection.doCommit(git, dto.getScmAuthentication().getUsername(), "unknown", "From scmwebeditor -- add the file : " + dto.getUploadFileName()); } catch (GitAPIException e) { if (log.isErrorEnabled()) { @@ -231,7 +236,7 @@ public class GitFileManager implements ScmFileManager { // getting the last version of the repository try { - updateRepository(dto.getUsername(), dto.getPassword()); + updateRepository(dto.getScmAuthentication()); } catch (RepositoryNotFoundException e) { if (log.isErrorEnabled()) { @@ -286,15 +291,7 @@ public class GitFileManager implements ScmFileManager { 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()); + CredentialsProvider credentials = GitUtil.createCredentialsProvider(dto.getScmAuthentication()); try { Git git = Git.open(connection.getLocalDirectory()); @@ -316,7 +313,7 @@ public class GitFileManager implements ScmFileManager { // commit try { - connection.doCommit(git, dto.getUsername(), "unknown", "From scmwebeditor -- remove the file : " + pathOnRepo); + connection.doCommit(git, dto.getScmAuthentication().getUsername(), "unknown", "From scmwebeditor -- remove the file : " + pathOnRepo); } catch (GitAPIException e) { if (log.isErrorEnabled()) { log.error("Can not commit", e); @@ -382,7 +379,7 @@ public class GitFileManager implements ScmFileManager { // getting the last version of the repository try { - updateRepository(dto.getUsername(), dto.getPassword()); + updateRepository(dto.getScmAuthentication()); } catch (RepositoryNotFoundException e) { if (log.isErrorEnabled()) { @@ -436,7 +433,7 @@ public class GitFileManager implements ScmFileManager { } File file = new File(connection.getLocalDirectory().getAbsolutePath() + File.separator + pathOnRepo + - File.separator + dto.getDirectoryName()); + File.separator + dto.getDirectoryName()); file.mkdir(); File emptyFile = new File(file.getAbsolutePath() + File.separator + "placeholder"); @@ -450,15 +447,7 @@ public class GitFileManager implements ScmFileManager { } // authentication - if (dto.getUsername() == null) { - dto.setUsername("anonymous"); - } - - if (dto.getPassword() == null) { - dto.setPassword("anonymous"); - } - - CredentialsProvider credentials = new UsernamePasswordCredentialsProvider(dto.getUsername(), dto.getPassword()); + CredentialsProvider credentials = GitUtil.createCredentialsProvider(dto.getScmAuthentication()); try { Git git = Git.open(connection.getLocalDirectory()); @@ -485,7 +474,7 @@ public class GitFileManager implements ScmFileManager { // commit try { - connection.doCommit(git, dto.getUsername(), "unknown", "From scmwebeditor -- create the directory : " + dto.getDirectoryName()); + connection.doCommit(git, dto.getScmAuthentication().getUsername(), "unknown", "From scmwebeditor -- create the directory : " + dto.getDirectoryName()); } catch (GitAPIException e) { if (log.isErrorEnabled()) { log.error("Can not commit", e); @@ -542,7 +531,7 @@ public class GitFileManager implements ScmFileManager { // getting the last version of the repository try { - updateRepository(dto.getUsername(), dto.getPassword()); + updateRepository(dto.getScmAuthentication()); } catch (RepositoryNotFoundException e) { if (log.isErrorEnabled()) { @@ -593,15 +582,7 @@ public class GitFileManager implements ScmFileManager { 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()); + CredentialsProvider credentials = GitUtil.createCredentialsProvider(dto.getScmAuthentication()); try { Git git = Git.open(connection.getLocalDirectory()); @@ -623,7 +604,7 @@ public class GitFileManager implements ScmFileManager { // commit try { - connection.doCommit(git, dto.getUsername(), "unknown", "From scmwebeditor -- remove the directory : " + pathOnRepo); + connection.doCommit(git, dto.getScmAuthentication().getUsername(), "unknown", "From scmwebeditor -- remove the directory : " + pathOnRepo); } catch (GitAPIException e) { if (log.isErrorEnabled()) { log.error("Can not commit", e); @@ -689,7 +670,7 @@ public class GitFileManager implements ScmFileManager { // getting the last version of the repository try { - updateRepository(dto.getUsername(), dto.getPassword()); + updateRepository(dto.getScmAuthentication()); } catch (RepositoryNotFoundException e) { if (log.isErrorEnabled()) { @@ -759,7 +740,7 @@ public class GitFileManager implements ScmFileManager { File sourceFile = new File(connection.getLocalDirectory().getAbsolutePath() + File.separator + pathOnRepoFile); File destFile = new File(connection.getLocalDirectory().getAbsolutePath() + File.separator + pathOnRepoDirectory - + File.separator + sourceFileName); + + File.separator + sourceFileName); try { FileUtils.moveFile(sourceFile, destFile); @@ -771,15 +752,7 @@ public class GitFileManager implements ScmFileManager { } // authentication - if (dto.getUsername() == null) { - dto.setUsername("anonymous"); - } - - if (dto.getPassword() == null) { - dto.setPassword("anonymous"); - } - - CredentialsProvider credentials = new UsernamePasswordCredentialsProvider(dto.getUsername(), dto.getPassword()); + CredentialsProvider credentials = GitUtil.createCredentialsProvider(dto.getScmAuthentication()); try { Git git = Git.open(connection.getLocalDirectory()); @@ -800,7 +773,7 @@ public class GitFileManager implements ScmFileManager { // commit try { - connection.doCommit(git, dto.getUsername(), "unknown", "From scmwebeditor -- move the file : " + pathOnRepoFile + + connection.doCommit(git, dto.getScmAuthentication().getUsername(), "unknown", "From scmwebeditor -- move the file : " + pathOnRepoFile + " to : " + pathOnRepoDirectory); } catch (GitAPIException e) { if (log.isErrorEnabled()) { @@ -863,14 +836,13 @@ public class GitFileManager implements ScmFileManager { /** * 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 + * + * @param scmAuthentication the username 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 + * @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 { + public void updateRepository(ScmAuthentication scmAuthentication) throws RepositoryNotFoundException, IOException, AuthenticationException { // Cloning the remote repository to a local directory String hashedAddress = connection.getAddressGit(); @@ -881,13 +853,9 @@ public class GitFileManager implements ScmFileManager { hashedAddress = hashResult; } - connection.setLocalDirectory(new File(connection.getPathToLocalRepos() + File.separator + hashedAddress)); + connection.setLocalDirectory(new File(connection.getPathToLocalRepos() + File.separator + hashedAddress)); - CredentialsProvider credentials = null; - - if (username != null && password != null) { - credentials = new UsernamePasswordCredentialsProvider(username, password); - } + CredentialsProvider credentials = GitUtil.createCredentialsProvider(scmAuthentication); if (!connection.getLocalDirectory().exists()) { @@ -902,13 +870,7 @@ public class GitFileManager implements ScmFileManager { try { pull.call(); - } catch (InvalidRemoteException e) { - if (log.isErrorEnabled()) { - log.error("Can't pull the remote repository", e); - } - connection.cloneRepository(credentials); - - } catch (JGitInternalException e) { + } catch (InvalidRemoteException | JGitInternalException e) { if (log.isErrorEnabled()) { log.error("Can't pull the remote repository", e); } 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 e17ea42..553dbcc 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 @@ -23,15 +23,23 @@ package org.nuiton.scmwebeditor.git; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.eclipse.jgit.api.*; -import org.eclipse.jgit.api.errors.*; +import org.eclipse.jgit.api.CreateBranchCommand; +import org.eclipse.jgit.api.DeleteBranchCommand; +import org.eclipse.jgit.api.Git; +import org.eclipse.jgit.api.LsRemoteCommand; +import org.eclipse.jgit.api.PushCommand; +import org.eclipse.jgit.api.errors.GitAPIException; import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.Ref; 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.*; +import org.nuiton.scmwebeditor.api.OperationNotSupportedException; +import org.nuiton.scmwebeditor.api.RepositoryNotFoundException; +import org.nuiton.scmwebeditor.api.ScmAuthentication; +import org.nuiton.scmwebeditor.api.ScmConnection; +import org.nuiton.scmwebeditor.api.ScmProvider; +import org.nuiton.scmwebeditor.api.SweInternalException; import org.nuiton.scmwebeditor.api.dto.CreateBranchDto; import org.nuiton.scmwebeditor.api.dto.result.AbstractResultDto; @@ -41,6 +49,7 @@ import java.io.IOException; import java.util.ArrayList; import java.util.Collection; import java.util.List; +import java.util.Optional; /** * Implementation of the Git's specific features @@ -59,18 +68,11 @@ public class GitProvider implements ScmProvider { } @Override - public List<String> listBranches(String address, String username, String password) throws OperationNotSupportedException { + public List<String> listBranches(String address, Optional<ScmAuthentication> optionalScmAuthentication) throws OperationNotSupportedException { - if (username == null) { - username = "anonymous"; - } - if (password == null) { - password = "anonymous"; - } + List<String> branches = new ArrayList<>(); - List<String> branches = new ArrayList<String>(); - - CredentialsProvider credentials = new UsernamePasswordCredentialsProvider(username, password); + CredentialsProvider credentials = GitUtil.createCredentialsProvider(optionalScmAuthentication.orElse(ScmAuthentication.ANONYMOUS)); LsRemoteCommand lsRemote = new LsRemoteCommand(null); lsRemote.setRemote(address); @@ -80,7 +82,7 @@ public class GitProvider implements ScmProvider { try { Collection<Ref> lsRemoteResult = lsRemote.call(); - branches = new ArrayList<String>(); + branches = new ArrayList<>(); for (Ref branch : lsRemoteResult) { @@ -105,22 +107,15 @@ public class GitProvider implements ScmProvider { AuthenticationException, RepositoryNotFoundException { String error = null; - GitConnection conn = (GitConnection) getConnection(address, dto.getPathToLocalRepos()); + GitConnection conn = getConnection(address, dto.getPathToLocalRepos()); // authentication - if (dto.getUsername() == null) { - dto.setUsername("anonymous"); - } - if (dto.getPassword() == null) { - dto.setPassword("anonymous"); - } - - CredentialsProvider credentials = new UsernamePasswordCredentialsProvider(dto.getUsername(), dto.getPassword()); + CredentialsProvider credentials = GitUtil.createCredentialsProvider(dto.getScmAuthentication()); try { // setting the local repository in the right state - conn.updateRepository(dto.getUsername(), dto.getPassword()); + conn.updateRepository(dto.getScmAuthentication()); File localDirectory = conn.getLocalDirectory(); conn.changeBranch(dto.getSelectedBranch()); @@ -156,25 +151,7 @@ public class GitProvider implements ScmProvider { push.call(); } catch (GitAPIException e) { - String logMessage = "Can not push"; - - if (e instanceof NoHeadException) { - logMessage = "Can not push : the Git repository has no HEAD reference"; - } else if (e instanceof UnmergedPathsException) { - logMessage = "Can not push : conflicts found (unmerged paths)"; - } else if (e instanceof ConcurrentRefUpdateException) { - logMessage = "Can not push : someone else is updating the HEAD or the branch"; - } else if (e instanceof WrongRepositoryStateException) { - logMessage = "Can not push : the repository is not in the right state"; - } - //TC-2015-12-22 does not exist any longer -// else if (e instanceof RejectCommitException) { -// logMessage = "Can not push : commit rejected"; -// } - - if (log.isErrorEnabled()) { - log.error(logMessage, e); - } + conn.handlePushException(e); // if the branch could not be pushed, we delete it to avoid errors DeleteBranchCommand deleteBranch = git.branchDelete(); @@ -207,14 +184,13 @@ public class GitProvider implements ScmProvider { } @Override - public String changeBranch(String branchName, String pathToLocalRepos, String username, String password) - throws OperationNotSupportedException { + public String changeBranch(String branchName, String pathToLocalRepos, Optional<ScmAuthentication> optionalScmAuthentication) throws OperationNotSupportedException { String error = null; - GitConnection conn = (GitConnection) getConnection(address, pathToLocalRepos); + GitConnection conn = getConnection(address, pathToLocalRepos); try { - conn.updateRepository(username, password); + conn.updateRepository(optionalScmAuthentication.orElse(ScmAuthentication.ANONYMOUS)); try { conn.changeBranch(branchName); @@ -245,7 +221,7 @@ public class GitProvider implements ScmProvider { } @Override - public ScmConnection getConnection(String address, String pathToLocalRepos) { + public GitConnection getConnection(String address, String pathToLocalRepos) { GitConnection gitConn = null; this.address = address; @@ -262,7 +238,7 @@ public class GitProvider implements ScmProvider { } @Override - public ScmFileManager getFileManager(ScmConnection connection) { + public GitFileManager getFileManager(ScmConnection connection) { GitFileManager fileManager = null; @@ -284,11 +260,7 @@ public class GitProvider implements ScmProvider { @Override public boolean addressSeemsCompatible(String address) { - if (address.contains(".git") || address.startsWith("git://") || address.contains("git.")) { - return true; - } else { - return false; - } + return address.contains(".git") || address.startsWith("git://") || address.contains("git."); } @Override diff --git a/swe-git/src/main/java/org/nuiton/scmwebeditor/git/GitUtil.java b/swe-git/src/main/java/org/nuiton/scmwebeditor/git/GitUtil.java new file mode 100644 index 0000000..d73b515 --- /dev/null +++ b/swe-git/src/main/java/org/nuiton/scmwebeditor/git/GitUtil.java @@ -0,0 +1,17 @@ +package org.nuiton.scmwebeditor.git; + +import org.eclipse.jgit.transport.CredentialsProvider; +import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider; +import org.nuiton.scmwebeditor.api.ScmAuthentication; + +/** + * Created on 14/02/16. + * + * @author Tony Chemit - chemit@codelutin.com + */ +public class GitUtil { + + public static CredentialsProvider createCredentialsProvider(ScmAuthentication scmAuthentication) { + return new UsernamePasswordCredentialsProvider(scmAuthentication.getUsername(), scmAuthentication.getPassword()); + } +} -- To stop receiving notification emails like this one, please contact nuiton.org SCM administrator <admin+scm@nuiton.org>.
This is an automated email from the git hooks/post-receive script. New commit to branch feature/3879 in repository scmwebeditor. See http://git.nuiton.org/scmwebeditor.git commit 412769c139ed3ba5574ff6286f5284bcdb358aa2 Author: Tony CHEMIT <chemit@codelutin.com> Date: Sun Feb 14 19:29:11 2016 +0100 Application dans le module web + nettoyage de code --- .../org/nuiton/scmwebeditor/uiweb/ScmSession.java | 4 +- .../org/nuiton/scmwebeditor/uiweb/ScmUser.java | 4 + .../AbstractScmWebEditorWithAddressAction.java | 106 +++++----------- .../scmwebeditor/uiweb/actions/BrowseAction.java | 14 +-- .../uiweb/actions/CreateBranchAction.java | 59 +++++---- .../uiweb/actions/CreateDirectoryAction.java | 10 +- .../uiweb/actions/DetectScmAction.java | 1 + .../uiweb/actions/DownloadFileAction.java | 19 ++- .../scmwebeditor/uiweb/actions/EditAction.java | 113 ++++++++++------- .../scmwebeditor/uiweb/actions/GetImageAction.java | 16 ++- .../uiweb/actions/ListBranchesAction.java | 27 +++-- .../scmwebeditor/uiweb/actions/LogoutAction.java | 20 +-- .../scmwebeditor/uiweb/actions/MoveFileAction.java | 55 ++++++--- .../uiweb/actions/RemoveDirectoryAction.java | 38 +++--- .../uiweb/actions/RemoveFileAction.java | 35 ++++-- .../scmwebeditor/uiweb/actions/ResetAction.java | 23 ++-- .../uiweb/actions/ScmWebEditorCommitAction.java | 135 ++++++++++++++------- .../uiweb/actions/ScmWebEditorMainAction.java | 6 +- .../uiweb/actions/UploadFileAction.java | 35 +++--- .../uiweb/actions/ViewDiffsAction.java | 61 +++++----- .../uiweb/actions/ViewImageAction.java | 71 +++++------ .../uiweb/actions/ViewRevisionAction.java | 66 +++++----- 22 files changed, 502 insertions(+), 416 deletions(-) diff --git a/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/ScmSession.java b/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/ScmSession.java index e92d119..2f099d5 100644 --- a/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/ScmSession.java +++ b/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/ScmSession.java @@ -21,7 +21,7 @@ */ package org.nuiton.scmwebeditor.uiweb; -import org.apache.shiro.authc.UsernamePasswordToken; +import org.nuiton.scmwebeditor.api.ScmAuthentication; import java.util.HashMap; import java.util.Map; @@ -55,7 +55,7 @@ public class ScmSession { scmUsers.put(address, new ScmUser(login, password)); } - public void addScmUser(String repositoryUUID, UsernamePasswordToken usernamePasswordToken) { + public void addScmUser(String repositoryUUID, ScmAuthentication usernamePasswordToken) { scmUsers.put(repositoryUUID, new ScmUser(usernamePasswordToken.getUsername(), new String(usernamePasswordToken.getPassword()))); } diff --git a/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/ScmUser.java b/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/ScmUser.java index 0f7629c..83a9edb 100644 --- a/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/ScmUser.java +++ b/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/ScmUser.java @@ -21,9 +21,13 @@ */ package org.nuiton.scmwebeditor.uiweb; +import org.nuiton.scmwebeditor.api.ScmAuthentication; + /** * The authentication information related to an user + * FIXME Use {@link ScmAuthentication}. */ +@Deprecated public class ScmUser { /** the user's username */ diff --git a/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/AbstractScmWebEditorWithAddressAction.java b/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/AbstractScmWebEditorWithAddressAction.java index 0cb136c..e717ee6 100644 --- a/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/AbstractScmWebEditorWithAddressAction.java +++ b/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/AbstractScmWebEditorWithAddressAction.java @@ -2,9 +2,9 @@ package org.nuiton.scmwebeditor.uiweb.actions; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.apache.shiro.authc.UsernamePasswordToken; import org.apache.shiro.codec.Base64; import org.apache.shiro.crypto.BlowfishCipherService; +import org.nuiton.scmwebeditor.api.ScmAuthentication; import org.nuiton.scmwebeditor.api.ScmConnection; import org.nuiton.scmwebeditor.api.ScmFileManager; import org.nuiton.scmwebeditor.api.ScmProvider; @@ -28,6 +28,8 @@ public class AbstractScmWebEditorWithAddressAction extends AbstractScmWebEditorA /** Logger. */ private static final Log log = LogFactory.getLog(AbstractScmWebEditorWithAddressAction.class); + protected static final Pattern AUTHENTICATED_URL_PATTERN = Pattern.compile("(http[s]://)([^:]+):([^@]+)@(.+)"); + /** the username to use to connect to the repository */ private String username; @@ -37,15 +39,13 @@ public class AbstractScmWebEditorWithAddressAction extends AbstractScmWebEditorA /** the repository's address */ private String address; - private ScmAuthenticatedUrl authenticatedUrl; - private ScmProvider scmProvider; private String sessionId; private String pathToLocalRepos; - private ScmConnection scmConnection; + private ScmConnection scmConnection; private ScmFileManager scmFileManager; @@ -86,11 +86,13 @@ public class AbstractScmWebEditorWithAddressAction extends AbstractScmWebEditorA address = address.substring(0, address.lastIndexOf('/')); } - authenticatedUrl = getAuthenticatedUrl(address, username, pw); + Matcher matcher = AUTHENTICATED_URL_PATTERN.matcher(address); + if (matcher.matches()) { - address = authenticatedUrl.getUrl(); - username = authenticatedUrl.getUsername(); - pw = authenticatedUrl.getPw(); + username = matcher.group(2); + pw = matcher.group(3); + address = matcher.group(1) + matcher.group(4); + } } @@ -130,9 +132,9 @@ public class AbstractScmWebEditorWithAddressAction extends AbstractScmWebEditorA return scmConnection; } - protected Optional<UsernamePasswordToken> initAuthenticationInformations(String repositoryUUID, boolean readCookie, boolean saveCookie) { + protected Optional<ScmAuthentication> initAuthenticationInformations(String repositoryUUID, boolean readCookie, boolean saveCookie) { - UsernamePasswordToken token = null; + ScmAuthentication token = null; if (readCookie) { @@ -161,7 +163,7 @@ public class AbstractScmWebEditorWithAddressAction extends AbstractScmWebEditorA if (usernameDecode != null) { String[] resCookie = usernameDecode.split(","); if (resCookie.length == 2) { - token = new UsernamePasswordToken(resCookie[0], resCookie[1]); + token = new ScmAuthentication(resCookie[0], resCookie[1].toCharArray()); } } @@ -170,26 +172,26 @@ public class AbstractScmWebEditorWithAddressAction extends AbstractScmWebEditorA } - if (saveCookie && token !=null) { + if (saveCookie && token != null) { BlowfishCipherService bf = new BlowfishCipherService(); byte[] privateKey = Base64.decode(ScmWebEditorConfig.getKey()); - Cookie authCookie = null; + Cookie authCookie = null; - try { - authCookie = new Cookie(repositoryUUID, bf.encrypt((token.getUsername() + "," + new String(token.getPassword())).getBytes("UTF-8"), privateKey).toBase64()); - } catch (UnsupportedEncodingException e) { - if (log.isErrorEnabled()) { - log.error("Can not get bytes from UTF-8 encoding"); - } + try { + authCookie = new Cookie(repositoryUUID, bf.encrypt((token.getUsername() + "," + new String(token.getPassword())).getBytes("UTF-8"), privateKey).toBase64()); + } catch (UnsupportedEncodingException e) { + if (log.isErrorEnabled()) { + log.error("Can not get bytes from UTF-8 encoding"); } + } - if (authCookie != null) { - authCookie.setMaxAge(60 * 60 * 24 * 365); - response.addCookie(authCookie); - } + if (authCookie != null) { + authCookie.setMaxAge(60 * 60 * 24 * 365); + response.addCookie(authCookie); + } } @@ -228,6 +230,7 @@ public class AbstractScmWebEditorWithAddressAction extends AbstractScmWebEditorA /** * Reads the information in the session to give the username and the password for a given repository if necessary + * * @param repositoryUUID the ID of the repository to connect to * @return the username to use at position [0] ; the password to use at position [1] */ @@ -270,62 +273,7 @@ public class AbstractScmWebEditorWithAddressAction extends AbstractScmWebEditorA protected void resetAuthentication() { username = null; - pw= null; - } - - protected static final Pattern AUTHENTICATED_URL_PATTERN = Pattern.compile("(http[s]://)([^:]+):([^@]+)@(.+)"); - - public static ScmAuthenticatedUrl getAuthenticatedUrl(String url, String username, String pw) { - - Matcher matcher = AUTHENTICATED_URL_PATTERN.matcher(url); - if (matcher.matches()) { - - username = matcher.group(2); - pw = matcher.group(3); - url = matcher.group(1) + matcher.group(4); - } else { - - if (username == null) { - username = "anonymous"; - } - if (pw == null) { - pw = "anonymous"; - } - - } - - return new ScmAuthenticatedUrl(url, username, pw); - } - - public static class ScmAuthenticatedUrl { - - private final String url; - private final String username; - private final String pw; - - - public ScmAuthenticatedUrl(String url, String username, String pw) { - this.url = url; - this.username = username; - this.pw = pw; - } - - public String getUrl() { - return url; - } - - public String getUsername() { - return username; - } - - public String getPw() { - return pw; - } - - public boolean isAuthenticated() { - return !username.equals("anonymous") && !username.equals("") && !pw.equals("anonymous") && !pw.equals(""); - } - + pw = null; } } diff --git a/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/BrowseAction.java b/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/BrowseAction.java index df692f2..bb986dc 100644 --- a/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/BrowseAction.java +++ b/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/BrowseAction.java @@ -24,7 +24,7 @@ package org.nuiton.scmwebeditor.uiweb.actions; import com.jgeppert.struts2.jquery.tree.result.TreeNode; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.apache.shiro.authc.UsernamePasswordToken; +import org.nuiton.scmwebeditor.api.ScmAuthentication; import org.nuiton.scmwebeditor.api.ScmConnection; import org.nuiton.scmwebeditor.api.dto.BrowseDto; import org.nuiton.scmwebeditor.api.dto.result.BrowseResultDto; @@ -100,7 +100,7 @@ public class BrowseAction extends AbstractScmWebEditorWithAddressAction { String repositoryUUID = getRepositoryUUID(); - Optional<UsernamePasswordToken> optionalAuthentication = initAuthenticationInformations(repositoryUUID, false, true); + Optional<ScmAuthentication> optionalAuthentication = initAuthenticationInformations(repositoryUUID, false, true); scmSupportsBranches = getScmProvider().supportsBranches(); @@ -112,13 +112,7 @@ public class BrowseAction extends AbstractScmWebEditorWithAddressAction { // putting all the parameters into a DTO BrowseDto dto = new BrowseDto(); dto.setAddress(getAddress()); - - boolean withAuthentication = optionalAuthentication.isPresent(); - if (withAuthentication) { - - dto.setUsername(optionalAuthentication.get().getUsername()); - dto.setPassword(new String(optionalAuthentication.get().getPassword())); - } + dto.setScmAuthentication(optionalAuthentication); dto.setId(id); dto.setSelectedBranch(selectedBranch); @@ -200,7 +194,7 @@ public class BrowseAction extends AbstractScmWebEditorWithAddressAction { // if the repository is not protected for writing, we get its UUID - if (withAuthentication) { + if (optionalAuthentication.isPresent()) { storeUsernamePasswordToCookie(repositoryUUID); diff --git a/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/CreateBranchAction.java b/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/CreateBranchAction.java index 75ccc51..af72e7e 100644 --- a/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/CreateBranchAction.java +++ b/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/CreateBranchAction.java @@ -21,11 +21,12 @@ */ package org.nuiton.scmwebeditor.uiweb.actions; +import com.google.common.base.Preconditions; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.apache.shiro.authc.UsernamePasswordToken; import org.nuiton.scmwebeditor.api.OperationNotSupportedException; import org.nuiton.scmwebeditor.api.RepositoryNotFoundException; +import org.nuiton.scmwebeditor.api.ScmAuthentication; import org.nuiton.scmwebeditor.api.ScmProvider; import org.nuiton.scmwebeditor.api.dto.CreateBranchDto; import org.nuiton.scmwebeditor.api.dto.result.AbstractResultDto; @@ -58,25 +59,45 @@ public class CreateBranchAction extends AbstractScmWebEditorWithAddressAction { /** the name of the new branch */ protected String newBranchName; - public boolean isBadLogin() { return badLogin; } + public boolean isBadLogin() { + return badLogin; + } - public void setBadLogin(boolean badLogin) { this.badLogin = badLogin; } + public void setBadLogin(boolean badLogin) { + this.badLogin = badLogin; + } - public boolean isError() { return error; } + public boolean isError() { + return error; + } - public void setError(boolean error) { this.error = error; } + public void setError(boolean error) { + this.error = error; + } - public List<String> getBranches() { return branches; } + public List<String> getBranches() { + return branches; + } - public void setBranches(List<String> branches) { this.branches = branches; } + public void setBranches(List<String> branches) { + this.branches = branches; + } - public String getSelectedBranch() { return selectedBranch; } + public String getSelectedBranch() { + return selectedBranch; + } - public void setSelectedBranch(String selectedBranch) { this.selectedBranch = selectedBranch; } + public void setSelectedBranch(String selectedBranch) { + this.selectedBranch = selectedBranch; + } - public String getNewBranchName() { return newBranchName; } + public String getNewBranchName() { + return newBranchName; + } - public void setNewBranchName(String newBranchName) { this.newBranchName = newBranchName; } + public void setNewBranchName(String newBranchName) { + this.newBranchName = newBranchName; + } @Override public String execute() { @@ -85,19 +106,17 @@ public class CreateBranchAction extends AbstractScmWebEditorWithAddressAction { String repositoryUUID = getRepositoryUUID(); - Optional<UsernamePasswordToken> optionalAuthentication = initAuthenticationInformations(repositoryUUID, false, false); + Optional<ScmAuthentication> optionalAuthentication = initAuthenticationInformations(repositoryUUID, false, false); - boolean withAuthentication = optionalAuthentication.isPresent(); - - String username = withAuthentication ? optionalAuthentication.get().getUsername() : null; - String pw = withAuthentication ? new String(optionalAuthentication.get().getPassword()) : null; + //FIXME Need to have an authentication + Preconditions.checkState(optionalAuthentication.isPresent(), "Can't commit without authentication informations"); ScmProvider provider = getScmProvider(); String address = getAddress(); try { - branches = provider.listBranches(address, username, pw); + branches = provider.listBranches(address, optionalAuthentication); } catch (OperationNotSupportedException e) { if (log.isErrorEnabled()) { log.error("The SCM " + scmType + " does not support branches", e); @@ -115,10 +134,8 @@ public class CreateBranchAction extends AbstractScmWebEditorWithAddressAction { CreateBranchDto dto = new CreateBranchDto(); dto.setAddress(address); dto.setNewBranchName(newBranchName); - if (withAuthentication) { - dto.setUsername(username); - dto.setPassword(pw); - } + dto.setScmAuthentication(optionalAuthentication); + dto.setPathToLocalRepos(getPathToLocalRepos()); dto.setSelectedBranch(selectedBranch); 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 1ce08ed..ed19038 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 @@ -21,8 +21,8 @@ */ package org.nuiton.scmwebeditor.uiweb.actions; -import org.apache.shiro.authc.UsernamePasswordToken; import org.apache.struts2.interceptor.ServletRequestAware; +import org.nuiton.scmwebeditor.api.ScmAuthentication; import org.nuiton.scmwebeditor.api.ScmConnection; import org.nuiton.scmwebeditor.api.ScmFileManager; import org.nuiton.scmwebeditor.api.dto.CreateDirectoryDto; @@ -94,15 +94,12 @@ public class CreateDirectoryAction extends AbstractScmWebEditorWithAddressAction String repositoryUUID = getRepositoryUUID(); - Optional<UsernamePasswordToken> optionalAuthentication = initAuthenticationInformations(repositoryUUID, false, false); + Optional<ScmAuthentication> optionalAuthentication = initAuthenticationInformations(repositoryUUID, false, false); boolean withAuthentication = optionalAuthentication.isPresent(); CreateDirectoryDto dto = new CreateDirectoryDto(); - if (withAuthentication) { - dto.setUsername(optionalAuthentication.get().getUsername()); - dto.setPassword(new String(optionalAuthentication.get().getPassword())); - } + dto.setScmAuthentication(optionalAuthentication); dto.setDirectoryName(directoryName); dto.setParentDirectory(parentDirectory); @@ -117,6 +114,7 @@ public class CreateDirectoryAction extends AbstractScmWebEditorWithAddressAction } if (withAuthentication) { + // FIXME Why do we reset action fields ? resetAuthentication(); } diff --git a/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/DetectScmAction.java b/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/DetectScmAction.java index b0978cc..c1bcb0f 100644 --- a/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/DetectScmAction.java +++ b/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/DetectScmAction.java @@ -58,6 +58,7 @@ public class DetectScmAction extends AbstractScmWebEditorAction { */ public String execute() { + //FIXME Use a ServiceLoader List<String> supportedScms = Lists.newArrayList(ScmWebEditorConfig.getProviders().keySet()); for (String scm : supportedScms) { diff --git a/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/DownloadFileAction.java b/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/DownloadFileAction.java index 498cce9..4c7cbeb 100644 --- a/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/DownloadFileAction.java +++ b/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/DownloadFileAction.java @@ -23,7 +23,7 @@ package org.nuiton.scmwebeditor.uiweb.actions; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.apache.shiro.authc.UsernamePasswordToken; +import org.nuiton.scmwebeditor.api.ScmAuthentication; import org.nuiton.scmwebeditor.api.ScmConnection; import javax.servlet.ServletOutputStream; @@ -44,9 +44,13 @@ public class DownloadFileAction extends AbstractScmWebEditorWithAddressAction { /** the path to the root of the repository */ protected String repositoryRoot; - public String getRepositoryRoot() { return repositoryRoot; } + public String getRepositoryRoot() { + return repositoryRoot; + } - public void setRepositoryRoot(String repositoryRoot) { this.repositoryRoot = repositoryRoot; } + public void setRepositoryRoot(String repositoryRoot) { + this.repositoryRoot = repositoryRoot; + } @Override public String execute() { @@ -55,17 +59,12 @@ public class DownloadFileAction extends AbstractScmWebEditorWithAddressAction { String repositoryUUID = getRepositoryUUID(); - Optional<UsernamePasswordToken> optionalAuthentication = initAuthenticationInformations(repositoryUUID, false, false); - boolean withAuthentication = optionalAuthentication.isPresent(); - - String username = withAuthentication ? optionalAuthentication.get().getUsername() : null; - String pw = withAuthentication ? new String(optionalAuthentication.get().getPassword()) : null; + Optional<ScmAuthentication> optionalAuthentication = initAuthenticationInformations(repositoryUUID, false, false); ScmConnection scmConn = getScmConnection(); - // getting the path to use to download the file - String filePath = scmConn.getFilePath(getAddress(), repositoryRoot, username, pw); + String filePath = scmConn.getFilePath(getAddress(), repositoryRoot, optionalAuthentication); String filename = filePath.substring(filePath.lastIndexOf('/') + 1); File fileToDownload = new File(filePath); diff --git a/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/EditAction.java b/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/EditAction.java index 84e441c..e5db311 100644 --- a/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/EditAction.java +++ b/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/EditAction.java @@ -24,8 +24,8 @@ package org.nuiton.scmwebeditor.uiweb.actions; import org.apache.commons.io.FileUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.apache.shiro.authc.UsernamePasswordToken; import org.nuiton.scmwebeditor.api.OperationNotSupportedException; +import org.nuiton.scmwebeditor.api.ScmAuthentication; import org.nuiton.scmwebeditor.api.ScmConnection; import org.nuiton.scmwebeditor.api.ScmProvider; import org.nuiton.scmwebeditor.api.ScmRevision; @@ -76,45 +76,83 @@ public class EditAction extends ScmWebEditorMainAction { protected boolean fileDirectlyAccessible; - public String getCodeMirrorVersion() { return ScmWebEditorConfig.getCodeMirrorVersion(); } - - public String getSelectedBranch() { return selectedBranch; } + public String getCodeMirrorVersion() { + return ScmWebEditorConfig.getCodeMirrorVersion(); + } - public void setSelectedBranch(String selectedBranch) { this.selectedBranch = selectedBranch; } + public String getSelectedBranch() { + return selectedBranch; + } - public boolean isScmSupportsBranches() { return scmSupportsBranches; } + public void setSelectedBranch(String selectedBranch) { + this.selectedBranch = selectedBranch; + } - public void setScmSupportsBranches(boolean scmSupportsBranches) { this.scmSupportsBranches = scmSupportsBranches; } + public boolean isScmSupportsBranches() { + return scmSupportsBranches; + } - public boolean isScmSupportsPush() { return scmSupportsPush; } + public void setScmSupportsBranches(boolean scmSupportsBranches) { + this.scmSupportsBranches = scmSupportsBranches; + } + + public boolean isScmSupportsPush() { + return scmSupportsPush; + } - public void setScmSupportsPush(boolean scmSupportsPush) { this.scmSupportsPush = scmSupportsPush; } + public void setScmSupportsPush(boolean scmSupportsPush) { + this.scmSupportsPush = scmSupportsPush; + } - public String getRepositoryRoot() { return repositoryRoot; } + public String getRepositoryRoot() { + return repositoryRoot; + } - public void setRepositoryRoot(String repositoryRoot) { this.repositoryRoot = repositoryRoot; } + public void setRepositoryRoot(String repositoryRoot) { + this.repositoryRoot = repositoryRoot; + } - public int getAutoSaveInterval() { return autoSaveInterval; } + public int getAutoSaveInterval() { + return autoSaveInterval; + } - public void setAutoSaveInterval(int autoSaveInterval) { this.autoSaveInterval = autoSaveInterval; } + public void setAutoSaveInterval(int autoSaveInterval) { + this.autoSaveInterval = autoSaveInterval; + } - public Map<ScmRevision, String> getRevisions() { return revisions; } + public Map<ScmRevision, String> getRevisions() { + return revisions; + } public void setRevisions( - Map<ScmRevision, String> revisions) { this.revisions = revisions; } + Map<ScmRevision, String> revisions) { + this.revisions = revisions; + } - public String getRevision1() { return revision1; } + public String getRevision1() { + return revision1; + } - public void setRevision1(String revision1) { this.revision1 = revision1; } + public void setRevision1(String revision1) { + this.revision1 = revision1; + } - public String getRevision2() { return revision2; } + public String getRevision2() { + return revision2; + } - public void setRevision2(String revision2) { this.revision2 = revision2; } + public void setRevision2(String revision2) { + this.revision2 = revision2; + } - public boolean isFileDirectlyAccessible() { return fileDirectlyAccessible; } + public boolean isFileDirectlyAccessible() { + return fileDirectlyAccessible; + } public void setFileDirectlyAccessible( - boolean fileDirectlyAccessible) { this.fileDirectlyAccessible = fileDirectlyAccessible; } + boolean fileDirectlyAccessible) { + this.fileDirectlyAccessible = fileDirectlyAccessible; + } @Override public String execute() { @@ -137,7 +175,7 @@ public class EditAction extends ScmWebEditorMainAction { if ("vm".equals(format)) { // Try to get extension before vm one - fileName = fileName.substring(0, fileName.length()-3); + fileName = fileName.substring(0, fileName.length() - 3); format = fileName.substring(fileName.lastIndexOf(".") + 1); } @@ -154,26 +192,7 @@ public class EditAction extends ScmWebEditorMainAction { repositoryRoot = address.substring(0, address.lastIndexOf('/')); } - Optional<UsernamePasswordToken> optionalAuthentication = initAuthenticationInformations(repositoryUUID, false, false); - boolean withAuthentication = optionalAuthentication.isPresent(); - - String username = withAuthentication ? optionalAuthentication.get().getUsername() : null; - String pw = withAuthentication ? new String(optionalAuthentication.get().getPassword()) : null; - - if (log.isDebugEnabled()) { - log.debug("Login : " + username + " to repository: " + repositoryUUID); - } - - - String name = username; - String password = pw; - - if (name == null) { - name = "anonymous"; - } - if (password == null) { - password = "anonymous"; - } + Optional<ScmAuthentication> optionalAuthentication = initAuthenticationInformations(repositoryUUID, false, false); String changeBranchError = null; @@ -187,7 +206,7 @@ public class EditAction extends ScmWebEditorMainAction { selectedBranch = provider.getDefaultBranchName(); } - changeBranchError = provider.changeBranch(selectedBranch, pathToLocalRepos, username, pw); + changeBranchError = provider.changeBranch(selectedBranch, pathToLocalRepos, optionalAuthentication); } catch (OperationNotSupportedException e) { if (log.isErrorEnabled()) { log.error("Can not change branch with SCM '" + scmType + "'"); @@ -204,7 +223,7 @@ public class EditAction extends ScmWebEditorMainAction { } try { - revisions = scmConn.getRevisions(address, username, pw); + revisions = scmConn.getRevisions(address, optionalAuthentication); } catch (AuthenticationException e) { if (log.isErrorEnabled()) { log.error("Can not get revisions for address " + address + " : authentication error", e); @@ -217,9 +236,9 @@ public class EditAction extends ScmWebEditorMainAction { */ try { - File originalFile = scmConn.getFileContent(address, name, password); + File originalFile = scmConn.getFileContent(address, optionalAuthentication); originalText = FileUtils.readFileToString(originalFile); - numRevision = scmConn.getHeadRevisionNumber(address, name, password); + numRevision = scmConn.getHeadRevisionNumber(address, optionalAuthentication); } catch (AuthenticationException e) { request.setAttribute(PARAMETER_ADDRESS, address); @@ -287,7 +306,7 @@ public class EditAction extends ScmWebEditorMainAction { if (log.isInfoEnabled()) { log.info("IP client : " + request.getRemoteAddr() + ", get file : " + address + ". File's mimetype : " - + mimeType); + + mimeType); } return EDIT_PAGE; diff --git a/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/GetImageAction.java b/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/GetImageAction.java index e64daaa..99d6101 100644 --- a/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/GetImageAction.java +++ b/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/GetImageAction.java @@ -47,20 +47,26 @@ public class GetImageAction extends ScmWebEditorMainAction { /** the path to the image */ protected String imagePath; - public byte[] getImageInByte() { return imageInByte; } + public byte[] getImageInByte() { + return imageInByte; + } public void setImageInByte(byte[] imageInByte) { this.imageInByte = new byte[0]; - if (imageInByte != null){ + if (imageInByte != null) { this.imageInByte = Arrays.copyOf(imageInByte, imageInByte.length); } } - public String getImagePath() { return imagePath; } + public String getImagePath() { + return imagePath; + } - public void setImagePath(String imagePath) { this.imagePath = imagePath; } + public void setImagePath(String imagePath) { + this.imagePath = imagePath; + } @Override public String execute() { @@ -116,7 +122,7 @@ public class GetImageAction extends ScmWebEditorMainAction { public String getCustomContentType() { String type = "image/jpeg"; - String path = imagePath.toLowerCase(); + String path = imagePath.toLowerCase(); if (path.endsWith(".png")) { type = "image/png"; diff --git a/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/ListBranchesAction.java b/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/ListBranchesAction.java index e9e4540..18b3bdb 100644 --- a/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/ListBranchesAction.java +++ b/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/ListBranchesAction.java @@ -23,8 +23,8 @@ package org.nuiton.scmwebeditor.uiweb.actions; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.apache.shiro.authc.UsernamePasswordToken; import org.nuiton.scmwebeditor.api.OperationNotSupportedException; +import org.nuiton.scmwebeditor.api.ScmAuthentication; import org.nuiton.scmwebeditor.api.ScmProvider; import java.util.List; @@ -43,13 +43,21 @@ public class ListBranchesAction extends AbstractScmWebEditorWithAddressAction { /** list of the existing branches on the repository */ protected List<String> branches; - public boolean isScmSupportsBranches() { return scmSupportsBranches; } + public boolean isScmSupportsBranches() { + return scmSupportsBranches; + } - public void setScmSupportsBranches(boolean scmSupportsBranches) { this.scmSupportsBranches = scmSupportsBranches; } + public void setScmSupportsBranches(boolean scmSupportsBranches) { + this.scmSupportsBranches = scmSupportsBranches; + } - public List<String> getBranches() { return branches; } + public List<String> getBranches() { + return branches; + } - public void setBranches(List<String> branches) { this.branches = branches; } + public void setBranches(List<String> branches) { + this.branches = branches; + } @Override public String execute() { @@ -58,17 +66,14 @@ public class ListBranchesAction extends AbstractScmWebEditorWithAddressAction { String repositoryUUID = getRepositoryUUID(); - Optional<UsernamePasswordToken> optionalAuthentication = initAuthenticationInformations(repositoryUUID, false, false); - boolean withAuthentication = optionalAuthentication.isPresent(); - String username = withAuthentication ? optionalAuthentication.get().getUsername() : null; - String pw = withAuthentication ? new String(optionalAuthentication.get().getPassword()) : null; + Optional<ScmAuthentication> optionalAuthentication = initAuthenticationInformations(repositoryUUID, false, false); ScmProvider provider = getScmProvider(); scmSupportsBranches = provider.supportsBranches(); - + //FIXME Why do this call if we already know the result if provider does not support branches ? try { - branches = provider.listBranches(getAddress(), username, pw); + branches = provider.listBranches(getAddress(), optionalAuthentication); } catch (OperationNotSupportedException e) { if (log.isDebugEnabled()) { log.debug("The SCM " + scmType + " does not support branches", e); diff --git a/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/LogoutAction.java b/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/LogoutAction.java index a4e1d40..a1e5d7a 100644 --- a/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/LogoutAction.java +++ b/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/LogoutAction.java @@ -21,8 +21,6 @@ */ package org.nuiton.scmwebeditor.uiweb.actions; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; import org.nuiton.scmwebeditor.api.ScmConnection; import org.nuiton.scmwebeditor.api.ScmProvider; import org.nuiton.scmwebeditor.uiweb.ScmWebEditorConfig; @@ -36,21 +34,27 @@ public class LogoutAction extends AbstractScmWebEditorAction { private static final long serialVersionUID = 6937086747942656369L; - private static final Log log = LogFactory.getLog(LogoutAction.class); - /** the repository's address */ protected String address; /** the URL to the repository's root */ protected String projectUrl; - public void setAddress(String address) { this.address = address; } + public void setAddress(String address) { + this.address = address; + } - public String getAddress() { return address; } + public String getAddress() { + return address; + } - public String getProjectUrl() { return projectUrl; } + public String getProjectUrl() { + return projectUrl; + } - public void setProjectUrl(String projectUrl) { this.projectUrl = projectUrl; } + public void setProjectUrl(String projectUrl) { + this.projectUrl = projectUrl; + } @Override public String execute() { 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 b2ff290..98e635b 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 @@ -21,7 +21,7 @@ */ package org.nuiton.scmwebeditor.uiweb.actions; -import org.apache.shiro.authc.UsernamePasswordToken; +import org.nuiton.scmwebeditor.api.ScmAuthentication; import org.nuiton.scmwebeditor.api.ScmConnection; import org.nuiton.scmwebeditor.api.ScmFileManager; import org.nuiton.scmwebeditor.api.dto.MoveFileDto; @@ -71,28 +71,49 @@ public class MoveFileAction extends AbstractScmWebEditorWithAddressAction { return scmRoot; } - public void setScmRoot(String scmRoot) { this.scmRoot = scmRoot; } + public void setScmRoot(String scmRoot) { + this.scmRoot = scmRoot; + } - public void setBadLogin(boolean badLogin) { this.badLogin = badLogin; } + public void setBadLogin(boolean badLogin) { + this.badLogin = badLogin; + } - public void setError(boolean error) { this.error = error; } + public void setError(boolean error) { + this.error = error; + } - public String getScmPath() { return scmPath; } + public String getScmPath() { + return scmPath; + } - public void setScmPath(String scmPath) { this.scmPath = scmPath; } + public void setScmPath(String scmPath) { + this.scmPath = scmPath; + } - public String getFileRoot() { return fileRoot; } + public String getFileRoot() { + return fileRoot; + } - public void setFileRoot(String fileRoot) { this.fileRoot = fileRoot; } + public void setFileRoot(String fileRoot) { + this.fileRoot = fileRoot; + } - public String getFileToMove() { return fileToMove; } + public String getFileToMove() { + return fileToMove; + } - public void setFileToMove(String fileToMove) { this.fileToMove = fileToMove; } + public void setFileToMove(String fileToMove) { + this.fileToMove = fileToMove; + } - public String getDestinationDirectory() { return destinationDirectory; } + public String getDestinationDirectory() { + return destinationDirectory; + } - public void setDestinationDirectory( - String destinationDirectory) { this.destinationDirectory = destinationDirectory; } + public void setDestinationDirectory(String destinationDirectory) { + this.destinationDirectory = destinationDirectory; + } @Override public String execute() { @@ -101,14 +122,10 @@ public class MoveFileAction extends AbstractScmWebEditorWithAddressAction { String repositoryUUID = getRepositoryUUID(); - Optional<UsernamePasswordToken> optionalAuthentication = initAuthenticationInformations(repositoryUUID, false, false); - boolean withAuthentication = optionalAuthentication.isPresent(); - String username = withAuthentication ? optionalAuthentication.get().getUsername() : null; - String pw = withAuthentication ? new String(optionalAuthentication.get().getPassword()) : null; + Optional<ScmAuthentication> optionalAuthentication = initAuthenticationInformations(repositoryUUID, false, false); MoveFileDto dto = new MoveFileDto(); - dto.setUsername(username); - dto.setPassword(pw); + dto.setScmAuthentication(optionalAuthentication); dto.setScmPath(scmPath); dto.setFileToMove(fileToMove); dto.setDestinationDirectory(destinationDirectory); 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 0c7ab44..e43941c 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 @@ -21,7 +21,7 @@ */ package org.nuiton.scmwebeditor.uiweb.actions; -import org.apache.shiro.authc.UsernamePasswordToken; +import org.nuiton.scmwebeditor.api.ScmAuthentication; import org.nuiton.scmwebeditor.api.ScmConnection; import org.nuiton.scmwebeditor.api.ScmFileManager; import org.nuiton.scmwebeditor.api.dto.RemoveDirectoryDto; @@ -65,19 +65,33 @@ public class RemoveDirectoryAction extends AbstractScmWebEditorWithAddressAction return directoryToRemove; } - public void setDirectoryToRemove(String directoryToRemove) { this.directoryToRemove = directoryToRemove; } + public void setDirectoryToRemove(String directoryToRemove) { + this.directoryToRemove = directoryToRemove; + } - public void setBadLogin(boolean badLogin) { this.badLogin = badLogin; } + public void setBadLogin(boolean badLogin) { + this.badLogin = badLogin; + } - public void setError(boolean error) { this.error = error; } + public void setError(boolean error) { + this.error = error; + } - public String getFileRoot() { return fileRoot; } + public String getFileRoot() { + return fileRoot; + } - public void setFileRoot(String fileRoot) { this.fileRoot = fileRoot; } + public void setFileRoot(String fileRoot) { + this.fileRoot = fileRoot; + } - public String getScmRoot() { return scmRoot; } + public String getScmRoot() { + return scmRoot; + } - public void setScmRoot(String scmRoot) { this.scmRoot = scmRoot; } + public void setScmRoot(String scmRoot) { + this.scmRoot = scmRoot; + } @Override public String execute() { @@ -86,14 +100,10 @@ public class RemoveDirectoryAction extends AbstractScmWebEditorWithAddressAction String repositoryUUID = getRepositoryUUID(); - Optional<UsernamePasswordToken> optionalAuthentication = initAuthenticationInformations(repositoryUUID, false, false); - boolean withAuthentication = optionalAuthentication.isPresent(); - String username = withAuthentication ? optionalAuthentication.get().getUsername() : null; - String pw = withAuthentication ? new String(optionalAuthentication.get().getPassword()) : null; + Optional<ScmAuthentication> optionalAuthentication = initAuthenticationInformations(repositoryUUID, false, false); RemoveDirectoryDto dto = new RemoveDirectoryDto(); - dto.setUsername(username); - dto.setPassword(pw); + dto.setScmAuthentication(optionalAuthentication); dto.setDirectoryToRemove(directoryToRemove); ScmFileManager scmFileManager = getScmFileManager(); 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 08d4fef..74df353 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 @@ -21,7 +21,7 @@ */ package org.nuiton.scmwebeditor.uiweb.actions; -import org.apache.shiro.authc.UsernamePasswordToken; +import org.nuiton.scmwebeditor.api.ScmAuthentication; import org.nuiton.scmwebeditor.api.ScmConnection; import org.nuiton.scmwebeditor.api.ScmFileManager; import org.nuiton.scmwebeditor.api.dto.RemoveFileDto; @@ -65,19 +65,33 @@ public class RemoveFileAction extends AbstractScmWebEditorWithAddressAction { return scmRoot; } - public void setScmRoot(String scmRoot) { this.scmRoot = scmRoot; } + public void setScmRoot(String scmRoot) { + this.scmRoot = scmRoot; + } - public void setBadLogin(boolean badLogin) { this.badLogin = badLogin; } + public void setBadLogin(boolean badLogin) { + this.badLogin = badLogin; + } - public void setError(boolean error) { this.error = error; } + public void setError(boolean error) { + this.error = error; + } - public String getScmPath() { return scmPath; } + public String getScmPath() { + return scmPath; + } - public void setScmPath(String scmPath) { this.scmPath = scmPath; } + public void setScmPath(String scmPath) { + this.scmPath = scmPath; + } - public String getFileRoot() { return fileRoot; } + public String getFileRoot() { + return fileRoot; + } - public void setFileRoot(String fileRoot) { this.fileRoot = fileRoot; } + public void setFileRoot(String fileRoot) { + this.fileRoot = fileRoot; + } @Override public String execute() { @@ -86,14 +100,13 @@ public class RemoveFileAction extends AbstractScmWebEditorWithAddressAction { String repositoryUUID = getRepositoryUUID(); - Optional<UsernamePasswordToken> optionalAuthentication = initAuthenticationInformations(repositoryUUID, false, false); + Optional<ScmAuthentication> optionalAuthentication = initAuthenticationInformations(repositoryUUID, false, false); boolean withAuthentication = optionalAuthentication.isPresent(); String username = withAuthentication ? optionalAuthentication.get().getUsername() : null; String pw = withAuthentication ? new String(optionalAuthentication.get().getPassword()) : null; RemoveFileDto dto = new RemoveFileDto(); - dto.setUsername(username); - dto.setPassword(pw); + dto.setScmAuthentication(optionalAuthentication); dto.setScmPath(scmPath); ScmFileManager scmFileManager = getScmFileManager(); diff --git a/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/ResetAction.java b/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/ResetAction.java index 6f6fd5b..d9627ee 100644 --- a/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/ResetAction.java +++ b/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/ResetAction.java @@ -24,7 +24,7 @@ package org.nuiton.scmwebeditor.uiweb.actions; import org.apache.commons.io.FileUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.apache.shiro.authc.UsernamePasswordToken; +import org.nuiton.scmwebeditor.api.ScmAuthentication; import org.nuiton.scmwebeditor.api.ScmConnection; import javax.naming.AuthenticationException; @@ -50,11 +50,17 @@ public class ResetAction extends AbstractScmWebEditorWithAddressAction { /** information about an error if one occurs */ protected String error; - public String getLastRevision() { return lastRevision; } + public String getLastRevision() { + return lastRevision; + } - public String getNumRevision() { return numRevision; } + public String getNumRevision() { + return numRevision; + } - public String getError() { return error; } + public String getError() { + return error; + } @Override public String execute() { @@ -63,18 +69,15 @@ public class ResetAction extends AbstractScmWebEditorWithAddressAction { String repositoryUUID = getRepositoryUUID(); - Optional<UsernamePasswordToken> optionalAuthentication = initAuthenticationInformations(repositoryUUID, false, false); - boolean withAuthentication = optionalAuthentication.isPresent(); - String username = withAuthentication ? optionalAuthentication.get().getUsername() : null; - String pw = withAuthentication ? new String(optionalAuthentication.get().getPassword()) : null; + Optional<ScmAuthentication> optionalAuthentication = initAuthenticationInformations(repositoryUUID, false, false); ScmConnection scmConn = getScmConnection(); try { String address = getAddress(); - File originalFile = scmConn.getFileContent(address, username, pw); + File originalFile = scmConn.getFileContent(address, optionalAuthentication); lastRevision = FileUtils.readFileToString(originalFile); - numRevision = scmConn.getHeadRevisionNumber(address, username, pw); + numRevision = scmConn.getHeadRevisionNumber(address, optionalAuthentication); } catch (AuthenticationException e) { if (log.isErrorEnabled()) { log.error("AUTH FAIL"); diff --git a/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/ScmWebEditorCommitAction.java b/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/ScmWebEditorCommitAction.java index 33de9c1..a987967 100644 --- a/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/ScmWebEditorCommitAction.java +++ b/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/ScmWebEditorCommitAction.java @@ -23,10 +23,10 @@ package org.nuiton.scmwebeditor.uiweb.actions; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.apache.shiro.authc.UsernamePasswordToken; import org.dom4j.Document; import org.nuiton.jrst.JRST; import org.nuiton.jrst.legacy.JRSTReader; +import org.nuiton.scmwebeditor.api.ScmAuthentication; import org.nuiton.scmwebeditor.api.ScmConnection; import org.nuiton.scmwebeditor.api.dto.CommitDto; import org.nuiton.scmwebeditor.api.dto.result.CommitResultDto; @@ -87,66 +87,125 @@ public class ScmWebEditorCommitAction extends AbstractScmWebEditorWithAddressAct /** equals true if only a commit is requested, without a push */ protected boolean commitOnly; - public String getCommitMessage() { return commitMessage; } + public String getCommitMessage() { + return commitMessage; + } - public void setCommitMessage(String commitMessage) { this.commitMessage = commitMessage; } + public void setCommitMessage(String commitMessage) { + this.commitMessage = commitMessage; + } - public String getNewText() { return newText; } + public String getNewText() { + return newText; + } - public void setNewText(String newText) { this.newText = newText; } + public void setNewText(String newText) { + this.newText = newText; + } - public String getFormat() { return format; } + public String getFormat() { + return format; + } - public void setFormat(String format) { this.format = format; } + public void setFormat(String format) { + this.format = format; + } - public String getOrigText() { return origText; } + public String getOrigText() { + return origText; + } - public void setOrigText(String origText) { this.origText = origText; } + public void setOrigText(String origText) { + this.origText = origText; + } - public String getLastText() { return lastText; } + public String getLastText() { + return lastText; + } - public void setLastText(String lastText) { this.lastText = lastText; } + public void setLastText(String lastText) { + this.lastText = lastText; + } - public String getProjectUrl() { return projectUrl; } + public String getProjectUrl() { + return projectUrl; + } - public void setProjectUrl(String projectUrl) { this.projectUrl = projectUrl; } + public void setProjectUrl(String projectUrl) { + this.projectUrl = projectUrl; + } - public String getNumRevision() { return numRevision; } + public String getNumRevision() { + return numRevision; + } - public void setNumRevision(String numRevision) { this.numRevision = numRevision; } + public void setNumRevision(String numRevision) { + this.numRevision = numRevision; + } - public String getDiff() { return diff; } + public String getDiff() { + return diff; + } - public void setDiff(String diff) { this.diff = diff; } + public void setDiff(String diff) { + this.diff = diff; + } - public String getHeadCommiter() { return headCommiter; } + public String getHeadCommiter() { + return headCommiter; + } - public void setHeadCommiter(String headCommiter) { this.headCommiter = headCommiter; } + public void setHeadCommiter(String headCommiter) { + this.headCommiter = headCommiter; + } - public boolean isSaveCookie() { return saveCookie; } + public boolean isSaveCookie() { + return saveCookie; + } - public void setSaveCookie(boolean saveCookie) { this.saveCookie = saveCookie; } + public void setSaveCookie(boolean saveCookie) { + this.saveCookie = saveCookie; + } - public String getMimeType() { return mimeType; } + public String getMimeType() { + return mimeType; + } - public void setMimeType(String mimeType) { this.mimeType = mimeType; } + public void setMimeType(String mimeType) { + this.mimeType = mimeType; + } - public boolean getForce() { return force; } + public boolean getForce() { + return force; + } - public void setForce(boolean force) { this.force = force; } + public void setForce(boolean force) { + this.force = force; + } - public String getParameterAddress() { return PARAMETER_ADDRESS; } + public String getParameterAddress() { + return PARAMETER_ADDRESS; + } - public boolean isBadLogin() { return badLogin; } + public boolean isBadLogin() { + return badLogin; + } - public void setBadLogin(boolean badLogin) { this.badLogin = badLogin; } + public void setBadLogin(boolean badLogin) { + this.badLogin = badLogin; + } - public boolean isCommitOnly() { return commitOnly; } + public boolean isCommitOnly() { + return commitOnly; + } - public void setCommitOnly(boolean commitOnly) { this.commitOnly = commitOnly; } + public void setCommitOnly(boolean commitOnly) { + this.commitOnly = commitOnly; + } /** * Tells whether a RST file has a valid syntax + * * @param rstContent the text to check * @return true if the syntax is valid */ @@ -189,19 +248,10 @@ public class ScmWebEditorCommitAction extends AbstractScmWebEditorWithAddressAct // if the repository is not protected for writing, we get its UUID String repositoryUUID = getRepositoryUUID(); - Optional<UsernamePasswordToken> optionalAuthentication = initAuthenticationInformations(repositoryUUID, true, saveCookie); - boolean withAuthentication = optionalAuthentication.isPresent(); - String username = withAuthentication ? optionalAuthentication.get().getUsername() : null; - String pw = withAuthentication ? new String(optionalAuthentication.get().getPassword()) : null; - - if (log.isDebugEnabled()) { - log.debug("Login : " + username + " to repository: " + repositoryUUID); - } - + Optional<ScmAuthentication> optionalAuthentication = initAuthenticationInformations(repositoryUUID, true, saveCookie); CommitDto dto = new CommitDto(); - dto.setUsername(username); - dto.setPassword(pw); + dto.setScmAuthentication(optionalAuthentication); dto.setNewText(newText); dto.setCommitMessage(commitMessage); dto.setForce(force); @@ -236,8 +286,7 @@ public class ScmWebEditorCommitAction extends AbstractScmWebEditorWithAddressAct } else if (error.equals(CommitResultDto.AUTH_ERROR)) { request.setAttribute(getParameterAddress(), getAddress()); getScmSession().delScmUser(scmConn.getRepositoryId()); - username = null; - pw = null; + resetAuthentication(); return LOGIN; } else if (error.equals(CommitResultDto.FILE_MODIFY)) { diff --git a/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/ScmWebEditorMainAction.java b/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/ScmWebEditorMainAction.java index 7943130..37b99d3 100644 --- a/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/ScmWebEditorMainAction.java +++ b/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/ScmWebEditorMainAction.java @@ -119,11 +119,7 @@ public class ScmWebEditorMainAction extends AbstractScmWebEditorWithAddressActio * @return true if the parameters are empty */ private boolean testParameters() { - if (Strings.isNullOrEmpty(getAddress())) { - return true; - } else { - return false; - } + return Strings.isNullOrEmpty(getAddress()); } @Override 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 08ebc59..fa08a05 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 @@ -21,7 +21,7 @@ */ package org.nuiton.scmwebeditor.uiweb.actions; -import org.apache.shiro.authc.UsernamePasswordToken; +import org.nuiton.scmwebeditor.api.ScmAuthentication; import org.nuiton.scmwebeditor.api.ScmConnection; import org.nuiton.scmwebeditor.api.dto.UploadFileDto; import org.nuiton.scmwebeditor.api.dto.result.UploadFileResultDto; @@ -62,7 +62,6 @@ public class UploadFileAction extends AbstractScmWebEditorWithAddressAction { /** equals true if an error occurs */ protected boolean error; - public File getUpload() { return upload; } @@ -103,17 +102,29 @@ public class UploadFileAction extends AbstractScmWebEditorWithAddressAction { return fileRoot; } - public void setScmRoot(String scmRoot) { this.scmRoot = scmRoot; } + public void setScmRoot(String scmRoot) { + this.scmRoot = scmRoot; + } - public void setFileRoot(String fileRoot) { this.fileRoot = fileRoot; } + public void setFileRoot(String fileRoot) { + this.fileRoot = fileRoot; + } - public void setBadLogin(boolean badLogin) { this.badLogin = badLogin; } + public void setBadLogin(boolean badLogin) { + this.badLogin = badLogin; + } - public void setError(boolean error) { this.error = error; } + public void setError(boolean error) { + this.error = error; + } - public void setScmPath(String scmPath) { this.scmPath = scmPath; } + public void setScmPath(String scmPath) { + this.scmPath = scmPath; + } - public String getScmPath() { return scmPath; } + public String getScmPath() { + return scmPath; + } @Override public String execute() { @@ -122,14 +133,10 @@ public class UploadFileAction extends AbstractScmWebEditorWithAddressAction { String repositoryUUID = getRepositoryUUID(); - Optional<UsernamePasswordToken> optionalAuthentication = initAuthenticationInformations(repositoryUUID, false, false); - boolean withAuthentication = optionalAuthentication.isPresent(); - String username = withAuthentication ? optionalAuthentication.get().getUsername() : null; - String pw = withAuthentication ? new String(optionalAuthentication.get().getPassword()) : null; + Optional<ScmAuthentication> optionalAuthentication = initAuthenticationInformations(repositoryUUID, false, false); UploadFileDto dto = new UploadFileDto(); - dto.setUsername(username); - dto.setPassword(pw); + dto.setScmAuthentication(optionalAuthentication); dto.setUpload(upload); dto.setUploadFileName(uploadFileName); dto.setUploadContentType(uploadContentType); diff --git a/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/ViewDiffsAction.java b/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/ViewDiffsAction.java index 4ba180a..3f75140 100644 --- a/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/ViewDiffsAction.java +++ b/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/ViewDiffsAction.java @@ -24,7 +24,7 @@ package org.nuiton.scmwebeditor.uiweb.actions; import org.apache.commons.io.FileUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.apache.shiro.authc.UsernamePasswordToken; +import org.nuiton.scmwebeditor.api.ScmAuthentication; import org.nuiton.scmwebeditor.api.ScmConnection; import javax.naming.AuthenticationException; @@ -52,21 +52,37 @@ public class ViewDiffsAction extends ScmWebEditorMainAction { protected String error; - public String getFileContent() { return fileContent; } + public String getFileContent() { + return fileContent; + } - public void setFileContent(String fileContent) { this.fileContent = fileContent; } + public void setFileContent(String fileContent) { + this.fileContent = fileContent; + } - public String getRevision1() { return revision1; } + public String getRevision1() { + return revision1; + } - public void setRevision1(String revision1) { this.revision1 = revision1; } + public void setRevision1(String revision1) { + this.revision1 = revision1; + } - public String getRevision2() { return revision2; } + public String getRevision2() { + return revision2; + } - public void setRevision2(String revision2) { this.revision2 = revision2; } + public void setRevision2(String revision2) { + this.revision2 = revision2; + } - public String getError() { return error; } + public String getError() { + return error; + } - public void setError(String error) { this.error = error; } + public void setError(String error) { + this.error = error; + } @Override public String execute() { @@ -75,26 +91,7 @@ public class ViewDiffsAction extends ScmWebEditorMainAction { String repositoryUUID = getRepositoryUUID(); - Optional<UsernamePasswordToken> optionalAuthentication = initAuthenticationInformations(repositoryUUID, true, saveCookie); - boolean withAuthentication = optionalAuthentication.isPresent(); - String username = withAuthentication ? optionalAuthentication.get().getUsername() : null; - String pw = withAuthentication ? new String(optionalAuthentication.get().getPassword()) : null; - - if (log.isDebugEnabled()) { - log.debug("Login : " + username + " to repository: " + repositoryUUID); - } - - initAuthenticationInformations(repositoryUUID, true, saveCookie); - - String name = username; - String password = pw; - - if (name == null) { - name = "anonymous"; - } - if (password == null) { - password = "anonymous"; - } + Optional<ScmAuthentication> optionalAuthentication = initAuthenticationInformations(repositoryUUID, true, saveCookie); String address = getAddress(); @@ -105,7 +102,7 @@ public class ViewDiffsAction extends ScmWebEditorMainAction { try { ScmConnection scmConn = getScmConnection(); - File tempFile = scmConn.getDiffs(address, name, password, revision1, revision2); + File tempFile = scmConn.getDiffs(address, optionalAuthentication, revision1, revision2); if (tempFile != null) { fileContent = FileUtils.readFileToString(tempFile); @@ -121,10 +118,8 @@ public class ViewDiffsAction extends ScmWebEditorMainAction { log.debug("Auth Fail ", e); } - // deleting the cookies for this repository - removeRepositoryCookie(repositoryUUID); + removeRepositoryFromCache(repositoryUUID); - getScmSession().delScmUser(repositoryUUID); error = LOGIN; return LOGIN; } catch (IOException e) { diff --git a/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/ViewImageAction.java b/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/ViewImageAction.java index 8dbf5c7..ac97a57 100644 --- a/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/ViewImageAction.java +++ b/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/ViewImageAction.java @@ -23,8 +23,8 @@ package org.nuiton.scmwebeditor.uiweb.actions; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.apache.shiro.authc.UsernamePasswordToken; import org.nuiton.scmwebeditor.api.OperationNotSupportedException; +import org.nuiton.scmwebeditor.api.ScmAuthentication; import org.nuiton.scmwebeditor.api.ScmConnection; import org.nuiton.scmwebeditor.api.ScmProvider; import org.nuiton.scmwebeditor.api.dto.result.AbstractResultDto; @@ -61,26 +61,46 @@ public class ViewImageAction extends ScmWebEditorMainAction { protected boolean filesDirectlyAccessible; - public String getSelectedBranch() { return selectedBranch; } + public String getSelectedBranch() { + return selectedBranch; + } - public void setSelectedBranch(String selectedBranch) { this.selectedBranch = selectedBranch; } + public void setSelectedBranch(String selectedBranch) { + this.selectedBranch = selectedBranch; + } - public boolean isScmSupportsBranches() { return scmSupportsBranches; } + public boolean isScmSupportsBranches() { + return scmSupportsBranches; + } - public void setScmSupportsBranches(boolean scmSupportsBranches) { this.scmSupportsBranches = scmSupportsBranches; } + public void setScmSupportsBranches(boolean scmSupportsBranches) { + this.scmSupportsBranches = scmSupportsBranches; + } - public String getRepositoryRoot() { return repositoryRoot; } + public String getRepositoryRoot() { + return repositoryRoot; + } - public void setRepositoryRoot(String repositoryRoot) { this.repositoryRoot = repositoryRoot; } + public void setRepositoryRoot(String repositoryRoot) { + this.repositoryRoot = repositoryRoot; + } - public String getImagePath() { return imagePath; } + public String getImagePath() { + return imagePath; + } - public void setImagePath(String imagePath) { this.imagePath = imagePath; } + public void setImagePath(String imagePath) { + this.imagePath = imagePath; + } - public boolean isFilesDirectlyAccessible() { return filesDirectlyAccessible; } + public boolean isFilesDirectlyAccessible() { + return filesDirectlyAccessible; + } public void setFilesDirectlyAccessible( - boolean filesDirectlyAccessible) { this.filesDirectlyAccessible = filesDirectlyAccessible; } + boolean filesDirectlyAccessible) { + this.filesDirectlyAccessible = filesDirectlyAccessible; + } @Override public String execute() { @@ -108,32 +128,14 @@ public class ViewImageAction extends ScmWebEditorMainAction { repositoryRoot = address.substring(0, address.lastIndexOf('/')); } - // authentication - Optional<UsernamePasswordToken> optionalAuthentication = initAuthenticationInformations(repositoryUUID, true, saveCookie); - boolean withAuthentication = optionalAuthentication.isPresent(); - String username = withAuthentication ? optionalAuthentication.get().getUsername() : null; - String pw = withAuthentication ? new String(optionalAuthentication.get().getPassword()) : null; - - if (log.isDebugEnabled()) { - log.debug("Login : " + username + " to " + repositoryUUID); - } - - String name = username; - String password = pw; - - if (name == null) { - name = "anonymous"; - } - if (password == null) { - password = "anonymous"; - } + Optional<ScmAuthentication> optionalAuthentication = initAuthenticationInformations(repositoryUUID, true, saveCookie); String changeBranchError = null; if (scmSupportsBranches) { try { - changeBranchError = provider.changeBranch(selectedBranch, pathToLocalRepos, username, pw); + changeBranchError = provider.changeBranch(selectedBranch, pathToLocalRepos, optionalAuthentication); } catch (OperationNotSupportedException e) { if (log.isErrorEnabled()) { log.error("Can not change branch with SCM '" + scmType + "'"); @@ -157,7 +159,7 @@ public class ViewImageAction extends ScmWebEditorMainAction { ScmConnection scmConn = provider.getConnection(address, pathToLocalRepos); try { - numRevision = scmConn.getHeadRevisionNumber(address, name, password); + numRevision = scmConn.getHeadRevisionNumber(address, optionalAuthentication); } catch (AuthenticationException e) { request.setAttribute(PARAMETER_ADDRESS, address); @@ -177,11 +179,10 @@ public class ViewImageAction extends ScmWebEditorMainAction { return ERROR_PATH; } - imagePath = scmConn.getFilePath(address, repositoryRoot, username, pw); + imagePath = scmConn.getFilePath(address, repositoryRoot, optionalAuthentication); if (log.isInfoEnabled()) { - log.info("IP client : " + request.getRemoteAddr() + " , get file : " + address + ". File's mimetype : " - + mimeType); + log.info("IP client : " + request.getRemoteAddr() + " , get file : " + address + ". File's mimetype : " + mimeType); } return VIEW_IMAGE; diff --git a/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/ViewRevisionAction.java b/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/ViewRevisionAction.java index 1c19d6b..2fb29f4 100644 --- a/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/ViewRevisionAction.java +++ b/swe-ui-web/src/main/java/org/nuiton/scmwebeditor/uiweb/actions/ViewRevisionAction.java @@ -24,7 +24,7 @@ package org.nuiton.scmwebeditor.uiweb.actions; import org.apache.commons.io.FileUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.apache.shiro.authc.UsernamePasswordToken; +import org.nuiton.scmwebeditor.api.ScmAuthentication; import org.nuiton.scmwebeditor.api.ScmConnection; import javax.naming.AuthenticationException; @@ -54,26 +54,45 @@ public class ViewRevisionAction extends ScmWebEditorMainAction { /** equals 1 to view the revision1 and equals 2 to view the revision2 */ protected int rev; + public String getFileContent() { + return fileContent; + } - public String getFileContent() { return fileContent; } - - public void setFileContent(String fileContent) { this.fileContent = fileContent; } + public void setFileContent(String fileContent) { + this.fileContent = fileContent; + } - public String getRevision1() { return revision1; } + public String getRevision1() { + return revision1; + } - public void setRevision1(String revision1) { this.revision1 = revision1; } + public void setRevision1(String revision1) { + this.revision1 = revision1; + } - public String getRevision2() { return revision2; } + public String getRevision2() { + return revision2; + } - public void setRevision2(String revision2) { this.revision2 = revision2; } + public void setRevision2(String revision2) { + this.revision2 = revision2; + } - public int getRev() { return rev; } + public int getRev() { + return rev; + } - public void setRev(int rev) { this.rev = rev; } + public void setRev(int rev) { + this.rev = rev; + } - public String getError() { return error; } + public String getError() { + return error; + } - public void setError(String error) { this.error = error; } + public void setError(String error) { + this.error = error; + } @Override public String execute() { @@ -85,26 +104,7 @@ public class ViewRevisionAction extends ScmWebEditorMainAction { // if the repository is not protected, we get its UUID String repositoryUUID = getRepositoryUUID(); - Optional<UsernamePasswordToken> optionalAuthentication = initAuthenticationInformations(repositoryUUID, true, saveCookie); - boolean withAuthentication = optionalAuthentication.isPresent(); - String username = withAuthentication ? optionalAuthentication.get().getUsername() : null; - String pw = withAuthentication ? new String(optionalAuthentication.get().getPassword()) : null; - - if (log.isDebugEnabled()) { - log.debug("Login : " + username + " to repository: " + repositoryUUID); - } - - - String name = username; - String password = pw; - - if (name == null) { - name = "anonymous"; - } - if (password == null) { - password = "anonymous"; - } - + Optional<ScmAuthentication> optionalAuthentication = initAuthenticationInformations(repositoryUUID, true, saveCookie); /* * Getting the file's revision @@ -118,7 +118,7 @@ public class ViewRevisionAction extends ScmWebEditorMainAction { revision = revision2; } - File tempFile = scmConn.getFileContentAtRevision(address, name, password, revision); + File tempFile = scmConn.getFileContentAtRevision(address, optionalAuthentication, revision); if (tempFile != null) { fileContent = FileUtils.readFileToString(tempFile); -- To stop receiving notification emails like this one, please contact nuiton.org SCM administrator <admin+scm@nuiton.org>.
participants (1)
-
nuiton.org scm