aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/gv/egiz/pdfas/utils/ConfigUtils.java
diff options
context:
space:
mode:
authortknall <tknall@7b5415b0-85f9-ee4d-85bd-d5d0c3b42d1c>2008-06-09 15:27:50 +0000
committertknall <tknall@7b5415b0-85f9-ee4d-85bd-d5d0c3b42d1c>2008-06-09 15:27:50 +0000
commiteb68e29e371ef3e944f682239e6f3f92cb084697 (patch)
tree39ef4547a7efe4e929886b3d2ca01bb037cb2ce4 /src/main/java/at/gv/egiz/pdfas/utils/ConfigUtils.java
parentdef55a4c6f4a26b8ddbf2a9e892e36a67d9380f1 (diff)
downloadpdf-as-3-eb68e29e371ef3e944f682239e6f3f92cb084697.tar.gz
pdf-as-3-eb68e29e371ef3e944f682239e6f3f92cb084697.tar.bz2
pdf-as-3-eb68e29e371ef3e944f682239e6f3f92cb084697.zip
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
Diffstat (limited to 'src/main/java/at/gv/egiz/pdfas/utils/ConfigUtils.java')
-rw-r--r--src/main/java/at/gv/egiz/pdfas/utils/ConfigUtils.java176
1 files changed, 176 insertions, 0 deletions
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 <a href="mailto:thomas.knall@egiz.gv.at">Thomas Knall</a>
+ */
+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 <code>true</code> an already existing configuration is overwritten. If <code>false</code> nothing is being copied if the destination folder already exists.
+ * @return <code>true</code> if the configuration has been deployed, <code>false</code> 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 <code>true</code> an already existing configuration is overwritten. If <code>false</code> nothing is being copied if the destination folder already exists.
+ * @return <code>true</code> if the configuration has been deployed, <code>false</code> 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 <code>true</code> an already existing configuration is overwritten. If <code>false</code> nothing is being copied if the destination folder already exists.
+ * @return <code>true</code> if the configuration has been deployed, <code>false</code> 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
+ * <code>Constants.Constants.USERHOME_CONFIG_FOLDER</code>.
+ *
+ * @param overwriteExisting If set <code>true</code> an already existing configuration is overwritten. If <code>false</code> nothing is being copied if the destination folder already exists.
+ * @return <code>true</code> if the configuration has been deployed, <code>false</code> 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
+ * <code>Constants.Constants.USERHOME_CONFIG_FOLDER</code>.
+ *
+ * @return <code>true</code> if the configuration has been deployed, <code>false</code> 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();
+ }
+
+}