aboutsummaryrefslogtreecommitdiff
path: root/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping
diff options
context:
space:
mode:
Diffstat (limited to 'pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping')
-rw-r--r--pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/CertificateResolver.java46
-rw-r--r--pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/IPDFStamper.java3
-rw-r--r--pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/IResolver.java14
-rw-r--r--pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/StamperFactory.java7
-rw-r--r--pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/TableFactory.java307
-rw-r--r--pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/ValueResolver.java64
6 files changed, 439 insertions, 2 deletions
diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/CertificateResolver.java b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/CertificateResolver.java
new file mode 100644
index 00000000..f30c326d
--- /dev/null
+++ b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/CertificateResolver.java
@@ -0,0 +1,46 @@
+package at.gv.egiz.pdfas.lib.impl.stamping;
+
+import at.gv.egiz.pdfas.common.settings.SignatureProfileSettings;
+import at.gv.egiz.pdfas.common.utils.DNUtils;
+import at.gv.egiz.pdfas.common.utils.OgnlUtils;
+import iaik.x509.X509Certificate;
+import ognl.OgnlContext;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.naming.InvalidNameException;
+import javax.naming.ldap.LdapName;
+import java.util.Map;
+
+public class CertificateResolver implements IResolver {
+
+ private static final Logger logger = LoggerFactory.getLogger(CertificateResolver.class);
+
+ private OgnlContext ctx;
+ private X509Certificate certificate;
+
+ public CertificateResolver(X509Certificate certificate) {
+ this.certificate = certificate;
+ this.ctx = new OgnlContext();
+
+ try {
+ Map<String, String> issuerDNMap = DNUtils.dnToMap(certificate.getIssuerDN().getName());
+ this.ctx.put("issuer", issuerDNMap);
+ } catch (InvalidNameException e) {
+ logger.error("Failed to build issuer Map", e);
+ }
+
+ try {
+ Map<String, String> subjectDNMap = DNUtils.dnToMap(certificate.getSubjectDN().getName());
+ this.ctx.put("subject", subjectDNMap);
+ } catch (InvalidNameException e) {
+ logger.error("Failed to build subject Map", e);
+ }
+
+ }
+
+ public String resolve(String key, String value, SignatureProfileSettings settings) {
+ return OgnlUtils.resolvsOgnlExpression(value, this.ctx);
+ }
+
+}
diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/IPDFStamper.java b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/IPDFStamper.java
index f1b59ceb..fce18ce7 100644
--- a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/IPDFStamper.java
+++ b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/IPDFStamper.java
@@ -1,6 +1,7 @@
package at.gv.egiz.pdfas.lib.impl.stamping;
import at.gv.egiz.pdfas.common.exceptions.PdfAsException;
+import at.gv.egiz.pdfas.common.settings.ISettings;
import at.gv.egiz.pdfas.lib.impl.status.PDFObject;
import at.knowcenter.wag.egov.egiz.pdf.PositioningInstruction;
import at.knowcenter.wag.egov.egiz.table.Table;
@@ -9,4 +10,6 @@ public interface IPDFStamper {
public IPDFVisualObject createVisualPDFObject(PDFObject pdf, Table table);
public byte[] writeVisualObject(IPDFVisualObject visualObject, PositioningInstruction positioningInstruction,
byte[] pdfData) throws PdfAsException;
+
+ public void setSettings(ISettings settings);
}
diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/IResolver.java b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/IResolver.java
new file mode 100644
index 00000000..040daf33
--- /dev/null
+++ b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/IResolver.java
@@ -0,0 +1,14 @@
+package at.gv.egiz.pdfas.lib.impl.stamping;
+
+import at.gv.egiz.pdfas.common.settings.SignatureProfileSettings;
+
+/**
+ * Created with IntelliJ IDEA.
+ * User: afitzek
+ * Date: 9/11/13
+ * Time: 1:44 PM
+ * To change this template use File | Settings | File Templates.
+ */
+public interface IResolver {
+ public String resolve(String key, String value, SignatureProfileSettings settings);
+}
diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/StamperFactory.java b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/StamperFactory.java
index 1720057a..f04a955a 100644
--- a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/StamperFactory.java
+++ b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/StamperFactory.java
@@ -1,16 +1,19 @@
package at.gv.egiz.pdfas.lib.impl.stamping;
import at.gv.egiz.pdfas.common.exceptions.PdfAsException;
+import at.gv.egiz.pdfas.common.settings.ISettings;
public class StamperFactory {
public static final String DEFAULT_STAMPER_CLASS = "at.gv.egiz.pdfas.stmp.itext.ITextStamper";
- public static IPDFStamper createDefaultStamper() throws PdfAsException {
+ public static IPDFStamper createDefaultStamper(ISettings settings) throws PdfAsException {
try {
Class<? extends IPDFStamper> cls = (Class<? extends IPDFStamper>)
Class.forName(DEFAULT_STAMPER_CLASS);
- return cls.newInstance();
+ IPDFStamper stamper = cls.newInstance();
+ stamper.setSettings(settings);
+ return stamper;
} catch (Throwable e) {
throw new PdfAsException("NO STAMPER!", e);
}
diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/TableFactory.java b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/TableFactory.java
new file mode 100644
index 00000000..45b8b711
--- /dev/null
+++ b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/TableFactory.java
@@ -0,0 +1,307 @@
+package at.gv.egiz.pdfas.lib.impl.stamping;
+
+import at.gv.egiz.pdfas.common.settings.IProfileConstants;
+import at.gv.egiz.pdfas.common.settings.ISettings;
+import at.gv.egiz.pdfas.common.settings.SignatureProfileSettings;
+import at.knowcenter.wag.egov.egiz.pdf.sig.SignatureEntry;
+import at.knowcenter.wag.egov.egiz.table.Entry;
+import at.knowcenter.wag.egov.egiz.table.Style;
+import at.knowcenter.wag.egov.egiz.table.Table;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.*;
+
+import static at.gv.egiz.pdfas.common.utils.StringUtils.extractLastID;
+
+public class TableFactory implements IProfileConstants {
+
+ private static final Logger logger = LoggerFactory.getLogger(TableFactory.class);
+
+ /**
+ * The default style definition for images.
+ */
+ private static Style defaultImageStyle_ = new Style();
+
+ /**
+ * The default style definition for captions.
+ */
+ private static Style defaultCaptionStyle_ = new Style();
+
+ /**
+ * The default style definition for values.
+ */
+ private static Style defaultValueStyle_ = new Style();
+
+ /**
+ * Reference from signature key to there corresponding value
+ */
+ private static Hashtable sigEntries_ = new Hashtable(8);
+
+ static {
+ setDefaultStyles();
+ }
+
+ /**
+ * This method set the default styles for images, captions and values.
+ */
+ private static void setDefaultStyles()
+ {
+ defaultImageStyle_.setPadding(3);
+ defaultImageStyle_.setHAlign(Style.CENTER);
+ defaultImageStyle_.setVAlign(Style.MIDDLE);
+
+ defaultCaptionStyle_.setHAlign(Style.CENTER);
+ defaultCaptionStyle_.setVAlign(Style.MIDDLE);
+
+ defaultValueStyle_.setHAlign(Style.LEFT);
+ defaultValueStyle_.setVAlign(Style.MIDDLE);
+ }
+
+ /**
+ * This method creates an abstract signature table object. It takes all keys
+ * and values set by the signature object to create the corresponding abstract
+ * table object. The table definition is read from the settings file.
+ *
+ * @param tableID
+ * is the name of the table definition in the settings file
+ * @return a new abstract signature table
+ * @see at.knowcenter.wag.egov.egiz.table.Style
+ * @see at.knowcenter.wag.egov.egiz.table.Table
+ * @see at.knowcenter.wag.egov.egiz.table.Entry
+ */
+ public static Table createSigTable(SignatureProfileSettings profile, String tableID, ISettings configuration )
+ {
+ String table_key_prefix = SIG_OBJ + profile.getProfileID() + "." + TABLE;
+ String table_key = table_key_prefix + tableID;
+ // String caption_prefix = SignatureTypes.SIG_OBJ + getSignationType() +
+ // ".key.";
+ // String value_prefix = SignatureTypes.SIG_OBJ + getSignationType() +
+ // ".value.";
+ // ArrayList table_def_keys = settings_.getKeys(table_key);
+ Vector<String> table_defs = configuration.getFirstLevelKeys(table_key);
+ if (table_defs == null)
+ {
+ return null;
+ }
+ Table sig_table = new Table(tableID);
+ //SignatureProfileSettings profile = createProfile(profileID);
+ boolean found_style = false;
+ Iterator<String> table_def_iter = table_defs.iterator();
+ while(table_def_iter.hasNext())
+ {
+ String table_def_key = table_def_iter.next();
+ int dot_idx = (table_def_key.lastIndexOf(".") > 0 ? table_def_key.lastIndexOf(".") + 1 : table_def_key.length());
+ String table_def = table_def_key.substring(dot_idx);
+ String table_def_keys_prefix = table_def_key.substring(0, dot_idx-1);
+ String table_def_string = configuration.getValue(table_def_key);
+ if (table_def.matches("\\D*"))
+ {
+ // if the table key is not a number (row number index)
+ if (COLS_WITH.equals(table_def))
+ {
+ String[] cols_s = table_def_string.split(" ");
+ float[] cols_f = new float[cols_s.length];
+ for (int i = 0; i < cols_s.length; i++)
+ {
+ cols_f[i] = Float.parseFloat(cols_s[i]);
+ }
+ sig_table.setColsRelativeWith(cols_f);
+ }
+ if (STYLE.equals(table_def) && !found_style)
+ {
+ Style style = readStyle(table_def_key, configuration);
+ sig_table.setStyle(style);
+ found_style = true;
+ }
+ continue;
+ }
+ if (table_def_string != null)
+ {
+ // analyse the row definition
+ String[] elems = table_def_string.split("\\|");
+ ArrayList row = new ArrayList();
+ for (int elem_idx = 0; elem_idx < elems.length; elem_idx++)
+ {
+ String elem = elems[elem_idx];
+ String[] key_type = elem.split("-");
+ if (key_type.length < 2)
+ {
+ return null;
+ }
+ String key = key_type[0];
+ String type = key_type[1];
+ if (TYPE_TABLE.equals(key))
+ {
+ // add a table entry
+ Table table = createSigTable(profile, type, configuration);
+ if (table != null)
+ {
+ Entry entry = new Entry(Entry.TYPE_TABLE, table, key);
+ row.add(entry);
+ }
+ }
+ if (TYPE_IMAGE.equals(type))
+ {
+ // add an image entry
+ String value = profile.getValue(key);
+ if (value != null)
+ {
+ Entry entry = new Entry(Entry.TYPE_IMAGE, value, key);
+ entry.setStyle(defaultImageStyle_);
+ row.add(entry);
+ } else {
+ Entry entry = new Entry(Entry.TYPE_VALUE, "IMG MISSING", key);
+ entry.setStyle(defaultValueStyle_);
+ row.add(entry);
+ }
+ }
+ if (TYPE_VALUE.equals(type))
+ {
+ // add a single value entry
+ String value = profile.getValue(key);
+ Entry entry = new Entry(Entry.TYPE_VALUE, value, key);
+ if (entry != null)
+ {
+ entry.setColSpan(2);
+ entry.setStyle(defaultValueStyle_);
+ row.add(entry);
+ }
+ }
+ if ((TYPE_VALUE + TYPE_CAPTION).equals(type) || (TYPE_CAPTION + TYPE_VALUE).equals(type))
+ {
+ // add a caption value pair
+ String caption = profile.getCaption(key);
+ String value = profile.getValue(key);
+ //String caption = getSigCaption(key);
+ //String value = getSigValue(key);
+ if (value != null)
+ {
+ Entry c_entry = new Entry(Entry.TYPE_CAPTION, caption, key);
+ c_entry.setNoWrap(true); // dferbas fix bug #331
+ c_entry.setStyle(defaultCaptionStyle_);
+
+ Entry v_entry = new Entry(Entry.TYPE_VALUE, value, key);
+ v_entry.setStyle(defaultValueStyle_);
+ if (c_entry != null && v_entry != null)
+ {
+ row.add(c_entry);
+ row.add(v_entry);
+ }
+ } else {
+ // RESOLV VALUE!!
+ Entry c_entry = new Entry(Entry.TYPE_CAPTION, caption, key);
+ c_entry.setNoWrap(true); // dferbas fix bug #331
+ c_entry.setStyle(defaultCaptionStyle_);
+
+ ValueResolver resolver = new ValueResolver();
+
+ Entry v_entry = new Entry(Entry.TYPE_VALUE,
+ resolver.resolve(key, value, profile), key);
+ v_entry.setStyle(defaultValueStyle_);
+ if (c_entry != null && v_entry != null)
+ {
+ row.add(c_entry);
+ row.add(v_entry);
+ }
+ }
+ }
+ }
+ sig_table.addRow(table_def, row);
+ }
+ }
+
+ return sig_table;
+ }
+
+ public static SignatureProfileSettings createProfile(String profileID, ISettings configuration) {
+ return new SignatureProfileSettings(profileID, configuration);
+ }
+
+ /**
+ * This method returns a value for a given signature key. If the key equals to
+ * <code>SIG_NORM</code> and the value is <code>null</code> the version
+ * string of the current normalizer is returned!
+ *
+ * @param key
+ * the key to get the value for
+ * @return a value for the given key
+ */
+ public static String getSigValue(String key)
+ {
+
+ String value = null;
+ SignatureEntry sigEntry = null;
+ if (sigEntries_.containsKey(key))
+ {
+ sigEntry = (SignatureEntry) sigEntries_.get(key);
+ value = sigEntry.getValue();
+ }
+ /*
+ if (value == null && SignatureTypes.SIG_NORM.equals(key))
+ {
+ value = normalizer_.getVersion();
+ }
+ */ /*
+ String overrideVal = OverridePropertyHolder.getProperty(key);
+ if (value != null && sigEntry != null && !sigEntry.isPlaceholder && overrideVal != null) { // TODO this!! SignatureEntry.isPlaceholder
+ value = overrideVal;
+ if (logger.isDebugEnabled()) {
+ logger.debug("Using override property for key '" + key + "' = " + value);
+ }
+ } */
+
+ return value;
+ }
+
+ /**
+ * This method returns a caption for a given signature key. If the key exists
+ * and the coresponding value is <code>null</code> the key itself is
+ * returned as caption! If the key does not exist the method returns
+ * <code>null</code>.
+ *
+ * @param key
+ * the key to get the caption for
+ * @return a caption for the given key
+ */
+ private static String getSigCaption(String key)
+ {
+
+ String caption = null;
+ if (sigEntries_.containsKey(key))
+ {
+ caption = ((SignatureEntry) sigEntries_.get(key)).getCaption();
+ if (caption == null)
+ {
+ caption = key;
+ }
+ }
+ return caption;
+ }
+
+ /**
+ * This method read the style definitions from the settings file.
+ *
+ * @param styleKey
+ * the key to read the style definitions
+ * @return the defined style informations
+ * @see at.knowcenter.wag.egov.egiz.table.Style
+ */
+ private static Style readStyle(String styleKey, ISettings configuration)
+ {
+ Map<String, String> styles = configuration.getValuesPrefix(styleKey);
+ Style style = new Style();
+ Iterator<String> keyStyleIt = styles.keySet().iterator();
+ while(keyStyleIt.hasNext())
+ {
+ String style_id_key = keyStyleIt.next();
+ String style_val = styles.get(style_id_key);
+ String style_id = extractLastID(style_id_key);
+
+ style.setStyle(style_id, style_val);
+ }
+ return style;
+ }
+
+}
diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/ValueResolver.java b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/ValueResolver.java
new file mode 100644
index 00000000..9052a7eb
--- /dev/null
+++ b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/ValueResolver.java
@@ -0,0 +1,64 @@
+package at.gv.egiz.pdfas.lib.impl.stamping;
+
+import at.gv.egiz.pdfas.common.settings.IProfileConstants;
+import at.gv.egiz.pdfas.common.settings.SignatureProfileSettings;
+import at.gv.egiz.pdfas.lib.impl.PdfAsImpl;
+
+import java.text.SimpleDateFormat;
+import java.util.Calendar;
+import java.util.Date;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Created with IntelliJ IDEA. User: afitzek Date: 9/11/13 Time: 11:11 AM To
+ * change this template use File | Settings | File Templates.
+ */
+public class ValueResolver implements IProfileConstants {
+
+ private static final Logger logger = LoggerFactory
+ .getLogger(ValueResolver.class);
+
+ private static final String defaultDateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
+
+ public static final String EXP_START = "${";
+ public static final String EXP_END = "}";
+
+ // TODO: Currently just for proof of concept ...
+ // Should support Reading Fields from Certificate and DATETIME
+
+ // TODO: Use status in real implementation to get currently needed
+ // informations...
+ public String resolve(String key, String value,
+ SignatureProfileSettings settings) {
+
+ logger.debug("Resolving value for key: " + key);
+ logger.debug("Resolving value with value: " + value);
+
+ if (value != null) {
+ if (value.startsWith(EXP_START) && value.endsWith(EXP_END)) {
+ // TODO: handle OGNL expression for key and value
+ // TODO: Here we need the certificate
+ CertificateResolver certificateResolver = new CertificateResolver(
+ null);
+ String exp = value.substring(EXP_START.length(), value.length()
+ - EXP_END.length());
+ return certificateResolver.resolve(key, exp, settings);
+ }
+ }
+
+ if (key.equals(SIG_DATE)) {
+ if(value == null) {
+ value = defaultDateFormat;
+ }
+ // Value holds the date format!
+ SimpleDateFormat formater = new SimpleDateFormat(value);
+ Calendar cal = Calendar.getInstance();
+ return formater.format(cal.getTime());
+ }
+
+ return value;
+ }
+
+}