From 3d982813b34f6f230baf4a467cdc37ec92a77595 Mon Sep 17 00:00:00 2001 From: netconomy Date: Fri, 17 Aug 2007 06:10:56 +0000 Subject: Performance git-svn-id: https://joinup.ec.europa.eu/svn/pdf-as/trunk@167 7b5415b0-85f9-ee4d-85bd-d5d0c3b42d1c --- .../wag/egov/egiz/pdf/AbsoluteTextSignature.java | 4 +- .../wag/egov/egiz/pdf/BinarySignature.java | 463 ++++++++++----------- .../wag/egov/egiz/pdf/BinarySignatureHolder.java | 165 +++++--- .../egiz/pdf/IncrementalUpdateInformation.java | 10 +- .../at/knowcenter/wag/egov/egiz/pdf/PDFPage.java | 6 +- .../wag/egov/egiz/pdf/PDFSignatureCreation.java | 20 +- .../wag/egov/egiz/pdf/PDFSignatureObjectIText.java | 3 +- .../knowcenter/wag/egov/egiz/pdf/PDFUtilities.java | 10 +- .../wag/egov/egiz/pdf/SignatureHolder.java | 36 +- .../wag/egov/egiz/pdf/TextualSignature.java | 26 +- .../wag/egov/egiz/pdf/TextualSignatureHolder.java | 74 +++- 11 files changed, 461 insertions(+), 356 deletions(-) (limited to 'src/main/java/at/knowcenter/wag/egov/egiz/pdf') diff --git a/src/main/java/at/knowcenter/wag/egov/egiz/pdf/AbsoluteTextSignature.java b/src/main/java/at/knowcenter/wag/egov/egiz/pdf/AbsoluteTextSignature.java index a5d1be8..4219475 100644 --- a/src/main/java/at/knowcenter/wag/egov/egiz/pdf/AbsoluteTextSignature.java +++ b/src/main/java/at/knowcenter/wag/egov/egiz/pdf/AbsoluteTextSignature.java @@ -75,7 +75,7 @@ public class AbsoluteTextSignature String current_text = text; for (;;) { - SignatureHolder signature_holder = extractLatestBlock(current_text); + TextualSignatureHolder signature_holder = extractLatestBlock(current_text); if (signature_holder == null) { break; @@ -99,7 +99,7 @@ public class AbsoluteTextSignature * @throws SignatureTypesException * F.e. */ - public static SignatureHolder extractLatestBlock(String text) throws SignatureException, SignatureTypesException + public static TextualSignatureHolder extractLatestBlock(String text) throws SignatureException, SignatureTypesException { FoundBlock latest_block = findLatestBlock(text); if (latest_block == null) 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 fb93fbd..d253393 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 @@ -24,6 +24,7 @@ import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; +import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; @@ -35,17 +36,16 @@ import java.util.Comparator; import java.util.Iterator; import java.util.List; +import at.gv.egiz.pdfas.exceptions.ErrorCode; +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.knowcenter.wag.egov.egiz.exceptions.PDFDocumentException; import at.knowcenter.wag.egov.egiz.exceptions.PlaceholderException; import at.knowcenter.wag.egov.egiz.exceptions.PresentableException; import at.knowcenter.wag.egov.egiz.exceptions.SettingNotFoundException; -import at.knowcenter.wag.egov.egiz.exceptions.SignatureException; -import at.knowcenter.wag.egov.egiz.exceptions.SignatureTypesException; -import at.knowcenter.wag.egov.egiz.sig.SignatureBlock; import at.knowcenter.wag.egov.egiz.sig.SignatureFieldDefinition; -import at.knowcenter.wag.egov.egiz.sig.SignatureObject; import at.knowcenter.wag.egov.egiz.sig.SignatureTypes; -import at.knowcenter.wag.egov.egiz.sig.X509Cert; import at.knowcenter.wag.egov.egiz.tools.CodingHelper; import at.knowcenter.wag.exactparser.ByteArrayUtils; @@ -288,8 +288,7 @@ public abstract class BinarySignature * @return Returns the indirect reference of the Egiz Dictionary, or null, if * none exists. */ - public static PdfIndirectReference getEgizDictIndRefFromReader( - PdfReader reader) + public static PdfIndirectReference getEgizDictIndRefFromReader(PdfReader reader) { PdfDictionary catalog = reader.getCatalog(); PdfIndirectReference dict_ir = (PdfIndirectReference) catalog.get(EGIZ_DICT_NAME); @@ -369,12 +368,11 @@ public abstract class BinarySignature * The original document size. * @return Returns the signable text. */ - public static String retrieveSignableTextFromData(final byte[] data, - final int ods) + public static String retrieveSignableTextFromData(final byte[] data, final int ods) { // byte[] digest = buildDigest(data, ods); String raw_text = CodingHelper.encodeBase64(data);// digest); // data); - + return raw_text; } @@ -387,8 +385,7 @@ public abstract class BinarySignature * The byte ranges. * @return Returns the filled text. */ - public static byte[] prepareDataToSign(final byte[] data, - final List byte_ranges) + public static byte[] prepareDataToSign(final byte[] data, final List byte_ranges) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); Iterator it = byte_ranges.iterator(); @@ -411,174 +408,176 @@ public abstract class BinarySignature return data_to_sign; } - /** - * Extracts the binary 'text' of a document. - * - *

