aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/gv
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/at/gv')
-rw-r--r--src/main/java/at/gv/egiz/pdfas/commandline/Main.java4
-rw-r--r--src/main/java/at/gv/egiz/pdfas/exceptions/ErrorCode.java3
-rw-r--r--src/main/java/at/gv/egiz/pdfas/exceptions/framework/CorrectorException.java36
-rw-r--r--src/main/java/at/gv/egiz/pdfas/framework/input/correction/Corrector.java41
-rw-r--r--src/main/java/at/gv/egiz/pdfas/framework/input/correction/CorrectorFactory.java43
-rw-r--r--src/main/java/at/gv/egiz/pdfas/impl/api/PdfAsObject.java2
-rw-r--r--src/main/java/at/gv/egiz/pdfas/impl/input/correction/ExternalCorrector.java263
-rw-r--r--src/main/java/at/gv/egiz/pdfas/impl/input/correction/InternalCorrector.java57
-rw-r--r--src/main/java/at/gv/egiz/pdfas/web/helper/TempDirHelper.java23
9 files changed, 463 insertions, 9 deletions
diff --git a/src/main/java/at/gv/egiz/pdfas/commandline/Main.java b/src/main/java/at/gv/egiz/pdfas/commandline/Main.java
index 147b540..b4546e4 100644
--- a/src/main/java/at/gv/egiz/pdfas/commandline/Main.java
+++ b/src/main/java/at/gv/egiz/pdfas/commandline/Main.java
@@ -573,10 +573,6 @@ public abstract class Main
public static void processSign(DataSource dataSource, String connector, String signature_mode, String signature_type, String pos_string, DataSink dataSink) throws PdfAsException
{
- // FIXME implement this
- // TODO this will change soon due to the document-cleaning change request.
- //PdfAS.applyStrictMode(pdfDataSource);
-
TablePos pos = null;
if (pos_string != null)
{
diff --git a/src/main/java/at/gv/egiz/pdfas/exceptions/ErrorCode.java b/src/main/java/at/gv/egiz/pdfas/exceptions/ErrorCode.java
index d2345c8..417c48f 100644
--- a/src/main/java/at/gv/egiz/pdfas/exceptions/ErrorCode.java
+++ b/src/main/java/at/gv/egiz/pdfas/exceptions/ErrorCode.java
@@ -50,6 +50,7 @@ public final class ErrorCode
public static final int PLACEHOLDER_EXCEPTION = 700;
public static final int CAPTION_NOT_FOUND_EXCEPTION = 701;
-
+ public static final int CORRECTOR_EXCEPTION = 801;
+ public static final int EXTERNAL_CORRECTOR_TIMEOUT_REACHED = 802;
}
diff --git a/src/main/java/at/gv/egiz/pdfas/exceptions/framework/CorrectorException.java b/src/main/java/at/gv/egiz/pdfas/exceptions/framework/CorrectorException.java
new file mode 100644
index 0000000..d4759f3
--- /dev/null
+++ b/src/main/java/at/gv/egiz/pdfas/exceptions/framework/CorrectorException.java
@@ -0,0 +1,36 @@
+/**
+ *
+ */
+package at.gv.egiz.pdfas.exceptions.framework;
+
+import at.knowcenter.wag.egov.egiz.exceptions.PresentableException;
+
+/**
+ * Exceptions thrown by Correctors.
+ *
+ * @author wprinz
+ *
+ */
+public class CorrectorException extends PresentableException
+{
+ /**
+ * SVUID.
+ */
+ private static final long serialVersionUID = -8646964226476111797L;
+
+ public CorrectorException(int errorCode, String message, Throwable cause)
+ {
+ super(errorCode, message, cause);
+ }
+
+ public CorrectorException(int errorCode, String message)
+ {
+ super(errorCode, message);
+ }
+
+ public CorrectorException(int errorCode, Throwable cause)
+ {
+ super(errorCode, cause);
+ }
+
+}
diff --git a/src/main/java/at/gv/egiz/pdfas/framework/input/correction/Corrector.java b/src/main/java/at/gv/egiz/pdfas/framework/input/correction/Corrector.java
new file mode 100644
index 0000000..3b4d4b4
--- /dev/null
+++ b/src/main/java/at/gv/egiz/pdfas/framework/input/correction/Corrector.java
@@ -0,0 +1,41 @@
+/**
+ *
+ */
+package at.gv.egiz.pdfas.framework.input.correction;
+
+import at.gv.egiz.pdfas.exceptions.framework.CorrectorException;
+import at.gv.egiz.pdfas.framework.input.PdfDataSource;
+
+/**
+ * Interface for PDF corretors.
+ *
+ * <p>
+ * Often PDF documents generated by various document to PDF converters have an
+ * invalid structure that upsets PDF-AS. The correction mechanism allows these
+ * documents to be corrected before being signed.
+ * </p>
+ * <p>
+ * A PDF corrector takes an incorrect PDF document and transforms it into a
+ * correct one.
+ * </p>
+ * <p>
+ * Note that correcting a document destroys all signatures in that document, so
+ * never correct an already signed document.
+ * </p>
+ *
+ * @author wprinz
+ */
+public interface Corrector
+{
+ /**
+ * Corrects the given PDF document to a form that PDF-AS can use.
+ *
+ * @param document
+ * The (incorrect) PDF document.
+ * @return Returns the corrected PDF document.
+ * @throws CorrectorException
+ * Exception thrown if the document couldn't be corrected.
+ */
+ public PdfDataSource correctDocument(PdfDataSource document) throws CorrectorException;
+
+}
diff --git a/src/main/java/at/gv/egiz/pdfas/framework/input/correction/CorrectorFactory.java b/src/main/java/at/gv/egiz/pdfas/framework/input/correction/CorrectorFactory.java
new file mode 100644
index 0000000..083713f
--- /dev/null
+++ b/src/main/java/at/gv/egiz/pdfas/framework/input/correction/CorrectorFactory.java
@@ -0,0 +1,43 @@
+/**
+ *
+ */
+package at.gv.egiz.pdfas.framework.input.correction;
+
+import at.gv.egiz.pdfas.impl.input.correction.ExternalCorrector;
+import at.gv.egiz.pdfas.impl.input.correction.InternalCorrector;
+import at.knowcenter.wag.egov.egiz.cfg.SettingsReader;
+import at.knowcenter.wag.egov.egiz.exceptions.SettingsException;
+
+/**
+ * Factory for creating Correctors.
+ *
+ * @author wprinz
+ */
+public class CorrectorFactory
+{
+ public static final String INTERNAL_CORRECTOR = "internal";
+
+ public static final String EXTERNAL_CORRECTOR = "external";
+
+ public static final String CORRECTOR_KEY = "corrector";
+
+ public static Corrector createCorrector(String id) throws SettingsException
+ {
+ if (id.equals(INTERNAL_CORRECTOR))
+ {
+ return new InternalCorrector();
+ }
+ if (id.equals(EXTERNAL_CORRECTOR))
+ {
+ return new ExternalCorrector();
+ }
+ throw new SettingsException("The connector id '" + id + "' is not a valid corrector id.");
+ }
+
+ public static Corrector createCorrector() throws SettingsException
+ {
+ String id = SettingsReader.getInstance().getSetting(CORRECTOR_KEY, INTERNAL_CORRECTOR);
+ return createCorrector(id);
+ }
+
+}
diff --git a/src/main/java/at/gv/egiz/pdfas/impl/api/PdfAsObject.java b/src/main/java/at/gv/egiz/pdfas/impl/api/PdfAsObject.java
index a343de8..3b5d0a6 100644
--- a/src/main/java/at/gv/egiz/pdfas/impl/api/PdfAsObject.java
+++ b/src/main/java/at/gv/egiz/pdfas/impl/api/PdfAsObject.java
@@ -134,6 +134,8 @@ public class PdfAsObject implements PdfAs
public SignResult sign(SignParameters signParameters) throws PdfAsException
{
CheckHelper.checkSignParameters(signParameters);
+
+ signParameters.setDocument(PdfAS.applyStrictMode(signParameters.getDocument()));
PdfASID signatorId = null;
if (signParameters.getSignatureType().equals(Constants.SIGNATURE_TYPE_BINARY))
diff --git a/src/main/java/at/gv/egiz/pdfas/impl/input/correction/ExternalCorrector.java b/src/main/java/at/gv/egiz/pdfas/impl/input/correction/ExternalCorrector.java
new file mode 100644
index 0000000..e244746
--- /dev/null
+++ b/src/main/java/at/gv/egiz/pdfas/impl/input/correction/ExternalCorrector.java
@@ -0,0 +1,263 @@
+/**
+ *
+ */
+package at.gv.egiz.pdfas.impl.input.correction;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStreamReader;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import at.gv.egiz.pdfas.exceptions.ErrorCode;
+import at.gv.egiz.pdfas.exceptions.framework.CorrectorException;
+import at.gv.egiz.pdfas.framework.input.PdfDataSource;
+import at.gv.egiz.pdfas.framework.input.correction.Corrector;
+import at.gv.egiz.pdfas.impl.input.FileBased;
+import at.gv.egiz.pdfas.impl.input.FileBasedPdfDataSourceImpl;
+import at.gv.egiz.pdfas.web.helper.TempDirHelper;
+import at.knowcenter.wag.egov.egiz.cfg.SettingsReader;
+import at.knowcenter.wag.egov.egiz.exceptions.SettingNotFoundException;
+import at.knowcenter.wag.egov.egiz.exceptions.SettingsException;
+
+/**
+ * Corrects the document using an extrenal commandline tool.
+ *
+ * <p>
+ * Process.destroy after a certain timeout does not work if the executable is a
+ * Windows batch file.
+ * </p>
+ *
+ * @author wprinz
+ */
+public class ExternalCorrector implements Corrector
+{
+ public static final String INPUT_DOCUMENT_REPLACE = "##input_document##";
+
+ public static final String OUTPUT_DOCUMENT_REPLACE = "##output_document##";
+
+ public static final String COMMANDLINE_KEY = "external_corrector_commandline";
+
+ public static final String TIMEOUT_KEY = "external_corrector_timeout";
+
+ protected static final int DEFAULT_TIMEOUT = 1000;
+
+ /**
+ * The log.
+ */
+ private static final Log log = LogFactory.getLog(ExternalCorrector.class);
+
+ /**
+ * @see at.gv.egiz.pdfas.framework.input.correction.Corrector#correctDocument(at.gv.egiz.pdfas.framework.input.PdfDataSource)
+ */
+ public PdfDataSource correctDocument(PdfDataSource document) throws CorrectorException
+ {
+
+ try
+ {
+ String outName = null;
+ File in = null;
+ if (document instanceof FileBased)
+ {
+ FileBased fb = (FileBased) document;
+ in = fb.getFile();
+ outName = in.getName() + "_correction_outfile.pdf";
+ }
+ else
+ {
+ in = TempDirHelper.placeInputIntoTempDirFile(document.createInputStream(), "correction_infile.pdf");
+ outName = "correction_outfile.pdf";
+ }
+
+ File out = TempDirHelper.formTempFile(outName);
+
+ String commandline = SettingsReader.getInstance().getSetting(COMMANDLINE_KEY);
+ long timeout = SettingsReader.getInstance().getIntSetting(TIMEOUT_KEY, DEFAULT_TIMEOUT);
+
+ String inF = in.getAbsolutePath();
+ commandline = commandline.replaceFirst(INPUT_DOCUMENT_REPLACE, inF.replaceAll("\\\\", "\\\\\\\\"));
+ String outF = out.getAbsolutePath();
+ commandline = commandline.replaceFirst(OUTPUT_DOCUMENT_REPLACE, outF.replaceAll("\\\\", "\\\\\\\\"));
+
+ log.info(commandline);
+
+ Process p = Runtime.getRuntime().exec(commandline);
+
+ Thread outT = null;
+ Thread errT = null;
+ TimeoutThread tt = null;
+ BufferedReader outReader = null;
+ BufferedReader errReader = null;
+
+ try
+ {
+ outReader = new BufferedReader(new InputStreamReader(p.getInputStream()));
+ errReader = new BufferedReader(new InputStreamReader(p.getErrorStream()));
+
+ outT = new Thread(new ReaderPrinter(outReader, "STDOUT"));
+ errT = new Thread(new ReaderPrinter(errReader, "STDERR"));
+
+ tt = new TimeoutThread(p, timeout, new Thread[] { outT, errT });
+
+ tt.start();
+ outT.start();
+ errT.start();
+
+ log.trace("Joining the STDOUT thread...");
+ outT.join();
+ log.trace("STDOUT thread joined.");
+ log.trace("Joining the STDERR thread...");
+ errT.join();
+ log.trace("STDERR thread joined.");
+
+ log.trace("Waiting for process to end...");
+ p.waitFor();
+ log.trace("process has ended.");
+
+ log.trace("Interrupting timeout thread...");
+ tt.interrupt();
+ log.trace("timeout thread has been interrupted.");
+
+ int exitValue = p.exitValue();
+ log.info("External Corrector exited with: " + exitValue);
+
+ if (tt.isTimedOut())
+ {
+ throw new CorrectorException(ErrorCode.EXTERNAL_CORRECTOR_TIMEOUT_REACHED, "The external corrector process timed out. timeout = " + timeout);
+ }
+
+ PdfDataSource ds = new FileBasedPdfDataSourceImpl(out, (int) out.length());
+ return ds;
+ }
+ finally
+ {
+ if (outT != null)
+ {
+ outT.interrupt();
+ }
+ if (errT != null)
+ {
+ errT.interrupt();
+ }
+ if (tt != null)
+ {
+ tt.interrupt();
+ }
+ if (outReader != null)
+ {
+ outReader.close();
+ }
+ if (errReader != null)
+ {
+ errReader.close();
+ }
+ }
+
+ }
+ catch (IOException e)
+ {
+ throw new CorrectorException(ErrorCode.CORRECTOR_EXCEPTION, e);
+ }
+ catch (InterruptedException e)
+ {
+ throw new CorrectorException(ErrorCode.CORRECTOR_EXCEPTION, e);
+ }
+ catch (SettingNotFoundException e)
+ {
+ throw new CorrectorException(ErrorCode.CORRECTOR_EXCEPTION, e);
+ }
+ catch (SettingsException e)
+ {
+ throw new CorrectorException(ErrorCode.CORRECTOR_EXCEPTION, e);
+ }
+ }
+
+ protected static class ReaderPrinter implements Runnable
+ {
+ protected BufferedReader reader = null;
+
+ protected String streamName = null;
+
+ public ReaderPrinter(BufferedReader reader, String streamName)
+ {
+ this.reader = reader;
+ this.streamName = streamName;
+ }
+
+ public void run()
+ {
+ try
+ {
+ String line = null;
+
+ while ((line = this.reader.readLine()) != null)
+ {
+ if (line != null)
+ {
+ log.info(streamName + ": " + line);
+ }
+ }
+ }
+ catch (IOException e)
+ {
+ log.error(e);
+ }
+ }
+ }
+
+ protected static class TimeoutThread extends Thread
+ {
+ protected Process proc = null;
+
+ protected long timeout = -1;
+
+ protected boolean ranIntoTimeout = false;
+
+ protected Thread[] threads;
+
+ protected BufferedReader errReader;
+
+ public TimeoutThread(Process proc, long timeout, Thread[] threadsToInterrupt)
+ {
+ this.proc = proc;
+ this.timeout = timeout;
+ this.threads = threadsToInterrupt;
+ }
+
+ public void run()
+ {
+ try
+ {
+ Thread.sleep(this.timeout);
+ log.info("The timeout was reached. Destroying the process.");
+ proc.destroy();
+ ranIntoTimeout = true;
+ log.trace("destroy has been called.");
+ log.trace("Interrupting threads...");
+ for (int i = 0; i < this.threads.length; i++)
+ {
+ this.threads[i].interrupt();
+ }
+ log.trace("threads have been interrupted.");
+ }
+ catch (InterruptedException e)
+ {
+ log.debug("Timeout thread interrupted. This means that the process finished successfully.");
+ }
+ }
+
+ /**
+ * Tells, if the process ran into the timeout.
+ *
+ * @return Returns true if the timeout was reached. Returns false if the
+ * timeout was not reached.
+ */
+ public boolean isTimedOut()
+ {
+ return this.ranIntoTimeout;
+ }
+ }
+
+}
diff --git a/src/main/java/at/gv/egiz/pdfas/impl/input/correction/InternalCorrector.java b/src/main/java/at/gv/egiz/pdfas/impl/input/correction/InternalCorrector.java
new file mode 100644
index 0000000..409a600
--- /dev/null
+++ b/src/main/java/at/gv/egiz/pdfas/impl/input/correction/InternalCorrector.java
@@ -0,0 +1,57 @@
+/**
+ *
+ */
+package at.gv.egiz.pdfas.impl.input.correction;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+
+import at.gv.egiz.pdfas.exceptions.ErrorCode;
+import at.gv.egiz.pdfas.exceptions.framework.CorrectorException;
+import at.gv.egiz.pdfas.framework.input.PdfDataSource;
+import at.gv.egiz.pdfas.framework.input.correction.Corrector;
+import at.gv.egiz.pdfas.impl.input.ByteArrayPdfDataSourceImpl;
+
+import com.lowagie.text.DocumentException;
+import com.lowagie.text.pdf.PdfReader;
+import com.lowagie.text.pdf.PdfStamper;
+
+/**
+ * Corrects a document using iText.
+ *
+ * @author wprinz
+ */
+public class InternalCorrector implements Corrector
+{
+
+ /**
+ * @see at.gv.egiz.pdfas.framework.input.correction.Corrector#correctDocument(at.gv.egiz.pdfas.framework.input.PdfDataSource)
+ */
+ public PdfDataSource correctDocument(PdfDataSource document) throws CorrectorException
+ {
+ try
+ {
+ byte[] pdf = document.getAsByteArray();
+ PdfReader reader = new PdfReader(pdf);
+
+ ByteArrayOutputStream baos = new ByteArrayOutputStream(pdf.length);
+
+ PdfStamper stamper = new PdfStamper(reader, baos, '\0', false);
+ stamper.close();
+
+ baos.close();
+ byte[] corrected_pdf = baos.toByteArray();
+
+ return new ByteArrayPdfDataSourceImpl(corrected_pdf);
+ }
+ catch (DocumentException e)
+ {
+ throw new CorrectorException(ErrorCode.CORRECTOR_EXCEPTION, e);
+ }
+ catch (IOException e)
+ {
+ throw new CorrectorException(ErrorCode.CORRECTOR_EXCEPTION, e);
+ }
+ }
+
+}
diff --git a/src/main/java/at/gv/egiz/pdfas/web/helper/TempDirHelper.java b/src/main/java/at/gv/egiz/pdfas/web/helper/TempDirHelper.java
index 3775776..1b7971c 100644
--- a/src/main/java/at/gv/egiz/pdfas/web/helper/TempDirHelper.java
+++ b/src/main/java/at/gv/egiz/pdfas/web/helper/TempDirHelper.java
@@ -137,7 +137,7 @@ public class TempDirHelper
return pdfDataSource;
}
- protected static File placeInputIntoTempDirFile(InputStream input, String fileNameSuffix) throws IOException
+ public static File placeInputIntoTempDirFile(InputStream input, String fileNameSuffix) throws IOException
{
String fileName = formatFileName(fileNameSuffix);
@@ -156,6 +156,23 @@ public class TempDirHelper
return tmpFile;
}
+
+ public static File formTempFile(String fileNameSuffix)
+ {
+ String fileName = formatFileName(fileNameSuffix);
+ File tmpFile = getFileInTempDir(fileName);
+
+ return tmpFile;
+ }
+
+ protected static File getFileInTempDir (String fileName)
+ {
+ File tempDir = new File(new File(SettingsReader.RESOURCES_PATH), "pdfastmp");
+
+ File tmpFile = new File(tempDir, fileName);
+
+ return tmpFile;
+ }
protected static String formatFileName(String fileNameSuffix)
{
@@ -176,9 +193,7 @@ public class TempDirHelper
protected static File createTempFileInDir(String fileName) throws IOException
{
- File tempDir = new File(new File(SettingsReader.RESOURCES_PATH), "pdfastmp");
-
- File tmpFile = new File(tempDir, fileName);
+ File tmpFile = getFileInTempDir(fileName);
tmpFile.createNewFile();