From eb68e29e371ef3e944f682239e6f3f92cb084697 Mon Sep 17 00:00:00 2001 From: tknall Date: Mon, 9 Jun 2008 15:27:50 +0000 Subject: Web-Application: Configuration may be declared via system property "pdf-as.work-dir" or via Servlet-Init-Parameter "work-dir". Bug fixed in RetrieveSignatureDataServlet: Response header didn't contain a content length attribute. The ITS Mac BKU rejects those requests. Workaround for ITS Mac BKU integrated. A redirect via response does only work if the response contains a valid SL request (e.g. a NullOperationRequest). API: The configuration folder may be omitted at instantiating the api. Configuration may be set via system property "pdf-as.work-dir". If no configuration is given at all, the current user's home directory is searched for a folder "PDF-AS". If not found a default configuration is created. If the configuration is explicitely given than the temporary folder is located within the given directory otherwise within the user's temporary directory. Declaring the configuration folder, replacements for system properties like "${catalina.base}/conf/pdfas" may be used. Web-Application: Session is now being invalidated after download of the signed pdf file. Web-Application: Every hardcoded context "pdf-as" has been replaced. git-svn-id: https://joinup.ec.europa.eu/svn/pdf-as/trunk@277 7b5415b0-85f9-ee4d-85bd-d5d0c3b42d1c --- .../java/at/gv/egiz/pdfas/utils/ConfigUtils.java | 176 +++++++++++++++++++++ .../java/at/gv/egiz/pdfas/utils/StreamUtils.java | 1 + 2 files changed, 177 insertions(+) create mode 100644 src/main/java/at/gv/egiz/pdfas/utils/ConfigUtils.java (limited to 'src/main/java/at/gv/egiz/pdfas/utils') diff --git a/src/main/java/at/gv/egiz/pdfas/utils/ConfigUtils.java b/src/main/java/at/gv/egiz/pdfas/utils/ConfigUtils.java new file mode 100644 index 0000000..c98cb59 --- /dev/null +++ b/src/main/java/at/gv/egiz/pdfas/utils/ConfigUtils.java @@ -0,0 +1,176 @@ +package at.gv.egiz.pdfas.utils; + +import java.io.BufferedOutputStream; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import at.gv.egiz.pdfas.api.commons.Constants; +import at.gv.egiz.pdfas.api.exceptions.ConfigUtilsException; + +/** + * @author Thomas Knall + */ +public final class ConfigUtils { + + private ConfigUtils() { + } + + /** + * The log. + */ + private static final Log logger_ = LogFactory.getLog(ConfigUtils.class); + + /** + * Deploys the default configuration with apache commons vfs. + * + * @param destination The destination folder. + * @param overwriteExisting If set true an already existing configuration is overwritten. If false nothing is being copied if the destination folder already exists. + * @return true if the configuration has been deployed, false if not. + * @throws ConfigUtilsException Thrown if there was an error during the deployment of the configuration. + */ + /* + private static boolean deployWithCommonsVFS(String destination, boolean overwriteExisting) throws ConfigUtilsException { + try { + FileSystemManager fsManager = VFS.getManager(); + FileObject defaultConfigurationFile = fsManager.resolveFile("res:DefaultConfiguration"); + FileObject destinationFile = fsManager.resolveFile(destination); + + if (destinationFile.exists() && !overwriteExisting) { + return false; + } + + destinationFile.copyFrom(defaultConfigurationFile, new AllFileSelector()); + return true; + } catch (FileSystemException e) { + throw new ConfigUtilsException(e); + } + } + */ + + + /** + * Deploys the default configuration from an included zip file. + * + * @param destination The destination folder. + * @param overwriteExisting If set true an already existing configuration is overwritten. If false nothing is being copied if the destination folder already exists. + * @return true if the configuration has been deployed, false if not. + * @throws ConfigUtilsException Thrown if there was an error during the deployment of the configuration. + */ + private static boolean deployFromZIP(String destination, boolean overwriteExisting) throws ConfigUtilsException { + try { + File destinationFolder = new File(destination); + if (destinationFolder.exists() && !overwriteExisting) { + 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; + destinationFolder.mkdirs(); + logger_.debug("Extracting default configuration to folder \"" + destinationFolder.getCanonicalPath() + "\"."); + while ((ze = zis.getNextEntry()) != null) { + if (ze.isDirectory()) { + File newFolder = new File(destinationFolder, ze.getName()); + logger_.debug("Extracting folder \"" + newFolder.getPath() + "\"."); + newFolder.mkdirs(); + } else { + File destFile = new File(destinationFolder, ze.getName()); + logger_.trace("Extracting file \"" + destFile.getName() + "\"."); + toFile(zis, destFile); + } + zis.closeEntry(); + } + zis.close(); + return true; + } catch (IOException e) { + throw new ConfigUtilsException(e); + } + } + + /** + * Deploys the default configuration to the given destination folder. + * + * @param destination The destination folder. + * @param overwriteExisting If set true an already existing configuration is overwritten. If false nothing is being copied if the destination folder already exists. + * @return true if the configuration has been deployed, false if not. + * @throws ConfigUtilsException Thrown if there was an error during the deployment of the configuration. + */ + public static boolean deployDefaultConfiguration(String destination, boolean overwriteExisting) throws ConfigUtilsException { + if (destination == null) { + throw new NullPointerException("Destination must not be null."); + } + if (destination.length() == 0) { + throw new IllegalArgumentException("Destination must not be empty."); + } + return deployFromZIP(destination, overwriteExisting); + } + + /** + * Deploys the default configuration to the user's home directory to the subdirectory specified by + * Constants.Constants.USERHOME_CONFIG_FOLDER. + * + * @param overwriteExisting If set true an already existing configuration is overwritten. If false nothing is being copied if the destination folder already exists. + * @return true if the configuration has been deployed, false if not. + * @throws ConfigUtilsException Thrown if there was an error during the deployment of the configuration. + * @see Constants#USERHOME_CONFIG_FOLDER + */ + public static boolean deployDefaultConfiguration(boolean overwriteExisting) throws ConfigUtilsException { + String userHome = System.getProperty("user.home"); + if (userHome == null || userHome.length() == 0) { + return false; + } + return deployDefaultConfiguration(userHome + File.separator + Constants.USERHOME_CONFIG_FOLDER, overwriteExisting); + } + + /** + * Deploys the default configuration to the user's home directory to the subdirectory specified by + * Constants.Constants.USERHOME_CONFIG_FOLDER. + * + * @return true if the configuration has been deployed, false if not. + * @throws ConfigUtilsException Thrown if there was an error during the deployment of the configuration. + * @see Constants#USERHOME_CONFIG_FOLDER + */ + public static boolean deployDefaultConfiguration() throws ConfigUtilsException { + return deployDefaultConfiguration(false); + } + + public static boolean toFile(InputStream inputStream, File file) throws IOException { + boolean result = false; + BufferedOutputStream bufferedOutputStream = null; + try { + bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file)); + writeInputStreamToOutputStream(inputStream, bufferedOutputStream); + } finally { + if (bufferedOutputStream != null) { + try { + bufferedOutputStream.close(); + result = true; + } catch (IOException e) { + result = false; + } + } + } + return result; + } + + public static void writeInputStreamToOutputStream(InputStream inputStream, OutputStream outputStream) throws IOException { + final int bufferSize = 1024; + byte[] buffer = new byte[bufferSize]; + int len = -1; + while ((len = inputStream.read(buffer)) != -1) { + outputStream.write(buffer, 0, len); + } + outputStream.flush(); + } + +} diff --git a/src/main/java/at/gv/egiz/pdfas/utils/StreamUtils.java b/src/main/java/at/gv/egiz/pdfas/utils/StreamUtils.java index acf7e75..44303b0 100644 --- a/src/main/java/at/gv/egiz/pdfas/utils/StreamUtils.java +++ b/src/main/java/at/gv/egiz/pdfas/utils/StreamUtils.java @@ -16,6 +16,7 @@ public class StreamUtils { os.write(buffer, 0, read); } + os.flush(); is.close(); os.close(); } -- cgit v1.2.3