From b5aefab37c25d5da114bb78768defc79fc709e29 Mon Sep 17 00:00:00 2001 From: tknall Date: Thu, 12 Jun 2008 08:19:49 +0000 Subject: A new check for the existence of a configuration has been implemented. The extraction is skipped if any files or folders would be overwritten. Files like log- or temp-files may exist and do not prevent the deployment of the default configuration. git-svn-id: https://joinup.ec.europa.eu/svn/pdf-as/trunk@283 7b5415b0-85f9-ee4d-85bd-d5d0c3b42d1c --- src/main/java/at/gv/egiz/pdfas/test/APITest.java | 65 ++++++++++++++++++++++ .../java/at/gv/egiz/pdfas/test/JarExtractTest.java | 28 ++++++++++ .../java/at/gv/egiz/pdfas/utils/ConfigUtils.java | 47 +++++++++++++++- 3 files changed, 137 insertions(+), 3 deletions(-) create mode 100644 src/main/java/at/gv/egiz/pdfas/test/APITest.java create mode 100644 src/main/java/at/gv/egiz/pdfas/test/JarExtractTest.java (limited to 'src/main/java/at/gv/egiz') diff --git a/src/main/java/at/gv/egiz/pdfas/test/APITest.java b/src/main/java/at/gv/egiz/pdfas/test/APITest.java new file mode 100644 index 0000000..9e52a41 --- /dev/null +++ b/src/main/java/at/gv/egiz/pdfas/test/APITest.java @@ -0,0 +1,65 @@ +package at.gv.egiz.pdfas.test; + +import java.io.File; +import java.io.IOException; + +import at.gv.egiz.pdfas.PdfAsFactory; +import at.gv.egiz.pdfas.api.PdfAs; +import at.gv.egiz.pdfas.api.commons.Constants; +import at.gv.egiz.pdfas.api.exceptions.PdfAsException; +import at.gv.egiz.pdfas.api.io.DataSource; +import at.gv.egiz.pdfas.api.sign.SignParameters; +import at.gv.egiz.pdfas.api.sign.pos.SignaturePositioning; +import at.gv.egiz.pdfas.io.FileBasedDataSink; +import at.gv.egiz.pdfas.io.FileBasedDataSource; + +public final class APITest { + + private APITest() { + } + + public static void main(String[] args) { + File configdir = new File("D:/downloads/testpdfas"); + File unsignedFile = new File("./test-files/blindtext.pdf"); + File signedFile = new File("d:/temp/blindtext_signed.pdf"); + String signatureMode = Constants.SIGNATURE_TYPE_BINARY; + String signatureDevice = Constants.SIGNATURE_DEVICE_MOA; + String signatureProfile = "SIGNATURBLOCK_MINIMAL_DE"; + SignaturePositioning signaturePos = null; + + PdfAs pdfasAPI = null; + + try { + + // instantiate api + pdfasAPI = PdfAsFactory.createPdfAs(configdir); + + // set source + DataSource dataSource = new FileBasedDataSource(unsignedFile, "application/pdf"); + + // set output + FileBasedDataSink dataSink = new FileBasedDataSink(signedFile); + + // configure signature + SignParameters signParameters = new SignParameters(); + signParameters.setDocument(dataSource); + signParameters.setOutput(dataSink); + signParameters.setSignatureType(signatureMode); + signParameters.setSignatureDevice(signatureDevice); + signParameters.setSignatureProfileId(signatureProfile); + signParameters.setSignaturePositioning(signaturePos); + + // sign + pdfasAPI.sign(signParameters); + + System.out.println("Successfully signed."); + + } catch (PdfAsException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + + } + +} diff --git a/src/main/java/at/gv/egiz/pdfas/test/JarExtractTest.java b/src/main/java/at/gv/egiz/pdfas/test/JarExtractTest.java new file mode 100644 index 0000000..e567452 --- /dev/null +++ b/src/main/java/at/gv/egiz/pdfas/test/JarExtractTest.java @@ -0,0 +1,28 @@ +package at.gv.egiz.pdfas.test; + +import at.gv.egiz.pdfas.api.commons.Constants; +import at.gv.egiz.pdfas.utils.ConfigUtils; + +public final class JarExtractTest { + + private JarExtractTest() { + } + + public static void main(String[] args) { + try { + + String sysProp = System.getProperty(Constants.CONFIG_DIR_SYSTEM_PROPERTY); + System.out.println("System property \"" + Constants.CONFIG_DIR_SYSTEM_PROPERTY + "\" = " + (sysProp != null ? ("\"" + sysProp + "\"") : null)); + + String deployedTo = ConfigUtils.deployDefaultConfiguration(); + if (deployedTo != null) { + System.out.println("Configuration successfully deployed to \"" + deployedTo + "\"."); + } else { + System.out.println("Configuration was NOT deployed. Maybe a configuration already exists."); + } + + } catch (Exception e) { + e.printStackTrace(); + } + } +} diff --git a/src/main/java/at/gv/egiz/pdfas/utils/ConfigUtils.java b/src/main/java/at/gv/egiz/pdfas/utils/ConfigUtils.java index 627e611..a7bca3f 100644 --- a/src/main/java/at/gv/egiz/pdfas/utils/ConfigUtils.java +++ b/src/main/java/at/gv/egiz/pdfas/utils/ConfigUtils.java @@ -71,9 +71,11 @@ public final class ConfigUtils { */ private static boolean deployFromZIP(String destination, boolean overwriteExisting) throws ConfigUtilsException { try { - File destinationFolder = new File(destination); - if (destinationFolder.exists() && !overwriteExisting) { - return false; + if (!overwriteExisting) { + if (configurationAlreadyExists(destination)) { + logger_.debug("There is at least one file or folder that would be overwritten at destination path \"" + destination + "\". Skipping extraction."); + return false; + } } InputStream in = ConfigUtils.class.getClassLoader().getResourceAsStream(Constants.DEFAULT_CONFIGURATION_ZIP_RESOURCE); if (in == null) { @@ -81,6 +83,7 @@ public final class ConfigUtils { } ZipInputStream zis = new ZipInputStream(in); ZipEntry ze; + File destinationFolder = new File(destination); destinationFolder.mkdirs(); logger_.debug("Extracting default configuration to folder \"" + destinationFolder.getCanonicalPath() + "\"."); while ((ze = zis.getNextEntry()) != null) { @@ -102,6 +105,44 @@ public final class ConfigUtils { } } + private static boolean configurationAlreadyExists(String destination) throws ConfigUtilsException, IOException { + logger_.debug("Checking configuration \"" + destination + "\" already exists (resp. if there are any directories or files that would be overwritten)."); + File destinationFolder = new File(destination); + if (destinationFolder == null || !destinationFolder.exists()) { + return false; + } + InputStream in = ConfigUtils.class.getClassLoader().getResourceAsStream(Constants.DEFAULT_CONFIGURATION_ZIP_RESOURCE); + if (in == null) { + throw new ConfigUtilsException("Unable to find default configuration resource \"" + Constants.DEFAULT_CONFIGURATION_ZIP_RESOURCE + "\"."); + } + ZipInputStream zis = new ZipInputStream(in); + ZipEntry ze; + while ((ze = zis.getNextEntry()) != null) { + if (ze.isDirectory()) { + File newFolder = new File(destinationFolder, ze.getName()); + logger_.debug("Checking if folder \"" + newFolder.getPath() + "\" already exists."); + if (newFolder.exists()) { + logger_.debug("YES !"); + return true; + } else { + logger_.debug("no"); + } + } else { + File destFile = new File(destinationFolder, ze.getName()); + logger_.trace("Checking if file \"" + destFile.getName() + "\" already exists."); + if (destFile.exists()) { + logger_.trace("YES !"); + return true; + } else { + logger_.trace("no"); + } + } + zis.closeEntry(); + } + zis.close(); + return false; + } + /** * Deploys the default configuration to the given destination folder. * -- cgit v1.2.3