aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/knowcenter/wag/egov/egiz/PdfAS.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/at/knowcenter/wag/egov/egiz/PdfAS.java')
-rw-r--r--src/main/java/at/knowcenter/wag/egov/egiz/PdfAS.java737
1 files changed, 365 insertions, 372 deletions
diff --git a/src/main/java/at/knowcenter/wag/egov/egiz/PdfAS.java b/src/main/java/at/knowcenter/wag/egov/egiz/PdfAS.java
index 0576781..e2505ea 100644
--- a/src/main/java/at/knowcenter/wag/egov/egiz/PdfAS.java
+++ b/src/main/java/at/knowcenter/wag/egov/egiz/PdfAS.java
@@ -19,19 +19,31 @@ package at.knowcenter.wag.egov.egiz;
import java.io.ByteArrayInputStream;
import java.io.IOException;
-import java.io.UnsupportedEncodingException;
-import java.lang.reflect.Array;
+import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Vector;
+import at.gv.egiz.pdfas.commandline.CommandlineConnectorChooser;
+import at.gv.egiz.pdfas.exceptions.ErrorCode;
+import at.gv.egiz.pdfas.impl.input.DelimitedInputStream;
+import at.gv.egiz.pdfas.impl.input.TextDataSourceImpl;
+import at.gv.egiz.pdfas.impl.input.helper.DataSourceHelper;
+import at.gv.egiz.pdfas.framework.input.DataSource;
+import at.gv.egiz.pdfas.framework.input.PdfDataSource;
+import at.gv.egiz.pdfas.framework.input.TextDataSource;
+import at.gv.egiz.pdfas.framework.output.DataSink;
+import at.gv.egiz.pdfas.framework.signator.SignatorInformation;
+import at.gv.egiz.pdfas.web.VerifySessionInformation;
+
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import at.knowcenter.wag.egov.egiz.cfg.SettingsReader;
import at.knowcenter.wag.egov.egiz.exceptions.ConnectorException;
+import at.knowcenter.wag.egov.egiz.exceptions.ConnectorFactoryException;
import at.knowcenter.wag.egov.egiz.exceptions.NormalizeException;
import at.knowcenter.wag.egov.egiz.exceptions.PDFDocumentException;
import at.knowcenter.wag.egov.egiz.exceptions.PresentableException;
@@ -40,11 +52,7 @@ import at.knowcenter.wag.egov.egiz.exceptions.SignatureException;
import at.knowcenter.wag.egov.egiz.exceptions.SignatureTypesException;
import at.knowcenter.wag.egov.egiz.framework.FoundBlock;
import at.knowcenter.wag.egov.egiz.framework.FoundKey;
-import at.knowcenter.wag.egov.egiz.framework.SignResult;
-import at.knowcenter.wag.egov.egiz.framework.Signator;
-import at.knowcenter.wag.egov.egiz.framework.SignatorFactory;
-import at.knowcenter.wag.egov.egiz.framework.VerificationFilter;
-import at.knowcenter.wag.egov.egiz.pdf.IncrementalUpdateInformation;
+import at.knowcenter.wag.egov.egiz.pdf.BinarySignatureHolder;
import at.knowcenter.wag.egov.egiz.pdf.PDFSignatureCreation;
import at.knowcenter.wag.egov.egiz.pdf.PDFSignatureObject;
import at.knowcenter.wag.egov.egiz.pdf.PDFUtilities;
@@ -64,9 +72,7 @@ import at.knowcenter.wag.egov.egiz.sig.connectors.Connector;
import at.knowcenter.wag.egov.egiz.sig.connectors.ConnectorChooser;
import at.knowcenter.wag.egov.egiz.sig.connectors.bku.SignSignatureObject;
import at.knowcenter.wag.egov.egiz.sig.signatureobject.SignatureObjectHelper;
-import at.knowcenter.wag.egov.egiz.tools.CodingHelper;
import at.knowcenter.wag.egov.egiz.tools.Normalizer;
-import at.knowcenter.wag.egov.egiz.web.SessionInformation;
import at.knowcenter.wag.exactparser.ParseDocument;
import at.knowcenter.wag.exactparser.parsing.PDFUtils;
import at.knowcenter.wag.exactparser.parsing.results.HeaderParseResult;
@@ -135,17 +141,26 @@ public abstract class PdfAS
/**
* Checks the version of the given PDF to be 1.4 or lower.
*
- * @param pdf
+ * <p>
+ * This scans the first 1000 bytes of the PDF for the pdf Header.
+ * </p>
+ *
+ * @param pdfDataSource
* The PDF.
* @return Returns true, if the given PDF is strict 1.4, false otherwise.
* @throws PDFDocumentException
* Forwarded exception.
*/
- public static boolean isPdf14(byte[] pdf) throws PDFDocumentException
+ public static boolean isPdf14(PdfDataSource pdfDataSource) throws PDFDocumentException
{
try
{
- HeaderParseResult hpr = PDFUtils.parseHeader(pdf, 0);
+ byte[] firstBytes = new byte[1000];
+ InputStream is = pdfDataSource.createInputStream();
+ is.read(firstBytes);
+ is.close();
+
+ HeaderParseResult hpr = PDFUtils.parseHeader(firstBytes, 0);
if (hpr.major <= 1 && hpr.minor <= 4)
{
@@ -168,21 +183,22 @@ public abstract class PdfAS
* parsability.
* </p>
*
- * @param pdf
+ * @param pdfDataSource
* The pdf to be checked against strict mode.
* @throws PDFDocumentException
*/
- public static void applyStrictMode(byte[] pdf) throws PDFDocumentException
+ public static void applyStrictMode(PdfDataSource pdfDataSource) throws PDFDocumentException
{
if (isStrictPdfChecking())
{
- if (!isPdf14(pdf))
+ if (!isPdf14(pdfDataSource))
{
throw new PDFDocumentException(201, "StrictMode: The pdf version is not 1.4 or lower.");
}
}
try
{
+ byte[] pdf = DataSourceHelper.convertDataSourceToByteArray(pdfDataSource);
ParseDocument.parseDocument(pdf);
}
catch (Exception e)
@@ -192,56 +208,60 @@ public abstract class PdfAS
}
}
- /**
- * Verifies the given PDF document.
- *
- * @param pdf
- * The PDF document.
- * @param connector
- * The connector.
- * @return Returns the List of results.
- * @throws PresentableException
- * Forwarded exception.
- */
- public static List verifyPdf(final byte[] pdf, final String connector) throws PresentableException
- {
- VerificationFilter vf = new VerificationFilter();
- List signature_holders = vf.extractSignaturesFromPdf(pdf);
- if (signature_holders.isEmpty())
- {
- throw new PDFDocumentException(206);
- }
-
- List results = verifySignatureHolders(signature_holders, connector);
-
- return results;
- }
-
- /**
- * Verifies the given text that is supposed to be extracted from a PDF
- * document using text extraction mechanisms.
- *
- * @param text
- * The text to be verified.
- * @param connector
- * The connecor.
- * @return Returns the List of results.
- * @throws PresentableException
- * Forwarded exception.
- */
- public static List verifyText(final String text, final String connector) throws PresentableException
- {
- VerificationFilter vf = new VerificationFilter();
- List signature_holders = vf.extractSignaturesFromPlainText(text);
- if (signature_holders.isEmpty())
- {
- throw new PDFDocumentException(206);
- }
-
- List results = verifySignatureHolders(signature_holders, connector);
-
- return results;
- }
+ // TODO: unused method - remove
+ // /**
+ // * Verifies the given PDF document.
+ // *
+ // * @param pdf
+ // * The PDF document.
+ // * @param connector
+ // * The connector.
+ // * @return Returns the List of results.
+ // * @throws PresentableException
+ // * Forwarded exception.
+ // */
+ // public static List verifyPdf(final byte[] pdf, final String connector)
+ // throws PresentableException
+ // {
+ // VerificationFilter vf = new VerificationFilter();
+ // List signature_holders = vf.extractSignaturesFromPdf(pdf);
+ // if (signature_holders.isEmpty())
+ // {
+ // throw new PDFDocumentException(206);
+ // }
+ //
+ // List results = verifySignatureHolders(signature_holders, connector);
+ //
+ // return results;
+ // }
+
+ // TODO: unused method - remove
+ // /**
+ // * Verifies the given text that is supposed to be extracted from a PDF
+ // * document using text extraction mechanisms.
+ // *
+ // * @param text
+ // * The text to be verified.
+ // * @param connector
+ // * The connecor.
+ // * @return Returns the List of results.
+ // * @throws PresentableException
+ // * Forwarded exception.
+ // */
+ // public static List verifyText(final String text, final String connector)
+ // throws PresentableException
+ // {
+ // VerificationFilter vf = new VerificationFilter();
+ // List signature_holders = vf.extractSignaturesFromPlainText(text);
+ // if (signature_holders.isEmpty())
+ // {
+ // throw new PDFDocumentException(206);
+ // }
+ //
+ // List results = verifySignatureHolders(signature_holders, connector);
+ //
+ // return results;
+ // }
/**
* Extracts all signature blocks from the given raw text using textual mode.
@@ -256,15 +276,14 @@ public abstract class PdfAS
* @throws SignatureTypesException
* @throws NormalizeException
*/
- public static List extractSignatureHoldersTextual(String raw_text,
- boolean old_style) throws PDFDocumentException, SignatureException, SignatureTypesException, NormalizeException
+ public static List extractSignatureHoldersTextual(String raw_text, boolean old_style) throws PDFDocumentException, SignatureException, SignatureTypesException, NormalizeException
{
List signature_holders = new ArrayList();
String text = raw_text;
for (;;)
{
- SignatureHolder holder = extractSignatureHolderTextual(text, old_style);
+ TextualSignatureHolder holder = extractSignatureHolderTextual(text, old_style);
if (holder == null)
{
break;
@@ -291,8 +310,7 @@ public abstract class PdfAS
* @throws SignatureTypesException
* @throws NormalizeException
*/
- public static SignatureHolder extractSignatureHolderTextual(String raw_text,
- boolean old_style) throws SignatureException, SignatureTypesException, NormalizeException
+ public static TextualSignatureHolder extractSignatureHolderTextual(String raw_text, boolean old_style) throws SignatureException, SignatureTypesException, NormalizeException
{
SignatureTypes sig_types = SignatureTypes.getInstance();
List signatureTypes_ = sig_types.getSignatureTypeDefinitions();
@@ -339,7 +357,7 @@ public abstract class PdfAS
if (!semantic_equality)
{
- throw new SignatureException(314);
+ throw new SignatureException(ErrorCode.NOT_SEMANTICALLY_EQUAL, "The found blocks are not semantically equal.");
}
if (!minimum_blocks.isEmpty())
@@ -368,7 +386,7 @@ public abstract class PdfAS
// this normalization is required to get rid of possible trailing
// newlines.
String normalized_text = normalizeText(signed_text);
- SignatureHolder holder = new TextualSignatureHolder(normalized_text, signatureObject_);
+ TextualSignatureHolder holder = new TextualSignatureHolder(normalized_text, signatureObject_);
return holder;
}
@@ -388,8 +406,7 @@ public abstract class PdfAS
* @return Returns a List of the found keys of the block, or null, if the
* block could not be found.
*/
- public static List findBlockInText(String text,
- SignatureTypeDefinition sig_type_def, boolean old_style)
+ public static List findBlockInText(String text, SignatureTypeDefinition sig_type_def, boolean old_style)
{
Vector keys = sig_type_def.getRevertSortedKeys();
Vector captions = sig_type_def.getRevertSortedCaptions();
@@ -472,8 +489,7 @@ public abstract class PdfAS
{
// sort the found_keys according to their start pos reversely.
- Collections.sort(found_keys, new Comparator()
- {
+ Collections.sort(found_keys, new Comparator() {
public int compare(Object arg0, Object arg1)
{
FoundKey fk0 = (FoundKey) arg0;
@@ -496,8 +512,7 @@ public abstract class PdfAS
{
// sort the found_keys according to their start pos.
- Collections.sort(found_keys, new Comparator()
- {
+ Collections.sort(found_keys, new Comparator() {
public int compare(Object arg0, Object arg1)
{
FoundKey fk0 = (FoundKey) arg0;
@@ -524,8 +539,7 @@ public abstract class PdfAS
* Tells, if SIG_KZ should be ignored, or not.
* @return Returns true, if the keys are correct.
*/
- public static boolean checkThatOrderIsCorrectAndCorrectFoundKeys(
- List found_keys, List profile_keys, boolean old_style)
+ public static boolean checkThatOrderIsCorrectAndCorrectFoundKeys(List found_keys, List profile_keys, boolean old_style)
{
int found_index = 0;
@@ -700,8 +714,7 @@ public abstract class PdfAS
* Flag that tells, if KZ is not required.
* @return Returns true, if all required keys were found.
*/
- public static boolean checkFoundRequiredKeys(List found_keys,
- boolean old_style)
+ public static boolean checkFoundRequiredKeys(List found_keys, boolean old_style)
{
if (!containsFoundKey(found_keys, SignatureTypes.SIG_DATE))
{
@@ -726,23 +739,7 @@ public abstract class PdfAS
return true;
}
- /**
- * Verifies the List of SignatureHolders using the given connector.
- *
- * @param signature_holders
- * The List of SignatureHolder objects to be verified.
- * @param connector
- * The connector.
- * @return Returns the List of SignatureResponse objects.
- * @throws PDFDocumentException
- * F.e.
- * @throws NormalizeException
- * F.e.
- * @throws SignatureException
- * F.e.
- */
- public static List verifySignatureHolders(List signature_holders,
- String connector) throws PDFDocumentException, NormalizeException, SignatureException
+ public static List verifySignatureHolders(List signature_holders, String connectorType) throws PDFDocumentException, NormalizeException, SignatureException, ConnectorException, ConnectorFactoryException
{
List results = new ArrayList();
for (int i = 0; i < signature_holders.size(); i++)
@@ -759,7 +756,7 @@ public abstract class PdfAS
// (holder.signature_object.isTextual() ? "textual" : "binary"));
// logger_.debug(holder.signature_object.toString());
- SignatureResponse result = verify(holder, connector);
+ SignatureResponse result = verify(holder, connectorType);
results.add(result);
// logger_.debug();
@@ -773,38 +770,24 @@ public abstract class PdfAS
}
return results;
}
-
- // TODO the choosing algorithm should be extracted into a visitor or factory design pattern.
+
public static List verifySignatureHoldersWeb(List signature_holders,
- //String connector, String loc_ref) throws PDFDocumentException, NormalizeException, SignatureException
- SessionInformation si, String loc_ref) throws PDFDocumentException, NormalizeException, SignatureException
+ VerifySessionInformation si, String loc_ref) throws PDFDocumentException, NormalizeException, SignatureException, ConnectorException
{
- List results = new ArrayList();
-
- si.current_operation = 0;
-
- if(signature_holders.size() == 1)
- {
- List temp_holders = new ArrayList(si.signature_holders);
- si.signature_holders = signature_holders;
- SignatureHolder tmp_holder = (SignatureHolder) signature_holders.get(0);
- SignatureResponse res = verifyWeb(tmp_holder, si.connector, loc_ref);
- results.add(res);
- si.signature_holders = temp_holders;
-
- return results;
- }
-
- for (int i = 0; i < signature_holders.size(); i++)
+ List results = new ArrayList();
+
+ for (int i = 0; i < signature_holders.size(); i++)
{
SignatureHolder holder = (SignatureHolder) signature_holders.get(i);
+ // store the current holder on the session so that moa can obtain
+ // it from the RetrieveSignatureDataServlet.
+ si.moa_holder = holder;
+
SignatureResponse result = verifyWeb(holder, si.connector, loc_ref);
results.add(result);
-
- si.current_operation++;
}
-
+
return results;
}
@@ -822,77 +805,67 @@ public abstract class PdfAS
* F.e.
* @throws SignatureException
* F.e.
+ * @throws ConnectorException
+ * @throws ConnectorFactoryException
*/
- public static SignatureResponse verify(SignatureHolder signature_holder,
- String connector) throws NormalizeException, PDFDocumentException, SignatureException
+ public static SignatureResponse verify(SignatureHolder signature_holder, String connectorType) throws NormalizeException, PDFDocumentException, SignatureException, ConnectorException, ConnectorFactoryException
{
- String text_to_be_verified = signature_holder.getSignedText();
+ // String text_to_be_verified = signature_holder.getSignedText();
// logger_.debug("verify text_to_be_verified"+text_to_be_verified);
SignatureObject so_to_be_verified = signature_holder.getSignatureObject();
- if (text_to_be_verified == null)
- {
- throw new SignatureException(311, "Document can not be verified because the text to be verified is either null.");
- }
- if (text_to_be_verified.length() <= 0)
- {
- throw new SignatureException(311, "Document can not be verified because the length of the text to be verified is 0. (length = " + text_to_be_verified.length() + ")");
- }
+ // if (text_to_be_verified == null)
+ // {
+ // throw new SignatureException(311, "Document can not be verified because
+ // the text to be verified is either null.");
+ // }
+ // if (text_to_be_verified.length() <= 0)
+ // {
+ // throw new SignatureException(311, "Document can not be verified because
+ // the length of the text to be verified is 0. (length = " +
+ // text_to_be_verified.length() + ")");
+ // }
if (so_to_be_verified == null)
{
throw new SignatureException(312, "Document can not be verified because no signature object are set.");
}
-
- // fixed by tknall
- if (so_to_be_verified.getX509Cert() == null) {
- throw new SignatureException(313, "Document certificate is not defined.");
- }
- // FIXME this has to be made better
- SignatureData sd = null;
- if (so_to_be_verified.isBinary())
+ // fixed by tknall
+ if (so_to_be_verified.getX509Cert() == null)
{
- byte[] data = CodingHelper.decodeBase64(text_to_be_verified);
- sd = new SignatureDataImpl(data, "application/pdf");
- }
- else
- {
- try
- {
- sd = new SignatureDataImpl(text_to_be_verified.getBytes("UTF-8"), "text/plain", "UTF-8");
- }
- catch (UnsupportedEncodingException e)
- {
- throw new RuntimeException("Very Strange: UTF-8 character encoding not supported???");
- }
+ throw new SignatureException(313, "Document certificate is not defined.");
}
SignSignatureObject so = SignatureObjectHelper.convertSignatureObjectToSignSignatureObject(so_to_be_verified);
-
+ SignatureData sd = convertSignatureHolderToSignatureData(signature_holder);
+
String profile = so_to_be_verified.getSignatureTypeDefinition().getType();
- Connector c = ConnectorChooser.chooseCommandlineConnectorForVerify(connector, so_to_be_verified.getKZ(), so.id, profile);
+// Connector c = ConnectorChooser.chooseCommandlineConnectorForVerify(connector, so_to_be_verified.getKZ(), so.id, profile);
+ String connectorId = CommandlineConnectorChooser.chooseCommandlineConnectorForVerify(connectorType, so_to_be_verified.getKZ(), so.id, profile);
+ Connector c = at.gv.egiz.pdfas.framework.ConnectorFactory.createConnector(connectorId, profile, null);
return c.doVerify(sd, so);
}
- // TODO make this better using the visitor DP.
- public static SignatureResponse verifyWeb(SignatureHolder signature_holder,
- String connector, String loc_ref) throws NormalizeException, PDFDocumentException, SignatureException
+ public static SignatureResponse verifyWeb(SignatureHolder signature_holder, String connector, String loc_ref) throws NormalizeException, PDFDocumentException, SignatureException, ConnectorException
{
- String text_to_be_verified = signature_holder.getSignedText();
+ // String text_to_be_verified = signature_holder.getSignedText();
// logger_.debug("verify text_to_be_verified"+text_to_be_verified);
SignatureObject so_to_be_verified = signature_holder.getSignatureObject();
- if (text_to_be_verified == null)
- {
- throw new SignatureException(311, "Document can not be verified because the text to be verified is either null.");
- }
- if (text_to_be_verified.length() <= 0)
- {
- throw new SignatureException(311, "Document can not be verified because the length of the text to be verified is 0. (length = " + text_to_be_verified.length() + ")");
- }
+ // if (text_to_be_verified == null)
+ // {
+ // throw new SignatureException(311, "Document can not be verified because
+ // the text to be verified is either null.");
+ // }
+ // if (text_to_be_verified.length() <= 0)
+ // {
+ // throw new SignatureException(311, "Document can not be verified because
+ // the length of the text to be verified is 0. (length = " +
+ // text_to_be_verified.length() + ")");
+ // }
if (so_to_be_verified == null)
{
@@ -900,30 +873,14 @@ public abstract class PdfAS
}
// added by tknall
- if (so_to_be_verified.getX509Cert() == null) {
- throw new SignatureException(313, "Document certificate is not defined.");
- }
-
- // FIXME this has to be made better
- SignatureData sd = null;
- if (so_to_be_verified.isBinary())
+ if (so_to_be_verified.getX509Cert() == null)
{
- byte[] data = CodingHelper.decodeBase64(text_to_be_verified);
- sd = new SignatureDataImpl(data, "application/pdf");
- }
- else
- {
- try
- {
- sd = new SignatureDataImpl(text_to_be_verified.getBytes("UTF-8"), "text/plain", "UTF-8");
- }
- catch (UnsupportedEncodingException e)
- {
- throw new RuntimeException("Very Strange: UTF-8 character encoding not supported???");
- }
+ throw new SignatureException(313, "Document certificate is not defined.");
}
+
SignSignatureObject so = SignatureObjectHelper.convertSignatureObjectToSignSignatureObject(so_to_be_verified);
+ SignatureData sd = convertSignatureHolderToSignatureData(signature_holder);
String profile = so_to_be_verified.getSignatureTypeDefinition().getType();
Connector c = ConnectorChooser.chooseWebConnectorForVerify(connector, so_to_be_verified.getKZ(), so.id, profile, loc_ref);
@@ -931,6 +888,23 @@ public abstract class PdfAS
return c.doVerify(sd, so);
}
+
+ public static SignatureData convertSignatureHolderToSignatureData(SignatureHolder signature_holder)
+ {
+ SignatureData sd = null;
+ if (signature_holder instanceof BinarySignatureHolder)
+ {
+ BinarySignatureHolder bsh = (BinarySignatureHolder) signature_holder;
+ sd = new SignatureDataImpl(bsh.getDataSource(), "application/pdf");
+ }
+ else
+ {
+ TextualSignatureHolder tsh = (TextualSignatureHolder)signature_holder;
+ sd = new SignatureDataImpl(tsh.getDataSource(), "text/plain", "UTF-8");
+ }
+ return sd;
+ }
+
/**
* Signs the given text with the provided connector using the given signature
* type.
@@ -946,18 +920,20 @@ public abstract class PdfAS
* F.e.
* @throws PDFDocumentException
* F.e.
+ * @throws ConnectorException
*/
- public static SignSignatureObject sign(final SignatureData data_to_sign,
- final Connector connector) throws SignatureException, PDFDocumentException
+ public static SignSignatureObject sign(final SignatureData data_to_sign, final Connector connector) throws SignatureException, PDFDocumentException, ConnectorException
{
- if (data_to_sign == null || data_to_sign.getData() == null)
- {
- throw new SignatureException(301, "Signature can not be produced. Data is null."); //$NON-NLS-1$
- }
- if (data_to_sign.getData().length <= 0)
- {
- throw new SignatureException(301, "Signature can not be produced. Data is empty. (length = " + data_to_sign.getData().length + ")"); //$NON-NLS-1$ //$NON-NLS-2$
- }
+// if (data_to_sign == null || data_to_sign.getData() == null)
+// {
+// throw new SignatureException(301, "Signature can not be produced. Data is null."); //$NON-NLS-1$
+// }
+ // if (data_to_sign.getData().length <= 0)
+ // {
+ // throw new SignatureException(301, "Signature can not be produced. Data is
+ // empty. (length = " + data_to_sign.getData().length + ")"); //$NON-NLS-1$
+ // //$NON-NLS-2$
+ // }
SignSignatureObject signed_signature_object = connector.doSign(data_to_sign);
return signed_signature_object;
@@ -975,8 +951,7 @@ public abstract class PdfAS
* @throws SignatureTypesException
* f.e.
*/
- public static SignatureObject createSignatureObjectFromType(
- final String signature_type) throws SignatureException, SignatureTypesException
+ public static SignatureObject createSignatureObjectFromType(final String signature_type) throws SignatureException, SignatureTypesException
{
SignatureObject sig_obj = new SignatureObject();
sig_obj.setSigType(signature_type);
@@ -985,45 +960,53 @@ public abstract class PdfAS
return sig_obj;
}
- /**
- * Signs the document using the given algorithm.
- *
- * @param algorithm
- * The Signator algorithm to be used.
- * @param pdf
- * The PDF.
- * @param signature_type
- * The signature type.
- * @param connector
- * The connector.
- * @param user_name
- * The user name.
- * @param user_password
- * The password.
- * @param pos
- * The absolute position. If null, the position is either taken from
- * the profile or computed automatically.
- * @return Returns the SignResult.
- * @throws PresentableException
- * F.e.
- */
- public static SignResult sign(PdfASID algorithm, final byte[] pdf,
- final String signature_type, final String connector,
- final String user_name, final String user_password, TablePos pos) throws PresentableException
+// /**
+// * @deprecated moved to Main.processSign use signCommandline instead
+// * @param pdfDataSource
+// * @param dataSink
+// * @param signatorId
+// * @param connectorType
+// * @param profile
+// * @param pos
+// * @throws PresentableException
+// */
+// public static void sign(PdfDataSource pdfDataSource, DataSink dataSink, PdfASID signatorId, final String connectorType, final String profile, TablePos pos) throws PresentableException
+// {
+//// //Signator signator = SignatorFactory.createSignator(algorithm);
+//// at.gv.egiz.pdfas.framework.signator.Signator signator = at.gv.egiz.pdfas.framework.SignatorFactory.createSignator(algorithm);
+////
+//// //IncrementalUpdateInformation iui = signator.prepareSign(pdfDataSource, signature_type, pos, ConnectorFactory.needsSIG_ID(connector));
+//// SignatorInformation si = signator.prepareSign(pdfDataSource, signature_type, pos, ConnectorFactory.needsSIG_ID(connector));
+//
+// // Connector c = ConnectorChooser.chooseCommandlineConnectorForSign(connector, signature_type);
+//
+//// SignSignatureObject sso = sign(si.getSignatureData(), c);
+////
+//// si.setSignSignatureObject(sso);
+////
+//// signator.finishSign(si, dataSink);
+//
+// String connectorId = CommandlineConnectorChooser.chooseCommandlineConnectorForSign(connectorType);
+//
+// signCommandline(pdfDataSource, dataSink, signatorId, connectorId, profile, pos);
+// }
+
+ public static void signCommandline(PdfDataSource pdfDataSource, DataSink dataSink, PdfASID signatorId, String connectorId, final String profile, TablePos pos) throws PresentableException
{
- Signator signator = SignatorFactory.createSignator(algorithm);
-
- IncrementalUpdateInformation iui = signator.prepareSign(pdf, signature_type, pos, ConnectorFactory.needsSIG_ID(connector));
+ at.gv.egiz.pdfas.framework.signator.Signator signator = at.gv.egiz.pdfas.framework.SignatorFactory.createSignator(signatorId);
- Connector c = ConnectorChooser.chooseCommandlineConnectorForSign(connector, signature_type);
+ Connector c = at.gv.egiz.pdfas.framework.ConnectorFactory.createConnector(connectorId, profile, null);
- iui.signed_signature_object = sign(iui.signature_data, c);
+ // SignatorInformation si = signator.prepareSign(pdfDataSource, profile, pos, ConnectorFactory.needsSIG_ID(connector));
+ SignatorInformation si = signator.prepareSign(pdfDataSource, profile, pos, CommandlineConnectorChooser.needsSigId(connectorId));
- SignResult sign_result = signator.finishSign(iui);
+ SignSignatureObject sso = sign(si.getSignatureData(), c);
+
+ si.setSignSignatureObject(sso);
- return sign_result;
+ signator.finishSign(si, dataSink);
}
-
+
/**
* Extracts and normalizes the text from the pdf.
*
@@ -1033,40 +1016,52 @@ public abstract class PdfAS
* @throws PresentableException
* F.e.
*/
- public static String extractNormalizedTextTextual(final byte[] pdf) throws PresentableException
+// public static String extractNormalizedTextTextual(final byte[] pdf) throws PresentableException
+// {
+// // ByteArrayInputStream bais = new ByteArrayInputStream(pdf);
+// // String raw_document_text = TextualSignature.extractTextTextual(bais);
+// //
+// // String document_text = normalizeText(raw_document_text);
+//
+// return extractNormalizedTextTextual(pdf, pdf.length);
+// }
+
+ public static String extractNormalizedTextTextual(InputStream pdfInputStream) throws PresentableException
{
- // ByteArrayInputStream bais = new ByteArrayInputStream(pdf);
- // String raw_document_text = TextualSignature.extractTextTextual(bais);
- //
- // String document_text = normalizeText(raw_document_text);
-
- return extractNormalizedTextTextual(pdf, pdf.length);
+ String raw_document_text = TextualSignature.extractTextTextual(pdfInputStream);
+ String document_text = normalizeText(raw_document_text);
+ return document_text;
}
-
- /**
- * Extracts and normalizes the text from the pdf.
- *
- * @param pdf
- * The PDF document.
- * @param length
- * The length of the PDF document. The decument is considered to be
- * that long even if the byte array is longer.
- * @return Returns the text String.
- * @throws PresentableException
- * F.e.
- */
- public static String extractNormalizedTextTextual(final byte[] pdf,
- final int length) throws PresentableException
+ public static String extractNormalizedTextTextual(InputStream pdfInputStream, int length) throws PresentableException
{
- ByteArrayInputStream bais = new ByteArrayInputStream(pdf, 0, length);
- String raw_document_text = TextualSignature.extractTextTextual(bais);
- // logger_.info("extractNormalizedTextTextual
- // raw_document_text="+raw_document_text);
+ DelimitedInputStream dis = new DelimitedInputStream(pdfInputStream, length);
+ String raw_document_text = TextualSignature.extractTextTextual(dis);
String document_text = normalizeText(raw_document_text);
- // logger_.info("extractNormalizedTextTextual document_text
- // ="+document_text);
- return document_text;
+ return document_text;
}
+// /**
+// * Extracts and normalizes the text from the pdf.
+// *
+// * @param pdf
+// * The PDF document.
+// * @param length
+// * The length of the PDF document. The decument is considered to be
+// * that long even if the byte array is longer.
+// * @return Returns the text String.
+// * @throws PresentableException
+// * F.e.
+// */
+// public static String extractNormalizedTextTextual(final byte[] pdf, final int length) throws PresentableException
+// {
+// ByteArrayInputStream bais = new ByteArrayInputStream(pdf, 0, length);
+// String raw_document_text = TextualSignature.extractTextTextual(bais);
+// // logger_.info("extractNormalizedTextTextual
+// // raw_document_text="+raw_document_text);
+// String document_text = normalizeText(raw_document_text);
+// // logger_.info("extractNormalizedTextTextual document_text
+// // ="+document_text);
+// return document_text;
+// }
/**
* Normalizes the given text.
@@ -1095,8 +1090,7 @@ public abstract class PdfAS
* @throws SignatureException
* F.e.
*/
- public static PdfPTable createPdfPTableFromSignatureObject(
- final SignatureObject signature_object) throws PDFDocumentException, SignatureException
+ public static PdfPTable createPdfPTableFromSignatureObject(final SignatureObject signature_object) throws PDFDocumentException, SignatureException
{
PDFSignatureCreation creation = new PDFSignatureCreation(signature_object);
PDFSignatureObject pdf_sig_obj = creation.getPDFSignatureObject();
@@ -1124,8 +1118,7 @@ public abstract class PdfAS
* @throws SettingsException
* F.e.
*/
- public static PositioningInstruction determineTablePositioning(TablePos pos,
- String signature_type, byte[] pdf, PdfPTable pdf_table) throws PDFDocumentException, SettingsException
+ public static PositioningInstruction determineTablePositioning(TablePos pos, String signature_type, PdfDataSource pdfDataSource, PdfPTable pdf_table) throws PDFDocumentException, SettingsException
{
if (pos == null)
{
@@ -1142,7 +1135,7 @@ public abstract class PdfAS
pos = new TablePos();
}
// System.out.println("Tablepos="+pos);
- return PdfAS.adjustSignatureTableandCalculatePosition(pdf, pdf_table, pos);
+ return PdfAS.adjustSignatureTableandCalculatePosition(pdfDataSource, pdf_table, pos);
}
/**
@@ -1157,12 +1150,11 @@ public abstract class PdfAS
* @throws PDFDocumentException
* F.e.
*/
- public static PositioningInstruction adjustSignatureTableandCalculatePosition(
- final byte[] pdf, PdfPTable pdf_table, TablePos pos) throws PDFDocumentException
+ public static PositioningInstruction adjustSignatureTableandCalculatePosition(final PdfDataSource pdfDataSource, PdfPTable pdf_table, TablePos pos) throws PDFDocumentException
{
// first check pageinstruction in TablePos-object
// new,auto,absolut
- PdfReader reader = readInPdfDocument(pdf);
+ PdfReader reader = readInPdfDocument(pdfDataSource);
// get pages of currentdocument
int doc_pages = reader.getNumberOfPages();
int page = doc_pages;
@@ -1235,7 +1227,7 @@ public abstract class PdfAS
// up to here no checks have to be made if Tablesize and Pagesize are fit
// Now we have to getfreespace in page and reguard footerline
float footer_line = pos.getFooterLine();
- float pre_page_length = PDFUtilities.calculatePageLength(pdf, page - 1, page_height - footer_line, page_rotation);
+ float pre_page_length = PDFUtilities.calculatePageLength(pdfDataSource, page - 1, page_height - footer_line, page_rotation);
if (pre_page_length == Float.NEGATIVE_INFINITY)
{
// we do have an empty page or nothing in area above footerline
@@ -1275,103 +1267,101 @@ public abstract class PdfAS
return new PositioningInstruction(make_new_page, page, pos_x, pos_y);
}
- /**
- * Sets the width of the table according to the layout of the document and
- * calculates the y position where the PDFPTable should be placed.
- *
- * @deprecated
- * @param pdf
- * The PDF document.
- * @param pdf_table
- * The PDFPTable to be placed.
- * @return Returns the position where the PDFPTable should be placed.
- * @throws PDFDocumentException
- * F.e.
- */
- public static PositioningInstruction adjustTableAndCalculatePosition(
- final byte[] pdf, PdfPTable pdf_table) throws PDFDocumentException
- {
- boolean make_new_page = false;
-
- PdfReader reader = readInPdfDocument(pdf);
-
- int page = reader.getNumberOfPages();
- Rectangle psize = reader.getPageSizeWithRotation(page);
- float page_width = psize.width();
- float page_height = psize.height();
-
- final float width = page_width - SIGNATURE_BORDER;
- pdf_table.setTotalWidth(width);
- pdf_table.setLockedWidth(true);
-
- final float pos_x = SIGNATURE_BORDER / 2;
-
- final float table_height = pdf_table.getTotalHeight();
- final float page_length = PDFUtilities.calculateLastPageLength(pdf, page_height);
- float pos_y = page_height - page_length - SIGNATURE_MARGIN;
-
- if (pos_y <= table_height)
- {
- make_new_page = true;
- page++;
-
- pos_y = page_height - SIGNATURE_BORDER / 2;
- }
-
- return new PositioningInstruction(make_new_page, page, pos_x, pos_y);
- }
-
- /**
- * Sets the width of the table according to the layout of the document and
- * calculates the y position where the PDFPTable should be placed.
- *
- * <p>
- * This algorithm tries to position the table between the end of the text and
- * the footer line.
- * </p>
- *
- * @deprecated
- * @param pdf
- * The PDF document.
- * @param pdf_table
- * The PDFPTable to be placed.
- * @return Returns the position where the PDFPTable should be placed.
- * @throws PDFDocumentException
- * F.e.
- */
- public static PositioningInstruction adjustTableAndCalculatePositionRegardingFooter(
- final byte[] pdf, PdfPTable pdf_table, float footer_line) throws PDFDocumentException
- {
- boolean make_new_page = false;
-
- PdfReader reader = readInPdfDocument(pdf);
-
- int page = reader.getNumberOfPages();
- Rectangle psize = reader.getPageSizeWithRotation(page);
- float page_width = psize.width();
- float page_height = psize.height();
-
- final float width = page_width - SIGNATURE_BORDER;
- pdf_table.setTotalWidth(width);
- pdf_table.setLockedWidth(true);
-
- final float pos_x = SIGNATURE_BORDER / 2;
-
- final float table_height = pdf_table.getTotalHeight();
-
- final float page_length = PDFUtilities.calculateLastPageLength(pdf, page_height - footer_line);
- float pos_y = page_height - page_length - SIGNATURE_MARGIN;
-
- if (pos_y - footer_line <= table_height)
- {
- make_new_page = true;
- page++;
-
- pos_y = page_height - SIGNATURE_BORDER / 2;
- }
-
- return new PositioningInstruction(make_new_page, page, pos_x, pos_y);
- }
+// /**
+// * Sets the width of the table according to the layout of the document and
+// * calculates the y position where the PDFPTable should be placed.
+// *
+// * @deprecated
+// * @param pdf
+// * The PDF document.
+// * @param pdf_table
+// * The PDFPTable to be placed.
+// * @return Returns the position where the PDFPTable should be placed.
+// * @throws PDFDocumentException
+// * F.e.
+// */
+// public static PositioningInstruction adjustTableAndCalculatePosition(final byte[] pdf, PdfPTable pdf_table) throws PDFDocumentException
+// {
+// boolean make_new_page = false;
+//
+// PdfReader reader = readInPdfDocument(pdf);
+//
+// int page = reader.getNumberOfPages();
+// Rectangle psize = reader.getPageSizeWithRotation(page);
+// float page_width = psize.width();
+// float page_height = psize.height();
+//
+// final float width = page_width - SIGNATURE_BORDER;
+// pdf_table.setTotalWidth(width);
+// pdf_table.setLockedWidth(true);
+//
+// final float pos_x = SIGNATURE_BORDER / 2;
+//
+// final float table_height = pdf_table.getTotalHeight();
+// final float page_length = PDFUtilities.calculateLastPageLength(pdf, page_height);
+// float pos_y = page_height - page_length - SIGNATURE_MARGIN;
+//
+// if (pos_y <= table_height)
+// {
+// make_new_page = true;
+// page++;
+//
+// pos_y = page_height - SIGNATURE_BORDER / 2;
+// }
+//
+// return new PositioningInstruction(make_new_page, page, pos_x, pos_y);
+// }
+
+// /**
+// * Sets the width of the table according to the layout of the document and
+// * calculates the y position where the PDFPTable should be placed.
+// *
+// * <p>
+// * This algorithm tries to position the table between the end of the text and
+// * the footer line.
+// * </p>
+// *
+// * @deprecated
+// * @param pdf
+// * The PDF document.
+// * @param pdf_table
+// * The PDFPTable to be placed.
+// * @return Returns the position where the PDFPTable should be placed.
+// * @throws PDFDocumentException
+// * F.e.
+// */
+// public static PositioningInstruction adjustTableAndCalculatePositionRegardingFooter(final byte[] pdf, PdfPTable pdf_table, float footer_line) throws PDFDocumentException
+// {
+// boolean make_new_page = false;
+//
+// PdfReader reader = readInPdfDocument(pdf);
+//
+// int page = reader.getNumberOfPages();
+// Rectangle psize = reader.getPageSizeWithRotation(page);
+// float page_width = psize.width();
+// float page_height = psize.height();
+//
+// final float width = page_width - SIGNATURE_BORDER;
+// pdf_table.setTotalWidth(width);
+// pdf_table.setLockedWidth(true);
+//
+// final float pos_x = SIGNATURE_BORDER / 2;
+//
+// final float table_height = pdf_table.getTotalHeight();
+//
+// final float page_length = PDFUtilities.calculateLastPageLength(pdf, page_height - footer_line);
+// float pos_y = page_height - page_length - SIGNATURE_MARGIN;
+//
+// if (pos_y - footer_line <= table_height)
+// {
+// make_new_page = true;
+// page++;
+//
+// pos_y = page_height - SIGNATURE_BORDER / 2;
+// }
+//
+// return new PositioningInstruction(make_new_page, page, pos_x, pos_y);
+// }
/**
* Creates an iText Reader that parses the document.
@@ -1386,11 +1376,14 @@ public abstract class PdfAS
* @throws PDFDocumentException
* F.e.
*/
- public static PdfReader readInPdfDocument(final byte[] pdf) throws PDFDocumentException
+ public static PdfReader readInPdfDocument(final PdfDataSource pdfDataSource) throws PDFDocumentException
{
try
{
- return new PdfReader(pdf);
+ InputStream is = pdfDataSource.createInputStream();
+ PdfReader reader = new PdfReader(is);
+ is.close();
+ return reader;
}
catch (IOException e)
{