Author: ssteiner
Date: Sat Jun 26 07:15:52 2021
New Revision: 1891054

URL: http://svn.apache.org/viewvc?rev=1891054&view=rev
Log:
FOP-3019: Merge useragent encryption params with fop.xconf

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

Modified: 
xmlgraphics/fop/trunk/fop-core/src/main/java/org/apache/fop/render/pdf/PDFRendererConfig.java
URL: 
http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/fop-core/src/main/java/org/apache/fop/render/pdf/PDFRendererConfig.java?rev=1891054&r1=1891053&r2=1891054&view=diff
==============================================================================
--- 
xmlgraphics/fop/trunk/fop-core/src/main/java/org/apache/fop/render/pdf/PDFRendererConfig.java
 (original)
+++ 
xmlgraphics/fop/trunk/fop-core/src/main/java/org/apache/fop/render/pdf/PDFRendererConfig.java
 Sat Jun 26 07:15:52 2021
@@ -161,20 +161,48 @@ public final class PDFRendererConfig imp
         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 imp
             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) {

Modified: 
xmlgraphics/fop/trunk/fop-core/src/main/java/org/apache/fop/render/pdf/PDFRenderingUtil.java
URL: 
http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/fop-core/src/main/java/org/apache/fop/render/pdf/PDFRenderingUtil.java?rev=1891054&r1=1891053&r2=1891054&view=diff
==============================================================================
--- 
xmlgraphics/fop/trunk/fop-core/src/main/java/org/apache/fop/render/pdf/PDFRenderingUtil.java
 (original)
+++ 
xmlgraphics/fop/trunk/fop-core/src/main/java/org/apache/fop/render/pdf/PDFRenderingUtil.java
 Sat Jun 26 07:15:52 2021
@@ -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()) {

Modified: 
xmlgraphics/fop/trunk/fop-core/src/test/java/org/apache/fop/apps/AbstractRendererConfigParserTester.java
URL: 
http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/fop-core/src/test/java/org/apache/fop/apps/AbstractRendererConfigParserTester.java?rev=1891054&r1=1891053&r2=1891054&view=diff
==============================================================================
--- 
xmlgraphics/fop/trunk/fop-core/src/test/java/org/apache/fop/apps/AbstractRendererConfigParserTester.java
 (original)
+++ 
xmlgraphics/fop/trunk/fop-core/src/test/java/org/apache/fop/apps/AbstractRendererConfigParserTester.java
 Sat Jun 26 07:15:52 2021
@@ -51,15 +51,19 @@ public abstract class AbstractRendererCo
     }
 
     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);
     }
 

Modified: 
xmlgraphics/fop/trunk/fop-core/src/test/java/org/apache/fop/render/pdf/PDFRendererConfigParserTestCase.java
URL: 
http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/fop-core/src/test/java/org/apache/fop/render/pdf/PDFRendererConfigParserTestCase.java?rev=1891054&r1=1891053&r2=1891054&view=diff
==============================================================================
--- 
xmlgraphics/fop/trunk/fop-core/src/test/java/org/apache/fop/render/pdf/PDFRendererConfigParserTestCase.java
 (original)
+++ 
xmlgraphics/fop/trunk/fop-core/src/test/java/org/apache/fop/render/pdf/PDFRendererConfigParserTestCase.java
 Sat Jun 26 07:15:52 2021
@@ -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.assertFal
 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 PDFRendererConfigParserTest
     }
 
     @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()



---------------------------------------------------------------------
To unsubscribe, e-mail: fop-commits-unsubscr...@xmlgraphics.apache.org
For additional commands, e-mail: fop-commits-h...@xmlgraphics.apache.org

Reply via email to