You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by or...@apache.org on 2023/05/02 15:27:05 UTC

[camel] 03/07: Substituted calls of System.out.println() to slf4j logging

This is an automated email from the ASF dual-hosted git repository.

orpiske pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 3ff6e2f54966b04573ff9f0b5d9deaa01190bc20
Author: Dmitry Kryukov <dk...@ya.ru>
AuthorDate: Wed Apr 26 18:36:23 2023 +0300

    Substituted calls of System.out.println() to slf4j logging
---
 .../camel/component/as2/MendelsonCertLoader.java   | 47 ++++++++++------------
 1 file changed, 22 insertions(+), 25 deletions(-)

diff --git a/components/camel-as2/camel-as2-component/src/test/java/org/apache/camel/component/as2/MendelsonCertLoader.java b/components/camel-as2/camel-as2-component/src/test/java/org/apache/camel/component/as2/MendelsonCertLoader.java
index ce82de3849e..8a6d106daba 100644
--- a/components/camel-as2/camel-as2-component/src/test/java/org/apache/camel/component/as2/MendelsonCertLoader.java
+++ b/components/camel-as2/camel-as2-component/src/test/java/org/apache/camel/component/as2/MendelsonCertLoader.java
@@ -22,12 +22,16 @@ import javax.net.ssl.SSLContext;
 import org.apache.commons.io.IOUtils;
 import org.apache.http.conn.ssl.TrustAllStrategy;
 import org.apache.http.ssl.SSLContexts;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * That's a utility class for preparing Mendelson-specific certificate chain, private key, ssl context
  */
 public class MendelsonCertLoader {
 
+    private static final Logger LOG = LoggerFactory.getLogger(MendelsonCertLoader.class);
+
     private static final String MENDELSON_CERT = "mendelson/key4.cer";
     private static final String MENDELSON_PRIVATE_KEY = "mendelson/key3.pfx";
 
@@ -43,14 +47,8 @@ public class MendelsonCertLoader {
             sslContext = SSLContexts.custom().setKeyStoreType("PKCS12")
                     .loadTrustMaterial(keyStore, new TrustAllStrategy())
                     .build();
-        } catch (KeyStoreException e) {
-            e.printStackTrace();
-        } catch (KeyManagementException e) {
-            e.printStackTrace();
-        } catch (IOException e) {
-            e.printStackTrace();
-        } catch (NoSuchAlgorithmException e) {
-            e.printStackTrace();
+        } catch (KeyStoreException | IOException | KeyManagementException | NoSuchAlgorithmException e) {
+            LOG.error("Failed to configure SSLContext", e);
         }
 
         if (sslContext == null) {
@@ -66,9 +64,9 @@ public class MendelsonCertLoader {
             ks.load(inputStream, password.toCharArray());
             return ks;
         } catch (KeyStoreException e) {
-            e.printStackTrace();
+            LOG.error("Failed to create instance of KeyStore", e);
         } catch (CertificateException e) {
-            e.printStackTrace();
+            LOG.error("Failed to load KeyStore");
         }
         throw new IllegalStateException("about to return null");
     }
@@ -95,17 +93,18 @@ public class MendelsonCertLoader {
             privateKey = getPrivateKeyFromPKCSStream(mendelsonPrivateKeyAsStream);
 
         } catch (IOException e) {
-            String errMsg = "Error while trying to load certificate to the keyload. IO error when reading a byte array.  " + e;
-            System.out.println(errMsg);
+            String errMsg
+                    = "Error while trying to load certificate to the key store. IO error when reading a byte array.  " + e;
+            LOG.error(errMsg);
         } catch (NoSuchAlgorithmException e) {
-            String errMsg = "Error while trying to load certificate to the keyload. Requested algorithm isn't found.  " + e;
-            System.out.println(errMsg);
+            String errMsg = "Error while trying to load certificate to the key store. Requested algorithm isn't found.  " + e;
+            LOG.error(errMsg);
         } catch (CertificateException e) {
-            String errMsg = "Error while trying to load certificate to the keyload. There is a certificate problem.  " + e;
-            System.out.println(errMsg);
+            String errMsg = "Error while trying to load certificate to the key store. There is a certificate problem.  " + e;
+            LOG.error(errMsg);
         } catch (InvalidKeySpecException e) {
             String errMsg = "Can not init private key store  " + e;
-            System.out.println(errMsg);
+            LOG.error(errMsg);
         }
     }
 
@@ -130,7 +129,7 @@ public class MendelsonCertLoader {
         return privateKey;
     }
 
-    private List<Certificate> getCertificatesFromStream(InputStream inputStream) throws IOException, CertificateException {
+    private List<Certificate> getCertificatesFromStream(InputStream inputStream) throws CertificateException {
         CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
         return (List<Certificate>) certificateFactory.generateCertificates(inputStream);
     }
@@ -148,25 +147,23 @@ public class MendelsonCertLoader {
         try {
             ks = KeyStore.getInstance("PKCS12");
         } catch (KeyStoreException e) {
-            e.printStackTrace();
+            LOG.error("Error while getting instance of KeyStore" + e);
         }
         try {
             ks.load(inputStream, password.toCharArray());
         } catch (CertificateException e) {
-            e.printStackTrace();
-        } catch (Exception e) {
-            e.printStackTrace();
+            LOG.error("Error while loading the certificate" + e);
         }
         try {
             return (PrivateKey) ks.getKey(
                     ks.aliases().nextElement(),
                     password.toCharArray());
         } catch (KeyStoreException e) {
-            e.printStackTrace();
+            LOG.error("Error while retrieving private key" + e);
         } catch (UnrecoverableKeyException e) {
-            e.printStackTrace();
+            LOG.error("Error while retrieving private key" + e);
         }
-        throw new IllegalStateException("about to return null");
+        throw new IllegalStateException("Failed to construct a PrivateKey from provided InputStream");
     }
 
     private byte[] getBytesFromPem(InputStream inputStream) throws IOException {