use of org.apache.pdfbox.pdmodel.encryption.StandardProtectionPolicy in project camel by apache.
the class PdfAppendTest method testAppendEncrypted.
@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();
}
use of org.apache.pdfbox.pdmodel.encryption.StandardProtectionPolicy in project camel by apache.
the class PdfTextExtractionTest method testExtractTextFromEncrypted.
@Test
public void testExtractTextFromEncrypted() throws Exception {
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);
PDDocument document = new PDDocument();
final String expectedText = "Test string";
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(expectedText);
contentStream.endText();
contentStream.close();
document.protect(protectionPolicy);
ByteArrayOutputStream output = new ByteArrayOutputStream();
document.save(output);
// Encryption happens after saving.
PDDocument encryptedDocument = PDDocument.load(new ByteArrayInputStream(output.toByteArray()));
template.sendBodyAndHeader("direct:start", encryptedDocument, PdfHeaderConstants.DECRYPTION_MATERIAL_HEADER_NAME, new StandardDecryptionMaterial(userPass));
resultEndpoint.setExpectedMessageCount(1);
resultEndpoint.expectedMessagesMatches(new Predicate() {
@Override
public boolean matches(Exchange exchange) {
Object body = exchange.getIn().getBody();
assertThat(body, instanceOf(String.class));
assertThat((String) body, containsString(expectedText));
return true;
}
});
resultEndpoint.assertIsSatisfied();
document.isEncrypted();
}
use of org.apache.pdfbox.pdmodel.encryption.StandardProtectionPolicy in project camel by apache.
the class PdfCreationTest method testPdfCreationWithEncryption.
@Test
public void testPdfCreationWithEncryption() throws Exception {
final String ownerPass = "ownerPass";
final String userPass = "userPass";
final String expectedText = "expectedText";
AccessPermission accessPermission = new AccessPermission();
accessPermission.setCanPrint(false);
StandardProtectionPolicy protectionPolicy = new StandardProtectionPolicy(ownerPass, userPass, accessPermission);
protectionPolicy.setEncryptionKeyLength(128);
template.sendBodyAndHeader("direct:start", expectedText, PdfHeaderConstants.PROTECTION_POLICY_HEADER_NAME, protectionPolicy);
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()));
assertTrue("Expected encrypted document", doc.isEncrypted());
doc.decrypt(userPass);
assertFalse("Printing should not be permitted", doc.getCurrentAccessPermission().canPrint());
PDFTextStripper pdfTextStripper = new PDFTextStripper();
String text = pdfTextStripper.getText(doc);
assertEquals(1, doc.getNumberOfPages());
assertThat(text, containsString(expectedText));
} catch (Exception e) {
throw new RuntimeException(e);
}
return true;
}
});
resultEndpoint.assertIsSatisfied();
}
use of org.apache.pdfbox.pdmodel.encryption.StandardProtectionPolicy in project camel by apache.
the class PdfProducer method doCreate.
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;
}
Aggregations