From: Simon Steiner Date: Sat, 26 Jun 2021 07:15:52 +0000 (+0000) Subject: FOP-3019: Merge useragent encryption params with fop.xconf X-Git-Tag: fop-2_7~30 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=fdde94a7aa524b3ac0e50d1306634fc99c5a2e4d;p=xmlgraphics-fop.git FOP-3019: Merge useragent encryption params with fop.xconf git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@1891054 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/fop-core/src/main/java/org/apache/fop/render/pdf/PDFRendererConfig.java b/fop-core/src/main/java/org/apache/fop/render/pdf/PDFRendererConfig.java index 1e95a8616..28111ceed 100644 --- a/fop-core/src/main/java/org/apache/fop/render/pdf/PDFRendererConfig.java +++ b/fop-core/src/main/java/org/apache/fop/render/pdf/PDFRendererConfig.java @@ -161,20 +161,48 @@ public final class PDFRendererConfig implements RendererConfig { private void configureEncryptionParams(Configuration cfg, FOUserAgent userAgent, boolean strict) { Configuration encryptCfg = cfg.getChild(ENCRYPTION_PARAMS, false); if (encryptCfg != null) { - encryptionConfig = new PDFEncryptionParams(); - encryptionConfig.setOwnerPassword(parseConfig(encryptCfg, OWNER_PASSWORD)); - encryptionConfig.setUserPassword(parseConfig(encryptCfg, USER_PASSWORD)); - encryptionConfig.setAllowPrint(!doesValueExist(encryptCfg, NO_PRINT)); - encryptionConfig.setAllowCopyContent(!doesValueExist(encryptCfg, NO_COPY_CONTENT)); - encryptionConfig.setAllowEditContent(!doesValueExist(encryptCfg, NO_EDIT_CONTENT)); - encryptionConfig.setAllowEditAnnotations(!doesValueExist(encryptCfg, NO_ANNOTATIONS)); - encryptionConfig.setAllowFillInForms(!doesValueExist(encryptCfg, NO_FILLINFORMS)); - encryptionConfig.setAllowAccessContent(!doesValueExist(encryptCfg, NO_ACCESSCONTENT)); - encryptionConfig.setAllowAssembleDocument(!doesValueExist(encryptCfg, NO_ASSEMBLEDOC)); - encryptionConfig.setAllowPrintHq(!doesValueExist(encryptCfg, NO_PRINTHQ)); - encryptionConfig.setEncryptMetadata(getConfigValue(encryptCfg, ENCRYPT_METADATA, true)); + encryptionConfig = PDFRenderingUtil.createFromUserAgent(userAgent).getEncryptionParameters(); + if (encryptionConfig == null) { + encryptionConfig = new PDFEncryptionParams(); + } + String ownerPassword = parseConfig(encryptCfg, OWNER_PASSWORD); + if (doesValueExist(encryptCfg, OWNER_PASSWORD)) { + encryptionConfig.setOwnerPassword(ownerPassword); + } + String userPassword = parseConfig(encryptCfg, USER_PASSWORD); + if (doesValueExist(encryptCfg, USER_PASSWORD)) { + encryptionConfig.setUserPassword(userPassword); + } + if (doesValueExist(encryptCfg, NO_PRINT)) { + encryptionConfig.setAllowPrint(false); + } + if (doesValueExist(encryptCfg, NO_COPY_CONTENT)) { + encryptionConfig.setAllowCopyContent(false); + } + if (doesValueExist(encryptCfg, NO_EDIT_CONTENT)) { + encryptionConfig.setAllowEditContent(false); + } + if (doesValueExist(encryptCfg, NO_ANNOTATIONS)) { + encryptionConfig.setAllowEditAnnotations(false); + } + if (doesValueExist(encryptCfg, NO_FILLINFORMS)) { + encryptionConfig.setAllowFillInForms(false); + } + if (doesValueExist(encryptCfg, NO_ACCESSCONTENT)) { + encryptionConfig.setAllowAccessContent(false); + } + if (doesValueExist(encryptCfg, NO_ASSEMBLEDOC)) { + encryptionConfig.setAllowAssembleDocument(false); + } + if (doesValueExist(encryptCfg, NO_PRINTHQ)) { + encryptionConfig.setAllowPrintHq(false); + } + String encryptMetadata = parseConfig(encryptCfg, ENCRYPT_METADATA); + if (doesValueExist(encryptCfg, ENCRYPT_METADATA)) { + encryptionConfig.setEncryptMetadata(Boolean.parseBoolean(encryptMetadata)); + } String encryptionLength = parseConfig(encryptCfg, ENCRYPTION_LENGTH); - if (encryptionLength != null) { + if (doesValueExist(encryptCfg, ENCRYPTION_LENGTH)) { int validatedLength = checkEncryptionLength(Integer.parseInt(encryptionLength), userAgent); encryptionConfig.setEncryptionLengthInBits(validatedLength); } @@ -229,19 +257,6 @@ public final class PDFRendererConfig implements RendererConfig { return cfg.getChild(option.getName(), false) != null; } - private boolean getConfigValue(Configuration cfg, RendererConfigOption option, boolean defaultTo) { - if (cfg.getChild(option.getName(), false) != null) { - Configuration child = cfg.getChild(option.getName()); - try { - return child.getValueAsBoolean(); - } catch (ConfigurationException e) { - return defaultTo; - } - } else { - return defaultTo; - } - } - private int checkEncryptionLength(int encryptionLength, FOUserAgent userAgent) { int correctEncryptionLength = encryptionLength; if (encryptionLength < 40) { diff --git a/fop-core/src/main/java/org/apache/fop/render/pdf/PDFRenderingUtil.java b/fop-core/src/main/java/org/apache/fop/render/pdf/PDFRenderingUtil.java index 9ecff2148..1f383a29e 100644 --- a/fop-core/src/main/java/org/apache/fop/render/pdf/PDFRenderingUtil.java +++ b/fop-core/src/main/java/org/apache/fop/render/pdf/PDFRenderingUtil.java @@ -136,7 +136,7 @@ class PDFRenderingUtil { } } - private static PDFRendererOptionsConfig createFromUserAgent(FOUserAgent userAgent) { + protected static PDFRendererOptionsConfig createFromUserAgent(FOUserAgent userAgent) { Map properties = new EnumMap(PDFRendererOption.class); for (PDFRendererOption option : PDFRendererOption.values()) { diff --git a/fop-core/src/test/java/org/apache/fop/apps/AbstractRendererConfigParserTester.java b/fop-core/src/test/java/org/apache/fop/apps/AbstractRendererConfigParserTester.java index 555f73226..6b1846efc 100644 --- a/fop-core/src/test/java/org/apache/fop/apps/AbstractRendererConfigParserTester.java +++ b/fop-core/src/test/java/org/apache/fop/apps/AbstractRendererConfigParserTester.java @@ -51,15 +51,19 @@ public abstract class AbstractRendererConfigParserTester