- * If the document contains an Egiz Dictionary, which means that it is already - * signed, the binary text is the Base64 coded string of the original document - * followed by the Ascii representation of the signature block. - *

- *

- * If the document does not contain an Egiz Dictionary, which means that it is - * unsigned, only the binary Base64 coded original document is returned as - * binary text. - *

- *

- * This function is intented for being used instead of the "text extraction" - * mechanism used in the plain text Egiz project. - *

- * - * @param doc - * The file. - * @return Returns the binary text of the document. - * @throws PDFDocumentException - * Forwarded exception. - */ - public static String extractTextBinary(File doc) throws PDFDocumentException - { - try - { - FileInputStream fis = new FileInputStream(doc); - return extractTextBinary(fis); - } - catch (FileNotFoundException e) - { - throw new PDFDocumentException(202, e); - } - } - - /** - * Extracts the text binary. - * - * @param is - * @return Returns the binary text. - * @throws PDFDocumentException - */ - public static String extractTextBinary(InputStream is) throws PDFDocumentException - { - try - { - // for some stupid reason this produces a read error if the is comes from - // a - // multipart servlet form..??? - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - int i = -1; - int acc = 0; - byte[] b = new byte[1000]; - while ((i = is.read(b)) > 0) - { - acc += i; - System.out.print(" " + i); - baos.write(b, 0, i); - } - System.out.println("acc = " + acc); - byte[] pdf = baos.toByteArray(); - - return extractTextBinary(pdf); - } - catch (IOException e) - { - throw new PDFDocumentException(202, e); - } - } - - /** - * Extracts the signable text from a binary pdf document. - * - *

- * The signable text is the text that will be signed or verified afterwards. - *

- * - * @param pdf - * The pdf document. - * @return Returns the extracted text String. - * @throws PDFDocumentException - * Forwarded exception. - */ - public static String extractTextBinary(final byte[] pdf) throws PDFDocumentException - { - try - { - PdfReader reader = new PdfReader(new ByteArrayInputStream(pdf)); - PdfDictionary egiz_dict = getEgizDictFromReader(reader); - if (egiz_dict == null) - { - System.out.println("NO Egiz Dict found - whole doc = original doc"); - - int ods = pdf.length; - return retrieveSignableTextFromData(pdf, ods); - } - - String sig_text = extractSignatureTextOnly(egiz_dict); - - int ods = getOriginalDocumentSizeFromEgizDict(egiz_dict); - - String raw_text = retrieveSignableTextFromData(pdf, ods); - raw_text += "\n"; - raw_text += sig_text; - - return raw_text; - } - catch (IOException e) - { - throw new PDFDocumentException(202, e); - } - } - - /** - * Retrieves the List of SignatureHolders containing the information of all - * digital signatures of the given document. - * - *

- * If the List of SignatureHolders is empty, the document is not signed - * anyways. - *

- * - * @param pdf - * The complete pdf document. - * @return Returns the List of SignatureHolders. - * @throws PDFDocumentException - * @throws SignatureTypesException - * @throws SignatureException - */ - public static List extractSignatureHoldersBinary(final byte[] pdf) throws PDFDocumentException, SignatureTypesException, SignatureException - { - try - { - PdfReader reader = new PdfReader(new ByteArrayInputStream(pdf)); - List chain = getEgizDictChainFromReader(reader); - - List signatures = new ArrayList(); - Iterator it = chain.iterator(); - while (it.hasNext()) - { - PdfDictionary dict = (PdfDictionary) it.next(); - - int ods = getOriginalDocumentSizeFromEgizDict(dict); - String signature_text = extractSignatureTextOnly(dict); - - SignatureTypes sig_types = SignatureTypes.getInstance(); - List types = sig_types.getSignatureTypeDefinitions(); - SignatureBlock sig_block = new SignatureBlock(types); - boolean could_separate = sig_block.separateBlockFromRawText(signature_text, false); - - if (could_separate) - { - SignatureObject sig_object = sig_block.getSignatureObject(); - - SignatureHolder holder = new BinarySignatureHolder(pdf, ods, sig_object); - signatures.add(holder); - } - } - - return signatures; - } - catch (IOException e) - { - throw new PDFDocumentException(201, e); - } - } + // TODO old code - remove + // /** + // * Extracts the binary 'text' of a document. + // * + // *

+ // * If the document contains an Egiz Dictionary, which means that it is already + // * signed, the binary text is the Base64 coded string of the original document + // * followed by the Ascii representation of the signature block. + // *

+ // *

+ // * If the document does not contain an Egiz Dictionary, which means that it is + // * unsigned, only the binary Base64 coded original document is returned as + // * binary text. + // *

+ // *

+ // * This function is intented for being used instead of the "text extraction" + // * mechanism used in the plain text Egiz project. + // *

+ // * + // * @param doc + // * The file. + // * @return Returns the binary text of the document. + // * @throws PDFDocumentException + // * Forwarded exception. + // */ + // public static String extractTextBinary(File doc) throws PDFDocumentException + // { + // try + // { + // FileInputStream fis = new FileInputStream(doc); + // return extractTextBinary(fis); + // } + // catch (FileNotFoundException e) + // { + // throw new PDFDocumentException(202, e); + // } + // } + // + // /** + // * Extracts the text binary. + // * + // * @param is + // * @return Returns the binary text. + // * @throws PDFDocumentException + // */ + // public static String extractTextBinary(InputStream is) throws PDFDocumentException + // { + // try + // { + // // for some stupid reason this produces a read error if the is comes from + // // a + // // multipart servlet form..??? + // ByteArrayOutputStream baos = new ByteArrayOutputStream(); + // int i = -1; + // int acc = 0; + // byte[] b = new byte[1000]; + // while ((i = is.read(b)) > 0) + // { + // acc += i; + // System.out.print(" " + i); + // baos.write(b, 0, i); + // } + // System.out.println("acc = " + acc); + // byte[] pdf = baos.toByteArray(); + // + // return extractTextBinary(pdf); + // } + // catch (IOException e) + // { + // throw new PDFDocumentException(202, e); + // } + // } + // + // /** + // * Extracts the signable text from a binary pdf document. + // * + // *

+ // * The signable text is the text that will be signed or verified afterwards. + // *

+ // * + // * @param pdf + // * The pdf document. + // * @return Returns the extracted text String. + // * @throws PDFDocumentException + // * Forwarded exception. + // */ + // public static String extractTextBinary(final byte[] pdf) throws PDFDocumentException + // { + // try + // { + // PdfReader reader = new PdfReader(new ByteArrayInputStream(pdf)); + // PdfDictionary egiz_dict = getEgizDictFromReader(reader); + // if (egiz_dict == null) + // { + // System.out.println("NO Egiz Dict found - whole doc = original doc"); + // + // int ods = pdf.length; + // return retrieveSignableTextFromData(pdf, ods); + // } + // + // String sig_text = extractSignatureTextOnly(egiz_dict); + // + // int ods = getOriginalDocumentSizeFromEgizDict(egiz_dict); + // + // String raw_text = retrieveSignableTextFromData(pdf, ods); + // raw_text += "\n"; + // raw_text += sig_text; + // + // return raw_text; + // } + // catch (IOException e) + // { + // throw new PDFDocumentException(202, e); + // } + // } + + // TODO obsolete code - remove + // /** + // * Retrieves the List of SignatureHolders containing the information of all + // * digital signatures of the given document. + // * + // *

+ // * If the List of SignatureHolders is empty, the document is not signed + // * anyways. + // *

+ // * + // * @param pdf + // * The complete pdf document. + // * @return Returns the List of SignatureHolders. + // * @throws PDFDocumentException + // * @throws SignatureTypesException + // * @throws SignatureException + // */ + // public static List extractSignatureHoldersBinary(final byte[] pdf) throws PDFDocumentException, SignatureTypesException, SignatureException + // { + // try + // { + // PdfReader reader = new PdfReader(new ByteArrayInputStream(pdf)); + // List chain = getEgizDictChainFromReader(reader); + // + // List signatures = new ArrayList(); + // Iterator it = chain.iterator(); + // while (it.hasNext()) + // { + // PdfDictionary dict = (PdfDictionary) it.next(); + // + // int ods = getOriginalDocumentSizeFromEgizDict(dict); + // String signature_text = extractSignatureTextOnly(dict); + // + // SignatureTypes sig_types = SignatureTypes.getInstance(); + // List types = sig_types.getSignatureTypeDefinitions(); + // SignatureBlock sig_block = new SignatureBlock(types); + // boolean could_separate = sig_block.separateBlockFromRawText(signature_text, false); + // + // if (could_separate) + // { + // SignatureObject sig_object = sig_block.getSignatureObject(); + // + // SignatureHolder holder = new BinarySignatureHolder(pdf, ods, sig_object); + // signatures.add(holder); + // } + // } + // + // return signatures; + // } + // catch (IOException e) + // { + // throw new PDFDocumentException(201, e); + // } + // } // /** // * Signs a document with the given signature table using the Incremental @@ -677,22 +676,26 @@ public abstract class BinarySignature * @throws PresentableException * Forwarded exception. */ - public static IncrementalUpdateInformation writeIncrementalUpdate( - byte[] original_document, PdfPTable pdf_table, PositioningInstruction pi, + public static IncrementalUpdateInformation writeIncrementalUpdate(PdfDataSource original_document, DataSink written_pdf, PdfPTable pdf_table, PositioningInstruction pi, List variable_field_definitions, List all_field_definitions) throws PresentableException { try { IncrementalUpdateInformation iui = new IncrementalUpdateInformation(); iui.original_document = original_document; - iui.start_index = original_document.length; + iui.start_index = original_document.getLength(); Document.compress = false; // System.out.println("wprinz: STAMPING PDF"); - PdfReader reader = new PdfReader(original_document); - ByteArrayOutputStream baos = new ByteArrayOutputStream(); + InputStream is = original_document.createInputStream(); + PdfReader reader = new PdfReader(is); + is.close(); + + OutputStream baos = written_pdf.createOutputStream("application/pdf"); + // ByteArrayOutputStream baos = new ByteArrayOutputStream(); + // IMPORTANT: append the new content to the original document using // incremental updated // The stamper allows this by setting append = true @@ -786,19 +789,21 @@ public abstract class BinarySignature stamper.close(); // System.out.println("wprinz: STAMPING FINISHED"); - iui.signed_pdf = baos.toByteArray(); + // just to make sure the stream is really closed + baos.close(); + // iui.signed_pdf = baos.toByteArray(); return iui; } catch (IOException e) { e.printStackTrace(); - throw new PresentableException(e); + throw new PresentableException(ErrorCode.CANNOT_WRITE_PDF, e); } catch (DocumentException e) { e.printStackTrace(); - throw new PresentableException(e); + throw new PresentableException(ErrorCode.CANNOT_WRITE_PDF, e); } } @@ -816,9 +821,8 @@ public abstract class BinarySignature * @throws IOException * @throws SettingNotFoundException */ - protected static void createEgizDict(PdfStamper stamper, - PdfTemplate table_template, IncrementalUpdateInformation iui, - List variable_field_definitions, List all_field_definitions) throws IOException, SettingNotFoundException + protected static void createEgizDict(PdfStamper stamper, PdfTemplate table_template, IncrementalUpdateInformation iui, List variable_field_definitions, List all_field_definitions) + throws IOException, SettingNotFoundException { // iui.temp_ir = table_template.getIndirectReference(); iui.temp_ir_number = table_template.getIndirectReference().getNumber(); @@ -1302,8 +1306,7 @@ public abstract class BinarySignature * @return Returns the list of ReplaceInfo objects specifying the variable * areas. */ - protected static List determineReplacesInContentStream(final byte[] pdf, - int begin, int end, List field_definitions) + protected static List determineReplacesInContentStream(final byte[] pdf, int begin, int end, List field_definitions) { List replaces = new ArrayList(); try @@ -1343,8 +1346,7 @@ public abstract class BinarySignature } // sort replaces - Collections.sort(replaces, new Comparator() - { + Collections.sort(replaces, new Comparator() { public int compare(Object arg0, Object arg1) { ReplaceInfo ri0 = (ReplaceInfo) arg0; @@ -1373,8 +1375,7 @@ public abstract class BinarySignature * @throws SettingNotFoundException * F.e. */ - protected static List determineKZ(final byte[] pdf, int begin, int end, - List field_definitions) throws SettingNotFoundException + protected static List determineKZ(final byte[] pdf, int begin, int end, List field_definitions) throws SettingNotFoundException { try { @@ -1414,7 +1415,7 @@ public abstract class BinarySignature { e.printStackTrace(); } - throw new SettingNotFoundException(102, "Field " + SignatureTypes.SIG_KZ + " not found."); + throw new KZSettingNotFoundException("Field " + SignatureTypes.SIG_KZ + " not found."); } /** @@ -1508,8 +1509,7 @@ public abstract class BinarySignature return true; } - protected static boolean startsWithCaption(StringInfo si, - List field_definitions) + protected static boolean startsWithCaption(StringInfo si, List field_definitions) { try { @@ -1568,8 +1568,7 @@ public abstract class BinarySignature * @param placeholder * The placeholder the string should be filled with. */ - public static void restorePlaceholder(final byte[] pdf, StringInfo si, - final byte placeholder) + public static void restorePlaceholder(final byte[] pdf, StringInfo si, final byte placeholder) { byte[] ph = new byte[si.string_length]; for (int i = 0; i < ph.length; i++) @@ -1592,8 +1591,7 @@ public abstract class BinarySignature * values. * @throws PDFDocumentException */ - public static List reconstructReplaces(final byte[] pdf, byte[][] brevs, - StringInfo[] sis, byte[][] encodings) throws PDFDocumentException + public static List reconstructReplaces(final byte[] pdf, byte[][] brevs, StringInfo[] sis, byte[][] encodings) throws PDFDocumentException { try { @@ -1654,8 +1652,7 @@ public abstract class BinarySignature * The number of digits. * @return Returns the read number. */ - public static int readNumber(final byte[] pdf, final int start_index, - final int num_digits) + public static int readNumber(final byte[] pdf, final int start_index, final int num_digits) { try { @@ -1685,8 +1682,7 @@ public abstract class BinarySignature * @param num_digits * The number of digits. */ - public static void replaceNumber(final byte[] pdf, final int start_index, - final int number, final int num_digits) + public static void replaceNumber(final byte[] pdf, final int start_index, final int number, final int num_digits) { try { @@ -1717,30 +1713,31 @@ public abstract class BinarySignature } } - /** - * For debugging purposes. - * - * @param args - * @throws IOException - */ - public static void main(String[] args) throws IOException - { - File signed_doc = new File("C:/wprinz/temp.pdf"); - - PdfReader reader = new PdfReader(new FileInputStream(signed_doc)); - PdfDictionary egiz_dict = getEgizDictFromReader(reader); - if (egiz_dict == null) - { - System.out.println("NO Egiz Dict"); - return; - } - - String sig_text = extractSignatureTextOnly(egiz_dict); - System.out.println("Sig Text:"); - System.out.println(sig_text); - - int ods = getOriginalDocumentSizeFromEgizDict(egiz_dict); - System.out.println("Original Document Size = " + ods); - } + // TODO old code - remove + // /** + // * For debugging purposes. + // * + // * @param args + // * @throws IOException + // */ + // public static void main(String[] args) throws IOException + // { + // File signed_doc = new File("C:/wprinz/temp.pdf"); + // + // PdfReader reader = new PdfReader(new FileInputStream(signed_doc)); + // PdfDictionary egiz_dict = getEgizDictFromReader(reader); + // if (egiz_dict == null) + // { + // System.out.println("NO Egiz Dict"); + // return; + // } + // + // String sig_text = extractSignatureTextOnly(egiz_dict); + // System.out.println("Sig Text:"); + // System.out.println(sig_text); + // + // int ods = getOriginalDocumentSizeFromEgizDict(egiz_dict); + // System.out.println("Original Document Size = " + ods); + // } } diff --git a/src/main/java/at/knowcenter/wag/egov/egiz/pdf/BinarySignatureHolder.java b/src/main/java/at/knowcenter/wag/egov/egiz/pdf/BinarySignatureHolder.java index 1f522ff..8d42370 100644 --- a/src/main/java/at/knowcenter/wag/egov/egiz/pdf/BinarySignatureHolder.java +++ b/src/main/java/at/knowcenter/wag/egov/egiz/pdf/BinarySignatureHolder.java @@ -19,6 +19,10 @@ package at.knowcenter.wag.egov.egiz.pdf; import java.io.Serializable; +import at.gv.egiz.pdfas.impl.input.ByteArrayPdfDataSourceImpl; +import at.gv.egiz.pdfas.framework.input.DataSource; +import at.gv.egiz.pdfas.framework.input.PdfDataSource; + import at.knowcenter.wag.egov.egiz.sig.SignatureObject; /** @@ -44,48 +48,58 @@ public class BinarySignatureHolder implements Serializable, SignatureHolder */ private static final long serialVersionUID = -7208103904479272760L; - /** - * The whole pdf this holder was extracted from. - */ - private byte[] signed_pdf = null; - - /** - * The number of bytes that give the signed document. - */ - private int signed_pdf_length = -1; - - /** - * The signed text of this object. - * - *

- * This is the value that will be signed by the Connector. - *

- */ - private String signed_text = null; +// /** +// * The whole pdf this holder was extracted from. +// */ +// private byte[] signed_pdf = null; + + private PdfDataSource pdfDataSource = null; + +// /** +// * The number of bytes that give the signed document. +// */ +// private int signed_pdf_length = -1; + +// /** +// * The signed text of this object. +// * +// *

+// * This is the value that will be signed by the Connector. +// *

+// */ +// private String signed_text = null; /** * The signature object. */ private SignatureObject signature_object = null; - /** - * Constructor that takes the pdf and the SignatureObject as parameters. - * - * @param pdf - * The pdf data. - * @param length - * The length (number of bytes) of the pdf data to be used for being - * converted into "signed text". - * @param so - * The signed signature object. - */ - public BinarySignatureHolder(final byte[] pdf, final int length, SignatureObject so) +// /** +// * Constructor that takes the pdf and the SignatureObject as parameters. +// * +// * @param pdf +// * The pdf data. +// * @param length +// * The length (number of bytes) of the pdf data to be used for being +// * converted into "signed text". +// * @param so +// * The signed signature object. +// */ +// public BinarySignatureHolder(final byte[] pdf, final int length, SignatureObject so) +// { +//// this.signed_pdf = pdf; +// this.pdfDataSource = new ByteArrayPdfDataSourceImpl(pdf); +// // streaming byte array +// this.signed_pdf_length = length; +// this.signature_object = so; +// +//// this.signed_text = null; +// } + + public BinarySignatureHolder(PdfDataSource pdf, SignatureObject so) { - this.signed_pdf = pdf; - this.signed_pdf_length = length; + this.pdfDataSource = pdf; this.signature_object = so; - - this.signed_text = null; } /** @@ -93,11 +107,12 @@ public class BinarySignatureHolder implements Serializable, SignatureHolder */ public String getSignedText() { - if (this.signed_text == null) - { - computeSignedText(); - } - return this.signed_text; + throw new RuntimeException("BinarySignatureHolder must not return text."); +// if (this.signed_text == null) +// { +// computeSignedText(); +// } +// return this.signed_text; } /** @@ -107,40 +122,56 @@ public class BinarySignatureHolder implements Serializable, SignatureHolder { return this.signature_object; } - + /** - * Computes or recomputes the signed text from the underlying binary data. - * - *

- * This usually encodes the binary data of given length in Base64. - *

- * - *

- * This is usually called automatically when the signed text is first - * requested. - *

+ * @see at.knowcenter.wag.egov.egiz.pdf.SignatureHolder#getDataSource() */ - protected void computeSignedText() + public DataSource getDataSource() { - this.signed_text = BinarySignature.retrieveSignableTextFromData(this.signed_pdf, this.signed_pdf_length); + return getSignedPdf(); } - - /** - * Returns the signed_pdf. - * @return Returns the signed_pdf. - */ - public byte[] getSignedPdf() + + + + // TODO obsolete funtction - remove; +// /** +// * Computes or recomputes the signed text from the underlying binary data. +// * +// *

+// * This usually encodes the binary data of given length in Base64. +// *

+// * +// *

+// * This is usually called automatically when the signed text is first +// * requested. +// *

+// */ +// protected void computeSignedText() +// { +// this.signed_text = BinarySignature.retrieveSignableTextFromData(this.signed_pdf, this.signed_pdf_length); +// } + +// /** +// * Returns the signed_pdf. +// * @return Returns the signed_pdf. +// */ +// public byte[] getSignedPdf() +// { +// return this.signed_pdf; +// } + + public PdfDataSource getSignedPdf () { - return this.signed_pdf; + return this.pdfDataSource; } - /** - * Returns the signed_pdf_length. - * @return Returns the signed_pdf_length. - */ - public int getSignedPdfLength() - { - return this.signed_pdf_length; - } +// /** +// * Returns the signed_pdf_length. +// * @return Returns the signed_pdf_length. +// */ +// public int getSignedPdfLength() +// { +// return this.signed_pdf_length; +// } } diff --git a/src/main/java/at/knowcenter/wag/egov/egiz/pdf/IncrementalUpdateInformation.java b/src/main/java/at/knowcenter/wag/egov/egiz/pdf/IncrementalUpdateInformation.java index 76c59c3..fc446f9 100644 --- a/src/main/java/at/knowcenter/wag/egov/egiz/pdf/IncrementalUpdateInformation.java +++ b/src/main/java/at/knowcenter/wag/egov/egiz/pdf/IncrementalUpdateInformation.java @@ -20,6 +20,9 @@ package at.knowcenter.wag.egov.egiz.pdf; import java.io.Serializable; import java.util.List; +import at.gv.egiz.pdfas.framework.input.PdfDataSource; +import at.gv.egiz.pdfas.framework.output.DataSink; + import at.knowcenter.wag.egov.egiz.sig.SignatureData; import at.knowcenter.wag.egov.egiz.sig.SignatureObject; import at.knowcenter.wag.egov.egiz.sig.connectors.bku.SignSignatureObject; @@ -48,7 +51,8 @@ public class IncrementalUpdateInformation implements Serializable /** * The original PDF document. */ - public byte[] original_document = null; + //public byte[] original_document = null; + public PdfDataSource original_document = null; /** * The Singature type to be created. @@ -63,6 +67,10 @@ public class IncrementalUpdateInformation implements Serializable *

*/ public byte[] signed_pdf = null; + // this is only valid during prepare + + // holds the variable iui data for bin sigs. + public byte [] sign_iui_block = null; /** * The start index of this incremental update block. diff --git a/src/main/java/at/knowcenter/wag/egov/egiz/pdf/PDFPage.java b/src/main/java/at/knowcenter/wag/egov/egiz/pdf/PDFPage.java index 666ee52..0bae3d2 100644 --- a/src/main/java/at/knowcenter/wag/egov/egiz/pdf/PDFPage.java +++ b/src/main/java/at/knowcenter/wag/egov/egiz/pdf/PDFPage.java @@ -355,7 +355,7 @@ public class PDFPage extends PDFTextStripper if (current_y > this.footer_line) { - logger_.debug("character is below footer_line. footer_line = " + this.footer_line + ", text.character=" + character + ", y=" + current_y); + //logger_.debug("character is below footer_line. footer_line = " + this.footer_line + ", text.character=" + character + ", y=" + current_y); return; } @@ -363,11 +363,11 @@ public class PDFPage extends PDFTextStripper if (!character.equals(" ") && current_y > this.max_character_ypos) { this.max_character_ypos = current_y; - logger_.debug("text.character=" + character + ", y=" + current_y); + //logger_.debug("text.character=" + character + ", y=" + current_y); // System.err.println(character + "|" + current_y); } - logger_.debug("text.character=" + character + ", y=" + current_y); + //logger_.debug("text.character=" + character + ", y=" + current_y); // System.err.println(character + "|" + current_y); } diff --git a/src/main/java/at/knowcenter/wag/egov/egiz/pdf/PDFSignatureCreation.java b/src/main/java/at/knowcenter/wag/egov/egiz/pdf/PDFSignatureCreation.java index 453103b..fd885fc 100644 --- a/src/main/java/at/knowcenter/wag/egov/egiz/pdf/PDFSignatureCreation.java +++ b/src/main/java/at/knowcenter/wag/egov/egiz/pdf/PDFSignatureCreation.java @@ -79,12 +79,12 @@ public class PDFSignatureCreation { * @throws PDFDocumentException ErrorCode:101 */ public PDFSignatureCreation(SignatureObject sigObject) throws PDFDocumentException { - try { +// try { loadSettings(); - } catch (PDFDocumentException e) { - e.setErrorCode(101); - throw e; - } +// } catch (PDFDocumentException e) { +// e.setErrorCode(101); +// throw e; +// } sigObject_ = sigObject; } @@ -157,12 +157,12 @@ public class PDFSignatureCreation { */ public PDFSignatureObject getPDFSignatureObject() throws PDFDocumentException { if (pdfSigObject_ == null) { - try { +// try { pdfSigObject_ = createPDFSignatureObject(); - } catch (PDFDocumentException e) { - e.setErrorCode(203); - throw e; - } +// } catch (PDFDocumentException e) { +// e.setErrorCode(203); +// throw e; +// } pdfSigObject_.setSignatorObject(sigObject_); } return pdfSigObject_; diff --git a/src/main/java/at/knowcenter/wag/egov/egiz/pdf/PDFSignatureObjectIText.java b/src/main/java/at/knowcenter/wag/egov/egiz/pdf/PDFSignatureObjectIText.java index 56b5f86..6c609ae 100644 --- a/src/main/java/at/knowcenter/wag/egov/egiz/pdf/PDFSignatureObjectIText.java +++ b/src/main/java/at/knowcenter/wag/egov/egiz/pdf/PDFSignatureObjectIText.java @@ -28,7 +28,6 @@ import org.apache.log4j.Logger; import at.knowcenter.wag.egov.egiz.cfg.ConfigLogger; import at.knowcenter.wag.egov.egiz.cfg.SettingsReader; -import at.knowcenter.wag.egov.egiz.exceptions.ErrorCodeException; import at.knowcenter.wag.egov.egiz.exceptions.PDFDocumentException; import at.knowcenter.wag.egov.egiz.exceptions.SettingsException; import at.knowcenter.wag.egov.egiz.sig.SignatureObject; @@ -342,7 +341,7 @@ public class PDFSignatureObjectIText implements PDFSignatureObject } if (logger_.isEnabledFor(Level.ERROR)) { - logger_.error("Error Code:222 " + ErrorCodeException.getErrorCodeMessage(222) + ". IOException:" + e.getMessage()); + logger_.error("Error Code: 222, IOException:" + e.getMessage()); } PDFDocumentException pde = new PDFDocumentException(222, "PDF table can not created: Image can not loaded"); throw pde; diff --git a/src/main/java/at/knowcenter/wag/egov/egiz/pdf/PDFUtilities.java b/src/main/java/at/knowcenter/wag/egov/egiz/pdf/PDFUtilities.java index 8c15d21..e19c244 100644 --- a/src/main/java/at/knowcenter/wag/egov/egiz/pdf/PDFUtilities.java +++ b/src/main/java/at/knowcenter/wag/egov/egiz/pdf/PDFUtilities.java @@ -22,6 +22,8 @@ import java.io.File; import java.io.IOException; import java.util.List; +import at.gv.egiz.pdfas.framework.input.PdfDataSource; + import org.pdfbox.pdfparser.PDFParser; import org.pdfbox.pdmodel.PDDocument; import org.pdfbox.pdmodel.PDPage; @@ -39,13 +41,15 @@ import com.lowagie.text.DocumentException; */ public abstract class PDFUtilities { - public static float calculatePageLength(final byte[] pdf,int page ,float footer_line, int pagerotation) throws PDFDocumentException + public static float calculatePageLength(final PdfDataSource pdfDataSource,int page ,float footer_line, int pagerotation) throws PDFDocumentException { try { - ByteArrayInputStream original_bais = new ByteArrayInputStream(pdf); - byte [] normalized_pdf = TextualSignature.normalizePDF(original_bais); + //ByteArrayInputStream original_bais = new ByteArrayInputStream(pdf); + //byte [] normalized_pdf = TextualSignature.normalizePDF(original_bais); + byte [] normalized_pdf = TextualSignature.normalizePDF(pdfDataSource.createInputStream()); + // PERF: The whole PDF normalization process is costy ByteArrayInputStream bais = new ByteArrayInputStream(normalized_pdf); PDFParser parser = new PDFParser(bais); diff --git a/src/main/java/at/knowcenter/wag/egov/egiz/pdf/SignatureHolder.java b/src/main/java/at/knowcenter/wag/egov/egiz/pdf/SignatureHolder.java index d7fcce9..aa8da2c 100644 --- a/src/main/java/at/knowcenter/wag/egov/egiz/pdf/SignatureHolder.java +++ b/src/main/java/at/knowcenter/wag/egov/egiz/pdf/SignatureHolder.java @@ -17,6 +17,7 @@ */ package at.knowcenter.wag.egov.egiz.pdf; +import at.gv.egiz.pdfas.framework.input.DataSource; import at.knowcenter.wag.egov.egiz.sig.SignatureObject; /** @@ -36,21 +37,21 @@ import at.knowcenter.wag.egov.egiz.sig.SignatureObject; public interface SignatureHolder { - /** - * Returns the signed text (verification) or the to-be-signed signable text - * (signation). - * - *

- * Note that this text must be the one that was actually signed. This text is - * directly passed to the connector for signation/verification. No - * normalization or modification will be / must be done to this text between - * reading out from the signature holder and passing the text to the - * connector. - *

- * - * @return Returns the signed text or the to-be-signed signable text. - */ - public String getSignedText(); +// /** +// * Returns the signed text (verification) or the to-be-signed signable text +// * (signation). +// * +// *

+// * Note that this text must be the one that was actually signed. This text is +// * directly passed to the connector for signation/verification. No +// * normalization or modification will be / must be done to this text between +// * reading out from the signature holder and passing the text to the +// * connector. +// *

+// * +// * @return Returns the signed text or the to-be-signed signable text. +// */ +// public String getSignedText(); /** * @@ -59,4 +60,9 @@ public interface SignatureHolder */ public SignatureObject getSignatureObject(); + /** + * Returns the DataSource providing the data. + * @return + */ + public DataSource getDataSource(); } \ No newline at end of file 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 a52d6dd..668bbcb 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 @@ -23,6 +23,9 @@ import java.io.File; import java.io.IOException; import java.io.InputStream; +import at.gv.egiz.pdfas.performance.PerformanceCounters; +import at.gv.egiz.pdfas.exceptions.pdf.TextExtractionException; + import org.apache.log4j.Logger; import org.pdfbox.pdfparser.PDFParser; import org.pdfbox.pdmodel.PDDocument; @@ -31,7 +34,6 @@ import org.pdfbox.util.PDFTextStripper; import at.knowcenter.wag.egov.egiz.cfg.ConfigLogger; import at.knowcenter.wag.egov.egiz.cfg.SettingsReader; import at.knowcenter.wag.egov.egiz.exceptions.PresentableException; -import at.knowcenter.wag.egov.egiz.framework.signators.TextualSignator_1_0_0; import com.lowagie.text.Document; import com.lowagie.text.DocumentException; @@ -52,6 +54,7 @@ public class TextualSignature * The logger definition. */ private static final Logger logger_ = ConfigLogger.getLogger(TextualSignature.class); + /** * Extracts the document text from a given pdf. * @@ -61,8 +64,10 @@ public class TextualSignature * @throws PresentableException * Forwarded exception. */ - public static String extractTextTextual(InputStream pdf_stream) throws PresentableException + public static String extractTextTextual(InputStream pdf_stream) throws TextExtractionException { + PerformanceCounters.textExtractions.increment(); + try { int first_page_rotation = 0; @@ -79,8 +84,10 @@ public class TextualSignature //iText PdfReader reader = new PdfReader(pdf_stream); + pdf_stream.close(); - ByteArrayOutputStream baos = new ByteArrayOutputStream(); + // PERF: PDF normalization needs byte array - this is costy + ByteArrayOutputStream baos = new ByteArrayOutputStream(4096); // For some reason the Reader -> ImportPage -> Writer mechanism produces // problems en mass. @@ -136,7 +143,7 @@ public class TextualSignature // } baos.close(); - byte[] normalizedPDF = baos.toByteArray(); + byte[] normalizedPDF = baos.toByteArray(); ByteArrayInputStream bais = new ByteArrayInputStream(normalizedPDF); //PDFBox-parser @@ -164,9 +171,13 @@ public class TextualSignature return text; } - catch (Exception e) + catch (IOException e) + { + throw new TextExtractionException(e); + } + catch (DocumentException e) { - throw new PresentableException(e); + throw new TextExtractionException(e); } } @@ -195,6 +206,9 @@ public class TextualSignature { //iText PdfReader reader = new PdfReader(input_pdf); + input_pdf.close(); + + // PERF: PDF Normalization needs byte array ByteArrayOutputStream baos = new ByteArrayOutputStream(); // For some reason the Reader -> ImportPage -> Writer mechanism produces // problems en mass. diff --git a/src/main/java/at/knowcenter/wag/egov/egiz/pdf/TextualSignatureHolder.java b/src/main/java/at/knowcenter/wag/egov/egiz/pdf/TextualSignatureHolder.java index fd56125..0971f90 100644 --- a/src/main/java/at/knowcenter/wag/egov/egiz/pdf/TextualSignatureHolder.java +++ b/src/main/java/at/knowcenter/wag/egov/egiz/pdf/TextualSignatureHolder.java @@ -19,6 +19,9 @@ package at.knowcenter.wag.egov.egiz.pdf; import java.io.Serializable; +import at.gv.egiz.pdfas.framework.input.DataSource; +import at.gv.egiz.pdfas.framework.input.TextDataSource; +import at.gv.egiz.pdfas.impl.input.TextDataSourceImpl; import at.knowcenter.wag.egov.egiz.sig.SignatureObject; /** @@ -35,34 +38,44 @@ public class TextualSignatureHolder implements Serializable, SignatureHolder */ private static final long serialVersionUID = -7208103904479272760L; - /** - * The signed text of this object. - * - *

- * This is the value that will be signed by the Connector. - *

- */ - private String signed_text = null; +// /** +// * The signed text of this object. +// * +// *

+// * This is the value that will be signed by the Connector. +// *

+// */ +// private String signed_text = null; /** * The signature object. */ private SignatureObject signature_object = null; + + private TextDataSource textDataSource = null; public TextualSignatureHolder(String text, SignatureObject so) { - this.signed_text = text; + //this.signed_text = text; this.signature_object = so; + this.textDataSource = new TextDataSourceImpl(text); } - /** - * @see at.knowcenter.wag.egov.egiz.pdf.SignatureHolder#getSignedText() - */ - public String getSignedText() + public TextualSignatureHolder(TextDataSource text, SignatureObject so) { - return this.signed_text; + //this.signed_text = text; + this.signature_object = so; + this.textDataSource = text; } +// /** +// * @see at.knowcenter.wag.egov.egiz.pdf.SignatureHolder#getSignedText() +// */ +// public String getSignedText() +// { +// return this.signed_text; +// } + /** * @see at.knowcenter.wag.egov.egiz.pdf.SignatureHolder#getSignatureObject() */ @@ -70,4 +83,37 @@ public class TextualSignatureHolder implements Serializable, SignatureHolder { return this.signature_object; } + + /** + * @see at.knowcenter.wag.egov.egiz.pdf.SignatureHolder#getDataSource() + */ + public DataSource getDataSource() + { + return this.textDataSource; + } + + /** + * This is used to replace the DataSource. + * + *

+ * After processing the text, data sources containing large texts are usually replaced by a FileBased ones to save memory. + *

+ * + * @param tds + */ + public void exchangeDataSource(TextDataSource tds) + { + this.textDataSource = tds; + } + + /** + * This is just a shortcut to getDataSource().getText() + * @return Returns the text of this data source. + */ + public String getSignedText() + { + return this.textDataSource.getText(); + } + + } -- cgit v1.2.3