Author: kcardineaud Date: 2011-07-07 16:00:26 +0200 (Thu, 07 Jul 2011) New Revision: 165 Url: http://nuiton.org/repositories/revision/scmwebeditor/165 Log: ScmwebEditor use stream to get the file content to edit, faster than checkout the directory Modified: trunk/src/main/java/org/nuiton/scmwebeditor/ScmWebEditorBaseAction.java trunk/src/main/java/org/nuiton/scmwebeditor/actions/ScmWebEditorMainAction.java trunk/src/main/webapp/ModificationViewer.jsp Modified: trunk/src/main/java/org/nuiton/scmwebeditor/ScmWebEditorBaseAction.java =================================================================== --- trunk/src/main/java/org/nuiton/scmwebeditor/ScmWebEditorBaseAction.java 2011-07-07 10:31:09 UTC (rev 164) +++ trunk/src/main/java/org/nuiton/scmwebeditor/ScmWebEditorBaseAction.java 2011-07-07 14:00:26 UTC (rev 165) @@ -236,7 +236,7 @@ protected String getMineType(File file) throws IOException, SAXException, TikaException { - FileInputStream is = new FileInputStream(file); + InputStream is = new FileInputStream(file); try { BodyContentHandler contenthandler = new BodyContentHandler(); Metadata metadata = new Metadata(); @@ -251,5 +251,22 @@ is.close(); } } + + protected String getMineType(String content, String filename) throws IOException, SAXException, TikaException { + InputStream is = new ByteArrayInputStream(content.getBytes()); + try { + BodyContentHandler contenthandler = new BodyContentHandler(); + Metadata metadata = new Metadata(); + metadata.set(Metadata.RESOURCE_NAME_KEY, filename); + AutoDetectParser parser = new AutoDetectParser(); + parser.parse(is, contenthandler, metadata); + String result = metadata.get(Metadata.CONTENT_TYPE); + log.info("Mine type of " + filename + " is : " + result); + return result; + } + finally { + is.close(); + } + } } Modified: trunk/src/main/java/org/nuiton/scmwebeditor/actions/ScmWebEditorMainAction.java =================================================================== --- trunk/src/main/java/org/nuiton/scmwebeditor/actions/ScmWebEditorMainAction.java 2011-07-07 10:31:09 UTC (rev 164) +++ trunk/src/main/java/org/nuiton/scmwebeditor/actions/ScmWebEditorMainAction.java 2011-07-07 14:00:26 UTC (rev 165) @@ -1,13 +1,11 @@ package org.nuiton.scmwebeditor.actions; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.IOException; +import java.io.ByteArrayOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; -import org.apache.commons.io.FileUtils; + import org.apache.commons.lang.StringEscapeUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -16,7 +14,13 @@ import org.nuiton.scmwebeditor.SvnSession; import org.tmatesoft.svn.core.SVNAuthenticationException; import org.tmatesoft.svn.core.SVNException; +import org.tmatesoft.svn.core.SVNNodeKind; +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.io.SVNRepository; +import org.tmatesoft.svn.core.io.SVNRepositoryFactory; +import org.tmatesoft.svn.core.wc.SVNWCUtil; import com.opensymphony.xwork2.Action; @@ -162,34 +166,84 @@ DAVRepositoryFactory.setup(); -// Checkout svn and file organisation + String originalText =""; + + + String login = svnSess.getLogin(); + String pw = svnSess.getPassword(); + String url = svnSess.getSvnPath(); + String file = svnSess.getFileName(); + + if(login==null && pw==null) { + login = "anonymous"; + pw = "anonymous"; + } + + SVNRepository repository = null; + try { - checkout(svnSess); - } catch (SVNAuthenticationException authexep) { - request.setAttribute(PARAMETER_ADDRESS, address); - - // if svn authentication failed user is redirected on login page - if(log.isDebugEnabled()) { - log.debug("Private SCM on reading " + svnSess.getRemoteUrl()); + repository = SVNRepositoryFactory.create( SVNURL.parseURIEncoded( url ) ); + ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager( login , pw ); + repository.setAuthenticationManager( authManager ); + + SVNNodeKind nodeKind = repository.checkPath( file , -1 ); + + + + + if ( nodeKind == SVNNodeKind.NONE ) { + if(log.isErrorEnabled()) { + log.error( "There is no entry at '" + url + "'." ); } - //On supprime le repertoire temporaire + request.setAttribute("projectUrl", projectUrl); + //Suppression du repertoire temporaire delTempDirectory(svnSess); - //redirect to a login page - return Action.LOGIN; - - } catch (SVNException e) { + return "errorPath"; + } else if ( nodeKind == SVNNodeKind.DIR ) { + if(log.isErrorEnabled()) { + log.error( "The entry at '" + url + "' is a file while a directory was expected." ); + } request.setAttribute("projectUrl", projectUrl); //Suppression du repertoire temporaire delTempDirectory(svnSess); return "errorPath"; } + + ByteArrayOutputStream baos = new ByteArrayOutputStream( ); + + repository.getFile( file , -1 , null , baos ); + + originalText=baos.toString(); + - File CheckOutFile = new File(svnSess.getCheckoutdir(), svnSess.getFileName()); + } catch (SVNAuthenticationException authexep) { + + request.setAttribute(PARAMETER_ADDRESS, address); + + // if svn authentication failed user is redirected on login page + if(log.isDebugEnabled()) { + log.debug("Private SCM on reading " + svnSess.getRemoteUrl()); + } + //On supprime le repertoire temporaire + delTempDirectory(svnSess); + //redirect to a login page + return Action.LOGIN; + + } catch (SVNException e) { + request.setAttribute("projectUrl", projectUrl); + //Suppression du repertoire temporaire + delTempDirectory(svnSess); + return "errorPath"; + } + + + + String mineType =null; try { - mineType = getMineType(CheckOutFile); + mineType = getMineType(originalText, svnSess.getFileName()); } catch (Exception e) { if(log.isErrorEnabled()) { log.error("Can't get MimeType",e); @@ -210,39 +264,24 @@ } - try { - String originalText = FileUtils.readFileToString(CheckOutFile); - request.setAttribute(PARAMETER_FORMAT, svnSess.getFormat()); - request.setAttribute(ATTRIBUTE_ORIG_TEXT, StringEscapeUtils.escapeHtml(originalText)); - request.setAttribute(ATTRIBUTE_INVALIDATE_MAX_TIME, (httpSession.getMaxInactiveInterval() / 60)); - request.setAttribute(ATTRIBUTE_LOGIN, (svnSess.getLogin() != null && !svnSess.getLogin().equalsIgnoreCase("") ? svnSess.getLogin() : null)); - request.setAttribute(ATTRIBUTE_IS_LOGIN, (svnSess.getLogin() != null && svnSess.getPassword() != null && !svnSess.getLogin().equalsIgnoreCase("") && !svnSess.getPassword().equalsIgnoreCase(""))); - request.setAttribute(ATTRIBUTE_PROJECT_URL, svnSess.getProjectUrl()); - request.setAttribute(PARAMETER_SCM_EDITOR_URL, svnSess.getScmEditorUrl()); - if (log.isDebugEnabled()) { - log.debug("l'url est : " + svnSess.getScmEditorUrl()); - } - request.setAttribute(ATTRIBUTE_SCM_EDITOR_URI, request.getRequestURI()); - request.setAttribute(ATTRIBUTE_PREVIEW_SERVLET_URL, request.getContextPath() + "/previewservlet"); + - // End on first part - - } catch (FileNotFoundException ee) { - /* fichier non trouve, on redirige vers BadFileRedirect.jsp - * après avoir supprimé le repertoire temporaire - */ - delTempDirectory(svnSess); - request.setAttribute(PARAMETER_SCM_EDITOR_URL, svnSess.getProjectUrl()); - return "errorPath"; - } catch (IOException e) { - log.error("Can't find the checkout file",e); + request.setAttribute(PARAMETER_FORMAT, svnSess.getFormat()); + request.setAttribute(ATTRIBUTE_ORIG_TEXT, StringEscapeUtils.escapeHtml(originalText)); + request.setAttribute(ATTRIBUTE_INVALIDATE_MAX_TIME, (httpSession.getMaxInactiveInterval() / 60)); + request.setAttribute(ATTRIBUTE_LOGIN, (svnSess.getLogin() != null && !svnSess.getLogin().equalsIgnoreCase("") ? svnSess.getLogin() : null)); + request.setAttribute(ATTRIBUTE_IS_LOGIN, (svnSess.getLogin() != null && svnSess.getPassword() != null && !svnSess.getLogin().equalsIgnoreCase("") && !svnSess.getPassword().equalsIgnoreCase(""))); + request.setAttribute(ATTRIBUTE_PROJECT_URL, svnSess.getProjectUrl()); + request.setAttribute(PARAMETER_SCM_EDITOR_URL, svnSess.getScmEditorUrl()); + if (log.isDebugEnabled()) { + log.debug("l'url est : " + svnSess.getScmEditorUrl()); } + request.setAttribute(ATTRIBUTE_SCM_EDITOR_URI, request.getRequestURI()); + request.setAttribute(ATTRIBUTE_PREVIEW_SERVLET_URL, request.getContextPath() + "/previewservlet"); - //On supprime le repertoire temporaire - delTempDirectory(svnSess); return "editPage"; + - } Modified: trunk/src/main/webapp/ModificationViewer.jsp =================================================================== --- trunk/src/main/webapp/ModificationViewer.jsp 2011-07-07 10:31:09 UTC (rev 164) +++ trunk/src/main/webapp/ModificationViewer.jsp 2011-07-07 14:00:26 UTC (rev 165) @@ -100,7 +100,6 @@ - <script type='text/javascript'> selectLanguage('<%=((String) request.getAttribute("minetype"))%>','<%=request.getAttribute("format")%>' ); changeModeBy(editor, document.getElementById('language')); @@ -125,7 +124,7 @@ <div id="htmlcontentCommit"></div> - <p>Commit Message: <input type="text" name="commitMessage" title="Let a commit message with this file."/></p> + <label>Commit Message: <input type="text" name="commitMessage" title="Let a commit message with this file."/></label> <%if (request.getAttribute("IsLogin").equals(false) == true){ %> @@ -163,6 +162,7 @@ button="true" buttonIcon="ui-icon-refresh" value="Save and Continue Editing" + title="Commit modifications and continue editing the file" > </sj:submit> @@ -179,16 +179,17 @@ href="%{ajaxPreview}" button="true" buttonIcon="ui-icon-refresh" - value="Preview" + value="Preview" > </sj:submit> <%}%> <sj:submit + title="Go back to the last revision of the file" id="resetButton" targets="resetResult" - value="Reset Ajax" + value="Reset" button="true" href="reset.action" /> @@ -204,7 +205,7 @@ </div> </form> <center> - <button onClick="javascript:upload_popup('uploadForm.jsp','upload');" >Upload</button> + <button title="Upload a file on the repository" onClick="javascript:upload_popup('uploadForm.jsp','upload');" >Upload</button> </center> <div>