This is an automated email from the ASF dual-hosted git repository.
orpiske pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new 80a0fba5072 CAMEL-21026: added a type converter for byte-array to PDF
80a0fba5072 is described below
commit 80a0fba507297de3ff23cc7f6beadb124f12dd1e
Author: Otavio Rodolfo Piske <[email protected]>
AuthorDate: Mon Jul 29 14:08:25 2024 +0200
CAMEL-21026: added a type converter for byte-array to PDF
This should make it easier to convert unprotected PDFs
---
.../pdf/converter/PdfConverterLoader.java | 63 ++++++++++++++++
.../services/org/apache/camel/TypeConverterLoader | 2 +
.../camel-pdf/src/main/docs/pdf-component.adoc | 20 ++++++
.../component/pdf/converter/PdfConverter.java | 31 ++++++++
.../org/apache/camel/component/pdf/PDFUtil.java | 43 +++++++++++
.../apache/camel/component/pdf/PdfAppendTest.java | 30 ++------
.../camel/component/pdf/PdfConverterTest.java | 83 ++++++++++++++++++++++
.../camel/component/pdf/PdfTextExtractionTest.java | 27 +------
8 files changed, 248 insertions(+), 51 deletions(-)
diff --git
a/components/camel-pdf/src/generated/java/org/apache/camel/component/pdf/converter/PdfConverterLoader.java
b/components/camel-pdf/src/generated/java/org/apache/camel/component/pdf/converter/PdfConverterLoader.java
new file mode 100644
index 00000000000..41f878e6320
--- /dev/null
+++
b/components/camel-pdf/src/generated/java/org/apache/camel/component/pdf/converter/PdfConverterLoader.java
@@ -0,0 +1,63 @@
+/* Generated by camel build tools - do NOT edit this file! */
+package org.apache.camel.component.pdf.converter;
+
+import javax.annotation.processing.Generated;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.CamelContextAware;
+import org.apache.camel.DeferredContextBinding;
+import org.apache.camel.Exchange;
+import org.apache.camel.TypeConversionException;
+import org.apache.camel.TypeConverterLoaderException;
+import org.apache.camel.spi.TypeConverterLoader;
+import org.apache.camel.spi.TypeConverterRegistry;
+import org.apache.camel.support.SimpleTypeConverter;
+import org.apache.camel.support.TypeConverterSupport;
+import org.apache.camel.util.DoubleMap;
+
+/**
+ * Generated by camel build tools - do NOT edit this file!
+ */
+@Generated("org.apache.camel.maven.packaging.TypeConverterLoaderGeneratorMojo")
+@SuppressWarnings("unchecked")
+@DeferredContextBinding
+public final class PdfConverterLoader implements TypeConverterLoader,
CamelContextAware {
+
+ private CamelContext camelContext;
+
+ public PdfConverterLoader() {
+ }
+
+ @Override
+ public void setCamelContext(CamelContext camelContext) {
+ this.camelContext = camelContext;
+ }
+
+ @Override
+ public CamelContext getCamelContext() {
+ return camelContext;
+ }
+
+ @Override
+ public void load(TypeConverterRegistry registry) throws
TypeConverterLoaderException {
+ registerConverters(registry);
+ }
+
+ private void registerConverters(TypeConverterRegistry registry) {
+ addTypeConverter(registry, org.apache.pdfbox.pdmodel.PDDocument.class,
byte[].class, false,
+ (type, exchange, value) -> getPdfConverter().convertToPDF((byte[])
value));
+ }
+
+ private static void addTypeConverter(TypeConverterRegistry registry,
Class<?> toType, Class<?> fromType, boolean allowNull,
SimpleTypeConverter.ConversionMethod method) {
+ registry.addTypeConverter(toType, fromType, new
SimpleTypeConverter(allowNull, method));
+ }
+
+ private volatile org.apache.camel.component.pdf.converter.PdfConverter
pdfConverter;
+ private org.apache.camel.component.pdf.converter.PdfConverter
getPdfConverter() {
+ if (pdfConverter == null) {
+ pdfConverter = new
org.apache.camel.component.pdf.converter.PdfConverter();
+ CamelContextAware.trySetCamelContext(pdfConverter, camelContext);
+ }
+ return pdfConverter;
+ }
+}
diff --git
a/components/camel-pdf/src/generated/resources/META-INF/services/org/apache/camel/TypeConverterLoader
b/components/camel-pdf/src/generated/resources/META-INF/services/org/apache/camel/TypeConverterLoader
new file mode 100644
index 00000000000..faf4edb72bc
--- /dev/null
+++
b/components/camel-pdf/src/generated/resources/META-INF/services/org/apache/camel/TypeConverterLoader
@@ -0,0 +1,2 @@
+# Generated by camel build tools - do NOT edit this file!
+org.apache.camel.component.pdf.converter.PdfConverterLoader
diff --git a/components/camel-pdf/src/main/docs/pdf-component.adoc
b/components/camel-pdf/src/main/docs/pdf-component.adoc
index f6e4412be53..631d10ecf5c 100644
--- a/components/camel-pdf/src/main/docs/pdf-component.adoc
+++ b/components/camel-pdf/src/main/docs/pdf-component.adoc
@@ -60,4 +60,24 @@ include::partial$component-endpoint-headers.adoc[]
// component headers: END
+== Type converter
+
+Since Camel 4.8, the component is capable of doing simple document
conversions. For instance,
+suppose you are receiving a PDF byte as a byte array:
+
+
+[source,java]
+----
+from("direct:start")
+ .to("pdf:extractText")
+ .to("mock:result");
+----
+
+
+It is now possible to get the body as a PD Document by using `PDDocument doc =
exchange.getIn().getBody(PDDocument.class);`,
+which saves the trouble of converting the byte-array to a document.
+
+NOTE: this only works for unprotected PDF files. For password-protected, the
files still need to be converted
+manually.
+
include::spring-boot:partial$starter.adoc[]
diff --git
a/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/converter/PdfConverter.java
b/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/converter/PdfConverter.java
new file mode 100644
index 00000000000..4d236b512ba
--- /dev/null
+++
b/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/converter/PdfConverter.java
@@ -0,0 +1,31 @@
+/*
+ * 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.pdf.converter;
+
+import org.apache.camel.Converter;
+import org.apache.pdfbox.Loader;
+import org.apache.pdfbox.pdmodel.PDDocument;
+
+@Converter(generateLoader = true)
+public class PdfConverter {
+
+ @Converter
+ public PDDocument convertToPDF(byte[] bytes) throws Exception {
+ return Loader.loadPDF(bytes);
+ }
+}
diff --git
a/components/camel-pdf/src/test/java/org/apache/camel/component/pdf/PDFUtil.java
b/components/camel-pdf/src/test/java/org/apache/camel/component/pdf/PDFUtil.java
new file mode 100644
index 00000000000..33770e54d0c
--- /dev/null
+++
b/components/camel-pdf/src/test/java/org/apache/camel/component/pdf/PDFUtil.java
@@ -0,0 +1,43 @@
+/*
+ * 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.pdf;
+
+import java.io.IOException;
+
+import org.apache.pdfbox.pdmodel.PDDocument;
+import org.apache.pdfbox.pdmodel.PDPage;
+import org.apache.pdfbox.pdmodel.PDPageContentStream;
+import org.apache.pdfbox.pdmodel.common.PDRectangle;
+import org.apache.pdfbox.pdmodel.font.PDType1Font;
+import org.apache.pdfbox.pdmodel.font.Standard14Fonts;
+
+public class PDFUtil {
+ public static PDDocument textToPDF(String originalText) throws IOException
{
+ PDDocument document = new PDDocument();
+ PDPage page = new PDPage(PDRectangle.A4);
+ document.addPage(page);
+ PDPageContentStream contentStream = new PDPageContentStream(document,
page);
+ contentStream.setFont(new
PDType1Font(Standard14Fonts.FontName.HELVETICA), 12);
+ contentStream.beginText();
+ contentStream.newLineAtOffset(20, 400);
+ contentStream.showText(originalText);
+ contentStream.endText();
+ contentStream.close();
+ return document;
+ }
+}
diff --git
a/components/camel-pdf/src/test/java/org/apache/camel/component/pdf/PdfAppendTest.java
b/components/camel-pdf/src/test/java/org/apache/camel/component/pdf/PdfAppendTest.java
index f421bb31bf1..ded3b2950a8 100644
---
a/components/camel-pdf/src/test/java/org/apache/camel/component/pdf/PdfAppendTest.java
+++
b/components/camel-pdf/src/test/java/org/apache/camel/component/pdf/PdfAppendTest.java
@@ -32,14 +32,9 @@ import org.apache.camel.test.junit5.CamelTestSupport;
import org.apache.pdfbox.Loader;
import org.apache.pdfbox.io.RandomAccessReadBuffer;
import org.apache.pdfbox.pdmodel.PDDocument;
-import org.apache.pdfbox.pdmodel.PDPage;
-import org.apache.pdfbox.pdmodel.PDPageContentStream;
-import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.encryption.AccessPermission;
import org.apache.pdfbox.pdmodel.encryption.StandardDecryptionMaterial;
import org.apache.pdfbox.pdmodel.encryption.StandardProtectionPolicy;
-import org.apache.pdfbox.pdmodel.font.PDType1Font;
-import org.apache.pdfbox.pdmodel.font.Standard14Fonts;
import org.apache.pdfbox.text.PDFTextStripper;
import org.junit.jupiter.api.Test;
@@ -55,18 +50,10 @@ public class PdfAppendTest extends CamelTestSupport {
@Test
public void testAppend() throws Exception {
- final String originalText = "Test";
final String textToAppend = "Append";
- PDDocument document = new PDDocument();
- PDPage page = new PDPage(PDRectangle.A4);
- document.addPage(page);
- PDPageContentStream contentStream = new PDPageContentStream(document,
page);
- contentStream.setFont(new
PDType1Font(Standard14Fonts.FontName.HELVETICA), 12);
- contentStream.beginText();
- contentStream.newLineAtOffset(20, 400);
- contentStream.showText(originalText);
- contentStream.endText();
- contentStream.close();
+ final String originalText = "Test";
+
+ PDDocument document = PDFUtil.textToPDF(originalText);
template.sendBodyAndHeader("direct:start", textToAppend,
PdfHeaderConstants.PDF_DOCUMENT_HEADER_NAME, document);
@@ -98,16 +85,7 @@ public class PdfAppendTest extends CamelTestSupport {
public void testAppendEncrypted() throws Exception {
final String originalText = "Test";
final String textToAppend = "Append";
- PDDocument document = new PDDocument();
- PDPage page = new PDPage(PDRectangle.A4);
- document.addPage(page);
- PDPageContentStream contentStream = new PDPageContentStream(document,
page);
- contentStream.setFont(new
PDType1Font(Standard14Fonts.FontName.HELVETICA), 12);
- contentStream.beginText();
- contentStream.newLineAtOffset(20, 400);
- contentStream.showText(originalText);
- contentStream.endText();
- contentStream.close();
+ PDDocument document = PDFUtil.textToPDF(originalText);
final String ownerPass = "ownerPass";
final String userPass = "userPass";
diff --git
a/components/camel-pdf/src/test/java/org/apache/camel/component/pdf/PdfConverterTest.java
b/components/camel-pdf/src/test/java/org/apache/camel/component/pdf/PdfConverterTest.java
new file mode 100644
index 00000000000..c17d2427be1
--- /dev/null
+++
b/components/camel-pdf/src/test/java/org/apache/camel/component/pdf/PdfConverterTest.java
@@ -0,0 +1,83 @@
+/*
+ * 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.pdf;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+
+import org.apache.camel.EndpointInject;
+import org.apache.camel.RuntimeCamelException;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.test.junit5.CamelTestSupport;
+import org.apache.pdfbox.pdmodel.PDDocument;
+import org.apache.pdfbox.text.PDFTextStripper;
+import org.junit.jupiter.api.Test;
+
+import static org.apache.camel.component.pdf.PDFUtil.textToPDF;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.containsString;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class PdfConverterTest extends CamelTestSupport {
+
+ @EndpointInject("mock:result")
+ protected MockEndpoint resultEndpoint;
+
+ @Test
+ public void testAppend() throws Exception {
+ final String originalText = "Test";
+ final PDDocument document = textToPDF(originalText);
+
+ byte[] array;
+ try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
+ document.save(baos);
+
+ array = baos.toByteArray();
+ template.sendBodyAndHeader("direct:start", array,
PdfHeaderConstants.PDF_DOCUMENT_HEADER_NAME, document);
+ }
+
+ resultEndpoint.setExpectedMessageCount(1);
+ resultEndpoint.expectedMessagesMatches(exchange -> {
+ PDDocument doc = exchange.getIn().getBody(PDDocument.class);
+
+ try {
+ PDFTextStripper pdfTextStripper = new PDFTextStripper();
+ String text = pdfTextStripper.getText(doc);
+ assertEquals(2, doc.getNumberOfPages());
+ assertThat(text, containsString(originalText));
+ } catch (IOException e) {
+ throw new RuntimeCamelException(e);
+ }
+ return true;
+ });
+ resultEndpoint.assertIsSatisfied();
+
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() {
+ return new RouteBuilder() {
+ @Override
+ public void configure() {
+ from("direct:start")
+ .to("pdf:extractText")
+ .to("mock:result");
+ }
+ };
+ }
+}
diff --git
a/components/camel-pdf/src/test/java/org/apache/camel/component/pdf/PdfTextExtractionTest.java
b/components/camel-pdf/src/test/java/org/apache/camel/component/pdf/PdfTextExtractionTest.java
index e49900da615..545343c6d35 100644
---
a/components/camel-pdf/src/test/java/org/apache/camel/component/pdf/PdfTextExtractionTest.java
+++
b/components/camel-pdf/src/test/java/org/apache/camel/component/pdf/PdfTextExtractionTest.java
@@ -28,14 +28,9 @@ import org.apache.camel.test.junit5.CamelTestSupport;
import org.apache.pdfbox.Loader;
import org.apache.pdfbox.io.RandomAccessReadBuffer;
import org.apache.pdfbox.pdmodel.PDDocument;
-import org.apache.pdfbox.pdmodel.PDPage;
-import org.apache.pdfbox.pdmodel.PDPageContentStream;
-import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.encryption.AccessPermission;
import org.apache.pdfbox.pdmodel.encryption.StandardDecryptionMaterial;
import org.apache.pdfbox.pdmodel.encryption.StandardProtectionPolicy;
-import org.apache.pdfbox.pdmodel.font.PDType1Font;
-import org.apache.pdfbox.pdmodel.font.Standard14Fonts;
import org.junit.jupiter.api.Test;
import static org.hamcrest.MatcherAssert.assertThat;
@@ -50,16 +45,7 @@ public class PdfTextExtractionTest extends CamelTestSupport {
@Test
public void testExtractText() throws Exception {
final String expectedText = "Test string";
- PDDocument document = new PDDocument();
- PDPage page = new PDPage(PDRectangle.A4);
- document.addPage(page);
- PDPageContentStream contentStream = new PDPageContentStream(document,
page);
- contentStream.setFont(new
PDType1Font(Standard14Fonts.FontName.HELVETICA), 12);
- contentStream.beginText();
- contentStream.newLineAtOffset(20, 400);
- contentStream.showText(expectedText);
- contentStream.endText();
- contentStream.close();
+ PDDocument document = PDFUtil.textToPDF(expectedText);
template.sendBody("direct:start", document);
@@ -84,18 +70,9 @@ public class PdfTextExtractionTest extends CamelTestSupport {
accessPermission.setCanExtractContent(false);
StandardProtectionPolicy protectionPolicy = new
StandardProtectionPolicy(ownerPass, userPass, accessPermission);
protectionPolicy.setEncryptionKeyLength(128);
- PDDocument document = new PDDocument();
final String expectedText = "Test string";
- PDPage page = new PDPage(PDRectangle.A4);
- document.addPage(page);
- PDPageContentStream contentStream = new PDPageContentStream(document,
page);
- contentStream.setFont(new
PDType1Font(Standard14Fonts.FontName.HELVETICA), 12);
- contentStream.beginText();
- contentStream.newLineAtOffset(20, 400);
- contentStream.showText(expectedText);
- contentStream.endText();
- contentStream.close();
+ PDDocument document = PDFUtil.textToPDF(expectedText);
document.protect(protectionPolicy);