From 7b105bafe5a1f4c01224c8ae0b820a478b09447f Mon Sep 17 00:00:00 2001 From: Andreas Fitzek Date: Thu, 16 Oct 2014 10:59:52 +0200 Subject: added basic Preprocessor infrastructure, moved logging to logback --- pdf-as-cli/src/main/resources/logback.xml | 16 +++++ pdf-as-common/build.gradle | 2 +- pdf-as-lib/build.gradle | 2 +- pdf-as-lib/src/configuration/cfg/logback.xml | 16 +++++ .../pdfas/lib/api/preprocessor/PreProcessor.java | 28 +++++++-- .../java/at/gv/egiz/pdfas/lib/impl/PdfAsImpl.java | 62 +++++++++++++++++++ .../lib/impl/preprocessor/DebugPreProcessor.java | 69 ++++++++++++++++++++++ .../lib/impl/preprocessor/PreProcessorCompare.java | 25 ++++++++ .../lib/impl/preprocessor/PreProcessorLoader.java | 44 ++++++++++++++ ...gv.egiz.pdfas.lib.api.preprocessor.PreProcessor | 1 + pdf-as-moa/build.gradle | 1 - pdf-as-tests/build.gradle | 1 - pdf-as-web/build.gradle | 2 - 13 files changed, 259 insertions(+), 10 deletions(-) create mode 100644 pdf-as-cli/src/main/resources/logback.xml create mode 100644 pdf-as-lib/src/configuration/cfg/logback.xml create mode 100644 pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/preprocessor/DebugPreProcessor.java create mode 100644 pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/preprocessor/PreProcessorCompare.java create mode 100644 pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/preprocessor/PreProcessorLoader.java create mode 100644 pdf-as-lib/src/main/resources/META-INF/services/at.gv.egiz.pdfas.lib.api.preprocessor.PreProcessor diff --git a/pdf-as-cli/src/main/resources/logback.xml b/pdf-as-cli/src/main/resources/logback.xml new file mode 100644 index 00000000..102931af --- /dev/null +++ b/pdf-as-cli/src/main/resources/logback.xml @@ -0,0 +1,16 @@ + + + + + + %date{ISO8601} [%thread] %-5level %class - %msg%n + + + + + + + + + + \ No newline at end of file diff --git a/pdf-as-common/build.gradle b/pdf-as-common/build.gradle index 63335233..a2e1ee13 100644 --- a/pdf-as-common/build.gradle +++ b/pdf-as-common/build.gradle @@ -17,7 +17,7 @@ task releases(type: Copy) { } dependencies { - compile group: 'org.slf4j', name: 'slf4j-log4j12', version: '1.7.5' + compile group: 'org.slf4j', name: 'slf4j-api', version: slf4jVersion compile group: 'org.apache.pdfbox', name: 'pdfbox', version: '1.8.5' compile group: 'commons-collections', name: 'commons-collections', version: '3.2' compile group: 'commons-io', name: 'commons-io', version: '2.4' diff --git a/pdf-as-lib/build.gradle b/pdf-as-lib/build.gradle index e82bbc7b..5d2b8689 100644 --- a/pdf-as-lib/build.gradle +++ b/pdf-as-lib/build.gradle @@ -66,7 +66,7 @@ dependencies { compile files('libs/iaik_eccelerate.jar') compile files('libs/iaik_jce_full.jar') compile files('libs/iaik_cms.jar') - compile group: 'log4j', name: 'log4j', version: '1.2.17' + compile 'ch.qos.logback:logback-classic:1.1.2' compile group: 'org.slf4j', name: 'slf4j-api', version: slf4jVersion compile group: 'com.google.zxing', name: 'core', version: '3.1.0' compile group: 'com.google.zxing', name: 'javase', version: '3.1.0' 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 @@ + + + + + + %date{ISO8601} [%thread] %-5level %class - %msg%n + + + + + + + + + + \ 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 preProcessors) { + logger.debug("--------------"); + logger.debug("Listing PreProcessors:"); + + Iterator 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 preProcessors = PreProcessorLoader.getPreProcessors(parameter.getConfiguration()); + + listPreProcessors(preProcessors); + + logger.debug("executing PreProcessors for verifing:"); + Iterator 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 preProcessors = PreProcessorLoader.getPreProcessors(parameter.getConfiguration()); + + listPreProcessors(preProcessors); + + logger.debug("executing PreProcessors for signing:"); + Iterator 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 map) { + if(map != null) { + logger.debug("pre processor arguments:"); + Iterator> entryIt = map.entrySet().iterator(); + while(entryIt.hasNext()) { + Entry 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 { + + @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 preProcessorLoader = ServiceLoader.load(PreProcessor.class); + + public static List getPreProcessors(Configuration configuration) { + logger.debug("building PreProcessors"); + + List processors = new ArrayList(); + Iterator 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 diff --git a/pdf-as-moa/build.gradle b/pdf-as-moa/build.gradle index c00c456b..0ae64724 100644 --- a/pdf-as-moa/build.gradle +++ b/pdf-as-moa/build.gradle @@ -46,7 +46,6 @@ repositories { dependencies { compile project (':pdf-as-lib') - compile group: 'log4j', name: 'log4j', version: '1.2.17' compile group: 'org.slf4j', name: 'slf4j-api', version: slf4jVersion testCompile group: 'junit', name: 'junit', version: '4.+' ws group: 'org.apache.cxf', name: 'cxf-tools', version: cxfVersion diff --git a/pdf-as-tests/build.gradle b/pdf-as-tests/build.gradle index 6d964735..4877f404 100644 --- a/pdf-as-tests/build.gradle +++ b/pdf-as-tests/build.gradle @@ -19,7 +19,6 @@ dependencies { testCompile project (':pdf-as-moa') compile project (':signature-standards:sigs-pkcs7detached') compile project (':signature-standards:sigs-pades') - compile group: 'log4j', name: 'log4j', version: '1.2.17' compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.7' testCompile group: 'junit', name: 'junit', version: '4.+' testCompile group: 'org.apache.pdfbox', name: 'preflight', version: '1.8.7' diff --git a/pdf-as-web/build.gradle b/pdf-as-web/build.gradle index d854a365..15fe8b3d 100644 --- a/pdf-as-web/build.gradle +++ b/pdf-as-web/build.gradle @@ -21,8 +21,6 @@ dependencies { compile project (':signature-standards:sigs-pkcs7detached') compile project (':signature-standards:sigs-pades') compile group: 'commons-fileupload', name: 'commons-fileupload', version: '1.3.1' - compile group: 'org.slf4j', name: 'slf4j-log4j12', version: '1.7.7' - compile 'org.slf4j:slf4j-log4j12:1.7.7' compile group: 'opensymphony', name: 'sitemesh', version: '2.4.2' compile "commons-codec:commons-codec:1.9" compile 'org.apache.commons:commons-lang3:3.3.2' -- cgit v1.2.3