From 8e0ccca7cfd1680e33737e0db8d6e1091661568c Mon Sep 17 00:00:00 2001 From: Thomas <> Date: Mon, 26 Jun 2023 21:11:26 +0200 Subject: refact(placeholder): clean-up, bugfix and optimize QR-Code placeholder detection Major re-factoring of QR-Code detection with huge clean-up of code. --- .../egiz/pdfas/api/processing/DocumentToSign.java | 2 + .../common/settings/SignatureProfileSettings.java | 442 ++++++----- .../api/commons/DynamicSignatureProfileImpl.java | 14 +- .../pdfas/lib/api/IConfigurationConstants.java | 1 - .../gv/egiz/pdfas/lib/api/sign/SignParameter.java | 16 + .../gv/egiz/pdfas/lib/impl/SignParameterImpl.java | 9 +- .../lib/impl/configuration/ConfigurationImpl.java | 265 ++++--- .../configuration/PlaceholderConfiguration.java | 103 ++- .../configuration/PlaceholderWebConfiguration.java | 24 - .../lib/impl/placeholder/PlaceholderExtractor.java | 7 +- .../lib/impl/placeholder/PlaceholderFilter.java | 176 ++--- .../placeholder/SignaturePlaceholderContext.java | 95 --- .../at/gv/egiz/pdfas/lib/settings/Settings.java | 684 ++++++++--------- .../placeholder/PDFBoxPlaceholderExtractor.java | 28 +- .../SignatureFieldsAndPlaceHolderExtractor.java | 134 ++-- .../placeholder/SignaturePlaceholderExtractor.java | 818 ++++++++++----------- .../impl/signing/pdfbox2/PADESPDFBOXSigner.java | 352 ++++----- .../testpdfbox/PDFBoxPlaceholderExtractorTest.java | 51 ++ ...SignatureFieldsAndPlaceHolderExtractorTest.java | 21 +- .../test/resources/data/platzhalter_en_de_test.pdf | Bin 0 -> 46732 bytes .../at/gv/egiz/pdfas/web/helper/PdfAsHelper.java | 6 +- .../egiz/pdfas/web/servlets/ExternSignServlet.java | 17 +- 22 files changed, 1417 insertions(+), 1848 deletions(-) delete mode 100644 pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/configuration/PlaceholderWebConfiguration.java delete mode 100644 pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/placeholder/SignaturePlaceholderContext.java create mode 100644 pdf-as-pdfbox-2/src/test/java/at/gv/egiz/pdfas/lib/testpdfbox/PDFBoxPlaceholderExtractorTest.java create mode 100644 pdf-as-pdfbox-2/src/test/resources/data/platzhalter_en_de_test.pdf diff --git a/pdf-as-common/src/main/java/at/gv/egiz/pdfas/api/processing/DocumentToSign.java b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/api/processing/DocumentToSign.java index 6cc3a933..6d92b312 100644 --- a/pdf-as-common/src/main/java/at/gv/egiz/pdfas/api/processing/DocumentToSign.java +++ b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/api/processing/DocumentToSign.java @@ -18,5 +18,7 @@ public class DocumentToSign implements Serializable { String qrCodeContent; String profile; + + String placeHolderId; } diff --git a/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/settings/SignatureProfileSettings.java b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/settings/SignatureProfileSettings.java index 7f047278..65722f88 100644 --- a/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/settings/SignatureProfileSettings.java +++ b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/settings/SignatureProfileSettings.java @@ -3,19 +3,19 @@ * PDF-AS has been contracted by the E-Government Innovation Center EGIZ, a * joint initiative of the Federal Chancellery Austria and Graz University of * Technology. - * + * * Licensed under the EUPL, Version 1.1 or - as soon they will be approved by * the European Commission - subsequent versions of the EUPL (the "Licence"); * You may not use this work except in compliance with the Licence. * You may obtain a copy of the Licence at: * http://www.osor.eu/eupl/ - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the Licence is distributed on an "AS IS" basis, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the Licence for the specific language governing permissions and * limitations under the Licence. - * + * * This product combines work with different licenses. See the "NOTICE" text * file for details on the various modules and licenses. * The "NOTICE" text file is part of the distribution. Any derivative works @@ -24,7 +24,6 @@ package at.gv.egiz.pdfas.common.settings; import java.util.HashMap; -import java.util.Iterator; import java.util.Map; import org.slf4j.Logger; @@ -35,241 +34,222 @@ import at.gv.egiz.pdfas.common.exceptions.PDFASError; public class SignatureProfileSettings implements IProfileConstants { - private static final Logger logger = LoggerFactory - .getLogger(SignatureProfileSettings.class); + private static final Logger logger = LoggerFactory + .getLogger(SignatureProfileSettings.class); - private Map profileInformations = new HashMap(); + private final Map profileInformations = + new HashMap<>(); - private Map profileSettings = new HashMap(); + private final Map profileSettings = new HashMap<>(); - private String profileID; + private final String profileID; - private String pdfAVersion = null; + private String pdfAVersion = null; - private ISettings configuration; + private final ISettings configuration; - public SignatureProfileSettings(String profileID, ISettings configuration) throws PDFASError { - - if (!configuration.hasPrefix(SIG_OBJ + profileID)) { + public SignatureProfileSettings(String profileID, ISettings configuration) throws PDFASError { + + if (!configuration.hasPrefix(SIG_OBJ + profileID)) { throw new PDFASError(ErrorConstants.ERROR_SIG_INVALID_PROFILE, PDFASError.buildInfoString(ErrorConstants.ERROR_SIG_INVALID_PROFILE, profileID)); } - - this.profileID = profileID; - String profilePrefix = SIG_OBJ + profileID + KEY_SEPARATOR; - String keysPrefix = profilePrefix + PROFILE_KEY; - String valuesPrefix = profilePrefix + PROFILE_VALUE; - String tablePrefix = profilePrefix + TABLE; - this.configuration = configuration; - - logger.debug("Reading Profile: " + profileID); - logger.debug("Keys Prefix: " + keysPrefix); - logger.debug("Values Prefix: " + valuesPrefix); - logger.debug("Table Prefix: " + tablePrefix); - - Map keys = configuration.getValuesPrefix(keysPrefix); - Map values = configuration.getValuesPrefix(valuesPrefix); - - if (keys != null) { - Iterator keyIterator = keys.keySet().iterator(); - - while (keyIterator.hasNext()) { - String key = keyIterator.next(); - key = key.substring(key.lastIndexOf('.') + 1); - String valueKey = keys.get(keysPrefix + KEY_SEPARATOR + key); - - String valueValue = values.get(valuesPrefix + KEY_SEPARATOR - + key); - - // Lookup default values - if(valueKey == null) { - valueKey = DefaultSignatureProfileSettings.getDefaultKeyCaption(key); - } - - if(valueValue == null) { - valueValue = DefaultSignatureProfileSettings.getDefaultKeyValue(key); - } - - SignatureProfileEntry entry = new SignatureProfileEntry(); - entry.setKey(key); - entry.setCaption(valueKey); - entry.setValue(valueValue); - profileInformations.put(key, entry); - logger.debug(" " + entry.toString()); - } - } - - if (values != null) { - // Find entries where only values exists - Iterator valuesIterator = values.keySet().iterator(); - - while (valuesIterator.hasNext()) { - String key = valuesIterator.next(); - key = key.substring(key.lastIndexOf('.') + 1); - - String valueValue = values.get(valuesPrefix + KEY_SEPARATOR - + key); - - // Lookup default values - if(valueValue == null) { - valueValue = DefaultSignatureProfileSettings.getDefaultKeyValue(key); - } - - SignatureProfileEntry entry = profileInformations.get(key); - if (entry == null) { - entry = new SignatureProfileEntry(); - entry.setKey(key); - entry.setCaption(null); - entry.setValue(valueValue); - profileInformations.put(key, entry); - } - - logger.debug(" " + entry.toString()); - } - } - - Map others = configuration - .getValuesPrefix(profilePrefix); - - if(others != null) { - Iterator otherIterator = others.keySet().iterator(); - - while (otherIterator.hasNext()) { - String key = otherIterator.next(); - - logger.trace("Checking key " + key); - if (key.startsWith(keysPrefix) || key.startsWith(valuesPrefix) - || key.startsWith(tablePrefix)) { - continue; - } - - String value = others.get(key); - key = key.substring(key.lastIndexOf('.') + 1); - - profileSettings.put(key, value); - - logger.debug(" Settings: " + key + " : " + value); - } - } - - Iterator dumpIterator = - profileInformations.values().iterator(); - - logger.debug("Settings for profile {}", profileID); - while(dumpIterator.hasNext()) { - SignatureProfileEntry entry = dumpIterator.next(); - logger.debug(" " + entry.toString()); - } - } - - public String getCaption(String key) { - SignatureProfileEntry entry = profileInformations.get(key); - if (entry != null) { - return entry.getCaption(); - } - return null; - } - - protected String getDefaultValue(String key) { - String profilePrefix = SIG_OBJ + profileID + KEY_SEPARATOR; - logger.debug("Searching default value for: " + key); - if (key.startsWith(profilePrefix)) { - key = key.substring(profilePrefix.length()); - } - key = "default." + key; - logger.debug("Searching default value for: " + key); - return this.configuration.getValue(key); - } - - public String getValue(String key) { - logger.debug("Searching: " + key); - SignatureProfileEntry entry = profileInformations.get(key); - if (entry != null) { - String value = entry.getValue(); - - if (value == null) { - return getDefaultValue(key); - } - - return value; - } - String v = profileSettings.get(key); - if (v != null) { - return v; - } - return getDefaultValue(key); - } - - public String getProfileID() { - return profileID; - } - - public String getSigningReason() { - return this.getValue(SIGNING_REASON); - } - - public String getSignFieldValue() { - return this.getValue(SIGNFIELD_VALUE); - } - - public String getProfileTimeZone() { - return this.getValue(TIMEZONE_BASE); - } - - public void setPDFAVersion(String version) { - this.pdfAVersion = version; - } - - public boolean isPDFA() { - - if(this.pdfAVersion != null) { - return "1".equals(this.pdfAVersion); - } - - SignatureProfileEntry entry = profileInformations.get(SIG_PDFA_VALID); - if (entry != null) { - String value = entry.getCaption(); - return "true".equals(value); - } - - entry = profileInformations.get(SIG_PDFA1B_VALID); - if (entry != null) { - String value = entry.getCaption(); - return "true".equals(value); - } - return false; - } - - public boolean isPDFUA() { - SignatureProfileEntry entry = profileInformations.get(SIG_PDFUA_FORCE); - if (entry != null) { - String value = entry.getCaption(); - return "true".equals(value); - } - return false; - } - - - public boolean isLatin1Encoding() { - SignatureProfileEntry entry = profileInformations.get(LATIN1_ENCODING); - if (entry != null) { - String value = entry.getCaption(); - return "true".equals(value); - } - return false; - } - - public boolean isPDFA3() { - if(this.pdfAVersion != null) { - return "3".equals(this.pdfAVersion); - } - - SignatureProfileEntry entry = profileInformations.get(SIG_PDFA_VALID); - if (entry != null) { - String value = entry.getCaption(); - return "true".equals(value); - } - return false; - } + + this.profileID = profileID; + final String profilePrefix = SIG_OBJ + profileID + KEY_SEPARATOR; + final String keysPrefix = profilePrefix + PROFILE_KEY; + final String valuesPrefix = profilePrefix + PROFILE_VALUE; + final String tablePrefix = profilePrefix + TABLE; + this.configuration = configuration; + + logger.debug("Reading Profile: " + profileID); + logger.debug("Keys Prefix: " + keysPrefix); + logger.debug("Values Prefix: " + valuesPrefix); + logger.debug("Table Prefix: " + tablePrefix); + + final Map keys = configuration.getValuesPrefix(keysPrefix); + final Map values = configuration.getValuesPrefix(valuesPrefix); + + for (String key : keys.keySet()) { + key = key.substring(key.lastIndexOf('.') + 1); + String valueKey = keys.get(keysPrefix + KEY_SEPARATOR + key); + + String valueValue = values.get(valuesPrefix + KEY_SEPARATOR + + key); + + // Lookup default values + if (valueKey == null) { + valueKey = DefaultSignatureProfileSettings.getDefaultKeyCaption(key); + } + + if (valueValue == null) { + valueValue = DefaultSignatureProfileSettings.getDefaultKeyValue(key); + } + + final SignatureProfileEntry entry = new SignatureProfileEntry(); + entry.setKey(key); + entry.setCaption(valueKey); + entry.setValue(valueValue); + profileInformations.put(key, entry); + logger.debug(" " + entry.toString()); + } + + for (String key : values.keySet()) { + key = key.substring(key.lastIndexOf('.') + 1); + + String valueValue = values.get(valuesPrefix + KEY_SEPARATOR + + key); + + // Lookup default values + if (valueValue == null) { + valueValue = DefaultSignatureProfileSettings.getDefaultKeyValue(key); + } + + SignatureProfileEntry entry = profileInformations.get(key); + if (entry == null) { + entry = new SignatureProfileEntry(); + entry.setKey(key); + entry.setCaption(null); + entry.setValue(valueValue); + profileInformations.put(key, entry); + } + + logger.debug(" " + entry.toString()); + } + + final Map others = configuration + .getValuesPrefix(profilePrefix); + + if (others != null) { + for (String key : others.keySet()) { + logger.trace("Checking key " + key); + if (key.startsWith(keysPrefix) || key.startsWith(valuesPrefix) + || key.startsWith(tablePrefix)) { + continue; + } + + final String value = others.get(key); + key = key.substring(key.lastIndexOf('.') + 1); + + profileSettings.put(key, value); + + logger.debug(" Settings: " + key + " : " + value); + } + } + + logger.debug("Settings for profile {}", profileID); + for (final SignatureProfileEntry entry : profileInformations.values()) { + logger.debug(" " + entry.toString()); + } + } + + public String getCaption(String key) { + final SignatureProfileEntry entry = profileInformations.get(key); + if (entry != null) { + return entry.getCaption(); + } + return null; + } + + protected String getDefaultValue(String key) { + final String profilePrefix = SIG_OBJ + profileID + KEY_SEPARATOR; + logger.debug("Searching default value for: " + key); + if (key.startsWith(profilePrefix)) { + key = key.substring(profilePrefix.length()); + } + key = "default." + key; + logger.debug("Searching default value for: " + key); + return this.configuration.getValue(key); + } + + public String getValue(String key) { + logger.debug("Searching: " + key); + final SignatureProfileEntry entry = profileInformations.get(key); + if (entry != null) { + final String value = entry.getValue(); + + if (value == null) { + return getDefaultValue(key); + } + + return value; + } + final String v = profileSettings.get(key); + if (v != null) { + return v; + } + return getDefaultValue(key); + } + + public String getProfileID() { + return profileID; + } + + public String getSigningReason() { + return this.getValue(SIGNING_REASON); + } + + public String getSignFieldValue() { + return this.getValue(SIGNFIELD_VALUE); + } + + public String getProfileTimeZone() { + return this.getValue(TIMEZONE_BASE); + } + + public void setPDFAVersion(String version) { + this.pdfAVersion = version; + } + + public boolean isPDFA() { + + if (this.pdfAVersion != null) { + return "1".equals(this.pdfAVersion); + } + + SignatureProfileEntry entry = profileInformations.get(SIG_PDFA_VALID); + if (entry != null) { + final String value = entry.getCaption(); + return "true".equals(value); + } + + entry = profileInformations.get(SIG_PDFA1B_VALID); + if (entry != null) { + final String value = entry.getCaption(); + return "true".equals(value); + } + return false; + } + + public boolean isPDFUA() { + final SignatureProfileEntry entry = profileInformations.get(SIG_PDFUA_FORCE); + if (entry != null) { + final String value = entry.getCaption(); + return "true".equals(value); + } + return false; + } + + public boolean isLatin1Encoding() { + final SignatureProfileEntry entry = profileInformations.get(LATIN1_ENCODING); + if (entry != null) { + final String value = entry.getCaption(); + return "true".equals(value); + } + return false; + } + + public boolean isPDFA3() { + if (this.pdfAVersion != null) { + return "3".equals(this.pdfAVersion); + } + + final SignatureProfileEntry entry = profileInformations.get(SIG_PDFA_VALID); + if (entry != null) { + final String value = entry.getCaption(); + return "true".equals(value); + } + return false; + } } diff --git a/pdf-as-legacy/src/main/java/at/gv/egiz/pdfas/api/commons/DynamicSignatureProfileImpl.java b/pdf-as-legacy/src/main/java/at/gv/egiz/pdfas/api/commons/DynamicSignatureProfileImpl.java index cc51d5e4..ce36fb9f 100644 --- a/pdf-as-legacy/src/main/java/at/gv/egiz/pdfas/api/commons/DynamicSignatureProfileImpl.java +++ b/pdf-as-legacy/src/main/java/at/gv/egiz/pdfas/api/commons/DynamicSignatureProfileImpl.java @@ -186,19 +186,7 @@ public class DynamicSignatureProfileImpl implements DynamicSignatureProfile { cfg = (ISettings)configuration; String parentKey = "sig_obj." + parentProfile + "."; - Map properties = cfg.getValuesPrefix(parentKey); - //Properties props = cfg.getProperties(); - // DTI: props.keys() does not support default properties, therefore we should better use props.propertyNames() -// for (Enumeration e = props.keys(); e.hasMoreElements();) { - /*for (Enumeration e = props.propertyNames(); e.hasMoreElements();) { - String oldKey = (String) e.nextElement(); - if (oldKey.startsWith("sig_obj." + parentProfile + ".")) { - String newKey = StringUtils.replace(oldKey, parentProfile, name); - String val = props.getProperty(oldKey); - this.newProps.put(newKey, val); - } - }*/ - + Map properties = cfg.getValuesPrefix(parentKey); Iterator keyIt = properties.keySet().iterator(); while(keyIt.hasNext()) { diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/api/IConfigurationConstants.java b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/api/IConfigurationConstants.java index 21a8e3b9..8314eef6 100644 --- a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/api/IConfigurationConstants.java +++ b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/api/IConfigurationConstants.java @@ -45,7 +45,6 @@ public interface IConfigurationConstants { public static final String LEGACY_40_POSITIONING = ".legacy40.pos"; public static final String MIN_WIDTH = "minWidth"; - public static final String PLACEHOLDER_WEB_ID = "placeholder_web_id"; public static final String PLACEHOLDER_ID = "placeholder_id"; public static final String PLACEHOLDER_MODE = "placeholder_mode"; diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/api/sign/SignParameter.java b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/api/sign/SignParameter.java index 6a7ccf24..3485c9ea 100644 --- a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/api/sign/SignParameter.java +++ b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/api/sign/SignParameter.java @@ -54,6 +54,22 @@ public interface SignParameter extends PdfAsParameter { */ public void setSignaturePosition(String signaturePosition); + + /** + * Get Id of a placeholder that should be used for positioning. + * + * @return Id of a placeholder + */ + String getPlaceHolderId(); + + + /** + * Set Id of a placeholder that should be used for positioning. + * + * @param id Id of a placeholder + */ + void setPlaceHolderId(String id); + /** * Sets the signer to use * diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/SignParameterImpl.java b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/SignParameterImpl.java index d2786f53..2dc047f1 100644 --- a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/SignParameterImpl.java +++ b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/SignParameterImpl.java @@ -25,9 +25,7 @@ package at.gv.egiz.pdfas.lib.impl; import java.io.OutputStream; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; -import java.util.Map; import javax.activation.DataSource; @@ -35,10 +33,17 @@ import at.gv.egiz.pdfas.lib.api.Configuration; import at.gv.egiz.pdfas.lib.api.sign.IPlainSigner; import at.gv.egiz.pdfas.lib.api.sign.SignParameter; import at.gv.egiz.sl.util.BKUHeader; +import lombok.Getter; +import lombok.Setter; public class SignParameterImpl extends PdfAsParameterImpl implements SignParameter, BKUHeaderHolder { protected String signatureProfileId = null; protected String signaturePosition = null; + + @Getter + @Setter + protected String placeHolderId; + protected DataSource output = null; protected IPlainSigner signer = null; protected OutputStream outputStream = null; diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/configuration/ConfigurationImpl.java b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/configuration/ConfigurationImpl.java index bfc05c85..fd47bac5 100644 --- a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/configuration/ConfigurationImpl.java +++ b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/configuration/ConfigurationImpl.java @@ -3,19 +3,19 @@ * PDF-AS has been contracted by the E-Government Innovation Center EGIZ, a * joint initiative of the Federal Chancellery Austria and Graz University of * Technology. - * + * * Licensed under the EUPL, Version 1.1 or - as soon they will be approved by * the European Commission - subsequent versions of the EUPL (the "Licence"); * You may not use this work except in compliance with the Licence. * You may obtain a copy of the Licence at: * http://www.osor.eu/eupl/ - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the Licence is distributed on an "AS IS" basis, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the Licence for the specific language governing permissions and * limitations under the Licence. - * + * * This product combines work with different licenses. See the "NOTICE" text * file for details on the various modules and licenses. * The "NOTICE" text file is part of the distribution. Any derivative works @@ -23,7 +23,6 @@ ******************************************************************************/ package at.gv.egiz.pdfas.lib.impl.configuration; -import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Properties; @@ -35,135 +34,135 @@ import at.gv.egiz.pdfas.lib.settings.Settings; public class ConfigurationImpl implements ISettings, Configuration { - protected Properties overwrittenProperties = new Properties(); - - protected ISettings settings; - - public ConfigurationImpl(ISettings settings) { - this.settings = settings; - } - - public void setValue(String key, String value) { - overwrittenProperties.setProperty(key, value); - } - - public String getValue(String key) { - if(overwrittenProperties.containsKey(key)) { - return overwrittenProperties.getProperty(key); - } else { - return this.settings.getValue(key); - } - } - - public boolean hasValue(String key) { - if(overwrittenProperties.containsKey(key)) { - return true; - } else { - return this.settings.hasValue(key); - } - } - - public Map getValuesPrefix(String prefix) { - - Map valueMap = null; - valueMap = this.settings.getValuesPrefix(prefix); - if(valueMap == null) { - valueMap = new HashMap(); - } - - Iterator keyIterator = overwrittenProperties.keySet().iterator(); - - while(keyIterator.hasNext()) { - String key = keyIterator.next().toString(); - - if(key.startsWith(prefix)) { - valueMap.put(key, overwrittenProperties.getProperty(key)); - } + protected Properties overwrittenProperties = new Properties(); + + protected ISettings settings; + + public ConfigurationImpl(ISettings settings) { + this.settings = settings; + } + + @Override + public void setValue(String key, String value) { + overwrittenProperties.setProperty(key, value); + } + + @Override + public String getValue(String key) { + if (overwrittenProperties.containsKey(key)) { + return overwrittenProperties.getProperty(key); + } else { + return this.settings.getValue(key); + } + } + + @Override + public boolean hasValue(String key) { + if (overwrittenProperties.containsKey(key)) { + return true; + } else { + return this.settings.hasValue(key); + } + } + + @Override + public Map getValuesPrefix(String prefix) { + final Map valueMap = this.settings.getValuesPrefix(prefix); + final Iterator keyIterator = overwrittenProperties.keySet().iterator(); + + while (keyIterator.hasNext()) { + final String key = keyIterator.next().toString(); + + if (key.startsWith(prefix)) { + valueMap.put(key, overwrittenProperties.getProperty(key)); + } + } + + if (valueMap.isEmpty()) { + return null; + } + + return valueMap; + } + + @Override + public Vector getFirstLevelKeys(String prefix) { + + Vector valueMap = this.settings.getFirstLevelKeys(prefix); + if (valueMap == null) { + valueMap = new Vector<>(); + } + + final String mPrefix = prefix.endsWith(".") ? prefix : prefix + "."; + final Iterator keyIterator = overwrittenProperties.keySet().iterator(); + + while (keyIterator.hasNext()) { + final String key = keyIterator.next().toString(); + + if (key.startsWith(prefix)) { + final int keyIdx = key.indexOf('.', mPrefix.length()) > 0 ? key.indexOf('.', mPrefix.length()) + : key.length(); + final String firstLevels = key.substring(0, keyIdx); + if (!valueMap.contains(firstLevels)) { + valueMap.add(firstLevels); } + } + } + + if (valueMap.isEmpty()) { + return null; + } + + return valueMap; + } + + @Override + public boolean hasPrefix(String prefix) { + + if (this.settings.hasPrefix(prefix)) { + return true; + } + + final Iterator keyIterator = overwrittenProperties.keySet().iterator(); + while (keyIterator.hasNext()) { + final String key = keyIterator.next().toString(); + + if (key.startsWith(prefix)) { + return true; + } + } + return false; + } + + @Override + public String getWorkingDirectory() { + return this.settings.getWorkingDirectory(); + } + + @Override + public void cloneProfile(String originalPrefix, String clonedPrefix) { + final Map source = getValuesPrefix(originalPrefix); + + for (final String origKey : source.keySet()) { + final String cloneKey = origKey.replace(originalPrefix, clonedPrefix); + this.overwrittenProperties.setProperty(cloneKey, source.get(origKey)); + } + } + + @Override + public void removeProfile(String configurationPrefix) { + final Iterator keyIterator = overwrittenProperties.keySet().iterator(); + while (keyIterator.hasNext()) { + final String key = keyIterator.next().toString(); + + if (key.startsWith(configurationPrefix)) { + overwrittenProperties.remove(key); + } + } + } + + public void debugDumpProfileSettings(String profileName) { + ((Settings) settings).debugDumpProfileSettings(profileName); + } - if(valueMap.isEmpty()) { - return null; - } - - return valueMap; - } - - public Vector getFirstLevelKeys(String prefix) { - - Vector valueMap = this.settings.getFirstLevelKeys(prefix); - if(valueMap == null) { - valueMap = new Vector(); - } - - - String mPrefix = prefix.endsWith(".")?prefix:prefix+"."; - Iterator keyIterator = overwrittenProperties.keySet().iterator(); - - while(keyIterator.hasNext()) { - String key = keyIterator.next().toString(); - - if(key.startsWith(prefix)) { - int keyIdx = key.indexOf('.', mPrefix.length()) > 0 ? key.indexOf('.', mPrefix.length()) : key.length(); - String firstLevels = key.substring(0, keyIdx); - if(!valueMap.contains(firstLevels)) { - valueMap.add(firstLevels); - } - } - } - - if(valueMap.isEmpty()) { - return null; - } - - return valueMap; - } - - public boolean hasPrefix(String prefix) { - - if(this.settings.hasPrefix(prefix)) { - return true; - } - - Iterator keyIterator = overwrittenProperties.keySet().iterator(); - while(keyIterator.hasNext()) { - String key = keyIterator.next().toString(); - - if(key.startsWith(prefix)) { - return true; - } - } - return false; - } - - public String getWorkingDirectory() { - return this.settings.getWorkingDirectory(); - } - - public void cloneProfile(String originalPrefix, String clonedPrefix) { - Map source = getValuesPrefix(originalPrefix); - - Iterator keyIt = source.keySet().iterator(); - - while(keyIt.hasNext()) { - String origKey = keyIt.next(); - String cloneKey = origKey.replace(originalPrefix, clonedPrefix); - this.overwrittenProperties.setProperty(cloneKey, source.get(origKey)); - } - } - - public void removeProfile(String configurationPrefix) { - Iterator keyIterator = overwrittenProperties.keySet().iterator(); - while(keyIterator.hasNext()) { - String key = keyIterator.next().toString(); - - if(key.startsWith(configurationPrefix)) { - overwrittenProperties.remove(key); - } - } - } - - public void debugDumpProfileSettings(String profileName) { - ((Settings)settings).debugDumpProfileSettings(profileName); - } - } diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/configuration/PlaceholderConfiguration.java b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/configuration/PlaceholderConfiguration.java index 326ed142..a3719168 100644 --- a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/configuration/PlaceholderConfiguration.java +++ b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/configuration/PlaceholderConfiguration.java @@ -3,19 +3,19 @@ * PDF-AS has been contracted by the E-Government Innovation Center EGIZ, a * joint initiative of the Federal Chancellery Austria and Graz University of * Technology. - * + * * Licensed under the EUPL, Version 1.1 or - as soon they will be approved by * the European Commission - subsequent versions of the EUPL (the "Licence"); * You may not use this work except in compliance with the Licence. * You may obtain a copy of the Licence at: * http://www.osor.eu/eupl/ - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the Licence is distributed on an "AS IS" basis, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the Licence for the specific language governing permissions and * limitations under the Licence. - * + * * This product combines work with different licenses. See the "NOTICE" text * file for details on the various modules and licenses. * The "NOTICE" text file is part of the distribution. Any derivative works @@ -25,60 +25,57 @@ package at.gv.egiz.pdfas.lib.impl.configuration; import at.gv.egiz.pdfas.common.settings.ISettings; import at.gv.egiz.pdfas.lib.api.IConfigurationConstants; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import lombok.extern.slf4j.Slf4j; -import java.util.Map; +@Slf4j +public class PlaceholderConfiguration extends SpecificBaseConfiguration + implements IConfigurationConstants { + public PlaceholderConfiguration(ISettings configuration) { + super(configuration); + } -public class PlaceholderConfiguration extends SpecificBaseConfiguration - implements IConfigurationConstants { + public boolean isGlobalPlaceholderEnabled() { + if (configuration.hasValue(PLACEHOLDER_SEARCH_ENABLED)) { + final String value = configuration.getValue(PLACEHOLDER_SEARCH_ENABLED); + if (value.equalsIgnoreCase(TRUE)) { + return true; + } + } + return false; + } - private static final Logger logger = LoggerFactory.getLogger(PlaceholderConfiguration.class); + /** + * Match selected Profile for Placeholder Enables to activate placeholder + * search/match for different profiles + * + * @return + */ + public boolean isProfileConfigurationEnabled(String profileID) { + log.trace("Check if placeHolders are enabled for profile: {}", profileID); + + final String profileMatch = SIG_OBJECT + SEPERATOR + profileID + SEPERATOR + + PLACEHOLDER_SEARCH_ENABLED; + final String value = configuration.getValue(profileMatch); + if (TRUE.equalsIgnoreCase(value)) { + log.debug("Placeholders enabled for profile: {} ", profileID); + return true; + + } + return false; + + } - public PlaceholderConfiguration(ISettings configuration) { - super(configuration); - } + /** + * Get placeholderId for a specific profile. + * + * @param selectedProfileID ProfileName + * @return Placeholder Id + */ + public String getProfilePlaceholderID(String selectedProfileID) { + log.info("SelectedProfileID in ProfileConfEnabled: " + selectedProfileID); + final String profileMatch = SIG_OBJECT + SEPERATOR + selectedProfileID + SEPERATOR + PLACEHOLDER_ID; + return configuration.getValue(profileMatch); - public boolean isGlobalPlaceholderEnabled() { - if (configuration.hasValue(PLACEHOLDER_SEARCH_ENABLED)) { - String value = configuration.getValue(PLACEHOLDER_SEARCH_ENABLED); - if (value.equalsIgnoreCase(TRUE)) { - return true; - } - } - return false; - } - - /** - * Match selected Profile for Placeholder - * Enables to activate placeholder search/match for different profiles - * @return - */ - public boolean isProfileConfigurationEnabled(String selectedProfileID) - { - logger.info("SelectedProfileID in ProfileConfEnabled: "+selectedProfileID); - String profileMatch = SIG_OBJECT+SEPERATOR+selectedProfileID+SEPERATOR+PLACEHOLDER_SEARCH_ENABLED; - if (configuration.getValuesPrefix(profileMatch)!=null) { - Map map = configuration.getValuesPrefix(profileMatch); - String value = map.get(profileMatch); - if (value.equalsIgnoreCase(TRUE)) { - logger.info("Configuration has Value: "+value); - return true; - } - } - return false; - } - - public String getProfilePlaceholderID(String selectedProfileID) - { - logger.info("SelectedProfileID in ProfileConfEnabled: "+selectedProfileID); - String profileMatch = SIG_OBJECT+SEPERATOR+selectedProfileID+SEPERATOR+PLACEHOLDER_ID; - if (configuration.getValuesPrefix(profileMatch)!=null) { - Map map = configuration.getValuesPrefix(profileMatch); - return map.get(profileMatch); - } - return null; - } + } } - diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/configuration/PlaceholderWebConfiguration.java b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/configuration/PlaceholderWebConfiguration.java deleted file mode 100644 index 3a78f24f..00000000 --- a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/configuration/PlaceholderWebConfiguration.java +++ /dev/null @@ -1,24 +0,0 @@ -package at.gv.egiz.pdfas.lib.impl.configuration; - -import java.util.Properties; - -public class PlaceholderWebConfiguration { - - protected static Properties properties = new Properties(); - - //todo properties not cleaned - public static void setValue(String key, String value) - { - properties.clear(); - properties.setProperty(key,value); - } - public static String getValue(String key) - { - return properties.getProperty(key); - } - - public static void clear () { - properties.clear(); - } - -} diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/placeholder/PlaceholderExtractor.java b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/placeholder/PlaceholderExtractor.java index 0a55b834..436024cd 100644 --- a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/placeholder/PlaceholderExtractor.java +++ b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/placeholder/PlaceholderExtractor.java @@ -3,10 +3,7 @@ package at.gv.egiz.pdfas.lib.impl.placeholder; import at.gv.egiz.pdfas.common.exceptions.PdfAsException; import at.gv.egiz.pdfas.lib.impl.status.PDFObject; -import java.util.List; - public interface PlaceholderExtractor { - SignaturePlaceholderData extract(PDFObject doc, String placeholderId, int matchMode) throws PdfAsException; - - List extractList(PDFObject pdfObject, String placeholderID, int placeholderMode) throws PdfAsException; + SignaturePlaceholderData extract(PDFObject pdfObject, String placeholderID, int placeholderMode) throws PdfAsException; + } diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/placeholder/PlaceholderFilter.java b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/placeholder/PlaceholderFilter.java index 99c09295..0d652b76 100644 --- a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/placeholder/PlaceholderFilter.java +++ b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/placeholder/PlaceholderFilter.java @@ -24,145 +24,69 @@ package at.gv.egiz.pdfas.lib.impl.placeholder; import java.io.IOException; -import java.util.List; + +import org.apache.commons.lang3.StringUtils; import at.gv.egiz.pdfas.common.exceptions.PDFASError; import at.gv.egiz.pdfas.common.exceptions.PdfAsErrorCarrier; import at.gv.egiz.pdfas.common.exceptions.PdfAsException; import at.gv.egiz.pdfas.common.settings.ISettings; import at.gv.egiz.pdfas.lib.api.IConfigurationConstants; -import at.gv.egiz.pdfas.lib.impl.configuration.PlaceholderWebConfiguration; import at.gv.egiz.pdfas.lib.impl.status.OperationStatus; -import org.apache.commons.lang3.StringUtils; public class PlaceholderFilter implements IConfigurationConstants, PlaceholderExtractorConstants { - + public static SignaturePlaceholderData checkPlaceholderSignatureLocation( - OperationStatus status, ISettings settings, String signatureLocation) throws PdfAsException, - IOException { - - String placeholderID; - - if (status.getPlaceholderConfiguration().isGlobalPlaceholderEnabled()) { - PlaceholderExtractor extractor = status.getBackend().getPlaceholderExtractor(); - - if(StringUtils.isNotEmpty(signatureLocation)) { - placeholderID = signatureLocation; - } else { - placeholderID = PlaceholderWebConfiguration.getValue(PLACEHOLDER_WEB_ID); - if(StringUtils.isEmpty(placeholderID)) { - placeholderID = settings.getValue(PLACEHOLDER_ID); - } - } - - String placeholderModeString = settings.getValue(PLACEHOLDER_MODE); - int placeholderMode = PLACEHOLDER_MATCH_MODE_MODERATE; - if (StringUtils.isNotEmpty(placeholderModeString)) { - try { - placeholderMode = Integer.parseInt(placeholderModeString); - if (placeholderMode < PLACEHOLDER_MODE_MIN - || placeholderMode > PLACEHOLDER_MODE_MAX) { - throw new PdfAsErrorCarrier(new PDFASError( - PDFASError.ERROR_INVALID_PLACEHOLDER_MODE)); - } - } catch (NumberFormatException e) { - throw new PdfAsErrorCarrier(new PDFASError( - PDFASError.ERROR_INVALID_PLACEHOLDER_MODE, e)); - } - } - SignaturePlaceholderData signaturePlaceholderData = extractor.extract(status.getPdfObject(), placeholderID, placeholderMode); - return signaturePlaceholderData; - - } else if (status.getPlaceholderConfiguration().isProfileConfigurationEnabled(status.getRequestedSignature().getSignatureProfileID())) { - //filter for local placeholder in selected profiles - PlaceholderExtractor extractor = status.getBackend().getPlaceholderExtractor(); - int placeholderMode = PLACEHOLDER_MATCH_MODE_SORTED; - - placeholderID = status.getPlaceholderConfiguration().getProfilePlaceholderID(status.getRequestedSignature().getSignatureProfileID()); - if(StringUtils.isNotEmpty(placeholderID)) { - placeholderMode = PLACEHOLDER_MATCH_MODE_MODERATE; - } - String placeholderModeString = settings.getValue(PLACEHOLDER_MODE); - if (StringUtils.isNotEmpty(placeholderModeString)) { - try { - placeholderMode = Integer.parseInt(placeholderModeString); - if (placeholderMode < PLACEHOLDER_MODE_MIN - || placeholderMode > PLACEHOLDER_MODE_MAX) { - throw new PdfAsErrorCarrier(new PDFASError( - PDFASError.ERROR_INVALID_PLACEHOLDER_MODE)); - } - } catch (NumberFormatException e) { - throw new PdfAsErrorCarrier(new PDFASError( - PDFASError.ERROR_INVALID_PLACEHOLDER_MODE, e)); - } - } - SignaturePlaceholderData signaturePlaceholderData = extractor.extract(status.getPdfObject(), placeholderID, placeholderMode); - return signaturePlaceholderData; + OperationStatus status, ISettings settings, String placeholderId) throws PdfAsException, IOException { + + String signingProfile = status.getRequestedSignature().getSignatureProfileID(); + + if (status.getPlaceholderConfiguration().isGlobalPlaceholderEnabled()) { + String defaultPlaceHolderId = settings.getValue(PLACEHOLDER_ID); + return status.getBackend().getPlaceholderExtractor().extract( + status.getPdfObject(), getPlaceHolderId(placeholderId, defaultPlaceHolderId) , + getPlaceHolderMode(settings, PLACEHOLDER_MATCH_MODE_SORTED)); + + } else if (status.getPlaceholderConfiguration().isProfileConfigurationEnabled(signingProfile)) { + String defaultPlaceHolderId = status.getPlaceholderConfiguration().getProfilePlaceholderID(signingProfile); + return status.getBackend().getPlaceholderExtractor().extract( + status.getPdfObject(), getPlaceHolderId(placeholderId, defaultPlaceHolderId), + getPlaceHolderMode(settings, + StringUtils.isNotEmpty(defaultPlaceHolderId) ? PLACEHOLDER_MATCH_MODE_MODERATE : PLACEHOLDER_MATCH_MODE_SORTED)); } return null; } - - public static List checkPlaceholderSignatureLocationList(OperationStatus status, ISettings settings, String signatureLocation) throws PdfAsException, - IOException { - String placeholderID; - - if (status.getPlaceholderConfiguration().isGlobalPlaceholderEnabled()) { - PlaceholderExtractor extractor = status.getBackend().getPlaceholderExtractor(); - - if(StringUtils.isNotEmpty(signatureLocation)) { - placeholderID = signatureLocation; - } else { - placeholderID = PlaceholderWebConfiguration.getValue(PLACEHOLDER_WEB_ID); - if(StringUtils.isEmpty(placeholderID)) { - placeholderID = settings.getValue(PLACEHOLDER_ID); - } - } - - String placeholderModeString = settings.getValue(PLACEHOLDER_MODE); - int placeholderMode = PLACEHOLDER_MATCH_MODE_MODERATE; - if (StringUtils.isNotEmpty(placeholderModeString)) { - try { - placeholderMode = Integer.parseInt(placeholderModeString); - if (placeholderMode < PLACEHOLDER_MODE_MIN - || placeholderMode > PLACEHOLDER_MODE_MAX) { - throw new PdfAsErrorCarrier(new PDFASError( - PDFASError.ERROR_INVALID_PLACEHOLDER_MODE)); - } - } catch (NumberFormatException e) { - throw new PdfAsErrorCarrier(new PDFASError( - PDFASError.ERROR_INVALID_PLACEHOLDER_MODE, e)); - } - } - return extractor.extractList(status.getPdfObject(), placeholderID, - placeholderMode); - - } else if (status.getPlaceholderConfiguration().isProfileConfigurationEnabled(status.getRequestedSignature().getSignatureProfileID())) { - //filter for local placeholder in selected profiles - PlaceholderExtractor extractor = status.getBackend().getPlaceholderExtractor(); - int placeholderMode = PLACEHOLDER_MATCH_MODE_SORTED; - - placeholderID = status.getPlaceholderConfiguration().getProfilePlaceholderID(status.getRequestedSignature().getSignatureProfileID()); - if(StringUtils.isNotEmpty(placeholderID)) { - placeholderMode = PLACEHOLDER_MATCH_MODE_MODERATE; - } - String placeholderModeString = settings.getValue(PLACEHOLDER_MODE); - if (StringUtils.isNotEmpty(placeholderModeString)) { - try { - placeholderMode = Integer.parseInt(placeholderModeString); - if (placeholderMode < PLACEHOLDER_MODE_MIN - || placeholderMode > PLACEHOLDER_MODE_MAX) { - throw new PdfAsErrorCarrier(new PDFASError( - PDFASError.ERROR_INVALID_PLACEHOLDER_MODE)); - } - } catch (NumberFormatException e) { - throw new PdfAsErrorCarrier(new PDFASError( - PDFASError.ERROR_INVALID_PLACEHOLDER_MODE, e)); - } - } - return extractor.extractList(status.getPdfObject(), placeholderID, - placeholderMode); - } - return null; + + private static int getPlaceHolderMode(ISettings settings, int defaultValue) throws PdfAsErrorCarrier { + String placeholderModeString = settings.getValue(PLACEHOLDER_MODE); + if (StringUtils.isNotEmpty(placeholderModeString)) { + try { + int placeholderMode = Integer.parseInt(placeholderModeString); + if (placeholderMode < PLACEHOLDER_MODE_MIN || placeholderMode > PLACEHOLDER_MODE_MAX) { + throw new PdfAsErrorCarrier(new PDFASError(PDFASError.ERROR_INVALID_PLACEHOLDER_MODE)); + + } + return placeholderMode; + + } catch (NumberFormatException e) { + throw new PdfAsErrorCarrier(new PDFASError( + PDFASError.ERROR_INVALID_PLACEHOLDER_MODE, e)); + } + + } else { + return defaultValue; + + } } + + private static String getPlaceHolderId(String requestId, String defaultValue) { + if (StringUtils.isEmpty(requestId)) { + return defaultValue; + + } else { + return requestId; + + } + } } \ No newline at end of file diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/placeholder/SignaturePlaceholderContext.java b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/placeholder/SignaturePlaceholderContext.java deleted file mode 100644 index 3c8a6d76..00000000 --- a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/placeholder/SignaturePlaceholderContext.java +++ /dev/null @@ -1,95 +0,0 @@ -/******************************************************************************* - * Copyright 2014 by E-Government Innovation Center EGIZ, Graz, Austria - * PDF-AS has been contracted by the E-Government Innovation Center EGIZ, a - * joint initiative of the Federal Chancellery Austria and Graz University of - * Technology. - * - * Licensed under the EUPL, Version 1.1 or - as soon they will be approved by - * the European Commission - subsequent versions of the EUPL (the "Licence"); - * You may not use this work except in compliance with the Licence. - * You may obtain a copy of the Licence at: - * http://www.osor.eu/eupl/ - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the Licence is distributed on an "AS IS" basis, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the Licence for the specific language governing permissions and - * limitations under the Licence. - * - * This product combines work with different licenses. See the "NOTICE" text - * file for details on the various modules and licenses. - * The "NOTICE" text file is part of the distribution. Any derivative works - * that you distribute must include a readable copy of the "NOTICE" text file. - ******************************************************************************/ -/** - * Copyright 2006 by Know-Center, Graz, Austria - * PDF-AS has been contracted by the E-Government Innovation Center EGIZ, a - * joint initiative of the Federal Chancellery Austria and Graz University of - * Technology. - * - * Licensed under the EUPL, Version 1.1 or - as soon they will be approved by - * the European Commission - subsequent versions of the EUPL (the "Licence"); - * You may not use this work except in compliance with the Licence. - * You may obtain a copy of the Licence at: - * http://www.osor.eu/eupl/ - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the Licence is distributed on an "AS IS" basis, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the Licence for the specific language governing permissions and - * limitations under the Licence. - * - * This product combines work with different licenses. See the "NOTICE" text - * file for details on the various modules and licenses. - * The "NOTICE" text file is part of the distribution. Any derivative works - * that you distribute must include a readable copy of the "NOTICE" text file. - */ -package at.gv.egiz.pdfas.lib.impl.placeholder; - -/** - * Store and retrieve {@link SignaturePlaceholderData} in/from a thread local context. - * - * @author exthex - * - */ -public class SignaturePlaceholderContext { - - private ThreadLocal sigHolder = new ThreadLocal(); - - private static SignaturePlaceholderContext instance = new SignaturePlaceholderContext(); - - /** - * Constructor. Private because this is a singleton. - */ - private SignaturePlaceholderContext() { - - } - - /** - * Get the {@link SignaturePlaceholderData} which is currently bound to this thread. - * Might be null. - * - * @return - */ - public static SignaturePlaceholderData getSignaturePlaceholderData(){ - return instance.sigHolder.get(); - } - - /** - * - * @return true if there is currently a {@link SignaturePlaceholderData} bound to this thread, false otherwise. - */ - public static boolean isSignaturePlaceholderDataSet() { - return instance.sigHolder.get() != null; - } - - /** - * Bind a {@link SignaturePlaceholderData} to this thread. - * If the given data is null, the context will be cleared. - * - * @param data if null, clears the ThreadLocal, else binds the data to the current thread. - */ - public static void setSignaturePlaceholderData(SignaturePlaceholderData data) { - instance.sigHolder.set(data); - } -} diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/settings/Settings.java b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/settings/Settings.java index c5b6dc57..0b376455 100644 --- a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/settings/Settings.java +++ b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/settings/Settings.java @@ -35,438 +35,364 @@ import java.util.Map; import java.util.Map.Entry; import java.util.Properties; import java.util.Vector; +import java.util.stream.Collectors; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOCase; import org.apache.commons.io.filefilter.WildcardFileFilter; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import at.gv.egiz.pdfas.common.exceptions.PdfAsSettingsException; import at.gv.egiz.pdfas.common.settings.IProfileConstants; import at.gv.egiz.pdfas.common.settings.ISettings; import at.gv.egiz.pdfas.common.settings.Profiles; +import lombok.extern.slf4j.Slf4j; + +@Slf4j +public class Settings implements ISettings, IProfileConstants { + protected Properties properties = new Properties(); + protected File workDirectory; + + public Settings(File workDirectory) { + try { + this.workDirectory = workDirectory; + loadSettings(workDirectory); + } catch (final PdfAsSettingsException e) { + log.error(e.getMessage(), e); + } + } -public class Settings implements ISettings, IProfileConstants { + private void loadSettingsRecursive(File workDirectory, File file) + throws RuntimeException { + try { + final String configDir = workDirectory.getAbsolutePath() + File.separator + + CFG_DIR; + final Properties tmpProps = new Properties(); + log.debug("Loading: " + file.getName()); + tmpProps.load(new FileInputStream(file)); - private static final Logger logger = LoggerFactory - .getLogger(Settings.class); + properties.putAll(tmpProps); - protected Properties properties = new Properties(); + this.getValuesPrefix(INCLUDE, tmpProps).values() + .forEach(el -> loadAdditionConfigFile(el, configDir)); - protected File workDirectory; + } catch (final IOException e) { + throw new RuntimeException(e); - public Settings(File workDirectory) { - try { - this.workDirectory = workDirectory; - loadSettings(workDirectory); - } catch (PdfAsSettingsException e) { - logger.error(e.getMessage(), e); - } } + } - private void loadSettingsRecursive(File workDirectory, File file) - throws PdfAsSettingsException { - try { - String configDir = workDirectory.getAbsolutePath() + File.separator - + CFG_DIR; - Properties tmpProps = new Properties(); - logger.debug("Loading: " + file.getName()); - tmpProps.load(new FileInputStream(file)); - - properties.putAll(tmpProps); - - Map includes = this.getValuesPrefix(INCLUDE, - tmpProps); - File contextFolder = new File(configDir); - if (includes != null) { - Iterator includeIterator = includes.values().iterator(); - while (includeIterator.hasNext()) { - contextFolder = new File(configDir); - String includeFileName = includeIterator.next(); - - File includeInstruction = new File(contextFolder, - includeFileName); - contextFolder = includeInstruction.getParentFile(); - String includeName = includeInstruction.getName(); - - WildcardFileFilter fileFilter = new WildcardFileFilter( - includeName, IOCase.SENSITIVE); - Collection includeFiles = null; - - if (contextFolder != null && contextFolder.exists() - && contextFolder.isDirectory()) { - includeFiles = FileUtils.listFiles(contextFolder, - fileFilter, null); - } - if (includeFiles != null && !includeFiles.isEmpty()) { - logger.debug("Including '" + includeFileName + "'."); - for (File includeFile : includeFiles) { - loadSettingsRecursive(workDirectory, includeFile); - } - } - } - } - - } catch (IOException e) { - throw new PdfAsSettingsException("Failed to read settings!", e); - } - } + private void loadAdditionConfigFile(String includeFileName, String configDir) { + File contextFolder = new File(configDir); - private void showAugments(Profiles profiles) { - if (!profiles.getAugments().isEmpty()) { - logger.debug("\tAugments for {}", profiles.getName()); - for (int i = 0; i < profiles.getAugments().size(); i++) { - logger.debug("\t\t{}", profiles.getAugments().get(i).getName()); - } - } + final File includeInstruction = new File(contextFolder, includeFileName); + contextFolder = includeInstruction.getParentFile(); + final String includeName = includeInstruction.getName(); + + final WildcardFileFilter fileFilter = new WildcardFileFilter( + includeName, IOCase.SENSITIVE); + Collection includeFiles = null; + + if (contextFolder != null && contextFolder.exists() + && contextFolder.isDirectory()) { + includeFiles = FileUtils.listFiles(contextFolder, + fileFilter, null); + } + if (includeFiles != null && !includeFiles.isEmpty()) { + log.debug("Including '" + includeFileName + "'."); + for (final File includeFile : includeFiles) { + loadSettingsRecursive(workDirectory, includeFile); + } } - private boolean isAugmentsReady(Profiles profiles) { - Iterator augmentingProfiles = profiles.getAugments().iterator(); - boolean allInitialized = true; - while (augmentingProfiles.hasNext()) { - if (!augmentingProfiles.next().isInitialized()) { - allInitialized = false; - } + } + + private void showAugments(Profiles profiles) { + if (!profiles.getAugments().isEmpty()) { + log.debug("\tAugments for {}", profiles.getName()); + for (final Profiles element : profiles.getAugments()) { + log.debug("\t\t{}", element.getName()); + } + } + } + + private boolean isAugmentsReady(Profiles profiles) { + boolean allInitialized = true; + for (final Profiles element : profiles.getAugments()) { + if (!element.isInitialized()) { + allInitialized = false; + } + } + return allInitialized; + } + + private boolean isParentReady(Profiles profiles) { + if (profiles.getParent() != null) { + return profiles.getParent().isInitialized(); + } else { + return false; + } + } + + private void performAugmentConfiguration(Profiles profiles) { + final String childBase = "sig_obj." + + profiles.getName(); + + for (final Profiles augmentingProfile : profiles.getAugments()) { + final String augmentingBase = "sig_obj." + augmentingProfile.getName(); + + for (final String key : this.getKeys( + augmentingBase + ".")) { + final String keyToCopy = key.substring(augmentingBase + .length()); + // log.debug("Profile: {} => {}", + // key, childBase+keyToCopy); + final String sourceKey = augmentingBase + keyToCopy; + final String targetKey = childBase + keyToCopy; + + if (!this.hasValue(targetKey)) { + properties.setProperty(targetKey, + this.getValue(sourceKey)); + // log.debug("Replaced: {} with Value from {}", + // childBase+keyToCopy, parentBase+keyToCopy); + } else { + // log.debug("NOT Replaced: {} with Value from {}", + // childBase+keyToCopy, parentBase+keyToCopy); } - return allInitialized; + } } - - private boolean isParentReady(Profiles profiles) { - if (profiles.getParent() != null) { - return profiles.getParent().isInitialized(); + } + + private void performParentConfiguration(Profiles profiles) { + if (profiles.getParent() != null) { + // If Parent is initialized Copy Properties from Parent + // to this profile + final String parentBase = "sig_obj." + profiles.getParent().getName(); + final String childBase = "sig_obj." + + profiles.getName(); + + for (final String key : this.getKeys( + parentBase + ".")) { + final String keyToCopy = key.substring(parentBase + .length()); + // log.debug("Profile: {} => {}", + // key, childBase+keyToCopy); + final String sourceKey = parentBase + keyToCopy; + final String targetKey = childBase + keyToCopy; + + if (!this.hasValue(targetKey)) { + properties.setProperty(targetKey, + this.getValue(sourceKey)); + // log.debug("Replaced: {} with Value from {}", + // childBase+keyToCopy, parentBase+keyToCopy); } else { - return false; + // log.debug("NOT Replaced: {} with Value from {}", + // childBase+keyToCopy, parentBase+keyToCopy); } + } + } + } + + private void buildProfiles() { + final Map profiles = new HashMap<>(); + + for (final String key : this.getFirstLevelKeys("sig_obj.types.")) { + final String profile = key.substring("sig_obj.types.".length()); + // System.out.println("[" + profile + "]: " + this.getValue(key)); + if (this.getValue(key).equals("on")) { + final Profiles prof = new Profiles(profile); + profiles.put(profile, prof); + } } - private void performAugmentConfiguration(Profiles profiles) { - Iterator augmentingProfiles = profiles.getAugments().iterator(); - - String childBase = "sig_obj." - + profiles.getName(); - - while (augmentingProfiles.hasNext()) { - Profiles augmentingProfile = augmentingProfiles.next(); - String augmentingBase = "sig_obj." + augmentingProfile.getName(); - - Iterator augmentingKeyIt = this.getKeys( - augmentingBase + ".").iterator(); - - while (augmentingKeyIt.hasNext()) { - String key = augmentingKeyIt.next(); - String keyToCopy = key.substring(augmentingBase - .length()); - //logger.debug("Profile: {} => {}", - // key, childBase+keyToCopy); - String sourceKey = augmentingBase + keyToCopy; - String targetKey = childBase + keyToCopy; - - if (!this.hasValue(targetKey)) { - properties.setProperty(targetKey, - this.getValue(sourceKey)); - //logger.debug("Replaced: {} with Value from {}", - // childBase+keyToCopy, parentBase+keyToCopy); - } else { - //logger.debug("NOT Replaced: {} with Value from {}", - // childBase+keyToCopy, parentBase+keyToCopy); - } - } - } + for (final Entry entry : profiles.entrySet()) { + entry.getValue().findParent(properties, profiles); } - private void performParentConfiguration(Profiles profiles) { - if (profiles.getParent() != null) { - // If Parent is initialized Copy Properties from Parent - // to this profile - String parentBase = "sig_obj." + profiles.getParent().getName(); - String childBase = "sig_obj." - + profiles.getName(); - - Iterator parentKeyIt = this.getKeys( - parentBase + ".").iterator(); - while (parentKeyIt.hasNext()) { - String key = parentKeyIt.next(); - String keyToCopy = key.substring(parentBase - .length()); - //logger.debug("Profile: {} => {}", - // key, childBase+keyToCopy); - String sourceKey = parentBase + keyToCopy; - String targetKey = childBase + keyToCopy; - - if (!this.hasValue(targetKey)) { - properties.setProperty(targetKey, - this.getValue(sourceKey)); - //logger.debug("Replaced: {} with Value from {}", - // childBase+keyToCopy, parentBase+keyToCopy); - } else { - //logger.debug("NOT Replaced: {} with Value from {}", - // childBase+keyToCopy, parentBase+keyToCopy); - } - } - } + for (final Entry entry : profiles.entrySet()) { + if (entry.getValue().getParent() == null) { + log.debug("Got Profile: [{}] : {}", entry.getKey(), entry.getValue().getName()); + showAugments(entry.getValue()); + } else { + log.debug("Got Profile: [{}] : {} (Parent {})", entry.getKey(), + entry.getValue().getName(), entry.getValue().getParent().getName()); + showAugments(entry.getValue()); + } } - private void buildProfiles() { - Map profiles = new HashMap(); - - Iterator itKeys = this.getFirstLevelKeys("sig_obj.types.") - .iterator(); - while (itKeys.hasNext()) { - String key = itKeys.next(); - String profile = key.substring("sig_obj.types.".length()); - //System.out.println("[" + profile + "]: " + this.getValue(key)); - if (this.getValue(key).equals("on")) { - Profiles prof = new Profiles(profile); - profiles.put(profile, prof); - } - } + log.debug("Configured Settings: {}", + properties.size()); + + // Resolve Parent Structures ... + while (!profiles.isEmpty()) { + final List removes = new ArrayList<>(); + for (final Entry entry : profiles.entrySet()) { + // Remove all base Profiles ... + if (entry.getValue().getParent() == null && entry.getValue().getAugments().isEmpty()) { + // Has neither parent or augmenting profiles + + entry.getValue().setInitialized(true); + removes.add(entry.getKey()); + } else if (entry.getValue().getParent() == null) { + // Has augmenting profiles but no parent + + // check if all augmenting profiles are initialized if so + // add them + + final Profiles profile = entry.getValue(); + if (this.isAugmentsReady(profile)) { + this.performAugmentConfiguration(profile); + // Copy done + entry.getValue().setInitialized(true); + removes.add(entry.getKey()); + } else { + log.debug("Not all augmenting profiles are ready yet for {}", entry.getValue().getName()); + } + } else if (entry.getValue().getAugments().isEmpty()) { + + // Has parent but no augmenting profiles + final Profiles profile = entry.getValue(); + + if (this.isParentReady(profile)) { + this.performParentConfiguration(profile); + // Copy done + entry.getValue().setInitialized(true); + removes.add(entry.getKey()); + } + } else { - // Initialize Parent Structure ... - Iterator> profileIterator = profiles.entrySet() - .iterator(); - while (profileIterator.hasNext()) { - Entry entry = profileIterator.next(); - entry.getValue().findParent(properties, profiles); - } + // Has parent and augmenting profiles - // Debug Output - Iterator> profileIteratorDbg = profiles.entrySet() - .iterator(); - while (profileIteratorDbg.hasNext()) { - Entry entry = profileIteratorDbg.next(); - if (entry.getValue().getParent() == null) { - logger.debug("Got Profile: [{}] : {}", entry.getKey(), entry.getValue().getName()); - showAugments(entry.getValue()); - } else { - logger.debug("Got Profile: [{}] : {} (Parent {})", entry.getKey(), - entry.getValue().getName(), entry.getValue().getParent().getName()); - showAugments(entry.getValue()); - } - } + final Profiles profile = entry.getValue(); + if (this.isAugmentsReady(profile) && this.isParentReady(profile)) { + // order is curcial, augments preceed over parent configuration + this.performAugmentConfiguration(profile); + this.performParentConfiguration(profile); - logger.debug("Configured Settings: {}", - properties.size()); - - // Resolve Parent Structures ... - while (!profiles.isEmpty()) { - List removes = new ArrayList(); - Iterator> profileIt = profiles.entrySet() - .iterator(); - while (profileIt.hasNext()) { - Entry entry = profileIt.next(); - - // Remove all base Profiles ... - if (entry.getValue().getParent() == null && entry.getValue().getAugments().isEmpty()) { - // Has neither parent or augmenting profiles - - entry.getValue().setInitialized(true); - removes.add(entry.getKey()); - } else if (entry.getValue().getParent() == null) { - // Has augmenting profiles but no parent - - // check if all augmenting profiles are initialized if so - // add them - - Profiles profile = entry.getValue(); - if (this.isAugmentsReady(profile)) { - this.performAugmentConfiguration(profile); - // Copy done - entry.getValue().setInitialized(true); - removes.add(entry.getKey()); - } else { - logger.debug("Not all augmenting profiles are ready yet for {}", entry.getValue().getName()); - } - } else if (entry.getValue().getAugments().isEmpty()) { - - // Has parent but no augmenting profiles - Profiles profile = entry.getValue(); - - if (this.isParentReady(profile)) { - this.performParentConfiguration(profile); - // Copy done - entry.getValue().setInitialized(true); - removes.add(entry.getKey()); - } - } else { - - // Has parent and augmenting profiles - - Profiles profile = entry.getValue(); - if (this.isAugmentsReady(profile) && this.isParentReady(profile)) { - // order is curcial, augments preceed over parent configuration - this.performAugmentConfiguration(profile); - this.performParentConfiguration(profile); - - // Copy done - entry.getValue().setInitialized(true); - removes.add(entry.getKey()); - } - } - } - - // Remove all Profiles from Remove List - - if (removes.isEmpty() && !profiles.isEmpty()) { - logger.error("Failed to build inheritant Profiles, running in infinite loop! (aborting ...)"); - logger.error("Profiles that cannot be resolved completly:"); - Iterator> failedProfiles = profiles.entrySet().iterator(); - while (failedProfiles.hasNext()) { - Entry entry = failedProfiles.next(); - logger.error("Problem Profile: [{}] : {}", entry.getKey(), entry.getValue().getName()); - } - return; - } - - Iterator removeIt = removes.iterator(); - while (removeIt.hasNext()) { - profiles.remove(removeIt.next()); - } + // Copy done + entry.getValue().setInitialized(true); + removes.add(entry.getKey()); + } } + } - logger.debug("Derived Settings: {}", - properties.size()); + // Remove all Profiles from Remove List - } - - public void debugDumpProfileSettings(String profileName) { - Iterator keysIterator = this.getKeys("sig_obj." + profileName + ".").iterator(); - - logger.debug("Configuration for {}", profileName); - while(keysIterator.hasNext()) { - String key = keysIterator.next(); - logger.debug(" {}: {}", key, this.getValue(key)); + if (removes.isEmpty() && !profiles.isEmpty()) { + log.error("Failed to build inheritant Profiles, running in infinite loop! (aborting ...)"); + log.error("Profiles that cannot be resolved completly:"); + for (final Entry entry : profiles.entrySet()) { + log.error("Problem Profile: [{}] : {}", entry.getKey(), entry.getValue().getName()); } - } - - public void loadSettings(File workDirectory) throws PdfAsSettingsException { - // try { - String configDir = workDirectory.getAbsolutePath() + File.separator - + CFG_DIR; - String configFile = configDir + File.separator + CFG_FILE; - loadSettingsRecursive(workDirectory, new File(configFile)); - buildProfiles(); - /* - * logger.debug("Loading cfg file: " + configFile); - * - * - * properties.load(new FileInputStream(configFile)); - * - * Map includes = this.getValuesPrefix(INCLUDE); File - * contextFolder = new File(configDir); if (includes != null) { - * Iterator includeIterator = includes.values().iterator(); - * while (includeIterator.hasNext()) { String includeFileName = - * includeIterator.next(); if (includeFileName.contains("*")) { - * WildcardFileFilter fileFilter = new WildcardFileFilter( - * includeFileName, IOCase.SENSITIVE); Collection includeFiles = - * null; - * - * if (contextFolder != null && contextFolder.exists() && - * contextFolder.isDirectory()) { includeFiles = - * FileUtils.listFiles(contextFolder, fileFilter, null); } if - * (includeFiles != null && !includeFiles.isEmpty()) { - * logger.info("Including '" + includeFileName + "'."); for (File - * includeFile : includeFiles) { properties .load(new - * FileInputStream(includeFile)); } } } else { String includeFile = - * configDir + File.separator + includeFileName; - * logger.debug("Loading included cfg file: " + includeFile); try { - * properties.load(new FileInputStream(includeFile)); } catch (Throwable - * e) { logger.error("Failed to load cfg file " + includeFile, e); } } } - * } - */ - // logger.debug("Configured Properties:"); - /* - * if(logger.isDebugEnabled()) { properties.list(System.out); } - */ - - // } catch (IOException e) { - // throw new PdfAsSettingsException("Failed to read settings!", e); - // } - } + return; + } - public String getValue(String key) { - return properties.getProperty(key); + for (final String element : removes) { + profiles.remove(element); + } } - public boolean hasValue(String key) { - return properties.containsKey(key); - } + log.debug("Derived Settings: {}", + properties.size()); - private Map getValuesPrefix(String prefix, Properties props) { - Iterator keyIterator = props.keySet().iterator(); - Map valueMap = new HashMap(); - while (keyIterator.hasNext()) { - String key = keyIterator.next().toString(); + } + public void debugDumpProfileSettings(String profileName) { + log.debug("Configuration for {}", profileName); + for (final String key : this.getKeys("sig_obj." + profileName + ".")) { + log.debug(" {}: {}", key, this.getValue(key)); + } + } - if (key.startsWith(prefix)) { - valueMap.put(key, props.getProperty(key)); - } - } + public void loadSettings(File workDirectory) throws PdfAsSettingsException { + try { + final String configDir = workDirectory.getAbsolutePath() + File.separator + + CFG_DIR; + final String configFile = configDir + File.separator + CFG_FILE; + loadSettingsRecursive(workDirectory, new File(configFile)); + buildProfiles(); - if (valueMap.isEmpty()) { - return null; - } + } catch (final RuntimeException e) { + throw new PdfAsSettingsException("Failed to read settings!", e); - return valueMap; } - - public Map getValuesPrefix(String prefix) { - return getValuesPrefix(prefix, properties); + } + + @Override + public String getValue(String key) { + return properties.getProperty(key); + } + + @Override + public boolean hasValue(String key) { + return properties.containsKey(key); + } + + private Map getValuesPrefix(String prefix, Properties props) { + return props.entrySet().stream() + .filter(el -> el.getKey().toString().startsWith(prefix)) + .collect(Collectors.toMap(key -> key.getKey().toString(), value -> value.getValue().toString())); + + } + + @Override + public Map getValuesPrefix(String prefix) { + return getValuesPrefix(prefix, properties); + } + + public Vector getKeys(String prefix) { + final Iterator keyIterator = properties.keySet().iterator(); + final Vector valueMap = new Vector<>(); + while (keyIterator.hasNext()) { + final String key = keyIterator.next().toString(); + + if (key.startsWith(prefix)) { + valueMap.add(key); + } } - - public Vector getKeys(String prefix) { - Iterator keyIterator = properties.keySet().iterator(); - Vector valueMap = new Vector(); - while (keyIterator.hasNext()) { - String key = keyIterator.next().toString(); - - if (key.startsWith(prefix)) { - valueMap.add(key); - } + return valueMap; + } + + @Override + public Vector getFirstLevelKeys(String prefix) { + final String mPrefix = prefix.endsWith(".") ? prefix : prefix + "."; + final Iterator keyIterator = properties.keySet().iterator(); + final Vector valueMap = new Vector<>(); + while (keyIterator.hasNext()) { + final String key = keyIterator.next().toString(); + + if (key.startsWith(prefix)) { + final int keyIdx = key.indexOf('.', mPrefix.length()) > 0 ? key + .indexOf('.', mPrefix.length()) : key.length(); + final String firstLevels = key.substring(0, keyIdx); + if (!valueMap.contains(firstLevels)) { + valueMap.add(firstLevels); } - return valueMap; + } } - public Vector getFirstLevelKeys(String prefix) { - String mPrefix = prefix.endsWith(".") ? prefix : prefix + "."; - Iterator keyIterator = properties.keySet().iterator(); - Vector valueMap = new Vector(); - while (keyIterator.hasNext()) { - String key = keyIterator.next().toString(); - - if (key.startsWith(prefix)) { - int keyIdx = key.indexOf('.', mPrefix.length()) > 0 ? key - .indexOf('.', mPrefix.length()) : key.length(); - String firstLevels = key.substring(0, keyIdx); - if (!valueMap.contains(firstLevels)) { - valueMap.add(firstLevels); - } - } - } - - if (valueMap.isEmpty()) { - return null; - } - - return valueMap; + if (valueMap.isEmpty()) { + return null; } - public boolean hasPrefix(String prefix) { - Iterator keyIterator = properties.keySet().iterator(); - while (keyIterator.hasNext()) { - String key = keyIterator.next().toString(); + return valueMap; + } - if (key.startsWith(prefix)) { - return true; - } - } - return false; - } + @Override + public boolean hasPrefix(String prefix) { + final Iterator keyIterator = properties.keySet().iterator(); + while (keyIterator.hasNext()) { + final String key = keyIterator.next().toString(); - public String getWorkingDirectory() { - return this.workDirectory.getAbsolutePath(); + if (key.startsWith(prefix)) { + return true; + } } + return false; + } + + @Override + public String getWorkingDirectory() { + return this.workDirectory.getAbsolutePath(); + } } diff --git a/pdf-as-pdfbox-2/src/main/java/at/gv/egiz/pdfas/lib/impl/pdfbox2/placeholder/PDFBoxPlaceholderExtractor.java b/pdf-as-pdfbox-2/src/main/java/at/gv/egiz/pdfas/lib/impl/pdfbox2/placeholder/PDFBoxPlaceholderExtractor.java index 63b006bf..ad874bc0 100644 --- a/pdf-as-pdfbox-2/src/main/java/at/gv/egiz/pdfas/lib/impl/pdfbox2/placeholder/PDFBoxPlaceholderExtractor.java +++ b/pdf-as-pdfbox-2/src/main/java/at/gv/egiz/pdfas/lib/impl/pdfbox2/placeholder/PDFBoxPlaceholderExtractor.java @@ -1,5 +1,7 @@ package at.gv.egiz.pdfas.lib.impl.pdfbox2.placeholder; +import java.io.IOException; + import at.gv.egiz.pdfas.common.exceptions.PDFIOException; import at.gv.egiz.pdfas.common.exceptions.PdfAsException; import at.gv.egiz.pdfas.lib.impl.pdfbox2.PDFBOXObject; @@ -7,9 +9,6 @@ import at.gv.egiz.pdfas.lib.impl.placeholder.PlaceholderExtractor; import at.gv.egiz.pdfas.lib.impl.placeholder.SignaturePlaceholderData; import at.gv.egiz.pdfas.lib.impl.status.PDFObject; -import java.io.IOException; -import java.util.List; - public class PDFBoxPlaceholderExtractor implements PlaceholderExtractor { @@ -18,27 +17,8 @@ public class PDFBoxPlaceholderExtractor implements PlaceholderExtractor { if (doc instanceof PDFBOXObject) { PDFBOXObject object = (PDFBOXObject) doc; try { - SignaturePlaceholderExtractor extractor = new SignaturePlaceholderExtractor(placeholderId, - matchMode, object.getDocument()); - return extractor.extract(object.getDocument(), - placeholderId, matchMode); - } catch (IOException | ClassNotFoundException | InstantiationException | IllegalAccessException e2) { - throw new PDFIOException("error.pdf.io.04", e2); - } - - } - throw new PdfAsException("INVALID STATE"); - } - - @Override - public List extractList(PDFObject doc, String placeholderId, int matchMode) throws PdfAsException { - if (doc instanceof PDFBOXObject) { - PDFBOXObject object = (PDFBOXObject) doc; - try { - SignaturePlaceholderExtractor extractor = new SignaturePlaceholderExtractor(placeholderId, - matchMode, object.getDocument()); - return extractor.extractList(object.getDocument(), - placeholderId, matchMode); + SignaturePlaceholderExtractor extractor = new SignaturePlaceholderExtractor(); + return extractor.extract(object.getDocument(), placeholderId, matchMode); } catch (IOException | ClassNotFoundException | InstantiationException | IllegalAccessException e2) { throw new PDFIOException("error.pdf.io.04", e2); } diff --git a/pdf-as-pdfbox-2/src/main/java/at/gv/egiz/pdfas/lib/impl/pdfbox2/placeholder/SignatureFieldsAndPlaceHolderExtractor.java b/pdf-as-pdfbox-2/src/main/java/at/gv/egiz/pdfas/lib/impl/pdfbox2/placeholder/SignatureFieldsAndPlaceHolderExtractor.java index 609f8254..8e5e5d4e 100644 --- a/pdf-as-pdfbox-2/src/main/java/at/gv/egiz/pdfas/lib/impl/pdfbox2/placeholder/SignatureFieldsAndPlaceHolderExtractor.java +++ b/pdf-as-pdfbox-2/src/main/java/at/gv/egiz/pdfas/lib/impl/pdfbox2/placeholder/SignatureFieldsAndPlaceHolderExtractor.java @@ -1,106 +1,58 @@ package at.gv.egiz.pdfas.lib.impl.pdfbox2.placeholder; -import at.gv.egiz.pdfas.lib.impl.placeholder.PlaceholderExtractorConstants; -import at.gv.egiz.pdfas.lib.impl.placeholder.SignaturePlaceholderData; +import java.util.ArrayList; +import java.util.List; + import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.interactive.digitalsignature.PDSignature; import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm; import org.apache.pdfbox.pdmodel.interactive.form.PDField; import org.apache.pdfbox.pdmodel.interactive.form.PDSignatureField; -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; +import at.gv.egiz.pdfas.lib.impl.placeholder.PlaceholderExtractorConstants; +import at.gv.egiz.pdfas.lib.impl.placeholder.SignaturePlaceholderData; public class SignatureFieldsAndPlaceHolderExtractor { - //Search for empty signature fields - public static List findEmptySignatureFields(PDDocument doc) - { - PDSignature signature; - List signatureField; - List signatureFieldNames = new ArrayList<>(); - PDAcroForm acroForm = doc.getDocumentCatalog().getAcroForm(); - if (acroForm != null) { - signatureField = acroForm.getFields(); - for (PDField pdField : signatureField) { - if(pdField instanceof PDSignatureField && pdField.getPartialName()!=null) - { - signature = ((PDSignatureField) pdField).getSignature(); - if(signature == null) signatureFieldNames.add(pdField.getPartialName()); - } - } - } - return signatureFieldNames; - } - /* - Needed by PDF-OVER - */ - - /** - * Returns the next unused signature placeholder - * @param doc The document to be searched for signature placeholders - * @return The next unused signature placeholder or null in case there is none - */ - public static SignaturePlaceholderData getNextUnusedSignaturePlaceHolder(PDDocument doc) { - try { - String placeholderId = "1"; - int mode = PlaceholderExtractorConstants.PLACEHOLDER_MATCH_MODE_SORTED; - SignaturePlaceholderExtractor signaturePlaceholderExtractor = new SignaturePlaceholderExtractor( placeholderId, - mode, doc); - List results = signaturePlaceholderExtractor.extractList(doc, placeholderId, - mode); - if (results == null) { - return null; - } - List used = getExistingSignatureLocations(doc); - //return first not used - for(SignaturePlaceholderData result : results) { - if(!used.contains(result.getPlaceholderName())) - return result; - } - return null; - } catch (Exception e) { - e.printStackTrace(); - return null; - } - } - - public static SignaturePlaceholderData getSignaturePlaceHolder(PDDocument doc, String placeholderId, - int mode) { - try { - SignaturePlaceholderExtractor signaturePlaceholderExtractor = new SignaturePlaceholderExtractor( placeholderId, - mode, doc); - return signaturePlaceholderExtractor.extract(doc, placeholderId, mode); - } catch (Exception e) { - e.printStackTrace(); - return null; + // Search for empty signature fields + public static List findEmptySignatureFields(PDDocument doc) { + PDSignature signature; + List signatureField; + final List signatureFieldNames = new ArrayList<>(); + final PDAcroForm acroForm = doc.getDocumentCatalog().getAcroForm(); + if (acroForm != null) { + signatureField = acroForm.getFields(); + for (final PDField pdField : signatureField) { + if (pdField instanceof PDSignatureField && pdField.getPartialName() != null) { + signature = ((PDSignatureField) pdField).getSignature(); + if (signature == null) { + signatureFieldNames.add(pdField.getPartialName()); + } } + } } - - public static List getSignaturePlaceHolderList(PDDocument doc, String placeholderId, int mode) { - try { - SignaturePlaceholderExtractor signaturePlaceholderExtractor = new SignaturePlaceholderExtractor( placeholderId, - mode, doc); - return signaturePlaceholderExtractor.extractList(doc, placeholderId, mode); - } catch (Exception e) { - e.printStackTrace(); - return null; - } - } - - public static List getExistingSignatureLocations(PDDocument doc) { - List existingLocations = new ArrayList<>(); - try { - List pdSignatureList = doc.getSignatureDictionaries(); - if(pdSignatureList.size() != 0) { - for(PDSignature sig : pdSignatureList) { - existingLocations.add(sig.getLocation()); - } - } - } catch (IOException e) { - e.printStackTrace(); - } - return existingLocations; + return signatureFieldNames; + } + /* + * Needed by PDF-OVER + */ + + /** + * Returns the next unused signature placeholder + * + * @param doc The document to be searched for signature placeholders + * @return The next unused signature placeholder or null in case there is none + */ + public static SignaturePlaceholderData getNextUnusedSignaturePlaceHolder(PDDocument doc) { + try { + final String placeholderId = "1"; + final int mode = PlaceholderExtractorConstants.PLACEHOLDER_MATCH_MODE_SORTED; + final SignaturePlaceholderExtractor signaturePlaceholderExtractor = new SignaturePlaceholderExtractor(); + return signaturePlaceholderExtractor.extract(doc, placeholderId, mode); + + } catch (final Exception e) { + e.printStackTrace(); + return null; } + } } diff --git a/pdf-as-pdfbox-2/src/main/java/at/gv/egiz/pdfas/lib/impl/pdfbox2/placeholder/SignaturePlaceholderExtractor.java b/pdf-as-pdfbox-2/src/main/java/at/gv/egiz/pdfas/lib/impl/pdfbox2/placeholder/SignaturePlaceholderExtractor.java index b62b660a..99027be0 100644 --- a/pdf-as-pdfbox-2/src/main/java/at/gv/egiz/pdfas/lib/impl/pdfbox2/placeholder/SignaturePlaceholderExtractor.java +++ b/pdf-as-pdfbox-2/src/main/java/at/gv/egiz/pdfas/lib/impl/pdfbox2/placeholder/SignaturePlaceholderExtractor.java @@ -3,19 +3,19 @@ * PDF-AS has been contracted by the E-Government Innovation Center EGIZ, a * joint initiative of the Federal Chancellery Austria and Graz University of * Technology. - * + * * Licensed under the EUPL, Version 1.1 or - as soon they will be approved by * the European Commission - subsequent versions of the EUPL (the "Licence"); * You may not use this work except in compliance with the Licence. * You may obtain a copy of the Licence at: * http://www.osor.eu/eupl/ - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the Licence is distributed on an "AS IS" basis, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the Licence for the specific language governing permissions and * limitations under the Licence. - * + * * This product combines work with different licenses. See the "NOTICE" text * file for details on the various modules and licenses. * The "NOTICE" text file is part of the distribution. Any derivative works @@ -51,30 +51,26 @@ import java.awt.geom.NoninvertibleTransformException; import java.awt.image.BufferedImage; import java.io.IOException; import java.util.ArrayList; -import java.util.HashMap; +import java.util.Collections; import java.util.Hashtable; import java.util.List; -import java.util.Map; import java.util.Map.Entry; import java.util.Properties; import java.util.Set; import java.util.Vector; +import java.util.stream.Collectors; import org.apache.pdfbox.contentstream.PDFStreamEngine; import org.apache.pdfbox.contentstream.operator.Operator; import org.apache.pdfbox.contentstream.operator.OperatorProcessor; import org.apache.pdfbox.cos.COSBase; -import org.apache.pdfbox.cos.COSDictionary; import org.apache.pdfbox.cos.COSName; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDPage; -import org.apache.pdfbox.pdmodel.font.PDFont; -import org.apache.pdfbox.pdmodel.font.PDFontFactory; import org.apache.pdfbox.pdmodel.graphics.PDXObject; import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject; +import org.apache.pdfbox.pdmodel.interactive.digitalsignature.PDSignature; import org.apache.pdfbox.util.Matrix; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import com.google.zxing.BarcodeFormat; import com.google.zxing.BinaryBitmap; @@ -91,10 +87,10 @@ import at.gv.egiz.pdfas.common.exceptions.PDFIOException; import at.gv.egiz.pdfas.common.exceptions.PdfAsException; import at.gv.egiz.pdfas.common.exceptions.PlaceholderExtractionException; import at.gv.egiz.pdfas.lib.impl.placeholder.PlaceholderExtractorConstants; -import at.gv.egiz.pdfas.lib.impl.placeholder.SignaturePlaceholderContext; import at.gv.egiz.pdfas.lib.impl.placeholder.SignaturePlaceholderData; import at.knowcenter.wag.egov.egiz.pdf.TablePos; import javassist.bytecode.stackmap.TypeData.ClassName; +import lombok.extern.slf4j.Slf4j; /** * Extract all relevant information from a placeholder image. @@ -102,423 +98,393 @@ import javassist.bytecode.stackmap.TypeData.ClassName; * @author exthex * */ -public class SignaturePlaceholderExtractor extends PDFStreamEngine implements PlaceholderExtractorConstants{ - /** - * The log. - */ - private static Logger logger = LoggerFactory - .getLogger(SignaturePlaceholderExtractor.class); - - private List placeholders = new ArrayList<>(); - private int currentPage = 0; - private PDDocument doc; - - protected SignaturePlaceholderExtractor(String placeholderId, - int placeholderMatchMode, PDDocument doc) throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException { - super(); - - final Properties properties = new Properties(); - properties.load(ClassName.class.getClassLoader().getResourceAsStream("placeholder/pdfbox-reader-2.properties")); - - Set> entries = properties.entrySet(); - for(Entry entry:entries){ - String processorClassName = (String)entry.getValue(); - Class klass = Class.forName( processorClassName ); - org.apache.pdfbox.contentstream.operator.OperatorProcessor processor = - (OperatorProcessor) klass.newInstance(); - - addOperator( processor ); - } - this.doc = doc; - } - - /** - * Search the document for placeholder images and possibly included - * additional info.
- * Searches only for the first placeholder page after page from top. - * - * @return all available info from the first found placeholder. - * @throws PdfAsException - * if the document could not be read. - * @throws PlaceholderExtractionException - * if STRICT matching mode was requested and no suitable - * placeholder could be found. - */ - public SignaturePlaceholderData extract(PDDocument doc, - String placeholderId, int matchMode) throws PdfAsException { - SignaturePlaceholderContext.setSignaturePlaceholderData(null); -// SignaturePlaceholderExtractor extractor; -// try { -// extractor = new SignaturePlaceholderExtractor(placeholderId, -// matchMode, doc); -// } catch (IOException | ClassNotFoundException | InstantiationException | IllegalAccessException e2) { -// throw new PDFIOException("error.pdf.io.04", e2); -// } - - int pageNr = 0; - for(PDPage page : doc.getPages()){ - pageNr++; - - try { - setCurrentPage(pageNr); - if(page.getContents() != null && page.getResources() != null && page.getContentStreams() != null) { - processPage(page); //TODO: pdfbox2 - right? - - } - SignaturePlaceholderData ret = matchPlaceholderPage( - placeholders, placeholderId, matchMode); - if (ret != null) { - SignaturePlaceholderContext - .setSignaturePlaceholderData(ret); - return ret; - } - } catch (IOException e1) { - throw new PDFIOException("error.pdf.io.04", e1); - } catch(Throwable e) { - throw new PDFIOException("error.pdf.io.04", e); - } - } - if (placeholders.size() > 0) { - SignaturePlaceholderData ret = matchPlaceholderDocument( - placeholders, placeholderId, matchMode); - SignaturePlaceholderContext.setSignaturePlaceholderData(ret); - return ret; - } - // no placeholders found, apply strict mode if set - if (matchMode == PLACEHOLDER_MATCH_MODE_STRICT) { - throw new PlaceholderExtractionException("error.pdf.stamp.09"); - } - - return null; - } - - public List extractList(PDDocument doc, - String placeholderId, int matchMode) throws PdfAsException { - SignaturePlaceholderContext.setSignaturePlaceholderData(null); - - int pageNr = 0; - for(PDPage page : doc.getPages()){ - pageNr++; - - try { - setCurrentPage(pageNr); - if(page.getContents() != null && page.getResources() != null && page.getContentStreams() != null) { - processPage(page); //TODO: pdfbox2 - right? - - } - SignaturePlaceholderData ret = matchPlaceholderPage( - placeholders, placeholderId, matchMode); - if (ret != null) { - SignaturePlaceholderContext - .setSignaturePlaceholderData(ret); - return placeholders; - } - } catch (IOException e1) { - throw new PDFIOException("error.pdf.io.04", e1); - } catch(Throwable e) { - throw new PDFIOException("error.pdf.io.04", e); - } - } - if (placeholders.size() > 0) { - SignaturePlaceholderData ret = matchPlaceholderDocument( - placeholders, placeholderId, matchMode); - SignaturePlaceholderContext.setSignaturePlaceholderData(ret); - return placeholders; - } - // no placeholders found, apply strict mode if set - if (matchMode == PLACEHOLDER_MATCH_MODE_STRICT) { - throw new PlaceholderExtractionException("error.pdf.stamp.09"); - } - return null; - } - - private SignaturePlaceholderData matchPlaceholderDocument( - List placeholders, String placeholderId, - int matchMode) throws PlaceholderExtractionException { - - if (matchMode == PLACEHOLDER_MATCH_MODE_STRICT) - throw new PlaceholderExtractionException("error.pdf.stamp.09"); - - if (placeholders.size() == 0) - return null; - - if (matchMode == PLACEHOLDER_MATCH_MODE_SORTED) { - // sort all placeholders by the id string if all ids are null do nothing - SignaturePlaceholderData currentFirstSpd = null; - for (int i = 0; i < placeholders.size(); i++) { - SignaturePlaceholderData spd = placeholders.get(i); - if (spd.getId() != null) { - if(currentFirstSpd == null) { - currentFirstSpd = spd; - logger.debug("Setting new current ID: {}", - currentFirstSpd.getId()); - } else { - String currentID = currentFirstSpd.getId(); - String testID = spd.getId(); - logger.debug("Testing placeholder current: {} compare to {}", - currentID, testID); - if(testID.compareToIgnoreCase(currentID) < 0) { - currentFirstSpd = spd; - logger.debug("Setting new current ID: {}", - testID); - } - } - } - } - - if(currentFirstSpd != null) { - logger.info("Running Placeholder sorted mode: using id: {}", currentFirstSpd.getId()); - return currentFirstSpd; - } else { - logger.info("Running Placeholder sorted mode: no placeholder with id found, fallback to first placeholder"); - } - } - - for (int i = 0; i < placeholders.size(); i++) { - SignaturePlaceholderData spd = placeholders.get(i); - if (spd.getId() == null) - return spd; - } - - if (matchMode == PLACEHOLDER_MATCH_MODE_LENIENT) - return placeholders.get(0); - - return null; - } - - private SignaturePlaceholderData matchPlaceholderPage( - List placeholders, String placeholderId, - int matchMode) { - - if(matchMode == PLACEHOLDER_MATCH_MODE_SORTED) - return null; - - if (placeholders.size() == 0) - return null; - for (int i = 0; i < placeholders.size(); i++) { - SignaturePlaceholderData data = placeholders.get(i); - if (placeholderId != null && placeholderId.equals(data.getId())) - return data; - if (placeholderId == null && data.getId() == null) - return data; - } - return null; - } - - - - private void setCurrentPage(int pageNr) { - this.currentPage = pageNr; - } - - @Override - protected void processOperator(Operator operator, List arguments) - throws IOException { - String operation = operator.getName(); - if (operation.equals("Do")) { - COSName objectName = (COSName) arguments.get(0); - PDXObject xobject = (PDXObject) getResources().getXObject(objectName); - if (xobject instanceof PDImageXObject) { - try { - PDImageXObject image = (PDImageXObject) xobject; - SignaturePlaceholderData data = checkImage(image); - if (data != null) { - PDPage page = getCurrentPage(); - Matrix ctm = getGraphicsState() - .getCurrentTransformationMatrix(); - int pageRotation = page.getRotation(); - pageRotation = pageRotation % 360; - double rotationInRadians = Math.toRadians(pageRotation);//(page.findRotation() * Math.PI) / 180; - - AffineTransform rotation = new AffineTransform(); - rotation.setToRotation(rotationInRadians); - AffineTransform rotationInverse = rotation - .createInverse(); - Matrix rotationInverseMatrix = new Matrix(); - rotationInverseMatrix - .setFromAffineTransform(rotationInverse); - Matrix rotationMatrix = new Matrix(); - rotationMatrix.setFromAffineTransform(rotation); - - Matrix unrotatedCTM = ctm - .multiply(rotationInverseMatrix); - - float x = unrotatedCTM.getXPosition(); - float yPos = unrotatedCTM.getYPosition(); - float yScale = unrotatedCTM.getScaleY(); - float y = yPos + yScale; - float w = unrotatedCTM.getScaleX(); - - logger.debug("Page height: {}", page.getCropBox().getHeight()); - logger.debug("Page width: {}", page.getCropBox().getWidth()); - - if(pageRotation == 90) { - y = page.getCropBox().getWidth() - (y * (-1)); - } else if(pageRotation == 180) { - x = page.getCropBox().getWidth() + x; - y = page.getCropBox().getHeight() - (y * (-1)); - } else if(pageRotation == 270) { - x = page.getCropBox().getHeight() + x; - } - - - - - - String posString = "p:" + currentPage + ";x:" + Math.floor(x) - + ";y:" + Math.ceil(y) + ";w:" + Math.ceil(w); - - logger.debug("Found Placeholder at: {}", posString); - try { - data.setTablePos(new TablePos(posString)); - data.setPlaceholderName(objectName.getName()); - placeholders.add(data); - } catch (PdfAsException e) { - throw new IOException(); - } - } - } catch (NoninvertibleTransformException e) { - throw new IOException(e); - } - } - } else { - super.processOperator(operator, arguments); - } - } - - private Map fonts; - - //TODO: pdfbox2 - was override - public Map getFonts() { - if (fonts == null) { - // at least an empty map will be returned - // TODO we should return null instead of an empty map - fonts = new HashMap(); - if(this.getResources() != null && this.getResources().getCOSObject() != null) { - COSDictionary fontsDictionary = (COSDictionary) this.getResources().getCOSObject().getDictionaryObject(COSName.FONT); - if (fontsDictionary == null) { - // ignore we do not want to set anything, never when creating a signature!!!!! - //fontsDictionary = new COSDictionary(); - //this.getResources().getCOSDictionary().setItem(COSName.FONT, fontsDictionary); +@Slf4j +public class SignaturePlaceholderExtractor extends PDFStreamEngine implements PlaceholderExtractorConstants { + + private final List placeholders = new ArrayList<>(); + private int currentPage = 0; + + protected SignaturePlaceholderExtractor() throws IOException, ClassNotFoundException, + InstantiationException, IllegalAccessException { + super(); + + final Properties properties = new Properties(); + properties.load(ClassName.class.getClassLoader().getResourceAsStream( + "placeholder/pdfbox-reader-2.properties")); + + final Set> entries = properties.entrySet(); + for (final Entry entry : entries) { + final String processorClassName = (String) entry.getValue(); + final Class klass = Class.forName(processorClassName); + final org.apache.pdfbox.contentstream.operator.OperatorProcessor processor = + (OperatorProcessor) klass.newInstance(); + + addOperator(processor); + + } + } + + /** + * Search the document for placeholder images and possibly included additional + * info.
+ * Searches only for the first placeholder page after page from top. + * + * @return available info from the first found placeholder. + * @throws PdfAsException if the document could not be read. + * @throws PlaceholderExtractionException if STRICT matching mode was requested + * and no suitable placeholder could be + * found. + */ + public SignaturePlaceholderData extract(PDDocument doc, + String placeholderId, int matchMode) throws PdfAsException { + + List extistingSignatureNames = existingExistingSignatureNames(doc); + + + int pageNr = 0; + for (final PDPage page : doc.getPages()) { + pageNr++; + + try { + this.currentPage = pageNr; + if (page.getContents() != null && page.getResources() != null && page.getContentStreams() != null) { + processPage(page); // TODO: pdfbox2 - right? + + } + + final SignaturePlaceholderData ret = matchPlaceholderPage( + removeAlreadyUsePlaceholders(placeholders, extistingSignatureNames), placeholderId, matchMode); + if (ret != null) { + return ret; + + } + + } catch (final IOException e1) { + throw new PDFIOException("error.pdf.io.04", e1); + + } catch (final Throwable e) { + throw new PDFIOException("error.pdf.io.04", e); + + } + } + + if (placeholders.size() > 0) { + final SignaturePlaceholderData ret = matchPlaceholderDocument( + removeAlreadyUsePlaceholders(placeholders, extistingSignatureNames), placeholderId, matchMode); + return ret; + + } + // no placeholders found, apply strict mode if set + if (matchMode == PLACEHOLDER_MATCH_MODE_STRICT) { + throw new PlaceholderExtractionException("error.pdf.stamp.09"); + + } + return null; + } + + @Override + protected void processOperator(Operator operator, List arguments) + throws IOException { + final String operation = operator.getName(); + if (operation.equals("Do")) { + final COSName objectName = (COSName) arguments.get(0); + final PDXObject xobject = getResources().getXObject(objectName); + if (xobject instanceof PDImageXObject) { + try { + final PDImageXObject image = (PDImageXObject) xobject; + final SignaturePlaceholderData data = checkImage(image); + if (data != null) { + final PDPage page = getCurrentPage(); + final Matrix ctm = getGraphicsState() + .getCurrentTransformationMatrix(); + int pageRotation = page.getRotation(); + pageRotation = pageRotation % 360; + final double rotationInRadians = Math.toRadians(pageRotation);// (page.findRotation() * Math.PI) / + // 180; + + final AffineTransform rotation = new AffineTransform(); + rotation.setToRotation(rotationInRadians); + final AffineTransform rotationInverse = rotation + .createInverse(); + final Matrix rotationInverseMatrix = new Matrix(); + rotationInverseMatrix + .setFromAffineTransform(rotationInverse); + final Matrix rotationMatrix = new Matrix(); + rotationMatrix.setFromAffineTransform(rotation); + + final Matrix unrotatedCTM = ctm + .multiply(rotationInverseMatrix); + + float x = unrotatedCTM.getXPosition(); + final float yPos = unrotatedCTM.getYPosition(); + final float yScale = unrotatedCTM.getScaleY(); + float y = yPos + yScale; + final float w = unrotatedCTM.getScaleX(); + + log.debug("Page height: {}", page.getCropBox().getHeight()); + log.debug("Page width: {}", page.getCropBox().getWidth()); + + if (pageRotation == 90) { + y = page.getCropBox().getWidth() - y * -1; + } else if (pageRotation == 180) { + x = page.getCropBox().getWidth() + x; + y = page.getCropBox().getHeight() - y * -1; + } else if (pageRotation == 270) { + x = page.getCropBox().getHeight() + x; } - else { - for (COSName fontName : fontsDictionary.keySet()) { - COSBase font = fontsDictionary.getDictionaryObject(fontName); - // data-000174.pdf contains a font that is a COSArray, looks to be an error in the - // PDF, we will just ignore entries that are not dictionaries. - if (font instanceof COSDictionary) { - PDFont newFont = null; - try { - newFont = PDFontFactory.createFont((COSDictionary) font); - } - catch (IOException exception) { - logger.error("error while creating a font", exception); - } - if (newFont != null) { - fonts.put(fontName.getName(), newFont); - } - } - } + + final String posString = "p:" + currentPage + ";x:" + Math.floor(x) + + ";y:" + Math.ceil(y) + ";w:" + Math.ceil(w); + + log.debug("Found Placeholder at: {}", posString); + try { + data.setTablePos(new TablePos(posString)); + data.setPlaceholderName(objectName.getName()); + placeholders.add(data); + + } catch (final PdfAsException e) { + throw new IOException(); + } + } + } catch (final NoninvertibleTransformException e) { + throw new IOException(e); + } + } + } else { + super.processOperator(operator, arguments); + } + } + + private SignaturePlaceholderData matchPlaceholderDocument( + List placeholders, String placeholderId, + int matchMode) throws PlaceholderExtractionException { + + if (matchMode == PLACEHOLDER_MATCH_MODE_STRICT) { + throw new PlaceholderExtractionException("error.pdf.stamp.09"); + } + + if (placeholders.size() == 0) { + return null; + } + + if (matchMode == PLACEHOLDER_MATCH_MODE_SORTED) { + // sort all placeholders by the id string if all ids are null do nothing + SignaturePlaceholderData currentFirstSpd = null; + for (final SignaturePlaceholderData spd : placeholders) { + if (spd.getId() != null) { + if (currentFirstSpd == null) { + currentFirstSpd = spd; + log.debug("Setting new current ID: {}", + currentFirstSpd.getId()); + } else { + currentFirstSpd = placeHolderIdMatcher(currentFirstSpd, spd); + + } + } + } + + if (currentFirstSpd != null) { + log.info("Running Placeholder sorted mode: using id: {}", currentFirstSpd.getId()); + return currentFirstSpd; + + } else { + log.info( + "Running Placeholder sorted mode: no placeholder with id found, fallback to first placeholder"); + } + } + + for (final SignaturePlaceholderData spd : placeholders) { + if (spd.getId() == null) { + return spd; + } + } + + if (matchMode == PLACEHOLDER_MATCH_MODE_LENIENT) { + return placeholders.get(0); + } + + return null; + } + + private SignaturePlaceholderData placeHolderIdMatcher(SignaturePlaceholderData currentFirstSpd, + SignaturePlaceholderData spd) { + try { + Integer currentIDInt = Integer.valueOf(currentFirstSpd.getId()); + Integer testIDInt = Integer.valueOf(spd.getId()); + + if (testIDInt < currentIDInt) { + log.debug("Setting new current ID: {}", testIDInt); + return spd; + + } else { + return currentFirstSpd; + + } + } catch (NumberFormatException e) { + log.trace("Can not compare placeholderId's on integer level. Using String compare ... "); + final String currentID = currentFirstSpd.getId(); + final String testID = spd.getId(); + log.debug("Testing placeholder current: {} compare to {}", + currentID, testID); + if (testID.compareToIgnoreCase(currentID) < 0) { + log.debug("Setting new current ID: {}", + testID); + return spd; + + } else { + return currentFirstSpd; + + } + } + } + + private SignaturePlaceholderData matchPlaceholderPage( + List placeholders, String placeholderId, + int matchMode) { + + if ((matchMode == PLACEHOLDER_MATCH_MODE_SORTED) || (placeholders.size() == 0)) { + return null; + } + + for (final SignaturePlaceholderData data : placeholders) { + if (placeholderId != null && placeholderId.equals(data.getId())) { + return data; + + } + if (placeholderId == null && data.getId() == null) { + return data; + + } + } + + return null; + } + + private List existingExistingSignatureNames(PDDocument doc) { + final List existingLocations = new ArrayList<>(); + try { + final List pdSignatureList = doc.getSignatureDictionaries(); + if (pdSignatureList.size() != 0) { + for (final PDSignature sig : pdSignatureList) { + existingLocations.add(sig.getLocation()); + } + } + } catch (final IOException e) { + e.printStackTrace(); + } + return existingLocations; + } + + private List removeAlreadyUsePlaceholders( + List placeholders, List existingPlaceholders) { + if (placeholders != null) { + return placeholders.stream() + .filter(el -> !existingPlaceholders.contains(el.getPlaceholderName())) + .collect(Collectors.toList()); + + } else { + return Collections.emptyList(); + + } + } + + /** + * Checks an image if it is a placeholder for a signature. + * + * @param image + * @return + * @throws IOException + */ + private SignaturePlaceholderData checkImage(PDImageXObject image) + throws IOException { + final BufferedImage bimg = image.getImage(); + if (bimg == null) { + String type = image.getSuffix(); + if (type != null) { + type = type.toUpperCase() + " images"; + } else { + type = "Image type"; + } + log.info("Unable to extract image for QRCode analysis. " + + type + + " not supported. Add additional JAI Image filters to your classpath. Refer to https://jai.dev.java.net. Skipping image."); + return null; + + } + + if (bimg.getHeight() < 10 || bimg.getWidth() < 10) { + log.debug("Image too small for QRCode. Skipping image."); + return null; + } + + final LuminanceSource source = new BufferedImageLuminanceSource(bimg); + final BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); + Result result; + final long before = System.currentTimeMillis(); + try { + final Hashtable hints = new Hashtable<>(); + final Vector formats = new Vector<>(); + formats.add(BarcodeFormat.QR_CODE); + hints.put(DecodeHintType.POSSIBLE_FORMATS, formats); + result = new MultiFormatReader().decode(bitmap, hints); + + final String text = result.getText(); + String profile = null; + String type = null; + String sigKey = null; + String id = null; + if (text != null) { + if (text.startsWith(QR_PLACEHOLDER_IDENTIFIER)) { + + final String[] data = text.split(";"); + if (data.length > 1) { + for (int i = 1; i < data.length; i++) { + final String kvPair = data[i]; + final String[] kv = kvPair.split("="); + if (kv.length != 2) { + log.debug("Invalid parameter in placeholder data: " + + kvPair); + } else { + if (kv[0] + .equalsIgnoreCase(SignaturePlaceholderData.ID_KEY)) { + id = kv[1]; + } else if (kv[0] + .equalsIgnoreCase(SignaturePlaceholderData.PROFILE_KEY)) { + profile = kv[1]; + } else if (kv[0] + .equalsIgnoreCase(SignaturePlaceholderData.SIG_KEY_KEY)) { + sigKey = kv[1]; + } else if (kv[0] + .equalsIgnoreCase(SignaturePlaceholderData.TYPE_KEY)) { + type = kv[1]; + } + } } + } + return new SignaturePlaceholderData(profile, type, sigKey, + id); + } else { + log.warn("QR-Code found but does not start with \"" + + QR_PLACEHOLDER_IDENTIFIER + + "\". Ignoring QR placeholder."); + } + } + } catch (final ReaderException re) { + if (log.isDebugEnabled()) { + log.debug("Could not decode - not a placeholder. needed: " + + (System.currentTimeMillis() - before)); + } + if (!(re instanceof NotFoundException)) { + if (log.isInfoEnabled()) { + log.info("Failed to decode image", re); } - return fonts; - } - - /** - * Checks an image if it is a placeholder for a signature. - * - * @param image - * @return - * @throws IOException - */ - private SignaturePlaceholderData checkImage(PDImageXObject image) - throws IOException { - BufferedImage bimg = image.getImage(); - if (bimg == null) { - String type = image.getSuffix(); - if (type != null) { - type = type.toUpperCase() + " images"; - } else { - type = "Image type"; - } - logger.info("Unable to extract image for QRCode analysis. " - + type - + " not supported. Add additional JAI Image filters to your classpath. Refer to https://jai.dev.java.net. Skipping image."); - return null; - } - if (bimg.getHeight() < 10 || bimg.getWidth() < 10) { - logger.debug("Image too small for QRCode. Skipping image."); - return null; - } - - LuminanceSource source = new BufferedImageLuminanceSource(bimg); - BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); - Result result; - long before = System.currentTimeMillis(); - try { - Hashtable hints = new Hashtable(); - Vector formats = new Vector(); - formats.add(BarcodeFormat.QR_CODE); - hints.put(DecodeHintType.POSSIBLE_FORMATS, formats); - result = new MultiFormatReader().decode(bitmap, hints); - - String text = result.getText(); - String profile = null; - String type = null; - String sigKey = null; - String id = null; - if (text != null) { - if (text.startsWith(QR_PLACEHOLDER_IDENTIFIER)) { - - String[] data = text.split(";"); - if (data.length > 1) { - for (int i = 1; i < data.length; i++) { - String kvPair = data[i]; - String[] kv = kvPair.split("="); - if (kv.length != 2) { - logger.debug("Invalid parameter in placeholder data: " - + kvPair); - } else { - if (kv[0] - .equalsIgnoreCase(SignaturePlaceholderData.ID_KEY)) { - id = kv[1]; - } else if (kv[0] - .equalsIgnoreCase(SignaturePlaceholderData.PROFILE_KEY)) { - profile = kv[1]; - } else if (kv[0] - .equalsIgnoreCase(SignaturePlaceholderData.SIG_KEY_KEY)) { - sigKey = kv[1]; - } else if (kv[0] - .equalsIgnoreCase(SignaturePlaceholderData.TYPE_KEY)) { - type = kv[1]; - } - } - } - } - return new SignaturePlaceholderData(profile, type, sigKey, - id); - } else { - logger.warn("QR-Code found but does not start with \"" - + QR_PLACEHOLDER_IDENTIFIER - + "\". Ignoring QR placeholder."); - } - } - } catch (ReaderException re) { - if (logger.isDebugEnabled()) { - logger.debug("Could not decode - not a placeholder. needed: " - + (System.currentTimeMillis() - before)); - } - if (!(re instanceof NotFoundException)) { - if (logger.isInfoEnabled()) { - logger.info("Failed to decode image", re); - } - } - } catch (ArrayIndexOutOfBoundsException e) { - if (logger.isInfoEnabled()) { - logger.info("Failed to decode image. Probably a zxing bug", e); - } - } - return null; - } + } + } catch (final ArrayIndexOutOfBoundsException e) { + if (log.isInfoEnabled()) { + log.info("Failed to decode image. Probably a zxing bug", e); + } + } + return null; + } } diff --git a/pdf-as-pdfbox-2/src/main/java/at/gv/egiz/pdfas/lib/impl/signing/pdfbox2/PADESPDFBOXSigner.java b/pdf-as-pdfbox-2/src/main/java/at/gv/egiz/pdfas/lib/impl/signing/pdfbox2/PADESPDFBOXSigner.java index b827abe6..e555cb39 100644 --- a/pdf-as-pdfbox-2/src/main/java/at/gv/egiz/pdfas/lib/impl/signing/pdfbox2/PADESPDFBOXSigner.java +++ b/pdf-as-pdfbox-2/src/main/java/at/gv/egiz/pdfas/lib/impl/signing/pdfbox2/PADESPDFBOXSigner.java @@ -70,8 +70,6 @@ import org.apache.pdfbox.rendering.PDFRenderer; import org.apache.xmpbox.XMPMetadata; import org.apache.xmpbox.schema.PDFAIdentificationSchema; import org.apache.xmpbox.xml.DomXmpParser; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import at.gv.egiz.pdfas.common.exceptions.PDFASError; import at.gv.egiz.pdfas.common.exceptions.PdfAsException; @@ -83,7 +81,6 @@ import at.gv.egiz.pdfas.lib.api.sign.IPlainSigner; import at.gv.egiz.pdfas.lib.api.sign.SignParameter; import at.gv.egiz.pdfas.lib.impl.ErrorExtractor; import at.gv.egiz.pdfas.lib.impl.SignaturePositionImpl; -import at.gv.egiz.pdfas.lib.impl.configuration.PlaceholderWebConfiguration; import at.gv.egiz.pdfas.lib.impl.configuration.SignatureProfileConfiguration; import at.gv.egiz.pdfas.lib.impl.pdfbox2.PDFBOXObject; import at.gv.egiz.pdfas.lib.impl.pdfbox2.positioning.Positioning; @@ -107,40 +104,32 @@ import at.knowcenter.wag.egov.egiz.pdf.PositioningInstruction; import at.knowcenter.wag.egov.egiz.pdf.TablePos; import at.knowcenter.wag.egov.egiz.table.Table; import iaik.x509.X509Certificate; +import lombok.extern.slf4j.Slf4j; +@Slf4j public class PADESPDFBOXSigner implements IPdfSigner, IConfigurationConstants { - private static final Logger logger = LoggerFactory.getLogger(PADESPDFBOXSigner.class); - private boolean isAdobeSigForm = false; + @Override public void signPDF(PDFObject genericPdfObject, RequestedSignature requestedSignature, PDFASSignatureInterface genericSigner) throws PdfAsException { PDFAsVisualSignatureProperties properties = null; - List placeholders; - List availablePlaceholders; - SignaturePlaceholderData signaturePlaceholderData = null; - - String placeholder_id = ""; - - if (PlaceholderWebConfiguration.getValue(PLACEHOLDER_WEB_ID) != null && !PlaceholderWebConfiguration - .getValue(PLACEHOLDER_WEB_ID).equalsIgnoreCase("")) { - placeholder_id = PlaceholderWebConfiguration.getValue(PLACEHOLDER_WEB_ID); - } - + + boolean isAdobeSigForm = false; + if (!(genericPdfObject instanceof PDFBOXObject)) { - // tODO: - throw new PdfAsException(); + throw new PdfAsException("PDF to signObject is of wrong type: " + genericPdfObject.getClass().getName()); + } - final PDFBOXObject pdfObject = (PDFBOXObject) genericPdfObject; - if (!(genericSigner instanceof PDFASPDFBOXSignatureInterface)) { - // tODO: - throw new PdfAsException(); + throw new PdfAsException("PDF signerObject is of wrong type:" + genericSigner.getClass().getName()); + } - + + final PDFBOXObject pdfObject = (PDFBOXObject) genericPdfObject; final PDFASPDFBOXSignatureInterface signer = (PDFASPDFBOXSignatureInterface) genericSigner; String pdfaVersion = null; @@ -148,76 +137,42 @@ public class PADESPDFBOXSigner implements IPdfSigner, IConfigurationConstants { PDDocument doc = null; SignatureOptions options = new SignatureOptions(); try { - doc = pdfObject.getDocument(); - // if signature already exists dont create new page - final List pdSignatureFieldList = doc.getSignatureFields(); - PDSignature signature; + // sign a PDF with an existing empty signature, as created by the // CreateEmptySignatureForm example. - String sigFieldName = pdfObject.getStatus().getSettings().getValue(SIGNATURE_FIELD_NAME); - signature = findExistingSignature(doc, sigFieldName); + PDSignature signature = findExistingSignature(doc, getSignatureFieldNameConfig(pdfObject)); if (signature == null) { // create signature dictionary signature = new PDSignature(); + } else { isAdobeSigForm = true; + } signature.setFilter(COSName.getPDFName(signer.getPDFFilter())); signature.setSubFilter(COSName.getPDFName(signer.getPDFSubFilter())); -// SignaturePlaceholderData signaturePlaceholderDataInit = - placeholders = PlaceholderFilter.checkPlaceholderSignatureLocationList(pdfObject.getStatus(), - pdfObject.getStatus().getSettings(), placeholder_id); - -// placeholders = SignaturePlaceholderExtractor.getPlaceholders(); - availablePlaceholders = listAvailablePlaceholders(placeholders, existingSignatureLocations(doc)); - - if (placeholder_id.equalsIgnoreCase("")) { - if (checkAvailablePlaceholders(placeholders, existingSignatureLocations(doc)) != null) { - placeholder_id = checkAvailablePlaceholders(placeholders, existingSignatureLocations(doc)).getId(); - } - } - - if (availablePlaceholders != null) { - signaturePlaceholderData = PlaceholderFilter - .checkPlaceholderSignatureLocation(pdfObject.getStatus(), pdfObject.getStatus().getSettings(), - placeholder_id); - } - - TablePos tablePos = null; - - if (signaturePlaceholderData != null) { - signature.setLocation(signaturePlaceholderData.getPlaceholderName()); - } - - if (signaturePlaceholderData != null) { - // Placeholder found! - placeholders.clear(); - logger.info("Placeholder data found."); - if (signaturePlaceholderData.getProfile() != null) { - logger.debug("Placeholder Profile set to: " + signaturePlaceholderData.getProfile()); - requestedSignature.setSignatureProfileID(signaturePlaceholderData.getProfile()); - } - - tablePos = signaturePlaceholderData.getTablePos(); - if (tablePos != null) { - - final SignatureProfileConfiguration signatureProfileConfiguration = pdfObject.getStatus() - .getSignatureProfileConfiguration(requestedSignature.getSignatureProfileID()); - - final float minWidth = signatureProfileConfiguration.getMinWidth(); - - if (minWidth > 0) { - if (tablePos.getWidth() < minWidth) { - tablePos.width = minWidth; - logger.debug("Correcting placeholder with to minimum width {}", minWidth); - } - } - logger.debug("Placeholder Position set to: " + tablePos.toString()); + + + String placeholder_id = pdfObject.getStatus().getSignParamter().getPlaceHolderId(); + + SignaturePlaceholderData nextPlaceholderData = PlaceholderFilter.checkPlaceholderSignatureLocation( + pdfObject.getStatus(), pdfObject.getStatus().getSettings(), placeholder_id); + + if (nextPlaceholderData != null) { + log.info("Placeholder data found."); + signature.setLocation(nextPlaceholderData.getPlaceholderName()); + + if (nextPlaceholderData.getProfile() != null) { + log.debug("Placeholder Profile set to: {}", nextPlaceholderData.getProfile()); + requestedSignature.setSignatureProfileID(nextPlaceholderData.getProfile()); + } } + + final SignatureProfileSettings signatureProfileSettings = TableFactory.createProfile( requestedSignature.getSignatureProfileID(), pdfObject.getStatus().getSettings()); @@ -243,9 +198,11 @@ public class PADESPDFBOXSigner implements IPdfSigner, IConfigurationConstants { } signature.setReason(signerReason); - logger.debug("Signing reason: " + signerReason); + log.debug("Signing reason: " + signerReason); - logger.debug("Signing @ " + signer.getSigningDate().getTime().toString()); + + + log.debug("Signing @ " + signer.getSigningDate().getTime().toString()); // the signing date, needed for valid signature // signature.setSignDate(signer.getSigningDate()); @@ -257,9 +214,9 @@ public class PADESPDFBOXSigner implements IPdfSigner, IConfigurationConstants { if (reservedSignatureSizeString != null) { signatureSize = Integer.parseInt(reservedSignatureSizeString); } - logger.debug("Reserving {} bytes for signature", signatureSize); + log.debug("Reserving {} bytes for signature", signatureSize); } catch (final NumberFormatException e) { - logger.warn("Invalid configuration value: {} should be a number using 0x1000", SIG_RESERVED_SIZE); + log.warn("Invalid configuration value: {} should be a number using 0x1000", SIG_RESERVED_SIZE); } options.setPreferredSignatureSize(signatureSize); @@ -269,44 +226,15 @@ public class PADESPDFBOXSigner implements IPdfSigner, IConfigurationConstants { } // Is visible Signature - if (requestedSignature.isVisual()) { - logger.info("Creating visual siganture block"); - - final SignatureProfileConfiguration signatureProfileConfiguration = pdfObject.getStatus() - .getSignatureProfileConfiguration(requestedSignature.getSignatureProfileID()); - - if (tablePos == null) { - // ================================================================ - // PositioningStage (visual) -> find position or use - // fixed - // position - - final String posString = pdfObject.getStatus().getSignParamter().getSignaturePosition(); - - TablePos signaturePos = null; - - final String signaturePosString = signatureProfileConfiguration.getDefaultPositioning(); - - if (signaturePosString != null) { - logger.debug("using signature Positioning: " + signaturePos); - signaturePos = new TablePos(signaturePosString); - } - - logger.debug("using Positioning: " + posString); - - if (posString != null) { - // Merge Signature Position - tablePos = new TablePos(posString, signaturePos); - } else { - // Fallback to signature Position! - tablePos = signaturePos; - } - - if (tablePos == null) { - // Last Fallback default position - tablePos = new TablePos(); - } - } + if (requestedSignature.isVisual()) { + log.info("Creating visual siganture block"); + + final SignatureProfileConfiguration signatureProfileConfiguration = + pdfObject.getStatus().getSignatureProfileConfiguration(requestedSignature.getSignatureProfileID()); + TablePos tablePos = prepareTablePosition(nextPlaceholderData, signatureProfileConfiguration, + pdfObject.getStatus().getSignParamter().getSignaturePosition()); + + // Legacy Modes not supported with pdfbox2 anymore // boolean legacy32Position = signatureProfileConfiguration.getLegacy32Positioning(); @@ -331,7 +259,7 @@ public class PADESPDFBOXSigner implements IPdfSigner, IConfigurationConstants { "", doc, visualObject, pdfObject.getStatus().getSettings(), signatureProfileSettings); - logger.debug("Positioning: {}", positioningInstruction.toString()); + log.debug("Positioning: {}", positioningInstruction.toString()); if (!isAdobeSigForm) { if (positioningInstruction.isMakeNewPage()) { @@ -347,11 +275,11 @@ public class PADESPDFBOXSigner implements IPdfSigner, IConfigurationConstants { // handle rotated page final int targetPageNumber = positioningInstruction.getPage(); - logger.debug("Target Page: " + targetPageNumber); + log.debug("Target Page: " + targetPageNumber); final PDPage targetPage = doc.getPages().get(targetPageNumber - 1); final int rot = targetPage.getRotation(); - logger.debug("Page rotation: " + rot); - logger.debug("resulting Sign rotation: " + positioningInstruction.getRotation()); + log.debug("Page rotation: " + rot); + log.debug("resulting Sign rotation: " + positioningInstruction.getRotation()); final SignaturePositionImpl position = new SignaturePositionImpl(); position.setX(positioningInstruction.getX()); @@ -386,7 +314,7 @@ public class PADESPDFBOXSigner implements IPdfSigner, IConfigurationConstants { * int pageNumber = positioningInstruction.getPage(); PDPage page = * doc.getPages().get(pageNumber - 1); * - * logger.info("Placeholder name: " + + * log.info("Placeholder name: " + * signaturePlaceholderData.getPlaceholderName()); COSDictionary * xobjectsDictionary = (COSDictionary) page.getResources().getCOSObject() * .getDictionaryObject(COSName.XOBJECT); @@ -395,7 +323,7 @@ public class PADESPDFBOXSigner implements IPdfSigner, IConfigurationConstants { * xobjectsDictionary.setItem(signaturePlaceholderData.getPlaceholderName(), * img); xobjectsDictionary.setNeedToBeUpdated(true); * page.getResources().getCOSObject().setNeedToBeUpdated(true); - * logger.info("Placeholder name: " + + * log.info("Placeholder name: " + * signaturePlaceholderData.getPlaceholderName()); } */ @@ -418,7 +346,7 @@ public class PADESPDFBOXSigner implements IPdfSigner, IConfigurationConstants { root.setOutputIntents(oi); root.getCOSObject().setNeedToBeUpdated(true); - logger.info("added Output Intent"); + log.info("added Output Intent"); } catch (final Throwable e) { e.printStackTrace(); throw new PdfAsException("Failed to add Output Intent", e); @@ -432,6 +360,8 @@ public class PADESPDFBOXSigner implements IPdfSigner, IConfigurationConstants { doc.addSignature(signature, signer, options); + String sigFieldName = getSignatureFieldNameConfig(pdfObject); + if (sigFieldName == null) { sigFieldName = "PDF-AS Signatur"; } @@ -473,7 +403,7 @@ public class PADESPDFBOXSigner implements IPdfSigner, IConfigurationConstants { } } } else { - logger.warn("Failed to name Signature Field! [Cannot find Field list in acroForm!]"); + log.warn("Failed to name Signature Field! [Cannot find Field list in acroForm!]"); } if (signatureField != null) { @@ -485,7 +415,7 @@ public class PADESPDFBOXSigner implements IPdfSigner, IConfigurationConstants { signatureField.setAlternateFieldName(sigFieldName); } } else { - logger.warn("Failed to name Signature Field! [Cannot find acroForm!]"); + log.warn("Failed to name Signature Field! [Cannot find acroForm!]"); } } @@ -496,16 +426,16 @@ public class PADESPDFBOXSigner implements IPdfSigner, IConfigurationConstants { } // PDF-UA - logger.info("Adding pdf/ua content."); + log.info("Adding pdf/ua content."); try { final PDDocumentCatalog root = doc.getDocumentCatalog(); final PDStructureTreeRoot structureTreeRoot = root.getStructureTreeRoot(); if (structureTreeRoot != null) { - logger.info("Tree Root: {}", structureTreeRoot.toString()); + log.info("Tree Root: {}", structureTreeRoot.toString()); final List kids = structureTreeRoot.getKids(); if (kids == null) { - logger.info("No kid-elements in structure tree Root, maybe not PDF/UA document"); + log.info("No kid-elements in structure tree Root, maybe not PDF/UA document"); } PDStructureElement docElement = null; @@ -549,7 +479,7 @@ public class PADESPDFBOXSigner implements IPdfSigner, IConfigurationConstants { PDNumberTreeNode ntn = structureTreeRoot.getParentTree(); if (ntn == null) { ntn = new PDNumberTreeNode(objectDic, null); - logger.info("No number-tree-node found!"); + log.info("No number-tree-node found!"); } final COSArray ntnKids = (COSArray) ntn.getCOSObject().getDictionaryObject(COSName.KIDS); @@ -622,10 +552,10 @@ public class PADESPDFBOXSigner implements IPdfSigner, IConfigurationConstants { } } catch (final Throwable e) { if (signatureProfileSettings.isPDFUA() == true) { - logger.error("Could not create PDF-UA conform document!"); + log.error("Could not create PDF-UA conform document!"); throw new PdfAsException("error.pdf.sig.pdfua.1", e); } else { - logger.info("Could not create PDF-UA conform signature"); + log.info("Could not create PDF-UA conform signature"); } } @@ -642,19 +572,19 @@ public class PADESPDFBOXSigner implements IPdfSigner, IConfigurationConstants { } } catch (final IOException e1) { - logger.error("Can not save incremental update", e1); + log.error("Can not save incremental update", e1); } System.gc(); - logger.debug("Signature done!"); + log.debug("Signature done!"); } catch (final IOException e) { - logger.warn(MessageResolver.resolveMessage("error.pdf.sig.01"), e); + log.warn(MessageResolver.resolveMessage("error.pdf.sig.01"), e); throw new PdfAsException("error.pdf.sig.01", e); } catch (PDFASError e2) { - logger.warn(e2.getInfo()); + log.warn(e2.getInfo()); throw new PdfAsException("error.pdf.sig.01", e2); } finally { @@ -664,7 +594,7 @@ public class PADESPDFBOXSigner implements IPdfSigner, IConfigurationConstants { options.getVisualSignature().close(); options.close(); } catch (IOException e) { - logger.debug("Failed to close VisualSignature!", e); + log.debug("Failed to close VisualSignature!", e); } } } @@ -674,13 +604,60 @@ public class PADESPDFBOXSigner implements IPdfSigner, IConfigurationConstants { doc.close(); // SignaturePlaceholderExtractor.getPlaceholders().clear(); } catch (final IOException e) { - logger.debug("Failed to close COS Doc!", e); + log.debug("Failed to close COS Doc!", e); // Ignore } } } } + private TablePos prepareTablePosition(SignaturePlaceholderData nextPlaceholderData, + SignatureProfileConfiguration signatureProfileConfiguration, String profilePosParam) throws PdfAsException { + + + if (nextPlaceholderData != null && nextPlaceholderData.getTablePos() != null) { + final float minWidth = signatureProfileConfiguration.getMinWidth(); + TablePos tablePos = nextPlaceholderData.getTablePos(); + if (minWidth > 0) { + if (tablePos.getWidth() < minWidth) { + tablePos.width = minWidth; + log.debug("Correcting placeholder with to minimum width {}", minWidth); + } + } + log.debug("Placeholder Position set to: " + tablePos.toString()); + return tablePos; + + } else { + TablePos signaturePos = null; + final String signaturePosString = signatureProfileConfiguration.getDefaultPositioning(); + + if (signaturePosString != null) { + log.debug("using signature Positioning: " + signaturePosString); + signaturePos = new TablePos(signaturePosString); + + } + + log.debug("using Positioning: " + profilePosParam); + + if (profilePosParam != null) { + // Merge Signature Position + return new TablePos(profilePosParam, signaturePos); + + } else if (signaturePos != null){ + // Fallback to signature Position! + return signaturePos; + + } else { + return new TablePos(); + + } + } + } + + private String getSignatureFieldNameConfig(PDFBOXObject pdfObject) { + return pdfObject.getStatus().getSettings().getValue(SIGNATURE_FIELD_NAME); + } + private int getParentTreeNextKey(PDStructureTreeRoot structureTreeRoot) throws IOException { int nextKey = structureTreeRoot.getParentTreeNextKey(); if (nextKey < 0) { @@ -717,31 +694,31 @@ public class PADESPDFBOXSigner implements IPdfSigner, IConfigurationConstants { document.close(); result = document.getResult(); - logger.info("PDF-A Validation Result: " + result.isValid()); + log.info("PDF-A Validation Result: " + result.isValid()); if (result.getErrorsList().size() > 0) { - logger.error("The following validation errors occured for PDF-A validation"); + log.error("The following validation errors occured for PDF-A validation"); } for (final ValidationResult.ValidationError ve : result.getErrorsList()) { - logger.error("\t" + ve.getErrorCode() + ": " + ve.getDetails()); + log.error("\t" + ve.getErrorCode() + ": " + ve.getDetails()); } if (!result.isValid()) { - logger.info("The file is not a valid PDF-A document"); + log.info("The file is not a valid PDF-A document"); } } catch (final SyntaxValidationException e) { - logger.error("The file is syntactically invalid.", e); + log.error("The file is syntactically invalid.", e); throw new PdfAsException("Resulting PDF Document is syntactically invalid."); } catch (final ValidationException e) { - logger.error("The file is not a valid PDF-A document.", e); + log.error("The file is not a valid PDF-A document.", e); } catch (final IOException e) { - logger.error("An IOException (" + e.getMessage() + log.error("An IOException (" + e.getMessage() + ") occurred, while validating the PDF-A conformance", e); throw new PdfAsException("Failed validating PDF Document IOException."); } catch (final RuntimeException e) { - logger.debug("An RuntimeException (" + e.getMessage() + log.debug("An RuntimeException (" + e.getMessage() + ") occurred, while validating the PDF-A conformance", e); throw new PdfAsException("Failed validating PDF Document RuntimeException."); } finally { @@ -876,10 +853,10 @@ public class PADESPDFBOXSigner implements IPdfSigner, IConfigurationConstants { return cutOut; } catch (final PdfAsException e) { - logger.warn("PDF-AS Exception", e); + log.warn("PDF-AS Exception", e); throw ErrorExtractor.searchPdfAsError(e, status); } catch (final Throwable e) { - logger.warn("Unexpected Throwable Exception", e); + log.warn("Unexpected Throwable Exception", e); throw ErrorExtractor.searchPdfAsError(e, status); } } @@ -897,7 +874,7 @@ public class PADESPDFBOXSigner implements IPdfSigner, IConfigurationConstants { if (pdfaIdentificationSchema != null) { final Integer pdfaversion = pdfaIdentificationSchema.getPart(); final String conformance = pdfaIdentificationSchema.getConformance(); - logger.info("Detected PDF/A Version: {} - {}", pdfaversion, conformance); + log.info("Detected PDF/A Version: {} - {}", pdfaversion, conformance); if (pdfaversion != null) { return String.valueOf(pdfaversion); @@ -906,7 +883,7 @@ public class PADESPDFBOXSigner implements IPdfSigner, IConfigurationConstants { } } } catch (final Throwable e) { - logger.warn("Failed to determine PDF/A Version!", e); + log.warn("Failed to determine PDF/A Version!", e); } return null; } @@ -931,74 +908,7 @@ public class PADESPDFBOXSigner implements IPdfSigner, IConfigurationConstants { } return signature; } - - private List existingSignatureLocations(PDDocument doc) { - final List existingLocations = new ArrayList<>(); - try { - final List pdSignatureList = doc.getSignatureDictionaries(); - if (pdSignatureList.size() != 0) { - for (final PDSignature sig : pdSignatureList) { - existingLocations.add(sig.getLocation()); - } - } - } catch (final IOException e) { - e.printStackTrace(); - } - return existingLocations; - } - - // find first placeholder_id - public SignaturePlaceholderData checkAvailablePlaceholders(List placeholders, - List existingPlaceholders) { - SignaturePlaceholderData result = null; - - if (placeholders != null) { - for (int i = 0; i < placeholders.size(); ++i) { - // take smallest id - if (!existingPlaceholders.contains(placeholders.get(i).getPlaceholderName())) { - final SignaturePlaceholderData spd = placeholders.get(i); - if (spd.getId() != null) { - if (result == null) { - result = spd; - } else { - try { - final int currentID = Integer.parseInt(result.getId()); - final int testID = Integer.parseInt(spd.getId()); - if (testID < currentID) { - result = spd; - } - } catch (final Exception e) { - // fallback to string compare - final String currentID = result.getId(); - final String testID = spd.getId(); - if (testID.compareToIgnoreCase(currentID) < 0) { - result = spd; - } - } - } - } - } - } - } - return result; - } - - // find first placeholder_id - public List listAvailablePlaceholders(List placeholders, - List existingPlaceholders) { - final List result = new ArrayList<>(); - - if (placeholders != null) { - for (int i = 0; i < placeholders.size(); ++i) { - // take smallest id - if (!existingPlaceholders.contains(placeholders.get(i).getPlaceholderName())) { - result.add(placeholders.get(i)); - } - } - } - return result; - } - + static Map getNumberTreeAsMap(PDNumberTreeNode tree) throws IOException { Map numbers = tree.getNumbers(); diff --git a/pdf-as-pdfbox-2/src/test/java/at/gv/egiz/pdfas/lib/testpdfbox/PDFBoxPlaceholderExtractorTest.java b/pdf-as-pdfbox-2/src/test/java/at/gv/egiz/pdfas/lib/testpdfbox/PDFBoxPlaceholderExtractorTest.java new file mode 100644 index 00000000..fbe3cdea --- /dev/null +++ b/pdf-as-pdfbox-2/src/test/java/at/gv/egiz/pdfas/lib/testpdfbox/PDFBoxPlaceholderExtractorTest.java @@ -0,0 +1,51 @@ +package at.gv.egiz.pdfas.lib.testpdfbox; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.io.IOException; +import java.util.List; + +import org.apache.pdfbox.pdmodel.PDDocument; +import org.junit.Test; + +import at.gv.egiz.pdfas.lib.impl.pdfbox2.placeholder.SignatureFieldsAndPlaceHolderExtractor; +import at.gv.egiz.pdfas.lib.impl.placeholder.SignaturePlaceholderData; +import lombok.SneakyThrows; + +public class PDFBoxPlaceholderExtractorTest { + + @Test + @SneakyThrows + public void nextPlaceholder() { + SignaturePlaceholderData result = getNextSignaturePlaceHolder("/data/platzhalter_en_de_test.pdf"); + assertEquals("Im48", result.getPlaceholderName()); + + } + + @Test + @SneakyThrows + public void allPlaceHolders() { + List listOfPlaceHolders = getPlaceHolders("/data/platzhalter_en_de_test.pdf"); + assertNotNull(listOfPlaceHolders); + + } + + private static List getPlaceHolders(String filePath) throws IOException { + final PDDocument doc = PDDocument.load(PDFBoxPlaceholderExtractorTest.class.getResourceAsStream( + filePath)); + final List results = SignatureFieldsAndPlaceHolderExtractor.findEmptySignatureFields(doc); + return results; + + } + + private static SignaturePlaceholderData getNextSignaturePlaceHolder(String filePath) throws IOException { + final PDDocument doc = PDDocument.load(PDFBoxPlaceholderExtractorTest.class.getResourceAsStream( + filePath)); + final SignaturePlaceholderData result = + SignatureFieldsAndPlaceHolderExtractor.getNextUnusedSignaturePlaceHolder(doc); + return result; + + } + +} diff --git a/pdf-as-pdfbox-2/src/test/java/at/gv/egiz/pdfas/lib/testpdfbox/SignatureFieldsAndPlaceHolderExtractorTest.java b/pdf-as-pdfbox-2/src/test/java/at/gv/egiz/pdfas/lib/testpdfbox/SignatureFieldsAndPlaceHolderExtractorTest.java index 0d85c82b..61a1199d 100644 --- a/pdf-as-pdfbox-2/src/test/java/at/gv/egiz/pdfas/lib/testpdfbox/SignatureFieldsAndPlaceHolderExtractorTest.java +++ b/pdf-as-pdfbox-2/src/test/java/at/gv/egiz/pdfas/lib/testpdfbox/SignatureFieldsAndPlaceHolderExtractorTest.java @@ -1,16 +1,17 @@ package at.gv.egiz.pdfas.lib.testpdfbox; -import at.gv.egiz.pdfas.lib.impl.pdfbox2.placeholder.SignatureFieldsAndPlaceHolderExtractor; -import at.gv.egiz.pdfas.lib.impl.placeholder.SignaturePlaceholderData; -import org.apache.pdfbox.pdmodel.PDDocument; -import org.junit.Assert; -import org.junit.Test; - import java.io.File; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import org.apache.pdfbox.pdmodel.PDDocument; +import org.junit.Assert; +import org.junit.Test; + +import at.gv.egiz.pdfas.lib.impl.pdfbox2.placeholder.SignatureFieldsAndPlaceHolderExtractor; +import at.gv.egiz.pdfas.lib.impl.placeholder.SignaturePlaceholderData; + public class SignatureFieldsAndPlaceHolderExtractorTest { public String getPath(String resourceName) { @@ -48,6 +49,14 @@ public class SignatureFieldsAndPlaceHolderExtractorTest { SignaturePlaceholderData result = getNextSignaturePlaceHolder(getPath("manySignFields.pdf")); Assert.assertEquals(null,result); } + + @Test + public void firstQrCodeOnUnsignedDoc() { + SignaturePlaceholderData result = getNextSignaturePlaceHolder(getPath("new_qr_2-2.pdf")); + Assert.assertEquals("Image5",result.getPlaceholderName()); + + } + @Test public void subsequentCalls(){ SignaturePlaceholderData result = getNextSignaturePlaceHolder(getPath("new_qr_2_signed_signed_signed.pdf")); diff --git a/pdf-as-pdfbox-2/src/test/resources/data/platzhalter_en_de_test.pdf b/pdf-as-pdfbox-2/src/test/resources/data/platzhalter_en_de_test.pdf new file mode 100644 index 00000000..06b9aa0e Binary files /dev/null and b/pdf-as-pdfbox-2/src/test/resources/data/platzhalter_en_de_test.pdf differ diff --git a/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/helper/PdfAsHelper.java b/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/helper/PdfAsHelper.java index b29228c0..3ac91cab 100644 --- a/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/helper/PdfAsHelper.java +++ b/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/helper/PdfAsHelper.java @@ -536,7 +536,8 @@ public class PdfAsHelper { // set Signature Position signParameter.setSignaturePosition(documentToSign.getPosition()); - + signParameter.setPlaceHolderId(documentToSign.getPlaceHolderId()); + // Set Preprocessor if (coreParams.getPreprocessor() != null) { signParameter.setPreprocessorArguments(coreParams.getPreprocessor()); @@ -660,7 +661,8 @@ public class PdfAsHelper { // set Signature Position signParameter.setSignaturePosition(pdfToSign.getPosition()); - + signParameter.setPlaceHolderId(pdfToSign.getPlaceHolderId()); + signParameter.setDynamicSignatureBlockArguments(coreSignParams.getSignatureBlockParameters()); return pdfAs.startSign(signParameter); diff --git a/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/servlets/ExternSignServlet.java b/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/servlets/ExternSignServlet.java index 898e44e2..b9f3f3de 100644 --- a/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/servlets/ExternSignServlet.java +++ b/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/servlets/ExternSignServlet.java @@ -46,10 +46,8 @@ import at.gv.egiz.pdfas.common.exceptions.PDFASError; import at.gv.egiz.pdfas.common.exceptions.PdfAsException; import at.gv.egiz.pdfas.common.exceptions.PdfAsSettingsValidationException; import at.gv.egiz.pdfas.common.settings.ISettings; -import at.gv.egiz.pdfas.lib.api.IConfigurationConstants; import at.gv.egiz.pdfas.lib.api.PdfAsFactory; import at.gv.egiz.pdfas.lib.api.verify.VerifyParameter.SignatureVerificationLevel; -import at.gv.egiz.pdfas.lib.impl.configuration.PlaceholderWebConfiguration; import at.gv.egiz.pdfas.web.config.WebConfiguration; import at.gv.egiz.pdfas.web.exception.PdfAsWebException; import at.gv.egiz.pdfas.web.filter.UserAgentFilter; @@ -346,20 +344,6 @@ public class ExternSignServlet extends HttpServlet { String responseMode = PdfAsParameterExtractor.getResonseMode(request); PdfAsHelper.setResponseMode(request, response, responseMode); - - //read and set placholder web id - try{ - String placeholder_id = PdfAsParameterExtractor.getPlaceholderId(request); - if(org.apache.commons.lang3.StringUtils.isNotEmpty(placeholder_id)) { - PlaceholderWebConfiguration.setValue(IConfigurationConstants.PLACEHOLDER_WEB_ID, placeholder_id); - } else { - PlaceholderWebConfiguration.clear(); - } - - } catch(Exception e) { - log.error(e.getLocalizedMessage()); - } - String filename = PdfAsParameterExtractor.getFilename(request); if(filename != null) { log.debug("Setting Filename in session: " + filename); @@ -395,6 +379,7 @@ public class ExternSignServlet extends HttpServlet { document.setInputData(pdfData); document.setPosition(PdfAsHelper.buildPosString(request, response)); document.setProfile(PdfAsParameterExtractor.getSigType(request)); + document.setPlaceHolderId(PdfAsParameterExtractor.getPlaceholderId(request)); document.setQrCodeContent(qrcodeContent); document.setFileName(PdfAsHelper.getPDFFileName(request)); data.addDocumentToSign(document); -- cgit v1.2.3