diff --git a/engines/libreoffice/src/main/java/org/alfresco/transform/libreoffice/patch/LibreOfficeProfileManagerV2.java b/engines/libreoffice/src/main/java/org/alfresco/transform/libreoffice/patch/LibreOfficeProfileManagerV2.java index 6075e9d4..950005e5 100644 --- a/engines/libreoffice/src/main/java/org/alfresco/transform/libreoffice/patch/LibreOfficeProfileManagerV2.java +++ b/engines/libreoffice/src/main/java/org/alfresco/transform/libreoffice/patch/LibreOfficeProfileManagerV2.java @@ -36,6 +36,11 @@ import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +/** + * Manages LibreOffice user profile templates for transformations. + * + * @author Sayan Bhattacharya + */ public class LibreOfficeProfileManagerV2 { private static final Logger logger = LoggerFactory.getLogger(LibreOfficeProfileManagerV2.class); @@ -43,37 +48,20 @@ public class LibreOfficeProfileManagerV2 private final String USER_DIR_NAME = "user"; private final String REGISTRY_FILE_NAME = "registrymodifications.xcu"; private final String LOCAL_TEMP_REGISTRY_FILE = "templateRegistrymodifications.xcu"; - private final String DEFAULT_TEMP_PROFILE = "libreoffice/templateProfile"; + private final String DEFAULT_LO_TEMPLATE_PROFILE = "libreoffice_templateProfile"; + private final String DEFAULT_ALFRESCO = "default_alfresco"; - private final String userTemplateDir; - private String systemTempUserDir = ""; + private final String configuredTemplateProfileDir; + private String tempDefaultTemplateDir; - public LibreOfficeProfileManagerV2(String templateProfileDir) + public LibreOfficeProfileManagerV2(String configuredTemplateProfileDir) { - this.userTemplateDir = templateProfileDir; + this.configuredTemplateProfileDir = configuredTemplateProfileDir; } - public String getTemplateProfileDir() + public String getEffectiveTemplateProfileDir() { - execute(); - - if (StringUtils.isNotBlank(userTemplateDir)) - { - return userTemplateDir; - } - else if (StringUtils.isNotBlank(systemTempUserDir)) - { - return systemTempUserDir; - } - else - { - return ""; - } - } - - private void execute() - { - if (StringUtils.isNotBlank(userTemplateDir)) + if (isDefaultAlfrescoClasspath(configuredTemplateProfileDir)) { validateAndCreateRegistryTemplate(); } @@ -81,37 +69,37 @@ public class LibreOfficeProfileManagerV2 { checkUserProvidedRegistry(); } + + return StringUtils.isBlank(tempDefaultTemplateDir) ? configuredTemplateProfileDir : tempDefaultTemplateDir; + } + + private boolean isDefaultAlfrescoClasspath(String templateDir) + { + return DEFAULT_ALFRESCO.equals(templateDir); } private void validateAndCreateRegistryTemplate() { - if (userTemplateDir.contains("classpath")) + try (InputStream regStream = loadRegistryStream()) { - String[] split = userTemplateDir.split(":"); - if (split.length == 2) + if (regStream == null) { - String classpathTemplateProfileDir = split[1]; - if (DEFAULT_TEMP_PROFILE.equals(classpathTemplateProfileDir)) - { - try (InputStream regStream = getClass().getClassLoader().getResourceAsStream(LOCAL_TEMP_REGISTRY_FILE)) - { - if (regStream == null) - { - logger.error("Local temporary registry file not found: {}", LOCAL_TEMP_REGISTRY_FILE); - return; - } - Path tempProfilePath = Files.createTempDirectory(DEFAULT_TEMP_PROFILE); - File registryFile = getRegistryFile(tempProfilePath); - Files.copy(regStream, registryFile.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING); - this.systemTempUserDir = tempProfilePath.toString(); - } - catch (Exception e) - { - logger.error("Error creating temporary directory for LibreOffice profile", e); - } - } + logger.error("Local temporary registry file not found: {}", LOCAL_TEMP_REGISTRY_FILE); + return; } + Path tempProfilePath = Files.createTempDirectory(DEFAULT_LO_TEMPLATE_PROFILE); + Files.copy(regStream, getRegistryFile(tempProfilePath).toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING); + this.tempDefaultTemplateDir = tempProfilePath.toString(); } + catch (Exception e) + { + logger.error("Error creating temporary directory for LibreOffice profile", e); + } + } + + private InputStream loadRegistryStream() + { + return getClass().getClassLoader().getResourceAsStream(LOCAL_TEMP_REGISTRY_FILE); } private File getRegistryFile(Path tempProfilePath) @@ -130,10 +118,10 @@ public class LibreOfficeProfileManagerV2 private void checkUserProvidedRegistry() { - File tempDir = new File(userTemplateDir); + File tempDir = new File(configuredTemplateProfileDir); if (!tempDir.exists() || !tempDir.isDirectory()) { - logger.warn("The provided template profile directory does not exist or is not a directory: {}", userTemplateDir); + logger.warn("The provided template profile directory does not exist or is not a directory: {}", configuredTemplateProfileDir); return; } File userDir = new File(tempDir, USER_DIR_NAME); diff --git a/engines/libreoffice/src/main/java/org/alfresco/transform/libreoffice/transformers/LibreOfficeTransformer.java b/engines/libreoffice/src/main/java/org/alfresco/transform/libreoffice/transformers/LibreOfficeTransformer.java index d609bda2..942d96fd 100644 --- a/engines/libreoffice/src/main/java/org/alfresco/transform/libreoffice/transformers/LibreOfficeTransformer.java +++ b/engines/libreoffice/src/main/java/org/alfresco/transform/libreoffice/transformers/LibreOfficeTransformer.java @@ -120,8 +120,8 @@ public class LibreOfficeTransformer implements JavaExecutor, CustomTransformerFi throw new IllegalArgumentException("LibreOfficeTransformer LIBREOFFICE_IS_ENABLED variable must be set to true/false"); } - LibreOfficeProfileManagerV2 lib = new LibreOfficeProfileManagerV2(templateProfileDir); - String tempDir = lib.getTemplateProfileDir(); + LibreOfficeProfileManagerV2 profileManager = new LibreOfficeProfileManagerV2(templateProfileDir); + String effectiveTemplateProfileDir = profileManager.getEffectiveTemplateProfileDir(); JodConverterSharedInstance sharedInstance = new JodConverterSharedInstance(); jodconverter = sharedInstance; @@ -132,7 +132,7 @@ public class LibreOfficeTransformer implements JavaExecutor, CustomTransformerFi sharedInstance.setConnectTimeout(timeout); sharedInstance.setPortNumbers(portNumbers); sharedInstance.setEnabled(isEnabled); - sharedInstance.setTemplateProfileDir(tempDir); + sharedInstance.setTemplateProfileDir(effectiveTemplateProfileDir); sharedInstance.afterPropertiesSet(); } diff --git a/engines/libreoffice/src/main/resources/application-default.yaml b/engines/libreoffice/src/main/resources/application-default.yaml index c7bf2608..9676a50a 100644 --- a/engines/libreoffice/src/main/resources/application-default.yaml +++ b/engines/libreoffice/src/main/resources/application-default.yaml @@ -7,5 +7,5 @@ transform: maxTasksPerProcess: ${LIBREOFFICE_MAX_TASKS_PER_PROCESS:200} timeout: ${LIBREOFFICE_TIMEOUT:1200000} portNumbers: ${LIBREOFFICE_PORT_NUMBERS:8100} - templateProfileDir: ${LIBREOFFICE_TEMPLATE_PROFILE_DIR:classpath:libreoffice/templateProfile} + templateProfileDir: ${LIBREOFFICE_TEMPLATE_PROFILE_DIR:default_alfresco} isEnabled: ${LIBREOFFICE_IS_ENABLED:true} \ No newline at end of file diff --git a/pom.xml b/pom.xml index eca07090..e239abd2 100644 --- a/pom.xml +++ b/pom.xml @@ -21,7 +21,7 @@ 11 latest quay.io - 3.0.6 + 3.0.5 3.0.1.20 ${project.version} 2.19.2