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 /pdf-as-lib/src/main | |
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
Diffstat (limited to 'pdf-as-lib/src/main')
16 files changed, 155 insertions, 294 deletions
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 |