pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From til...@apache.org
Subject svn commit: r1854940 - in /pdfbox/branches/2.0/examples/src/main/java/org/apache/pdfbox/examples/signature/validation: AddValidationInformation.java CertInformationCollector.java
Date Wed, 06 Mar 2019 19:10:29 GMT
Author: tilman
Date: Wed Mar  6 19:10:29 2019
New Revision: 1854940

URL: http://svn.apache.org/viewvc?rev=1854940&view=rev
Log:
PDFBOX-3017: replace map with set

Modified:
    pdfbox/branches/2.0/examples/src/main/java/org/apache/pdfbox/examples/signature/validation/AddValidationInformation.java
    pdfbox/branches/2.0/examples/src/main/java/org/apache/pdfbox/examples/signature/validation/CertInformationCollector.java

Modified: pdfbox/branches/2.0/examples/src/main/java/org/apache/pdfbox/examples/signature/validation/AddValidationInformation.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/examples/src/main/java/org/apache/pdfbox/examples/signature/validation/AddValidationInformation.java?rev=1854940&r1=1854939&r2=1854940&view=diff
==============================================================================
--- pdfbox/branches/2.0/examples/src/main/java/org/apache/pdfbox/examples/signature/validation/AddValidationInformation.java
(original)
+++ pdfbox/branches/2.0/examples/src/main/java/org/apache/pdfbox/examples/signature/validation/AddValidationInformation.java
Wed Mar  6 19:10:29 2019
@@ -387,7 +387,7 @@ public class AddValidationInformation
                 certInfo.getCertificate(),
                 signDate.getTime(),
                 certInfo.getIssuerCertificate(),
-                new HashSet<X509Certificate>(certInformationHelper.getCertificatesMap().values()),
+                new HashSet<X509Certificate>(certInformationHelper.getCertificateSet()),
                 certInfo.getOcspUrl());
         OCSPResp ocspResp = ocspHelper.getResponseOcsp();
         BasicOCSPResp basicResponse = (BasicOCSPResp) ocspResp.getResponseObject();
@@ -447,7 +447,7 @@ public class AddValidationInformation
         X509Certificate issuerCertificate = certInfo.getIssuerCertificate();
 
         // find the issuer certificate (usually issuer of signature certificate)
