aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/knowcenter/wag
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/at/knowcenter/wag')
-rw-r--r--src/main/java/at/knowcenter/wag/egov/egiz/pdf/AdobeSignatureHelper.java187
1 files changed, 187 insertions, 0 deletions
diff --git a/src/main/java/at/knowcenter/wag/egov/egiz/pdf/AdobeSignatureHelper.java b/src/main/java/at/knowcenter/wag/egov/egiz/pdf/AdobeSignatureHelper.java
new file mode 100644
index 0000000..a391c15
--- /dev/null
+++ b/src/main/java/at/knowcenter/wag/egov/egiz/pdf/AdobeSignatureHelper.java
@@ -0,0 +1,187 @@
+package at.knowcenter.wag.egov.egiz.pdf;
+
+import java.util.HashMap;
+
+import javax.xml.datatype.DatatypeFactory;
+import javax.xml.datatype.XMLGregorianCalendar;
+
+import org.apache.log4j.Logger;
+
+import at.gv.egiz.pdfas.exceptions.ErrorCode;
+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.PresentableException;
+import at.knowcenter.wag.egov.egiz.exceptions.SettingsException;
+import at.knowcenter.wag.egov.egiz.sig.SignatureObject;
+
+import com.lowagie.text.Rectangle;
+import com.lowagie.text.pdf.PdfDate;
+import com.lowagie.text.pdf.PdfDictionary;
+import com.lowagie.text.pdf.PdfName;
+import com.lowagie.text.pdf.PdfSignature;
+import com.lowagie.text.pdf.PdfSignatureAppearance;
+import com.lowagie.text.pdf.PdfStamper;
+import com.lowagie.text.pdf.PdfString;
+
+/**
+ * Helper class for creating adobe signature attributes.
+ *
+ * @author dferbas
+ *
+ */
+public class AdobeSignatureHelper {
+ private static final String ADOBE_SIGN_FIELDNAME_KEY = "adobeSignFieldName";
+
+ private static final String ADOBE_SIG_ENABLED_KEY = "adobeSignEnabled";
+
+ private static Logger logger = Logger.getLogger(AdobeSignatureHelper.class);
+
+ public static final String ADOBE_SIG_FILTER = "Adobe.PDF-AS";
+
+ public static final String ADOBE_SIG_TEXT_KEY = "adobeSignText";
+
+ /**
+ * Writes Adobe-pdf signature entry with itext
+ *
+ * @param stamper
+ * @param si
+ * @param so
+ * @param atp
+ * @throws PresentableException
+ */
+ public static void createAdobeSignatureField(PdfStamper stamper, SignatorInformation si,
+ SignatureObject so, ActualTablePos atp) throws PresentableException {
+
+ try {
+ logger.info("create adobe signature field");
+ PdfSignatureAppearance sap = stamper.getSignatureAppearance();
+
+ String fieldName = getAdobeFieldName(so.getSignatureTypeDefinition().getType());
+ // find field num
+ int nexSigNum = 1;
+ while (stamper.getAcroFields().getField(fieldName + nexSigNum) != null) {
+ nexSigNum++;
+ }
+
+ 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, fieldName + " #" + nexSigNum);
+ 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_SIG_FILTER), 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(getAdobeSignText(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);
+ }
+ }
+
+
+ /**
+ * Returns if adobe signature is enabled for the passed signature profile.
+ * Config key: {@value #ADOBE_SIG_ENABLED_KEY}
+ * @param sigProfile
+ * @return
+ */
+ public static boolean isAdobeSignatureFieldEnabled(String sigProfile) {
+ return "true".equalsIgnoreCase(
+ getDefaultableConfigProperty(sigProfile, ADOBE_SIG_ENABLED_KEY, "false"));
+ }
+
+ protected static Rectangle createRectangleFromTablePos(ActualTablePos pos) {
+ return new Rectangle(pos.x, pos.y, pos.x + pos.width, pos.y - pos.height);
+ }
+
+ private static String getAdobeFieldName(String sigProfile) {
+ return getDefaultableConfigProperty(sigProfile, ADOBE_SIGN_FIELDNAME_KEY, "PDF-AS-Signatur");
+ }
+
+ private static String getDefaultableConfigProperty(String sigProfile, String propName, String defaultValue) {
+ String confVal;
+ try {
+ confVal = SettingsReader.getInstance().getSetting(
+ "sig_obj." + sigProfile + "." + propName,
+ "default." + propName,
+ defaultValue);
+ } catch (SettingsException e) {
+ logger.warn("error reading " + propName + " from config. Using default: " + defaultValue, e);
+ return defaultValue;
+ }
+ return confVal;
+ }
+
+ /**
+ * Evaluate name for adobe signature field. Get from config. Evaluate ognl if
+ * ok.
+ *
+ * @param sigProfile
+ * @param si
+ * @return
+ */
+ private static String getAdobeSignText(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 = getDefaultableConfigProperty(sigProfile, 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 '" + defaultName + "'", ex);
+ return defaultName;
+ }
+ }
+
+}