]> source.dussan.org Git - xmlgraphics-fop.git/commitdiff
FOP-3019: Merge useragent encryption params with fop.xconf
authorSimon Steiner <ssteiner@apache.org>
Sat, 26 Jun 2021 07:15:52 +0000 (07:15 +0000)
committerSimon Steiner <ssteiner@apache.org>
Sat, 26 Jun 2021 07:15:52 +0000 (07:15 +0000)
git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@1891054 13f79535-47bb-0310-9956-ffa450edef68

fop-core/src/main/java/org/apache/fop/render/pdf/PDFRendererConfig.java
fop-core/src/main/java/org/apache/fop/render/pdf/PDFRenderingUtil.java
fop-core/src/test/java/org/apache/fop/apps/AbstractRendererConfigParserTester.java
fop-core/src/test/java/org/apache/fop/render/pdf/PDFRendererConfigParserTestCase.java

index 1e95a8616b0d5b909d8cfcb4fa1da07cafbb3d89..28111ceed73c52035be05b9f94ae13252df84a60 100644 (file)
@@ -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) {
index 9ecff2148648d0e8dd3bbbc483f0cce9549513fe..1f383a29e2173aef293dba7e159858afa1268946 100644 (file)
@@ -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()) {
index 555f73226b1e93999673c75e376832ca65c39201..6b1846efc8b94c1f42e7ef9ce3a8875a10cef770 100644 (file)
@@ -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);
     }
 
index 4dd3a6610043c725cc2457cbf0e79e1886a91340..7b81b4db89f36b5d1054b9aee3f6e3af80ba1b04 100644 (file)
@@ -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;
@@ -95,6 +100,19 @@ public class PDFRendererConfigParserTestCase
         testRestrictEncryptionParameter(PDFEncryptionOption.NO_PRINTHQ);
     }
 
+    @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";