Author: glorieux Date: 2009-09-14 11:45:47 +0200 (Mon, 14 Sep 2009) New Revision: 93 Modified: nuiton-rstedit/src/main/java/HelloServlet.java Log: Modified: nuiton-rstedit/src/main/java/HelloServlet.java =================================================================== --- nuiton-rstedit/src/main/java/HelloServlet.java 2009-09-14 09:26:45 UTC (rev 92) +++ nuiton-rstedit/src/main/java/HelloServlet.java 2009-09-14 09:45:47 UTC (rev 93) @@ -8,19 +8,23 @@ import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; import org.apache.commons.io.FileUtils; +import org.tmatesoft.svn.core.SVNAuthenticationException; import org.tmatesoft.svn.core.SVNDepth; import org.tmatesoft.svn.core.SVNException; import org.tmatesoft.svn.core.SVNURL; +import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager; import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory; +import org.tmatesoft.svn.core.internal.wc.DefaultSVNOptions; import org.tmatesoft.svn.core.wc.SVNClientManager; import org.tmatesoft.svn.core.wc.SVNCommitClient; import org.tmatesoft.svn.core.wc.SVNRevision; import org.tmatesoft.svn.core.wc.SVNUpdateClient; +import org.tmatesoft.svn.core.wc.SVNWCUtil; /** - * - * @author glorieux + * @author geoffroy lorieux */ public class HelloServlet extends HttpServlet { @@ -33,42 +37,38 @@ * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ + @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { // First Part Svn checkout and JSP call - + System.out.println("Connection to RSTEditor"); PrintWriter out = response.getWriter(); - String svnpath = "http://svn.nuiton.org/svn/sandbox/test_rstedit/src/site/rst/"; - String filename = "index.rst"; - SVNURL remote_url = SVNURL.parseURIEncoded(svnpath); + Info my_inf = new Info("http://svn.nuiton.org/svn/sandbox/test_rstedit/src/site/rst/", "index.rst", null, null); + HttpSession sess = request.getSession(true); + sess.setAttribute("my_info", my_inf); +// Tempdir creation + FileUtils.deleteDirectory(my_inf.checkoutdir); + System.out.println("Creating temporary directory on: " + my_inf.checkoutdir); + my_inf.checkoutdir.mkdir(); -// Tempdir localisation and creation - - File dir = (File) getServletContext().getAttribute("javax.servlet.context.tempdir"); - File checkoutdir = new File(dir, "temp_rst"); - checkoutdir.mkdir(); - // doCheckout declaration - - SVNClientManager manager = SVNClientManager.newInstance(); DAVRepositoryFactory.setup(); - SVNUpdateClient upclient = new SVNUpdateClient(manager, null); + SVNUpdateClient upclient = new SVNUpdateClient(my_inf.manager, null); // Checkout svn and file organisation - - upclient.doCheckout(remote_url, checkoutdir, SVNRevision.HEAD, SVNRevision.HEAD, SVNDepth.FILES, false); - File file_in_dir = new File(checkoutdir, filename); + System.out.println("Do Checkout"); + upclient.doCheckout(my_inf.remote_url, my_inf.checkoutdir, SVNRevision.HEAD, SVNRevision.HEAD, SVNDepth.FILES, false); + System.out.println("Checkout done"); + File file_in_dir = new File(my_inf.checkoutdir, my_inf.filename); String original_text = FileUtils.readFileToString(file_in_dir); -// JSP call - +// JSP data transfert request.setAttribute("OrigText", original_text); - RequestDispatcher disp = this.getServletContext().getRequestDispatcher("/test.jsp"); + RequestDispatcher disp = this.getServletContext().getRequestDispatcher("/ModificationViewer.jsp"); // End on first part - disp.forward(request, response); out.close(); } catch (SVNException ex) { @@ -83,47 +83,99 @@ * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ + @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException { + throws ServletException, IOException{ try { // Second Part Client response and SVN commit - PrintWriter out = response.getWriter(); - String filename = "index.rst"; - - File dir = (File) getServletContext().getAttribute("javax.servlet.context.tempdir"); - File checkoutdir = new File(dir, "temp_rst"); - SVNClientManager manager = SVNClientManager.newInstance(); + HttpSession sess = request.getSession(true); + Info my_inf = (Info) sess.getAttribute("my_info"); // Data reception and organisation - String myData = request.getParameter("Mytext"); - - File path_to_file = new File(checkoutdir, filename); - SVNCommitClient comcli = new SVNCommitClient(manager, null); + String commit_message = request.getParameter("Commit_message"); +// Resetting authentification information and manager + my_inf.login = request.getParameter("username"); + my_inf.password = request.getParameter("pw"); + my_inf.auth_manager = SVNWCUtil.createDefaultAuthenticationManager(my_inf.checkoutdir,my_inf.login, my_inf.password, false); + my_inf.manager = SVNClientManager.newInstance(my_inf.svn_optn, my_inf.auth_manager); + + File path_to_file = new File(my_inf.checkoutdir, my_inf.filename); + SVNCommitClient comcli = new SVNCommitClient(my_inf.manager, null); FileUtils.writeStringToFile(path_to_file, myData); File[] tab_file = new File[1]; tab_file[0] = path_to_file; -// Sending Data to SVN +// Sending Data to SVN + try + { + System.out.println("Try to Commit"); + comcli.doCommit(tab_file, false, commit_message, null, null, false, true, SVNDepth.FILES); + + } + catch (SVNAuthenticationException authexep) + { + System.out.println("Bad Login or Password"); + response.setContentType("text/html"); + out.println("<HTML>"); + out.println("<HEAD><TITLE>Error</TITLE></HEAD>"); + out.println("<BODY>"); + out.println("Bad login or password"); + out.println("Please try again"); + out.println("</BODY>"); + out.println("</HTML>"); + return; + } + System.out.println("Commit done"); + response.setContentType("text/html"); + out.println("<HTML>"); + out.println("<HEAD><TITLE>RSTEditor</TITLE></HEAD>"); + out.println("<BODY>"); + out.println("Thank you using RSTEditor"); + out.println("</BODY>"); + out.println("</HTML>"); +// Deleting temp directory + System.out.println("Deleting temporary directory : " + my_inf.checkoutdir); + FileUtils.deleteDirectory(my_inf.checkoutdir); - comcli.doCommit(tab_file, false, "New_rst", null, null, false, true, SVNDepth.FILES); -// End of Second part +// End of Second part out.close(); } catch (SVNException ex) { Logger.getLogger(HelloServlet.class.getName()).log(Level.SEVERE, null, ex); } } - /** - * Returns a short description of the servlet. - * @return a String containing servlet description - */ - public String getServletInfo() { - return "Short description"; - }// </editor-fold> +// Information object + protected class Info{ + String svnpath; // svn path without filename + String filename; // filename of modif file + String login; // user's login + String password; // user's password + File tmp_dir; // Temp directory path + File checkoutdir; // Temp directory for checkout + SVNURL remote_url; // url of svn path + ISVNAuthenticationManager auth_manager; // authmanager with login and password + DefaultSVNOptions svn_optn; // svn default option + SVNClientManager manager; // svn manager with default option and authentification manager + + + Info (String svnpath, String filename, String login, String password) throws SVNException{ + this.svnpath = svnpath; + this.filename = filename; + this.login = login; + this.password = password; + this.tmp_dir = (File) getServletContext().getAttribute("javax.servlet.context.tempdir"); + this.checkoutdir = new File(this.tmp_dir, "temp_rst"); + this.remote_url = SVNURL.parseURIEncoded(svnpath); + this.auth_manager = SVNWCUtil.createDefaultAuthenticationManager(this.checkoutdir, this.login, this.password, false); + this.svn_optn = SVNWCUtil.createDefaultOptions(false); + this.manager = SVNClientManager.newInstance(this.svn_optn, this.auth_manager); + + } + } }
participants (1)
-
glorieux@users.nuiton.org