From 4852f856d9713ec07d1223417932cd5d5070ff8c Mon Sep 17 00:00:00 2001 From: tknall Date: Tue, 19 Jan 2010 21:59:41 +0000 Subject: - handling protected documents improved. - minor web application improvements git-svn-id: https://joinup.ec.europa.eu/svn/pdf-as/trunk@557 7b5415b0-85f9-ee4d-85bd-d5d0c3b42d1c --- .../impl/input/correction/InternalCorrector.java | 7 ++++- .../java/at/gv/egiz/pdfas/utils/PDFASUtils.java | 30 ++++++++++++++++++++++ .../java/at/knowcenter/wag/egov/egiz/PdfAS.java | 2 ++ .../wag/egov/egiz/pdf/BinarySignature.java | 3 +++ .../wag/egov/egiz/pdf/TextualSignature.java | 11 ++++---- .../wag/egov/egiz/web/servlets/SignServlet.java | 5 ++++ src/main/resources/config/help_text.properties | 2 +- 7 files changed, 52 insertions(+), 8 deletions(-) create mode 100644 src/main/java/at/gv/egiz/pdfas/utils/PDFASUtils.java (limited to 'src/main') diff --git a/src/main/java/at/gv/egiz/pdfas/impl/input/correction/InternalCorrector.java b/src/main/java/at/gv/egiz/pdfas/impl/input/correction/InternalCorrector.java index 409a600..6f73739 100644 --- a/src/main/java/at/gv/egiz/pdfas/impl/input/correction/InternalCorrector.java +++ b/src/main/java/at/gv/egiz/pdfas/impl/input/correction/InternalCorrector.java @@ -11,6 +11,8 @@ import at.gv.egiz.pdfas.exceptions.framework.CorrectorException; import at.gv.egiz.pdfas.framework.input.PdfDataSource; import at.gv.egiz.pdfas.framework.input.correction.Corrector; import at.gv.egiz.pdfas.impl.input.ByteArrayPdfDataSourceImpl; +import at.gv.egiz.pdfas.utils.PDFASUtils; +import at.knowcenter.wag.egov.egiz.exceptions.PDFDocumentException; import com.lowagie.text.DocumentException; import com.lowagie.text.pdf.PdfReader; @@ -33,6 +35,7 @@ public class InternalCorrector implements Corrector { byte[] pdf = document.getAsByteArray(); PdfReader reader = new PdfReader(pdf); + PDFASUtils.checkReaderPermissions(reader); ByteArrayOutputStream baos = new ByteArrayOutputStream(pdf.length); @@ -51,7 +54,9 @@ public class InternalCorrector implements Corrector catch (IOException e) { throw new CorrectorException(ErrorCode.CORRECTOR_EXCEPTION, e); - } + } catch (PDFDocumentException e) { + throw new CorrectorException(e.getErrorCode(), e); + } } } diff --git a/src/main/java/at/gv/egiz/pdfas/utils/PDFASUtils.java b/src/main/java/at/gv/egiz/pdfas/utils/PDFASUtils.java new file mode 100644 index 0000000..b26cc9b --- /dev/null +++ b/src/main/java/at/gv/egiz/pdfas/utils/PDFASUtils.java @@ -0,0 +1,30 @@ +package at.gv.egiz.pdfas.utils; + +import at.gv.egiz.pdfas.exceptions.ErrorCode; +import at.knowcenter.wag.egov.egiz.exceptions.PDFDocumentException; + +import com.lowagie.text.pdf.PdfReader; + +/** + * @author tknall + */ +public class PDFASUtils { + + private PDFASUtils() { + } + + /** + * Verifies that a document could be opened with full permissions. + * @param pdfReader The PdfReader + * @throws PDFDocumentException Thrown if document has not been opened with full permissions. + */ + public static void checkReaderPermissions(PdfReader pdfReader) throws PDFDocumentException { + if (pdfReader.isEncrypted()) { + throw new PDFDocumentException(ErrorCode.DOCUMENT_IS_PROTECTED, "Document is encrypted."); + } + if (!pdfReader.isOpenedWithFullPermissions()) { + throw new PDFDocumentException(ErrorCode.DOCUMENT_IS_PROTECTED, "Document is protected."); + } + } + +} diff --git a/src/main/java/at/knowcenter/wag/egov/egiz/PdfAS.java b/src/main/java/at/knowcenter/wag/egov/egiz/PdfAS.java index 0ae2e8e..423c222 100644 --- a/src/main/java/at/knowcenter/wag/egov/egiz/PdfAS.java +++ b/src/main/java/at/knowcenter/wag/egov/egiz/PdfAS.java @@ -43,6 +43,7 @@ import at.gv.egiz.pdfas.framework.signator.SignatorInformation; import at.gv.egiz.pdfas.impl.api.commons.PdfDataSourceAdapter; import at.gv.egiz.pdfas.impl.input.ByteArrayPdfDataSourceImpl; import at.gv.egiz.pdfas.impl.input.helper.DataSourceHelper; +import at.gv.egiz.pdfas.utils.PDFASUtils; import at.gv.egiz.pdfas.web.VerifySessionInformation; import at.knowcenter.wag.egov.egiz.cfg.SettingsReader; import at.knowcenter.wag.egov.egiz.exceptions.ConnectorException; @@ -1256,6 +1257,7 @@ public abstract class PdfAS // first check pageinstruction in TablePos-object // new,auto,absolut PdfReader reader = readInPdfDocument(pdfDataSource); + PDFASUtils.checkReaderPermissions(reader); // get pages of currentdocument int doc_pages = reader.getNumberOfPages(); int page = doc_pages; diff --git a/src/main/java/at/knowcenter/wag/egov/egiz/pdf/BinarySignature.java b/src/main/java/at/knowcenter/wag/egov/egiz/pdf/BinarySignature.java index 8cb0d42..d1e227a 100644 --- a/src/main/java/at/knowcenter/wag/egov/egiz/pdf/BinarySignature.java +++ b/src/main/java/at/knowcenter/wag/egov/egiz/pdf/BinarySignature.java @@ -37,11 +37,13 @@ import org.apache.commons.logging.LogFactory; import at.gv.egiz.pdfas.api.timestamp.TimeStamper; import at.gv.egiz.pdfas.exceptions.ErrorCode; +import at.gv.egiz.pdfas.exceptions.framework.CorrectorException; import at.gv.egiz.pdfas.exceptions.pdf.CaptionNotFoundException; import at.gv.egiz.pdfas.exceptions.pdf.KZSettingNotFoundException; import at.gv.egiz.pdfas.framework.input.PdfDataSource; import at.gv.egiz.pdfas.framework.output.DataSink; import at.gv.egiz.pdfas.framework.signator.SignatorInformation; +import at.gv.egiz.pdfas.utils.PDFASUtils; import at.knowcenter.wag.egov.egiz.cfg.SettingsReader; import at.knowcenter.wag.egov.egiz.exceptions.PDFDocumentException; import at.knowcenter.wag.egov.egiz.exceptions.PlaceholderException; @@ -795,6 +797,7 @@ public abstract class BinarySignature // InputStream is = original_document.createInputStream(); byte[] pdf_data = original_document.getAsByteArray(); PdfReader reader = new PdfReader(pdf_data); + PDFASUtils.checkReaderPermissions(reader); // is.close(); OutputStream baos = written_pdf.createOutputStream("application/pdf"); diff --git a/src/main/java/at/knowcenter/wag/egov/egiz/pdf/TextualSignature.java b/src/main/java/at/knowcenter/wag/egov/egiz/pdf/TextualSignature.java index 841e2a6..85bc4fd 100644 --- a/src/main/java/at/knowcenter/wag/egov/egiz/pdf/TextualSignature.java +++ b/src/main/java/at/knowcenter/wag/egov/egiz/pdf/TextualSignature.java @@ -30,6 +30,7 @@ import org.pdfbox.util.PDFTextStripper; import at.gv.egiz.pdfas.exceptions.ErrorCode; import at.gv.egiz.pdfas.framework.input.PdfDataSource; import at.gv.egiz.pdfas.performance.PerformanceCounters; +import at.gv.egiz.pdfas.utils.PDFASUtils; import at.knowcenter.wag.egov.egiz.cfg.ConfigLogger; import at.knowcenter.wag.egov.egiz.cfg.SettingsReader; import at.knowcenter.wag.egov.egiz.exceptions.PDFDocumentException; @@ -85,11 +86,7 @@ public class TextualSignature byte [] pdf_data = pdfDataSource.getAsByteArray(); PdfReader reader = new PdfReader(pdf_data); - if (!reader.isOpenedWithFullPermissions()) { - // cannot perform modification and extraction - throw new PDFDocumentException(ErrorCode.DOCUMENT_IS_PROTECTED, "Document is protected."); - - } + PDFASUtils.checkReaderPermissions(reader); //pdf_stream.close(); // PERF: PDF normalization needs byte array - this is costy @@ -212,12 +209,14 @@ public class TextualSignature * @return Returns the normalized pdf. * @throws IOException * @throws DocumentException + * @throws PDFDocumentException */ - public static byte[] normalizePDF(PdfDataSource pdfDataSource) throws IOException, DocumentException + public static byte[] normalizePDF(PdfDataSource pdfDataSource) throws IOException, DocumentException, PDFDocumentException { //iText byte [] pdf_data = pdfDataSource.getAsByteArray(); PdfReader reader = new PdfReader(pdf_data); + PDFASUtils.checkReaderPermissions(reader); //input_pdf.close(); // PERF: PDF Normalization needs byte array diff --git a/src/main/java/at/knowcenter/wag/egov/egiz/web/servlets/SignServlet.java b/src/main/java/at/knowcenter/wag/egov/egiz/web/servlets/SignServlet.java index f30f0fb..c946225 100644 --- a/src/main/java/at/knowcenter/wag/egov/egiz/web/servlets/SignServlet.java +++ b/src/main/java/at/knowcenter/wag/egov/egiz/web/servlets/SignServlet.java @@ -315,6 +315,11 @@ public class SignServlet extends HttpServlet log.error(e.getMessage(), e); prepareDispatchToErrorPage(e, request); dispatch(request, response, "/jsp/error.jsp"); + } catch (Exception e) { + log.error(e.getMessage(), e); + PresentableException pe = new PresentableException(ErrorCode.UNKNOWN_ERROR, e); + prepareDispatchToErrorPage(pe, request); + dispatch(request, response, "/jsp/error.jsp"); } } diff --git a/src/main/resources/config/help_text.properties b/src/main/resources/config/help_text.properties index 290e59f..61b35d1 100644 --- a/src/main/resources/config/help_text.properties +++ b/src/main/resources/config/help_text.properties @@ -35,7 +35,7 @@ error.code.227=Der angegebene Wert f error.code.228=Der angegebene Wert für die x-Position in der absoluten Position darf nicht kleiner gleich 0 sein. error.code.229=Der angegebene Wert für die y-Position in der absoluten Position darf nicht kleiner gleich 0 sein. error.code.230=Die Schriftart konnte nicht geladen werden. -error.code.231=Das Dokument kann nicht signiert werden, da es geschützt ist. +error.code.231=Das Dokument kann nicht verarbeitet werden, da es geschützt ist. error.code.250=Das angegebene Dokument ist leer. error.code.251=Das angegebene Dokument enthält keinen extrahierbaren Text. Falls sich der Inhalt ausschließlich aus Bildern zusammensetzt, wird die Verwendung einer binären Signatur empfohlen. -- cgit v1.2.3