PDF component created.
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/4a5258c4 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/4a5258c4 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/4a5258c4 Branch: refs/heads/master Commit: 4a5258c45a0e8c734cbb31f9de12003765560bf0 Parents: 6ea8df6 Author: nkukhar <kukha...@gmail.com> Authored: Thu Apr 16 23:29:14 2015 -0700 Committer: Claus Ibsen <davscl...@apache.org> Committed: Sun Apr 19 10:40:23 2015 +0200 ---------------------------------------------------------------------- components/camel-pdf/pom.xml | 79 ++++++++ .../camel/component/pdf/PdfComponent.java | 38 ++++ .../camel/component/pdf/PdfConfiguration.java | 157 ++++++++++++++ .../camel/component/pdf/PdfConstants.java | 24 +++ .../apache/camel/component/pdf/PdfEndpoint.java | 60 ++++++ .../camel/component/pdf/PdfHeaderConstants.java | 26 +++ .../camel/component/pdf/PdfOperation.java | 21 ++ .../component/pdf/PdfPageSizeConstant.java | 32 +++ .../apache/camel/component/pdf/PdfProducer.java | 171 ++++++++++++++++ .../component/pdf/TextProcessingFactory.java | 21 ++ .../AutoFormattedWriterAbstractFactory.java | 47 +++++ .../pdf/text/DefaultLineBuilderStrategy.java | 169 +++++++++++++++ .../pdf/text/DefaultWriteStrategy.java | 81 ++++++++ .../component/pdf/text/LineBuilderStrategy.java | 27 +++ .../LineTerminationWriterAbstractFactory.java | 56 +++++ .../pdf/text/PatternSplitStrategy.java | 33 +++ .../camel/component/pdf/text/PdfUtils.java | 39 ++++ .../camel/component/pdf/text/SplitStrategy.java | 26 +++ .../pdf/text/TextProcessingAbstractFactory.java | 23 +++ .../component/pdf/text/WordSplitStrategy.java | 34 ++++ .../camel/component/pdf/text/WriteStrategy.java | 29 +++ .../src/main/resources/META-INF/LICENSE.txt | 203 +++++++++++++++++++ .../src/main/resources/META-INF/NOTICE.txt | 11 + .../services/org/apache/camel/component/pdf | 17 ++ .../camel/component/pdf/PdfAppendTest.java | 166 +++++++++++++++ .../camel/component/pdf/PdfCreationTest.java | 124 +++++++++++ .../component/pdf/PdfTextExtractionTest.java | 133 ++++++++++++ .../pdf/text/PatternSplitStrategyTest.java | 36 ++++ .../pdf/text/WordSplitStrategyTest.java | 36 ++++ .../src/test/resources/log4j.properties | 38 ++++ components/pom.xml | 1 + 31 files changed, 1958 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/4a5258c4/components/camel-pdf/pom.xml ---------------------------------------------------------------------- diff --git a/components/camel-pdf/pom.xml b/components/camel-pdf/pom.xml new file mode 100644 index 0000000..09ed5a4 --- /dev/null +++ b/components/camel-pdf/pom.xml @@ -0,0 +1,79 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <artifactId>components</artifactId> + <groupId>org.apache.camel</groupId> + <version>2.16-SNAPSHOT</version> + </parent> + + <artifactId>camel-pdf</artifactId> + + <dependencies> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-core</artifactId> + </dependency> + + <dependency> + <groupId>org.apache.commons</groupId> + <artifactId>commons-lang3</artifactId> + <version>${commons-lang3-version}</version> + </dependency> + + <dependency> + <groupId>org.apache.pdfbox</groupId> + <artifactId>pdfbox</artifactId> + <version>${pdfbox-version}</version> + </dependency> + + <!-- Encryption libraries required for PDFBox --> + <dependency> + <groupId>org.bouncycastle</groupId> + <artifactId>bcprov-jdk15</artifactId> + <version>1.44</version> + </dependency> + <dependency> + <groupId>org.bouncycastle</groupId> + <artifactId>bcmail-jdk15</artifactId> + <version>1.44</version> + </dependency> + + + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-log4j12</artifactId> + <scope>test</scope> + </dependency> + + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-test</artifactId> + <scope>test</scope> + </dependency> + + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <scope>test</scope> + </dependency> + + <dependency> + <groupId>org.hamcrest</groupId> + <artifactId>hamcrest-core</artifactId> + <version>${hamcrest-version}</version> + <scope>test</scope> + </dependency> + + <dependency> + <groupId>org.mockito</groupId> + <artifactId>mockito-core</artifactId> + <scope>test</scope> + </dependency> + </dependencies> + + +</project> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/4a5258c4/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/PdfComponent.java ---------------------------------------------------------------------- diff --git a/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/PdfComponent.java b/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/PdfComponent.java new file mode 100644 index 0000000..791ed9f --- /dev/null +++ b/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/PdfComponent.java @@ -0,0 +1,38 @@ +/** + * 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.net.URI; +import java.util.Map; + +import org.apache.camel.Endpoint; +import org.apache.camel.impl.UriEndpointComponent; + +public class PdfComponent extends UriEndpointComponent { + + public PdfComponent() { + super(PdfEndpoint.class); + } + + @Override + protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception { + PdfConfiguration pdfConfiguration = new PdfConfiguration(); + setProperties(pdfConfiguration, parameters); + pdfConfiguration.setOperation(new URI(uri).getHost()); + return new PdfEndpoint(uri, this, pdfConfiguration); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/4a5258c4/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/PdfConfiguration.java ---------------------------------------------------------------------- diff --git a/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/PdfConfiguration.java b/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/PdfConfiguration.java new file mode 100644 index 0000000..1feedbf --- /dev/null +++ b/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/PdfConfiguration.java @@ -0,0 +1,157 @@ +/** + * 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.util.HashMap; +import java.util.Map; + +import org.apache.camel.spi.Metadata; +import org.apache.camel.spi.UriParam; +import org.apache.camel.spi.UriParams; +import org.apache.camel.spi.UriPath; +import org.apache.pdfbox.pdmodel.PDPage; +import org.apache.pdfbox.pdmodel.common.PDRectangle; +import org.apache.pdfbox.pdmodel.font.PDFont; +import org.apache.pdfbox.pdmodel.font.PDType1Font; + +import static org.apache.camel.component.pdf.PdfPageSizeConstant.*; + +/** + * Handles pdf component configuration values. + */ +@UriParams +public class PdfConfiguration { + private static final Map<String, PDRectangle> PAGE_MAP = new HashMap<>(); + + static { + PAGE_MAP.put(PAGE_SIZE_A0, PDPage.PAGE_SIZE_A0); + PAGE_MAP.put(PAGE_SIZE_A1, PDPage.PAGE_SIZE_A1); + PAGE_MAP.put(PAGE_SIZE_A2, PDPage.PAGE_SIZE_A2); + PAGE_MAP.put(PAGE_SIZE_A3, PDPage.PAGE_SIZE_A3); + PAGE_MAP.put(PAGE_SIZE_A4, PDPage.PAGE_SIZE_A4); + PAGE_MAP.put(PAGE_SIZE_A5, PDPage.PAGE_SIZE_A5); + PAGE_MAP.put(PAGE_SIZE_A6, PDPage.PAGE_SIZE_A6); + PAGE_MAP.put(PAGE_SIZE_LETTER, PDPage.PAGE_SIZE_LETTER); + } + + @UriPath(description = "Operation type") @Metadata(required = "true") + private PdfOperation operation; + @UriParam + private int marginTop = 20; + @UriParam + private int marginBottom = 20; + @UriParam + private int marginLeft = 20; + @UriParam + private int marginRight = 40; + @UriParam + private float fontSize = 14; + @UriParam + private PDRectangle pageSize = PDPage.PAGE_SIZE_A4; + @UriParam + private PDFont font = PDType1Font.HELVETICA; + @UriParam + private TextProcessingFactory textProcessingFactory = TextProcessingFactory.lineTermination; + + public PdfOperation getOperation() { + return operation; + } + + public void setOperation(String operation) { + this.operation = PdfOperation.valueOf(operation); + } + + public void setOperation(PdfOperation operation) { + this.operation = operation; + } + + public int getMarginTop() { + return marginTop; + } + + public void setMarginTop(int marginTop) { + this.marginTop = marginTop; + } + + public int getMarginBottom() { + return marginBottom; + } + + public void setMarginBottom(int marginBottom) { + this.marginBottom = marginBottom; + } + + public int getMarginLeft() { + return marginLeft; + } + + public void setMarginLeft(int marginLeft) { + this.marginLeft = marginLeft; + } + + public int getMarginRight() { + return marginRight; + } + + public void setMarginRight(int marginRight) { + this.marginRight = marginRight; + } + + public float getFontSize() { + return fontSize; + } + + public void setFontSize(float fontSize) { + this.fontSize = fontSize; + } + + public PDRectangle getPageSize() { + return pageSize; + } + + public void setPageSize(PDRectangle pageSize) { + this.pageSize = pageSize; + } + + public void setPageSize(String pageSize) { + setPageSize(PAGE_MAP.get(pageSize)); + } + + public PDFont getFont() { + return font; + } + + public void setFont(PDFont font) { + this.font = font; + } + + public void setFont(String font) { + setFont(PDType1Font.getStandardFont(font)); + } + + public TextProcessingFactory getTextProcessingFactory() { + return textProcessingFactory; + } + + public void setTextProcessingFactory(TextProcessingFactory textProcessingFactory) { + this.textProcessingFactory = textProcessingFactory; + } + + public void setTextProcessingFactory(String textProcessingFactory) { + this.textProcessingFactory = TextProcessingFactory.valueOf(textProcessingFactory); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/4a5258c4/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/PdfConstants.java ---------------------------------------------------------------------- diff --git a/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/PdfConstants.java b/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/PdfConstants.java new file mode 100644 index 0000000..b3d89d1 --- /dev/null +++ b/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/PdfConstants.java @@ -0,0 +1,24 @@ +/** + * 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; + +public final class PdfConstants { + public static final int PDF_PIXEL_SIZE = 1000; + public static final int MIN_CONTENT_WIDTH = 20; + + private PdfConstants() { } +} http://git-wip-us.apache.org/repos/asf/camel/blob/4a5258c4/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/PdfEndpoint.java ---------------------------------------------------------------------- diff --git a/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/PdfEndpoint.java b/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/PdfEndpoint.java new file mode 100644 index 0000000..8487be1 --- /dev/null +++ b/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/PdfEndpoint.java @@ -0,0 +1,60 @@ +/** + * 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 org.apache.camel.Component; +import org.apache.camel.Consumer; +import org.apache.camel.Processor; +import org.apache.camel.Producer; +import org.apache.camel.impl.DefaultEndpoint; +import org.apache.camel.spi.UriEndpoint; +import org.apache.camel.spi.UriParam; + +@UriEndpoint(scheme = "pdf", syntax = "pdf:operation", producerOnly = true, label = "pdf") +public class PdfEndpoint extends DefaultEndpoint { + + @UriParam + private PdfConfiguration pdfConfiguration; + + public PdfEndpoint(String endpointUri, Component component, PdfConfiguration pdfConfiguration) { + super(endpointUri, component); + this.pdfConfiguration = pdfConfiguration; + } + + @Override + public Producer createProducer() throws Exception { + return new PdfProducer(this); + } + + @Override + public Consumer createConsumer(Processor processor) throws Exception { + throw new UnsupportedOperationException("Consumer does not supported for PDF component:" + getEndpointUri()); + } + + @Override + public boolean isSingleton() { + return true; + } + + public PdfConfiguration getPdfConfiguration() { + return pdfConfiguration; + } + + public void setPdfConfiguration(PdfConfiguration pdfConfiguration) { + this.pdfConfiguration = pdfConfiguration; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/4a5258c4/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/PdfHeaderConstants.java ---------------------------------------------------------------------- diff --git a/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/PdfHeaderConstants.java b/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/PdfHeaderConstants.java new file mode 100644 index 0000000..55317cb --- /dev/null +++ b/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/PdfHeaderConstants.java @@ -0,0 +1,26 @@ +/** + * 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; + +public final class PdfHeaderConstants { + public static final String PROTECTION_POLICY_HEADER_NAME = "protection-policy"; + public static final String PDF_DOCUMENT_HEADER_NAME = "pdf-document"; + public static final String DECRYPTION_MATERIAL_HEADER_NAME = "decryption-material"; + + private PdfHeaderConstants() { } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/4a5258c4/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/PdfOperation.java ---------------------------------------------------------------------- diff --git a/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/PdfOperation.java b/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/PdfOperation.java new file mode 100644 index 0000000..d131372 --- /dev/null +++ b/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/PdfOperation.java @@ -0,0 +1,21 @@ +/** + * 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; + +public enum PdfOperation { + create, append, extractText +} http://git-wip-us.apache.org/repos/asf/camel/blob/4a5258c4/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/PdfPageSizeConstant.java ---------------------------------------------------------------------- diff --git a/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/PdfPageSizeConstant.java b/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/PdfPageSizeConstant.java new file mode 100644 index 0000000..b0c3331 --- /dev/null +++ b/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/PdfPageSizeConstant.java @@ -0,0 +1,32 @@ +/** + * 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; + +public final class PdfPageSizeConstant { + + public static final String PAGE_SIZE_A0 = "PAGE_SIZE_A0"; + public static final String PAGE_SIZE_A1 = "PAGE_SIZE_A1"; + public static final String PAGE_SIZE_A2 = "PAGE_SIZE_A2"; + public static final String PAGE_SIZE_A3 = "PAGE_SIZE_A3"; + public static final String PAGE_SIZE_A4 = "PAGE_SIZE_A4"; + public static final String PAGE_SIZE_A5 = "PAGE_SIZE_A5"; + public static final String PAGE_SIZE_A6 = "PAGE_SIZE_A6"; + public static final String PAGE_SIZE_LETTER = "PAGE_SIZE_LETTER"; + + private PdfPageSizeConstant() { } +} http://git-wip-us.apache.org/repos/asf/camel/blob/4a5258c4/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/PdfProducer.java ---------------------------------------------------------------------- diff --git a/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/PdfProducer.java b/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/PdfProducer.java new file mode 100644 index 0000000..72ef67d --- /dev/null +++ b/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/PdfProducer.java @@ -0,0 +1,171 @@ +/** + * 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 java.io.OutputStream; +import java.util.Collection; + +import org.apache.camel.Exchange; +import org.apache.camel.component.pdf.text.AutoFormattedWriterAbstractFactory; +import org.apache.camel.component.pdf.text.LineBuilderStrategy; +import org.apache.camel.component.pdf.text.LineTerminationWriterAbstractFactory; +import org.apache.camel.component.pdf.text.SplitStrategy; +import org.apache.camel.component.pdf.text.TextProcessingAbstractFactory; +import org.apache.camel.component.pdf.text.WriteStrategy; +import org.apache.camel.impl.DefaultProducer; +import org.apache.pdfbox.exceptions.COSVisitorException; +import org.apache.pdfbox.exceptions.CryptographyException; +import org.apache.pdfbox.exceptions.InvalidPasswordException; +import org.apache.pdfbox.pdmodel.PDDocument; +import org.apache.pdfbox.pdmodel.encryption.BadSecurityHandlerException; +import org.apache.pdfbox.pdmodel.encryption.DecryptionMaterial; +import org.apache.pdfbox.pdmodel.encryption.ProtectionPolicy; +import org.apache.pdfbox.pdmodel.encryption.StandardProtectionPolicy; +import org.apache.pdfbox.util.PDFTextStripper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import static org.apache.camel.component.pdf.PdfHeaderConstants.*; + +public class PdfProducer extends DefaultProducer { + private static final Logger LOG = LoggerFactory.getLogger(PdfProducer.class); + + private final WriteStrategy writeStrategy; + private final SplitStrategy splitStrategy; + private final LineBuilderStrategy lineBuilderStrategy; + private final PdfConfiguration pdfConfiguration; + + public PdfProducer(PdfEndpoint endpoint) { + super(endpoint); + this.pdfConfiguration = endpoint.getPdfConfiguration(); + TextProcessingAbstractFactory textProcessingFactory = createTextProcessingFactory(pdfConfiguration); + this.writeStrategy = textProcessingFactory.createWriteStrategy(); + this.splitStrategy = textProcessingFactory.createSplitStrategy(); + this.lineBuilderStrategy = textProcessingFactory.createLineBuilderStrategy(); + } + + @Override + public void process(Exchange exchange) throws Exception { + Object result; + switch (pdfConfiguration.getOperation()) { + case append: + result = doAppend(exchange); + break; + case create: + result = doCreate(exchange); + break; + case extractText: + result = doExtractText(exchange); + break; + default: + throw new IllegalArgumentException(String.format("Unknown operation %s", pdfConfiguration.getOperation())); + } + exchange.getOut().setBody(result); + } + + private Object doAppend(Exchange exchange) throws IOException, BadSecurityHandlerException, CryptographyException, InvalidPasswordException, COSVisitorException { + LOG.debug("Got {} operation, going to append text to provided pdf.", pdfConfiguration.getOperation()); + String body = exchange.getIn().getBody(String.class); + PDDocument document = exchange.getIn().getHeader(PDF_DOCUMENT_HEADER_NAME, PDDocument.class); + if (document == null) { + throw new IllegalArgumentException(String.format("%s header is expected for append operation", + PDF_DOCUMENT_HEADER_NAME)); + } + + if (document.isEncrypted()) { + DecryptionMaterial decryptionMaterial = exchange.getIn().getHeader(DECRYPTION_MATERIAL_HEADER_NAME, + DecryptionMaterial.class); + if (decryptionMaterial == null) { + throw new IllegalArgumentException(String.format("%s header is expected for %s operation " + + "on encrypted document", + DECRYPTION_MATERIAL_HEADER_NAME, + pdfConfiguration.getOperation())); + } + + document.openProtection(decryptionMaterial); + document.setAllSecurityToBeRemoved(true); + } + + ProtectionPolicy protectionPolicy = exchange.getIn().getHeader( + PROTECTION_POLICY_HEADER_NAME, ProtectionPolicy.class); + + appendToPdfDocument(body, document, protectionPolicy); + OutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + document.save(byteArrayOutputStream); + return byteArrayOutputStream; + } + + private String doExtractText(Exchange exchange) throws IOException, CryptographyException, InvalidPasswordException, BadSecurityHandlerException { + LOG.debug("Got {} operation, going to extract text from provided pdf.", pdfConfiguration.getOperation()); + PDDocument document = exchange.getIn().getBody(PDDocument.class); + + if (document.isEncrypted()) { + DecryptionMaterial decryptionMaterial = exchange.getIn().getHeader(DECRYPTION_MATERIAL_HEADER_NAME, + DecryptionMaterial.class); + if (decryptionMaterial == null) { + throw new IllegalArgumentException(String.format("%s header is expected for %s operation " + + "on encrypted document", + DECRYPTION_MATERIAL_HEADER_NAME, + pdfConfiguration.getOperation())); + } + document.openProtection(decryptionMaterial); + } + + PDFTextStripper pdfTextStripper = new PDFTextStripper(); + return pdfTextStripper.getText(document); + } + + private OutputStream doCreate(Exchange exchange) throws IOException, BadSecurityHandlerException, COSVisitorException { + LOG.debug("Got {} operation, going to create and write provided string to pdf document.", + pdfConfiguration.getOperation()); + String body = exchange.getIn().getBody(String.class); + PDDocument document = new PDDocument(); + StandardProtectionPolicy protectionPolicy = exchange.getIn().getHeader( + PROTECTION_POLICY_HEADER_NAME, StandardProtectionPolicy.class); + appendToPdfDocument(body, document, protectionPolicy); + OutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + document.save(byteArrayOutputStream); + return byteArrayOutputStream; + } + + private void appendToPdfDocument(String text, PDDocument document, ProtectionPolicy protectionPolicy) throws IOException, BadSecurityHandlerException { + Collection<String> words = splitStrategy.split(text); + Collection<String> lines = lineBuilderStrategy.buildLines(words); + writeStrategy.write(lines, document); + if (protectionPolicy != null) { + document.protect(protectionPolicy); + } + } + + private TextProcessingAbstractFactory createTextProcessingFactory(PdfConfiguration pdfConfiguration) { + TextProcessingAbstractFactory result; + switch (pdfConfiguration.getTextProcessingFactory()) { + case autoFormatting: + result = new AutoFormattedWriterAbstractFactory(pdfConfiguration); + break; + case lineTermination: + result = new LineTerminationWriterAbstractFactory(pdfConfiguration); + break; + default: + throw new IllegalArgumentException(String.format("Unknown text processing factory %s", + pdfConfiguration.getTextProcessingFactory())); + } + return result; + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/4a5258c4/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/TextProcessingFactory.java ---------------------------------------------------------------------- diff --git a/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/TextProcessingFactory.java b/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/TextProcessingFactory.java new file mode 100644 index 0000000..c771be0 --- /dev/null +++ b/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/TextProcessingFactory.java @@ -0,0 +1,21 @@ +/** + * 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; + +public enum TextProcessingFactory { + autoFormatting, lineTermination +} http://git-wip-us.apache.org/repos/asf/camel/blob/4a5258c4/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/text/AutoFormattedWriterAbstractFactory.java ---------------------------------------------------------------------- diff --git a/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/text/AutoFormattedWriterAbstractFactory.java b/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/text/AutoFormattedWriterAbstractFactory.java new file mode 100644 index 0000000..2a80d23 --- /dev/null +++ b/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/text/AutoFormattedWriterAbstractFactory.java @@ -0,0 +1,47 @@ +/** + * 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.text; + +import org.apache.camel.component.pdf.PdfConfiguration; + +/** + * Builds set of classes for auto-formatting writing strategy. Text is getting sliced by words, + * then max amount of words that fits in the line will be written into pdf document. With this strategy all words + * that doesn't fit in the line will be moved to the new line. + */ +public class AutoFormattedWriterAbstractFactory implements TextProcessingAbstractFactory { + private final PdfConfiguration pdfConfiguration; + + public AutoFormattedWriterAbstractFactory(PdfConfiguration pdfConfiguration) { + this.pdfConfiguration = pdfConfiguration; + } + + @Override + public WriteStrategy createWriteStrategy() { + return new DefaultWriteStrategy(pdfConfiguration); + } + + @Override + public SplitStrategy createSplitStrategy() { + return new WordSplitStrategy(); + } + + @Override + public LineBuilderStrategy createLineBuilderStrategy() { + return new DefaultLineBuilderStrategy(pdfConfiguration); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/4a5258c4/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/text/DefaultLineBuilderStrategy.java ---------------------------------------------------------------------- diff --git a/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/text/DefaultLineBuilderStrategy.java b/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/text/DefaultLineBuilderStrategy.java new file mode 100644 index 0000000..cb53ac9 --- /dev/null +++ b/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/text/DefaultLineBuilderStrategy.java @@ -0,0 +1,169 @@ +/** + * 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.text; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.LinkedList; + +import org.apache.camel.component.pdf.PdfConfiguration; +import org.apache.commons.lang3.builder.ToStringBuilder; + +import static org.apache.camel.component.pdf.PdfConstants.MIN_CONTENT_WIDTH; + +/** + * Builds lines from words based on line width and PDF document page size. + * Built lines then will be written to pdf document. + */ +public class DefaultLineBuilderStrategy implements LineBuilderStrategy { + private final PdfConfiguration pdfConfiguration; + + public DefaultLineBuilderStrategy(PdfConfiguration pdfConfiguration) { + this.pdfConfiguration = pdfConfiguration; + } + + /** + * Builds lines from words. Utilizes the same behaviour as office software: + * <ul> + * <li>If word doesn't fit in current line, and current lines contains other words, then + * it will be moved to new line.</td> + * <li>Word doesn't fit in the line and line does not contain other words, then word will be + * slitted, and split index will be on max amount of characters that fits in the line </li> + * </ul> + */ + @Override + public Collection<String> buildLines(Collection<String> splittedText) throws IOException { + LinkedList<String> wordsList = new LinkedList<>(splittedText); + Collection<String> lines = new ArrayList<>(); + LineBuilder currentLine = new LineBuilder(); + float allowedLineWidth = getAllowedLineWidth(); + while (!wordsList.isEmpty()) { + String word = wordsList.removeFirst(); + if (isWordFitInCurrentLine(currentLine, word, allowedLineWidth)) { + currentLine.appendWord(word); + if (wordsList.isEmpty()) { + lines.add(currentLine.buildLine()); + } + } else if (currentLine.getWordsCount() != 0) { + lines.add(currentLine.buildLine()); + wordsList.addFirst(word); + } else { + int splitIndex = findSplitIndex(word, allowedLineWidth); + currentLine.appendWord(word.substring(0, splitIndex)); + lines.add(currentLine.buildLine()); + wordsList.addFirst(word.substring(splitIndex)); + } + } + return lines; + } + + private int findSplitIndex(String word, float allowedLineWidth) throws IOException { + // Using binary search algorithm to find max amount of characters that fit int the line. + + int middle = word.length() >> 1; + int end = word.length(); + + int currentSplitIndex = 0; + + do { + if (isLineFitInLineWidth(word.substring(0, middle), allowedLineWidth)) { + currentSplitIndex = middle; + middle += word.substring(middle, end).length() >> 1; + } else { + end = middle; + middle = currentSplitIndex + (word.substring(currentSplitIndex, middle).length() >> 1); + } + } while ((currentSplitIndex == -1) || !isSplitIndexFound(word, allowedLineWidth, currentSplitIndex)); + + return currentSplitIndex; + } + + private boolean isSplitIndexFound(String word, float allowedLineWidth, int currentSplitIndex) throws IOException { + return isLineFitInLineWidth(word.substring(0, currentSplitIndex), allowedLineWidth) + && !isLineFitInLineWidth(word.substring(0, currentSplitIndex + 1), allowedLineWidth); + } + + private boolean isWordFitInCurrentLine(LineBuilder currentLine, String word, float allowedLineWidth) throws IOException { + LineBuilder lineBuilder = currentLine.clone().appendWord(word); + return isLineFitInLineWidth(lineBuilder.buildLine(), allowedLineWidth); + } + + private boolean isLineFitInLineWidth(String currentLine, float allowedLineWidth) throws IOException { + float fontWidth = PdfUtils.getFontWidth( + currentLine, + pdfConfiguration.getFont(), + pdfConfiguration.getFontSize()); + + return fontWidth <= allowedLineWidth; + } + + public float getAllowedLineWidth() { + float result = pdfConfiguration.getPageSize().getWidth() + - pdfConfiguration.getMarginLeft() + - pdfConfiguration.getMarginRight(); + if (result < MIN_CONTENT_WIDTH) { + throw new IllegalStateException(String.format("Allowed line width cannot be < %d, make sure" + + " (marginLeft + marginRight) < pageSize", MIN_CONTENT_WIDTH)); + } + return result; + } + + private static final class LineBuilder { + private StringBuilder line = new StringBuilder(); + private int wordsCount; + + public LineBuilder() { } + + public LineBuilder(String line, int wordsCount) { + this.line = new StringBuilder(line); + this.wordsCount = wordsCount; + } + + + public LineBuilder appendWord(String word) { + line.append(word).append(" "); + wordsCount++; + return this; + } + + public String buildLine() { + String line = this.line.toString(); + reset(); + return line; + } + + public int getWordsCount() { + return wordsCount; + } + + private void reset() { + line = new StringBuilder(); + wordsCount = 0; + } + + public LineBuilder clone() { + return new LineBuilder(this.line.toString(), this.wordsCount); + } + + @Override + public String toString() { + return ToStringBuilder.reflectionToString(this); + } + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/4a5258c4/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/text/DefaultWriteStrategy.java ---------------------------------------------------------------------- diff --git a/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/text/DefaultWriteStrategy.java b/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/text/DefaultWriteStrategy.java new file mode 100644 index 0000000..188d846 --- /dev/null +++ b/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/text/DefaultWriteStrategy.java @@ -0,0 +1,81 @@ +/** + * 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.text; + +import java.io.IOException; +import java.util.Collection; + +import org.apache.camel.component.pdf.PdfConfiguration; +import org.apache.pdfbox.pdmodel.PDDocument; +import org.apache.pdfbox.pdmodel.PDPage; +import org.apache.pdfbox.pdmodel.edit.PDPageContentStream; + +/** + * Writes given lines to PDF document. If document already contains some text then new text will be appended + * to new page. + */ +public class DefaultWriteStrategy implements WriteStrategy { + + private final PdfConfiguration pdfConfiguration; + + public DefaultWriteStrategy(PdfConfiguration pdfConfiguration) { + this.pdfConfiguration = pdfConfiguration; + } + + @Override + public PDDocument write(Collection<String> lines, PDDocument document) throws IOException { + PDPage page = new PDPage(pdfConfiguration.getPageSize()); + document.addPage(page); + float x = pdfConfiguration.getMarginLeft(); + float y = page.getMediaBox().getHeight() - pdfConfiguration.getMarginTop(); + float averageFontHeight = PdfUtils.getAverageFontHeight(pdfConfiguration.getFont(), + pdfConfiguration.getFontSize()); + float lineSpacing = averageFontHeight * 2; + + PDPageContentStream contentStream = initializeContentStream(document, page); + for (String line : lines) { + writeLine(x, y, line, contentStream); + y -= lineSpacing; + if (goToNextPage(y)) { + contentStream.close(); + page = new PDPage(pdfConfiguration.getPageSize()); + document.addPage(page); + contentStream = initializeContentStream(document, page); + y = page.getMediaBox().getHeight() - pdfConfiguration.getMarginTop(); + } + } + contentStream.close(); + return document; + } + + private boolean goToNextPage(float y) { + return y < pdfConfiguration.getMarginBottom(); + } + + private void writeLine(float x, float y, String currentLine, PDPageContentStream contentStream) throws IOException { + contentStream.beginText(); + contentStream.moveTextPositionByAmount(x, y); + contentStream.drawString(currentLine); + contentStream.endText(); + } + + private PDPageContentStream initializeContentStream(PDDocument document, PDPage page) throws IOException { + PDPageContentStream contentStream = new PDPageContentStream(document, page); + contentStream.setFont(pdfConfiguration.getFont(), pdfConfiguration.getFontSize()); + return contentStream; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/4a5258c4/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/text/LineBuilderStrategy.java ---------------------------------------------------------------------- diff --git a/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/text/LineBuilderStrategy.java b/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/text/LineBuilderStrategy.java new file mode 100644 index 0000000..046991d --- /dev/null +++ b/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/text/LineBuilderStrategy.java @@ -0,0 +1,27 @@ +/** + * 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.text; + +import java.io.IOException; +import java.util.Collection; + +/** + * Builds lines from given words. + */ +public interface LineBuilderStrategy { + Collection<String> buildLines(Collection<String> splittedText) throws IOException; +} http://git-wip-us.apache.org/repos/asf/camel/blob/4a5258c4/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/text/LineTerminationWriterAbstractFactory.java ---------------------------------------------------------------------- diff --git a/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/text/LineTerminationWriterAbstractFactory.java b/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/text/LineTerminationWriterAbstractFactory.java new file mode 100644 index 0000000..30b4ca4 --- /dev/null +++ b/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/text/LineTerminationWriterAbstractFactory.java @@ -0,0 +1,56 @@ +/** + * 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.text; + +import java.io.IOException; +import java.util.Collection; + +import org.apache.camel.component.pdf.PdfConfiguration; + +/** + * Builds set of classes for line-termination writing strategy. Text getting sliced by line termination symbol (\n) + * and then it will be written regardless it fits in the line or not. + */ +public class LineTerminationWriterAbstractFactory implements TextProcessingAbstractFactory { + + private final PdfConfiguration pdfConfiguration; + + public LineTerminationWriterAbstractFactory(PdfConfiguration pdfConfiguration) { + this.pdfConfiguration = pdfConfiguration; + } + + @Override + public WriteStrategy createWriteStrategy() { + return new DefaultWriteStrategy(pdfConfiguration); + } + + @Override + public SplitStrategy createSplitStrategy() { + return new PatternSplitStrategy("\n"); + } + + @Override + public LineBuilderStrategy createLineBuilderStrategy() { + // Do not format when LineByLineWriter is used. + return new LineBuilderStrategy() { + @Override + public Collection<String> buildLines(Collection<String> splittedText) throws IOException { + return splittedText; + } + }; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/4a5258c4/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/text/PatternSplitStrategy.java ---------------------------------------------------------------------- diff --git a/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/text/PatternSplitStrategy.java b/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/text/PatternSplitStrategy.java new file mode 100644 index 0000000..3d8085f --- /dev/null +++ b/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/text/PatternSplitStrategy.java @@ -0,0 +1,33 @@ +/** + * 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.text; + +import java.util.Arrays; +import java.util.Collection; + +public class PatternSplitStrategy implements SplitStrategy { + private final String splitPattern; + + public PatternSplitStrategy(String splitPattern) { + this.splitPattern = splitPattern; + } + + @Override + public Collection<String> split(String text) { + return Arrays.asList(text.split(splitPattern)); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/4a5258c4/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/text/PdfUtils.java ---------------------------------------------------------------------- diff --git a/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/text/PdfUtils.java b/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/text/PdfUtils.java new file mode 100644 index 0000000..cc910f4 --- /dev/null +++ b/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/text/PdfUtils.java @@ -0,0 +1,39 @@ +/** + * 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.text; + +import java.io.IOException; + +import org.apache.pdfbox.pdmodel.font.PDFont; + +import static org.apache.camel.component.pdf.PdfConstants.PDF_PIXEL_SIZE; + +public final class PdfUtils { + private PdfUtils() { } + + public static float getAverageFontHeight(PDFont font, float fontSize) throws IOException { + return font.getFontHeight("A".getBytes(), 0, 1) / PDF_PIXEL_SIZE * fontSize; + } + + public static float getFontHeightForString(String str, PDFont font, float fontSize) throws IOException { + return font.getFontHeight(str.getBytes(), 0, 1) / PDF_PIXEL_SIZE * fontSize; + } + + public static float getFontWidth(String str, PDFont font, float fontSize) throws IOException { + return font.getStringWidth(str) / PDF_PIXEL_SIZE * fontSize; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/4a5258c4/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/text/SplitStrategy.java ---------------------------------------------------------------------- diff --git a/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/text/SplitStrategy.java b/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/text/SplitStrategy.java new file mode 100644 index 0000000..4976cc4 --- /dev/null +++ b/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/text/SplitStrategy.java @@ -0,0 +1,26 @@ +/** + * 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.text; + +import java.util.Collection; + +/** + * Strategy to split text. + */ +public interface SplitStrategy { + Collection<String> split(String text); +} http://git-wip-us.apache.org/repos/asf/camel/blob/4a5258c4/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/text/TextProcessingAbstractFactory.java ---------------------------------------------------------------------- diff --git a/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/text/TextProcessingAbstractFactory.java b/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/text/TextProcessingAbstractFactory.java new file mode 100644 index 0000000..3ea4eb6 --- /dev/null +++ b/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/text/TextProcessingAbstractFactory.java @@ -0,0 +1,23 @@ +/** + * 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.text; + +public interface TextProcessingAbstractFactory { + WriteStrategy createWriteStrategy(); + SplitStrategy createSplitStrategy(); + LineBuilderStrategy createLineBuilderStrategy(); +} http://git-wip-us.apache.org/repos/asf/camel/blob/4a5258c4/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/text/WordSplitStrategy.java ---------------------------------------------------------------------- diff --git a/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/text/WordSplitStrategy.java b/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/text/WordSplitStrategy.java new file mode 100644 index 0000000..339d6be --- /dev/null +++ b/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/text/WordSplitStrategy.java @@ -0,0 +1,34 @@ +/** + * 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.text; + +import java.util.Arrays; +import java.util.Collection; + +import org.apache.commons.lang3.StringUtils; + +/** + * Splits given string by words. + */ +public class WordSplitStrategy implements SplitStrategy { + + @Override + public Collection<String> split(String in) { + String text = in.replaceAll("\\r?\\n", " "); + return Arrays.asList(StringUtils.split(text)); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/4a5258c4/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/text/WriteStrategy.java ---------------------------------------------------------------------- diff --git a/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/text/WriteStrategy.java b/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/text/WriteStrategy.java new file mode 100644 index 0000000..81f26ab --- /dev/null +++ b/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/text/WriteStrategy.java @@ -0,0 +1,29 @@ +/** + * 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.text; + +import java.io.IOException; +import java.util.Collection; + +import org.apache.pdfbox.pdmodel.PDDocument; + +/** + * Strategy to write given lines to pdf document. + */ +public interface WriteStrategy { + PDDocument write(Collection<String> lines, PDDocument document) throws IOException; +} http://git-wip-us.apache.org/repos/asf/camel/blob/4a5258c4/components/camel-pdf/src/main/resources/META-INF/LICENSE.txt ---------------------------------------------------------------------- diff --git a/components/camel-pdf/src/main/resources/META-INF/LICENSE.txt b/components/camel-pdf/src/main/resources/META-INF/LICENSE.txt new file mode 100644 index 0000000..6b0b127 --- /dev/null +++ b/components/camel-pdf/src/main/resources/META-INF/LICENSE.txt @@ -0,0 +1,203 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed 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. + http://git-wip-us.apache.org/repos/asf/camel/blob/4a5258c4/components/camel-pdf/src/main/resources/META-INF/NOTICE.txt ---------------------------------------------------------------------- diff --git a/components/camel-pdf/src/main/resources/META-INF/NOTICE.txt b/components/camel-pdf/src/main/resources/META-INF/NOTICE.txt new file mode 100644 index 0000000..2e215bf --- /dev/null +++ b/components/camel-pdf/src/main/resources/META-INF/NOTICE.txt @@ -0,0 +1,11 @@ + ========================================================================= + == NOTICE file corresponding to the section 4 d of == + == the Apache License, Version 2.0, == + == in this case for the Apache Camel distribution. == + ========================================================================= + + This product includes software developed by + The Apache Software Foundation (http://www.apache.org/). + + Please read the different LICENSE files present in the licenses directory of + this distribution. http://git-wip-us.apache.org/repos/asf/camel/blob/4a5258c4/components/camel-pdf/src/main/resources/META-INF/services/org/apache/camel/component/pdf ---------------------------------------------------------------------- diff --git a/components/camel-pdf/src/main/resources/META-INF/services/org/apache/camel/component/pdf b/components/camel-pdf/src/main/resources/META-INF/services/org/apache/camel/component/pdf new file mode 100644 index 0000000..017bb7a --- /dev/null +++ b/components/camel-pdf/src/main/resources/META-INF/services/org/apache/camel/component/pdf @@ -0,0 +1,17 @@ +# 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. +# + +class=org.apache.camel.component.pdf.PdfComponent http://git-wip-us.apache.org/repos/asf/camel/blob/4a5258c4/components/camel-pdf/src/test/java/org/apache/camel/component/pdf/PdfAppendTest.java ---------------------------------------------------------------------- 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 new file mode 100644 index 0000000..2f5da2d --- /dev/null +++ b/components/camel-pdf/src/test/java/org/apache/camel/component/pdf/PdfAppendTest.java @@ -0,0 +1,166 @@ +/** + * 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.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +import org.apache.camel.EndpointInject; +import org.apache.camel.Exchange; +import org.apache.camel.Predicate; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.test.junit4.CamelTestSupport; +import org.apache.pdfbox.pdmodel.PDDocument; +import org.apache.pdfbox.pdmodel.PDPage; +import org.apache.pdfbox.pdmodel.edit.PDPageContentStream; +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.util.PDFTextStripper; +import org.junit.Before; +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.CoreMatchers.instanceOf; + +public class PdfAppendTest extends CamelTestSupport { + + @EndpointInject(uri = "mock:result") + protected MockEndpoint resultEndpoint; + + @Override + @Before + public void setUp() throws Exception { + super.setUp(); + } + + @Test + public void testAppend() throws Exception { + final String originalText = "Test"; + final String textToAppend = "Append"; + PDDocument document = new PDDocument(); + PDPage page = new PDPage(PDPage.PAGE_SIZE_A4); + document.addPage(page); + PDPageContentStream contentStream = new PDPageContentStream(document, page); + contentStream.setFont(PDType1Font.HELVETICA, 12); + contentStream.beginText(); + contentStream.moveTextPositionByAmount(20, 400); + contentStream.drawString(originalText); + contentStream.endText(); + contentStream.close(); + + template.sendBodyAndHeader("direct:start", textToAppend, PdfHeaderConstants.PDF_DOCUMENT_HEADER_NAME, document); + + resultEndpoint.setExpectedMessageCount(1); + resultEndpoint.expectedMessagesMatches(new Predicate() { + @Override + public boolean matches(Exchange exchange) { + Object body = exchange.getIn().getBody(); + assertThat(body, instanceOf(ByteArrayOutputStream.class)); + try { + PDDocument doc = PDDocument.load(new ByteArrayInputStream(((ByteArrayOutputStream) body).toByteArray())); + PDFTextStripper pdfTextStripper = new PDFTextStripper(); + String text = pdfTextStripper.getText(doc); + assertEquals(2, doc.getNumberOfPages()); + assertThat(text, containsString(originalText)); + assertThat(text, containsString(textToAppend)); + } catch (IOException e) { + throw new RuntimeException(e); + } + return true; + } + }); + resultEndpoint.assertIsSatisfied(); + + } + + @Test + public void testAppendEncrypted() throws Exception { + final String originalText = "Test"; + final String textToAppend = "Append"; + PDDocument document = new PDDocument(); + PDPage page = new PDPage(PDPage.PAGE_SIZE_A4); + document.addPage(page); + PDPageContentStream contentStream = new PDPageContentStream(document, page); + contentStream.setFont(PDType1Font.HELVETICA, 12); + contentStream.beginText(); + contentStream.moveTextPositionByAmount(20, 400); + contentStream.drawString(originalText); + contentStream.endText(); + contentStream.close(); + + final String ownerPass = "ownerPass"; + final String userPass = "userPass"; + AccessPermission accessPermission = new AccessPermission(); + accessPermission.setCanExtractContent(false); + StandardProtectionPolicy protectionPolicy = new StandardProtectionPolicy(ownerPass, userPass, accessPermission); + protectionPolicy.setEncryptionKeyLength(128); + + document.protect(protectionPolicy); + + ByteArrayOutputStream output = new ByteArrayOutputStream(); + document.save(output); + + // Encryption happens after saving. + PDDocument encryptedDocument = PDDocument.load(new ByteArrayInputStream(output.toByteArray())); + + Map<String, Object> headers = new HashMap<String, Object>(); + headers.put(PdfHeaderConstants.PDF_DOCUMENT_HEADER_NAME, encryptedDocument); + headers.put(PdfHeaderConstants.DECRYPTION_MATERIAL_HEADER_NAME, new StandardDecryptionMaterial(userPass)); + + template.sendBodyAndHeaders("direct:start", textToAppend, headers); + + resultEndpoint.setExpectedMessageCount(1); + resultEndpoint.expectedMessagesMatches(new Predicate() { + @Override + public boolean matches(Exchange exchange) { + Object body = exchange.getIn().getBody(); + assertThat(body, instanceOf(ByteArrayOutputStream.class)); + try { + PDDocument doc = PDDocument.load(new ByteArrayInputStream(((ByteArrayOutputStream) body).toByteArray())); + PDFTextStripper pdfTextStripper = new PDFTextStripper(); + String text = pdfTextStripper.getText(doc); + assertEquals(2, doc.getNumberOfPages()); + assertThat(text, containsString(originalText)); + assertThat(text, containsString(textToAppend)); + } catch (IOException e) { + throw new RuntimeException(e); + } + return true; + } + }); + resultEndpoint.assertIsSatisfied(); + + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:start") + .to("pdf:append") + .to("mock:result"); + } + }; + } +}