diff options
author | Simon Steiner <ssteiner@apache.org> | 2021-06-26 07:15:52 +0000 |
---|---|---|
committer | Simon Steiner <ssteiner@apache.org> | 2021-06-26 07:15:52 +0000 |
commit | fdde94a7aa524b3ac0e50d1306634fc99c5a2e4d (patch) | |
tree | 89d808179063a14409c5c8f8039a153f51d3d139 /fop-core/src | |
parent | 623169e4d96680930179c0e98e072d2674947cd4 (diff) | |
download | xmlgraphics-fop-fdde94a7aa524b3ac0e50d1306634fc99c5a2e4d.tar.gz xmlgraphics-fop-fdde94a7aa524b3ac0e50d1306634fc99c5a2e4d.zip |
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
Diffstat (limited to 'fop-core/src')
4 files changed, 68 insertions, 31 deletions
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<PDFRendererOption, Object> properties = new EnumMap<PDFRendererOption, Object>(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<B extends RendererConfB } protected void parseConfig(B rendererConfBuilder) throws Exception { - DefaultConfigurationBuilder cfgBuilder = new DefaultConfigurationBuilder(); - Configuration cfg = cfgBuilder.build(rendererConfBuilder.endRendererConfig().build()) - .getChild("renderers") - .getChild("renderer"); FOUserAgent userAgent = mock(FOUserAgent.class); when(userAgent.validateStrictly()).thenReturn(true); FontManager fontManager = mock(FontManager.class); when(userAgent.getFontManager()).thenReturn(fontManager); when(userAgent.getEventBroadcaster()).thenReturn(new DefaultEventBroadcaster()); + parseConfig(rendererConfBuilder, userAgent); + } + + protected void parseConfig(B rendererConfBuilder, FOUserAgent userAgent) throws Exception { + DefaultConfigurationBuilder cfgBuilder = new DefaultConfigurationBuilder(); + Configuration cfg = cfgBuilder.build(rendererConfBuilder.endRendererConfig().build()) + .getChild("renderers") + .getChild("renderer"); conf = (C) configBuilder.build(userAgent, cfg); } diff --git a/fop-core/src/test/java/org/apache/fop/render/pdf/PDFRendererConfigParserTestCase.java b/fop-core/src/test/java/org/apache/fop/render/pdf/PDFRendererConfigParserTestCase.java index 4dd3a6610..7b81b4db8 100644 --- a/fop-core/src/test/java/org/apache/fop/render/pdf/PDFRendererConfigParserTestCase.java +++ b/fop-core/src/test/java/org/apache/fop/render/pdf/PDFRendererConfigParserTestCase.java @@ -19,6 +19,8 @@ package org.apache.fop.render.pdf; +import java.io.File; + import org.junit.Test; import static org.junit.Assert.assertEquals; @@ -26,8 +28,11 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import org.apache.fop.apps.AbstractRendererConfigParserTester; +import org.apache.fop.apps.FOUserAgent; +import org.apache.fop.apps.FopFactory; import org.apache.fop.apps.PDFRendererConfBuilder; import org.apache.fop.pdf.PDFAMode; +import org.apache.fop.pdf.PDFEncryptionParams; import org.apache.fop.pdf.PDFXMode; import org.apache.fop.pdf.Version; import org.apache.fop.render.pdf.PDFRendererConfig.PDFRendererConfigParser; @@ -96,6 +101,19 @@ public class PDFRendererConfigParserTestCase } @Test + public void testMergeEncryptionParams() throws Exception { + FOUserAgent userAgent = FopFactory.newInstance(new File(".").toURI()).newFOUserAgent(); + PDFEncryptionParams params = new PDFEncryptionParams(); + String testPassword = "x"; + params.setUserPassword(testPassword); + userAgent.getRendererOptions().put(PDFEncryptionOption.ENCRYPTION_PARAMS, params); + parseConfig(createRenderer() + .startEncryptionParams() + .endEncryptionParams(), userAgent); + assertEquals(testPassword, conf.getConfigOptions().getEncryptionParameters().getUserPassword()); + } + + @Test public void testOwnerPassword() throws Exception { String testPassword = "this is a password purely for test purposes"; parseConfig(createRenderer() |