From 4415250ebc5f2b3cd785211dbc1e2afad4bb1239 Mon Sep 17 00:00:00 2001 From: bsayan2 Date: Fri, 19 Dec 2025 11:51:05 +0530 Subject: [PATCH] ACS-10505 used a temp office manager to create a temp user profile. --- .../patch/LibreOfficeProfileManager.java | 106 +++++++++++------- .../JodConverterSharedInstance.java | 33 ++++-- 2 files changed, 91 insertions(+), 48 deletions(-) diff --git a/engines/libreoffice/src/main/java/org/alfresco/transform/libreoffice/patch/LibreOfficeProfileManager.java b/engines/libreoffice/src/main/java/org/alfresco/transform/libreoffice/patch/LibreOfficeProfileManager.java index 31fc5a3b..bcb22a94 100644 --- a/engines/libreoffice/src/main/java/org/alfresco/transform/libreoffice/patch/LibreOfficeProfileManager.java +++ b/engines/libreoffice/src/main/java/org/alfresco/transform/libreoffice/patch/LibreOfficeProfileManager.java @@ -39,6 +39,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; import org.artofsolving.jodconverter.OfficeDocumentConverter; +import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration; import org.artofsolving.jodconverter.office.OfficeManager; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -60,21 +61,36 @@ public class LibreOfficeProfileManager private final File workDir; private final File templateProfileDir; - private final OfficeManager officeManager; + private final OfficeManager tempOfficeManager; private final boolean disableExternalLinks; + private static LibreOfficeProfileManager instance; - public LibreOfficeProfileManager(File workDir, File templateProfileDir, - OfficeManager officeManager, boolean disableExternalLinks) + private LibreOfficeProfileManager(File workDir, File templateProfileDir, + DefaultOfficeManagerConfiguration officeManagerConfiguration, + boolean disableExternalLinks) { this.workDir = workDir; this.templateProfileDir = templateProfileDir; - this.officeManager = officeManager; + this.tempOfficeManager = officeManagerConfiguration.buildOfficeManager(); this.disableExternalLinks = disableExternalLinks; + this.tempOfficeManager.start(); } - public void setupTemplateUserProfile() throws Exception + public static void initializeTemplateUserProfile(File workDir, File templateProfileDir, + DefaultOfficeManagerConfiguration officeManagerConfiguration, + boolean disableExternalLinks) throws Exception { - OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager); + if (instance == null) + { + instance = new LibreOfficeProfileManager(workDir, templateProfileDir, + officeManagerConfiguration, disableExternalLinks); + } + instance.execute(); + } + + private void execute() throws Exception + { + OfficeDocumentConverter converter = new OfficeDocumentConverter(tempOfficeManager); convertProbeDocument(converter); copyUserProfile(); @@ -82,6 +98,13 @@ public class LibreOfficeProfileManager { patchLibreOfficeRegistry(); } + if (tempOfficeManager.isRunning()) + { + tempOfficeManager.stop(); + } + + // delete everything in workDir to ensure a fresh start next time + FileUtils.cleanDirectory(workDir); } /** @@ -105,7 +128,7 @@ public class LibreOfficeProfileManager private void copyUserProfile() throws Exception { - File officeUserProfile = findLibreOfficeUserProfile(workDir); + File officeUserProfile = findLibreOfficeDirectory(workDir, USER_PROFILE_DIR); if (officeUserProfile != null) { File destination = new File(templateProfileDir, officeUserProfile.getName()); @@ -118,48 +141,55 @@ public class LibreOfficeProfileManager * * @throws Exception */ - private void patchLibreOfficeRegistry() throws Exception + private void patchLibreOfficeRegistry() { - File userProfileDir = findLibreOfficeUserProfile(templateProfileDir); - if (userProfileDir == null) + try { - throw new IllegalStateException("Cannot find LO user profile to patch"); - } + File userProfileDir = findLibreOfficeDirectory(templateProfileDir, USER_PROFILE_DIR); + if (userProfileDir == null) + { + throw new IllegalStateException("Cannot find LO user profile to patch"); + } - File registry = new File(userProfileDir, REGISTRY_FILE); - if (!registry.exists()) + File registry = new File(userProfileDir, REGISTRY_FILE); + if (!registry.exists()) + { + throw new IllegalStateException(REGISTRY_FILE + " not found!"); + } + + String registryContent = FileUtils.readFileToString(registry, StandardCharsets.UTF_8); + List patchItems = readPatchItemsFromJson(); + + // Remove existing matching items + for (PatchItem item : patchItems) + { + String pattern = String.format( + "]*>.*?", + Pattern.quote(item.path), + Pattern.quote(item.propName)); + registryContent = registryContent.replaceAll(pattern, ""); + } + + // Insert the new patch before closing tag + String patch = generatePatchXml(patchItems); + registryContent = registryContent.replace("", " " + patch + "\n"); + + FileUtils.writeStringToFile(registry, registryContent, StandardCharsets.UTF_8); + } + catch (Exception e) { - throw new IllegalStateException(REGISTRY_FILE + " not found!"); + logger.error("Error patching LibreOffice registry to disable external link updates", e); } - - String registryContent = FileUtils.readFileToString(registry, StandardCharsets.UTF_8); - List patchItems = readPatchItemsFromJson(); - - // Remove existing matching items - for (PatchItem item : patchItems) - { - String pattern = String.format( - "]*>.*?", - Pattern.quote(item.path), - Pattern.quote(item.propName)); - registryContent = registryContent.replaceAll(pattern, ""); - } - - // Insert the new patch before closing tag - String patch = generatePatchXml(patchItems); - registryContent = registryContent.replace("", " " + patch + "\n"); - - FileUtils.writeStringToFile(registry, registryContent, StandardCharsets.UTF_8); } - private File findLibreOfficeUserProfile(File dir) + private File findLibreOfficeDirectory(File parentDir, String searchDir) { - File userDir = findDirectChild(dir, USER_PROFILE_DIR); + File userDir = findDirectChild(parentDir, searchDir); if (userDir != null) { return userDir; } - return findUserInJodConverterDirs(dir); + return findUserInJodConverterDirs(parentDir); } private File findDirectChild(File dir, String name) @@ -188,7 +218,7 @@ public class LibreOfficeProfileManager for (File d : jodDirs) { - File user = new File(d, "user"); + File user = new File(d, USER_PROFILE_DIR); if (user.exists()) { return user; diff --git a/engines/libreoffice/src/main/java/org/alfresco/transform/libreoffice/transformers/JodConverterSharedInstance.java b/engines/libreoffice/src/main/java/org/alfresco/transform/libreoffice/transformers/JodConverterSharedInstance.java index 9f245863..60ae0850 100644 --- a/engines/libreoffice/src/main/java/org/alfresco/transform/libreoffice/transformers/JodConverterSharedInstance.java +++ b/engines/libreoffice/src/main/java/org/alfresco/transform/libreoffice/transformers/JodConverterSharedInstance.java @@ -168,6 +168,20 @@ public class JodConverterSharedInstance implements JodConverter throw new RuntimeException( "OpenOffice template profile directory " + templateProfileDir + " does not exist."); } + else + { + // check if it contains user subdir and create a sub dir if not + File userDir = new File(tmp, "user"); + if (!userDir.exists() || !userDir.isDirectory()) + { + File newUserDir = new File(tmp, "user"); + if (!newUserDir.mkdir()) + { + throw new RuntimeException( + "Could not create user subdirectory in template profile directory " + templateProfileDir); + } + } + } this.templateProfileDir = tmp; } } @@ -378,20 +392,19 @@ public class JodConverterSharedInstance implements JodConverter defaultOfficeMgrConfig.setConnectTimeout(connectTimeout); } + if (workDir != null && templateProfileDir != null) + { + LibreOfficeProfileManager.initializeTemplateUserProfile( + workDir, + templateProfileDir, + defaultOfficeMgrConfig, + disableExternalLinks); + } + // Try to configure and start the JodConverter library. officeManager = defaultOfficeMgrConfig.buildOfficeManager(); officeManager.start(); - if (workDir != null && templateProfileDir != null) - { - LibreOfficeProfileManager profileManager = new LibreOfficeProfileManager( - workDir, - templateProfileDir, - officeManager, - disableExternalLinks); - profileManager.setupTemplateUserProfile(); - } - } catch (IllegalStateException e) {