diff options
author | tkellner <tkellner@174cde9d-5d70-4d2a-aa98-46368bc2aaf7> | 2013-04-10 18:51:24 +0000 |
---|---|---|
committer | tkellner <tkellner@174cde9d-5d70-4d2a-aa98-46368bc2aaf7> | 2013-04-10 18:51:24 +0000 |
commit | 782e41eee1f04f86bc895a95cd2d51353284987a (patch) | |
tree | 746d56e559d27f6a54a1ad63901dc5135d7128d2 /pdf-over-signator/src | |
parent | 5793f0dc194ae3519de7a12808d99aa2a555cd73 (diff) | |
download | pdf-over-782e41eee1f04f86bc895a95cd2d51353284987a.tar.gz pdf-over-782e41eee1f04f86bc895a95cd2d51353284987a.tar.bz2 pdf-over-782e41eee1f04f86bc895a95cd2d51353284987a.zip |
Refactoring
git-svn-id: https://joinup.ec.europa.eu/svn/pdf-over/trunk@11 174cde9d-5d70-4d2a-aa98-46368bc2aaf7
Diffstat (limited to 'pdf-over-signator/src')
15 files changed, 657 insertions, 0 deletions
diff --git a/pdf-over-signator/src/main/java/at/asit/pdfover/signator/ByteArrayDocumentSource.java b/pdf-over-signator/src/main/java/at/asit/pdfover/signator/ByteArrayDocumentSource.java new file mode 100644 index 00000000..812ddc88 --- /dev/null +++ b/pdf-over-signator/src/main/java/at/asit/pdfover/signator/ByteArrayDocumentSource.java @@ -0,0 +1,39 @@ +package at.asit.pdfover.signator; + +import java.io.ByteArrayInputStream; +import java.io.InputStream; + +/** + * A DocumentSource using a byte[] to store the document content + */ +public class ByteArrayDocumentSource implements DocumentSource { + + /** + * Document content + */ + protected byte[] data; + + /** + * Constructor with byte[] content + * @param data the document content + */ + public ByteArrayDocumentSource(byte[] data) { + this.data = data; + } + + @Override + public InputStream getInputStream() { + return new ByteArrayInputStream(this.data); + } + + @Override + public int getLength() { + return this.data.length; + } + + @Override + public byte[] getByteArray() { + return this.data; + } + +} diff --git a/pdf-over-signator/src/main/java/at/asit/pdfover/signator/DocumentSource.java b/pdf-over-signator/src/main/java/at/asit/pdfover/signator/DocumentSource.java new file mode 100644 index 00000000..8b444020 --- /dev/null +++ b/pdf-over-signator/src/main/java/at/asit/pdfover/signator/DocumentSource.java @@ -0,0 +1,27 @@ +package at.asit.pdfover.signator; + +import java.io.InputStream; + +/** + * A Document Source + */ +public interface DocumentSource { + + /** + * Gets Document as Input Stream + * @return InputStream of the document + */ + public InputStream getInputStream(); + + /** + * Get Length of document + * @return length of the document + */ + public int getLength(); + + /** + * Get Document as byte[] + * @return byte[] of the Document + */ + public byte[] getByteArray(); +} diff --git a/pdf-over-signator/src/main/java/at/asit/pdfover/signator/Emblem.java b/pdf-over-signator/src/main/java/at/asit/pdfover/signator/Emblem.java new file mode 100644 index 00000000..e86b7100 --- /dev/null +++ b/pdf-over-signator/src/main/java/at/asit/pdfover/signator/Emblem.java @@ -0,0 +1,12 @@ +package at.asit.pdfover.signator; + +/** + * Provides a logo for a signature block + */ +public interface Emblem { + /** + * Return the file name of the Emblem + * @return the file name + */ + public String getFileName(); +} diff --git a/pdf-over-signator/src/main/java/at/asit/pdfover/signator/SLRequest.java b/pdf-over-signator/src/main/java/at/asit/pdfover/signator/SLRequest.java new file mode 100644 index 00000000..9b393e46 --- /dev/null +++ b/pdf-over-signator/src/main/java/at/asit/pdfover/signator/SLRequest.java @@ -0,0 +1,12 @@ +package at.asit.pdfover.signator; + +/** + * Security Layer Request + */ +public interface SLRequest { + /** + * Gets the signature data for this request + * @return The document source + */ + public DocumentSource getSignatureData(); +} diff --git a/pdf-over-signator/src/main/java/at/asit/pdfover/signator/SLResponse.java b/pdf-over-signator/src/main/java/at/asit/pdfover/signator/SLResponse.java new file mode 100644 index 00000000..88adc117 --- /dev/null +++ b/pdf-over-signator/src/main/java/at/asit/pdfover/signator/SLResponse.java @@ -0,0 +1,25 @@ +package at.asit.pdfover.signator; + +/** + * Security Layer response + */ +public class SLResponse { + + private String response; + + /** + * Create a new Security Layer response + * @param response the SLResponse + */ + public SLResponse(String response) { + this.response = response; + } + + /** + * Get the Security Layer response text + * @return the Security Layer response text + */ + public String getSLRespone() { + return this.response; + } +} diff --git a/pdf-over-signator/src/main/java/at/asit/pdfover/signator/SignResult.java b/pdf-over-signator/src/main/java/at/asit/pdfover/signator/SignResult.java new file mode 100644 index 00000000..7a8d4d0d --- /dev/null +++ b/pdf-over-signator/src/main/java/at/asit/pdfover/signator/SignResult.java @@ -0,0 +1,27 @@ +package at.asit.pdfover.signator; + +import java.security.cert.X509Certificate; + +/** + * Signature Result containing the signed document as document source + */ +public interface SignResult { + + /** + * Getter of the property <tt>signaturePosition</tt> + * @return Returns the signaturePosition. + */ + public SignaturePosition getSignaturePosition(); + + /** + * Gets the signed Document + * @return Returns the documentSource. + */ + public DocumentSource getSignedDocument(); + + /** + * Gets the signer certificate + * @return The signer x509 certificate + */ + public X509Certificate getSignerCertificate(); +} diff --git a/pdf-over-signator/src/main/java/at/asit/pdfover/signator/SignResultImpl.java b/pdf-over-signator/src/main/java/at/asit/pdfover/signator/SignResultImpl.java new file mode 100644 index 00000000..dc0986cf --- /dev/null +++ b/pdf-over-signator/src/main/java/at/asit/pdfover/signator/SignResultImpl.java @@ -0,0 +1,52 @@ +package at.asit.pdfover.signator; + +import java.security.cert.X509Certificate; + +/** + * The result of a signature operation + */ +public class SignResultImpl implements SignResult { + + private SignaturePosition position; + private DocumentSource source; + private X509Certificate certificate; + + @Override + public SignaturePosition getSignaturePosition() { + return this.position; + } + + @Override + public DocumentSource getSignedDocument() { + return this.source; + } + + @Override + public X509Certificate getSignerCertificate() { + return this.certificate; + } + + /** + * Set the signer certificate + * @param x509Certificate the signer certificate + */ + public void setSignerCertificate(X509Certificate x509Certificate) { + this.certificate = x509Certificate; + } + + /** + * Set the signature position + * @param postion the signature position + */ + public void setSignaturePosition(SignaturePosition postion) { + this.position = postion; + } + + /** + * Set the signed document + * @param source DocumentSource containing the signed document + */ + public void setSignedDocument(DocumentSource source) { + this.source = source; + } +} diff --git a/pdf-over-signator/src/main/java/at/asit/pdfover/signator/Signator.java b/pdf-over-signator/src/main/java/at/asit/pdfover/signator/Signator.java new file mode 100644 index 00000000..4ba0b981 --- /dev/null +++ b/pdf-over-signator/src/main/java/at/asit/pdfover/signator/Signator.java @@ -0,0 +1,58 @@ +package at.asit.pdfover.signator; + +import java.util.EnumMap; +import java.util.Map; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * PDF Signator Interface + */ +public class Signator { + + private static Logger log = LoggerFactory.getLogger(Signator.class); + + /** + * List of available PDF signing libraries + */ + public enum Signers { + /** + * PDF-AS + */ + PDFAS + }; + + private static Map<Signers, SignerFactory> factoryMap; + + static { + factoryMap = new EnumMap<Signers, SignerFactory>(Signers.class); + + try { + Class<?> pdfAsClass = Class.forName("at.asit.pdfover.signer.pdfas.PDFASSignerFactory"); + SignerFactory factory = (SignerFactory)pdfAsClass.newInstance(); + registerSigner(Signers.PDFAS, factory); + } catch (ClassNotFoundException e) { + log.error("PDF Signer Factory not found", e); + throw new RuntimeException("PDF Signer Factory not found", e); + } catch (InstantiationException e) { + log.error("PDF Signer Factory could not be instantiated", e); + throw new RuntimeException("PDF Signer Factory could not be instantiated", e); + } catch (IllegalAccessException e) { + log.error("PDF Signer Factory could not accessed", e); + throw new RuntimeException("PDF Signer Factory could not accessed", e); + } + } + + private static void registerSigner(Signers signer, SignerFactory factory) { + factoryMap.put(signer, factory); + } + + /** + * Gets a PDF Signer according to the chosen signer library + * @param signer the chosen Signer type + * @return the PDF Signer + */ + public static Signer getSigner(Signers signer) { + return factoryMap.get(signer).createSigner(); + } +} diff --git a/pdf-over-signator/src/main/java/at/asit/pdfover/signator/SignatureDimension.java b/pdf-over-signator/src/main/java/at/asit/pdfover/signator/SignatureDimension.java new file mode 100644 index 00000000..8d47cb19 --- /dev/null +++ b/pdf-over-signator/src/main/java/at/asit/pdfover/signator/SignatureDimension.java @@ -0,0 +1,69 @@ +package at.asit.pdfover.signator; + +/** + * The dimensions of the visible signature block + */ +public class SignatureDimension { + + /** + * The visible Signature block width + */ + protected int width; + + /** + * The visible Signature block height + */ + protected int height; + + /** + * Constructor + * @param width The width of the signature block + * @param height The height of the signature block + */ + public SignatureDimension(int width, int height) { + setDimension(width, height); + } + + /** + * Sets the the dimension of the signature block + * @param width The width + * @param height The height + */ + public void setDimension(int width, int height) + { + setWidth(width); + setHeight(height); + } + + /** + * Sets the width for the dimension + * @param width + */ + public void setWidth(int width) { + this.width = width; + } + + /** + * Gets the width of the visible Signature block + * @return the width + */ + public int getWidth() { + return this.width; + } + + /** + * Sets the height for the dimension + * @param height + */ + public void setHeight(int height) { + this.height = height; + } + + /** + * Gets the height of the visible Signature block + * @return the height + */ + public int getHeight() { + return this.height; + } +} diff --git a/pdf-over-signator/src/main/java/at/asit/pdfover/signator/SignatureException.java b/pdf-over-signator/src/main/java/at/asit/pdfover/signator/SignatureException.java new file mode 100644 index 00000000..d41ba24d --- /dev/null +++ b/pdf-over-signator/src/main/java/at/asit/pdfover/signator/SignatureException.java @@ -0,0 +1,45 @@ +package at.asit.pdfover.signator; + +/** + * Base class for signature exceptions + */ +public class SignatureException extends Exception { + + /** + * + */ + private static final long serialVersionUID = 711578398780816710L; + + + /** + * Empty constructor + */ + public SignatureException() { + super(); + } + + /** + * Constructor with causing exception + * @param cause the cause + */ + public SignatureException(Throwable cause) { + super(cause); + } + + /** + * Constructor with message + * @param msg the message + */ + public SignatureException(String msg) { + super(msg); + } + + /** + * Constructor with message and causing exception + * @param message the message + * @param cause the cause + */ + public SignatureException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/pdf-over-signator/src/main/java/at/asit/pdfover/signator/SignatureParameter.java b/pdf-over-signator/src/main/java/at/asit/pdfover/signator/SignatureParameter.java new file mode 100644 index 00000000..3fa9c7ce --- /dev/null +++ b/pdf-over-signator/src/main/java/at/asit/pdfover/signator/SignatureParameter.java @@ -0,0 +1,117 @@ +package at.asit.pdfover.signator; + +/** + * The Signature Parameter + */ +public abstract class SignatureParameter { + + /** + * The Signature Position + * @uml.property name="signaturePosition" + * @uml.associationEnd + */ + protected SignaturePosition signaturePosition = null; + + /** + * The signature Device + */ + protected String keyIdentifier = null; + + /** + * The input document + * @uml.property name="documentSource" + * @uml.associationEnd + */ + protected DocumentSource documentSource = null; + + /** + * holds the collimating mark + * @uml.property name="collimark" + * @uml.associationEnd + */ + protected Emblem emblem; + + /** + * Getter of the property <tt>signaturePosition</tt> + * @return Returns the signaturePosition. + */ + public SignaturePosition getSignaturePosition() { + return this.signaturePosition; + } + + /** + * Setter of the property <tt>signaturePosition</tt> + * @param signaturePosition The signaturePosition to set. + */ + public void setSignaturePosition(SignaturePosition signaturePosition) { + this.signaturePosition = signaturePosition; + } + + /** + * Getter of the property <tt>keyIdentifier</tt> + * @return Returns the keyIdentifier. + */ + public String getKeyIdentifier() { + return this.keyIdentifier; + } + + /** + * Setter of the property <tt>keyIdentifier</tt> + * @param keyIdentifier The keyIdentifier to set. + */ + public void setKeyIdentifier(String keyIdentifier) { + this.keyIdentifier = keyIdentifier; + } + + /** + * Getter of the property <tt>documentSource</tt> + * @return Returns the documentSource. + */ + public DocumentSource getInputDocument() { + return this.documentSource; + } + + /** + * Setter of the property <tt>documentSource</tt> + * @param inputDocument The documentSource to set. + */ + public void setInputDocument(DocumentSource inputDocument) { + this.documentSource = inputDocument; + } + + /** + * Gets the Dimension to display the Placeholder + * @return the placeholder dimensions + */ + public abstract SignatureDimension getPlaceholderDimension(); + + /** + * Gets the Emblem + * @return the Emblem + */ + public Emblem getEmblem() { + return this.emblem; + } + + /** + * Sets the Emblem + * @param emblem The new Emblem + */ + public void setEmblem(Emblem emblem) { + this.emblem = emblem; + } + + /** + * Sets generic properties + * @param key + * @param value + */ + public abstract void setProperty(String key, String value); + + /** + * Gets generic properties + * @param key + * @return associated value + */ + public abstract String getProperty(String key); +} diff --git a/pdf-over-signator/src/main/java/at/asit/pdfover/signator/SignaturePosition.java b/pdf-over-signator/src/main/java/at/asit/pdfover/signator/SignaturePosition.java new file mode 100644 index 00000000..59fa4864 --- /dev/null +++ b/pdf-over-signator/src/main/java/at/asit/pdfover/signator/SignaturePosition.java @@ -0,0 +1,107 @@ +package at.asit.pdfover.signator; + + +/** + * Represents the position of a visible signature block + */ +public class SignaturePosition { + /** + * The x value of the position + */ + protected float x = 0; + + /** + * The y value of the position + */ + protected float y = 0; + + /** + * The page value of the position + */ + protected int page = 0; + + /** + * Whether automatic positioning is used + */ + protected boolean autoPositioning; + + /** + * Default constructor + * No position given, hence automatic positioning will be used + */ + public SignaturePosition() { + this.autoPositioning = true; + } + + /** + * X - Y Constructor (Page = 0) + * @param x The x value of the position + * @param y The y value of the position + */ + public SignaturePosition(float x, float y) { + this.autoPositioning = false; + setPosition(x, y); + } + + /** + * Constructor + * @param x The x value of the signature position + * @param y The y value of the signature position + * @param page The page value of the signature position + */ + public SignaturePosition(float x, float y, int page) { + this.autoPositioning = false; + setPosition(x, y); + setPage(page); + } + + /** + * Sets new position + * @param x the new x value + * @param y the new y value + */ + public void setPosition(float x, float y) { + this.x = x; + this.y = y; + } + + /** + * Gets the X value of the position + * @return float the x value of the position + */ + public float getX() { + return this.x; + } + + /** + * Gets the Y value of the position + * @return float the y value of the position + */ + public float getY() { + return this.y; + } + + /** + * Sets Page value of position + * @param page the new page value + */ + public void setPage(int page) { + this.page = page; + } + + /** + * Gets the Page value of the position + * @return int the page value of the position + */ + public int getPage() { + return this.page; + } + + /** + * Gets whether automatic positioning is used + * @return true if the signature position is determined automatically + */ + public boolean useAutoPositioning() { + return this.autoPositioning; + } +} diff --git a/pdf-over-signator/src/main/java/at/asit/pdfover/signator/Signer.java b/pdf-over-signator/src/main/java/at/asit/pdfover/signator/Signer.java new file mode 100644 index 00000000..fd7bee09 --- /dev/null +++ b/pdf-over-signator/src/main/java/at/asit/pdfover/signator/Signer.java @@ -0,0 +1,33 @@ +package at.asit.pdfover.signator; + +/** + * PDF Signer base Class + * This class should be extended to support libraries such as PDF-AS or PADES. + */ +public interface Signer { + + /** + * Prepare a signature + * Defines signature parameters, the pdf library prepares the pdf document to sign and + * creates a Security Layer Request. + * @param parameter The signature parameters + * @return The signing state (contains the prepared document and the signature request + * @throws SignatureException + */ + public SigningState prepare(SignatureParameter parameter) throws SignatureException; + + /** + * Adds the signature to the document. + * The SL Response has to be set in the state + * @param state The signing state + * @return The signature Result + * @throws SignatureException + */ + public SignResult sign(SigningState state) throws SignatureException; + + /** + * Creates new signing profile + * @return The new Profile + */ + public SignatureParameter newParameter(); +} diff --git a/pdf-over-signator/src/main/java/at/asit/pdfover/signator/SignerFactory.java b/pdf-over-signator/src/main/java/at/asit/pdfover/signator/SignerFactory.java new file mode 100644 index 00000000..6c0b6487 --- /dev/null +++ b/pdf-over-signator/src/main/java/at/asit/pdfover/signator/SignerFactory.java @@ -0,0 +1,15 @@ +package at.asit.pdfover.signator; + +import at.asit.pdfover.signator.Signer; + +/** + * A Signer factory + * Creates Signer instances + */ +public abstract class SignerFactory { + /** + * Create a Signer + * @return the new Signer + */ + public abstract Signer createSigner(); +} diff --git a/pdf-over-signator/src/main/java/at/asit/pdfover/signator/SigningState.java b/pdf-over-signator/src/main/java/at/asit/pdfover/signator/SigningState.java new file mode 100644 index 00000000..c1e82da3 --- /dev/null +++ b/pdf-over-signator/src/main/java/at/asit/pdfover/signator/SigningState.java @@ -0,0 +1,19 @@ +package at.asit.pdfover.signator; + +/** + * The state of the pdf signing library + */ +public interface SigningState { + + /** + * Gets the Security Layer Request to create the signature + * @return The Signature Request + */ + public abstract SLRequest getSignatureRequest(); + + /** + * Sets the Security Layer Response to the Signature Request + * @param value The Signature Response + */ + public abstract void setSignatureResponse(SLResponse value); +} |