From db52e4d66d60184d53a27ba4d6772461daacc03d Mon Sep 17 00:00:00 2001 From: tknall Date: Fri, 22 Mar 2013 08:57:51 +0000 Subject: Maintenance update (bugfixes, new features, cleanup...) Refer to /dok/RELEASE_NOTES-3.3.txt for further information. git-svn-id: https://joinup.ec.europa.eu/svn/pdf-as/pdf-as/trunk@931 7b5415b0-85f9-ee4d-85bd-d5d0c3b42d1c --- .../at/gv/egiz/pdfas/impl/api/PdfAsObject.java | 441 +++++++++++++++------ 1 file changed, 322 insertions(+), 119 deletions(-) (limited to 'pdf-as-lib/src/main/java/at/gv/egiz/pdfas/impl/api/PdfAsObject.java') diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/impl/api/PdfAsObject.java b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/impl/api/PdfAsObject.java index eda94c0..a2bcd15 100644 --- a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/impl/api/PdfAsObject.java +++ b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/impl/api/PdfAsObject.java @@ -33,6 +33,7 @@ import java.util.Properties; import java.util.Vector; import org.apache.commons.lang.math.NumberUtils; +import org.apache.commons.lang.time.StopWatch; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -66,6 +67,9 @@ import at.gv.egiz.pdfas.framework.DataSourceHolder; import at.gv.egiz.pdfas.framework.config.SettingsHelper; import at.gv.egiz.pdfas.framework.input.ExtractionStage; import at.gv.egiz.pdfas.framework.input.PdfDataSource; +import at.gv.egiz.pdfas.framework.logging.StatisticData; +import at.gv.egiz.pdfas.framework.logging.StatisticLogFactory; +import at.gv.egiz.pdfas.framework.logging.StatisticLogger; import at.gv.egiz.pdfas.framework.signator.Signator; import at.gv.egiz.pdfas.framework.signator.SignatorInformation; import at.gv.egiz.pdfas.framework.vfilter.VerificationFilterParameters; @@ -87,6 +91,7 @@ import at.gv.egiz.pdfas.placeholder.SignaturePlaceholderContext; import at.gv.egiz.pdfas.placeholder.SignaturePlaceholderData; import at.gv.egiz.pdfas.placeholder.SignaturePlaceholderExtractor; import at.gv.egiz.pdfas.utils.ConfigUtils; +import at.gv.egiz.pdfas.utils.PDFASUtils; import at.knowcenter.wag.egov.egiz.PdfAS; import at.knowcenter.wag.egov.egiz.PdfASID; import at.knowcenter.wag.egov.egiz.cfg.OverridePropertyHolder; @@ -111,12 +116,13 @@ import at.knowcenter.wag.egov.egiz.sig.SignatureDataImpl; import at.knowcenter.wag.egov.egiz.sig.SignatureResponse; import at.knowcenter.wag.egov.egiz.sig.SignatureTypeDefinition; import at.knowcenter.wag.egov.egiz.sig.SignatureTypes; +import at.knowcenter.wag.egov.egiz.sig.SignatureTypes.State; import at.knowcenter.wag.egov.egiz.sig.connectors.Connector; import at.knowcenter.wag.egov.egiz.sig.connectors.bku.SignSignatureObject; /** * Implementation of the {@link PdfAs} interface. - * + * * @author wprinz */ public class PdfAsObject implements PdfAs @@ -127,25 +133,26 @@ public class PdfAsObject implements PdfAs * The log. */ private static Log log = LogFactory.getLog(PdfAsObject.class); - + private static StatisticLogger statLog = StatisticLogFactory.getLog(Constants.STATISTIC_LOGGER_NAME); + private static final String ENABLE_PLACEHOLDER_SEARCH_KEY = "enable_placeholder_search"; - + /** * Configuration key for minimal signature block width threshold. Any width below this certain value will lead to a warning log entry." */ private static final String SIGNATURE_BLOCK_WIDTH_THRESHOLD_FOR_WARNING_KEY = "signature_block_width_warning_threshold"; - + /** * Minimal signature block width. If a width below that value is defined (by parameter, by placeholder or by configuration) a warning log entry is created. */ public static final float DEFAULT_SIGNATURE_BLOCK_WIDTH_THRESHOLD = 150; - + /** * This constructor is for internal use only - use * {@link at.gv.egiz.pdfas.PdfAsFactory} instead. * Note: IAIK JCE and IAIK ECC security providers are automatically registered. - * + * * @param workDirectory * The work directory. * @throws PdfAsException @@ -155,16 +162,16 @@ public class PdfAsObject implements PdfAs { this(workDirectory, SettingsReader.REGISTER_IAIK_PROVIDERS_ON_DEFAULT); } - + /** * This constructor is for internal use only - use * {@link at.gv.egiz.pdfas.PdfAsFactory} instead. - * + * * @param workDirectory * The work directory. * @param registerProvider true: automatically registers IAIK JCE and ECC Provider; * false: providers will NOT be automatically registered, providers - * needed have to be registered by the API user + * needed have to be registered by the API user * @throws PdfAsException * Thrown, if the configuration cannot be processed. */ @@ -174,12 +181,12 @@ public class PdfAsObject implements PdfAs SettingsReader.initialize(path, path); reloadConfig(registerProvider); } - + /** * This constructor is for internal use only - use * {@link at.gv.egiz.pdfas.PdfAsFactory} instead. * Note: IAIK JCE and IAIK ECC security providers are automatically registered. - * + * * @throws PdfAsException * Thrown, if the configuration cannot be processed. */ @@ -187,7 +194,7 @@ public class PdfAsObject implements PdfAs { this(null); } - + /** * @see at.gv.egiz.pdfas.api.PdfAs#reloadConfig() */ @@ -197,11 +204,11 @@ public class PdfAsObject implements PdfAs SettingsReader.createInstance(); SignatureTypes.createInstance(); } - + /** * @param registerProvider true: automatically registers IAIK JCE and ECC Provider; * false: providers will NOT be automatically registered, providers - * needed have to be registered by the API user + * needed have to be registered by the API user * @see at.gv.egiz.pdfas.api.PdfAs#reloadConfig() */ private void reloadConfig(boolean registerProvider) throws PdfAsException @@ -228,7 +235,7 @@ public class PdfAsObject implements PdfAs List profileInformation = new ArrayList(profiles.size()); String default_type = settings.getValueFromKey(SignatureTypes.DEFAULT_TYPE); - + Iterator it = profiles.iterator(); while (it.hasNext()) { @@ -240,14 +247,17 @@ public class PdfAsObject implements PdfAs final String profileDescription = settings.getSetting("sig_obj." + profileId + "." + SignatureTypes.SIG_DESCR, null); boolean isDefault = (default_type != null && default_type.equals(profileId)); + + State state = State.fromString(settings.getSetting(SignatureTypes.TYPES + "." + profileId)); + // modified by tknall - SignatureProfileImpl signatureProfile = new SignatureProfileImpl(profileId, profileDescription, moaKeyIdentifier, isDefault); + SignatureProfileImpl signatureProfile = new SignatureProfileImpl(profileId, state, profileDescription, moaKeyIdentifier, isDefault); // start - added by tknall - + // signature entries relevant to the search algorithm Properties signatureEntries = new Properties(); - + // search for table entries String parentPropertyKey = "sig_obj." + profileId + ".table"; log.debug("Looking for subkeys of \"" + parentPropertyKey + "\"."); @@ -275,7 +285,7 @@ public class PdfAsObject implements PdfAs } } } - + // search for table entries parentPropertyKey = "sig_obj." + profileId + ".key"; log.debug("Looking for subkeys of \"" + parentPropertyKey + "\"."); @@ -295,46 +305,89 @@ public class PdfAsObject implements PdfAs } } } - + // set properties signatureProfile.setSignatureBlockEntries(signatureEntries); // stop - added by tknall - + profileInformation.add(signatureProfile); } return profileInformation; } - /** - * @see at.gv.egiz.pdfas.api.PdfAs#sign(at.gv.egiz.pdfas.api.sign.SignParameters) - */ - public SignResult sign(SignParameters signParameters) throws PdfAsException + public SignResult sign(SignParameters signParameters) throws PdfAsException { + // prepare log data + StatisticData sd = new StatisticData(signParameters); + StopWatch sw = new StopWatch(); + try { + sw.start(); + return performSign(signParameters); + } catch (PdfAsException e) { + sd.setException(e); + throw e; + } catch (RuntimeException e) { + sd.setException(e); + throw e; + } finally { + sw.stop(); + sd.setDuration(sw.getTime()); + statLog.log(sd); + } + } + + private SignResult performSign(SignParameters signParameters) throws PdfAsException { CheckHelper.checkSignParameters(signParameters, false); - + + // make sure that the selected profile is allowed to be used for signature + SettingsReader settings = SettingsReader.getInstance(); + if (!State.fromString(settings.getSetting(SignatureTypes.TYPES + "." + signParameters.getSignatureProfileId())) + .canSign()) { + throw new SignatureException(ErrorCode.SIGNATURE_PROFILE_IS_NOT_ALLOWED_FOR_SIGNATURE, + "The signature profile '" + signParameters.getSignatureProfileId() + + "' is not allowed to be used for signature."); + } + try { - SignatureDetailInformation signatorInfo = prepareSign(signParameters); - - return sign(signParameters, signatorInfo); - + + SignatureDetailInformation signatorInfo = performPrepareSign(signParameters); + return performSign(signParameters, signatorInfo); + } catch (java.lang.OutOfMemoryError e) { throw new OutOfMemoryException(ErrorCode.OUT_OF_MEMORY_ERROR, "Insufficient memory allocated to virtual machine. Start Java with parameters \"-Xms128m -Xmx786m -XX:MaxPermSize=256m\".", e); } } - /** - * @see at.gv.egiz.pdfas.api.PdfAs#verify(at.gv.egiz.pdfas.api.verify.VerifyParameters) - */ - public VerifyResults verify(VerifyParameters verifyParameters) throws PdfAsException + public VerifyResults verify(VerifyParameters verifyParameters) throws PdfAsException { + // prepare log data + StatisticData sd = new StatisticData(verifyParameters); + StopWatch sw = new StopWatch(); + try { + sw.start(); + return performVerify(verifyParameters); + } catch (PdfAsException e) { + sd.setException(e); + throw e; + } catch (RuntimeException e) { + sd.setException(e); + throw e; + } finally { + sw.stop(); + sd.setDuration(sw.getTime()); + statLog.log(sd); + } + } + + private VerifyResults performVerify(VerifyParameters verifyParameters) throws PdfAsException { CheckHelper.checkVerifyParameters(verifyParameters); AnalyzeParameters ap = new AnalyzeParameters(); fillAnalyzeParametersWithVerifyParameters(ap, verifyParameters); - AnalyzeResult analyzeResult = analyze(ap); + AnalyzeResult analyzeResult = performAnalyze(ap); if (verifyParameters.getSignatureToVerify() != Constants.VERIFY_ALL) { @@ -352,17 +405,17 @@ public class PdfAsObject implements PdfAs VerifyAfterAnalysisParameters vaap = new VerifyAfterAnalysisParameters(); vaap.setAnalyzeResult(analyzeResult); fillVerifyAfterAnalysisParametersWithVerifyParameters(vaap, verifyParameters); - VerifyResults res = verify(vaap); - + VerifyResults res = performVerify(vaap); + return res; - + } - + /** * Copies all adequate parameters from the {@link VerifyParameters} to the * {@link AnalyzeParameters}. - * + * * @param ap * The {@link AnalyzeParameters}. * @param vp @@ -385,7 +438,7 @@ public class PdfAsObject implements PdfAs /** * Copies all adequate parameters from the {@link VerifyParameters} to the * {@link VerifyAfterAnalysisParameters}. - * + * * @param vaap * The {@link VerifyAfterAnalysisParameters}. * @param vp @@ -406,10 +459,30 @@ public class PdfAsObject implements PdfAs reconstructParams.setSignatureDevice(verifyAfterAnalysisParameters.getSignatureDevice()); } - /** - * @see at.gv.egiz.pdfas.api.PdfAs#analyze(at.gv.egiz.pdfas.api.analyze.AnalyzeParameters) - */ - public AnalyzeResult analyze(AnalyzeParameters analyzeParameters) throws PdfAsException + public AnalyzeResult analyze(AnalyzeParameters analyzeParameters) throws PdfAsException { + // prepare log data + StatisticData sd = new StatisticData(analyzeParameters); + StopWatch sw = new StopWatch(); + try { + sw.start(); + return performAnalyze(analyzeParameters); + } catch (PdfAsException e) { + sd.setException(e); + throw e; + } catch (RuntimeException e) { + sd.setException(e); + throw e; + } finally { + sw.stop(); + sd.setDuration(sw.getTime()); + // only log in case of error + if (sd.isError()) { + statLog.log(sd); + } + } + } + + private AnalyzeResult performAnalyze(AnalyzeParameters analyzeParameters) throws PdfAsException { CheckHelper.checkAnalyzeParameters(analyzeParameters); @@ -429,10 +502,12 @@ public class PdfAsObject implements PdfAs assumeOnlySB = false; } VerificationFilterParameters parameters = new VerificationFilterParametersImpl(binaryOnly, assumeOnlySB, parametersConfig.scanForOldSignatures()); - + at.gv.egiz.pdfas.framework.input.DataSource inputDataSource = null; if (analyzeParameters.getDocument().getMimeType().equals("application/pdf")) { + // check document permissions + PDFASUtils.createPdfReaderCheckingPermissions(analyzeParameters.getDocument()); inputDataSource = new PdfDataSourceAdapter(analyzeParameters.getDocument()); } else @@ -447,13 +522,13 @@ public class PdfAsObject implements PdfAs } } assert inputDataSource != null; - + try { ExtractionStage es = new ExtractionStage(); DataSourceHolder dsh = new DataSourceHolder(inputDataSource); List signature_holders = es.extractSignatureHolders(dsh, parameters); - - + + // List sigInfs = new ArrayList(signature_holders.size()); List sigInfs = new ArrayList(); List noSigs = new ArrayList(); @@ -461,24 +536,24 @@ public class PdfAsObject implements PdfAs while (it.hasNext()) { SignatureHolder sh = (SignatureHolder)it.next(); - + if(sh instanceof NoSignatureHolder) { noSigs.add(sh); } else { - + SignatureInformation si = new SignatureInformationAdapter(sh); sigInfs.add(si); - if (analyzeParameters.isReturnNonTextualObjects()) { - si.setNonTextualObjects(doExtractNonTexualObjects(sh, (PdfDataSource) dsh.getDataSource())); - } - + if (analyzeParameters.isReturnNonTextualObjects()) { + si.setNonTextualObjects(doExtractNonTexualObjects(sh, (PdfDataSource) dsh.getDataSource())); + } + } } return new AnalyzeResultImpl(sigInfs, noSigs, parameters.hasBeenCorrected()); } catch (java.lang.OutOfMemoryError e) { throw new OutOfMemoryException(ErrorCode.OUT_OF_MEMORY_ERROR, "Insufficient memory allocated to virtual machine. Start Java with parameters \"-Xms128m -Xmx786m -XX:MaxPermSize=256m\".", e); } - + } private List doExtractNonTexualObjects(SignatureHolder sh, PdfDataSource pdfDataSource) { @@ -499,30 +574,47 @@ public class PdfAsObject implements PdfAs } } -/** - * @see at.gv.egiz.pdfas.api.PdfAs#verify(at.gv.egiz.pdfas.api.verify.VerifyAfterAnalysisParameters) - */ - public VerifyResults verify(VerifyAfterAnalysisParameters verifyAfterAnalysisParameters) throws PdfAsException + public VerifyResults verify(VerifyAfterAnalysisParameters verifyAfterAnalysisParameters) throws PdfAsException { + // prepare log data + StatisticData sd = new StatisticData(verifyAfterAnalysisParameters); + StopWatch sw = new StopWatch(); + try { + sw.start(); + return performVerify(verifyAfterAnalysisParameters); + } catch (PdfAsException e) { + sd.setException(e); + throw e; + } catch (RuntimeException e) { + sd.setException(e); + throw e; + } finally { + sw.stop(); + sd.setDuration(sw.getTime()); + statLog.log(sd); + } + } + + private VerifyResults performVerify(VerifyAfterAnalysisParameters verifyAfterAnalysisParameters) throws PdfAsException { CheckHelper.checkVerifyAfterAnalysisParameters(verifyAfterAnalysisParameters); List signatures = verifyAfterAnalysisParameters.getAnalyzeResult().getSignatures(); - + // added by tknall if (signatures == null || signatures.isEmpty()) { throw new PDFDocumentException(ErrorCode.DOCUMENT_NOT_SIGNED, "PDF document not signed."); //$NON-NLS-1$ } - + ReconstructXMLDsigAfterAnalysisParameters rxaap = new ReconstructXMLDsigAfterAnalysisParameters(); fillReconstructXMLDsigAfterAnalysisParametersWithVerifyAfterAnalysisParameters(rxaap, verifyAfterAnalysisParameters); - ReconstructXMLDsigResult reconstructResult = reconstructXMLDSIG(rxaap); - + ReconstructXMLDsigResult reconstructResult = performReconstructXMLDSIG(rxaap); + VerifyAfterReconstructXMLDsigParameters varxp = new VerifyAfterReconstructXMLDsigParameters(); fillVerifyAfterReconstructXMLDsigParametersWithVerifyAfterAnalysisParameters(varxp, verifyAfterAnalysisParameters); varxp.setReconstructXMLDsigResult(reconstructResult); - - return verify(varxp); - + + return performVerify(varxp); + } protected void fillVerifyAfterReconstructXMLDsigParametersWithVerifyAfterAnalysisParameters( @@ -534,31 +626,75 @@ public class PdfAsObject implements PdfAs varxp.setVerifySignatureIndex(verifyAfterAnalysisParameters.getVerifySignatureIndex()); } - /** - * @see PdfAs#reconstructXMLDSIG(ReconstructXMLDsigParameters) - */ public ReconstructXMLDsigResult reconstructXMLDSIG( + ReconstructXMLDsigParameters reconstructXMLDsigParameters) + throws PdfAsException { + // prepare log data + StatisticData sd = new StatisticData(reconstructXMLDsigParameters); + StopWatch sw = new StopWatch(); + try { + sw.start(); + return performReconstructXMLDSIG(reconstructXMLDsigParameters); + } catch (PdfAsException e) { + sd.setException(e); + throw e; + } catch (RuntimeException e) { + sd.setException(e); + throw e; + } finally { + sw.stop(); + sd.setDuration(sw.getTime()); + // only log in case of error + if (sd.isError()) { + statLog.log(sd); + } + } + } + + private ReconstructXMLDsigResult performReconstructXMLDSIG( ReconstructXMLDsigParameters reconstructXMLDsigParameters) throws PdfAsException { - + AnalyzeParameters analyzeParameters = new AnalyzeParameters(); fillAnalyzeParametersWithReconstructXMLDsigParameters(analyzeParameters, reconstructXMLDsigParameters); - AnalyzeResult ar = analyze(analyzeParameters); - + AnalyzeResult ar = performAnalyze(analyzeParameters); + ReconstructXMLDsigAfterAnalysisParameters rxaap = new ReconstructXMLDsigAfterAnalysisParameters(); rxaap.setSignatureDevice(reconstructXMLDsigParameters.getSignatureDevice()); rxaap.setAnalyzeResult(ar); - - return reconstructXMLDSIG(rxaap); + + return performReconstructXMLDSIG(rxaap); } - /** - * @see PdfAs#reconstructXMLDSIG(ReconstructXMLDsigAfterAnalysisParameters) - */ public ReconstructXMLDsigResult reconstructXMLDSIG( + ReconstructXMLDsigAfterAnalysisParameters reconstructXMLDsigParameters) + throws PdfAsException { + // prepare log data + StatisticData sd = new StatisticData(reconstructXMLDsigParameters); + StopWatch sw = new StopWatch(); + try { + sw.start(); + return performReconstructXMLDSIG(reconstructXMLDsigParameters); + } catch (PdfAsException e) { + sd.setException(e); + throw e; + } catch (RuntimeException e) { + sd.setException(e); + throw e; + } finally { + sw.stop(); + sd.setDuration(sw.getTime()); + // only log in case of error + if (sd.isError()) { + statLog.log(sd); + } + } + } + + private ReconstructXMLDsigResult performReconstructXMLDSIG( ReconstructXMLDsigAfterAnalysisParameters reconstructXMLDsigParameters) throws PdfAsException { - + AnalyzeResult ar = reconstructXMLDsigParameters.getAnalyzeResult(); List extendedSignatureInfos = new Vector(); for (int i = 0; i < ar.getSignatures().size(); i++) @@ -572,31 +708,52 @@ public class PdfAsObject implements PdfAs // don't care for connector exceptions because of mutli signs. they are handled during verify extendedSignatureInfos.add(new ExtendedSignatureInformation(si, null)); } - + } return new ReconstructXMLDsigResult(extendedSignatureInfos, reconstructXMLDsigParameters.getSignatureDevice()); } + public VerifyResults verify(VerifyAfterReconstructXMLDsigParameters verifyAfterReconstructXMLDsigParameters) + throws PdfAsException { + // prepare log data + StatisticData sd = new StatisticData(verifyAfterReconstructXMLDsigParameters); + StopWatch sw = new StopWatch(); + try { + sw.start(); + return performVerify(verifyAfterReconstructXMLDsigParameters); + } catch (PdfAsException e) { + sd.setException(e); + throw e; + } catch (RuntimeException e) { + sd.setException(e); + throw e; + } finally { + sw.stop(); + sd.setDuration(sw.getTime()); + statLog.log(sd); + } + } + /** * @see PdfAs#verify(VerifyAfterReconstructXMLDsigParameters) */ - public VerifyResults verify( + private VerifyResults performVerify( VerifyAfterReconstructXMLDsigParameters verifyAfterReconstructXMLDsigParameters) throws PdfAsException { - + try { List extSignatures = verifyAfterReconstructXMLDsigParameters.getReconstructXMLDsigResult().getExtendedSignatures(); String signatureDevice = verifyAfterReconstructXMLDsigParameters.getSignatureDevice(); if (signatureDevice == null){ signatureDevice = verifyAfterReconstructXMLDsigParameters.getReconstructXMLDsigResult().getDevice(); } - List results = PdfAS.verifyExtendedSignatureHolders(extSignatures, - signatureDevice, - verifyAfterReconstructXMLDsigParameters.isReturnHashInputData(), + List results = PdfAS.verifyExtendedSignatureHolders(extSignatures, + signatureDevice, + verifyAfterReconstructXMLDsigParameters.isReturnHashInputData(), verifyAfterReconstructXMLDsigParameters.getVerificationTime(), verifyAfterReconstructXMLDsigParameters.getVerifySignatureIndex()); - + List vrs = new ArrayList(results.size()); - + int verifySignatureIndex = verifyAfterReconstructXMLDsigParameters.getVerifySignatureIndex(); if (verifySignatureIndex < 0) { @@ -605,23 +762,23 @@ public class PdfAsObject implements PdfAs SignatureResponse response = (SignatureResponse) results.get(i); ExtendedSignatureInformation extSigInfo = (ExtendedSignatureInformation)extSignatures.get(i); SignatureHolder holder = (SignatureHolder) extSigInfo.getSignatureInformation().getInternalSignatureInformation(); - + VerifyResult vr = new VerifyResultAdapter(response, holder, verifyAfterReconstructXMLDsigParameters.getVerificationTime(), extSigInfo.getXmlDsigData()); vr.setNonTextualObjects( extSigInfo.getSignatureInformation().getNonTextualObjects()); - + vrs.add(vr); } }else{ SignatureResponse response = (SignatureResponse) results.get(0); ExtendedSignatureInformation extSigInfo = (ExtendedSignatureInformation)extSignatures.get(verifySignatureIndex); SignatureHolder holder = (SignatureHolder) extSigInfo.getSignatureInformation().getInternalSignatureInformation(); - + VerifyResult vr = new VerifyResultAdapter(response, holder, verifyAfterReconstructXMLDsigParameters.getVerificationTime(), extSigInfo.getXmlDsigData()); vr.setNonTextualObjects( extSigInfo.getSignatureInformation().getNonTextualObjects()); - + vrs.add(vr); } - + VerifyResultsImpl verifyResults = new VerifyResultsImpl(vrs); return verifyResults; } catch (java.lang.OutOfMemoryError e) { @@ -632,7 +789,7 @@ public class PdfAsObject implements PdfAs public DynamicSignatureProfile createDynamicSignatureProfile(String parentProfile, DynamicSignatureLifetimeEnum mode) { return DynamicSignatureProfileImpl.createFromParent(null, parentProfile, mode); } - + public DynamicSignatureProfile createEmptyDynamicSignatureProfile(DynamicSignatureLifetimeEnum mode) { return DynamicSignatureProfileImpl.createEmptyProfile(null, mode); } @@ -648,19 +805,44 @@ public class PdfAsObject implements PdfAs public DynamicSignatureProfile createEmptyDynamicSignatureProfile(String myUniqueName, DynamicSignatureLifetimeEnum mode) { - return DynamicSignatureProfileImpl.createEmptyProfile(myUniqueName, mode); + return DynamicSignatureProfileImpl.createEmptyProfile(myUniqueName, mode); + } + + public SignatureDetailInformation prepareSign(SignParameters signParameters) throws PdfAsException { + // prepare log data + StatisticData sd = new StatisticData(signParameters); + StopWatch sw = new StopWatch(); + try { + sw.start(); + return performPrepareSign(signParameters); + } catch (PdfAsException e) { + sd.setException(e); + throw e; + } catch (RuntimeException e) { + sd.setException(e); + throw e; + } finally { + sw.stop(); + sd.setDuration(sw.getTime()); + // only log in case of error + if (sd.isError()) { + statLog.log(sd); + } + } } /** * @see PdfAs#prepareSign(SignParameters) */ - public SignatureDetailInformation prepareSign(SignParameters signParameters) throws PdfAsException { + private SignatureDetailInformation performPrepareSign(SignParameters signParameters) throws PdfAsException { CheckHelper.checkSignParameters(signParameters, true); - + if (signParameters.getProfileOverrideProperties() != null) { OverridePropertyHolder.setOverrideProps(signParameters.getProfileOverrideProperties()); } + PDFASUtils.createPdfReaderCheckingPermissions(signParameters.getDocument()); + signParameters.setDocument(PdfAS.applyStrictMode(signParameters.getDocument())); SettingsReader settings = SettingsReader.getInstance(); @@ -730,29 +912,29 @@ public class PdfAsObject implements PdfAs } else if (fromConfig) { arguments[0] = "width defined by the profile " + signParameters.getSignatureProfileId(); } - log.warn(MessageFormat.format(msg, arguments)); + log.warn(MessageFormat.format(msg, (Object[]) arguments)); } } - + Signator signator = createSignator(signParameters.getSignatureType()); SignatorInformation signatorInfo = signator.prepareSign( - new PdfDataSourceAdapter(signParameters.getDocument()), - signParameters.getSignatureProfileId(), - pos, + new PdfDataSourceAdapter(signParameters.getDocument()), + signParameters.getSignatureProfileId(), + pos, signParameters.getTimeStamperImpl()); - + SignatureDetailInformationImpl ret = new SignatureDetailInformationImpl(); ret.setSignatorInformation(signatorInfo); return ret; } - + private SignaturePlaceholderData getSignaturePlaceholder(SignParameters signParameters, String defaultProfile) throws SettingsException, PDFDocumentException, PlaceholderExtractionException { SignaturePlaceholderData spd = null; SignaturePlaceholderContext.setSignaturePlaceholderData(null); - + SettingsReader settings = SettingsReader.getInstance(); // check sig_obj.PROFILEID.enable_placeholder_search @@ -766,15 +948,15 @@ public class PdfAsObject implements PdfAs // check global enable_placeholder_search configFileActivedString = settings.getValueFromKey(ENABLE_PLACEHOLDER_SEARCH_KEY); } - + Boolean configFileActived = null; if (configFileActivedString != null) configFileActived = Boolean.valueOf(configFileActivedString); Boolean signParamsActivated = signParameters.isCheckForPlaceholder(); - + boolean enableSearch; - + if (signParamsActivated != null) { enableSearch = signParamsActivated.booleanValue(); @@ -788,8 +970,8 @@ public class PdfAsObject implements PdfAs if (enableSearch) { spd = SignaturePlaceholderExtractor.extract( - signParameters.getDocument().createInputStream(), - signParameters.getPlaceholderId(), + signParameters.getDocument().createInputStream(), + signParameters.getPlaceholderId(), signParameters.getPlaceholderMatchMode()); } return spd; @@ -809,15 +991,36 @@ public class PdfAsObject implements PdfAs { signatorId = SignatorFactory.MOST_RECENT_DETACHEDTEXT_SIGNATOR_ID; } - + return at.gv.egiz.pdfas.framework.SignatorFactory.createSignator(signatorId); } - public SignResult sign(SignParameters signParameters, SignatureDetailInformation signatorInfo) + public SignResult sign(SignParameters signParameters, SignatureDetailInformation signatorInfo) + throws PdfAsException { + // prepare log data + StatisticData sd = new StatisticData(signParameters); + StopWatch sw = new StopWatch(); + try { + sw.start(); + return performSign(signParameters, signatorInfo); + } catch (PdfAsException e) { + sd.setException(e); + throw e; + } catch (RuntimeException e) { + sd.setException(e); + throw e; + } finally { + sw.stop(); + sd.setDuration(sw.getTime()); + statLog.log(sd); + } + } + + private SignResult performSign(SignParameters signParameters, SignatureDetailInformation signatorInfo) throws PdfAsException { CheckHelper.checkSignParametersForSignAfterPrepare(signParameters, false); - + if (signParameters.getProfileOverrideProperties() != null) { OverridePropertyHolder.setOverrideProps(signParameters.getProfileOverrideProperties()); } @@ -832,34 +1035,34 @@ public class PdfAsObject implements PdfAs SignatureData sd = new SignatureDataImpl(new PdfDataSourceAdapter(signatorInfo.getSignatureData()), signatorInfo.getSignatureData().getMimeType()); SignSignatureObject sso = PdfAS.sign(sd, c, signParameters.getTimeStamperImpl()); ((SignatureDetailInformationImpl)signatorInfo).setSignSignatureObject(sso); - + return finishSign(signParameters, signatorInfo); } - public SignResult finishSign(SignParameters signParameters, SignatureDetailInformation signatureDetailInformation) + public SignResult finishSign(SignParameters signParameters, SignatureDetailInformation signatureDetailInformation) throws PdfAsException { try { CheckHelper.checkSignParametersForSignAfterPrepare(signParameters, true); CheckHelper.checkSignatureDetailInformation(signatureDetailInformation); - + if (signParameters.getProfileOverrideProperties() != null) { OverridePropertyHolder.setOverrideProps(signParameters.getProfileOverrideProperties()); } - + Signator signator = createSignator(signParameters.getSignatureType()); SignatorInformation signatorInfo = ((SignatureDetailInformationImpl)signatureDetailInformation).getSignatorInfo(); signator.finishSign(signatorInfo, new DataSinkAdapter(signParameters.getOutput())); return new SignResultImpl( - signParameters.getOutput(), + signParameters.getOutput(), signatorInfo.getSignSignatureObject().getX509Certificate(), - new ActualSignaturePositionAdapter(signatorInfo.getActualTablePos()), + new ActualSignaturePositionAdapter(signatorInfo.getActualTablePos()), signatorInfo.getNonTextualObjects()); } finally { OverridePropertyHolder.removeProperties(); DynamicSignatureProfileImpl.disposeLocalProfile(); } } - + } -- cgit v1.2.3