From d6489d303d7766dccec7c748f9d4fbb72bb1415d Mon Sep 17 00:00:00 2001 From: ferbas Date: Thu, 10 Dec 2009 09:29:07 +0000 Subject: refactored adobe signature field git-svn-id: https://joinup.ec.europa.eu/svn/pdf-as/trunk@494 7b5415b0-85f9-ee4d-85bd-d5d0c3b42d1c --- .../wag/egov/egiz/pdf/BinarySignature.java | 124 ++------------------- 1 file changed, 12 insertions(+), 112 deletions(-) (limited to 'src/main') 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 ebf7d42..4672cc5 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 @@ -28,12 +28,9 @@ import java.security.cert.X509Certificate; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; -import java.util.HashMap; import java.util.Iterator; import java.util.List; -import javax.xml.datatype.DatatypeFactory; -import javax.xml.datatype.XMLGregorianCalendar; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -45,7 +42,6 @@ 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.OgnlUtil; import at.knowcenter.wag.egov.egiz.cfg.SettingsReader; import at.knowcenter.wag.egov.egiz.exceptions.PDFDocumentException; import at.knowcenter.wag.egov.egiz.exceptions.PlaceholderException; @@ -65,7 +61,6 @@ import com.lowagie.text.Rectangle; import com.lowagie.text.pdf.PRStream; import com.lowagie.text.pdf.PdfArray; import com.lowagie.text.pdf.PdfContentByte; -import com.lowagie.text.pdf.PdfDate; import com.lowagie.text.pdf.PdfDictionary; import com.lowagie.text.pdf.PdfIndirectObject; import com.lowagie.text.pdf.PdfIndirectReference; @@ -74,8 +69,6 @@ import com.lowagie.text.pdf.PdfNumber; import com.lowagie.text.pdf.PdfObject; import com.lowagie.text.pdf.PdfPTable; import com.lowagie.text.pdf.PdfReader; -import com.lowagie.text.pdf.PdfSignature; -import com.lowagie.text.pdf.PdfSignatureAppearance; import com.lowagie.text.pdf.PdfStamper; import com.lowagie.text.pdf.PdfStamperImp; import com.lowagie.text.pdf.PdfString; @@ -241,8 +234,6 @@ public abstract class BinarySignature */ protected static final PdfNumber NUMBER_PLACEHOLDER = new PdfNumber(99999999); - public static final String ADOBE_SIG_TEXT_KEY = "adobeSignText"; - /** * Extracts the signature text only. * @@ -812,8 +803,13 @@ public abstract class BinarySignature // IMPORTANT: append the new content to the original document using // incremental updated // The stamper allows this by setting append = true - //PdfStamper stamper = new PdfStamper(reader, baos, '\0', true); - PdfStamper stamper = PdfStamper.createSignature(reader, baos, '\0', null, true); + boolean adobeSigField = AdobeSignatureHelper.isAdobeSignatureFieldEnabled(so.getSignatureTypeDefinition().getType()); + PdfStamper stamper = null; + if (adobeSigField) { + stamper = PdfStamper.createSignature(reader, baos, '\0', null, true); + } else { + stamper = new PdfStamper(reader, baos, '\0', true); + } if (pi.isMakeNewPage()) { @@ -909,7 +905,11 @@ public abstract class BinarySignature } //stamper.close(); - createAdobeSigAttrib(stamper, si, so, atp); // here..., stamper is closed + if (adobeSigField) { + AdobeSignatureHelper.createAdobeSignatureField(stamper, si, so, atp); // here..., stamper is closed + } else { + stamper.close(); + } // System.out.println("wprinz: STAMPING FINISHED"); // just to make sure the stream is really closed @@ -930,106 +930,6 @@ public abstract class BinarySignature } } - /** - * Writes Adobe-pdf signature entry with itext - * @param stamper - * @param si - * @param so - * @param atp - * @throws PresentableException - */ - public static void createAdobeSigAttrib(PdfStamper stamper, SignatorInformation si, SignatureObject so, ActualTablePos atp) - throws PresentableException { - - try { - logger.info("create adobe signature field"); - PdfSignatureAppearance sap = stamper.getSignatureAppearance(); - - sap.setCrypto(null, null, null, null); - // supress overlay text for visible signatures - sap.setLayer2Text(""); - sap.setLayer4Text(""); - - // the following line marks the sig block as adobe sig - //sap.setVisibleSignature(createRectangleFromTablePos(iui.actualTablePos), iui.actualTablePos.page, "PDF-AS-Signatur"); - sap.setVisibleSignature(new Rectangle(0, 0, 0, 0), atp.page, "PDF-AS-Signatur"); - String subfilter = "unknown"; - if (so != null && so.getKZ() != null) { - subfilter = so.getKZ().toString(); - } else if (si != null) { - subfilter = si.getSignSignatureObject().kz; - } - PdfSignature sig = new PdfSignature(new PdfName("Adobe.PDF-AS"), new PdfName(subfilter)); - // the following fields are not shown by the reader, because its is no Standard filter - //sig.setLocation("location is not visible"); - //sig.setReason("reason is not visible"); - //sig.setContact("contact is not visible"); - sig.setDate(new PdfDate()); - if (si != null) { - XMLGregorianCalendar c = DatatypeFactory.newInstance().newXMLGregorianCalendar(si.getSignSignatureObject().date); - sig.setDate(new PdfDate(c.toGregorianCalendar())); - } - sig.setName(getAdobeSigName(so.getSignatureTypeDefinition().getType(), si)); - sap.setCryptoDictionary(sig); - - // content element is mandatory but empty - HashMap exc = new HashMap(); - exc.put(PdfName.CONTENTS, new Integer(2)); - sap.preClose(exc); // *2+2 - - PdfDictionary dic = new PdfDictionary(); - dic.put(PdfName.CONTENTS, new PdfString((String)null).setHexWriting(true)); - sap.close(dic); - } catch (Exception ex) { - logger.error("error", ex); - throw new PresentableException(ErrorCode.CANNOT_WRITE_PDF, "Error creating adobe signature attribute", ex); - } - } - - /** - * Evaluate name for adobe signature field. Get from config. Evaluate ognl if ok. - * @param sigProfile - * @param si - * @return - */ - private static String getAdobeSigName(String sigProfile, SignatorInformation si) { - String defaultName = "PDF-AS"; - try { - logger.debug("reading adobe sig name for profile: " + sigProfile); - String propKey = ADOBE_SIG_TEXT_KEY + ".textual"; - if (si == null) { - propKey = propKey.replaceAll("textual", "binary"); - } - String adobeStr = SettingsReader.getInstance().getSetting( - "sig_obj." +sigProfile+"."+propKey, - "default."+propKey, defaultName); - - HashMap ognlCtx = new HashMap(); - OgnlUtil ognl = new OgnlUtil(ognlCtx); - if (ognl.containsExpression(adobeStr)) { - if (si == null) { - logger.error( ADOBE_SIG_TEXT_KEY + - " ognl expressions not allowed for binary signatures (SignatorInformation not available)"); - return defaultName; - } - ognlCtx.put("si", si); - ognlCtx.put("sso", si.getSignSignatureObject()); - String res = ognl.compileMessage(adobeStr); - return res; - } else { - return adobeStr; - } - - } catch (Exception ex) { - logger.warn("error creating adobe sign text, using default 'PDF-AS'", ex); - return defaultName; - } - } - - private static Rectangle createRectangleFromTablePos(ActualTablePos pos) { - return new Rectangle(pos.x, pos.y, pos.x + pos.width, pos.y - pos.height); - } - /** * Creates the EGIZ Dictionary and adds it to the document. * -- cgit v1.2.3