diff options
author | Andreas Fitzek <andreas.fitzek@iaik.tugraz.at> | 2014-10-21 10:28:54 +0200 |
---|---|---|
committer | Andreas Fitzek <andreas.fitzek@iaik.tugraz.at> | 2014-10-21 10:28:54 +0200 |
commit | d16a7c27a77399806d1e2c6ae082e98d456c3baf (patch) | |
tree | 309ec8c90dd8274375ebb8480dfc3e5cafeeedb6 | |
parent | 40841f154c31d8e9e6e670def11dcb5899f5db71 (diff) | |
download | pdf-as-4-d16a7c27a77399806d1e2c6ae082e98d456c3baf.tar.gz pdf-as-4-d16a7c27a77399806d1e2c6ae082e98d456c3baf.tar.bz2 pdf-as-4-d16a7c27a77399806d1e2c6ae082e98d456c3baf.zip |
PDF API update to ouput signed documents via output stream
13 files changed, 76 insertions, 72 deletions
diff --git a/pdf-as-cli/src/main/java/at/gv/egiz/pdfas/cli/Main.java b/pdf-as-cli/src/main/java/at/gv/egiz/pdfas/cli/Main.java index 3ce215c8..d847f31b 100644 --- a/pdf-as-cli/src/main/java/at/gv/egiz/pdfas/cli/Main.java +++ b/pdf-as-cli/src/main/java/at/gv/egiz/pdfas/cli/Main.java @@ -39,7 +39,6 @@ import org.apache.commons.cli.HelpFormatter; import org.apache.commons.cli.Option; import org.apache.commons.cli.Options; import org.apache.commons.cli.ParseException; -import org.apache.commons.io.IOUtils; import at.gv.egiz.pdfas.common.exceptions.PDFASError; import at.gv.egiz.pdfas.common.utils.StreamUtils; @@ -51,8 +50,8 @@ import at.gv.egiz.pdfas.lib.api.sign.IPlainSigner; import at.gv.egiz.pdfas.lib.api.sign.SignParameter; import at.gv.egiz.pdfas.lib.api.sign.SignResult; import at.gv.egiz.pdfas.lib.api.verify.VerifyParameter; -import at.gv.egiz.pdfas.lib.api.verify.VerifyResult; import at.gv.egiz.pdfas.lib.api.verify.VerifyParameter.SignatureVerificationLevel; +import at.gv.egiz.pdfas.lib.api.verify.VerifyResult; import at.gv.egiz.pdfas.moa.MOAConnector; import at.gv.egiz.pdfas.sigs.pades.PAdESSigner; import at.gv.egiz.pdfas.sigs.pades.PAdESSignerKeystore; @@ -334,9 +333,9 @@ public class Main { pdfAs = PdfAsFactory.createPdfAs(new File(configurationFile)); Configuration configuration = pdfAs.getConfiguration(); - + FileOutputStream fos = new FileOutputStream(outputPdfFile, false); SignParameter signParameter = PdfAsFactory.createSignParameter( - configuration, dataSource); + configuration, dataSource, fos); String id = UUID.randomUUID().toString(); signParameter.setTransactionId(id); @@ -415,11 +414,10 @@ public class Main { signParameter.setSignatureProfileId(profilID); System.out.println("Starting signature for " + pdfFile); System.out.println("Selected signature Profile " + profilID); + + @SuppressWarnings("unused") SignResult result = pdfAs.sign(signParameter); - FileOutputStream fos = new FileOutputStream(outputPdfFile, false); - IOUtils.copy(result.getOutputDocument(), fos); - fos.close(); System.out.println("Signed document " + outputFile); } diff --git a/pdf-as-cli/src/test/java/at/gv/egiz/pdfas/cli/test/SignaturProfileTest.java b/pdf-as-cli/src/test/java/at/gv/egiz/pdfas/cli/test/SignaturProfileTest.java index 6b1eeaa9..75f19c71 100644 --- a/pdf-as-cli/src/test/java/at/gv/egiz/pdfas/cli/test/SignaturProfileTest.java +++ b/pdf-as-cli/src/test/java/at/gv/egiz/pdfas/cli/test/SignaturProfileTest.java @@ -98,17 +98,16 @@ public class SignaturProfileTest { System.out.println("Testing " + profile); DataSource source = new ByteArrayDataSource(input); - + + FileOutputStream fos = new FileOutputStream(targetFolder + profile + ".pdf"); SignParameter signParameter = PdfAsFactory.createSignParameter( - config, source); + config, source, fos); signParameter.setPlainSigner(signer); signParameter.setSignatureProfileId(profile); SignResult result = pdfas.sign(signParameter); - FileOutputStream fos = new FileOutputStream(targetFolder + profile + ".pdf"); - IOUtils.copy(result.getOutputDocument(), fos); fos.close(); } @@ -120,17 +119,15 @@ public class SignaturProfileTest { System.out.println("Testing " + profile); DataSource source = new ByteArrayDataSource(inputPDFA); - + FileOutputStream fos = new FileOutputStream(targetFolder + "PDFA_" + profile + ".pdf"); SignParameter signParameter = PdfAsFactory.createSignParameter( - config, source); + config, source, fos); signParameter.setPlainSigner(signer); signParameter.setSignatureProfileId(profile); SignResult result = pdfas.sign(signParameter); - FileOutputStream fos = new FileOutputStream(targetFolder + "PDFA_" + profile + ".pdf"); - IOUtils.copy(result.getOutputDocument(), fos); fos.close(); } } catch (Throwable e) { diff --git a/pdf-as-legacy/src/main/java/at/gv/egiz/pdfas/wrapper/PdfAsObject.java b/pdf-as-legacy/src/main/java/at/gv/egiz/pdfas/wrapper/PdfAsObject.java index 2412b8cd..798fcd6f 100644 --- a/pdf-as-legacy/src/main/java/at/gv/egiz/pdfas/wrapper/PdfAsObject.java +++ b/pdf-as-legacy/src/main/java/at/gv/egiz/pdfas/wrapper/PdfAsObject.java @@ -26,6 +26,7 @@ package at.gv.egiz.pdfas.wrapper; import iaik.x509.X509Certificate; import java.io.File; +import java.io.IOException; import java.security.cert.CertificateEncodingException; import java.security.cert.CertificateException; import java.util.ArrayList; @@ -234,7 +235,9 @@ public class PdfAsObject implements PdfAs { SignParameter signParameter4 = PdfAsFactory.createSignParameter( this.configuration, new ByteArrayDataSource(signParameters - .getDocument().getAsByteArray())); + .getDocument().getAsByteArray()), + signParameters.getOutput() + .createOutputStream("application/pdf")); SignParameterWrapper wrapper = new SignParameterWrapper( signParameters, signParameter4); @@ -273,7 +276,10 @@ public class PdfAsObject implements PdfAs { e.printStackTrace(); throw new PdfAsException( ErrorCode.SIGNATURE_COULDNT_BE_CREATED, e.getMessage()); - + } catch (IOException e) { + e.printStackTrace(); + throw new PdfAsException( + ErrorCode.SIGNATURE_COULDNT_BE_CREATED, e.getMessage()); } } diff --git a/pdf-as-legacy/src/main/java/at/gv/egiz/pdfas/wrapper/SignParameterWrapper.java b/pdf-as-legacy/src/main/java/at/gv/egiz/pdfas/wrapper/SignParameterWrapper.java index 12395bde..7349dbec 100644 --- a/pdf-as-legacy/src/main/java/at/gv/egiz/pdfas/wrapper/SignParameterWrapper.java +++ b/pdf-as-legacy/src/main/java/at/gv/egiz/pdfas/wrapper/SignParameterWrapper.java @@ -106,10 +106,10 @@ public class SignParameterWrapper { public void syncNewToOld(SignResult result) throws PdfAsException { try { - OutputStream os = this.signParameters.getOutput() - .createOutputStream("application/pdf"); - IOUtils.copy(result.getOutputDocument(), os); - os.close(); + //OutputStream os = this.signParameters.getOutput() + // .createOutputStream("application/pdf"); + //IOUtils.copy(result.getOutputDocument(), os); + //os.close(); } catch (Exception e) { throw new PdfAsException(ErrorCode.SIGNATURE_COULDNT_BE_CREATED, e.getMessage()); diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/api/PdfAsFactory.java b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/api/PdfAsFactory.java index 6570c419..acfb7ef5 100644 --- a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/api/PdfAsFactory.java +++ b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/api/PdfAsFactory.java @@ -31,6 +31,7 @@ import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import java.io.OutputStream; import java.security.Provider; import java.security.Security; import java.util.zip.ZipEntry; @@ -188,8 +189,8 @@ public class PdfAsFactory implements IConfigurationConstants { * @return */ public static SignParameter createSignParameter( - Configuration configuration, DataSource dataSource) { - SignParameter param = new SignParameterImpl(configuration, dataSource); + Configuration configuration, DataSource dataSource, OutputStream output) { + SignParameter param = new SignParameterImpl(configuration, dataSource, output); return param; } diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/api/sign/SignParameter.java b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/api/sign/SignParameter.java index c1bfe53f..6a7ccf24 100644 --- a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/api/sign/SignParameter.java +++ b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/api/sign/SignParameter.java @@ -23,6 +23,8 @@ ******************************************************************************/ package at.gv.egiz.pdfas.lib.api.sign; +import java.io.OutputStream; + import at.gv.egiz.pdfas.lib.api.PdfAsParameter; public interface SignParameter extends PdfAsParameter { @@ -65,4 +67,10 @@ public interface SignParameter extends PdfAsParameter { * @return */ public IPlainSigner getPlainSigner(); + + /** + * Gets the outputstream, where the signed document will be written to + * @return + */ + public OutputStream getSignatureResult(); } diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/api/sign/SignResult.java b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/api/sign/SignResult.java index 180792ea..8084f8f1 100644 --- a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/api/sign/SignResult.java +++ b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/api/sign/SignResult.java @@ -23,18 +23,11 @@ ******************************************************************************/ package at.gv.egiz.pdfas.lib.api.sign; -import java.io.InputStream; import java.security.cert.X509Certificate; import at.gv.egiz.pdfas.lib.api.SignaturePosition; public interface SignResult { - /** - * Returns the filled output data sink. - * - * @return Returns the filled output data sink. - */ - public InputStream getOutputDocument(); /** * Returns the certificate of the signer. diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/PdfAsImpl.java b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/PdfAsImpl.java index 387a6c31..4828bae3 100644 --- a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/PdfAsImpl.java +++ b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/PdfAsImpl.java @@ -26,7 +26,6 @@ package at.gv.egiz.pdfas.lib.impl; import iaik.x509.X509Certificate; import java.awt.Image; -import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; import java.util.Calendar; @@ -450,9 +449,9 @@ public class PdfAsImpl implements PdfAs, IConfigurationConstants, throws IOException { // ================================================================ // Create SignResult - SignResultImpl result = new SignResultImpl(new ByteArrayInputStream( - status.getPdfObject().getSignedDocument())); - + SignResultImpl result = new SignResultImpl(); + status.getSignParamter().getSignatureResult().write(status.getPdfObject().getSignedDocument()); + status.getSignParamter().getSignatureResult().flush(); result.setSignerCertificate(status.getRequestedSignature() .getCertificate()); result.setSignaturePosition(status.getRequestedSignature() diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/SignParameterImpl.java b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/SignParameterImpl.java index f547b2b0..9584e1bf 100644 --- a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/SignParameterImpl.java +++ b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/SignParameterImpl.java @@ -23,6 +23,8 @@ ******************************************************************************/ package at.gv.egiz.pdfas.lib.impl; +import java.io.OutputStream; + import javax.activation.DataSource; import at.gv.egiz.pdfas.lib.api.Configuration; @@ -34,10 +36,12 @@ public class SignParameterImpl extends PdfAsParameterImpl implements SignParamet protected String signaturePosition = null; protected DataSource output = null; protected IPlainSigner signer = null; + protected OutputStream outputStream = null; public SignParameterImpl(Configuration configuration, - DataSource dataSource) { + DataSource dataSource, OutputStream outputStream) { super(configuration, dataSource); + this.outputStream = outputStream; } // ======================================================================== @@ -58,14 +62,6 @@ public class SignParameterImpl extends PdfAsParameterImpl implements SignParamet this.signaturePosition = signaturePosition; } - public void setOutput(DataSource output) { - this.output = output; - } - - public DataSource getOutput() { - return this.output; - } - public void setPlainSigner(IPlainSigner signer) { this.signer = signer; } @@ -73,5 +69,10 @@ public class SignParameterImpl extends PdfAsParameterImpl implements SignParamet public IPlainSigner getPlainSigner() { return this.signer; } + + @Override + public OutputStream getSignatureResult() { + return outputStream; + } } diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/SignResultImpl.java b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/SignResultImpl.java index 1a0e6490..f713eb6a 100644 --- a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/SignResultImpl.java +++ b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/SignResultImpl.java @@ -23,7 +23,6 @@ ******************************************************************************/ package at.gv.egiz.pdfas.lib.impl; -import java.io.InputStream; import java.security.cert.X509Certificate; import at.gv.egiz.pdfas.lib.api.SignaturePosition; @@ -31,16 +30,10 @@ import at.gv.egiz.pdfas.lib.api.sign.SignResult; public class SignResultImpl implements SignResult { - protected InputStream dataSink; protected X509Certificate certificate; protected SignaturePosition position; - public SignResultImpl(InputStream dataSink) { - this.dataSink = dataSink; - } - - public InputStream getOutputDocument() { - return this.dataSink; + public SignResultImpl() { } public X509Certificate getSignerCertificate() { diff --git a/pdf-as-lib/src/test/java/at/gv/egiz/pdfas/lib/test/mains/PDFToImage.java b/pdf-as-lib/src/test/java/at/gv/egiz/pdfas/lib/test/mains/PDFToImage.java index f78319c6..caf3472a 100644 --- a/pdf-as-lib/src/test/java/at/gv/egiz/pdfas/lib/test/mains/PDFToImage.java +++ b/pdf-as-lib/src/test/java/at/gv/egiz/pdfas/lib/test/mains/PDFToImage.java @@ -4,6 +4,7 @@ import iaik.x509.X509Certificate; import java.awt.Image; import java.awt.image.RenderedImage; +import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.util.ArrayList; @@ -59,8 +60,10 @@ public class PDFToImage { X509Certificate cert = new X509Certificate(new FileInputStream( "/home/afitzek/qualified.cer")); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + SignParameter parameter = PdfAsFactory.createSignParameter( - config, null); + config, null, baos); parameter.setSignatureProfileId(profile); Image img = pdfas.generateVisibleSignaturePreview(parameter, cert, 128); diff --git a/pdf-as-tests/src/test/java/at/gv/egiz/param_tests/SignatureTest.java b/pdf-as-tests/src/test/java/at/gv/egiz/param_tests/SignatureTest.java index 6b18afa5..f0e47896 100644 --- a/pdf-as-tests/src/test/java/at/gv/egiz/param_tests/SignatureTest.java +++ b/pdf-as-tests/src/test/java/at/gv/egiz/param_tests/SignatureTest.java @@ -101,9 +101,9 @@ public class SignatureTest { pdfAs = PdfAsFactory.createPdfAs(new File(baseTestData .getConfigurationFile())); Configuration configuration = pdfAs.getConfiguration(); - + FileOutputStream fos = new FileOutputStream(outputPdfFile, false); signParameter = PdfAsFactory.createSignParameter(configuration, - dataSource); + dataSource, fos); String id = UUID.randomUUID().toString(); signParameter.setTransactionId(id); @@ -157,11 +157,9 @@ public class SignatureTest { signParameter.setSignatureProfileId(baseTestData.getProfilID()); logger.debug("Starting signature for " + baseTestData.getPdfFile()); logger.debug("Selected signature Profile " + baseTestData.getProfilID()); - SignResult result = pdfAs.sign(signParameter); - FileOutputStream fos = null; + @SuppressWarnings("unused") + SignResult result = pdfAs.sign(signParameter); try { - fos = new FileOutputStream(outputPdfFile, false); - IOUtils.copy(result.getOutputDocument(), fos); fos.close(); } catch (IOException e) { logger.debug("IO exception occured while writing PDF output file", diff --git a/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/helper/PdfAsHelper.java b/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/helper/PdfAsHelper.java index fc499f94..caf6a967 100644 --- a/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/helper/PdfAsHelper.java +++ b/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/helper/PdfAsHelper.java @@ -49,7 +49,6 @@ import javax.xml.ws.WebServiceException; import org.apache.commons.codec.binary.Base64; import org.apache.commons.io.FileUtils; -import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringEscapeUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -89,6 +88,7 @@ public class PdfAsHelper { private static final String PDF_CONFIG = "PDF_CONFIG"; private static final String PDF_STATUS = "PDF_STATUS"; + private static final String PDF_OUTPUT = "PDF_OUTPUT"; private static final String PDF_SL_CONNECTOR = "PDF_SL_CONNECTOR"; private static final String PDF_SIGNER = "PDF_SIGNER"; private static final String PDF_SL_INTERACTIVE = "PDF_SL_INTERACTIVE"; @@ -338,9 +338,11 @@ public class PdfAsHelper { Configuration config = pdfAs.getConfiguration(); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + // Generate Sign Parameter SignParameter signParameter = PdfAsFactory.createSignParameter(config, - new ByteArrayDataSource(pdfData)); + new ByteArrayDataSource(pdfData), baos); // Get Connector String connector = PdfAsParameterExtractor.getConnector(request); @@ -374,7 +376,7 @@ public class PdfAsHelper { SignResult result = pdfAs.sign(signParameter); - return IOUtils.toByteArray(result.getOutputDocument()); + return baos.toByteArray(); } /** @@ -393,9 +395,11 @@ public class PdfAsHelper { PDFASSignParameters params) throws Exception { Configuration config = pdfAs.getConfiguration(); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + // Generate Sign Parameter SignParameter signParameter = PdfAsFactory.createSignParameter(config, - new ByteArrayDataSource(pdfData)); + new ByteArrayDataSource(pdfData), baos); // Get Connector @@ -435,8 +439,7 @@ public class PdfAsHelper { SignResult signResult = pdfAs.sign(signParameter); PDFASSignResponse signResponse = new PDFASSignResponse(); - signResponse.setSignedPDF(IOUtils.toByteArray(signResult - .getOutputDocument())); + signResponse.setSignedPDF(baos.toByteArray()); PDFASVerificationResponse verResponse = new PDFASVerificationResponse(); @@ -471,9 +474,12 @@ public class PdfAsHelper { Configuration config = pdfAs.getConfiguration(); session.setAttribute(PDF_CONFIG, config); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + session.setAttribute(PDF_OUTPUT, baos); + // Generate Sign Parameter SignParameter signParameter = PdfAsFactory.createSignParameter(config, - new ByteArrayDataSource(pdfData)); + new ByteArrayDataSource(pdfData), baos); logger.info("Setting TransactionID: " + transactionId); @@ -532,7 +538,7 @@ public class PdfAsHelper { PdfAsHelper.class.getResourceAsStream("/qualified.cer")); Configuration config = pdfAs.getConfiguration(); SignParameter parameter = PdfAsFactory - .createSignParameter(config, null); + .createSignParameter(config, null, null); parameter.setSignatureProfileId(profile); Image img = pdfAs.generateVisibleSignaturePreview(parameter, cert, resolution); @@ -677,13 +683,14 @@ public class PdfAsHelper { logger.debug("Document ready!"); SignResult result = pdfAs.finishSign(statusRequest); - - byte[] signedPdf = IOUtils.toByteArray(result - .getOutputDocument()); - + + ByteArrayOutputStream baos = (ByteArrayOutputStream) session + .getAttribute(PDF_OUTPUT); + baos.close(); + PDFASVerificationResponse verResponse = new PDFASVerificationResponse(); List<VerifyResult> verResults = PdfAsHelper.synchornousVerify( - signedPdf, -2, + baos.toByteArray(), -2, PdfAsHelper.getVerificationLevel(request), null); if (verResults.size() != 1) { @@ -698,7 +705,7 @@ public class PdfAsHelper { .getCode()); PdfAsHelper.setPDFASVerificationResponse(request, verResponse); - PdfAsHelper.setSignedPdf(request, response, signedPdf); + PdfAsHelper.setSignedPdf(request, response, baos.toByteArray()); PdfAsHelper.gotoProvidePdf(context, request, response); String signerCert = Base64.encodeBase64String(result |