diff options
author | Tobias Kellner <tobias.kellner@iaik.tugraz.at> | 2014-11-27 20:48:16 +0100 |
---|---|---|
committer | Tobias Kellner <tobias.kellner@iaik.tugraz.at> | 2014-11-27 20:48:16 +0100 |
commit | 896fbe4a5643bf8c29968f8514a1c14fd51c4d9e (patch) | |
tree | 0f3e3bec7093e9be39c94199b50db1a61154c54d /pdf-over-signer/pdf-over-sigpdfas3/src/main/java | |
parent | 5ef48a2002856bea0cac10e1dac6fb2985dca893 (diff) | |
download | pdf-over-896fbe4a5643bf8c29968f8514a1c14fd51c4d9e.tar.gz pdf-over-896fbe4a5643bf8c29968f8514a1c14fd51c4d9e.tar.bz2 pdf-over-896fbe4a5643bf8c29968f8514a1c14fd51c4d9e.zip |
Add PDF-AS4 signer
Diffstat (limited to 'pdf-over-signer/pdf-over-sigpdfas3/src/main/java')
8 files changed, 1253 insertions, 0 deletions
diff --git a/pdf-over-signer/pdf-over-sigpdfas3/src/main/java/at/asit/pdfover/signer/pdfas/ByteArrayPDFASDataSource.java b/pdf-over-signer/pdf-over-sigpdfas3/src/main/java/at/asit/pdfover/signer/pdfas/ByteArrayPDFASDataSource.java new file mode 100644 index 00000000..b2f95f50 --- /dev/null +++ b/pdf-over-signer/pdf-over-sigpdfas3/src/main/java/at/asit/pdfover/signer/pdfas/ByteArrayPDFASDataSource.java @@ -0,0 +1,67 @@ +/* + * Copyright 2012 by A-SIT, Secure Information Technology Center Austria + * + * Licensed under the EUPL, Version 1.1 or - as soon they will be approved by + * the European Commission - subsequent versions of the EUPL (the "Licence"); + * You may not use this work except in compliance with the Licence. + * You may obtain a copy of the Licence at: + * http://joinup.ec.europa.eu/software/page/eupl + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the Licence is distributed on an "AS IS" basis, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the Licence for the specific language governing permissions and + * limitations under the Licence. + */ +package at.asit.pdfover.signer.pdfas; + +//Imports +import java.io.ByteArrayInputStream; +import java.io.InputStream; + +import at.gv.egiz.pdfas.api.io.DataSource; + +/** + * PDF - AS Specific Data Source with byte array representation + */ +public class ByteArrayPDFASDataSource implements DataSource { + + /** + * Internal data byte array + */ + private byte[] data; + + /** + * Default constructor + * @param data The byte[] to be used + */ + public ByteArrayPDFASDataSource(byte[] data) { + this.data = data; + } + + @Override + public InputStream createInputStream() { + return new ByteArrayInputStream(this.data); + } + + @Override + public int getLength() { + return this.data.length; + } + + @Override + public byte[] getAsByteArray() { + return this.data; + } + + @Override + public String getMimeType() { + return "application/pdf"; + } + + @Override + public String getCharacterEncoding() { + return "UTF8"; + } + +}
\ No newline at end of file diff --git a/pdf-over-signer/pdf-over-sigpdfas3/src/main/java/at/asit/pdfover/signer/pdfas/PDFASHelper.java b/pdf-over-signer/pdf-over-sigpdfas3/src/main/java/at/asit/pdfover/signer/pdfas/PDFASHelper.java new file mode 100644 index 00000000..473bbf42 --- /dev/null +++ b/pdf-over-signer/pdf-over-sigpdfas3/src/main/java/at/asit/pdfover/signer/pdfas/PDFASHelper.java @@ -0,0 +1,102 @@ +/* + * Copyright 2012 by A-SIT, Secure Information Technology Center Austria + * + * Licensed under the EUPL, Version 1.1 or - as soon they will be approved by + * the European Commission - subsequent versions of the EUPL (the "Licence"); + * You may not use this work except in compliance with the Licence. + * You may obtain a copy of the Licence at: + * http://joinup.ec.europa.eu/software/page/eupl + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the Licence is distributed on an "AS IS" basis, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the Licence for the specific language governing permissions and + * limitations under the Licence. + */ +package at.asit.pdfover.signer.pdfas; + +//Imports +import java.io.File; + +import at.asit.pdfover.signator.SignatureException; +import at.gv.egiz.pdfas.api.PdfAs; +import at.gv.egiz.pdfas.api.exceptions.PdfAsException; +import at.gv.egiz.pdfas.api.internal.PdfAsInternal; + +/** + * Encapsulates PDF AS API Object to need just one initialization + */ +public class PDFASHelper { + + /** + * PDF AS Object + */ + private static PdfAs pdfAs = null; + + /** + * Internal Pdf AS Object + */ + private static PdfAsInternal pdfAsInternal = null; + + /** + * Creates PDF AS Object + * @return + * @throws PdfAsException + */ + private static PdfAs createPdfAs() throws PdfAsException { + File directory = new File ("."); + System.setProperty("log4j.configuration", directory.getAbsolutePath() + + "/log4j.properties"); + System.setProperty("pdf-as.work-dir", getWorkDir()); + return new at.gv.egiz.pdfas.impl.api.PdfAsObject(); + } + + /** + * Provides the working directory + * @return the working directory + */ + public static String getWorkDir() { + return System.getProperty("user.home") + "/.pdf-over"; + } + + /** + * Creates a PDF-AS Internal object + * @return the PDF-AS Internal object + * @throws PdfAsException + */ + private static PdfAsInternal createPdfAsInternal() throws PdfAsException { + return new at.gv.egiz.pdfas.impl.api.internal.PdfAsInternalObject(); + } + + /** + * Gets PDF-AS Object + * @return the PDF-AS Object + * @throws SignatureException + */ + public static synchronized PdfAs getPdfAs() throws SignatureException { + if (pdfAs == null) { + try { + pdfAs = createPdfAs(); + } catch(PdfAsException e) { + throw new SignatureException(e); + } + } + return pdfAs; + } + + /** + * Gets PDF-AS Internal object + * @return the PDF-AS Internal object + * @throws SignatureException + */ + public static synchronized PdfAsInternal getPdfAsInternal() throws SignatureException { + if(pdfAsInternal == null) { + try { + pdfAsInternal = createPdfAsInternal(); + } catch(PdfAsException e) { + throw new SignatureException(e); + } + } + return pdfAsInternal; + } +} diff --git a/pdf-over-signer/pdf-over-sigpdfas3/src/main/java/at/asit/pdfover/signer/pdfas/PDFASSLRequest.java b/pdf-over-signer/pdf-over-sigpdfas3/src/main/java/at/asit/pdfover/signer/pdfas/PDFASSLRequest.java new file mode 100644 index 00000000..f274068a --- /dev/null +++ b/pdf-over-signer/pdf-over-sigpdfas3/src/main/java/at/asit/pdfover/signer/pdfas/PDFASSLRequest.java @@ -0,0 +1,54 @@ +/* + * Copyright 2012 by A-SIT, Secure Information Technology Center Austria + * + * Licensed under the EUPL, Version 1.1 or - as soon they will be approved by + * the European Commission - subsequent versions of the EUPL (the "Licence"); + * You may not use this work except in compliance with the Licence. + * You may obtain a copy of the Licence at: + * http://joinup.ec.europa.eu/software/page/eupl + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the Licence is distributed on an "AS IS" basis, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the Licence for the specific language governing permissions and + * limitations under the Licence. + */ +package at.asit.pdfover.signer.pdfas; + +// Imports +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import at.asit.pdfover.signator.ByteArrayDocumentSource; +import at.asit.pdfover.signator.SLRequest; +import at.asit.pdfover.signer.pdfas.exceptions.PDFASSLRequestException; + +/** + * PDF - AS Security Layer Request implementation + */ +public class PDFASSLRequest extends SLRequest { + + /** + * SLF4J Logger instance + **/ + private static final Logger log = LoggerFactory.getLogger(PDFASSLRequest.class); + + /** + * Default constructor + * @param slRequest + * @param signData + * @throws PDFASSLRequestException + */ + public PDFASSLRequest(String slRequest, byte[] signData) throws PDFASSLRequestException { + if(!slRequest.contains(PDFASSigner.LOC_REF)) { + log.error("PDF-AS SL request doesn't contain " + PDFASSigner.LOC_REF); + log.debug("Request: " + slRequest); + throw new PDFASSLRequestException("PDF-AS SL request doesn't contain " + PDFASSigner.LOC_REF); + } + + // Modifing SL Request ... + setRequest(slRequest.replace(PDFASSigner.LOC_REF, SLRequest.DATAOBJECT_STRING)); + + setSignatureData(new ByteArrayDocumentSource(signData)); + } +} diff --git a/pdf-over-signer/pdf-over-sigpdfas3/src/main/java/at/asit/pdfover/signer/pdfas/PDFASSigner.java b/pdf-over-signer/pdf-over-sigpdfas3/src/main/java/at/asit/pdfover/signer/pdfas/PDFASSigner.java new file mode 100644 index 00000000..6fa1c094 --- /dev/null +++ b/pdf-over-signer/pdf-over-sigpdfas3/src/main/java/at/asit/pdfover/signer/pdfas/PDFASSigner.java @@ -0,0 +1,190 @@ +package at.asit.pdfover.signer.pdfas; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import at.asit.pdfover.signator.BKUs; +import at.asit.pdfover.signator.ByteArrayDocumentSource; +import at.asit.pdfover.signator.SLResponse; +import at.asit.pdfover.signator.SignResult; +import at.asit.pdfover.signator.SignResultImpl; +import at.asit.pdfover.signator.SignatureException; +import at.asit.pdfover.signator.SignatureParameter; +import at.asit.pdfover.signator.SignaturePosition; +import at.asit.pdfover.signator.Signer; +import at.asit.pdfover.signator.SigningState; +import at.asit.pdfover.signer.pdfas.exceptions.PDFASSLRequestException; +import at.gv.egiz.pdfas.api.PdfAs; +import at.gv.egiz.pdfas.api.commons.Constants; +import at.gv.egiz.pdfas.api.exceptions.PdfAsException; +import at.gv.egiz.pdfas.api.internal.LocalBKUParams; +import at.gv.egiz.pdfas.api.internal.PdfAsInternal; +import at.gv.egiz.pdfas.api.sign.SignParameters; +import at.gv.egiz.pdfas.api.sign.SignatureDetailInformation; +import at.gv.egiz.pdfas.io.ByteArrayDataSink; + +/** + * PDF AS Signer Implementation + */ +public class PDFASSigner implements Signer { + + /** + * SLF4J Logger instance + **/ + static final Logger log = LoggerFactory.getLogger(PDFASSigner.class); + + /** + * The template URL + */ + protected static final String URL_TEMPLATE = "http://pdfover.4.gv.at/template"; + + /** + * Location reference string + */ + protected static final String LOC_REF = "<sl:LocRefContent>" + URL_TEMPLATE + + "</sl:LocRefContent>"; + + @Override + public SigningState prepare(SignatureParameter parameter) + throws SignatureException { + try { + PdfAsSignatureParameter sign_para = null; + + if (PdfAsSignatureParameter.class.isInstance(parameter)) { + sign_para = PdfAsSignatureParameter.class.cast(parameter); + } + + if (sign_para == null) { + throw new SignatureException("Incorrect SignatureParameter!"); + } + + PdfAs pdfas = PDFASHelper.getPdfAs(); + + PDFASSigningState state = new PDFASSigningState(); + + SignParameters params = new SignParameters(); + params.setSignaturePositioning(sign_para.getPDFASPositioning()); + + if (parameter.getSignatureDevice() == BKUs.LOCAL) { + params.setSignatureDevice(Constants.SIGNATURE_DEVICE_BKU); + } else if (parameter.getSignatureDevice() == BKUs.MOBILE) { + params.setSignatureDevice(Constants.SIGNATURE_DEVICE_MOBILE); + // params.setSignatureDevice(Constants.SIGNATURE_DEVICE_MOBILETEST); + } + params.setSignatureType(Constants.SIGNATURE_TYPE_BINARY); + + params.setSignatureProfileId(sign_para.getSignatureProfileID()); + + params.setOutput(new ByteArrayDataSink()); + if (parameter.getEmblem() != null) { + params.setProfileOverrideValue("SIG_LABEL", parameter + .getEmblem().getFileName()); + } + + if(parameter.getProperty("SIG_NOTE") != null) { + params.setProfileOverrideValue("SIG_NOTE", parameter.getProperty("SIG_NOTE")); + } + + // Prepare Output sink + state.setOutput(new ByteArrayDataSink()); + params.setOutput(state.getOutput()); + + params.setDocument(sign_para.getPDFASDataSource()); + + state.setSignParameters(params); + + PdfAsInternal pdfasInternal = PDFASHelper.getPdfAsInternal(); + + // Prepares the document + SignatureDetailInformation sdi = pdfas.prepareSign(params); + + state.setSignatureDetailInformation(sdi); + + // Retrieve the SL Request + String slRequest = pdfasInternal.prepareLocalSignRequest(params, + false, URL_TEMPLATE, sdi); + + at.gv.egiz.pdfas.api.io.DataSource sig_data = sdi + .getSignatureData(); + + PDFASSLRequest request = new PDFASSLRequest(slRequest, + sig_data.getAsByteArray()); + + state.setSignatureRequest(request); + + return state; + } catch (PDFASSLRequestException e) { + throw new SignatureException(e); + } catch (PdfAsException e) { + throw new SignatureException(e); + } catch (Throwable t) { + throw new SignatureException(t); + } + } + + @Override + public SignResult sign(SigningState state) throws SignatureException { + try { + PDFASSigningState sstate = null; + + if (PDFASSigningState.class.isInstance(state)) { + sstate = PDFASSigningState.class.cast(state); + } + + if (sstate == null) { + throw new SignatureException("Incorrect SigningState!"); + } + + // Retrieve objects + PdfAs pdfas = PDFASHelper.getPdfAs(); + + PdfAsInternal pdfasInternal = PDFASHelper.getPdfAsInternal(); + + SignParameters params = sstate.getSignParameters(); + + SignatureDetailInformation sdi = sstate + .getSignatureDetailInformation(); + + SLResponse slResponse = sstate.getSignatureResponse(); + + LocalBKUParams bkuParams = new LocalBKUParams( + slResponse.getServer(), slResponse.getUserAgent(), + slResponse.getSignaturLayout()); + + // Perform signature + at.gv.egiz.pdfas.api.sign.SignResult signResult = pdfasInternal + .finishLocalSign(pdfas, params, sdi, bkuParams, false, + slResponse.getSLRespone()); + + // Preparing Result Response + SignResultImpl result = new SignResultImpl(); + + // Set Signer Certificate + result.setSignerCertificate(signResult.getSignerCertificate()); + at.gv.egiz.pdfas.api.sign.pos.SignaturePosition pdfasPos = signResult + .getSignaturePosition(); + + // Set Signature position + SignaturePosition pos = new SignaturePosition(pdfasPos.getX(), + pdfasPos.getY(), pdfasPos.getPage()); + /* + * pos.SetAuto(sstate.getPDFAsSignatureParameter() + * .getSignaturePosition().useAutoPositioning()); + */ + result.setSignaturePosition(pos); + + // Set signed Document + result.setSignedDocument(new ByteArrayDocumentSource( + ((ByteArrayDataSink) sstate.getOutput()).getData())); + + return result; + } catch (PdfAsException e) { + throw new SignatureException(e); + } + } + + @Override + public SignatureParameter newParameter() { + return new PdfAsSignatureParameter(); + } +} diff --git a/pdf-over-signer/pdf-over-sigpdfas3/src/main/java/at/asit/pdfover/signer/pdfas/PDFASSignerFactory.java b/pdf-over-signer/pdf-over-sigpdfas3/src/main/java/at/asit/pdfover/signer/pdfas/PDFASSignerFactory.java new file mode 100644 index 00000000..68a29341 --- /dev/null +++ b/pdf-over-signer/pdf-over-sigpdfas3/src/main/java/at/asit/pdfover/signer/pdfas/PDFASSignerFactory.java @@ -0,0 +1,31 @@ +/* + * Copyright 2012 by A-SIT, Secure Information Technology Center Austria + * + * Licensed under the EUPL, Version 1.1 or - as soon they will be approved by + * the European Commission - subsequent versions of the EUPL (the "Licence"); + * You may not use this work except in compliance with the Licence. + * You may obtain a copy of the Licence at: + * http://joinup.ec.europa.eu/software/page/eupl + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the Licence is distributed on an "AS IS" basis, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the Licence for the specific language governing permissions and + * limitations under the Licence. + */ +package at.asit.pdfover.signer.pdfas; + +//Imports +import at.asit.pdfover.signator.Signer; +import at.asit.pdfover.signator.SignerFactory; + +/** + * Factory class for PDF-AS Signer + */ +public class PDFASSignerFactory extends SignerFactory { + + @Override + public Signer createSigner() { + return new PDFASSigner(); + } +} diff --git a/pdf-over-signer/pdf-over-sigpdfas3/src/main/java/at/asit/pdfover/signer/pdfas/PDFASSigningState.java b/pdf-over-signer/pdf-over-sigpdfas3/src/main/java/at/asit/pdfover/signer/pdfas/PDFASSigningState.java new file mode 100644 index 00000000..247b1afc --- /dev/null +++ b/pdf-over-signer/pdf-over-sigpdfas3/src/main/java/at/asit/pdfover/signer/pdfas/PDFASSigningState.java @@ -0,0 +1,173 @@ +/* + * Copyright 2012 by A-SIT, Secure Information Technology Center Austria + * + * Licensed under the EUPL, Version 1.1 or - as soon they will be approved by + * the European Commission - subsequent versions of the EUPL (the "Licence"); + * You may not use this work except in compliance with the Licence. + * You may obtain a copy of the Licence at: + * http://joinup.ec.europa.eu/software/page/eupl + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the Licence is distributed on an "AS IS" basis, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the Licence for the specific language governing permissions and + * limitations under the Licence. + */ +package at.asit.pdfover.signer.pdfas; + +//Imports +import at.asit.pdfover.signator.BkuSlConnector; +import at.asit.pdfover.signator.SLRequest; +import at.asit.pdfover.signator.SLResponse; +import at.asit.pdfover.signator.SigningState; +import at.gv.egiz.pdfas.api.io.DataSink; +import at.gv.egiz.pdfas.api.sign.SignatureDetailInformation; + +/** + * Signing State for PDFAS Wrapper + */ +public class PDFASSigningState implements SigningState { + + /** + * The Signature Layer request + */ + protected SLRequest slrequest; + + /** + * The PDF AS DataSink + */ + protected DataSink output; + + /** + * Gets the DataSink + * @return the datasink + */ + public DataSink getOutput() { + return this.output; + } + + /** + * Sets the datasing + * @param output the pdf as datasink + */ + public void setOutput(DataSink output) { + this.output = output; + } + + /** + * The Signature Layer response + */ + protected SLResponse slresponse; + + @Override + public SLRequest getSignatureRequest() { + return this.slrequest; + } + + @Override + public void setSignatureResponse(SLResponse response) { + this.slresponse = response; + } + + /** + * Sets the SL Request + * @param request The SL Request + */ + public void setSignatureRequest(SLRequest request) { + this.slrequest = request; + } + + /** + * Gets the SL Response + * @return The SL Repsonse object + */ + public SLResponse getSignatureResponse() { + return this.slresponse; + } + + /* (non-Javadoc) + * @see at.asit.pdfover.signator.SigningState#hasSignatureResponse() + */ + @Override + public boolean hasSignatureResponse() { + return this.getSignatureResponse() != null; + } + + // ---------------------------------------- + // PDF AS Specific stuff + // ---------------------------------------- + + /** + * signature detail information + */ + protected SignatureDetailInformation signatureDetailInformation; + + /** + * PDF - AS sign parameters + */ + protected at.gv.egiz.pdfas.api.sign.SignParameters signParameters; + + /** + * Signature parameters + */ + protected PdfAsSignatureParameter pdfAsSignatureParameter; + + /** + * Gets PDF - AS Signature Parameters + * @return PdfAsSignatureParameter + */ + public PdfAsSignatureParameter getPdfAsSignatureParameter() { + return this.pdfAsSignatureParameter; + } + + /** + * Sets PDF - AS Signature Parameters + * @param pdfAsSignatureParameter + */ + public void setPdfAsSignatureParameter( + PdfAsSignatureParameter pdfAsSignatureParameter) { + this.pdfAsSignatureParameter = pdfAsSignatureParameter; + } + + /** + * Get Sign Parameters + * @return SignParameters + */ + public at.gv.egiz.pdfas.api.sign.SignParameters getSignParameters() { + return this.signParameters; + } + + /** + * Sets sign Parameter + * @param signParameters + */ + public void setSignParameters( + at.gv.egiz.pdfas.api.sign.SignParameters signParameters) { + this.signParameters = signParameters; + } + + /** + * Gets the signature detail information + * @return SignatureDetailInformation + */ + public SignatureDetailInformation getSignatureDetailInformation() { + return this.signatureDetailInformation; + } + + /** + * Sets the SignatureDetailInformation + * @param signatureDetailInformation + */ + public void setSignatureDetailInformation( + SignatureDetailInformation signatureDetailInformation) { + this.signatureDetailInformation = signatureDetailInformation; + } + + /* (non-Javadoc) + * @see at.asit.pdfover.signator.SigningState#setBKUConnector(at.asit.pdfover.signator.BkuSlConnector) + */ + @Override + public void setBKUConnector(BkuSlConnector connector) { + // Nothing to do here + } +} diff --git a/pdf-over-signer/pdf-over-sigpdfas3/src/main/java/at/asit/pdfover/signer/pdfas/PdfAsSignatureParameter.java b/pdf-over-signer/pdf-over-sigpdfas3/src/main/java/at/asit/pdfover/signer/pdfas/PdfAsSignatureParameter.java new file mode 100644 index 00000000..1ceb6493 --- /dev/null +++ b/pdf-over-signer/pdf-over-sigpdfas3/src/main/java/at/asit/pdfover/signer/pdfas/PdfAsSignatureParameter.java @@ -0,0 +1,602 @@ +/* + * Copyright 2012 by A-SIT, Secure Information Technology Center Austria + * + * Licensed under the EUPL, Version 1.1 or - as soon they will be approved by + * the European Commission - subsequent versions of the EUPL (the "Licence"); + * You may not use this work except in compliance with the Licence. + * You may obtain a copy of the Licence at: + * http://joinup.ec.europa.eu/software/page/eupl + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the Licence is distributed on an "AS IS" basis, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the Licence for the specific language governing permissions and + * limitations under the Licence. + */ +package at.asit.pdfover.signer.pdfas; + +//Imports +import java.awt.Image; +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.IOException; +import java.util.HashMap; +import java.util.Locale; + +import javax.imageio.ImageIO; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import at.asit.pdfover.signator.SignatureDimension; +import at.asit.pdfover.signator.SignatureParameter; +import at.asit.pdfover.signator.SignaturePosition; +import at.gv.egiz.pdfas.api.io.DataSource; +import at.gv.egiz.pdfas.api.sign.pos.SignaturePositioning; +import at.knowcenter.wag.egov.egiz.exceptions.PDFDocumentException; + +/** + * Implementation of SignatureParameter specific for PDF - AS Library + */ +public class PdfAsSignatureParameter extends SignatureParameter { + + /** + * SLF4J Logger instance + **/ + static final Logger log = LoggerFactory + .getLogger(PdfAsSignatureParameter.class); + + /** The profile ID for the german signature block */ + private static final String PROFILE_ID_DE = "SIGNATURBLOCK_SMALL_DE"; + /** The profile ID for the german signature block if a signature note is set */ + private static final String PROFILE_ID_DE_NOTE = "SIGNATURBLOCK_SMALL_DE_NOTE"; + /** The profile ID for the english signature block */ + private static final String PROFILE_ID_EN = "SIGNATURBLOCK_SMALL_EN"; + /** The profile ID for the english signature block if a signature note is set */ + private static final String PROFILE_ID_EN_NOTE = "SIGNATURBLOCK_SMALL_EN_NOTE"; + + private HashMap<String, String> genericProperties = new HashMap<String, String>(); + +// private static final int PLACEHOLDER_SCALE = 4; +// +// private int height = -1; +// +// private float perUnitHeight = 0; + + /** + * Gets the PDFAS Positioning + * + * @return SignaturePositioning + * @throws PDFDocumentException + */ + public SignaturePositioning getPDFASPositioning() + throws PDFDocumentException { + SignaturePosition position = this.getSignaturePosition(); + + SignaturePositioning positioning = null; + if (!position.useAutoPositioning()) { + if (position.getPage() < 1) { + positioning = new SignaturePositioning(String.format( + (Locale) null, + "p:new;x:%f;y:%f;w:276", position.getX(), + position.getY())); + } else { + positioning = new SignaturePositioning(String.format( + (Locale) null, + "p:%d;x:%f;y:%f;w:276", position.getPage(), position.getX(), + position.getY())); + } + } else { + positioning = new SignaturePositioning("p:auto;x:auto;y:auto;w:276"); + } + + return positioning; + } + + /** + * Gets PDF - AS specific data source + * + * @return ByteArrayPDFASDataSource + */ + public DataSource getPDFASDataSource() { + return new ByteArrayPDFASDataSource(this.getInputDocument() + .getByteArray()); + } + + @Override + public void setProperty(String key, String value) { + this.genericProperties.put(key, value); +// this.height = -1; + } + + @Override + public String getProperty(String key) { + return this.genericProperties.get(key); + } + + /* (non-Javadoc) + * @see at.asit.pdfover.signator.SignatureParameter#setSignatureLanguage(java.lang.String) + */ + @Override + public void setSignatureLanguage(String signatureLanguage) { + super.setSignatureLanguage(signatureLanguage); +// this.height = -1; + } + + @Override + public SignatureDimension getPlaceholderDimension() { + // return new SignatureDimension(487, 206); + return new SignatureDimension(276, 126); + //return new SignatureDimension(getWidth(), getHeight()); + } + +// private static int getWidth() { +// return 276; +// } +// +// private int getHeight() { +// if (this.height < 0) +// { +// BufferedImage timage = new BufferedImage(1, 1, +// BufferedImage.TYPE_INT_RGB); +// try { +// this.height = (int) (getTableHeight(getSignatureTable(), null, getWidth() * PLACEHOLDER_SCALE, timage.getGraphics()) / PLACEHOLDER_SCALE); +// } catch (SignatureException e) { +// log.error("getTableHeight failed ...", e); +// } catch (SignatureTypesException e) { +// log.error("getTableHeight failed ...", e); +// } +// timage.flush(); +// } +// if (this.height < 0) +// return 95; +// +// return this.height; +// } + + /** + * Get the Signature Profile ID for this set of parameters + * @return the Signature Profile ID + */ + public String getSignatureProfileID() { + String lang = getSignatureLanguage(); + boolean useNote = (getProperty("SIG_NOTE") != null); + + if (lang != null && lang.equals("en")) + return useNote ? PROFILE_ID_EN_NOTE : PROFILE_ID_EN; + + return useNote ? PROFILE_ID_DE_NOTE : PROFILE_ID_DE; + } + + /* + * (non-Javadoc) + * + * @see at.asit.pdfover.signator.SignatureParameter#getPlaceholder() + */ + @Override + public Image getPlaceholder() { + + try { + Image logo = null; + try { + if (this.getEmblem() != null + && this.getEmblem().getFileName() != null + && new File(this.getEmblem().getFileName()) + .exists()) { + logo = ImageIO.read(new File(this.getEmblem() + .getFileName())); + + } + } + catch(Exception e) { + log.error("Failed to get emblem ...", e); + } + + Image img = null; + String lang = getSignatureLanguage(); + if (lang != null && lang.equals("en")) { + img = ImageIO.read(PdfAsSignatureParameter.class + .getResourceAsStream("/img/sign_prev_en.png")); + + if(logo != null) { + logo = logo.getScaledInstance(141, 140, + Image.SCALE_SMOOTH); + img.getGraphics().drawImage(logo, 6, 115, null); + } + + } else { + img = ImageIO.read(PdfAsSignatureParameter.class + .getResourceAsStream("/img/sign_prev_de.png")); + + if(logo != null) { + logo = logo.getScaledInstance(141, 140, + Image.SCALE_SMOOTH); + img.getGraphics().drawImage(logo, 6, 115, null); + } + } + return img; + } catch (IOException e) { + return new BufferedImage(getPlaceholderDimension().getWidth(), + getPlaceholderDimension().getHeight(), + BufferedImage.TYPE_INT_RGB); + } + +// Try to render signature block - disabled for now (just use images) +// +// try { +// PDFASHelper.getPdfAs(); +// +// +// float width = getPlaceholderDimension().getWidth() * PLACEHOLDER_SCALE; +// float height = getPlaceholderDimension().getHeight() * PLACEHOLDER_SCALE; +// +// Table table = this.getSignatureTable(); +// +// //log.info(table.toString()); +// +// BufferedImage timage = new BufferedImage(1, 1, +// BufferedImage.TYPE_INT_RGB); +// float[] heights = this.getTableHeights(table, table.getStyle(), +// height, (int)width, timage.getGraphics()); +// timage.flush(); +// float mheight = 0; +// +// for(int i = 0; i < heights.length; i++) { +// mheight += heights[i]; +// } +// +// this.height = (int) (mheight / PLACEHOLDER_SCALE); +// +// log.info("Width: " + width + " Height: " + height + " HShould: " + mheight); +// BufferedImage image = new BufferedImage((int) width, (int) mheight, +// BufferedImage.TYPE_INT_RGB); +// Graphics g = image.getGraphics(); +// +// g.setColor(table.getStyle().getBgColor()); +// g.fillRect(0, 0, (int) width, (int) mheight); +// +// g.setColor(Color.black); +// g.drawRect(0, 0, (int) width, (int) mheight); +// +// this.drawTable(0, 0, (int) width, (int) mheight, table, +// table.getStyle(), g, heights); +// +// g.dispose(); +// +// //save(image, "png"); +// +// return image; +// } catch (Exception ex) { +// try { +// return ImageIO.read(PdfAsSignatureParameter.class +// .getResourceAsStream("/img/fallbackPlaceholder.png")); +// } catch (IOException e) { +// return new BufferedImage(getPlaceholderDimension().getWidth(), +// getPlaceholderDimension().getHeight(), +// BufferedImage.TYPE_INT_RGB); +// } +// } + } + +// private Table getSignatureTable() throws SignatureException, SignatureTypesException { +// SignatureObject sign_obj = at.knowcenter.wag.egov.egiz.PdfAS +// .createSignatureObjectFromType(getSignatureProfileID()); +// +// sign_obj.fillValues(' ', true, false); +// sign_obj.setKZ(BinarySignator_1_1_0.MY_ID); +// return sign_obj.getAbstractTable(); +// } +// +// /** +// * used for debugging .. +// * +// * @param image +// * @param ext +// */ +// @SuppressWarnings("unused") +// @Deprecated +// private static void save(BufferedImage image, String ext) { +// String fileName = "savingAnImage"; +// File file = new File(fileName + "." + ext); +// try { +// ImageIO.write(image, ext, file); // ignore returned boolean +// log.debug("Saved as: " + file.getAbsolutePath()); +// } catch (IOException e) { +// System.out.println("Write error for " + file.getPath() + ": " +// + e.getMessage()); +// } +// } +// +// @SuppressWarnings("rawtypes") +// private int drawTable(int xoff, int yoff, int width, int height, +// Table table, Style parentstyle, Graphics g, float[] heights) { +// Style style = parentstyle; +// if (table.getStyle() != null) { +// style = table.getStyle(); +// } +// +// log.debug(String.format((Locale) null, "Table@ %dx%d", xoff, yoff)); +// +// Font oldFont = g.getFont(); +// Font font = PdfAsSignatureParameter.getFont(style); +// g.setFont(font); +// // draw background +// // graphic.setColor(style.getBgColor()); +// // graphic.fillRect(xoff, yoff, width, height); +// +// g.setColor(Color.black); +// +// // draw border +// if (style.getBorder() > 0) { +// g.setColor(Color.black); +// g.drawRect(xoff, yoff, width, height); +// } +// float[] colWidths = table.getColsRelativeWith(); +// float sum = 0; +// +// for (int i = 0; i < colWidths.length; i++) { +// sum += colWidths[i]; +// } +// +// float perUnit = width / sum; +// +// int padding = (int) (style.getPadding() * this.perUnitHeight * PLACEHOLDER_SCALE); +// +// ArrayList rows = table.getRows(); +// float roffset = 0; +// for (int rowidx = 0; rowidx < rows.size(); rowidx++) { +// ArrayList cols = (ArrayList) rows.get(rowidx); +// int rsize = (int) heights[rowidx]; +// for (int j = 0; j < cols.size(); j++) { +// Entry entry = (Entry) cols.get(j); +// float offset = 0; +// for (int k = 0; k < j; k++) { +// offset += colWidths[k] * perUnit; +// } +// if (entry.getType() == 0 || entry.getType() == 1) { +// // Text +// g.drawRect((int) (xoff + offset), +// (int) (yoff + roffset), +// (int) (colWidths[j] * perUnit), rsize); +// +// String[] lines = getLines(entry.getValue().toString(), (int)(colWidths[j] * perUnit), g.getFontMetrics(), (int) style.getPadding() * PLACEHOLDER_SCALE); +// +// for(int i = 0; i < lines.length; i++) { +// g.drawString(lines[i].toString(), (int) (xoff +// + offset + padding / PLACEHOLDER_SCALE), (int) (yoff + padding +// + roffset + (i + 1) * g.getFontMetrics().getHeight() * this.perUnitHeight)); +// } +// //g.drawString(entry.getValue().toString(), (int) (xoff +// // + offset + padding / PLACEHOLDER_SCALE), (int) (yoff + padding +// // + roffset + font.getSize() * this.perUnitHeight)); +// } else if (entry.getType() == 2) { +// // Image ... +// BufferedImage image; +// try { +// if (this.getEmblem() != null +// && this.getEmblem().getFileName() != null +// && new File(this.getEmblem().getFileName()) +// .exists()) { +// image = ImageIO.read(new File(this.getEmblem() +// .getFileName())); +// } else { +// image = ImageIO.read(new File(PDFASHelper +// .getWorkDir() +// + File.separator +// + entry.getValue().toString())); +// } +// int imgWidth = 30 * PLACEHOLDER_SCALE; +// int imgHeight = 30 * PLACEHOLDER_SCALE; +// Image img = image.getScaledInstance(imgWidth, imgHeight, +// Image.SCALE_SMOOTH); +// +// g.drawImage( +// img, +// (int) (xoff + offset + padding + (((colWidths[j] * perUnit) - imgWidth - 2* padding)) / 2), +// (int) (yoff + roffset + padding + ((rsize - imgHeight - 2* padding) / 2)), +// null); +// } catch (IOException e) { +// log.warn("Failed to paint emblem to placeholder image"); +// } +// } else { +// // Table +// +// int colWidth = (int) (colWidths[j] * perUnit); +// +// float[] cheights = this.getTableHeights( +// (Table) entry.getValue(), style, rsize, colWidth, g); +// +// this.drawTable( +// (int) (xoff + offset), +// (int) (yoff + roffset), +// (int) (colWidths[j] * perUnit), +// // (int)this.getTableHeight((Table) +// // entry.getValue(), style), +// rsize, (Table) entry.getValue(), style, g, +// cheights); +// /* +// * if (rsize < tsize) { rsize = tsize; } +// */ +// } +// } +// roffset += rsize; +// } +// +// g.setFont(oldFont); +// +// return (int) roffset; +// } +// +// private static Font getFont(Style style) { +// String def = "COURIER-NORMAL-8"; +// String fontString = style.getFont(); +// String[] font_arr = fontString.split(","); +// if (font_arr.length != 3) { +// return Font.decode(def); +// } +// Font font = Font.decode(String.format("%s-%s-%s", font_arr[0], font_arr[2], +// font_arr[1])); +// return font.deriveFont((float) font.getSize() * PLACEHOLDER_SCALE); +// } +// +// /** +// * extracts the value font +// * +// * @param style +// * the table style +// * @return the value font +// */ +// @SuppressWarnings("unused") +// private static Font getValueFont(Style style) { +// String def = "COURIER-NORMAL-8"; +// String fontString = style.getValueFont(); +// String[] font_arr = fontString.split(","); +// if (font_arr.length != 3) { +// return Font.decode(def); +// } +// Font font = Font.decode(String.format("%s-%s-%s", font_arr[0], font_arr[2], +// font_arr[1])); +// return font.deriveFont((float) font.getSize() * PLACEHOLDER_SCALE); +// } +// +// private static String[] getLines(String text, int width, FontMetrics fmetric, int padding) { +// String currentline = text; +// int averageCharWi = fmetric.charWidth('c'); +// +// int max_line_chars = (width - padding) / (averageCharWi); +// ArrayList<String> lines = new ArrayList<String>(); +// +// while(currentline.length() > max_line_chars) { +// int cutidx = currentline.substring(0, max_line_chars).lastIndexOf(' '); +// if(cutidx < 1) { +// cutidx = max_line_chars - 1; +// } else { +// cutidx++; +// } +// String tmpLine = currentline.substring(0, cutidx); +// lines.add(tmpLine); +// currentline = currentline.substring(cutidx); +// } +// lines.add(currentline); +// +// +// String[] arrline = new String[lines.size()]; +// for(int i = 0; i < lines.size(); i++) { +// arrline[i] = lines.get(i); +// } +// +// //log.debug(text + " needs " + lines.size() + " lines"); +// +// return arrline; +// } +// +// @SuppressWarnings("rawtypes") +// private float[] getTableHeights(Table table, Style parentstyle, float height, int width, Graphics g) { +// ArrayList rows = table.getRows(); +// float[] sizes = new float[rows.size()]; +// Style style = parentstyle; +// if (table.getStyle() != null) { +// style = table.getStyle(); +// } +// Font font = PdfAsSignatureParameter.getFont(style); +// g.setFont(font); +// +// float total_height = this.getTableHeight(table, parentstyle, width, g); +// +// float perUnit = height / total_height; +// +// this.perUnitHeight = perUnit; +// +// float[] colWidths = table.getColsRelativeWith(); +// float sum = 0; +// +// for (int i = 0; i < colWidths.length; i++) { +// sum += colWidths[i]; +// } +// +// float perUnitWidth = width / sum; +// +// for (int i = 0; i < rows.size(); i++) { +// Object robj = rows.get(i); +// ArrayList cols = (ArrayList) robj; +// float tsize = 0; +// float rsize = 0; +// for (int j = 0; j < cols.size(); j++) { +// Entry entry = (Entry) cols.get(j); +// if (entry.getType() == 0 || entry.getType() == 1) { +// int colWidth = (int) (colWidths[j] * perUnitWidth); +// +// float trsize = getLines(entry.getValue().toString(), colWidth, g.getFontMetrics(), (int) style.getPadding() * PLACEHOLDER_SCALE).length * g.getFontMetrics().getHeight() + (style.getPadding() * PLACEHOLDER_SCALE * 2); +// +// if (rsize < trsize) { +// rsize = trsize; +// } +// } else if (entry.getType() == 3) { +// +// int colWidth = (int) (colWidths[j] * perUnitWidth); +// +// tsize = this +// .getTableHeight((Table) entry.getValue(), style, colWidth, g); +// if (rsize < tsize) { +// rsize = tsize; +// } +// } +// } +// sizes[i] = perUnit * rsize; +// } +// +// return sizes; +// } +// +// @SuppressWarnings("rawtypes") +// private float getTableHeight(Table table, Style parentstyle, int width, Graphics g) { +// ArrayList rows = table.getRows(); +// Style style = parentstyle; +// if (table.getStyle() != null) { +// style = table.getStyle(); +// } +// float size = 0; +// +// float[] colWidths = table.getColsRelativeWith(); +// float sum = 0; +// +// for (int i = 0; i < colWidths.length; i++) { +// sum += colWidths[i]; +// } +// +// float perUnitWidth = width / sum; +// +// for (int i = 0; i < rows.size(); i++) { +// Object robj = rows.get(i); +// ArrayList cols = (ArrayList) robj; +// float tsize = 0; +// float rsize = 0; +// for (int j = 0; j < cols.size(); j++) { +// Entry entry = (Entry) cols.get(j); +// if (entry.getType() == 0 || entry.getType() == 1) { +// int colWidth = (int) (colWidths[j] * perUnitWidth); +// +// float trsize = getLines(entry.getValue().toString(), colWidth, g.getFontMetrics(), (int) style.getPadding() * PLACEHOLDER_SCALE).length * g.getFontMetrics().getHeight() + (style.getPadding() * PLACEHOLDER_SCALE * 2); +// +// if (rsize < trsize) { +// rsize = trsize; +// } +// +// /*if (rsize < ((style.getPadding() * PLACEHOLDER_SCALE * 2) + fontSize)) { +// rsize = ((style.getPadding() * PLACEHOLDER_SCALE * 2) + fontSize); +// }*/ +// } else if (entry.getType() == 3) { +// int colWidth = (int) (colWidths[j] * perUnitWidth); +// tsize = this +// .getTableHeight((Table) entry.getValue(), style, colWidth, g); +// if (rsize < tsize) { +// rsize = tsize; +// } +// } +// } +// size += rsize; +// } +// +// return size; +// } +} diff --git a/pdf-over-signer/pdf-over-sigpdfas3/src/main/java/at/asit/pdfover/signer/pdfas/exceptions/PDFASSLRequestException.java b/pdf-over-signer/pdf-over-sigpdfas3/src/main/java/at/asit/pdfover/signer/pdfas/exceptions/PDFASSLRequestException.java new file mode 100644 index 00000000..ceb25779 --- /dev/null +++ b/pdf-over-signer/pdf-over-sigpdfas3/src/main/java/at/asit/pdfover/signer/pdfas/exceptions/PDFASSLRequestException.java @@ -0,0 +1,34 @@ +/* + * Copyright 2012 by A-SIT, Secure Information Technology Center Austria + * + * Licensed under the EUPL, Version 1.1 or - as soon they will be approved by + * the European Commission - subsequent versions of the EUPL (the "Licence"); + * You may not use this work except in compliance with the Licence. + * You may obtain a copy of the Licence at: + * http://joinup.ec.europa.eu/software/page/eupl + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the Licence is distributed on an "AS IS" basis, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the Licence for the specific language governing permissions and + * limitations under the Licence. + */ +package at.asit.pdfover.signer.pdfas.exceptions; + +/** + * + */ +public class PDFASSLRequestException extends Exception { + /** + * + */ + private static final long serialVersionUID = -7515747014505057787L; + + /** + * Constructor + * @param msg + */ + public PDFASSLRequestException(String msg) { + super(msg); + } +} |