diff --git a/pom.xml b/pom.xml index 89e22b575..f2b96a611 100644 --- a/pom.xml +++ b/pom.xml @@ -363,7 +363,7 @@ 2.9.1 4.2.0.Final 1.3 - 2.2.12 + 2.2.13 2.1.3 1.66 3.4.2 diff --git a/sejda-core/src/test/java/org/sejda/core/service/SetMetadataTaskTest.java b/sejda-core/src/test/java/org/sejda/core/service/SetMetadataTaskTest.java index ed8fe9bf4..3351c208b 100644 --- a/sejda-core/src/test/java/org/sejda/core/service/SetMetadataTaskTest.java +++ b/sejda-core/src/test/java/org/sejda/core/service/SetMetadataTaskTest.java @@ -32,8 +32,10 @@ import org.sejda.sambox.util.DateConverter; import java.io.IOException; +import java.util.Arrays; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; /** * Test unit for the set metadata task @@ -72,6 +74,22 @@ public void testExecuteEncrypted() throws IOException { doExecute(); } + @Test + public void removingField() throws IOException { + SetMetadataParameters parameters = new SetMetadataParameters(); + parameters.addFieldsToRemove(Arrays.asList("Creator", "Author", "RandomStringThatDoesNotExist")); + parameters.setSource(shortInput()); + + parameters.setExistingOutputPolicy(ExistingOutputPolicy.OVERWRITE); + testContext.pdfOutputTo(parameters); + execute(parameters); + + PDDocument document = testContext.assertTaskCompleted(); + PDDocumentInformation info = document.getDocumentInformation(); + assertNull(info.getAuthor()); + assertNull(info.getCreator()); + } + private void doExecute() throws IOException { testContext.pdfOutputTo(parameters); execute(parameters); diff --git a/sejda-model/src/main/java/org/sejda/model/parameter/SetMetadataParameters.java b/sejda-model/src/main/java/org/sejda/model/parameter/SetMetadataParameters.java index 521b0515b..8ff6ef757 100644 --- a/sejda-model/src/main/java/org/sejda/model/parameter/SetMetadataParameters.java +++ b/sejda-model/src/main/java/org/sejda/model/parameter/SetMetadataParameters.java @@ -22,12 +22,12 @@ import org.apache.commons.lang3.builder.EqualsBuilder; import org.apache.commons.lang3.builder.HashCodeBuilder; +import org.sejda.commons.collection.NullSafeSet; import org.sejda.model.parameter.base.SinglePdfSourceSingleOutputParameters; import org.sejda.model.validation.constraint.NotEmpty; import org.sejda.model.validation.constraint.SingleOutputAllowedExtensions; -import java.util.HashMap; -import java.util.Map; +import java.util.*; /** * Parameter class for the set metadata manipulation. @@ -40,6 +40,7 @@ public final class SetMetadataParameters extends SinglePdfSourceSingleOutputPara @NotEmpty private final Map metadata = new HashMap(); + private final Set toRemove = new NullSafeSet<>(); public Map getMetadata() { return metadata; @@ -52,10 +53,22 @@ public void putAll(Map m) { public void put(String key, String metadata) { this.metadata.put(key, metadata); } + + public void addFieldToRemove(String fieldName) { + toRemove.add(fieldName); + } + + public void addFieldsToRemove(Collection fieldNames) { + toRemove.addAll(fieldNames); + } + + public Set getToRemove() { + return toRemove; + } @Override public int hashCode() { - return new HashCodeBuilder().appendSuper(super.hashCode()).append(metadata).toHashCode(); + return new HashCodeBuilder().appendSuper(super.hashCode()).append(metadata).append(toRemove).toHashCode(); } @Override @@ -68,6 +81,7 @@ public boolean equals(Object other) { } SetMetadataParameters parameter = (SetMetadataParameters) other; return new EqualsBuilder().appendSuper(super.equals(other)).append(metadata, parameter.metadata) + .append(toRemove, parameter.toRemove) .isEquals(); } } diff --git a/sejda-sambox/src/main/java/org/sejda/impl/sambox/SetMetadataTask.java b/sejda-sambox/src/main/java/org/sejda/impl/sambox/SetMetadataTask.java index 16bfe5891..70fc7f1d7 100644 --- a/sejda-sambox/src/main/java/org/sejda/impl/sambox/SetMetadataTask.java +++ b/sejda-sambox/src/main/java/org/sejda/impl/sambox/SetMetadataTask.java @@ -85,6 +85,11 @@ public void onBeforeWrite() throws IOException { LOG.trace("'{}' -> '{}'", meta.getKey(), meta.getValue()); actualMeta.setCustomMetadataValue(meta.getKey(), meta.getValue()); } + + for (String keyToRemove : parameters.getToRemove()) { + LOG.trace("Removing '{}'", keyToRemove); + actualMeta.removeMetadataField(keyToRemove); + } } });