From aa45585a4cb9e7aff0ef3da3c6a6fe43b9ace98c Mon Sep 17 00:00:00 2001 From: Alexander Marsalek Date: Tue, 20 Apr 2021 21:12:16 +0200 Subject: fix? --- .../lib/impl/placeholder/PlaceholderExtractor.java | 4 + .../lib/impl/placeholder/PlaceholderFilter.java | 63 ++++++++++++++ .../placeholder/PDFBoxPlaceholderExtractor.java | 32 +++++++- .../placeholder/SignaturePlaceholderExtractor.java | 95 ++++++++++++++++------ .../impl/signing/pdfbox2/PADESPDFBOXSigner.java | 10 ++- 5 files changed, 175 insertions(+), 29 deletions(-) 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 4c352b90..0a55b834 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,6 +3,10 @@ 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; } 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 500b9e24..99c09295 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,6 +24,7 @@ package at.gv.egiz.pdfas.lib.impl.placeholder; import java.io.IOException; +import java.util.List; import at.gv.egiz.pdfas.common.exceptions.PDFASError; import at.gv.egiz.pdfas.common.exceptions.PdfAsErrorCarrier; @@ -102,4 +103,66 @@ public class PlaceholderFilter implements IConfigurationConstants, 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; + } } \ No newline at end of file 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 256400a0..63b006bf 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,19 +1,47 @@ package at.gv.egiz.pdfas.lib.impl.pdfbox2.placeholder; +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; 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 { + @Override public SignaturePlaceholderData extract(PDFObject doc, String placeholderId, int matchMode) throws PdfAsException { if (doc instanceof PDFBOXObject) { PDFBOXObject object = (PDFBOXObject) doc; - return SignaturePlaceholderExtractor.extract(object.getDocument(), - placeholderId, matchMode); + 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); + } catch (IOException | ClassNotFoundException | InstantiationException | IllegalAccessException e2) { + throw new PDFIOException("error.pdf.io.04", e2); + } } throw new PdfAsException("INVALID STATE"); } 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 8a2c1cff..4031d07f 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 @@ -103,11 +103,11 @@ public class SignaturePlaceholderExtractor extends PDFStreamEngine implements Pl private static Logger logger = LoggerFactory .getLogger(SignaturePlaceholderExtractor.class); - private static List placeholders = new ArrayList<>(); + private List placeholders = new ArrayList<>(); private int currentPage = 0; private PDDocument doc; - private SignaturePlaceholderExtractor(String placeholderId, + protected SignaturePlaceholderExtractor(String placeholderId, int placeholderMatchMode, PDDocument doc) throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException { super(); @@ -126,9 +126,9 @@ public class SignaturePlaceholderExtractor extends PDFStreamEngine implements Pl this.doc = doc; } - public static List getPlaceholders() { - return placeholders; - } +// public static List getPlaceholders() { +// return placeholders; +// } /** @@ -136,38 +136,36 @@ public class SignaturePlaceholderExtractor extends PDFStreamEngine implements Pl * additional info.
* Searches only for the first placeholder page after page from top. * - * @param inputStream * @return all available info from the first found placeholder. - * @throws PDFDocumentException + * @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 static SignaturePlaceholderData extract(PDDocument doc, + 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); - } +// 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 { - extractor.setCurrentPage(pageNr); + setCurrentPage(pageNr); if(page.getContents() != null && page.getResources() != null && page.getContentStreams() != null) { - extractor.processPage(page); //TODO: pdfbox2 - right? + processPage(page); //TODO: pdfbox2 - right? } SignaturePlaceholderData ret = matchPlaceholderPage( - extractor.placeholders, placeholderId, matchMode); + placeholders, placeholderId, matchMode); if (ret != null) { SignaturePlaceholderContext .setSignaturePlaceholderData(ret); @@ -179,9 +177,9 @@ public class SignaturePlaceholderExtractor extends PDFStreamEngine implements Pl throw new PDFIOException("error.pdf.io.04", e); } } - if (extractor.placeholders.size() > 0) { + if (placeholders.size() > 0) { SignaturePlaceholderData ret = matchPlaceholderDocument( - extractor.placeholders, placeholderId, matchMode); + placeholders, placeholderId, matchMode); SignaturePlaceholderContext.setSignaturePlaceholderData(ret); return ret; } @@ -193,7 +191,56 @@ public class SignaturePlaceholderExtractor extends PDFStreamEngine implements Pl return null; } - private static SignaturePlaceholderData matchPlaceholderDocument( + public List extractList(PDDocument doc, + String placeholderId, int matchMode) throws PdfAsException { + SignaturePlaceholderContext.setSignaturePlaceholderData(null); +// List placeholders = new ArrayList<>(); +// 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 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 { @@ -247,7 +294,7 @@ public class SignaturePlaceholderExtractor extends PDFStreamEngine implements Pl return null; } - private static SignaturePlaceholderData matchPlaceholderPage( + private SignaturePlaceholderData matchPlaceholderPage( List placeholders, String placeholderId, int matchMode) { @@ -266,6 +313,8 @@ public class SignaturePlaceholderExtractor extends PDFStreamEngine implements Pl return null; } + + private void setCurrentPage(int pageNr) { this.currentPage = pageNr; } 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 94f70a4c..6345a714 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 @@ -157,9 +157,11 @@ public class PADESPDFBOXSigner implements IPdfSigner, IConfigurationConstants { signature.setFilter(COSName.getPDFName(signer.getPDFFilter())); signature.setSubFilter(COSName.getPDFName(signer.getPDFSubFilter())); - SignaturePlaceholderData signaturePlaceholderDataInit = PlaceholderFilter.checkPlaceholderSignatureLocation(pdfObject.getStatus(), pdfObject.getStatus().getSettings(), placeholder_id); +// SignaturePlaceholderData signaturePlaceholderDataInit = + placeholders =PlaceholderFilter.checkPlaceholderSignatureLocationList(pdfObject.getStatus(), + pdfObject.getStatus().getSettings(), placeholder_id); - placeholders = SignaturePlaceholderExtractor.getPlaceholders(); +// placeholders = SignaturePlaceholderExtractor.getPlaceholders(); availablePlaceholders = listAvailablePlaceholders(placeholders, existingSignatureLocations(doc)); @@ -653,7 +655,7 @@ public class PADESPDFBOXSigner implements IPdfSigner, IConfigurationConstants { if (doc != null) { try { doc.close(); - SignaturePlaceholderExtractor.getPlaceholders().clear(); + //SignaturePlaceholderExtractor.getPlaceholders().clear(); } catch (IOException e) { logger.debug("Failed to close COS Doc!", e); // Ignore @@ -936,7 +938,7 @@ public class PADESPDFBOXSigner implements IPdfSigner, IConfigurationConstants { //find first placeholder_id public List listAvailablePlaceholders(List placeholders, List existingPlaceholders) { - List result = null; + List result = new ArrayList<>(); if(placeholders!=null) { for(int i = 0; i < placeholders.size(); ++i) { -- cgit v1.2.3