This is an automated email from the git hooks/post-receive script. New commit to branch feature/GIT in repository scmwebeditor. See http://git.nuiton.org/scmwebeditor.git commit cbd8b5dbc57f011d15a046485e13750edd11da1f Author: Hugo PIGEON <hpigeon@codelutin.com> Date: Mon May 11 11:59:28 2015 +0200 Store the file to edit's content in a file instead of a String for SVN repositories --- .../org/nuiton/scmwebeditor/GitConnection.java | 67 +++++++++++++--------- .../org/nuiton/scmwebeditor/ScmConnection.java | 3 +- .../org/nuiton/scmwebeditor/SvnConnection.java | 41 +++++++++++-- .../java/org/nuiton/scmwebeditor/SvnProvider.java | 2 +- .../nuiton/scmwebeditor/actions/EditAction.java | 9 ++- .../nuiton/scmwebeditor/actions/ResetAction.java | 10 +++- src/main/webapp/css/main.css | 4 +- src/main/webapp/js/preview.js | 2 + 8 files changed, 99 insertions(+), 39 deletions(-) diff --git a/src/main/java/org/nuiton/scmwebeditor/GitConnection.java b/src/main/java/org/nuiton/scmwebeditor/GitConnection.java index 793e19e..b2acf67 100644 --- a/src/main/java/org/nuiton/scmwebeditor/GitConnection.java +++ b/src/main/java/org/nuiton/scmwebeditor/GitConnection.java @@ -628,7 +628,7 @@ public class GitConnection implements ScmConnection { @Override - public String getFileContent(String path, String username, String password) throws AuthenticationException { + public File getFileContent(String path, String username, String password) throws AuthenticationException { try { cloneRepository(username, password); @@ -646,16 +646,7 @@ public class GitConnection implements ScmConnection { File fileToEdit = new File(localDirectory.getAbsolutePath() + "/" + fileName); - String origText = null; - try { - origText = FileUtils.readFileToString(fileToEdit); - } catch (IOException e) { - if (log.isErrorEnabled()) { - log.error("Can not read file " + fileToEdit, e); - } - } - - return origText; + return fileToEdit; } @Override @@ -739,24 +730,10 @@ public class GitConnection implements ScmConnection { String localReposPath = ScmWebEditorConfig.getLocalRepositoriesPath(); String hashedAddress = addressGit; - try { - MessageDigest md = MessageDigest.getInstance("SHA-512"); - md.update(addressGit.getBytes()); - byte byteData[] = md.digest(); - - StringBuilder sb = new StringBuilder(); - for (byte aByteData : byteData) { - String hexByte = Integer.toString((aByteData & 0xff) + 0x100, 16); - hexByte = hexByte.substring(1); - sb.append(hexByte); - } + String hashResult = hash(addressGit, "SHA-512"); - hashedAddress = sb.toString(); - if (log.isDebugEnabled()) { - log.debug("hashed address " + addressGit + " : " + hashedAddress); - } - } catch (NoSuchAlgorithmException e) { - log.error("Can not hash the repository address : the algorithm does not exist", e); + if (hashResult != null) { + hashedAddress = hashResult; } localDirectory = new File(localReposPath + "/" + sessionId + "/" + hashedAddress); @@ -861,4 +838,38 @@ public class GitConnection implements ScmConnection { } } } + + + /** + * Hashes a String with then given algorithms + * @param toHash the String to hash + * @param algorithm the algorithm to use to hash the String + * @return the hashed String + */ + private String hash(String toHash, String algorithm) { + + String hashed = null; + + try { + MessageDigest md = MessageDigest.getInstance(algorithm); + md.update(toHash.getBytes()); + byte byteData[] = md.digest(); + + StringBuilder sb = new StringBuilder(); + for (byte aByteData : byteData) { + String hexByte = Integer.toString((aByteData & 0xff) + 0x100, 16); + hexByte = hexByte.substring(1); + sb.append(hexByte); + } + + hashed = sb.toString(); + if (log.isDebugEnabled()) { + log.debug("hashed " + toHash + " : " + hashed); + } + } catch (NoSuchAlgorithmException e) { + log.error("Can not hash " + toHash + " : the algorithm " + algorithm + " does not exist", e); + } + + return hashed; + } } \ No newline at end of file diff --git a/src/main/java/org/nuiton/scmwebeditor/ScmConnection.java b/src/main/java/org/nuiton/scmwebeditor/ScmConnection.java index 3c4fd3f..d148a85 100644 --- a/src/main/java/org/nuiton/scmwebeditor/ScmConnection.java +++ b/src/main/java/org/nuiton/scmwebeditor/ScmConnection.java @@ -24,6 +24,7 @@ package org.nuiton.scmwebeditor; import org.nuiton.scmwebeditor.dto.*; import javax.naming.AuthenticationException; +import java.io.File; /** * An interface which the SCM classes have to implement @@ -61,7 +62,7 @@ public interface ScmConnection { * @param password the user's password for the SCM * @return a String which contains the file's content */ - String getFileContent(String path, String username, String password) throws AuthenticationException; + File getFileContent(String path, String username, String password) throws AuthenticationException; /** diff --git a/src/main/java/org/nuiton/scmwebeditor/SvnConnection.java b/src/main/java/org/nuiton/scmwebeditor/SvnConnection.java index 677c8e3..a973ee4 100644 --- a/src/main/java/org/nuiton/scmwebeditor/SvnConnection.java +++ b/src/main/java/org/nuiton/scmwebeditor/SvnConnection.java @@ -66,6 +66,9 @@ public class SvnConnection implements ScmConnection { /** svn default option */ protected DefaultSVNOptions svnOption; + /** the id of the HTTP session */ + protected String sessionId; + /** * */ @@ -74,7 +77,7 @@ public class SvnConnection implements ScmConnection { public File getCheckoutdir() { return checkoutdir; } - public SvnConnection(String address) throws SVNException { + public SvnConnection(String address, String sessionId) throws SVNException { if(log.isDebugEnabled()) { log.debug("SVN repository"); @@ -92,6 +95,8 @@ public class SvnConnection implements ScmConnection { svnOption = SVNWCUtil.createDefaultOptions(false); svnOption.setPropertyValue(SVNProperty.EOL_STYLE, SVNProperty.EOL_STYLE_LF); manager = SVNClientManager.newInstance(svnOption, authManager); + + this.sessionId = sessionId; } @@ -610,14 +615,27 @@ public class SvnConnection implements ScmConnection { @Override - public String getFileContent(String path, String username, String password) throws AuthenticationException { + public File getFileContent(String path, String username, String password) throws AuthenticationException { String url = path.substring(0, path.lastIndexOf("/")); String file = path.substring(path.lastIndexOf("/") + 1); + // storing the file content to the user's local directory + File localDirectory = new File(ScmWebEditorConfig.getLocalRepositoriesPath() + "/" + sessionId); + + if (!localDirectory.exists()) { + localDirectory.mkdir(); + } + + String tempFileName = localDirectory.getAbsolutePath() + "/" + file; + File tempFile = new File(tempFileName); + + if(tempFile.exists()) { + tempFile.delete(); + } + updateAuthentication(username, password); - String lastRevision = null; SVNRepository repository; try { @@ -642,13 +660,26 @@ public class SvnConnection implements ScmConnection { } + // writing the result to a temp file ByteArrayOutputStream baos = new ByteArrayOutputStream(); SVNProperties fileProperties = new SVNProperties(); repository.getFile(file, -1, fileProperties, baos); fileProperties.getStringValue(SVNProperty.REVISION); - lastRevision = baos.toString(); + try { + OutputStream fileOutput = new FileOutputStream(tempFileName); + baos.writeTo(fileOutput); + } catch (FileNotFoundException e) { + if (log.isErrorEnabled()) { + log.error("Can not find file " + tempFileName, e); + } + } catch (IOException e) { + if (log.isErrorEnabled()) { + log.error("Can not write to file " + tempFileName, e); + } + } + try { baos.close(); @@ -665,7 +696,7 @@ public class SvnConnection implements ScmConnection { } } - return lastRevision; + return tempFile; } diff --git a/src/main/java/org/nuiton/scmwebeditor/SvnProvider.java b/src/main/java/org/nuiton/scmwebeditor/SvnProvider.java index 1fcd2ca..94af21f 100644 --- a/src/main/java/org/nuiton/scmwebeditor/SvnProvider.java +++ b/src/main/java/org/nuiton/scmwebeditor/SvnProvider.java @@ -57,7 +57,7 @@ public class SvnProvider implements ScmProvider { SvnConnection svnConn = null; try { - svnConn = new SvnConnection(address); + svnConn = new SvnConnection(address, sessionId); } catch (SVNException e) { if (log.isErrorEnabled()) { log.error("Can not connect to SVN repository at " + address, e); diff --git a/src/main/java/org/nuiton/scmwebeditor/actions/EditAction.java b/src/main/java/org/nuiton/scmwebeditor/actions/EditAction.java index 3800fa4..101feac 100644 --- a/src/main/java/org/nuiton/scmwebeditor/actions/EditAction.java +++ b/src/main/java/org/nuiton/scmwebeditor/actions/EditAction.java @@ -1,6 +1,7 @@ package org.nuiton.scmwebeditor.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; @@ -12,6 +13,7 @@ import org.nuiton.scmwebeditor.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.util.LinkedList; @@ -127,7 +129,8 @@ public class EditAction extends ScmWebEditorMainAction { */ try { - originalText = scmConn.getFileContent(address, name, password); + File originalFile = scmConn.getFileContent(address, name, password); + originalText = FileUtils.readFileToString(originalFile); numRevision = scmConn.getHeadRevisionNumber(address, name, password); } catch (AuthenticationException e) { request.setAttribute(PARAMETER_ADDRESS, address); @@ -151,6 +154,10 @@ public class EditAction extends ScmWebEditorMainAction { getScmSession().delScmUser(repositoryUUID); //redirect to a login page return LOGIN; + } catch (IOException e) { + if (log.isErrorEnabled()) { + log.error("Can not read content file", e); + } } mimeType = null; diff --git a/src/main/java/org/nuiton/scmwebeditor/actions/ResetAction.java b/src/main/java/org/nuiton/scmwebeditor/actions/ResetAction.java index e1761e6..4bb788a 100644 --- a/src/main/java/org/nuiton/scmwebeditor/actions/ResetAction.java +++ b/src/main/java/org/nuiton/scmwebeditor/actions/ResetAction.java @@ -21,12 +21,15 @@ */ package org.nuiton.scmwebeditor.actions; +import org.apache.commons.io.FileUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.nuiton.scmwebeditor.ScmConnection; import org.nuiton.scmwebeditor.ScmProvider; import javax.naming.AuthenticationException; +import java.io.File; +import java.io.IOException; public class ResetAction extends AbstractScmWebEditorAction { @@ -96,7 +99,8 @@ public class ResetAction extends AbstractScmWebEditorAction { } try { - lastRevision = scmConn.getFileContent(address, username, pw); + File originalFile = scmConn.getFileContent(address, username, pw); + lastRevision = FileUtils.readFileToString(originalFile); numRevision = scmConn.getHeadRevisionNumber(address, username, pw); } catch (AuthenticationException e) { if (log.isErrorEnabled()) { @@ -104,6 +108,10 @@ public class ResetAction extends AbstractScmWebEditorAction { } error = AUTH_ERROR; return AUTH_ERROR; + } catch (IOException e) { + if (log.isErrorEnabled()) { + log.error("Can not read content file", e); + } } return SUCCESS; diff --git a/src/main/webapp/css/main.css b/src/main/webapp/css/main.css index 48f45b3..a2ee71d 100644 --- a/src/main/webapp/css/main.css +++ b/src/main/webapp/css/main.css @@ -23,7 +23,7 @@ body { font-family: Verdana,Arial,Helvetica,sans-serif; background-color: #b8b1b1; - width: 80%; + width: 90%; margin:auto; } @@ -181,7 +181,7 @@ li { background-repeat:no-repeat; width:468px; height:190px; - right: 8%; + right: 4%; top: 0; } diff --git a/src/main/webapp/js/preview.js b/src/main/webapp/js/preview.js index a942b5d..b809caa 100644 --- a/src/main/webapp/js/preview.js +++ b/src/main/webapp/js/preview.js @@ -65,9 +65,11 @@ $(document).ready(function() { if (selectedPos == "side") { $editor.css("width", "49%"); $preview.css("width", "49%"); + $preview.css("margin-top", "0px"); } else { $editor.css("width", "100%"); $preview.css("width", "100%"); + $preview.css("margin-top", "20px"); } } }); -- To stop receiving notification emails like this one, please contact nuiton.org SCM administrator <admin+scm@nuiton.org>.