From fc44d4bcad00192f0df8f6086737b9b126094dcd Mon Sep 17 00:00:00 2001 From: Andreas Fitzek Date: Thu, 26 Sep 2013 15:48:43 +0200 Subject: initial code commit --- .../pdfas/common/exceptions/PDFIOException.java | 20 ++++ .../pdfas/common/exceptions/PdfAsException.java | 31 ++++++ .../common/exceptions/PdfAsSettingsException.java | 20 ++++ .../egiz/pdfas/common/exceptions/package-info.java | 8 ++ .../pdfas/common/messages/MessageResolver.java | 42 +++++++ .../egiz/pdfas/common/messages/package-info.java | 8 ++ .../java/at/gv/egiz/pdfas/common/package-info.java | 8 ++ .../pdfas/common/settings/IProfileConstants.java | 65 +++++++++++ .../gv/egiz/pdfas/common/settings/ISettings.java | 13 +++ .../at/gv/egiz/pdfas/common/settings/Settings.java | 124 +++++++++++++++++++++ .../common/settings/SignatureProfileEntry.java | 39 +++++++ .../common/settings/SignatureProfileSettings.java | 121 ++++++++++++++++++++ .../egiz/pdfas/common/settings/package-info.java | 8 ++ .../at/gv/egiz/pdfas/common/utils/DNUtils.java | 34 ++++++ .../at/gv/egiz/pdfas/common/utils/OgnlUtils.java | 22 ++++ .../at/gv/egiz/pdfas/common/utils/PDFUtils.java | 94 ++++++++++++++++ .../at/gv/egiz/pdfas/common/utils/StreamUtils.java | 28 +++++ .../at/gv/egiz/pdfas/common/utils/StringUtils.java | 33 ++++++ .../gv/egiz/pdfas/common/utils/package-info.java | 8 ++ 19 files changed, 726 insertions(+) create mode 100644 pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/exceptions/PDFIOException.java create mode 100644 pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/exceptions/PdfAsException.java create mode 100644 pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/exceptions/PdfAsSettingsException.java create mode 100644 pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/exceptions/package-info.java create mode 100644 pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/messages/MessageResolver.java create mode 100644 pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/messages/package-info.java create mode 100644 pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/package-info.java create mode 100644 pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/settings/IProfileConstants.java create mode 100644 pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/settings/ISettings.java create mode 100644 pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/settings/Settings.java create mode 100644 pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/settings/SignatureProfileEntry.java create mode 100644 pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/settings/SignatureProfileSettings.java create mode 100644 pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/settings/package-info.java create mode 100644 pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/utils/DNUtils.java create mode 100644 pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/utils/OgnlUtils.java create mode 100644 pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/utils/PDFUtils.java create mode 100644 pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/utils/StreamUtils.java create mode 100644 pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/utils/StringUtils.java create mode 100644 pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/utils/package-info.java (limited to 'pdf-as-common/src/main/java/at/gv/egiz') diff --git a/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/exceptions/PDFIOException.java b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/exceptions/PDFIOException.java new file mode 100644 index 00000000..57c1e7ab --- /dev/null +++ b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/exceptions/PDFIOException.java @@ -0,0 +1,20 @@ +package at.gv.egiz.pdfas.common.exceptions; + +/** + * Created with IntelliJ IDEA. + * User: afitzek + * Date: 8/28/13 + * Time: 12:04 PM + * To change this template use File | Settings | File Templates. + */ +public class PDFIOException extends PdfAsException { + + public PDFIOException(String msgId) { + super(msgId); + } + + + public PDFIOException(String msgId, Throwable e) { + super(msgId, e); + } +} diff --git a/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/exceptions/PdfAsException.java b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/exceptions/PdfAsException.java new file mode 100644 index 00000000..e36aa082 --- /dev/null +++ b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/exceptions/PdfAsException.java @@ -0,0 +1,31 @@ +package at.gv.egiz.pdfas.common.exceptions; + +import at.gv.egiz.pdfas.common.messages.MessageResolver; + +public class PdfAsException extends Exception { + public PdfAsException() { + super(); + } + + public PdfAsException(String msgId) { + super(msgId); + } + + public PdfAsException(String msgId, Throwable e) { + super(msgId, e); + } + + @Override + public String getMessage() { + return localizeMessage(super.getMessage()); + } + + @Override + public String getLocalizedMessage() { + return localizeMessage(super.getMessage()); + } + + protected String localizeMessage(String msgId) { + return MessageResolver.resolveMessage(msgId); + } +} diff --git a/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/exceptions/PdfAsSettingsException.java b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/exceptions/PdfAsSettingsException.java new file mode 100644 index 00000000..431ffa5d --- /dev/null +++ b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/exceptions/PdfAsSettingsException.java @@ -0,0 +1,20 @@ +package at.gv.egiz.pdfas.common.exceptions; + +/** + * Created with IntelliJ IDEA. + * User: afitzek + * Date: 9/10/13 + * Time: 10:54 AM + * To change this template use File | Settings | File Templates. + */ +public class PdfAsSettingsException extends PdfAsException { + + public PdfAsSettingsException(String msgId) { + super(msgId); + } + + + public PdfAsSettingsException(String msgId, Throwable e) { + super(msgId, e); + } +} diff --git a/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/exceptions/package-info.java b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/exceptions/package-info.java new file mode 100644 index 00000000..cfd6a993 --- /dev/null +++ b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/exceptions/package-info.java @@ -0,0 +1,8 @@ +/** + * + */ +/** + * @author afitzek + * + */ +package at.gv.egiz.pdfas.common.exceptions; \ No newline at end of file diff --git a/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/messages/MessageResolver.java b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/messages/MessageResolver.java new file mode 100644 index 00000000..0bc1bab7 --- /dev/null +++ b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/messages/MessageResolver.java @@ -0,0 +1,42 @@ +package at.gv.egiz.pdfas.common.messages; + +import java.util.Locale; +import java.util.ResourceBundle; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class MessageResolver { + private static final String messageResource = "resources.messages.common"; + private static final String missingMsg = "Please add message "; + + private static final Logger logger = LoggerFactory.getLogger(MessageResolver.class); + + private static ResourceBundle bundle; + + static { + ResourceBundle bundle = ResourceBundle.getBundle(messageResource); + if(bundle == null) { + logger.error("Failed to load resource bundle!!"); + Runtime.getRuntime().exit(-1); + } + } + + public static void forceLocale(Locale locale) { + bundle = ResourceBundle.getBundle(messageResource, locale); + } + + public static String resolveMessage(String msgId) { + if(bundle == null) { + return missingMsg + msgId; + } + if(bundle.containsKey(msgId)) { + String value = bundle.getString(msgId); + if(value == null) { + return missingMsg + msgId; + } + return value; + } + return missingMsg + msgId; + } +} diff --git a/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/messages/package-info.java b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/messages/package-info.java new file mode 100644 index 00000000..0ac78c22 --- /dev/null +++ b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/messages/package-info.java @@ -0,0 +1,8 @@ +/** + * + */ +/** + * @author afitzek + * + */ +package at.gv.egiz.pdfas.common.messages; \ No newline at end of file diff --git a/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/package-info.java b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/package-info.java new file mode 100644 index 00000000..58c99dad --- /dev/null +++ b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/package-info.java @@ -0,0 +1,8 @@ +/** + * + */ +/** + * @author afitzek + * + */ +package at.gv.egiz.pdfas.common; \ No newline at end of file diff --git a/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/settings/IProfileConstants.java b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/settings/IProfileConstants.java new file mode 100644 index 00000000..2e2ee024 --- /dev/null +++ b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/settings/IProfileConstants.java @@ -0,0 +1,65 @@ +package at.gv.egiz.pdfas.common.settings; + +/** + * Created with IntelliJ IDEA. + * User: afitzek + * Date: 9/10/13 + * Time: 12:58 PM + * To change this template use File | Settings | File Templates. + */ +public interface IProfileConstants { + /** + * The settings key prefix for signature definitions. "sig_obj." + */ + public static final String SIG_OBJ = "sig_obj."; + + public static final String SIG_DATE = "SIG_DATE"; + + /** + * The settings key prefix for the signature table object definition + */ + public static final String TABLE = "table."; + + /** + * The settings value refering to a table + */ + public final static String TYPE_TABLE = "TABLE"; + + /** + * The settings value refering to an image + */ + public final static String TYPE_IMAGE = "i"; + + /** + * The settings value refering to a text caption + */ + public final static String TYPE_CAPTION = "c"; + + /** + * The settings value refering to a text value + */ + public final static String TYPE_VALUE = "v"; + + /** + * The settings key sub prefix getting the width of columns for a table + * definition + */ + public final static String COLS_WITH = "ColsWidth"; + + /** + * The settings key sub prefix getting the style definition + */ + public final static String STYLE = "Style"; + + public final static String PROFILE_VALUE = "value"; + + public final static String PROFILE_KEY = "key"; + + public final static String KEY_SEPARATOR = "."; + + public final static String INCLUDE = "include"; + + public final static String CFG_DIR = "cfg"; + public final static String CFG_FILE = "config.properties"; + +} diff --git a/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/settings/ISettings.java b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/settings/ISettings.java new file mode 100644 index 00000000..9e3291d2 --- /dev/null +++ b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/settings/ISettings.java @@ -0,0 +1,13 @@ +package at.gv.egiz.pdfas.common.settings; + +import java.util.Map; +import java.util.Vector; + +public interface ISettings { + public String getValue(String key); + public boolean hasValue(String key); + public boolean hasPrefix(String prefix); + public Map getValuesPrefix(String prefix); + public Vector getFirstLevelKeys(String prefix); + public String getWorkingDirectory(); +} diff --git a/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/settings/Settings.java b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/settings/Settings.java new file mode 100644 index 00000000..da946215 --- /dev/null +++ b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/settings/Settings.java @@ -0,0 +1,124 @@ +package at.gv.egiz.pdfas.common.settings; + +import at.gv.egiz.pdfas.common.exceptions.PdfAsSettingsException; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.*; + +public class Settings implements ISettings, IProfileConstants{ + + private static final Logger logger = LoggerFactory.getLogger(Settings.class); + + protected Properties properties = new Properties(); + + protected File workDirectory; + + public Settings(File workDirectory) { + try { + this.workDirectory = workDirectory; + loadSettings(workDirectory); + } catch (PdfAsSettingsException e) { + logger.error(e.getMessage(), e); + } + } + + public void loadSettings(File workDirectory) throws PdfAsSettingsException { + try { + + String configDir = workDirectory.getAbsolutePath() + File.separator + CFG_DIR; + String configFile = configDir + File.separator + CFG_FILE; + logger.debug("Loading cfg file: " + configFile); + properties.load(new FileInputStream(configFile)); + + Map includes = this.getValuesPrefix(INCLUDE); + + if(includes != null) { + Iterator includeIterator = includes.values().iterator(); + while(includeIterator.hasNext()) { + String includeFile = configDir + File.separator + includeIterator.next(); + logger.debug("Loading included cfg file: " + includeFile); + properties.load(new FileInputStream(includeFile)); + } + } + + logger.debug("Configured Properties:"); + if(logger.isDebugEnabled()) { + properties.list(System.out); + } + + } catch (IOException e) { + throw new PdfAsSettingsException("Failed to read settings!", e); + } + } + + public String getValue(String key) { + return properties.getProperty(key); + } + + public boolean hasValue(String key) { + return properties.containsKey(key); + } + + public Map getValuesPrefix(String prefix) { + Iterator keyIterator = properties.keySet().iterator(); + Map valueMap = new HashMap(); + while(keyIterator.hasNext()) { + String key = keyIterator.next().toString(); + + if(key.startsWith(prefix)) { + valueMap.put(key, properties.getProperty(key)); + } + } + + if(valueMap.isEmpty()) { + return null; + } + + return valueMap; + } + + public Vector getFirstLevelKeys(String prefix) { + String mPrefix = prefix.endsWith(".")?prefix:prefix+"."; + Iterator keyIterator = properties.keySet().iterator(); + Vector valueMap = new Vector(); + while(keyIterator.hasNext()) { + String key = keyIterator.next().toString(); + + if(key.startsWith(prefix)) { + int keyIdx = key.indexOf('.', mPrefix.length()) > 0 ? key.indexOf('.', mPrefix.length()) : key.length(); + String firstLevels = key.substring(0, keyIdx); + if(!valueMap.contains(firstLevels)) { + valueMap.add(firstLevels); + } + } + } + + if(valueMap.isEmpty()) { + return null; + } + + return valueMap; + } + + public boolean hasPrefix(String prefix) { + Iterator keyIterator = properties.keySet().iterator(); + while(keyIterator.hasNext()) { + String key = keyIterator.next().toString(); + + if(key.startsWith(prefix)) { + return true; + } + } + return false; + } + + public String getWorkingDirectory() { + return this.workDirectory.getAbsolutePath(); + } + +} diff --git a/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/settings/SignatureProfileEntry.java b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/settings/SignatureProfileEntry.java new file mode 100644 index 00000000..3df929b1 --- /dev/null +++ b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/settings/SignatureProfileEntry.java @@ -0,0 +1,39 @@ +package at.gv.egiz.pdfas.common.settings; + +public class SignatureProfileEntry { + private String key = null; + private String caption = null; + private String value = null; + + public SignatureProfileEntry() { + } + + public String getKey() { + return key; + } + + public void setKey(String key) { + this.key = key; + } + + public String getCaption() { + return caption; + } + + public void setCaption(String caption) { + this.caption = caption; + } + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } + + @Override + public String toString() { + return getKey() + "[ " + getCaption() + " : " + getValue() + " ]"; + } +} diff --git a/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/settings/SignatureProfileSettings.java b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/settings/SignatureProfileSettings.java new file mode 100644 index 00000000..46f2ed09 --- /dev/null +++ b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/settings/SignatureProfileSettings.java @@ -0,0 +1,121 @@ +package at.gv.egiz.pdfas.common.settings; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.*; + +public class SignatureProfileSettings implements IProfileConstants { + + private static final Logger logger = LoggerFactory.getLogger(SignatureProfileSettings.class); + + private Map profileInformations = new HashMap(); + + private Map profileSettings = new HashMap(); + + private String profileID; + + public SignatureProfileSettings(String profileID, ISettings configuration) { + this.profileID = profileID; + String profilePrefix = SIG_OBJ + profileID + KEY_SEPARATOR; + String keysPrefix = profilePrefix + PROFILE_KEY; + String valuesPrefix = profilePrefix + PROFILE_VALUE; + String tablePrefix = profilePrefix + TABLE; + + logger.debug("Reading Profile: " + profileID); + logger.debug("Keys Prefix: " + keysPrefix); + logger.debug("Values Prefix: " + valuesPrefix); + logger.debug("Table Prefix: " + tablePrefix); + + Map keys = configuration.getValuesPrefix(keysPrefix); + Map values = configuration.getValuesPrefix(valuesPrefix); + + Iterator keyIterator = keys.keySet().iterator(); + + while(keyIterator.hasNext()) { + String key = keyIterator.next(); + key = key.substring(key.lastIndexOf('.') + 1); + String valueKey = keys.get(keysPrefix + KEY_SEPARATOR + key); + + String valueValue = values.get(valuesPrefix + KEY_SEPARATOR + key); + + + SignatureProfileEntry entry = new SignatureProfileEntry(); + entry.setKey(key); + entry.setCaption(valueKey); + entry.setValue(valueValue); + profileInformations.put(key, entry); + logger.debug(" " + entry.toString()); + } + + // Find entries where only values exists + Iterator valuesIterator = values.keySet().iterator(); + + while(valuesIterator.hasNext()) { + String key = valuesIterator.next(); + key = key.substring(key.lastIndexOf('.') + 1); + + String valueValue = values.get(valuesPrefix + KEY_SEPARATOR + key); + + SignatureProfileEntry entry = profileInformations.get(key); + if(entry == null) { + entry = new SignatureProfileEntry(); + entry.setKey(key); + entry.setCaption(null); + entry.setValue(valueValue); + profileInformations.put(key, entry); + } + + logger.debug(" " + entry.toString()); + } + + Map others = configuration.getValuesPrefix(profilePrefix); + + Iterator otherIterator = others.keySet().iterator(); + + while(otherIterator.hasNext()) { + String key = otherIterator.next(); + + logger.trace("Checking key " + key); + if( key.startsWith(keysPrefix) || + key.startsWith(valuesPrefix) || + key.startsWith(tablePrefix)) { + continue; + } + + String value = others.get(key); + key = key.substring(key.lastIndexOf('.') + 1); + + profileSettings.put(key, others.get(value)); + + logger.debug(" Settings: " + key + " : " + value); + } + } + + public String getCaption(String key) { + SignatureProfileEntry entry = profileInformations.get(key); + if(entry != null) { + return entry.getCaption(); + } + return null; + } + + public String getValue(String key) { + SignatureProfileEntry entry = profileInformations.get(key); + if(entry != null) { + String value = entry.getValue(); + + if(value == null) { + // TODO: try to find default value for key! + } + + return value; + } + // TODO: try to find default value for key! + return null; + } + + public String getProfileID() { + return profileID; + } +} diff --git a/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/settings/package-info.java b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/settings/package-info.java new file mode 100644 index 00000000..c2b123e2 --- /dev/null +++ b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/settings/package-info.java @@ -0,0 +1,8 @@ +/** + * + */ +/** + * @author afitzek + * + */ +package at.gv.egiz.pdfas.common.settings; \ No newline at end of file diff --git a/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/utils/DNUtils.java b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/utils/DNUtils.java new file mode 100644 index 00000000..429151ee --- /dev/null +++ b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/utils/DNUtils.java @@ -0,0 +1,34 @@ +package at.gv.egiz.pdfas.common.utils; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; + +import javax.naming.InvalidNameException; +import javax.naming.ldap.LdapName; +import javax.naming.ldap.Rdn; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class DNUtils { + private static final Logger logger = LoggerFactory.getLogger(DNUtils.class); + + + public static Map dnToMap(String dn) throws InvalidNameException { + Map map = new HashMap(); + + LdapName ldapName = new LdapName(dn); + + Iterator rdnIterator = ldapName.getRdns().iterator(); + + while(rdnIterator.hasNext()) { + Rdn rdn = rdnIterator.next(); + + logger.debug(rdn.getType() + " = " + rdn.getValue().toString()); + map.put(rdn.getType(), rdn.getValue().toString()); + } + + return map; + } +} diff --git a/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/utils/OgnlUtils.java b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/utils/OgnlUtils.java new file mode 100644 index 00000000..e98cb124 --- /dev/null +++ b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/utils/OgnlUtils.java @@ -0,0 +1,22 @@ +package at.gv.egiz.pdfas.common.utils; + +import ognl.OgnlContext; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Created with IntelliJ IDEA. + * User: afitzek + * Date: 9/11/13 + * Time: 1:05 PM + * To change this template use File | Settings | File Templates. + */ +public class OgnlUtils { + + private static final Logger logger = LoggerFactory.getLogger(OgnlUtils.class); + + public static String resolvsOgnlExpression(String expression, OgnlContext ctx) { + // TODO! + return expression; + } +} diff --git a/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/utils/PDFUtils.java b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/utils/PDFUtils.java new file mode 100644 index 00000000..155f9cfb --- /dev/null +++ b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/utils/PDFUtils.java @@ -0,0 +1,94 @@ +package at.gv.egiz.pdfas.common.utils; + +import org.apache.pdfbox.pdmodel.PDDocument; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.ArrayList; +import java.util.List; + + +public class PDFUtils { + + private static final Logger logger = LoggerFactory.getLogger(PDFUtils.class); + + + private static final byte[] signature_pattern = new byte[] { + (byte) 0x0A, (byte) 0x2F, (byte) 0x43, (byte) 0x6F, // ./Co + (byte) 0x6E, (byte) 0x74, (byte) 0x65, (byte) 0x6E, // nten + (byte) 0x74, (byte) 0x73, (byte) 0x20, (byte) 0x0A, // ts . + (byte) 0x2F, (byte) 0x42, (byte) 0x79, (byte) 0x74, // /Byt + (byte) 0x65, (byte) 0x52, (byte) 0x61, (byte) 0x6E, // eRan + (byte) 0x67, (byte) 0x65, (byte) 0x20, (byte) 0x5B, // ge [ + + }; + + private static final byte range_seperation = (byte) 0x20; + private static final byte range_end = (byte) 0x5D; + + private static int extractASCIIInteger(byte[] data, int offset) { + int nextsepp = nextSeperator(data, offset); + + if(nextsepp < offset) { + return -1; + } + + String asciiString = new String(data, offset, nextsepp - offset); + + logger.debug("Extracted " + asciiString); + + return Integer.parseInt(asciiString); + } + + private static int nextSeperator(byte[] data, int offset) { + for(int i = offset; i < data.length; i++) { + if(data[i] == range_seperation) { + return i; + } else if(data[i] == range_end) { + return i; + } + } + return -2; + } + + public static int[] extractSignatureByteRange(byte[] rawPdfData) { + int i = 0; + for(i = rawPdfData.length - 1; i >= 0; i--) { + if(rawPdfData[i] == signature_pattern[0] && + i+signature_pattern.length < rawPdfData.length) { + boolean match = true; + for(int j = 0; j < signature_pattern.length; j++) { + + if(rawPdfData[i+j] != signature_pattern[j]) { + match = false; + break; + } + } + + if(match) { + + int offset = i + signature_pattern.length; + List byteRange = new ArrayList(); + while(offset > 0) { + byteRange.add(extractASCIIInteger(rawPdfData, offset)); + offset = nextSeperator(rawPdfData, offset); + if(rawPdfData[offset] == range_end) { + break; + } + offset++; + } + int[] range = new int[byteRange.size()]; + for(int j = 0; j < byteRange.size(); j++) { + range[j] = byteRange.get(j); + } + return range; + } + } + } + return null; + } + + public static void checkPDFPermissions(PDDocument doc) { + // TODO: Check permission for document + } +} diff --git a/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/utils/StreamUtils.java b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/utils/StreamUtils.java new file mode 100644 index 00000000..0b15d700 --- /dev/null +++ b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/utils/StreamUtils.java @@ -0,0 +1,28 @@ +package at.gv.egiz.pdfas.common.utils; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; + +/** + * Created with IntelliJ IDEA. + * User: afitzek + * Date: 8/29/13 + * Time: 9:54 AM + * To change this template use File | Settings | File Templates. + */ +public class StreamUtils { + + public static byte[] inputStreamToByteArray(InputStream stream) throws IOException { + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + byte[] buffer = new byte[4096]; + int readBytes = 0; + + while((readBytes = stream.read(buffer)) != -1) { + bos.write(buffer, 0, readBytes); + } + stream.close(); + bos.close(); + return bos.toByteArray(); + } +} diff --git a/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/utils/StringUtils.java b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/utils/StringUtils.java new file mode 100644 index 00000000..63aae211 --- /dev/null +++ b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/utils/StringUtils.java @@ -0,0 +1,33 @@ +package at.gv.egiz.pdfas.common.utils; + +import java.util.Formatter; + +/** + * Created with IntelliJ IDEA. + * User: afitzek + * Date: 8/28/13 + * Time: 12:42 PM + * To change this template use File | Settings | File Templates. + */ +public class StringUtils { + + public static String bytesToHexString(byte[] bytes) { + StringBuilder sb = new StringBuilder(bytes.length * 2); + + Formatter formatter = new Formatter(sb); + for (byte b : bytes) { + formatter.format("%02x", b); + } + + return sb.toString(); + } + + public static String extractLastID(String id) { + int lastIDX = id.lastIndexOf('.'); + String result = id; + if(lastIDX > 0) { + result = id.substring(lastIDX+1); + } + return result; + } +} diff --git a/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/utils/package-info.java b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/utils/package-info.java new file mode 100644 index 00000000..91b05145 --- /dev/null +++ b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/utils/package-info.java @@ -0,0 +1,8 @@ +/** + * + */ +/** + * @author afitzek + * + */ +package at.gv.egiz.pdfas.common.utils; \ No newline at end of file -- cgit v1.2.3