public static X509Certificate[] createCertificateChain(String certChainPEM){
  AssertUtil.assertNotNull(certChainPEM,"certChainPEM should not be null.");
  List<X509Certificate> chain=new ArrayList<>();
  try (PEMParser parser=new PEMParser(new StringReader(certChainPEM))){
    JcaX509CertificateConverter converter=new JcaX509CertificateConverter();
    X509CertificateHolder certificateHolder;
    while ((certificateHolder=(X509CertificateHolder)parser.readObject()) != null) {
      chain.add(converter.getCertificate(certificateHolder));
    }
  }
 catch (  IOException|CertificateException e) {
    throw new RuntimeException("Failed to create certificate: " + certChainPEM,e);
  }
  if (chain.isEmpty()) {
    throw new RuntimeException("A valid certificate was not found: " + certChainPEM);
  }
  return chain.toArray(new X509Certificate[chain.size()]);
}

public static X509Certificate [ ] createCertificateChain ( String certChainPEM ) { AssertUtil . assertNotNull ( certChainPEM , "certChainPEM should not be null." ) ; List < X509Certificate > chain = new ArrayList < > ( ) ; try ( PEMParser parser = new PEMParser ( new StringReader ( certChainPEM ) ) ) { JcaX509CertificateConverter converter = new JcaX509CertificateConverter ( ) ; X509CertificateHolder certificateHolder ; while ( ( certificateHolder = ( X509CertificateHolder ) parser . readObject ( ) ) != null ) { chain . add ( converter . getCertificate ( certificateHolder ) ) ; } } catch ( IOException | CertificateException e ) { throw new RuntimeException ( "Failed to create certificate: " + certChainPEM , e ) ; } if ( chain . isEmpty ( ) ) { throw new RuntimeException ( "A valid certificate was not found: " + certChainPEM ) ; } return chain . toArray ( new X509Certificate [ chain . size ( ) ] ) ; }
utility method to decode a certificate chain pem encoded string value to an array of x509certificate certificate instances .
