pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From til...@apache.org
Subject svn commit: r1792091 - in /pdfbox/trunk: examples/src/main/java/org/apache/pdfbox/examples/signature/ pdfbox/src/main/java/org/apache/pdfbox/pdmodel/fdf/ pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/digitalsignature/
Date Thu, 20 Apr 2017 17:06:33 GMT
Author: tilman
Date: Thu Apr 20 17:06:33 2017
New Revision: 1792091

URL: http://svn.apache.org/viewvc?rev=1792091&view=rev
Log:
PDFBOX-2852: use JDK7 syntax

Modified:
    pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/signature/CreateSignatureBase.java
    pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/signature/ShowSignature.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/fdf/XMLUtil.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/digitalsignature/PDSignature.java

Modified: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/signature/CreateSignatureBase.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/signature/CreateSignatureBase.java?rev=1792091&r1=1792090&r2=1792091&view=diff
==============================================================================
--- pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/signature/CreateSignatureBase.java
(original)
+++ pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/signature/CreateSignatureBase.java
Thu Apr 20 17:06:33 2017
@@ -202,6 +202,8 @@ public abstract class CreateSignatureBas
      * This method is for internal use only.
      *
      * Use your favorite cryptographic library to implement PKCS #7 signature creation.
+     *
+     * @throws IOException
      */
     @Override
     public byte[] sign(InputStream content) throws IOException
@@ -225,19 +227,7 @@ public abstract class CreateSignatureBas
             }
             return signedData.getEncoded();
         }
-        catch (GeneralSecurityException e)
-        {
-            throw new IOException(e);
-        }
-        catch (CMSException e)
-        {
-            throw new IOException(e);
-        }
-        catch (TSPException e)
-        {
-            throw new IOException(e);
-        }
-        catch (OperatorCreationException e)
+        catch (GeneralSecurityException | CMSException | TSPException | OperatorCreationException
e)
         {
             throw new IOException(e);
         }

