From add4460d9619f3586a02ae0d8c028f01903494bc Mon Sep 17 00:00:00 2001 From: Andreas Fitzek Date: Tue, 1 Oct 2013 15:27:32 +0200 Subject: + Extracting information from Certificate (Ognl) + CMS KeyStore Signer (still wrong format) --- .../pdfas/common/settings/IProfileConstants.java | 2 + .../at/gv/egiz/pdfas/common/utils/OgnlUtils.java | 18 ++- .../gv/egiz/pdfas/common/utils/TempFileHelper.java | 121 +++++++++++++++++++++ 3 files changed, 140 insertions(+), 1 deletion(-) create mode 100644 pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/utils/TempFileHelper.java (limited to 'pdf-as-common/src/main/java/at/gv/egiz/pdfas') 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 index 2e2ee024..897ced8e 100644 --- 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 @@ -62,4 +62,6 @@ public interface IProfileConstants { public final static String CFG_DIR = "cfg"; public final static String CFG_FILE = "config.properties"; + public final static String TMP_DIR = "default.pdfastmp_dir"; + public final static String TMP_DIR_DEFAULT_VALUE = "pdfastmp"; } 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 index e98cb124..4afb8932 100644 --- 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 @@ -1,6 +1,9 @@ package at.gv.egiz.pdfas.common.utils; +import ognl.Ognl; import ognl.OgnlContext; +import ognl.OgnlException; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -16,7 +19,20 @@ public class OgnlUtils { private static final Logger logger = LoggerFactory.getLogger(OgnlUtils.class); public static String resolvsOgnlExpression(String expression, OgnlContext ctx) { - // TODO! + try { + Object value = Ognl.getValue(expression, ctx); + String valueString = value.toString(); + if(valueString.startsWith("[")) { + valueString = valueString.substring(1); + } + if(valueString.endsWith("]")) { + valueString = valueString.substring(0, valueString.length() - 1); + } + return valueString; + } catch (OgnlException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } return expression; } } diff --git a/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/utils/TempFileHelper.java b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/utils/TempFileHelper.java new file mode 100644 index 00000000..611d7a0b --- /dev/null +++ b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/utils/TempFileHelper.java @@ -0,0 +1,121 @@ +package at.gv.egiz.pdfas.common.utils; + +import java.io.File; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import at.gv.egiz.pdfas.common.settings.IProfileConstants; +import at.gv.egiz.pdfas.common.settings.ISettings; + + +public class TempFileHelper implements IProfileConstants { + + private static final Logger logger = LoggerFactory.getLogger(TempFileHelper.class); + + private final String tmpFilePrefix = "/tmp/"; + private static final String tmpFileSuffix = ".tmppdf"; + + private String tmpDir = "tmp"; + + private MessageDigest messageDigest = null; + + private List tmpFiles = new ArrayList(); + + public TempFileHelper(ISettings settings) { + initializeMD(); + + String myTmpDir = settings.getValue(TMP_DIR); + if(myTmpDir != null) { + File myTmpDirFile = new File(myTmpDir); + if(!myTmpDirFile.isAbsolute()) { + // relatives tmp dir + myTmpDirFile = new File(settings.getWorkingDirectory() + File.separator + myTmpDir); + } + tmpDir = myTmpDirFile.getAbsolutePath(); + } else { + tmpDir = settings.getWorkingDirectory() + File.separator + TMP_DIR_DEFAULT_VALUE; + } + + logger.info("TempDirHelper for TempDirectory: " + tmpDir); + + createTmpDir(); + } + + @Override + protected void finalize() throws Throwable { + this.deleteTmpDir(); + super.finalize(); + } + + private void deleteTmpDir() { + try { + File tmpdir = new File(tmpDir); + tmpdir.delete(); + } catch (Throwable e) { + logger.error("Failed to delete temporary directory: " + tmpDir, e); + } + } + + private void createTmpDir() { + try { + File tmpdir = new File(tmpDir); + tmpdir.mkdirs(); + } catch (Throwable e) { + logger.error("Failed to create temporary directory: " + tmpDir, e); + } + } + + private void initializeMD() { + try { + messageDigest = MessageDigest.getInstance("SHA1"); + return; + } catch (NoSuchAlgorithmException e) { + logger.warn("SHA1 not available", e); + } + try { + messageDigest = MessageDigest.getInstance("MD5"); + return; + } catch (NoSuchAlgorithmException e) { + logger.warn("MD5 not available", e); + } + throw new RuntimeException("Need at least SHA1 or MD5 Message Digest, none available!"); + } + + + public void setTemporaryDirectory(String directory) { + tmpDir = directory; + createTmpDir(); + } + + public String getHashedHexString(String str) { + byte[] digest = messageDigest.digest(str.getBytes()); + return StringUtils.bytesToHexString(digest); + } + + public String getStaticFilename() { + String uuidString = UUID.randomUUID().toString(); + logger.debug("Generated UUID " + uuidString); + String tmpFilename = tmpFilePrefix + getHashedHexString(uuidString) + tmpFileSuffix; + logger.info("Temporary filename " + tmpFilename); + tmpFiles.add(tmpFilename); + return tmpFilename; + } + + public void deleteFile(String filename) { + try { + File tmpFile = new File(filename); + if(tmpFile.exists()) { + tmpFile.delete(); + tmpFiles.remove(filename); + } + } catch (Throwable e) { + logger.error("Failed to delete temporary file: " + filename, e); + } + } +} -- cgit v1.2.3