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 --- .../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 ++ 6 files changed, 219 insertions(+) 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/pdfas/common/utils') 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