Modified: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/signature/ShowSignature.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/signature/ShowSignature.java?rev=1792091&r1=1792090&r2=1792091&view=diff
==============================================================================
--- pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/signature/ShowSignature.java
(original)
+++ pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/signature/ShowSignature.java
Thu Apr 20 17:06:33 2017
@@ -56,8 +56,8 @@ import org.bouncycastle.util.StoreExcept
  */
 public final class ShowSignature
 {
-    private SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
-    
+    private final SimpleDateFormat SDF = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
+
     private ShowSignature()
     {
     }
@@ -94,10 +94,8 @@ public final class ShowSignature
         {
             String password = args[0];
             String infile = args[1];
-            PDDocument document = null;
-            try
+            try (PDDocument document = PDDocument.load(new File(infile), password))
             {
-                document = PDDocument.load(new File(infile), password);
                 for (PDSignature sig : document.getSignatureDictionaries())
                 {
                     COSDictionary sigDict = sig.getCOSObject();
@@ -117,48 +115,50 @@ public final class ShowSignature
 
                     System.out.println("Signature found");
                     System.out.println("Name:     " + sig.getName());
-                    System.out.println("Modified: " + sdf.format(sig.getSignDate().getTime()));
+                    System.out.println("Modified: " + SDF.format(sig.getSignDate().getTime()));
                     String subFilter = sig.getSubFilter();
                     if (subFilter != null)
                     {
-                        if (subFilter.equals("adbe.pkcs7.detached"))
-                        {
-                            verifyPKCS7(buf, contents, sig);
-                            
-                            //TODO check certificate chain, revocation lists, timestamp...
-                        }
-                        else if (subFilter.equals("adbe.pkcs7.sha1"))
-                        {
-                            // example: PDFBOX-1452.pdf
-                            COSString certString = (COSString) sigDict.getDictionaryObject(
-                                    COSName.CONTENTS);
-                            byte[] certData = certString.getBytes();
-                            CertificateFactory factory = CertificateFactory.getInstance("X.509");
-                            ByteArrayInputStream certStream = new ByteArrayInputStream(certData);
-                            Collection<? extends Certificate> certs = factory.generateCertificates(certStream);
-                            System.out.println("certs=" + certs);
-
-                            byte[] hash = MessageDigest.getInstance("SHA1").digest(buf);
-                            verifyPKCS7(hash, contents, sig);
-                            
-                            //TODO check certificate chain, revocation lists, timestamp...
-                        }
-                        else if (subFilter.equals("adbe.x509.rsa_sha1"))
+                        switch (subFilter)
                         {
-                            // example: PDFBOX-2693.pdf
-                            COSString certString = (COSString) sigDict.getDictionaryObject(
-                                    COSName.getPDFName("Cert"));
-                            byte[] certData = certString.getBytes();
-                            CertificateFactory factory = CertificateFactory.getInstance("X.509");
-                            ByteArrayInputStream certStream = new ByteArrayInputStream(certData);
-                            Collection<? extends Certificate> certs = factory.generateCertificates(certStream);
-                            System.out.println("certs=" + certs);
-                            
-                            //TODO verify signature
-                        }
-                        else
-                        {
-                            System.err.println("Unknown certificate type: " + subFilter);
+                            case "adbe.pkcs7.detached":
+                                verifyPKCS7(buf, contents, sig);
+
+                                //TODO check certificate chain, revocation lists, timestamp...
+                                break;
+                            case "adbe.pkcs7.sha1":
+                            {
+                                // example: PDFBOX-1452.pdf
+                                COSString certString = (COSString) sigDict.getDictionaryObject(
+                                        COSName.CONTENTS);
+                                byte[] certData = certString.getBytes();
+                                CertificateFactory factory = CertificateFactory.getInstance("X.509");
+                                ByteArrayInputStream certStream = new ByteArrayInputStream(certData);
+                                Collection<? extends Certificate> certs = factory.generateCertificates(certStream);
+                                System.out.println("certs=" + certs);
+                                byte[] hash = MessageDigest.getInstance("SHA1").digest(buf);
+                                verifyPKCS7(hash, contents, sig);
+
+                                //TODO check certificate chain, revocation lists, timestamp...
+                                break;
+                            }
+                            case "adbe.x509.rsa_sha1":
+                            {
+                                // example: PDFBOX-2693.pdf
+                                COSString certString = (COSString) sigDict.getDictionaryObject(
+                                        COSName.getPDFName("Cert"));
+                                byte[] certData = certString.getBytes();
+                                CertificateFactory factory = CertificateFactory.getInstance("X.509");
+                                ByteArrayInputStream certStream = new ByteArrayInputStream(certData);
+                                Collection<? extends Certificate> certs = factory.generateCertificates(certStream);
+                                System.out.println("certs=" + certs);
+
+                                //TODO verify signature
+                                break;
+                            }
+                            default:
+                                System.err.println("Unknown certificate type: " + subFilter);
+                                break;
                         }
                     }
                     else
@@ -167,21 +167,10 @@ public final class ShowSignature
                     }
                 }
             }
-            catch (CMSException ex)
-            {
-                throw new IOException(ex);
-            }
-            catch (OperatorCreationException ex)
+            catch (CMSException | OperatorCreationException ex)
             {
                 throw new IOException(ex);
             }
-            finally
-            {
-                if (document != null)
-                {
-                    document.close();
-                }
-            }
         }
     }
 

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/fdf/XMLUtil.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/fdf/XMLUtil.java?rev=1792091&r1=1792090&r2=1792091&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/fdf/XMLUtil.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/fdf/XMLUtil.java Thu Apr 20
17:06:33 2017
@@ -67,11 +67,7 @@ final class XMLUtil
             DocumentBuilder builder = builderFactory.newDocumentBuilder();
             return builder.parse(is);
         }
-        catch (FactoryConfigurationError e)
-        {
-            throw new IOException(e.getMessage(), e);
-        }
-        catch (ParserConfigurationException | SAXException e)
+        catch (FactoryConfigurationError | ParserConfigurationException | SAXException e)
         {
             throw new IOException(e.getMessage(), e);
         }

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/digitalsignature/PDSignature.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/digitalsignature/PDSignature.java?rev=1792091&r1=1792090&r2=1792091&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/digitalsignature/PDSignature.java
(original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/digitalsignature/PDSignature.java
Thu Apr 20 17:06:33 2017
@@ -379,20 +379,10 @@ public class PDSignature implements COSO
      */
     public byte[] getSignedContent(InputStream pdfFile) throws IOException
     {
-        COSFilterInputStream fis=null;
-
-        try
+        try (COSFilterInputStream fis = new COSFilterInputStream(pdfFile, getByteRange()))
         {
-            fis = new COSFilterInputStream(pdfFile,getByteRange());
             return fis.toByteArray();
         }
-        finally
-        {
-            if (fis != null)
-            {
-                fis.close();
-            }
-        }
     }
 
     /**
@@ -404,19 +394,10 @@ public class PDSignature implements COSO
      */
     public byte[] getSignedContent(byte[] pdfFile) throws IOException
     {
-        COSFilterInputStream fis=null;
-        try
+        try (COSFilterInputStream fis = new COSFilterInputStream(pdfFile, getByteRange()))
         {
-            fis = new COSFilterInputStream(pdfFile,getByteRange());
             return fis.toByteArray();
         }
-        finally
-        {
-            if (fis != null)
-            {
-                fis.close();
-            }
-        }
     }
 
     /**



Mime
View raw message