From 8e3ed85168edaeadcd4f3ec92450036a399d6ede Mon Sep 17 00:00:00 2001 From: knowcenter Date: Sat, 12 May 2007 17:37:16 +0000 Subject: Detached Multipart BKU for Auftrag "Support" git-svn-id: https://joinup.ec.europa.eu/svn/pdf-as/trunk@73 7b5415b0-85f9-ee4d-85bd-d5d0c3b42d1c --- .../java/at/knowcenter/wag/egov/egiz/PdfAS.java | 250 ++++++++++++--------- 1 file changed, 143 insertions(+), 107 deletions(-) (limited to 'src/main/java/at/knowcenter/wag/egov/egiz/PdfAS.java') 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 313a11c..8de93c0 100644 --- a/src/main/java/at/knowcenter/wag/egov/egiz/PdfAS.java +++ b/src/main/java/at/knowcenter/wag/egov/egiz/PdfAS.java @@ -53,10 +53,16 @@ import at.knowcenter.wag.egov.egiz.pdf.TablePos; import at.knowcenter.wag.egov.egiz.pdf.TextualSignature; import at.knowcenter.wag.egov.egiz.sig.Connector; import at.knowcenter.wag.egov.egiz.sig.ConnectorFactory; +import at.knowcenter.wag.egov.egiz.sig.SignatureData; +import at.knowcenter.wag.egov.egiz.sig.SignatureDataImpl; import at.knowcenter.wag.egov.egiz.sig.SignatureObject; 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.connectors.bku.BKUPostConnection; +import at.knowcenter.wag.egov.egiz.sig.connectors.bku.DetachedMultipartBKUConnector; +import at.knowcenter.wag.egov.egiz.sig.connectors.bku.SignSignatureObject; +import at.knowcenter.wag.egov.egiz.tools.CodingHelper; import at.knowcenter.wag.egov.egiz.tools.Normalizer; import at.knowcenter.wag.exactparser.ParseDocument; import at.knowcenter.wag.exactparser.parsing.PDFUtils; @@ -390,7 +396,7 @@ public abstract class PdfAS for (int key_idx = 0; key_idx < keys.size(); key_idx++) { String key = (String) keys.get(key_idx); - //logger_.debug("Key="+key); + // logger_.debug("Key="+key); if (old_style && key.equals(SignatureTypes.SIG_KZ)) { // If separating the old style way - skip The "Kennzeichnung" @@ -402,15 +408,19 @@ public abstract class PdfAS // int found_index = text.lastIndexOf(caption); // we're searching for captions that start at the beginning of the line. - int found_index =text.lastIndexOf("\n" +caption) + 1;//text.lastIndexOf("\n" + caption) + 1; // the +1 text.lastIndexOf(caption) + 1; // + int found_index = text.lastIndexOf("\n" + caption) + 1;// text.lastIndexOf("\n" + // + caption) + 1; + // // the +1 + // text.lastIndexOf(caption) + // + 1; // // compensates the // \n if (found_index == 0) { - //try without /n - found_index = text.lastIndexOf(caption); + // try without /n + found_index = text.lastIndexOf(caption); } - logger_.debug("found key:"+caption+" at index:"+found_index); + logger_.debug("found key:" + caption + " at index:" + found_index); if (key.equals(SignatureTypes.SIG_ID)) { if (found_index < 0 || found_index >= last_index) @@ -430,16 +440,16 @@ public abstract class PdfAS } FoundKey fk = new FoundKey(key, caption, found_index); - found_keys.add(fk); + found_keys.add(fk); last_index = found_index; } } sortFoundKeysDescendingly(found_keys); - + boolean matched = checkThatOrderIsCorrectAndCorrectFoundKeys(found_keys, keys, old_style); // boolean found_required = checkFoundRequiredKeys(found_keys, old_style); - //logger_.debug("KKKKKKKKKKmatched="+matched); + // logger_.debug("KKKKKKKKKKmatched="+matched); if (matched) { return found_keys; @@ -780,7 +790,7 @@ public abstract class PdfAS String connector) throws NormalizeException, PDFDocumentException, SignatureException { String text_to_be_verified = signature_holder.getSignedText(); - //logger_.debug("verify text_to_be_verified"+text_to_be_verified); + // logger_.debug("verify text_to_be_verified"+text_to_be_verified); SignatureObject so_to_be_verified = signature_holder.getSignatureObject(); if (text_to_be_verified == null) @@ -799,10 +809,31 @@ public abstract class PdfAS try { - Connector connector_impl = ConnectorFactory.createConnector(connector); - return connector_impl.doVerify(text_to_be_verified, so_to_be_verified); + DetachedMultipartBKUConnector connector_impl = new DetachedMultipartBKUConnector("xxxprofile"); + + // FIXME this has to be made better + SignatureData sd = null; + if (so_to_be_verified.isBinary()) + { + byte[] data = CodingHelper.decodeBase64(text_to_be_verified); + sd = new SignatureDataImpl(data, "application/pdf"); + } + else + { + sd = new SignatureDataImpl(text_to_be_verified.getBytes("UTF-8"), "text/plain", "UTF-8"); + } + + SignSignatureObject so = new SignSignatureObject(); + so.date = so_to_be_verified.getSignationDate(); + so.signatureValue = so_to_be_verified.getSignationValue(); + so.issuer = so_to_be_verified.getSignationIssuer(); + so.x509Certificate = so_to_be_verified.getX509Cert().getX509Certificate(); + so.id = so_to_be_verified.getSignationIds(); + + // Connector connector_impl = ConnectorFactory.createConnector(connector); + return connector_impl.doVerify(sd, so); } - catch (ConnectorFactoryException e) + catch (Exception e) { throw new SignatureException(310, e); } @@ -812,46 +843,46 @@ public abstract class PdfAS * Signs the given text with the provided connector using the given signature * type. * - * @param text_to_sign - * The text String to be signed. + * @param data_to_sign + * The data to be signed. * @param signature_type * The type of the signature. * @param connector * The connector. - * @param user_name - * The user name. - * @param user_password - * The user password. * @return Returns the corresponding SignatureObject. * @throws SignatureException * F.e. * @throws PDFDocumentException * F.e. */ - public static SignatureObject sign(final String text_to_sign, - final String signature_type, final String connector, - final String user_name, final String user_password) throws SignatureException, PDFDocumentException + public static SignSignatureObject sign(final SignatureData data_to_sign, + final String signature_type, final String connector) throws SignatureException, PDFDocumentException { - logger_.info("User signed a document: " + user_name); - if (text_to_sign == null) + if (data_to_sign == null || data_to_sign.getData() == null) { - throw new SignatureException(301, "Signature can not be produced. Text is null."); + throw new SignatureException(301, "Signature can not be produced. Data is null."); //$NON-NLS-1$ } - if (text_to_sign.length() <= 0) + if (data_to_sign.getData().length <= 0) { - throw new SignatureException(301, "Signature can not be produced. Text is empty. (length = " + text_to_sign.length() + ")"); + throw new SignatureException(301, "Signature can not be produced. Data is empty. (length = " + data_to_sign.getData().length + ")"); //$NON-NLS-1$ //$NON-NLS-2$ } try { - Connector connector_impl = ConnectorFactory.createConnector(connector); - SignatureObject signed_signature_object = connector_impl.doSign(signature_type, user_name, text_to_sign); + // TODO temporary workaround: + DetachedMultipartBKUConnector connector_impl = new DetachedMultipartBKUConnector(signature_type); + // Connector connector_impl = ConnectorFactory.createConnector(connector); + SignSignatureObject signed_signature_object = connector_impl.doSign(data_to_sign); return signed_signature_object; } - catch (ConnectorFactoryException e) + catch (SettingsException e) { throw new SignatureException(300, e); } + // catch (ConnectorFactoryException e) + // { + // throw new SignatureException(300, e); + // } } /** @@ -903,10 +934,10 @@ public abstract class PdfAS final String user_name, final String user_password, TablePos pos) throws PresentableException { Signator signator = SignatorFactory.createSignator(algorithm); - + IncrementalUpdateInformation iui = signator.prepareSign(pdf, signature_type, pos, ConnectorFactory.needsSIG_ID(connector)); - iui.signed_signature_object = sign(iui.document_text, signature_type, connector, user_name, user_password); + iui.signed_signature_object = sign(iui.signature_data, signature_type, connector); SignResult sign_result = signator.finishSign(iui); @@ -949,9 +980,11 @@ public abstract class PdfAS { ByteArrayInputStream bais = new ByteArrayInputStream(pdf, 0, length); String raw_document_text = TextualSignature.extractTextTextual(bais); - //logger_.info("extractNormalizedTextTextual raw_document_text="+raw_document_text); + // logger_.info("extractNormalizedTextTextual + // raw_document_text="+raw_document_text); String document_text = normalizeText(raw_document_text); - //logger_.info("extractNormalizedTextTextual document_text ="+document_text); + // logger_.info("extractNormalizedTextTextual document_text + // ="+document_text); return document_text; } @@ -1024,12 +1057,14 @@ public abstract class PdfAS } if (pos == null) { - // The default algorithm. x,y,w =auto ,p=lastpage, f:ignored because y:auto - pos = new TablePos(); + // The default algorithm. x,y,w =auto ,p=lastpage, f:ignored because + // y:auto + pos = new TablePos(); } - //System.out.println("Tablepos="+pos); - return PdfAS.adjustSignatureTableandCalculatePosition(pdf, pdf_table, pos); + // System.out.println("Tablepos="+pos); + return PdfAS.adjustSignatureTableandCalculatePosition(pdf, pdf_table, pos); } + /** * Sets the width of the table according to the layout of the document and * calculates the y position where the PDFPTable should be placed. @@ -1045,125 +1080,125 @@ public abstract class PdfAS public static PositioningInstruction adjustSignatureTableandCalculatePosition( final byte[] pdf, PdfPTable pdf_table, TablePos pos) throws PDFDocumentException { - //first check pageinstruction in TablePos-object - //new,auto,absolut - PdfReader reader = readInPdfDocument(pdf); - //get pages of currentdocument - int doc_pages = reader.getNumberOfPages(); - int page = doc_pages; - boolean make_new_page = pos.isNewPage(); - if(!(pos.isNewPage() || pos.isPauto())) - { - //we should posit signaturtable on this page - - page = pos.getPage(); - //System.out.println("XXXXPAGE="+page+" doc_pages="+doc_pages); - if (page > doc_pages) - { - make_new_page = true; - page = doc_pages; - //throw new PDFDocumentException(227, "Page number is to big(=" + page+ ") cannot be parsed."); - } - } - - //getPagedimensions + // first check pageinstruction in TablePos-object + // new,auto,absolut + PdfReader reader = readInPdfDocument(pdf); + // get pages of currentdocument + int doc_pages = reader.getNumberOfPages(); + int page = doc_pages; + boolean make_new_page = pos.isNewPage(); + if (!(pos.isNewPage() || pos.isPauto())) + { + // we should posit signaturtable on this page + + page = pos.getPage(); + // System.out.println("XXXXPAGE="+page+" doc_pages="+doc_pages); + if (page > doc_pages) + { + make_new_page = true; + page = doc_pages; + // throw new PDFDocumentException(227, "Page number is to big(=" + page+ + // ") cannot be parsed."); + } + } + + // getPagedimensions Rectangle psize = reader.getPageSizeWithRotation(page); int page_rotation = reader.getPageRotation(page); - + float page_width = psize.width(); float page_height = psize.height(); - //now we can calculate x-position + // now we can calculate x-position float pre_pos_x = SIGNATURE_BORDER / 2; if (!pos.isXauto()) { - //we do have absolute x + // we do have absolute x pre_pos_x = pos.getPosX(); - } - //calculate width - //center + } + // calculate width + // center float pre_width = page_width - pre_pos_x - pre_pos_x; if (!pos.isWauto()) { - //we do have absolute width + // we do have absolute width pre_width = pos.getWidth(); if (pos.isXauto()) - { //center x + { // center x pre_pos_x = (page_width - pre_width) / 2; } - } + } final float pos_x = pre_pos_x; final float width = pre_width; - //Signatur table dimensions are complete + // Signatur table dimensions are complete pdf_table.setTotalWidth(width); pdf_table.setLockedWidth(true); final float table_height = pdf_table.getTotalHeight(); - //now check pos_y + // now check pos_y float pos_y = pos.getPosY(); if (!pos.isYauto()) { - //we do have y-position too --> all parameters but page ok - if (make_new_page) - { - page++; - } - return new PositioningInstruction(make_new_page, page, pos_x, pos_y); + // we do have y-position too --> all parameters but page ok + if (make_new_page) + { + page++; + } + return new PositioningInstruction(make_new_page, page, pos_x, pos_y); } - //pos_y is auto + // pos_y is auto if (make_new_page) { - //ignore footer in new page - page++; - pos_y = page_height - SIGNATURE_BORDER / 2; - return new PositioningInstruction(make_new_page, page, pos_x, pos_y); - } - //up to here no checks have to be made if Tablesize and Pagesize are fit - //Now we have to getfreespace in page and reguard footerline + // ignore footer in new page + page++; + pos_y = page_height - SIGNATURE_BORDER / 2; + return new PositioningInstruction(make_new_page, page, pos_x, pos_y); + } + // up to here no checks have to be made if Tablesize and Pagesize are fit + // Now we have to getfreespace in page and reguard footerline float footer_line = pos.getFooterLine(); - float pre_page_length = PDFUtilities.calculatePageLength(pdf, page-1, page_height-footer_line, page_rotation); + float pre_page_length = PDFUtilities.calculatePageLength(pdf, page - 1, page_height - footer_line, page_rotation); if (pre_page_length == Float.NEGATIVE_INFINITY) { - //we do have an empty page or nothing in area above footerline - pre_page_length = page_height; - //no text --> SIGNATURE_BORDER + // we do have an empty page or nothing in area above footerline + pre_page_length = page_height; + // no text --> SIGNATURE_BORDER pos_y = page_height - SIGNATURE_BORDER / 2; if (pos_y - footer_line <= table_height) { make_new_page = true; if (!pos.isPauto()) - { - //we have to correct pagenumber - page = reader.getNumberOfPages(); + { + // we have to correct pagenumber + page = reader.getNumberOfPages(); } page++; - //no text --> SIGNATURE_BORDER - pos_y = page_height - SIGNATURE_BORDER / 2; + // no text --> SIGNATURE_BORDER + pos_y = page_height - SIGNATURE_BORDER / 2; } - return new PositioningInstruction(make_new_page, page, pos_x, pos_y); - } + return new PositioningInstruction(make_new_page, page, pos_x, pos_y); + } final float page_length = pre_page_length; - //we do have text take SIGNATURE_MARGIN + // we do have text take SIGNATURE_MARGIN pos_y = page_height - page_length - SIGNATURE_MARGIN; if (pos_y - footer_line <= table_height) { make_new_page = true; if (!pos.isPauto()) - { - //we have to correct pagenumber in case of absolute page and not enough space - page = reader.getNumberOfPages(); + { + // we have to correct pagenumber in case of absolute page and not enough + // space + page = reader.getNumberOfPages(); } page++; - //no text --> SIGNATURE_BORDER - pos_y = page_height - SIGNATURE_BORDER / 2; + // no text --> SIGNATURE_BORDER + pos_y = page_height - SIGNATURE_BORDER / 2; } return new PositioningInstruction(make_new_page, page, pos_x, pos_y); } - - - /** * Sets the width of the table according to the layout of the document and * calculates the y position where the PDFPTable should be placed. + * * @deprecated * @param pdf * The PDF document. @@ -1214,6 +1249,7 @@ public abstract class PdfAS * This algorithm tries to position the table between the end of the text and * the footer line. *

+ * * @deprecated * @param pdf * The PDF document. @@ -1286,14 +1322,14 @@ public abstract class PdfAS * Parses the TablePos object from a given String with the appropriate format. * * @param pos_string - * The pos string. e.g. x:40.0;y:auto;w:auto;p:1;f:300.0 + * The pos string. e.g. x:40.0;y:auto;w:auto;p:1;f:300.0 * @return Returns the parsed TablePos object. * @throws PDFDocumentException * Thrown, if the String doesn't have the proper format. */ public static TablePos parsePositionFromPosString(String pos_string) throws PDFDocumentException { - TablePos pos = new TablePos(pos_string); - return pos; + TablePos pos = new TablePos(pos_string); + return pos; } } -- cgit v1.2.3