diff --git a/apps/maarch_entreprise/img/logo_only.png b/apps/maarch_entreprise/img/logo_only.png new file mode 100644 index 0000000000000000000000000000000000000000..18ca021d0ba2c27813762c5eedce078b35dc882f Binary files /dev/null and b/apps/maarch_entreprise/img/logo_only.png differ diff --git a/modules/content_management/dist/maarchCM.jar b/modules/content_management/dist/maarchCM.jar index 8d16d226a185c33124c19aeff65125ca685dbf2c..99037225b72943bb5cf716ff4701c9840b8fff21 100644 Binary files a/modules/content_management/dist/maarchCM.jar and b/modules/content_management/dist/maarchCM.jar differ diff --git a/modules/content_management/dist/not_signed/maarchCM.jar b/modules/content_management/dist/not_signed/maarchCM.jar index 40b16f84956335eea8e5a71ff27e9a0bc664167f..0a6d9b191a4c1d892c33bcc04bf436d469103805 100644 Binary files a/modules/content_management/dist/not_signed/maarchCM.jar and b/modules/content_management/dist/not_signed/maarchCM.jar differ diff --git a/modules/content_management/dist/src/FileManager.java b/modules/content_management/dist/src/FileManager.java index 9bccdfc217649377a0411ba3378d0aeae47d8525..b79e487ed16a3b57ebd345a8beca0f979c12598e 100644 --- a/modules/content_management/dist/src/FileManager.java +++ b/modules/content_management/dist/src/FileManager.java @@ -11,13 +11,17 @@ package com.maarch; import java.io.*; import java.lang.reflect.InvocationTargetException; +import java.nio.file.FileVisitResult; import java.security.AccessController; import java.security.PrivilegedActionException; import java.security.PrivilegedExceptionAction; import org.apache.commons.codec.binary.Base64; import java.nio.file.Files; +import java.nio.file.Path; import java.nio.file.Paths; +import java.nio.file.SimpleFileVisitor; +import java.nio.file.attribute.BasicFileAttributes; import java.util.List; /** @@ -59,22 +63,6 @@ public class FileManager { return msg; } - /** - * Controls the existance of psExec file in tmp dir - * @param path path to the tmp dir - * @return boolean - */ - public boolean isPsExecFileExists(String path) throws IOException { - File file=new File(path); - if (!file.exists()) { - System.out.println("psExec on path " + path + " not exists so the applet will create it"); - return false; - } else { - System.out.println("psExec on path " + path + " already exists"); - return true; - } - } - /** * Creates the template sended by Maarch file in tmp dir * @param encodedContent the file to create @@ -197,54 +185,7 @@ public class FileManager { } ); } - - /** - * Launchs a command to execute - * @param launchCommand the command to launch - * @return process - */ - /*public Process launchApp(final String launchCommand) throws PrivilegedActionException { - System.out.println("COMMAND : " + launchCommand); - Object proc = new Object(); - try{ - proc = AccessController.doPrivileged( - new PrivilegedExceptionAction() { - - public Object run() throws IOException { - - Runtime rt = Runtime.getRuntime(); - Process proc = rt.exec(launchCommand); - String line; - BufferedReader in = new BufferedReader( - new InputStreamReader(proc.getInputStream()) ); - System.out.println("INFO: LAUNCH OF THE COMMAND -->"); - while ((line = in.readLine()) != null) { - System.out.println(line); - } - System.out.println("<--"); - in.close(); - - in = new BufferedReader( - new InputStreamReader(proc.getErrorStream()) ); - System.out.println("INFO: ERROR ON THE COMMAND -->"); - while ((line = in.readLine()) != null) { - System.out.println(line); - } - System.out.println("<--"); - in.close(); - - return proc; - - } - } - ); - } catch (Throwable t) - { - t.printStackTrace(); - } - return (Process) proc; - }*/ - + /** * Retrieves the right program to edit the template with his extension * @param ext extension of the template @@ -357,4 +298,25 @@ public class FileManager { } } + public static void deleteEnvDir (String path) throws IOException { + File dir_app_conv = new File(path); + if (dir_app_conv.exists()) { + Path directory = Paths.get(path); + + Files.walkFileTree(directory, new SimpleFileVisitor<Path>() { + @Override + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { + Files.delete(file); + return FileVisitResult.CONTINUE; + } + @Override + public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { + Files.delete(dir); + return FileVisitResult.CONTINUE; + } + }); + } + } + + } diff --git a/modules/content_management/dist/src/MaarchCM.java b/modules/content_management/dist/src/MaarchCM.java index f0efe00251a91087393b4cf682623fd6c27ddd64..2471595382c28198d1ed2be67a9f9c04c5c6167f 100644 --- a/modules/content_management/dist/src/MaarchCM.java +++ b/modules/content_management/dist/src/MaarchCM.java @@ -9,16 +9,28 @@ package com.maarch; //import java.applet.Applet; +import java.awt.AWTException; +import java.awt.Image; +import java.awt.MenuItem; +import java.awt.PopupMenu; +import java.awt.SystemTray; +import java.awt.Toolkit; +import java.awt.TrayIcon; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; import java.io.*; import java.lang.reflect.InvocationTargetException; import java.net.MalformedURLException; import java.net.URL; -import java.nio.file.FileVisitResult; -import java.nio.file.Files; +import java.nio.file.FileSystems; import java.nio.file.Path; import java.nio.file.Paths; -import java.nio.file.SimpleFileVisitor; -import java.nio.file.attribute.BasicFileAttributes; +import static java.nio.file.StandardWatchEventKinds.ENTRY_CREATE; +import static java.nio.file.StandardWatchEventKinds.ENTRY_DELETE; +import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY; +import java.nio.file.WatchEvent; +import java.nio.file.WatchKey; +import java.nio.file.WatchService; import java.security.PrivilegedActionException; import java.util.ArrayList; import java.util.HashSet; @@ -45,7 +57,6 @@ import org.apache.http.impl.cookie.BasicClientCookie; import org.w3c.dom.Document; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; -import netscape.javascript.JSObject; import javax.swing.JOptionPane; import org.apache.http.NameValuePair; @@ -100,25 +111,76 @@ public class MaarchCM { public String fileToEdit; public String editMode; public String programName; + SystemTray tray = SystemTray.getSystemTray(); + //If the icon is a file + Image image = Toolkit.getDefaultToolkit().createImage(this.getClass().getResource("logo_only.png")); + //Alternative (if the icon is on the classpath): + + ActionListener exitListener = new ActionListener() { + public void actionPerformed(ActionEvent e) { + System.out.println("Exiting..."); + try { + endRequestApplet(); + } catch (UnsupportedEncodingException ex) { + Logger.getLogger(MaarchCM.class.getName()).log(Level.SEVERE, null, ex); + } catch (InterruptedException ex) { + Logger.getLogger(MaarchCM.class.getName()).log(Level.SEVERE, null, ex); + } + System.exit(0); + } + }; + TrayIcon trayIcon = new TrayIcon(image, "Tray Demo"); public List<String> fileToDelete = new ArrayList<String>(); - public static void main(String[] args) throws JSException { + public static void main(String[] args) throws JSException, AWTException, InterruptedException, IOException { MaarchCM MaarchCM = new MaarchCM(); MaarchCM.start(args); } - + + /** * Launch of the JNLP */ - public void start(String[] args) throws JSException { + public void start(String[] args) throws JSException, AWTException, InterruptedException, IOException { - System.out.println("----------BEGIN----------"); - System.out.println("----------ARGUMENTS----------"); + + PopupMenu popup = new PopupMenu(); + MenuItem defaultItem = new MenuItem("Fermer l'applet"); + defaultItem.addActionListener(exitListener); + popup.add(defaultItem); + //Let the system resize the image if needed + trayIcon.setImageAutoSize(true); + //Set tooltip text for the tray icon + trayIcon.setToolTip("Maarch content editor"); + tray.add(trayIcon); + + trayIcon.setPopupMenu(popup); + + initDatas(args); + + initHttpRequest(); + + getClientEnv(); + try { + //editObject(); + if (onlyConvert.equals("true")) { + launchOnlyConvert(); + } else { + editObject_v2(); + } + + System.exit(0); + } catch (Exception ex) { + Logger.getLogger(MaarchCM.class.getName()).log(Level.SEVERE, null, ex); + } + } + + public void initDatas(String[] args) { int index; for (index = 0; index < args.length; ++index) @@ -136,7 +198,7 @@ public class MaarchCM { userMaarch = args[8]; convertPdf = args[9]; onlyConvert = args[10]; - + System.out.println("URL : " + url); System.out.println("OBJECT TYPE : " + objectType); System.out.println("ID APPLET : " + idApplet); @@ -149,7 +211,46 @@ public class MaarchCM { System.out.println("CONVERTPDF : " + convertPdf); System.out.println("ONLYCONVERT : " + onlyConvert); System.out.println("----------CONTROL PARAMETERS----------"); + } + public void getClientEnv() throws InterruptedException, IOException { + os = System.getProperty("os.name").toLowerCase(); + boolean isUnix = os.contains("nix") || os.contains("nux"); + boolean isWindows = os.contains("win"); + boolean isMac = os.contains("mac"); + if (isWindows) { + System.out.println("This is Windows"); + os = "win"; + } else if (isMac) { + System.out.println("This is Mac"); + os = "mac"; + } else if (isUnix) { + System.out.println("This is Unix or Linux"); + os = "linux"; + } else { + System.out.println("Your OS is not supported!!"); + } + fM = new FileManager(); + String userLocalDir = System.getProperty("user.home"); + userLocalDirTmp = userLocalDir + File.separator + "maarchTmp"; + + System.out.println("Create the logger"); + logger = new MyLogger(userLocalDirTmp + File.separator); + System.out.println("APP PATH: " + appPath); + System.out.println("----------BEGIN LOCAL DIR TMP IF NOT EXISTS----------"); + + String info = fM.createUserLocalDirTmp(userLocalDirTmp, os); + + if (info == "ERROR") { + logger.log("ERREUR : Permissions insuffisante sur votre répertoire temporaire maarch", Level.SEVERE); + messageStatus = "ERROR"; + messageResult.clear(); + messageResult.put("ERROR", "Permissions insuffisante sur votre répertoire temporaire maarch"); + processReturn(messageResult); + } + } + + public void initHttpRequest() { if ( isURLInvalid() || isObjectTypeInvalid() || @@ -219,18 +320,8 @@ public class MaarchCM { if (httpClient == null) { System.out.println("NO HTTP CLIENT WAS INSTANTIATED, THE APPLICATION WILL FAIL!"); } - - try { - editObject(); - //TODO exit of JNLP - //MaarchCM.destroy(); - //MaarchCM.stop(); - System.exit(0); - } catch (Exception ex) { - Logger.getLogger(MaarchCM.class.getName()).log(Level.SEVERE, null, ex); - } } - + /** * Controls the url parameter * @return boolean @@ -276,18 +367,11 @@ public class MaarchCM { private boolean isObjectTableInvalid() { Set<String> whiteList = new HashSet<>(); whiteList.add("res_letterbox"); - whiteList.add("res_business"); - whiteList.add("res_x"); whiteList.add("res_attachments"); whiteList.add("mlb_coll_ext"); - whiteList.add("business_coll_ext"); whiteList.add("res_version_letterbox"); - whiteList.add("res_version_business"); - whiteList.add("res_version_x"); whiteList.add("res_view_attachments"); - whiteList.add("res_view"); whiteList.add("res_view_letterbox"); - whiteList.add("res_view_business"); whiteList.add("templates"); if (whiteList.contains(objectTable)) return false; //success System.out.println("objectTable not in the authorized list " + objectTable); @@ -366,53 +450,47 @@ public class MaarchCM { if (docxFile.contains(".odt") || docxFile.contains(".ods") || docxFile.contains(".ODT") || docxFile.contains(".ODS")) { logger.log("This is opendocument ! ", Level.INFO); if (os == "linux") { - cmd = "libreoffice -env:UserInstallation=file://"+userLocalDirTmp+idApplet+"_conv/ --headless --convert-to pdf --outdir \"" + userLocalDirTmp.substring(0, userLocalDirTmp.length() - 1) + "\" \"" + docxFile + "\""; + cmd = "libreoffice -env:UserInstallation=file://"+userLocalDirTmp + File.separator + idApplet+"_conv/ --headless --convert-to pdf --outdir \"" + userLocalDirTmp + "\" \"" + docxFile + "\""; } else if (os == "mac") { - cmd = "cd /Applications/LibreOffice.app/Contents/MacOs && ./soffice --headless --convert-to pdf:writer_pdf_Export --outdir \"" + userLocalDirTmp.substring(0, userLocalDirTmp.length() - 1) + "\" \"" + docxFile + "\""; + cmd = "cd /Applications/LibreOffice.app/Contents/MacOs && ./soffice --headless --convert-to pdf:writer_pdf_Export --outdir \"" + userLocalDirTmp + "\" \"" + docxFile + "\""; } else { String convertProgram; convertProgram = fM.findPathProgramInRegistry("soffice.exe"); - cmd = convertProgram + " \"-env:UserInstallation=file:///"+userLocalDirTmp.replace("\\", "/")+idApplet+"_conv/\" --headless --convert-to pdf --outdir \"" + userLocalDirTmp.substring(0, userLocalDirTmp.length() - 1) + "\" \"" + docxFile + "\" \r\n"; + cmd = convertProgram + " --headless --convert-to pdf --outdir \"" + userLocalDirTmp + "\" \"" + docxFile + "\" \r\n"; } } else if (docxFile.contains(".doc") || docxFile.contains(".docx") || docxFile.contains(".DOC") || docxFile.contains(".DOCX")) { logger.log("This is MSOffice document ", Level.INFO); if (useExeConvert.equals("false")) { if (os == "linux") { - cmd = "libreoffice -env:UserInstallation=file://"+userLocalDirTmp+idApplet+"_conv\\ --headless --convert-to pdf --outdir \"" + userLocalDirTmp.substring(0, userLocalDirTmp.length() - 1) + "\" \"" + docxFile + "\""; + cmd = "libreoffice --headless --convert-to pdf --outdir \"" + userLocalDirTmp + "\" \"" + docxFile + "\""; } else if (os == "mac") { - cmd = "cd /Applications/LibreOffice.app/Contents/MacOs && ./soffice --headless --convert-to pdf:writer_pdf_Export --outdir \"" + userLocalDirTmp.substring(0, userLocalDirTmp.length() - 1) + "\" \"" + docxFile + "\""; + cmd = "cd /Applications/LibreOffice.app/Contents/MacOs && ./soffice --headless --convert-to pdf:writer_pdf_Export --outdir \"" + userLocalDirTmp + "\" \"" + docxFile + "\""; } else if(editMode.equals("libreoffice")){ String convertProgram; convertProgram = fM.findPathProgramInRegistry("soffice.exe"); - cmd = convertProgram + " \"-env:UserInstallation=file:///"+userLocalDirTmp.replace("\\", "/")+idApplet+"_conv/\" --headless --convert-to pdf --outdir \"" + userLocalDirTmp.substring(0, userLocalDirTmp.length() - 1) + "\" \"" + docxFile + "\" \r\n"; + cmd = convertProgram + " --headless --convert-to pdf --outdir \"" + userLocalDirTmp + "\" \"" + docxFile + "\" \r\n"; }else{ + vbsPath = userLocalDirTmp + File.separator + "DOC2PDF_VBS.vbs"; + fM.createFile(fileContentVbs, vbsPath); + fileToDelete.add(vbsPath); cmd = "cmd /C c:\\Windows\\System32\\cscript \"" + vbsPath + "\" \"" + docxFile + "\" /nologo \r\n"; } - } else { - - StringBuffer buffer = new StringBuffer(docxFile); - buffer.replace(buffer.lastIndexOf("."), buffer.length(), ".pdf"); - String pdfOut = buffer.toString(); - - cmd = "cmd /C \"" + userLocalDirTmp + "Word2Pdf.exe\" \"" + docxFile + "\" \"" + pdfOut + "\" \r\n"; } } else { conversion = false; } if (conversion) { - logger.log("EXEC PATH : " + cmd, Level.INFO); - FileManager fM = new FileManager(); - Process proc_vbs; + appPath_convert = userLocalDirTmp + File.separator + "conversion_"+idApplet+".sh"; + fileToDelete.add(appPath_convert); + logger.log("EXEC PATH : " + cmd, Level.INFO); if (os == "linux" || os == "mac") { - //cmd = "cscript \""+vbsPath+"\" \""+docxFile+"\" /nologo \r\n"; final Writer outBat; outBat = new OutputStreamWriter(new FileOutputStream(appPath_convert), "CP850"); logger.log("--- cmd sh --- " + cmd, Level.INFO); outBat.write(cmd); - //outBat.write("exit \r\n"); outBat.close(); File myFileBat = new File(appPath_convert); @@ -420,22 +498,12 @@ public class MaarchCM { myFileBat.setWritable(true, false); myFileBat.setExecutable(true, false); - /*String cmd2 = "start /WAIT /MIN "+appPath_convert+" \r\n"; - final Writer outBat2 = new OutputStreamWriter(new FileOutputStream(appPath), "CP850"); - outBat2.write(cmd2); - outBat2.write("exit \r\n"); - outBat2.close();*/ - - /*File myFileBat2 = new File(appPath); - myFileBat2.setReadable(true, false); - myFileBat2.setWritable(true, false); - myFileBat2.setExecutable(true, false);*/ - final String exec_vbs = "\"" + appPath + "\""; proc_vbs = fM.launchApp(appPath_convert); } else { proc_vbs = fM.launchApp(cmd); } + proc_vbs.waitFor(); } @@ -449,7 +517,7 @@ public class MaarchCM { * Retrieve the xml message from Maarch and parse it * @param flux_xml xml content message */ - public void parse_xml(InputStream flux_xml) throws SAXException, IOException, ParserConfigurationException { + public void parse_xml(InputStream flux_xml) throws SAXException, IOException, ParserConfigurationException, InterruptedException { logger.log("----------BEGIN PARSE XML----------", Level.INFO); DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); @@ -475,8 +543,8 @@ public class MaarchCM { logger.log("ERREUR : Le document n'a pas pu être transféré du coté client. Assurez-vous que le modèle n'est pas corrompu et que la zone de stockage des templates soit correct.", Level.SEVERE); messageStatus = "ERROR"; - messageResult.put("ERROR", "ERREUR : Le document n'a pas pu être transféré du coté client. Assurez-vous que le modèle n'est pas corrompu et que la zone de stockage des templates soit correct."); - JOptionPane.showMessageDialog(null, "ERREUR ! L'édition de votre document a échoué. Assurez-vous que le modèle n'est pas corrompu et que la zone de stockage des modèles soit correct."); + messageResult.put("ERROR", "Le document n'a pas pu être transféré du coté client. Assurez-vous que le modèle n'est pas corrompu et que la zone de stockage des templates soit correct."); + processReturn(messageResult); } logger.log("----------END PARSE XML----------", Level.INFO); } @@ -485,7 +553,7 @@ public class MaarchCM { * Manage the return of program execution * @param result result of the program execution */ - public void processReturn(Hashtable result) { + public void processReturn(Hashtable result) throws InterruptedException, UnsupportedEncodingException { Iterator itValue = result.values().iterator(); Iterator itKey = result.keySet().iterator(); while (itValue.hasNext()) { @@ -511,290 +579,26 @@ public class MaarchCM { } //send message error to Maarch if necessary if (!error.isEmpty()) { - //TODO exit of the JNLP - //destroy(); - //stop(); + endRequestApplet(); + trayIcon.displayMessage("Maarch content editor", error, TrayIcon.MessageType.ERROR); + Thread.sleep(5000); System.exit(0); } } - /** - * Main function of the class - * enables you to edit document with the user favorit editor - * @return nothing - * @throws java.lang.Exception - */ - public String editObject() throws Exception, InterruptedException, JSException { - - System.out.println("----------BEGIN EDIT OBJECT---------- LGI by Maarch 02/10/2017"); - System.out.println("----------BEGIN LOCAL DIR TMP IF NOT EXISTS----------"); - os = System.getProperty("os.name").toLowerCase(); - boolean isUnix = os.contains("nix") || os.contains("nux"); - boolean isWindows = os.contains("win"); - boolean isMac = os.contains("mac"); - userLocalDirTmp = System.getProperty("user.home"); - - fM = new FileManager(); - - if (isWindows) { - System.out.println("This is Windows"); - userLocalDirTmp = userLocalDirTmp + "\\maarchTmp\\"; - appPath = userLocalDirTmp + "start.bat"; - appPath_convert = userLocalDirTmp + "conversion_"+idApplet+".bat"; - os = "win"; - } else if (isMac) { - System.out.println("This is Mac"); - userLocalDirTmp = userLocalDirTmp + "/maarchTmp/"; - appPath = userLocalDirTmp + "start.sh"; - appPath_convert = userLocalDirTmp + "conversion_"+idApplet+".sh"; - os = "mac"; - } else if (isUnix) { - System.out.println("This is Unix or Linux"); - userLocalDirTmp = userLocalDirTmp + "/maarchTmp/"; - appPath = userLocalDirTmp + "start.sh"; - appPath_convert = userLocalDirTmp + "conversion_"+idApplet+".sh"; - os = "linux"; - } else { - System.out.println("Your OS is not supported!!"); - } - fileToDelete.add(appPath_convert); - - System.out.println("Create the logger"); - logger = new MyLogger(userLocalDirTmp); - - System.out.println("APP PATH: " + appPath); - System.out.println("----------BEGIN LOCAL DIR TMP IF NOT EXISTS----------"); - - String info = fM.createUserLocalDirTmp(userLocalDirTmp, os); - - if (info == "ERROR") { - logger.log("ERREUR : Permissions insuffisante sur votre répertoire temporaire maarch", Level.SEVERE); - messageStatus = "ERROR"; - messageResult.clear(); - messageResult.put("ERROR", "ERREUR : Permissions insuffisante sur votre répertoire temporaire maarch"); - JOptionPane.showMessageDialog(null, "ERREUR ! Permissions insuffisante sur votre répertoire temporaire maarch."); - processReturn(messageResult); - } - - System.out.println("Create the logger"); - logger = new MyLogger(userLocalDirTmp); - - /*logger.log("Delete thefile if exists", Level.INFO); - FileManager.deleteFilesOnDir(userLocalDirTmp, "thefile");*/ - - logger.log("----------PARAM ----------", Level.INFO); - logger.log("URL : " + url, Level.INFO); - logger.log("OBJECT TYPE : " + objectType, Level.INFO); - logger.log("ID APPLET : " + idApplet, Level.INFO); - logger.log("OBJECT TABLE : " + objectTable, Level.INFO); - logger.log("OBJECT ID : " + objectId, Level.INFO); - logger.log("UNIQUE ID : " + uniqueId, Level.INFO); - logger.log("COOKIE : " + cookie, Level.INFO); - logger.log("CLIENTSIDECOOKIES : " + clientSideCookies, Level.INFO); - logger.log("USERMAARCH : " + userMaarch, Level.INFO); - logger.log("ONLYCONVERT : " + onlyConvert, Level.INFO); - - logger.log("----------BEGIN OPEN REQUEST----------", Level.INFO); - String urlToSend; - - urlToSend = url + "?action=editObject&objectType=" + objectType - + "&objectTable=" + objectTable + "&objectId=" + objectId - + "&uniqueId=" + uniqueId; - - - logger.log("FIRST URL CALL : " + urlToSend, Level.INFO); - sendHttpRequest(urlToSend, "none", false); - logger.log("MESSAGE STATUS : " + messageStatus, Level.INFO); - logger.log("MESSAGE RESULT : ", Level.INFO); - processReturn(messageResult); - logger.log("----------END OPEN REQUEST----------", Level.INFO); - - fileToEdit = "thefile_" + idApplet + "." + fileExtension; - - logger.log("----------BEGIN CREATE THE BAT TO LAUNCH IF NECESSARY----------", Level.INFO); - logger.log("create the file : " + appPath, Level.INFO); - fM.createBatFile( - appPath, - userLocalDirTmp, - fileToEdit, - os, - idApplet - ); - logger.log("----------END CREATE THE BAT TO LAUNCH IF NECESSARY----------", Level.INFO); - - if ("ok".equals(status)) { - logger.log("RESPONSE OK", Level.INFO); - - if ("true".equals(convertPdf)) { - logger.log("CREATE FILE IN LOCAL PATH", Level.INFO); - if (useExeConvert.equals("false")) { - logger.log("---------- VBS FILE ----------", Level.INFO); - logger.log(" Path = " + vbsPath, Level.INFO); - if (vbsPath.equals("")) vbsPath = userLocalDirTmp + "DOC2PDF_VBS.vbs"; - boolean isVbsExists = fM.isPsExecFileExists(vbsPath); - if (!isVbsExists) fM.createFile(fileContentVbs, vbsPath); - } else { - boolean isConvExecExists = fM.isPsExecFileExists(userLocalDirTmp + "Word2Pdf.exe"); - if (!isConvExecExists) fM.createFile(fileContentExe, userLocalDirTmp + "Word2Pdf.exe"); - } - } - - logger.log("----------BEGIN EXECUTION OF THE EDITOR----------", Level.INFO); - logger.log("CREATE FILE IN LOCAL PATH", Level.INFO); - fM.createFile(fileContent, userLocalDirTmp + fileToEdit); - fileToDelete.add(userLocalDirTmp + fileToEdit); - - fileContentTosend = ""; - - if (onlyConvert.equals("false")) { - Thread theThread; - theThread = new Thread(new ProcessLoop(this)); - - theThread.start(); - - String actualContent; - - do { - theThread.sleep(3000); - File fileTotest = new File(userLocalDirTmp + fileToEdit); - if (fileTotest.canRead()) { - actualContent = FileManager.encodeFile(userLocalDirTmp + fileToEdit); - if (!fileContentTosend.equals(actualContent)) { - fileContentTosend = actualContent; - logger.log("----------[SECURITY BACKUP] BEGIN SEND OF THE OBJECT----------", Level.INFO); - String urlToSave = url + "?action=saveObject&objectType=" + objectType - + "&objectTable=" + objectTable + "&objectId=" + objectId - + "&uniqueId=" + uniqueId + "&step=backup&userMaarch=" + userMaarch; - logger.log("[SECURITY BACKUP] URL TO SAVE : " + urlToSave, Level.INFO); - sendHttpRequest(urlToSave, fileContentTosend, false); - logger.log("[SECURITY BACKUP] MESSAGE STATUS : " + messageStatus, Level.INFO); - } - } else { - logger.log(userLocalDirTmp + fileToEdit + " FILE NOT READABLE !!!!!!", Level.INFO); - } - } - while (theThread.isAlive()); - - theThread.interrupt(); - } - - - - logger.log("----------END EXECUTION OF THE EDITOR----------", Level.INFO); - - logger.log("----------BEGIN RETRIEVE CONTENT OF THE OBJECT----------", Level.INFO); - - fileContentTosend = FileManager.encodeFile(userLocalDirTmp + fileToEdit); - - logger.log("----------END RETRIEVE CONTENT OF THE OBJECT----------", Level.INFO); - - logger.log("conversion pdf ? " + convertPdf , Level.INFO); - - if ("true".equals(convertPdf)) { - if ((fileExtension.equalsIgnoreCase("docx") || fileExtension.equalsIgnoreCase("doc") || fileExtension.equalsIgnoreCase("docm") || fileExtension.equalsIgnoreCase("odt") || fileExtension.equalsIgnoreCase("ott"))) { - logger.log("----------CONVERSION PDF----------", Level.INFO); - - createPDF(userLocalDirTmp + fileToEdit, userLocalDirTmp, os); - - String pdfFile = userLocalDirTmp + "thefile_" + idApplet + ".pdf"; - - logger.log("----------BEGIN RETRIEVE CONTENT OF THE OBJECT----------", Level.INFO); - if (fM.isPsExecFileExists(pdfFile)) { - pdfContentTosend = FileManager.encodeFile(pdfFile); - fileToDelete.add(pdfFile); - } else { - pdfContentTosend = "null"; - logger.log("ERREUR DE CONVERSION PDF !", Level.WARNING); - JOptionPane.showMessageDialog(null, "Attention ! La conversion PDF a échoué mais le document a bien été transféré."); - } - logger.log("----------END RETRIEVE CONTENT OF THE OBJECT----------", Level.INFO); - - logger.log("---------- FIN CONVERSION PDF----------", Level.INFO); - }else{ - pdfContentTosend = "not allowed"; - logger.log("Conversion not allowed for this extension : " + fileExtension, Level.INFO); - } - } - - - String urlToSave = url + "?action=saveObject&objectType=" + objectType - + "&objectTable=" + objectTable + "&objectId=" + objectId - + "&uniqueId=" + uniqueId + "&idApplet=" + idApplet + "&step=end&userMaarch=" + userMaarch - + "&onlyConvert=" + onlyConvert; - logger.log("----------BEGIN SEND OF THE OBJECT----------", Level.INFO); - logger.log("URL TO SAVE : " + urlToSave, Level.INFO); - sendHttpRequest(urlToSave, fileContentTosend, true); - logger.log("MESSAGE STATUS : " + messageStatus, Level.INFO); - logger.log("LAST MESSAGE RESULT : ", Level.INFO); - processReturn(messageResult); - - if ("true".equals(convertPdf)) { - if (pdfContentTosend == "null") { - endMessage = endMessage + " mais la conversion pdf n'a pas fonctionné (le document ne pourra pas être signé)"; - } - } - - logger.log("----------END SEND OF THE OBJECT----------", Level.INFO); - } else { - logger.log("RESPONSE KO", Level.WARNING); - } - logger.log("----------END EDIT OBJECT----------", Level.INFO); - - //delete tmp files - FileManager.deleteSpecificFilesOnDir(fileToDelete); - - //delete env libreoffice instance - File dir_app = new File(userLocalDirTmp+idApplet); - if (dir_app.exists()) { - Path directory = Paths.get(userLocalDirTmp+idApplet); - Files.walkFileTree(directory, new SimpleFileVisitor<Path>() { - @Override - public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { - Files.delete(file); - return FileVisitResult.CONTINUE; - } - - @Override - public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { - Files.delete(dir); - return FileVisitResult.CONTINUE; - } - }); - } - //delete env libreoffice convert instance - File dir_app_conv = new File(userLocalDirTmp+idApplet+"_conv"); - if (dir_app_conv.exists()) { - Path directory = Paths.get(userLocalDirTmp+idApplet+"_conv"); - Files.walkFileTree(directory, new SimpleFileVisitor<Path>() { - @Override - public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { - Files.delete(file); - return FileVisitResult.CONTINUE; - } - - @Override - public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { - Files.delete(dir); - return FileVisitResult.CONTINUE; - } - }); - } - - return "ok"; - } - /** * Launch the external program and wait his execution end * @return boolean */ public Boolean launchProcess() throws PrivilegedActionException, InterruptedException, IllegalArgumentException, IllegalAccessException, InvocationTargetException { - Process proc; - logger.log("LAUNCH THE EDITOR !", Level.INFO); - if ("linux".equals(os) || "mac".equals(os)) { + if ("linux".equals(os)) { editMode = "libreoffice"; - proc = fM.launchApp(appPath); + fM.launchApp("libreoffice --nolockcheck --nodefault --nofirststartwizard --nofirststartwizard --norestore " + userLocalDirTmp + File.separator + fileToEdit); + } else if ("mac".equals(os)) { + editMode = "libreoffice"; + fM.launchApp("open -W " + userLocalDirTmp + File.separator + fileToEdit); } else { logger.log("FILE TO EDIT : " + userLocalDirTmp + fileToEdit, Level.INFO); @@ -811,14 +615,14 @@ public class MaarchCM { pathProgram = fM.findPathProgramInRegistry(programName); options = fM.findGoodOptionsToEdit(fileExtension); }else{ - options = "\"-env:UserInstallation=file:///" + userLocalDirTmp.replace("\\", "/") + idApplet +"/\" "; + options = " --nolockcheck --nodefault --nofirststartwizard --nofirststartwizard --norestore "; } }else{ if("\"null\"".equals(pathProgram)){ System.out.println(programName+" not found! switch to libreoffice..."); programName = "soffice.exe"; pathProgram = fM.findPathProgramInRegistry(programName); - options = "\"-env:UserInstallation=file:///" + userLocalDirTmp.replace("\\", "/") + idApplet +"/\" "; + options = " --nolockcheck --nodefault --nofirststartwizard --nofirststartwizard --norestore "; }else{ options = fM.findGoodOptionsToEdit(fileExtension); } @@ -835,13 +639,10 @@ public class MaarchCM { String pathCommand; - pathCommand = pathProgram + " " + options + "\"" + userLocalDirTmp + fileToEdit + "\""; + pathCommand = pathProgram + " " + options + "\"" + userLocalDirTmp + File.separator + fileToEdit + "\""; logger.log("PATH COMMAND TO EDIT " + pathCommand, Level.INFO); - proc = fM.launchApp(pathCommand); + fM.launchApp(pathCommand); } - proc.waitFor(); - logger.log("END OF THE PROCESS", Level.INFO); - return true; } @@ -953,34 +754,249 @@ public class MaarchCM { } } catch (Exception ex) { logger.log("erreur: " + ex, Level.SEVERE); - JOptionPane.showMessageDialog(null, "ERREUR ! La connexion au serveur a été interrompue, le document édité n'a pas été sauvegardé !"); + trayIcon.displayMessage("Maarch content editor", "La connexion au serveur a été interrompue, le document édité n'a pas été sauvegardé !", TrayIcon.MessageType.ERROR); } } - /** - * Class to manage the execution of an external program - */ - public class ProcessLoop extends Thread { - public MaarchCM MaarchCM; + public void editObject_v2() throws InterruptedException, IOException, PrivilegedActionException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, Exception { + + + logger.log("----------BEGIN OPEN REQUEST----------", Level.INFO); + + String urlToSend; - public ProcessLoop(MaarchCM MaarchCM){ - this.MaarchCM = MaarchCM; + urlToSend = url + "?action=editObject&objectType=" + objectType + + "&objectTable=" + objectTable + "&objectId=" + objectId + + "&uniqueId=" + uniqueId; + + + logger.log("FIRST URL CALL : " + urlToSend, Level.INFO); + sendHttpRequest(urlToSend, "none", false); + logger.log("MESSAGE STATUS : " + messageStatus, Level.INFO); + logger.log("MESSAGE RESULT : ", Level.INFO); + processReturn(messageResult); + logger.log("----------END OPEN REQUEST----------", Level.INFO); + logger.log("CREATE FILE IN LOCAL PATH", Level.INFO); + + fileToEdit = "thefile_" + idApplet + "." + fileExtension; + //fileToEdit = "test.odt"; + fM.createFile(fileContent, userLocalDirTmp + File.separator + fileToEdit); + fileToDelete.add(userLocalDirTmp + File.separator + fileToEdit); + fileContentTosend = ""; + + launchProcess(); + try { + WatchService watcher = FileSystems.getDefault().newWatchService(); + + Path dir = Paths.get(userLocalDirTmp); + dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY); + String editor = ""; + + while (true) { + WatchKey key; + try { + key = watcher.take(); + } catch (InterruptedException ex) { + return; + } + + for (WatchEvent<?> event : key.pollEvents()) { + WatchEvent.Kind<?> kind = event.kind(); + + @SuppressWarnings("unchecked") + WatchEvent<Path> ev = (WatchEvent<Path>) event; + Path fileName = ev.context(); + + //System.out.println(kind.name() + ": " + fileName); + + if (kind == ENTRY_CREATE && fileName.toString().equals(".~lock." + fileToEdit + "#")) { + editor = "libreoffice"; + } + if (kind == ENTRY_CREATE && fileName.toString().equals("~$" + fileToEdit.substring(2, fileToEdit.length())) ) { + editor = "office"; + } + if (kind == ENTRY_MODIFY && fileName.toString().equals(fileToEdit)) { + System.out.println("Fichier modifié!!!"); + Thread.sleep(3000); + File fileTotest = new File(userLocalDirTmp + File.separator + fileToEdit); + if (fileTotest.canRead()) { + String actualContent = FileManager.encodeFile(userLocalDirTmp + File.separator + fileToEdit); + if (!fileContentTosend.equals(actualContent)) { + fileContentTosend = actualContent; + logger.log("----------[SECURITY BACKUP] BEGIN SEND OF THE OBJECT----------", Level.INFO); + String urlToSave = url + "?action=saveObject&objectType=" + objectType + + "&objectTable=" + objectTable + "&objectId=" + objectId + + "&uniqueId=" + uniqueId + "&step=backup&userMaarch=" + userMaarch; + logger.log("[SECURITY BACKUP] URL TO SAVE : " + urlToSave, Level.INFO); + trayIcon.displayMessage("Maarch content editor", "Envoi du brouillon ...", TrayIcon.MessageType.INFO); + sendHttpRequest(urlToSave, fileContentTosend, false); + logger.log("[SECURITY BACKUP] MESSAGE STATUS : " + messageStatus, Level.INFO); + } + } else { + logger.log(userLocalDirTmp + fileToEdit + " FILE NOT READABLE !!!!!!", Level.INFO); + } + } + if (kind == ENTRY_CREATE && (fileName.toString().equals(".~lock." + fileToEdit + "#") || fileName.toString().equals("~$" + fileToEdit.substring(2, fileToEdit.length())))) { + System.out.println("Fichier fichier en cours d'édition ..."); + logger.log("----------BEGIN OPEN REQUEST----------", Level.INFO); + + urlToSend = url + "?action=editObject&objectType=" + objectType + + "&objectTable=" + objectTable + "&objectId=" + objectId + + "&uniqueId=" + uniqueId; + + + logger.log("FIRST URL CALL : " + urlToSend, Level.INFO); + sendHttpRequest(urlToSend, "none", false); + logger.log("MESSAGE STATUS : " + messageStatus, Level.INFO); + logger.log("MESSAGE RESULT : ", Level.INFO); + processReturn(messageResult); + logger.log("----------END OPEN REQUEST----------", Level.INFO); + + } + + if (kind == ENTRY_DELETE && (fileName.toString().equals(".~lock." + fileToEdit + "#") || fileName.toString().equals("~$" + fileToEdit.substring(2, fileToEdit.length())))) { + Thread.sleep(500); + File fileTotest = new File(userLocalDirTmp + File.separator +".~lock." + fileToEdit + "#"); + if(!fileTotest.exists() || editor.equals("office")) { + System.out.println("Fermeture de l'éditeur..."); + logger.log("----------END EXECUTION OF THE EDITOR----------", Level.INFO); + + logger.log("----------BEGIN RETRIEVE CONTENT OF THE OBJECT----------", Level.INFO); + + fileContentTosend = FileManager.encodeFile(userLocalDirTmp + File.separator + fileToEdit); + + logger.log("----------END RETRIEVE CONTENT OF THE OBJECT----------", Level.INFO); + + logger.log("conversion pdf ? " + convertPdf , Level.INFO); + + if ("true".equals(convertPdf)) { + if ((fileExtension.equalsIgnoreCase("docx") || fileExtension.equalsIgnoreCase("doc") || fileExtension.equalsIgnoreCase("docm") || fileExtension.equalsIgnoreCase("odt") || fileExtension.equalsIgnoreCase("ott"))) { + logger.log("----------CONVERSION PDF----------", Level.INFO); + String pdfFile = userLocalDirTmp + File.separator + "thefile_" + idApplet + ".pdf"; + createPDF(userLocalDirTmp + File.separator + fileToEdit, userLocalDirTmp, os); + File file=new File(pdfFile); + if (file.exists()) { + pdfContentTosend = FileManager.encodeFile(pdfFile); + fileToDelete.add(pdfFile); + + } else { + pdfContentTosend = "null"; + logger.log("ERREUR DE CONVERSION PDF !", Level.WARNING); + } + + logger.log("---------- FIN CONVERSION PDF----------", Level.INFO); + }else{ + pdfContentTosend = "not allowed"; + logger.log("Conversion not allowed for this extension : " + fileExtension, Level.INFO); + } + } + + trayIcon.displayMessage("Maarch content editor", "Envoi du document ...", TrayIcon.MessageType.INFO); + String urlToSave = url + "?action=saveObject&objectType=" + objectType + + "&objectTable=" + objectTable + "&objectId=" + objectId + + "&uniqueId=" + uniqueId + "&idApplet=" + idApplet + "&step=end&userMaarch=" + userMaarch + + "&onlyConvert=" + onlyConvert; + logger.log("----------BEGIN SEND OF THE OBJECT----------", Level.INFO); + logger.log("URL TO SAVE : " + urlToSave, Level.INFO); + sendHttpRequest(urlToSave, fileContentTosend, true); + logger.log("MESSAGE STATUS : " + messageStatus, Level.INFO); + logger.log("LAST MESSAGE RESULT : ", Level.INFO); + processReturn(messageResult); + + if ("true".equals(convertPdf)) { + if (pdfContentTosend == "null") { + endMessage = endMessage + " mais la conversion pdf n'a pas fonctionné (le document ne pourra pas être signé)"; + } + } + FileManager.deleteSpecificFilesOnDir(fileToDelete); + FileManager.deleteEnvDir(userLocalDirTmp + File.separator + idApplet + "_conv"); + logger.log("----------END SEND OF THE OBJECT----------", Level.INFO); + return; + } + } + } + + boolean valid = key.reset(); + if (!valid) { + break; + } + } + + } catch (IOException ex) { + System.err.println(ex); } + } + + public void launchOnlyConvert() throws UnsupportedEncodingException, InterruptedException, IOException, PrivilegedActionException, Exception { - public void run() { - try { - MaarchCM.launchProcess(); - } catch (PrivilegedActionException ex) { - Logger.getLogger(MaarchCM.class.getName()).log(Level.SEVERE, null, ex); - } catch (InterruptedException ex) { - Logger.getLogger(MaarchCM.class.getName()).log(Level.SEVERE, null, ex); - } catch (IllegalArgumentException ex) { - Logger.getLogger(MaarchCM.class.getName()).log(Level.SEVERE, null, ex); - } catch (IllegalAccessException ex) { - Logger.getLogger(MaarchCM.class.getName()).log(Level.SEVERE, null, ex); - } catch (InvocationTargetException ex) { - Logger.getLogger(MaarchCM.class.getName()).log(Level.SEVERE, null, ex); + String urlToSend = url + "?action=editObject&objectType=" + objectType + + "&objectTable=" + objectTable + "&objectId=" + objectId + + "&uniqueId=" + uniqueId; + + logger.log("ONLYCONVERT GET DOCUMENT : " + urlToSend, Level.INFO); + + sendHttpRequest(urlToSend, "none", false); + processReturn(messageResult); + + logger.log("MESSAGE STATUS : " + messageStatus, Level.INFO); + logger.log("MESSAGE RESULT : ", Level.INFO); + + fileToEdit = "thefile_" + idApplet + "." + fileExtension; + + logger.log("CREATE FILE IN LOCAL PATH :" + fileToEdit, Level.INFO); + + fM.createFile(fileContent, userLocalDirTmp + File.separator + fileToEdit); + fileToDelete.add(userLocalDirTmp + File.separator + fileToEdit); + fileContentTosend = FileManager.encodeFile(userLocalDirTmp + File.separator + fileToEdit); + if ((fileExtension.equalsIgnoreCase("docx") || fileExtension.equalsIgnoreCase("doc") || fileExtension.equalsIgnoreCase("docm") || fileExtension.equalsIgnoreCase("odt") || fileExtension.equalsIgnoreCase("ott"))) { + logger.log("----------CONVERSION PDF----------", Level.INFO); + String pdfFile = userLocalDirTmp + File.separator + "thefile_" + idApplet + ".pdf"; + createPDF(userLocalDirTmp + File.separator + fileToEdit, userLocalDirTmp, os); + File file=new File(pdfFile); + if (file.exists()) { + pdfContentTosend = FileManager.encodeFile(pdfFile); + fileToDelete.add(pdfFile); + + } else { + pdfContentTosend = "null"; + logger.log("ERREUR DE CONVERSION PDF !", Level.WARNING); } + + logger.log("---------- FIN CONVERSION PDF----------", Level.INFO); + }else{ + pdfContentTosend = "not allowed"; + logger.log("Conversion not allowed for this extension : " + fileExtension, Level.INFO); } + trayIcon.displayMessage("Maarch content editor", "Envoi du document ...", TrayIcon.MessageType.INFO); + String urlToSave = url + "?action=saveObject&objectType=" + objectType + + "&objectTable=" + objectTable + "&objectId=" + objectId + + "&uniqueId=" + uniqueId + "&idApplet=" + idApplet + "&step=end&userMaarch=" + userMaarch + + "&onlyConvert=" + onlyConvert; + logger.log("----------BEGIN SEND OF THE OBJECT----------", Level.INFO); + logger.log("URL TO SAVE : " + urlToSave, Level.INFO); + sendHttpRequest(urlToSave, fileContentTosend, true); + logger.log("MESSAGE STATUS : " + messageStatus, Level.INFO); + logger.log("LAST MESSAGE RESULT : ", Level.INFO); + processReturn(messageResult); + + FileManager.deleteSpecificFilesOnDir(fileToDelete); + FileManager.deleteEnvDir(userLocalDirTmp + File.separator + idApplet + "_conv"); + logger.log("----------END SEND OF THE OBJECT----------", Level.INFO); + return; + } + + public void endRequestApplet() throws UnsupportedEncodingException, InterruptedException { + fileContentTosend = ""; + String urlToSave = url + "?action=terminate&objectType=" + objectType + + "&objectTable=" + objectTable + "&objectId=" + objectId + + "&uniqueId=" + uniqueId + "&idApplet=" + idApplet + "&step=end&userMaarch=" + userMaarch + + "&onlyConvert=" + onlyConvert; + logger.log("REQUEST END APPLET : " + urlToSave, Level.INFO); + sendHttpRequest(urlToSave, "none", true); + logger.log("MESSAGE STATUS : " + messageStatus, Level.INFO); + logger.log("LAST MESSAGE RESULT : ", Level.INFO); + return; } + + } \ No newline at end of file