@Override public boolean equals(Object other) { if (this == other) { return true; } if (!(other instanceof EncryptParameters)) { return false; } EncryptParameters parameter = (EncryptParameters) other; return new EqualsBuilder().appendSuper(super.equals(other)).append(userPassword, parameter.getUserPassword()) .append(ownerPassword, parameter.getOwnerPassword()) .append(encryptionAlgorithm, parameter.getEncryptionAlgorithm()) .append(permissions, parameter.getPermissions()).isEquals(); } }
@Override public boolean equals(Object other) { if (this == other) { return true; } if (!(other instanceof EncryptParameters)) { return false; } EncryptParameters parameter = (EncryptParameters) other; return new EqualsBuilder().appendSuper(super.equals(other)).append(userPassword, parameter.getUserPassword()) .append(ownerPassword, parameter.getOwnerPassword()) .append(encryptionAlgorithm, parameter.getEncryptionAlgorithm()) .append(permissions, parameter.getPermissions()).isEquals(); } }
public void before(EncryptParameters parameters) { totalSteps = parameters.getSourceList().size(); sourceOpener = PdfSourceOpeners.newPartialReadOpener(); outputWriter = OutputWriters.newMultipleOutputWriter(parameters.isOverwrite()); for (PdfAccessPermission permission : parameters.getPermissions()) { permissions |= getAccessPermission(permission); } }
@Test public void testClear() { EncryptParameters victim = new EncryptParameters(PdfEncryption.STANDARD_ENC_40); victim.addPermission(PdfAccessPermission.ANNOTATION); assertEquals(1, victim.getPermissions().size()); victim.clearPermissions(); assertEquals(0, victim.getPermissions().size()); }
@Test(expected = UnsupportedOperationException.class) public void testUnmodifiableList() { EncryptParameters victim = new EncryptParameters(PdfEncryption.STANDARD_ENC_40); victim.addPermission(PdfAccessPermission.ANNOTATION); victim.getPermissions().clear(); }
/** * @param params * @return the {@link StandardSecurity} corresponding to the {@link EncryptParameters} */ public static StandardSecurity securityFromParams(EncryptParameters params) { return new StandardSecurity(params.getOwnerPassword(), params.getUserPassword(), getEncryptionFrom(params.getEncryptionAlgorithm()), getPermissionsFrom(params.getPermissions()), true); }
/** * @param params * @return the {@link StandardSecurity} corresponding to the {@link EncryptParameters} */ public static StandardSecurity securityFromParams(EncryptParameters params) { return new StandardSecurity(params.getOwnerPassword(), params.getUserPassword(), getEncryptionFrom(params.getEncryptionAlgorithm()), getPermissionsFrom(params.getPermissions()), true); }
@Test public void testAdd() { EncryptParameters victim = new EncryptParameters(PdfEncryption.STANDARD_ENC_40); victim.addPermission(PdfAccessPermission.ANNOTATION); assertEquals(1, victim.getPermissions().size()); }
@Test public void noPermissions() { EncryptParameters parameters = defaultCommandLine().invokeSejdaConsole(); assertEquals(Collections.EMPTY_SET, parameters.getPermissions()); }
@Override public void execute(EncryptParameters parameters) throws TaskException { int currentStep = 0; for (PdfSource<?> source : parameters.getSourceList()) { executionContext().assertTaskNotCancelled(); currentStep++; LOG.debug("Opening {}", source); try { documentHandler = source.open(documentLoader); documentHandler.setCreatorOnPDDocument(); File tmpFile = createTemporaryBuffer(parameters.getOutput()); LOG.debug("Created output on temporary buffer {}", tmpFile); documentHandler.setVersionOnPDDocument(parameters.getVersion()); documentHandler.setCompress(parameters.isCompress()); documentHandler.savePDDocument(tmpFile, security); String outName = nameGenerator(parameters.getOutputPrefix()) .generate(nameRequest().originalName(source.getName()).fileNumber(currentStep)); outputWriter.addOutput(file(tmpFile).name(outName)); } finally { nullSafeCloseQuietly(documentHandler); } notifyEvent(executionContext().notifiableTaskMetadata()).stepsCompleted(currentStep).outOf(totalSteps); } parameters.getOutput().accept(outputWriter); LOG.debug("Input documents encrypted using {}, standard security handler revision {} and written to {}", security.encryption, security.encryption.revision.revisionNumber, parameters.getOutput()); LOG.debug("Permissions: {}", parameters.getPermissions().stream().map(PdfAccessPermission::getFriendlyName) .collect(Collectors.joining(", "))); }
@Override public void execute(EncryptParameters parameters) throws TaskException { int currentStep = 0; for (PdfSource<?> source : parameters.getSourceList()) { executionContext().assertTaskNotCancelled(); currentStep++; LOG.debug("Opening {}", source); try { documentHandler = source.open(documentLoader); documentHandler.setCreatorOnPDDocument(); File tmpFile = createTemporaryBuffer(parameters.getOutput()); LOG.debug("Created output on temporary buffer {}", tmpFile); documentHandler.setVersionOnPDDocument(parameters.getVersion()); documentHandler.setCompress(parameters.isCompress()); documentHandler.savePDDocument(tmpFile, security); String outName = nameGenerator(parameters.getOutputPrefix()) .generate(nameRequest().originalName(source.getName()).fileNumber(currentStep)); outputWriter.addOutput(file(tmpFile).name(outName)); } finally { nullSafeCloseQuietly(documentHandler); } notifyEvent(executionContext().notifiableTaskMetadata()).stepsCompleted(currentStep).outOf(totalSteps); } parameters.getOutput().accept(outputWriter); LOG.debug("Input documents encrypted using {}, standard security handler revision {} and written to {}", security.encryption, security.encryption.revision.revisionNumber, parameters.getOutput()); LOG.debug("Permissions: {}", parameters.getPermissions().stream().map(PdfAccessPermission::getFriendlyName) .collect(Collectors.joining(", "))); }
@Test public void testPermissions() { EncryptParameters parameters = defaultCommandLine().with("--allow", "print modify copy modifyannotations fill screenreaders assembly degradedprinting") .invokeSejdaConsole(); assertThat(parameters.getPermissions(), hasItems(PdfAccessPermission.values())); } }
@Test public void defaultPermissions() throws IOException { setUpParameters(PdfEncryption.STANDARD_ENC_128); assertEquals(parameters.getPermissions().size(), 0); parameters.setOwnerPassword("test"); testContext.directoryOutputTo(parameters); execute(parameters); testContext.assertTaskCompleted(); testContext.forEachPdfOutput(d -> { assertTrue(d.isEncrypted()); assertFalse(d.getCurrentAccessPermission().canAssembleDocument()); assertFalse(d.getCurrentAccessPermission().canExtractContent()); assertTrue(d.getCurrentAccessPermission().canExtractForAccessibility()); assertFalse(d.getCurrentAccessPermission().canFillInForm()); assertFalse(d.getCurrentAccessPermission().canModify()); assertFalse(d.getCurrentAccessPermission().canModifyAnnotations()); assertFalse(d.getCurrentAccessPermission().canPrint()); assertFalse(d.getCurrentAccessPermission().canPrintDegraded()); }); } }