diff options
author | Andreas Fitzek <andreas.fitzek@iaik.tugraz.at> | 2014-10-15 14:23:04 +0200 |
---|---|---|
committer | Andreas Fitzek <andreas.fitzek@iaik.tugraz.at> | 2014-10-15 14:23:04 +0200 |
commit | cfbd5f9d8af638aa830aa535feadb094109e6455 (patch) | |
tree | 5d65bb93b64314f7e3f3455002b4c909242e5bc5 | |
parent | 0e31e524b419cd898139d1b8afa92cb794a3f821 (diff) | |
download | pdf-as-4-cfbd5f9d8af638aa830aa535feadb094109e6455.tar.gz pdf-as-4-cfbd5f9d8af638aa830aa535feadb094109e6455.tar.bz2 pdf-as-4-cfbd5f9d8af638aa830aa535feadb094109e6455.zip |
Rebuild API to support Java DataSource and Streams for in and output
23 files changed, 220 insertions, 387 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 13bf87d3..222fd33a 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 @@ -30,6 +30,8 @@ import java.util.Iterator; import java.util.List; import java.util.UUID; +import javax.activation.DataSource; + import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.CommandLineParser; import org.apache.commons.cli.GnuParser; @@ -37,16 +39,16 @@ 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.utils.StreamUtils; -import at.gv.egiz.pdfas.lib.api.ByteArrayDataSink; import at.gv.egiz.pdfas.lib.api.ByteArrayDataSource; import at.gv.egiz.pdfas.lib.api.Configuration; -import at.gv.egiz.pdfas.lib.api.DataSource; import at.gv.egiz.pdfas.lib.api.PdfAs; import at.gv.egiz.pdfas.lib.api.PdfAsFactory; 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; @@ -322,8 +324,6 @@ public class Main { StreamUtils.inputStreamToByteArray(new FileInputStream( inputFile))); - ByteArrayDataSink dataSink = new ByteArrayDataSink(); - PdfAs pdfAs = null; pdfAs = PdfAsFactory.createPdfAs(new File(configurationFile)); @@ -404,17 +404,17 @@ public class Main { slConnector = new PAdESSigner(new BKUSLConnector(configuration)); } - signParameter.setOutput(dataSink); signParameter.setPlainSigner(slConnector); signParameter.setDataSource(dataSource); signParameter.setSignaturePosition(positionString); signParameter.setSignatureProfileId(profilID); System.out.println("Starting signature for " + pdfFile); System.out.println("Selected signature Profile " + profilID); - /*SignResult result = */pdfAs.sign(signParameter); + SignResult result = pdfAs.sign(signParameter); FileOutputStream fos = new FileOutputStream(outputPdfFile, false); - fos.write(dataSink.getData()); + 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 e14b8ca0..6b1eeaa9 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 @@ -30,13 +30,13 @@ import java.util.ArrayList; import java.util.Iterator; import java.util.List; +import javax.activation.DataSource; + import org.apache.commons.io.IOUtils; import at.gv.egiz.pdfas.common.settings.ISettings; -import at.gv.egiz.pdfas.lib.api.ByteArrayDataSink; import at.gv.egiz.pdfas.lib.api.ByteArrayDataSource; import at.gv.egiz.pdfas.lib.api.Configuration; -import at.gv.egiz.pdfas.lib.api.DataSource; import at.gv.egiz.pdfas.lib.api.PdfAs; import at.gv.egiz.pdfas.lib.api.PdfAsFactory; import at.gv.egiz.pdfas.lib.api.sign.IPlainSigner; @@ -98,19 +98,17 @@ public class SignaturProfileTest { System.out.println("Testing " + profile); DataSource source = new ByteArrayDataSource(input); - ByteArrayDataSink sink = new ByteArrayDataSink(); SignParameter signParameter = PdfAsFactory.createSignParameter( config, source); signParameter.setPlainSigner(signer); - signParameter.setOutput(sink); signParameter.setSignatureProfileId(profile); SignResult result = pdfas.sign(signParameter); FileOutputStream fos = new FileOutputStream(targetFolder + profile + ".pdf"); - fos.write(sink.getData()); + IOUtils.copy(result.getOutputDocument(), fos); fos.close(); } @@ -122,19 +120,17 @@ public class SignaturProfileTest { System.out.println("Testing " + profile); DataSource source = new ByteArrayDataSource(inputPDFA); - ByteArrayDataSink sink = new ByteArrayDataSink(); SignParameter signParameter = PdfAsFactory.createSignParameter( config, source); signParameter.setPlainSigner(signer); - signParameter.setOutput(sink); signParameter.setSignatureProfileId(profile); SignResult result = pdfas.sign(signParameter); FileOutputStream fos = new FileOutputStream(targetFolder + "PDFA_" + profile + ".pdf"); - fos.write(sink.getData()); + 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 bef3b01f..ac76a56b 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 @@ -101,7 +101,7 @@ public class PdfAsObject implements PdfAs { request = (StatusRequestImpl) this.pdfas4.process(request); if(request.isReady()) { at.gv.egiz.pdfas.lib.api.sign.SignResult result = this.pdfas4.finishSign(request); - sdi.wrapper.syncNewToOld(); + sdi.wrapper.syncNewToOld(result); SignResultImpl oldresult = new SignResultImpl(sdi.wrapper.getSignParameters().getOutput(), sdi.getX509Certificate(), new at.gv.egiz.pdfas.wrapper.SignaturePositionImpl( result.getSignaturePosition())); 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 7043baaa..12395bde 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 @@ -26,14 +26,15 @@ package at.gv.egiz.pdfas.wrapper; import java.io.OutputStream; import java.util.Enumeration; +import org.apache.commons.io.IOUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import at.gv.egiz.pdfas.api.exceptions.ErrorCode; import at.gv.egiz.pdfas.api.exceptions.PdfAsException; import at.gv.egiz.pdfas.api.sign.SignParameters; -import at.gv.egiz.pdfas.lib.api.ByteArrayDataSink; import at.gv.egiz.pdfas.lib.api.sign.SignParameter; +import at.gv.egiz.pdfas.lib.api.sign.SignResult; import at.gv.egiz.pdfas.moa.MOAConnector; import at.gv.egiz.pdfas.sigs.pades.PAdESSigner; import at.gv.egiz.sl.util.BKUSLConnector; @@ -45,7 +46,6 @@ public class SignParameterWrapper { private SignParameter signParameter4; private SignParameters signParameters; - private ByteArrayDataSink output; public SignParameterWrapper(SignParameters signParameters, SignParameter signParameter4) { @@ -54,9 +54,7 @@ public class SignParameterWrapper { } public void syncOldToNew() throws PdfAsException { - output = new ByteArrayDataSink(); - this.signParameter4.setOutput(output); - + if (this.signParameters.getSignaturePositioning() != null) { // Create positioning string String posString = this.signParameters.getSignaturePositioning() @@ -106,11 +104,11 @@ public class SignParameterWrapper { } } - public void syncNewToOld() throws PdfAsException { + public void syncNewToOld(SignResult result) throws PdfAsException { try { OutputStream os = this.signParameters.getOutput() .createOutputStream("application/pdf"); - os.write(output.getData()); + IOUtils.copy(result.getOutputDocument(), os); os.close(); } catch (Exception e) { throw new PdfAsException(ErrorCode.SIGNATURE_COULDNT_BE_CREATED, diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/api/ByteArrayDataSink.java b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/api/ByteArrayDataSink.java deleted file mode 100644 index b7c14dbf..00000000 --- a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/api/ByteArrayDataSink.java +++ /dev/null @@ -1,48 +0,0 @@ -/******************************************************************************* - * <copyright> Copyright 2014 by E-Government Innovation Center EGIZ, Graz, Austria </copyright> - * PDF-AS has been contracted by the E-Government Innovation Center EGIZ, a - * joint initiative of the Federal Chancellery Austria and Graz University of - * Technology. - * - * 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://www.osor.eu/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. - * - * This product combines work with different licenses. See the "NOTICE" text - * file for details on the various modules and licenses. - * The "NOTICE" text file is part of the distribution. Any derivative works - * that you distribute must include a readable copy of the "NOTICE" text file. - ******************************************************************************/ -package at.gv.egiz.pdfas.lib.api; - -import java.io.ByteArrayOutputStream; -import java.io.OutputStream; - -/** - * A simple byte array data sink - */ -public class ByteArrayDataSink implements DataSink { - - protected ByteArrayOutputStream bos = new ByteArrayOutputStream(); - - public OutputStream createOutputStream() { - bos = new ByteArrayOutputStream(); - return bos; - } - - /** - * Returns the output data - * @return the output data - */ - public byte[] getData() { - return bos.toByteArray(); - } -} diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/api/ByteArrayDataSource.java b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/api/ByteArrayDataSource.java index fdf175c7..fa55bcd0 100644 --- a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/api/ByteArrayDataSource.java +++ b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/api/ByteArrayDataSource.java @@ -1,45 +1,38 @@ -/******************************************************************************* - * <copyright> Copyright 2014 by E-Government Innovation Center EGIZ, Graz, Austria </copyright> - * PDF-AS has been contracted by the E-Government Innovation Center EGIZ, a - * joint initiative of the Federal Chancellery Austria and Graz University of - * Technology. - * - * 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://www.osor.eu/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. - * - * This product combines work with different licenses. See the "NOTICE" text - * file for details on the various modules and licenses. - * The "NOTICE" text file is part of the distribution. Any derivative works - * that you distribute must include a readable copy of the "NOTICE" text file. - ******************************************************************************/ package at.gv.egiz.pdfas.lib.api; -/** - * A simple byte array data source - */ +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +import javax.activation.DataSource; + public class ByteArrayDataSource implements DataSource { - private byte[] byteData; + private byte[] data; + + public ByteArrayDataSource(byte[] data) { + this.data = data; + } + + @Override + public String getContentType() { + return "application/pdf"; + } - public ByteArrayDataSource(byte[] data) { - this.byteData = data; - } + @Override + public InputStream getInputStream() throws IOException { + return new ByteArrayInputStream(data); + } - public String getMIMEType() { - return "application/pdf"; - } + @Override + public String getName() { + return null; + } - public byte[] getByteData() { - return this.byteData; - } + @Override + public OutputStream getOutputStream() throws IOException { + throw new IOException("Not supported!"); + } } diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/api/DataSink.java b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/api/DataSink.java deleted file mode 100644 index 89dfd8c5..00000000 --- a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/api/DataSink.java +++ /dev/null @@ -1,37 +0,0 @@ -/******************************************************************************* - * <copyright> Copyright 2014 by E-Government Innovation Center EGIZ, Graz, Austria </copyright> - * PDF-AS has been contracted by the E-Government Innovation Center EGIZ, a - * joint initiative of the Federal Chancellery Austria and Graz University of - * Technology. - * - * 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://www.osor.eu/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. - * - * This product combines work with different licenses. See the "NOTICE" text - * file for details on the various modules and licenses. - * The "NOTICE" text file is part of the distribution. Any derivative works - * that you distribute must include a readable copy of the "NOTICE" text file. - ******************************************************************************/ -package at.gv.egiz.pdfas.lib.api; - -import java.io.OutputStream; - -/** - * Data Sink interface. - */ -public interface DataSink { - /** - * Creates an output stream to receive the data - * @return an output stream for the data - */ - public OutputStream createOutputStream(); -} diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/api/DataSource.java b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/api/DataSource.java deleted file mode 100644 index 9bb2f7fc..00000000 --- a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/api/DataSource.java +++ /dev/null @@ -1,45 +0,0 @@ -/******************************************************************************* - * <copyright> Copyright 2014 by E-Government Innovation Center EGIZ, Graz, Austria </copyright> - * PDF-AS has been contracted by the E-Government Innovation Center EGIZ, a - * joint initiative of the Federal Chancellery Austria and Graz University of - * Technology. - * - * 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://www.osor.eu/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. - * - * This product combines work with different licenses. See the "NOTICE" text - * file for details on the various modules and licenses. - * The "NOTICE" text file is part of the distribution. Any derivative works - * that you distribute must include a readable copy of the "NOTICE" text file. - ******************************************************************************/ -package at.gv.egiz.pdfas.lib.api; - -/** - * Data Source interface - * - * All data sources in PDF-AS implement this interface. Also custom data sources have to - * implement this interface to allow PDF-AS to use them. - */ -public interface DataSource { - - /** - * Gets the MIME Type of the contained data. - * @return MIME Type - */ - public String getMIMEType(); - - /** - * Gets the contained data - * @return the contained data - */ - public byte[] getByteData(); -} 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 b0b34f5e..6570c419 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 @@ -36,6 +36,8 @@ import java.security.Security; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; +import javax.activation.DataSource; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/api/PdfAsParameter.java b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/api/PdfAsParameter.java index bb35f68f..02779ff1 100644 --- a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/api/PdfAsParameter.java +++ b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/api/PdfAsParameter.java @@ -23,6 +23,8 @@ ******************************************************************************/ package at.gv.egiz.pdfas.lib.api; +import javax.activation.DataSource; + public interface PdfAsParameter { /** 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 905b8c2a..c1bfe53f 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,7 +23,6 @@ ******************************************************************************/ package at.gv.egiz.pdfas.lib.api.sign; -import at.gv.egiz.pdfas.lib.api.DataSink; import at.gv.egiz.pdfas.lib.api.PdfAsParameter; public interface SignParameter extends PdfAsParameter { @@ -52,18 +51,6 @@ public interface SignParameter extends PdfAsParameter { * @param signaturePosition The signature position string */ public void setSignaturePosition(String signaturePosition); - - /** - * Sets the data sink for the signature process - * @param output - */ - public void setOutput(DataSink output); - - /** - * Gets the data sink for the signature process - * @return - */ - public DataSink getOutput(); /** * Sets the signer to use 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 0af34121..180792ea 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,9 +23,9 @@ ******************************************************************************/ package at.gv.egiz.pdfas.lib.api.sign; +import java.io.InputStream; import java.security.cert.X509Certificate; -import at.gv.egiz.pdfas.lib.api.DataSink; import at.gv.egiz.pdfas.lib.api.SignaturePosition; public interface SignResult { @@ -34,7 +34,7 @@ public interface SignResult { * * @return Returns the filled output data sink. */ - public DataSink getOutputDocument(); + 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 7fe39ff7..c853f7eb 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 @@ -32,11 +32,12 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; -import java.io.OutputStream; +import java.io.InputStream; import java.util.ArrayList; import java.util.Calendar; import java.util.List; +import org.apache.commons.io.IOUtils; import org.apache.pdfbox.cos.COSArray; import org.apache.pdfbox.cos.COSBase; import org.apache.pdfbox.cos.COSDictionary; @@ -57,6 +58,7 @@ import at.gv.egiz.pdfas.common.settings.Settings; import at.gv.egiz.pdfas.common.settings.SignatureProfileSettings; import at.gv.egiz.pdfas.common.utils.PDFUtils; import at.gv.egiz.pdfas.common.utils.StreamUtils; +import at.gv.egiz.pdfas.lib.api.ByteArrayDataSource; import at.gv.egiz.pdfas.lib.api.Configuration; import at.gv.egiz.pdfas.lib.api.IConfigurationConstants; import at.gv.egiz.pdfas.lib.api.PdfAs; @@ -100,9 +102,10 @@ public class PdfAsImpl implements PdfAs, IConfigurationConstants { logger.info("Initializing PDF-AS with config: " + cfgFile.getPath()); this.settings = new Settings(cfgFile); } - + public PdfAsImpl(ISettings cfgObject) { - logger.info("Initializing PDF-AS with config: " + cfgObject.getClass().getName()); + logger.info("Initializing PDF-AS with config: " + + cfgObject.getClass().getName()); this.settings = cfgObject; } @@ -123,14 +126,10 @@ public class PdfAsImpl implements PdfAs, IConfigurationConstants { } } - if (parameter.getDataSource() == null - || parameter.getDataSource().getByteData() == null) { + if (parameter.getDataSource() == null) { throw new PdfAsValidationException("error.pdf.sig.10", null); } - if (parameter.getOutput() == null) { - throw new PdfAsValidationException("error.pdf.sig.11", null); - } } private void verifyVerifyParameter(VerifyParameter parameter) @@ -140,8 +139,7 @@ public class PdfAsImpl implements PdfAs, IConfigurationConstants { throw new PdfAsSettingsException("Invalid settings object!"); } - if (parameter.getDataSource() == null - || parameter.getDataSource().getByteData() == null) { + if (parameter.getDataSource() == null) { throw new PdfAsValidationException("error.pdf.verify.01", null); } } @@ -162,8 +160,8 @@ public class PdfAsImpl implements PdfAs, IConfigurationConstants { status = new OperationStatus(settings, parameter); // set Original PDF Document Data - status.getPdfObject().setOriginalDocument( - parameter.getDataSource().getByteData()); + status.getPdfObject() + .setOriginalDocument(parameter.getDataSource()); PDDocument doc = status.getPdfObject().getDocument(); PDFUtils.checkPDFPermissions(doc); @@ -227,33 +225,32 @@ public class PdfAsImpl implements PdfAs, IConfigurationConstants { List<VerifyResult> result = new ArrayList<VerifyResult>(); ISettings settings = (ISettings) parameter.getConfiguration(); VerifierDispatcher verifier = new VerifierDispatcher(settings); - doc = PDDocument.load(new ByteArrayInputStream(parameter - .getDataSource().getByteData())); + doc = PDDocument.load(parameter.getDataSource().getInputStream()); COSDictionary trailer = doc.getDocument().getTrailer(); - if(trailer == null) { + if (trailer == null) { // No signatures ... return result; } COSDictionary root = (COSDictionary) trailer .getDictionaryObject(COSName.ROOT); - if(root == null) { + if (root == null) { // No signatures ... return result; } COSDictionary acroForm = (COSDictionary) root .getDictionaryObject(COSName.ACRO_FORM); - if(acroForm == null) { + if (acroForm == null) { // No signatures ... return result; } COSArray fields = (COSArray) acroForm .getDictionaryObject(COSName.FIELDS); - if(fields == null) { + if (fields == null) { // No signatures ... return result; } - + int lastSig = -1; for (int i = 0; i < fields.size(); i++) { COSDictionary field = (COSDictionary) fields.getObject(i); @@ -262,6 +259,9 @@ public class PdfAsImpl implements PdfAs, IConfigurationConstants { lastSig = i; } } + + byte[] inputData = IOUtils.toByteArray(parameter.getDataSource() + .getInputStream()); for (int i = 0; i < fields.size(); i++) { COSDictionary field = (COSDictionary) fields.getObject(i); @@ -273,11 +273,11 @@ public class PdfAsImpl implements PdfAs, IConfigurationConstants { // verify only specific siganture! verifyThis = signatureToVerify == currentSignature; } - - if(signatureToVerify == -2) { + + if (signatureToVerify == -2) { verifyThis = i == lastSig; } - + if (verifyThis) { logger.trace("Found Signature: "); COSBase base = field.getDictionaryObject("V"); @@ -303,13 +303,13 @@ public class PdfAsImpl implements PdfAs, IConfigurationConstants { COSString content = (COSString) dict .getDictionaryObject("Contents"); - + ByteArrayOutputStream contentData = new ByteArrayOutputStream(); for (int j = 0; j < bytes.length; j = j + 2) { int offset = bytes[j]; int length = bytes[j + 1]; - contentData.write(parameter.getDataSource() - .getByteData(), offset, length); + + contentData.write(inputData, offset, length); } contentData.close(); @@ -317,13 +317,17 @@ public class PdfAsImpl implements PdfAs, IConfigurationConstants { dict.getNameAsString("Filter"), dict.getNameAsString("SubFilter")); - IVerifier lvlVerifier = verifier.getVerifierByLevel(parameter.getSignatureVerificationLevel()); - lvlVerifier.setConfiguration(parameter.getConfiguration()); + IVerifier lvlVerifier = verifier + .getVerifierByLevel(parameter + .getSignatureVerificationLevel()); + lvlVerifier.setConfiguration(parameter + .getConfiguration()); if (verifyFilter != null) { List<VerifyResult> results = verifyFilter.verify( contentData.toByteArray(), content.getBytes(), - parameter.getVerificationTime(), bytes, lvlVerifier); + parameter.getVerificationTime(), bytes, + lvlVerifier); if (results != null && !results.isEmpty()) { result.addAll(results); } @@ -402,7 +406,7 @@ public class PdfAsImpl implements PdfAs, IConfigurationConstants { // set Original PDF Document Data status.getPdfObject().setOriginalDocument( - status.getSignParamter().getDataSource().getByteData()); + status.getSignParamter().getDataSource()); // STAMPER! // stampPdf(status); @@ -452,16 +456,20 @@ public class PdfAsImpl implements PdfAs, IConfigurationConstants { String signature = new COSString(request.getSignature()) .getHexString(); byte[] pdfSignature = signature.getBytes(); - //byte[] input = PDFUtils.blackOutSignature(status.getPdfObject().getSignedDocument(), - // request.getSignatureDataByteRange()); - VerifyResult verifyResult = SignatureUtils.verifySignature(request.getSignature(), request.getSignatureData()); - RequestedSignature requestedSignature = request.getStatus().getRequestedSignature(); - - if(!StreamUtils.dataCompare(requestedSignature.getCertificate().getFingerprintSHA(), - ((X509Certificate)verifyResult.getSignerCertificate()).getFingerprintSHA())) { + // byte[] input = + // PDFUtils.blackOutSignature(status.getPdfObject().getSignedDocument(), + // request.getSignatureDataByteRange()); + VerifyResult verifyResult = SignatureUtils.verifySignature( + request.getSignature(), request.getSignatureData()); + RequestedSignature requestedSignature = request.getStatus() + .getRequestedSignature(); + + if (!StreamUtils.dataCompare(requestedSignature.getCertificate() + .getFingerprintSHA(), ((X509Certificate) verifyResult + .getSignerCertificate()).getFingerprintSHA())) { throw new PdfAsSignatureException("Certificates missmatch!"); } - + for (int i = 0; i < pdfSignature.length; i++) { status.getPdfObject().getSignedDocument()[offset + i] = pdfSignature[i]; } @@ -501,14 +509,8 @@ public class PdfAsImpl implements PdfAs, IConfigurationConstants { throws IOException { // ================================================================ // Create SignResult - SignResultImpl result = new SignResultImpl(status.getSignParamter() - .getOutput()); - OutputStream outputStream = result.getOutputDocument() - .createOutputStream(); - - outputStream.write(status.getPdfObject().getSignedDocument()); - - outputStream.close(); + SignResultImpl result = new SignResultImpl(new ByteArrayInputStream( + status.getPdfObject().getSignedDocument())); result.setSignerCertificate(status.getRequestedSignature() .getCertificate()); @@ -518,28 +520,28 @@ public class PdfAsImpl implements PdfAs, IConfigurationConstants { return result; } - public Image generateVisibleSignaturePreview(SignParameter parameter, X509Certificate cert, int resolution) - throws PdfAsException { - + public Image generateVisibleSignaturePreview(SignParameter parameter, + X509Certificate cert, int resolution) throws PdfAsException { + OperationStatus status = null; try { // Status initialization if (!(parameter.getConfiguration() instanceof ISettings)) { throw new PdfAsSettingsException("Invalid settings object!"); } - + ISettings settings = (ISettings) parameter.getConfiguration(); status = new OperationStatus(settings, parameter); - + RequestedSignature requestedSignature = new RequestedSignature( status); requestedSignature.setCertificate(cert); - + if (!requestedSignature.isVisual()) { logger.warn("Profile is invisible so not block image is generated"); return null; } - + PDFObject pdfObject = status.getPdfObject(); PDDocument origDoc = new PDDocument(); @@ -547,42 +549,41 @@ public class PdfAsImpl implements PdfAs, IConfigurationConstants { ByteArrayOutputStream baos = new ByteArrayOutputStream(); origDoc.save(baos); baos.close(); - - pdfObject.setOriginalDocument(baos.toByteArray()); + + pdfObject.setOriginalDocument(new ByteArrayDataSource(baos.toByteArray())); SignatureProfileSettings signatureProfileSettings = TableFactory .createProfile(requestedSignature.getSignatureProfileID(), pdfObject.getStatus().getSettings()); - + // create Table describtion - Table main = TableFactory.createSigTable( - signatureProfileSettings, MAIN, pdfObject.getStatus(), - requestedSignature); + Table main = TableFactory.createSigTable(signatureProfileSettings, + MAIN, pdfObject.getStatus(), requestedSignature); - IPDFStamper stamper = StamperFactory - .createDefaultStamper(pdfObject.getStatus() - .getSettings()); + IPDFStamper stamper = StamperFactory.createDefaultStamper(pdfObject + .getStatus().getSettings()); IPDFVisualObject visualObject = stamper.createVisualPDFObject( - pdfObject, main); - + pdfObject, main); + SignatureProfileConfiguration signatureProfileConfiguration = pdfObject .getStatus().getSignatureProfileConfiguration( requestedSignature.getSignatureProfileID()); - + String signaturePosString = signatureProfileConfiguration .getDefaultPositioning(); PositioningInstruction positioningInstruction = null; - if(signaturePosString != null) { - positioningInstruction = Positioning.determineTablePositioning(new TablePos(signaturePosString), "", origDoc, + if (signaturePosString != null) { + positioningInstruction = Positioning.determineTablePositioning( + new TablePos(signaturePosString), "", origDoc, visualObject, false); } else { - positioningInstruction = Positioning.determineTablePositioning(new TablePos(), "", origDoc, - visualObject, false); + positioningInstruction = Positioning.determineTablePositioning( + new TablePos(), "", origDoc, visualObject, false); } - + origDoc.close(); - + SignaturePositionImpl position = new SignaturePositionImpl(); position.setX(positioningInstruction.getX()); position.setY(positioningInstruction.getY()); @@ -591,44 +592,49 @@ public class PdfAsImpl implements PdfAs, IConfigurationConstants { position.setWidth(visualObject.getWidth()); requestedSignature.setSignaturePosition(position); - + PDFAsVisualSignatureProperties properties = new PDFAsVisualSignatureProperties( - pdfObject.getStatus().getSettings(), pdfObject, (PdfBoxVisualObject) visualObject, - positioningInstruction); + pdfObject.getStatus().getSettings(), pdfObject, + (PdfBoxVisualObject) visualObject, positioningInstruction); properties.buildSignature(); - PDDocument visualDoc = PDDocument.load(properties.getVisibleSignature()); - //PDPageable pageable = new PDPageable(visualDoc); + PDDocument visualDoc = PDDocument.load(properties + .getVisibleSignature()); + // PDPageable pageable = new PDPageable(visualDoc); List<PDPage> pages = new ArrayList<PDPage>(); visualDoc.getDocumentCatalog().getPages().getAllKids(pages); - + PDPage firstPage = pages.get(0); - + float stdRes = 72; float targetRes = resolution; float factor = targetRes / stdRes; - - BufferedImage outputImage = firstPage.convertToImage(BufferedImage.TYPE_4BYTE_ABGR, (int)targetRes); - - BufferedImage cutOut = new BufferedImage((int)(position.getWidth() * factor), (int)(position.getHeight() * factor), + + BufferedImage outputImage = firstPage.convertToImage( + BufferedImage.TYPE_4BYTE_ABGR, (int) targetRes); + + BufferedImage cutOut = new BufferedImage( + (int) (position.getWidth() * factor), + (int) (position.getHeight() * factor), BufferedImage.TYPE_4BYTE_ABGR); - + Graphics2D graphics = (Graphics2D) cutOut.getGraphics(); - - graphics.drawImage(outputImage, 0, 0, cutOut.getWidth(), cutOut.getHeight(), - (int)(1 * factor), - (int)(outputImage.getHeight() - ((position.getHeight() + 1) * factor)), - (int)((1 + position.getWidth()) * factor), - (int)(outputImage.getHeight() - ((position.getHeight() + 1) * factor) + (position.getHeight() * factor)), - null); + + graphics.drawImage(outputImage, 0, 0, cutOut.getWidth(), cutOut + .getHeight(), (int) (1 * factor), (int) (outputImage + .getHeight() - ((position.getHeight() + 1) * factor)), + (int) ((1 + position.getWidth()) * factor), + (int) (outputImage.getHeight() + - ((position.getHeight() + 1) * factor) + (position + .getHeight() * factor)), null); return cutOut; - } catch(PdfAsException e) { + } catch (PdfAsException e) { logger.error("PDF-AS Exception", e); throw e; - } catch(Throwable e) { + } catch (Throwable e) { logger.error("Throwable Exception", e); throw new PdfAsException("", e); } - + } } diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/PdfAsParameterImpl.java b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/PdfAsParameterImpl.java index 362715a3..7a141525 100644 --- a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/PdfAsParameterImpl.java +++ b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/PdfAsParameterImpl.java @@ -23,8 +23,9 @@ ******************************************************************************/ package at.gv.egiz.pdfas.lib.impl; +import javax.activation.DataSource; + import at.gv.egiz.pdfas.lib.api.Configuration; -import at.gv.egiz.pdfas.lib.api.DataSource; import at.gv.egiz.pdfas.lib.api.PdfAsParameter; public class PdfAsParameterImpl implements PdfAsParameter { 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 ddf2eaef..f547b2b0 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,16 +23,16 @@ ******************************************************************************/ package at.gv.egiz.pdfas.lib.impl; +import javax.activation.DataSource; + import at.gv.egiz.pdfas.lib.api.Configuration; -import at.gv.egiz.pdfas.lib.api.DataSink; -import at.gv.egiz.pdfas.lib.api.DataSource; import at.gv.egiz.pdfas.lib.api.sign.IPlainSigner; import at.gv.egiz.pdfas.lib.api.sign.SignParameter; public class SignParameterImpl extends PdfAsParameterImpl implements SignParameter { protected String signatureProfileId = null; protected String signaturePosition = null; - protected DataSink output = null; + protected DataSource output = null; protected IPlainSigner signer = null; public SignParameterImpl(Configuration configuration, @@ -58,11 +58,11 @@ public class SignParameterImpl extends PdfAsParameterImpl implements SignParamet this.signaturePosition = signaturePosition; } - public void setOutput(DataSink output) { + public void setOutput(DataSource output) { this.output = output; } - public DataSink getOutput() { + public DataSource getOutput() { return this.output; } 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 87fb2881..1a0e6490 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,23 +23,23 @@ ******************************************************************************/ package at.gv.egiz.pdfas.lib.impl; +import java.io.InputStream; import java.security.cert.X509Certificate; -import at.gv.egiz.pdfas.lib.api.DataSink; import at.gv.egiz.pdfas.lib.api.SignaturePosition; import at.gv.egiz.pdfas.lib.api.sign.SignResult; public class SignResultImpl implements SignResult { - protected DataSink dataSink; + protected InputStream dataSink; protected X509Certificate certificate; protected SignaturePosition position; - public SignResultImpl(DataSink dataSink) { + public SignResultImpl(InputStream dataSink) { this.dataSink = dataSink; } - public DataSink getOutputDocument() { + public InputStream getOutputDocument() { return this.dataSink; } diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/VerifyParameterImpl.java b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/VerifyParameterImpl.java index 166c17e0..ea1da9e7 100644 --- a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/VerifyParameterImpl.java +++ b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/VerifyParameterImpl.java @@ -25,8 +25,9 @@ package at.gv.egiz.pdfas.lib.impl; import java.util.Date; +import javax.activation.DataSource; + import at.gv.egiz.pdfas.lib.api.Configuration; -import at.gv.egiz.pdfas.lib.api.DataSource; import at.gv.egiz.pdfas.lib.api.verify.VerifyParameter; public class VerifyParameterImpl extends PdfAsParameterImpl implements VerifyParameter { diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/signing/pdfbox/PADESPDFBOXSigner.java b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/signing/pdfbox/PADESPDFBOXSigner.java index 6d17e919..a129f18a 100644 --- a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/signing/pdfbox/PADESPDFBOXSigner.java +++ b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/signing/pdfbox/PADESPDFBOXSigner.java @@ -32,6 +32,7 @@ import java.util.ArrayList; import java.util.Calendar; import java.util.List; +import org.apache.commons.io.IOUtils; import org.apache.pdfbox.cos.COSBase; import org.apache.pdfbox.cos.COSDictionary; import org.apache.pdfbox.cos.COSName; @@ -42,11 +43,8 @@ import org.apache.pdfbox.pdmodel.PDDocumentCatalog; import org.apache.pdfbox.pdmodel.PDPage; import org.apache.pdfbox.pdmodel.PDPageNode; import org.apache.pdfbox.pdmodel.PDResources; -import org.apache.pdfbox.pdmodel.documentinterchange.logicalstructure.PDStructureElement; -import org.apache.pdfbox.pdmodel.documentinterchange.logicalstructure.PDStructureTreeRoot; import org.apache.pdfbox.pdmodel.graphics.color.PDOutputIntent; import org.apache.pdfbox.pdmodel.graphics.xobject.PDJpeg; -import org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceCharacteristicsDictionary; import org.apache.pdfbox.pdmodel.interactive.digitalsignature.PDSignature; import org.apache.pdfbox.pdmodel.interactive.digitalsignature.SignatureOptions; import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm; @@ -75,7 +73,6 @@ import at.gv.egiz.pdfas.lib.impl.stamping.TableFactory; import at.gv.egiz.pdfas.lib.impl.stamping.ValueResolver; import at.gv.egiz.pdfas.lib.impl.stamping.pdfbox.PDFAsVisualSignatureProperties; import at.gv.egiz.pdfas.lib.impl.stamping.pdfbox.PdfBoxVisualObject; -import at.gv.egiz.pdfas.lib.impl.stamping.pdfbox.tagging.PDFBoxTaggingUtils; import at.gv.egiz.pdfas.lib.impl.status.PDFObject; import at.gv.egiz.pdfas.lib.impl.status.RequestedSignature; import at.gv.egiz.pdfas.lib.util.SignatureUtils; @@ -100,7 +97,7 @@ public class PADESPDFBOXSigner implements IPdfSigner, IConfigurationConstants { // write to temporary file FileOutputStream fos = new FileOutputStream(new File(fisTmpFile)); - fos.write(pdfObject.getOriginalDocument()); + IOUtils.copy(pdfObject.getOriginalDocument().getInputStream(), fos); FileInputStream fis = new FileInputStream(new File(fisTmpFile)); diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/status/PDFObject.java b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/status/PDFObject.java index 628c87a1..f69c20a4 100644 --- a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/status/PDFObject.java +++ b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/status/PDFObject.java @@ -23,8 +23,10 @@ ******************************************************************************/ package at.gv.egiz.pdfas.lib.impl.status; -import java.io.ByteArrayInputStream; import java.io.IOException; +import java.io.OutputStream; + +import javax.activation.DataSource; import org.apache.pdfbox.pdmodel.PDDocument; @@ -33,7 +35,7 @@ public class PDFObject { private OperationStatus status; private PDDocument doc; - private byte[] originalDocument; + private DataSource originalDocument; private byte[] signedDocument; public PDFObject(OperationStatus operationStatus) { @@ -60,16 +62,16 @@ public class PDFObject { } } - public byte[] getOriginalDocument() { + public DataSource getOriginalDocument() { return originalDocument; } - public void setOriginalDocument(byte[] originalDocument) throws IOException { + public void setOriginalDocument(DataSource originalDocument) throws IOException { this.originalDocument = originalDocument; if(doc != null) { doc.close(); } - this.doc = PDDocument.load(new ByteArrayInputStream(this.originalDocument)); + this.doc = PDDocument.load(this.originalDocument.getInputStream()); if(this.doc != null) { this.doc.getDocument().setWarnMissingClose(false); } diff --git a/pdf-as-lib/src/main/resources/config/config.zip b/pdf-as-lib/src/main/resources/config/config.zip Binary files differindex 2810b9d2..71070c84 100644 --- a/pdf-as-lib/src/main/resources/config/config.zip +++ b/pdf-as-lib/src/main/resources/config/config.zip 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 b1a290e4..f78319c6 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 @@ -6,7 +6,6 @@ import java.awt.Image; import java.awt.image.RenderedImage; import java.io.File; import java.io.FileInputStream; -import java.io.FileOutputStream; import java.util.ArrayList; import java.util.Iterator; import java.util.List; @@ -14,15 +13,10 @@ import java.util.List; import javax.imageio.ImageIO; import at.gv.egiz.pdfas.common.settings.ISettings; -import at.gv.egiz.pdfas.lib.api.ByteArrayDataSink; -import at.gv.egiz.pdfas.lib.api.ByteArrayDataSource; import at.gv.egiz.pdfas.lib.api.Configuration; -import at.gv.egiz.pdfas.lib.api.DataSource; import at.gv.egiz.pdfas.lib.api.PdfAs; import at.gv.egiz.pdfas.lib.api.PdfAsFactory; -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; public class PDFToImage { 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 64b302c4..e4ff9325 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 @@ -11,6 +11,8 @@ import java.security.cert.CertificateException; import java.util.Map; import java.util.UUID; +import javax.activation.DataSource; + import org.apache.commons.io.IOUtils; import org.junit.Rule; import org.junit.rules.TestRule; @@ -21,14 +23,13 @@ import at.gv.egiz.param_tests.provider.BaseSignatureDataProvider; import at.gv.egiz.param_tests.provider.BaseSignatureTestData; import at.gv.egiz.pdfas.common.exceptions.PdfAsException; import at.gv.egiz.pdfas.common.utils.StreamUtils; -import at.gv.egiz.pdfas.lib.api.ByteArrayDataSink; import at.gv.egiz.pdfas.lib.api.ByteArrayDataSource; import at.gv.egiz.pdfas.lib.api.Configuration; -import at.gv.egiz.pdfas.lib.api.DataSource; import at.gv.egiz.pdfas.lib.api.PdfAs; import at.gv.egiz.pdfas.lib.api.PdfAsFactory; 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.moa.MOAConnector; import at.gv.egiz.pdfas.sigs.pades.PAdESSigner; import at.gv.egiz.pdfas.sigs.pades.PAdESSignerKeystore; @@ -94,7 +95,6 @@ public class SignatureTest { DataSource dataSource = new ByteArrayDataSource( StreamUtils.inputStreamToByteArray(new FileInputStream( inputFile))); - ByteArrayDataSink dataSink = new ByteArrayDataSink(); pdfAs = null; pdfAs = PdfAsFactory.createPdfAs(new File(baseTestData @@ -148,7 +148,6 @@ public class SignatureTest { slConnector = new PAdESSigner(new BKUSLConnector(configuration)); } - signParameter.setOutput(dataSink); signParameter.setPlainSigner(slConnector); signParameter.setDataSource(dataSource); // this is not needed for PDF-A test @@ -157,11 +156,11 @@ 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); + SignResult result = pdfAs.sign(signParameter); FileOutputStream fos = null; try { fos = new FileOutputStream(outputPdfFile, false); - fos.write(dataSink.getData()); + 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 331e851a..0a9a7e5e 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 @@ -48,6 +48,7 @@ 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; @@ -57,10 +58,8 @@ import at.gv.egiz.pdfas.api.ws.PDFASSignParameters.Connector; import at.gv.egiz.pdfas.api.ws.PDFASSignResponse; import at.gv.egiz.pdfas.api.ws.PDFASVerificationResponse; import at.gv.egiz.pdfas.common.exceptions.PdfAsException; -import at.gv.egiz.pdfas.lib.api.ByteArrayDataSink; import at.gv.egiz.pdfas.lib.api.ByteArrayDataSource; import at.gv.egiz.pdfas.lib.api.Configuration; -import at.gv.egiz.pdfas.lib.api.DataSink; import at.gv.egiz.pdfas.lib.api.PdfAs; import at.gv.egiz.pdfas.lib.api.PdfAsFactory; import at.gv.egiz.pdfas.lib.api.StatusRequest; @@ -112,14 +111,14 @@ public class PdfAsHelper { private static final String SIGNATURE_DATA_HASH = "SIGNATURE_DATA_HASH"; private static final String SIGNATURE_ACTIVE = "SIGNATURE_ACTIVE"; private static final String VERIFICATION_RESULT = "VERIFICATION_RESULT"; - + private static final Logger logger = LoggerFactory .getLogger(PdfAsHelper.class); private static PdfAs pdfAs; private static ObjectFactory of = new ObjectFactory(); private static Configuration pdfAsConfig; - + static { reloadConfig(); } @@ -135,11 +134,11 @@ public class PdfAsHelper { pdfAsConfig = pdfAs.getConfiguration(); logger.info("Creating PDF-AS done"); } - + public static Configuration getPdfAsConfig() { return pdfAsConfig; } - + private static void validatePdfSize(HttpServletRequest request, HttpServletResponse response, byte[] pdfData) throws PdfAsWebException { @@ -368,15 +367,12 @@ public class PdfAsHelper { signParameter.setSignatureProfileId(PdfAsParameterExtractor .getSigType(request)); - ByteArrayDataSink output = new ByteArrayDataSink(); - signParameter.setOutput(output); - // set Signature Position signParameter.setSignaturePosition(buildPosString(request, response)); - pdfAs.sign(signParameter); + SignResult result = pdfAs.sign(signParameter); - return output.getData(); + return IOUtils.toByteArray(result.getOutputDocument()); } /** @@ -426,24 +422,22 @@ public class PdfAsHelper { // set Signature Profile (null use default ...) signParameter.setSignatureProfileId(params.getProfile()); - ByteArrayDataSink output = new ByteArrayDataSink(); - signParameter.setOutput(output); - // set Signature Position signParameter.setSignaturePosition(params.getPosition()); SignResult signResult = pdfAs.sign(signParameter); PDFASSignResponse signResponse = new PDFASSignResponse(); - signResponse.setSignedPDF(output.getData()); - + signResponse.setSignedPDF(IOUtils.toByteArray(signResult + .getOutputDocument())); + PDFASVerificationResponse verResponse = new PDFASVerificationResponse(); - + verResponse.setSignerCertificate(signResult.getSignerCertificate() .getEncoded()); signResponse.setVerificationResponse(verResponse); - + return signResponse; } @@ -497,9 +491,6 @@ public class PdfAsHelper { // set Signature Profile (null use default ...) signParameter.setSignatureProfileId(profile); - ByteArrayDataSink dataSink = new ByteArrayDataSink(); - signParameter.setOutput(dataSink); - // set Signature Position signParameter.setSignaturePosition(position); @@ -679,38 +670,34 @@ public class PdfAsHelper { logger.debug("Document ready!"); SignResult result = pdfAs.finishSign(statusRequest); - DataSink output = result.getOutputDocument(); - if (output instanceof ByteArrayDataSink) { - ByteArrayDataSink byteDataSink = (ByteArrayDataSink) output; - byte[] signedPdf = byteDataSink.getData(); - - PDFASVerificationResponse verResponse = new PDFASVerificationResponse(); - List<VerifyResult> verResults = PdfAsHelper - .synchornousVerify(signedPdf, -2, - PdfAsHelper.getVerificationLevel(request)); - - if (verResults.size() != 1) { - throw new WebServiceException( - "Document verification failed!"); - } - VerifyResult verifyResult = verResults.get(0); - - verResponse.setCertificateCode(verifyResult - .getCertificateCheck().getCode()); - verResponse.setValueCode(verifyResult.getValueCheckCode() - .getCode()); - - PdfAsHelper.setPDFASVerificationResponse(request, verResponse); - PdfAsHelper.setSignedPdf(request, response, signedPdf); - PdfAsHelper.gotoProvidePdf(context, request, response); - - String signerCert = Base64.encodeBase64String(result - .getSignerCertificate().getEncoded()); - - PdfAsHelper.setSignerCertificate(request, signerCert); - } else { - throw new PdfAsWebException("No Signature data available"); + + byte[] signedPdf = IOUtils.toByteArray(result + .getOutputDocument()); + + PDFASVerificationResponse verResponse = new PDFASVerificationResponse(); + List<VerifyResult> verResults = PdfAsHelper.synchornousVerify( + signedPdf, -2, + PdfAsHelper.getVerificationLevel(request)); + + if (verResults.size() != 1) { + throw new WebServiceException( + "Document verification failed!"); } + VerifyResult verifyResult = verResults.get(0); + + verResponse.setCertificateCode(verifyResult + .getCertificateCheck().getCode()); + verResponse.setValueCode(verifyResult.getValueCheckCode() + .getCode()); + + PdfAsHelper.setPDFASVerificationResponse(request, verResponse); + PdfAsHelper.setSignedPdf(request, response, signedPdf); + PdfAsHelper.gotoProvidePdf(context, request, response); + + String signerCert = Base64.encodeBase64String(result + .getSignerCertificate().getEncoded()); + + PdfAsHelper.setSignerCertificate(request, signerCert); } else { throw new PdfAsWebException("Invalid state!"); @@ -752,7 +739,7 @@ public class PdfAsHelper { .getResource("/template_generic_param.html"))); return xml; } - + public static String getInvokeRedirectTemplateSL() throws IOException { String xml = FileUtils.readFileToString(FileUtils .toFile(PdfAsHelper.class @@ -874,12 +861,10 @@ public class PdfAsHelper { Object obj = session.getAttribute(PDF_INVOKE_URL); return obj == null ? null : obj.toString(); } - + public static void setInvokeTarget(HttpServletRequest request, HttpServletResponse response, String url) { - - - + HttpSession session = request.getSession(); session.setAttribute(PDF_INVOKE_TARGET, url); logger.debug("External Invoke TARGET: " + url); @@ -1046,7 +1031,7 @@ public class PdfAsHelper { } return SignatureVerificationLevel.INTEGRITY_ONLY_VERIFICATION; } - + public static void setPDFASVerificationResponse(HttpServletRequest request, PDFASVerificationResponse resp) { HttpSession session = request.getSession(); |