diff options
Diffstat (limited to 'pdf-as-lib/src')
7 files changed, 241 insertions, 4 deletions
diff --git a/pdf-as-lib/src/configuration/cfg/logback.xml b/pdf-as-lib/src/configuration/cfg/logback.xml new file mode 100644 index 00000000..3665f8f0 --- /dev/null +++ b/pdf-as-lib/src/configuration/cfg/logback.xml @@ -0,0 +1,16 @@ +<?xml version="1.0" encoding="UTF-8"?> +<configuration> + + <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> + <layout class="ch.qos.logback.classic.PatternLayout"> + <Pattern>%date{ISO8601} [%thread] %-5level %class - %msg%n</Pattern> + </layout> + </appender> + + <logger name="at.gv.egiz" level="INFO"/> + <logger name="at.knowcenter" level="INFO"/> + + <root level="INFO"> + <appender-ref ref="STDOUT" /> + </root> +</configuration>
\ No newline at end of file diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/api/preprocessor/PreProcessor.java b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/api/preprocessor/PreProcessor.java index a70d31d7..1621843a 100644 --- a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/api/preprocessor/PreProcessor.java +++ b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/api/preprocessor/PreProcessor.java @@ -1,6 +1,7 @@ package at.gv.egiz.pdfas.lib.api.preprocessor; import at.gv.egiz.pdfas.common.exceptions.PDFASError; +import at.gv.egiz.pdfas.lib.api.Configuration; import at.gv.egiz.pdfas.lib.api.sign.SignParameter; import at.gv.egiz.pdfas.lib.api.verify.VerifyParameter; @@ -10,7 +11,15 @@ import at.gv.egiz.pdfas.lib.api.verify.VerifyParameter; public interface PreProcessor { /** - * Sign. + * Initialize this instance. This Method is called once when the + * PreProcessor is constructed! + * + * @param configuration the configuration + */ + public void initialize(Configuration configuration); + + /** + * Sign. This Method is called once for each sign call. * * @param parameter the parameter * @throws PDFASError the PDFAS error @@ -18,7 +27,7 @@ public interface PreProcessor { public void sign(SignParameter parameter) throws PDFASError; /** - * Verify. + * Verify. This Method is called once for each verify call. * * @param parameter the parameter * @throws PDFASError the PDFAS error @@ -27,9 +36,20 @@ public interface PreProcessor { /** - * Registration position. - * + * The Position in which the PreProcessor should be registered. + * Lowest positions will be executed first. + * + * Negative values mean that PDF-AS chooses where to register the PreProcessor + * This should be used if the PreProcessor has no dependencies. + * * @return the int */ public int registrationPosition(); + + /** + * Gets the name. + * + * @return the name + */ + public String getName(); } diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/PdfAsImpl.java b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/PdfAsImpl.java index a94f63ad..85a11d0d 100644 --- a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/PdfAsImpl.java +++ b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/PdfAsImpl.java @@ -34,6 +34,7 @@ import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Calendar; +import java.util.Iterator; import java.util.List; import org.apache.commons.io.IOUtils; @@ -61,6 +62,7 @@ import at.gv.egiz.pdfas.lib.api.Configuration; import at.gv.egiz.pdfas.lib.api.IConfigurationConstants; import at.gv.egiz.pdfas.lib.api.PdfAs; import at.gv.egiz.pdfas.lib.api.StatusRequest; +import at.gv.egiz.pdfas.lib.api.preprocessor.PreProcessor; import at.gv.egiz.pdfas.lib.api.sign.SignParameter; import at.gv.egiz.pdfas.lib.api.sign.SignResult; import at.gv.egiz.pdfas.lib.api.verify.VerifyParameter; @@ -68,6 +70,7 @@ import at.gv.egiz.pdfas.lib.api.verify.VerifyResult; import at.gv.egiz.pdfas.lib.impl.configuration.ConfigurationImpl; import at.gv.egiz.pdfas.lib.impl.configuration.SignatureProfileConfiguration; import at.gv.egiz.pdfas.lib.impl.positioning.Positioning; +import at.gv.egiz.pdfas.lib.impl.preprocessor.PreProcessorLoader; import at.gv.egiz.pdfas.lib.impl.signing.IPdfSigner; import at.gv.egiz.pdfas.lib.impl.signing.PdfSignerFactory; import at.gv.egiz.pdfas.lib.impl.signing.pdfbox.PdfboxSignerWrapper; @@ -155,6 +158,9 @@ public class PdfAsImpl implements PdfAs, IConfigurationConstants, throw new PdfAsSettingsException("Invalid settings object!"); } + // execute pre Processors + signPreProcessing(parameter); + ISettings settings = (ISettings) parameter.getConfiguration(); status = new OperationStatus(settings, parameter); @@ -217,6 +223,9 @@ public class PdfAsImpl implements PdfAs, IConfigurationConstants, verifyVerifyParameter(parameter); + // execute pre Processors + verifyPreProcessing(parameter); + int signatureToVerify = parameter.getWhichSignature(); int currentSignature = 0; PDDocument doc = null; @@ -369,6 +378,9 @@ public class PdfAsImpl implements PdfAs, IConfigurationConstants, throw new PdfAsSettingsException("Invalid settings object!"); } + // execute pre Processors + signPreProcessing(parameter); + ISettings settings = (ISettings) parameter.getConfiguration(); OperationStatus status = new OperationStatus(settings, parameter); @@ -502,6 +514,56 @@ public class PdfAsImpl implements PdfAs, IConfigurationConstants, } } + private void listPreProcessors(List<PreProcessor> preProcessors) { + logger.debug("--------------"); + logger.debug("Listing PreProcessors:"); + + Iterator<PreProcessor> preProcessorsIterator = preProcessors.iterator(); + int idx = 0; + while(preProcessorsIterator.hasNext()) { + PreProcessor preProcessor = preProcessorsIterator.next(); + logger.debug("{}: {} [{}]", idx, preProcessor.getName(), preProcessor.getClass().getName()); + idx++; + } + logger.debug("--------------"); + } + + private void verifyPreProcessing(VerifyParameter parameter) throws PDFASError { + List<PreProcessor> preProcessors = PreProcessorLoader.getPreProcessors(parameter.getConfiguration()); + + listPreProcessors(preProcessors); + + logger.debug("executing PreProcessors for verifing:"); + Iterator<PreProcessor> preProcessorsIterator = preProcessors.iterator(); + + while(preProcessorsIterator.hasNext()) { + PreProcessor preProcessor = preProcessorsIterator.next(); + logger.debug("executing: {} [{}]", preProcessor.getName(), preProcessor.getClass().getName()); + preProcessor.verify(parameter); + logger.debug("done executing: {} [{}]", preProcessor.getName(), preProcessor.getClass().getName()); + } + + logger.debug("executing PreProcessors for verifing done"); + } + + private void signPreProcessing(SignParameter parameter) throws PDFASError { + List<PreProcessor> preProcessors = PreProcessorLoader.getPreProcessors(parameter.getConfiguration()); + + listPreProcessors(preProcessors); + + logger.debug("executing PreProcessors for signing:"); + Iterator<PreProcessor> preProcessorsIterator = preProcessors.iterator(); + + while(preProcessorsIterator.hasNext()) { + PreProcessor preProcessor = preProcessorsIterator.next(); + logger.debug("executing: {} [{}]", preProcessor.getName(), preProcessor.getClass().getName()); + preProcessor.sign(parameter); + logger.debug("done executing: {} [{}]", preProcessor.getName(), preProcessor.getClass().getName()); + } + + logger.debug("executing PreProcessors for signing done"); + } + private SignResult createSignResult(OperationStatus status) throws IOException { // ================================================================ diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/preprocessor/DebugPreProcessor.java b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/preprocessor/DebugPreProcessor.java new file mode 100644 index 00000000..1a262aca --- /dev/null +++ b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/preprocessor/DebugPreProcessor.java @@ -0,0 +1,69 @@ +package at.gv.egiz.pdfas.lib.impl.preprocessor; + +import java.util.Iterator; +import java.util.Map; +import java.util.Map.Entry; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import at.gv.egiz.pdfas.common.exceptions.PDFASError; +import at.gv.egiz.pdfas.lib.api.Configuration; +import at.gv.egiz.pdfas.lib.api.preprocessor.PreProcessor; +import at.gv.egiz.pdfas.lib.api.sign.SignParameter; +import at.gv.egiz.pdfas.lib.api.verify.VerifyParameter; + +public class DebugPreProcessor implements PreProcessor { + + private static final Logger logger = LoggerFactory + .getLogger(DebugPreProcessor.class); + + private static final String NAME = "Debug PreProcessor"; + + @Override + public void initialize(Configuration configuration) { + logger.debug("Initializing {}", getName()); + } + + private void listPPArguments(Map<String, String> map) { + if(map != null) { + logger.debug("pre processor arguments:"); + Iterator<Entry<String, String>> entryIt = map.entrySet().iterator(); + while(entryIt.hasNext()) { + Entry<String, String> entry = entryIt.next(); + logger.debug(" {} => {}", entry.getKey(), entry.getValue()); + } + } else { + logger.debug("No pre processor arguments"); + } + } + + @Override + public void sign(SignParameter parameter) throws PDFASError { + logger.debug("preprocessor signing ..."); + + listPPArguments(parameter.getPreprocessorArguments()); + + logger.debug("preprocessor signing done"); + } + + @Override + public void verify(VerifyParameter parameter) throws PDFASError { + logger.debug("preprocessor verifing ..."); + + listPPArguments(parameter.getPreprocessorArguments()); + + logger.debug("preprocessor verifing done"); + } + + @Override + public int registrationPosition() { + return -1; + } + + @Override + public String getName() { + return NAME; + } + +} diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/preprocessor/PreProcessorCompare.java b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/preprocessor/PreProcessorCompare.java new file mode 100644 index 00000000..ef716403 --- /dev/null +++ b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/preprocessor/PreProcessorCompare.java @@ -0,0 +1,25 @@ +package at.gv.egiz.pdfas.lib.impl.preprocessor; + +import java.util.Comparator; + +import at.gv.egiz.pdfas.lib.api.preprocessor.PreProcessor; + +public class PreProcessorCompare implements Comparator<PreProcessor> { + + @Override + public int compare(PreProcessor o1, PreProcessor o2) { + if(o1.registrationPosition() < 0 && o2.registrationPosition() < 0) { + // equal + return 0; + } else if(o1.registrationPosition() < 0) { + // o2 vor o1 + return 1; + } else if(o2.registrationPosition() < 0) { + // o1 vor o2 + return -1; + } else { + return Integer.compare(o1.registrationPosition(), o2.registrationPosition()); + } + } + +} diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/preprocessor/PreProcessorLoader.java b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/preprocessor/PreProcessorLoader.java new file mode 100644 index 00000000..91218c82 --- /dev/null +++ b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/preprocessor/PreProcessorLoader.java @@ -0,0 +1,44 @@ +package at.gv.egiz.pdfas.lib.impl.preprocessor; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; +import java.util.ServiceLoader; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import at.gv.egiz.pdfas.lib.api.Configuration; +import at.gv.egiz.pdfas.lib.api.preprocessor.PreProcessor; + +public class PreProcessorLoader { + + private static final Logger logger = LoggerFactory + .getLogger(PreProcessorLoader.class); + + private static ServiceLoader<PreProcessor> preProcessorLoader = ServiceLoader.load(PreProcessor.class); + + public static List<PreProcessor> getPreProcessors(Configuration configuration) { + logger.debug("building PreProcessors"); + + List<PreProcessor> processors = new ArrayList<PreProcessor>(); + Iterator<PreProcessor> processorIterator = preProcessorLoader.iterator(); + + while(processorIterator.hasNext()) { + PreProcessor preProcessor = processorIterator.next(); + logger.debug("Loading " + preProcessor.getName() + " [" + preProcessor.getClass().getName() + "]"); + preProcessor.initialize(configuration); + logger.debug("Initialized " + preProcessor.getName()); + processors.add(preProcessor); + logger.debug("Preprocessor added " + preProcessor.getName()); + } + + logger.debug("PreProcessors constructed"); + + Collections.sort(processors, new PreProcessorCompare()); + + logger.debug("PreProcessors sorted"); + return processors; + } +} diff --git a/pdf-as-lib/src/main/resources/META-INF/services/at.gv.egiz.pdfas.lib.api.preprocessor.PreProcessor b/pdf-as-lib/src/main/resources/META-INF/services/at.gv.egiz.pdfas.lib.api.preprocessor.PreProcessor new file mode 100644 index 00000000..d9d2cd96 --- /dev/null +++ b/pdf-as-lib/src/main/resources/META-INF/services/at.gv.egiz.pdfas.lib.api.preprocessor.PreProcessor @@ -0,0 +1 @@ +at.gv.egiz.pdfas.lib.impl.preprocessor.DebugPreProcessor
\ No newline at end of file |