-        for (X509Certificate certificate : certInformationHelper.getCertificatesMap().values())
+        for (X509Certificate certificate : certInformationHelper.getCertificateSet())
         {
             if (certificate.getSubjectX500Principal().equals(crl.getIssuerX500Principal()))
             {
@@ -557,7 +557,7 @@ public class AddValidationInformation
     {
         try
         {
-            for (X509Certificate cert : certInformationHelper.getCertificatesMap().values())
+            for (X509Certificate cert : certInformationHelper.getCertificateSet())
             {
                 COSStream stream = writeDataToStream(cert.getEncoded());
                 certs.add(stream);

Modified: pdfbox/branches/2.0/examples/src/main/java/org/apache/pdfbox/examples/signature/validation/CertInformationCollector.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/examples/src/main/java/org/apache/pdfbox/examples/signature/validation/CertInformationCollector.java?rev=1854940&r1=1854939&r2=1854940&view=diff
==============================================================================
--- pdfbox/branches/2.0/examples/src/main/java/org/apache/pdfbox/examples/signature/validation/CertInformationCollector.java
(original)
+++ pdfbox/branches/2.0/examples/src/main/java/org/apache/pdfbox/examples/signature/validation/CertInformationCollector.java
Wed Mar  6 19:10:29 2019
@@ -20,7 +20,6 @@ package org.apache.pdfbox.examples.signa
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
-import java.math.BigInteger;
 import java.net.URL;
 import java.security.GeneralSecurityException;
 import java.security.InvalidKeyException;
@@ -32,8 +31,8 @@ import java.security.cert.CertificateFac
 import java.security.cert.X509Certificate;
 import java.util.Arrays;
 import java.util.Collection;
-import java.util.HashMap;
-import java.util.Map;
+import java.util.HashSet;
+import java.util.Set;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -58,7 +57,7 @@ import org.bouncycastle.util.Selector;
 import org.bouncycastle.util.Store;
 
 /**
- * This Class helps to extract Data/Information from a signature. The information is held
in
+ * This class helps to extract data/information from a signature. The information is held
in
  * CertSignatureInformation. Some information is needed for validation processing of the
  * participating certificates.
  *
@@ -71,7 +70,7 @@ public class CertInformationCollector
 
     private static final int MAX_CERTIFICATE_CHAIN_DEPTH = 5;
 
-    private final Map<BigInteger, X509Certificate> certificatesMap = new HashMap<BigInteger,
X509Certificate>();
+    private final Set<X509Certificate> certificateSet = new HashSet<X509Certificate>();
 
     private final JcaX509CertificateConverter certConverter = new JcaX509CertificateConverter();
 
@@ -189,7 +188,7 @@ public class CertInformationCollector
      * not yet practicable.
      *
      * @param certificatesStore To get the certificate information from. Certificates will
be saved
-     * in certificatesMap.
+     * in certificateSet.
      * @param signedData data from which to get the SignerInformation
      * @param certInfo where to add certificate information
      * @return Signer Information of the processed certificatesStore for further usage.
@@ -208,6 +207,7 @@ public class CertInformationCollector
                 .getMatches((Selector<X509CertificateHolder>) signerInformation.getSID());
 
         X509Certificate certificate = getCertFromHolder(matches.iterator().next());
+        certificateSet.add(certificate);
 
         Collection<X509CertificateHolder> allCerts = certificatesStore.getMatches(null);
         addAllCerts(allCerts);
@@ -263,7 +263,7 @@ public class CertInformationCollector
             return;
         }
 
-        for (X509Certificate issuer : certificatesMap.values())
+        for (X509Certificate issuer : certificateSet)
         {
             if (certificate.getIssuerX500Principal().equals(issuer.getSubjectX500Principal()))
             {
@@ -327,7 +327,7 @@ public class CertInformationCollector
             InputStream in = certUrl.openStream();
 
             X509Certificate altIssuerCert = (X509Certificate) certFactory.generateCertificate(in);
-            addCertToCertificatesMap(altIssuerCert);
+            certificateSet.add(altIssuerCert);
 
             certInfo.alternativeCertChain = new CertSignatureInformation();
             traverseChain(altIssuerCert, certInfo.alternativeCertChain, maxDepth - 1);
@@ -344,51 +344,29 @@ public class CertInformationCollector
     }
 
     /**
-     * Adds the given Certificate to the certificatesMap, if not yet containing.
-     * 
-     * @param certificate to add to the certificatesMap
-     */
-    private void addCertToCertificatesMap(X509Certificate certificate)
-    {
-        if (!certificatesMap.containsKey(certificate.getSerialNumber()))
-        {
-            certificatesMap.put(certificate.getSerialNumber(), certificate);
-        }
-    }
-
-    /**
-     * Gets the X509Certificate out of the X509CertificateHolder and add it to certificatesMap.
+     * Gets the X509Certificate out of the X509CertificateHolder.
      *
      * @param certificateHolder to get the certificate from
      * @return a X509Certificate or <code>null</code> when there was an Error
with the Certificate
-     * @throws CertificateProccessingException on failed conversion from X509CertificateHolder
to X509Certificate
+     * @throws CertificateProccessingException on failed conversion from X509CertificateHolder
to
+     * X509Certificate
      */
     private X509Certificate getCertFromHolder(X509CertificateHolder certificateHolder)
             throws CertificateProccessingException
     {
-        //TODO getCertFromHolder violates "do one thing" rule (adds to the map and returns
a certificate)
-        if (!certificatesMap.containsKey(certificateHolder.getSerialNumber()))
+        try
         {
-            try
-            {
-                X509Certificate certificate = certConverter.getCertificate(certificateHolder);
-                certificatesMap.put(certificate.getSerialNumber(), certificate);
-                return certificate;
-            }
-            catch (CertificateException e)
-            {
-                LOG.error("Certificate Exception getting Certificate from certHolder.", e);
-                throw new CertificateProccessingException(e);
-            }
+            return certConverter.getCertificate(certificateHolder);
         }
-        else
+        catch (CertificateException e)
         {
-            return certificatesMap.get(certificateHolder.getSerialNumber());
+            LOG.error("Certificate Exception getting Certificate from certHolder.", e);
+            throw new CertificateProccessingException(e);
         }
     }
 
     /**
-     * Adds multiple Certificates out of a Collection of X509CertificateHolder into certificatesMap.
+     * Adds multiple Certificates out of a Collection of X509CertificateHolder into certificateSet.
      *
      * @param certHolders Collection of X509CertificateHolder
      */
@@ -398,7 +376,8 @@ public class CertInformationCollector
         {
             try
             {
-                getCertFromHolder(certificateHolder);
+                X509Certificate certificate = getCertFromHolder(certificateHolder);
+                certificateSet.add(certificate);
             }
             catch (CertificateProccessingException e)
             {
@@ -409,7 +388,7 @@ public class CertInformationCollector
 
     /**
      * Gets a list of X509Certificate out of an array of X509CertificateHolder. The certificates
-     * will be added to certificatesMap.
+     * will be added to certificateSet.
      *
      * @param certHolders Array of X509CertificateHolder
      * @throws CertificateProccessingException when one of the Certificates could not be
parsed.
@@ -442,13 +421,13 @@ public class CertInformationCollector
     }
 
     /**
-     * Get the map of all processed certificates until now.
+     * Get the set of all processed certificates until now.
      * 
-     * @return a map of serial numbers to certificates.
+     * @return a set of serial numbers to certificates.
      */
-    public Map<BigInteger, X509Certificate> getCertificatesMap()
+    public Set<X509Certificate> getCertificateSet()
     {
-        return certificatesMap;
+        return certificateSet;
     }
 
     /**



Mime
View raw message