This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch backport/31062-to-camel-4-22-x in repository https://gitbox.apache.org/repos/asf/camel.git
commit 306829940731053cea47ec0c54ba0d5d38e8ad43 Author: Claus Ibsen <[email protected]> AuthorDate: Sat Nov 28 09:36:45 2026 +0100 CAMEL-27751: camel-fop - apply the CamelFop.Render and CamelFop.Encrypt header values as given (#31062) The fop producer now maps the supported CamelFop.Render.* and CamelFop.Encrypt.* headers to the setters of FOUserAgent and PDFEncryptionParams, and converts each value to the type of the setter with the type converter, instead of binding all such headers with PropertyBindingSupport like endpoint options. Option names are matched case-insensitively, other names are ignored with a WARN. The component doc lists all supported headers, and the upgrade guides for 4.24, 4.22.4 and 4.18.7 describe the change. Signed-off-by: Claus Ibsen <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]> (cherry picked from commit d9d5691389529bb67fa1f806fbdcb1e98ee99859) Signed-off-by: Claus Ibsen <[email protected]> --- .../apache/camel/catalog/docs/fop-component.adoc | 33 +++++- .../camel-fop/src/main/docs/fop-component.adoc | 33 +++++- .../apache/camel/component/fop/FopProducer.java | 91 ++++++++++++++--- .../component/fop/FopEncryptionHeadersTest.java | 74 ++++++++++++++ .../component/fop/FopMetadataHeadersTest.java | 113 +++++++++++++++++++++ 5 files changed, 328 insertions(+), 16 deletions(-) diff --git a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/fop-component.adoc b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/fop-component.adoc index 4b8140c6875c..7330e7407bbc 100644 --- a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/fop-component.adoc +++ b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/fop-component.adoc @@ -91,6 +91,13 @@ loaded from the file system. === Message Operations +The `CamelFop.Render.*` headers set the metadata and rendering options of each document, and the `CamelFop.Encrypt.*` +headers encrypt a PDF document. The option names are matched case-insensitively (`CamelFop.Render.Creator` sets the +creator too). The values are used as given and converted to the type of the option, so a route that needs a property +placeholder or a bean in such a value resolves it before the `fop` endpoint, for example with the +xref:languages:simple-language.adoc[Simple] language. Other names are ignored, and a WARN is logged. Other FOP user +agent settings are set in the <<Configuration file>> or on a custom `FopFactory`. + [width="100%",cols="10%,10%,80%",options="header",] |======================================================================= |name |default value |description @@ -103,17 +110,29 @@ loaded from the file system. |`CamelFop.Encrypt.allowPrint` | `true` |Allows printing the PDF +|`CamelFop.Encrypt.allowPrintHq` | `true` |Allows printing the PDF in high quality + |`CamelFop.Encrypt.allowCopyContent` |`true` |Allows copying content of the PDF |`CamelFop.Encrypt.allowEditContent` |`true` |Allows editing content of the PDF |`CamelFop.Encrypt.allowEditAnnotations` |`true` |Allows editing annotation of the PDF +|`CamelFop.Encrypt.allowFillInForms` |`true` |Allows filling in the forms of the PDF + +|`CamelFop.Encrypt.allowAccessContent` |`true` |Allows extracting content of the PDF for accessibility + +|`CamelFop.Encrypt.allowAssembleDocument` |`true` |Allows assembling the PDF (inserting, rotating and deleting pages) + +|`CamelFop.Encrypt.encryptMetadata` |`true` |Whether the metadata of the PDF is encrypted + +|`CamelFop.Encrypt.encryptionLengthInBits` |`128` |The length of the encryption key in bits + |`CamelFop.Render.producer` |Apache FOP |Metadata element for the system/software that produces the document |`CamelFop.Render.creator` | | Metadata element for the user that created the document -|`CamelFop.Render.creationDate` | | Creation Date +|`CamelFop.Render.creationDate` | | Creation Date, as a `java.util.Date` |`CamelFop.Render.author` | | Author of the content of the document @@ -122,6 +141,18 @@ loaded from the file system. |`CamelFop.Render.subject` | | Subject of the document |`CamelFop.Render.keywords` | | Set of keywords applicable to this document + +|`CamelFop.Render.targetResolution` | |The target resolution in dpi (the FOP configuration sets the default) + +|`CamelFop.Render.accessibility` | |Whether the accessibility features (tagged PDF) are enabled (the FOP configuration sets the default) + +|`CamelFop.Render.pdfUAEnabled` | |Whether the document conforms to PDF/UA + +|`CamelFop.Render.keepEmptyTags` | |Whether empty tags are kept in the structure tree of a tagged PDF (the FOP configuration sets the default) + +|`CamelFop.Render.locatorEnabled` |`true` |Whether the location of an FO element is kept for error messages + +|`CamelFop.Render.conserveMemoryPolicy` | |Whether FOP writes pages to a temporary file to save memory |======================================================================= === Example diff --git a/components/camel-fop/src/main/docs/fop-component.adoc b/components/camel-fop/src/main/docs/fop-component.adoc index 4b8140c6875c..7330e7407bbc 100644 --- a/components/camel-fop/src/main/docs/fop-component.adoc +++ b/components/camel-fop/src/main/docs/fop-component.adoc @@ -91,6 +91,13 @@ loaded from the file system. === Message Operations +The `CamelFop.Render.*` headers set the metadata and rendering options of each document, and the `CamelFop.Encrypt.*` +headers encrypt a PDF document. The option names are matched case-insensitively (`CamelFop.Render.Creator` sets the +creator too). The values are used as given and converted to the type of the option, so a route that needs a property +placeholder or a bean in such a value resolves it before the `fop` endpoint, for example with the +xref:languages:simple-language.adoc[Simple] language. Other names are ignored, and a WARN is logged. Other FOP user +agent settings are set in the <<Configuration file>> or on a custom `FopFactory`. + [width="100%",cols="10%,10%,80%",options="header",] |======================================================================= |name |default value |description @@ -103,17 +110,29 @@ loaded from the file system. |`CamelFop.Encrypt.allowPrint` | `true` |Allows printing the PDF +|`CamelFop.Encrypt.allowPrintHq` | `true` |Allows printing the PDF in high quality + |`CamelFop.Encrypt.allowCopyContent` |`true` |Allows copying content of the PDF |`CamelFop.Encrypt.allowEditContent` |`true` |Allows editing content of the PDF |`CamelFop.Encrypt.allowEditAnnotations` |`true` |Allows editing annotation of the PDF +|`CamelFop.Encrypt.allowFillInForms` |`true` |Allows filling in the forms of the PDF + +|`CamelFop.Encrypt.allowAccessContent` |`true` |Allows extracting content of the PDF for accessibility + +|`CamelFop.Encrypt.allowAssembleDocument` |`true` |Allows assembling the PDF (inserting, rotating and deleting pages) + +|`CamelFop.Encrypt.encryptMetadata` |`true` |Whether the metadata of the PDF is encrypted + +|`CamelFop.Encrypt.encryptionLengthInBits` |`128` |The length of the encryption key in bits + |`CamelFop.Render.producer` |Apache FOP |Metadata element for the system/software that produces the document |`CamelFop.Render.creator` | | Metadata element for the user that created the document -|`CamelFop.Render.creationDate` | | Creation Date +|`CamelFop.Render.creationDate` | | Creation Date, as a `java.util.Date` |`CamelFop.Render.author` | | Author of the content of the document @@ -122,6 +141,18 @@ loaded from the file system. |`CamelFop.Render.subject` | | Subject of the document |`CamelFop.Render.keywords` | | Set of keywords applicable to this document + +|`CamelFop.Render.targetResolution` | |The target resolution in dpi (the FOP configuration sets the default) + +|`CamelFop.Render.accessibility` | |Whether the accessibility features (tagged PDF) are enabled (the FOP configuration sets the default) + +|`CamelFop.Render.pdfUAEnabled` | |Whether the document conforms to PDF/UA + +|`CamelFop.Render.keepEmptyTags` | |Whether empty tags are kept in the structure tree of a tagged PDF (the FOP configuration sets the default) + +|`CamelFop.Render.locatorEnabled` |`true` |Whether the location of an FO element is kept for error messages + +|`CamelFop.Render.conserveMemoryPolicy` | |Whether FOP writes pages to a temporary file to save memory |======================================================================= === Example diff --git a/components/camel-fop/src/main/java/org/apache/camel/component/fop/FopProducer.java b/components/camel-fop/src/main/java/org/apache/camel/component/fop/FopProducer.java index 4e340648c773..85c7f33a107f 100644 --- a/components/camel-fop/src/main/java/org/apache/camel/component/fop/FopProducer.java +++ b/components/camel-fop/src/main/java/org/apache/camel/component/fop/FopProducer.java @@ -18,6 +18,8 @@ package org.apache.camel.component.fop; import java.io.ByteArrayOutputStream; import java.io.OutputStream; +import java.util.Date; +import java.util.Locale; import java.util.Map; import javax.xml.XMLConstants; @@ -30,19 +32,23 @@ import javax.xml.transform.sax.SAXResult; import javax.xml.transform.stream.StreamSource; import org.apache.camel.Exchange; +import org.apache.camel.NoTypeConversionAvailableException; import org.apache.camel.support.DefaultProducer; -import org.apache.camel.support.PropertyBindingSupport; import org.apache.camel.util.PropertiesHelper; import org.apache.fop.apps.FOPException; import org.apache.fop.apps.FOUserAgent; import org.apache.fop.apps.Fop; import org.apache.fop.apps.FopFactory; import org.apache.fop.pdf.PDFEncryptionParams; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * The Fop producer. */ public class FopProducer extends DefaultProducer { + private static final Logger LOG = LoggerFactory.getLogger(FopProducer.class); + private final FopFactory fopFactory; private final String outputFormat; @@ -56,8 +62,8 @@ public class FopProducer extends DefaultProducer { public void process(Exchange exchange) throws Exception { FOUserAgent userAgent = fopFactory.newFOUserAgent(); Map<String, Object> headers = exchange.getIn().getHeaders(); - setRenderParameters(userAgent, headers); - setEncryptionParameters(userAgent, headers); + setRenderParameters(exchange, userAgent, headers); + setEncryptionParameters(exchange, userAgent, headers); setUserAgentRendererOptions(userAgent, headers); String outputFormat = getOutputFormat(exchange); @@ -110,15 +116,41 @@ public class FopProducer extends DefaultProducer { return out; } - @SuppressWarnings("unchecked") - private void setEncryptionParameters(FOUserAgent userAgent, Map<String, Object> headers) { - Map<String, Object> encryptionParameters = PropertiesHelper - .extractProperties(headers, FopConstants.CAMEL_FOP_ENCRYPT); - if (!encryptionParameters.isEmpty()) { - PDFEncryptionParams encryptionParams = new PDFEncryptionParams(); - PropertyBindingSupport.bindProperties(getEndpoint().getCamelContext(), encryptionParams, encryptionParameters); - userAgent.getRendererOptions().put("encryption-params", encryptionParams); + private static void setEncryptionParameters(Exchange exchange, FOUserAgent userAgent, Map<String, Object> headers) + throws NoTypeConversionAvailableException { + Map<String, Object> parameters = PropertiesHelper.extractProperties(headers, FopConstants.CAMEL_FOP_ENCRYPT); + if (parameters.isEmpty()) { + return; + } + PDFEncryptionParams encryptionParams = new PDFEncryptionParams(); + for (Map.Entry<String, Object> entry : parameters.entrySet()) { + String name = entry.getKey(); + Object value = entry.getValue(); + if (value == null) { + continue; + } + // the values are applied as given, only converted to the type of the setter + switch (name.toLowerCase(Locale.ROOT)) { + case "userpassword" -> encryptionParams.setUserPassword(convertTo(exchange, String.class, value)); + case "ownerpassword" -> encryptionParams.setOwnerPassword(convertTo(exchange, String.class, value)); + case "allowprint" -> encryptionParams.setAllowPrint(convertTo(exchange, Boolean.class, value)); + case "allowcopycontent" -> encryptionParams.setAllowCopyContent(convertTo(exchange, Boolean.class, value)); + case "alloweditcontent" -> encryptionParams.setAllowEditContent(convertTo(exchange, Boolean.class, value)); + case "alloweditannotations" -> + encryptionParams.setAllowEditAnnotations(convertTo(exchange, Boolean.class, value)); + case "allowfillinforms" -> encryptionParams.setAllowFillInForms(convertTo(exchange, Boolean.class, value)); + case "allowaccesscontent" -> + encryptionParams.setAllowAccessContent(convertTo(exchange, Boolean.class, value)); + case "allowassembledocument" -> + encryptionParams.setAllowAssembleDocument(convertTo(exchange, Boolean.class, value)); + case "allowprinthq" -> encryptionParams.setAllowPrintHq(convertTo(exchange, Boolean.class, value)); + case "encryptmetadata" -> encryptionParams.setEncryptMetadata(convertTo(exchange, Boolean.class, value)); + case "encryptionlengthinbits" -> + encryptionParams.setEncryptionLengthInBits(convertTo(exchange, Integer.class, value)); + default -> logUnsupported(FopConstants.CAMEL_FOP_ENCRYPT, name); + } } + userAgent.getRendererOptions().put("encryption-params", encryptionParams); } private void setUserAgentRendererOptions(FOUserAgent userAgent, Map<String, Object> headers) { @@ -128,11 +160,42 @@ public class FopProducer extends DefaultProducer { } } - private void setRenderParameters(FOUserAgent userAgent, Map<String, Object> headers) { + private static void setRenderParameters(Exchange exchange, FOUserAgent userAgent, Map<String, Object> headers) + throws NoTypeConversionAvailableException { Map<String, Object> parameters = PropertiesHelper.extractProperties(headers, FopConstants.CAMEL_FOP_RENDER); - if (!parameters.isEmpty()) { - PropertyBindingSupport.bindProperties(getEndpoint().getCamelContext(), userAgent, parameters); + for (Map.Entry<String, Object> entry : parameters.entrySet()) { + String name = entry.getKey(); + Object value = entry.getValue(); + if (value == null) { + continue; + } + // the values are applied as given, only converted to the type of the setter + switch (name.toLowerCase(Locale.ROOT)) { + case "producer" -> userAgent.setProducer(convertTo(exchange, String.class, value)); + case "creator" -> userAgent.setCreator(convertTo(exchange, String.class, value)); + case "creationdate" -> userAgent.setCreationDate(convertTo(exchange, Date.class, value)); + case "author" -> userAgent.setAuthor(convertTo(exchange, String.class, value)); + case "title" -> userAgent.setTitle(convertTo(exchange, String.class, value)); + case "subject" -> userAgent.setSubject(convertTo(exchange, String.class, value)); + case "keywords" -> userAgent.setKeywords(convertTo(exchange, String.class, value)); + case "targetresolution" -> userAgent.setTargetResolution(convertTo(exchange, Float.class, value)); + case "accessibility" -> userAgent.setAccessibility(convertTo(exchange, Boolean.class, value)); + case "pdfuaenabled" -> userAgent.setPdfUAEnabled(convertTo(exchange, Boolean.class, value)); + case "keepemptytags" -> userAgent.setKeepEmptyTags(convertTo(exchange, Boolean.class, value)); + case "locatorenabled" -> userAgent.setLocatorEnabled(convertTo(exchange, Boolean.class, value)); + case "conservememorypolicy" -> userAgent.setConserveMemoryPolicy(convertTo(exchange, Boolean.class, value)); + default -> logUnsupported(FopConstants.CAMEL_FOP_RENDER, name); + } } } + private static <T> T convertTo(Exchange exchange, Class<T> type, Object value) + throws NoTypeConversionAvailableException { + return exchange.getContext().getTypeConverter().mandatoryConvertTo(type, exchange, value); + } + + private static void logUnsupported(String prefix, String name) { + LOG.warn("Ignoring header {}{} as it is not a supported option", prefix, name); + } + } diff --git a/components/camel-fop/src/test/java/org/apache/camel/component/fop/FopEncryptionHeadersTest.java b/components/camel-fop/src/test/java/org/apache/camel/component/fop/FopEncryptionHeadersTest.java new file mode 100644 index 000000000000..7e819d96d425 --- /dev/null +++ b/components/camel-fop/src/test/java/org/apache/camel/component/fop/FopEncryptionHeadersTest.java @@ -0,0 +1,74 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.fop; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.camel.test.junit6.CamelTestSupport; +import org.apache.pdfbox.Loader; +import org.apache.pdfbox.pdmodel.PDDocument; +import org.apache.pdfbox.pdmodel.encryption.AccessPermission; +import org.apache.pdfbox.pdmodel.encryption.InvalidPasswordException; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class FopEncryptionHeadersTest extends CamelTestSupport { + + @Test + public void passwordHeaderIsUsedAsGiven() throws Exception { + context.getPropertiesComponent().addInitialProperty("name", "Camel"); + String password = "{{name}}-2026"; + + Map<String, Object> headers = new HashMap<>(); + headers.put("CamelFop.Encrypt.userPassword", password); + byte[] pdf = render(headers); + + try (PDDocument document = Loader.loadPDF(pdf, password)) { + assertTrue(document.isEncrypted()); + assertEquals("Test Content", FopHelper.extractTextFrom(document)); + } + assertThrows(InvalidPasswordException.class, () -> Loader.loadPDF(pdf, "Camel-2026")); + } + + @Test + public void permissionHeadersAreApplied() throws Exception { + Map<String, Object> headers = new HashMap<>(); + headers.put("CamelFop.Encrypt.userPassword", "user"); + headers.put("CamelFop.Encrypt.ownerPassword", "owner"); + headers.put("CamelFop.Encrypt.allowPrint", "false"); + headers.put("CamelFop.Encrypt.allowCopyContent", false); + headers.put("CamelFop.Encrypt.allowEditContent", "true"); + + try (PDDocument document = Loader.loadPDF(render(headers), "user")) { + assertTrue(document.isEncrypted()); + AccessPermission permission = document.getCurrentAccessPermission(); + assertFalse(permission.canPrint()); + assertFalse(permission.canExtractContent()); + assertTrue(permission.canModify()); + } + } + + private byte[] render(Map<String, Object> headers) { + return template.requestBodyAndHeaders("fop:pdf", FopHelper.decorateTextWithXSLFO("Test Content"), headers, + byte[].class); + } +} diff --git a/components/camel-fop/src/test/java/org/apache/camel/component/fop/FopMetadataHeadersTest.java b/components/camel-fop/src/test/java/org/apache/camel/component/fop/FopMetadataHeadersTest.java new file mode 100644 index 000000000000..d2aa3870a89d --- /dev/null +++ b/components/camel-fop/src/test/java/org/apache/camel/component/fop/FopMetadataHeadersTest.java @@ -0,0 +1,113 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.fop; + +import java.util.Calendar; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; + +import org.apache.camel.spi.Registry; +import org.apache.camel.test.junit6.CamelTestSupport; +import org.apache.pdfbox.Loader; +import org.apache.pdfbox.pdmodel.PDDocument; +import org.apache.pdfbox.pdmodel.PDDocumentInformation; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class FopMetadataHeadersTest extends CamelTestSupport { + + @Override + protected void bindToRegistry(Registry registry) { + registry.bind("team", "Riders"); + } + + @Test + public void metadataHeadersAreWrittenToThePdf() throws Exception { + Date creationDate = new Date(1700000000000L); + Map<String, Object> headers = new HashMap<>(); + headers.put("CamelFop.Render.producer", "Report Service"); + headers.put("CamelFop.Render.creator", "Test User"); + headers.put("CamelFop.Render.author", "Jane Doe"); + headers.put("CamelFop.Render.title", "Monthly Report"); + headers.put("CamelFop.Render.subject", "Sales"); + headers.put("CamelFop.Render.keywords", "sales, monthly"); + headers.put("CamelFop.Render.creationDate", creationDate); + + try (PDDocument document = render(headers)) { + PDDocumentInformation info = document.getDocumentInformation(); + assertEquals("Report Service", info.getProducer()); + assertEquals("Test User", info.getCreator()); + assertEquals("Jane Doe", info.getAuthor()); + assertEquals("Monthly Report", info.getTitle()); + assertEquals("Sales", info.getSubject()); + assertEquals("sales, monthly", info.getKeywords()); + Calendar created = info.getCreationDate(); + assertEquals(creationDate.getTime() / 1000, created.getTimeInMillis() / 1000); + } + } + + @Test + public void metadataHeaderValueIsWrittenAsGiven() throws Exception { + context.getPropertiesComponent().addInitialProperty("name", "Camel"); + + Map<String, Object> headers = new HashMap<>(); + headers.put("CamelFop.Render.title", "Template {{name}}"); + headers.put("CamelFop.Render.keywords", "#team"); + headers.put("CamelFop.Render.author", "#1 Team"); + + try (PDDocument document = render(headers)) { + PDDocumentInformation info = document.getDocumentInformation(); + assertEquals("Template {{name}}", info.getTitle()); + assertEquals("#team", info.getKeywords()); + assertEquals("#1 Team", info.getAuthor()); + } + } + + @Test + public void metadataHeaderNameIsCaseInsensitive() throws Exception { + Map<String, Object> headers = new HashMap<>(); + headers.put("CamelFop.Render.Creator", "Test User"); + headers.put("CamelFop.Render.TITLE", "Upper Case"); + + try (PDDocument document = render(headers)) { + PDDocumentInformation info = document.getDocumentInformation(); + assertEquals("Test User", info.getCreator()); + assertEquals("Upper Case", info.getTitle()); + } + } + + @Test + public void unsupportedRenderHeaderIsIgnored() throws Exception { + Map<String, Object> headers = new HashMap<>(); + headers.put("CamelFop.Render.unknownOption", "some value"); + headers.put("CamelFop.Render.title", "Monthly Report"); + + try (PDDocument document = render(headers)) { + assertEquals("Monthly Report", document.getDocumentInformation().getTitle()); + assertTrue(FopHelper.extractTextFrom(document).contains("Test Content")); + } + } + + private PDDocument render(Map<String, Object> headers) throws Exception { + byte[] pdf = template.requestBodyAndHeaders("fop:pdf", FopHelper.decorateTextWithXSLFO("Test Content"), headers, + byte[].class); + return Loader.loadPDF(pdf); + } +}
