View Javadoc
1   /*
2    * Licensed under the GPL License. You may not use this file except in compliance with the License.
3    * You may obtain a copy of the License at
4    *
5    *   https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
6    *
7    * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
8    * WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
9    * PURPOSE.
10   */
11  package psiprobe.controllers.certificates;
12  
13  import java.lang.reflect.InvocationTargetException;
14  import java.util.ArrayList;
15  import java.util.List;
16  import java.util.Set;
17  
18  import org.apache.commons.beanutils.BeanUtils;
19  import org.apache.coyote.http11.AbstractHttp11JsseProtocol;
20  import org.apache.tomcat.util.net.SSLHostConfig;
21  import org.apache.tomcat.util.net.SSLHostConfigCertificate;
22  
23  import psiprobe.model.certificates.CertificateInfo;
24  import psiprobe.model.certificates.ConnectorInfo;
25  import psiprobe.model.certificates.SslHostConfigInfo;
26  
27  /**
28   * The Class SslHostConfigHelper.
29   */
30  public class SslHostConfigHelper {
31  
32    /**
33     * Instantiates a new SSL host config helper.
34     *
35     * @param protocol the protocol
36     * @param info the info
37     *
38     * @throws IllegalAccessException the illegal access exception
39     * @throws InvocationTargetException the invocation target exception
40     */
41    public SslHostConfigHelper(AbstractHttp11JsseProtocol<?> protocol, ConnectorInfo info)
42        throws IllegalAccessException, InvocationTargetException {
43      SSLHostConfig[] sslHostConfigs = protocol.findSslHostConfigs();
44      List<SslHostConfigInfo> sslHostConfigInfos = new ArrayList<>(sslHostConfigs.length);
45      for (SSLHostConfig sslHostConfig : sslHostConfigs) {
46        sslHostConfigInfos.add(toSslHostConfigInfo(sslHostConfig));
47      }
48      info.setSslHostConfigInfos(sslHostConfigInfos);
49    }
50  
51    /**
52     * To SslHostConfig info.
53     *
54     * @param sslHostConfig the SslHostConfig
55     *
56     * @return the SslHostConfig info
57     *
58     * @throws IllegalAccessException the illegal access exception
59     * @throws InvocationTargetException the invocation target exception
60     */
61    private SslHostConfigInfo toSslHostConfigInfo(SSLHostConfig sslHostConfig)
62        throws IllegalAccessException, InvocationTargetException {
63      SslHostConfigInfo sslHostConfigInfo = new SslHostConfigInfo();
64      BeanUtils.copyProperties(sslHostConfigInfo, sslHostConfig);
65  
66      Set<SSLHostConfigCertificate> certificates = sslHostConfig.getCertificates();
67      List<CertificateInfo> certificateInfos = new ArrayList<>(certificates.size());
68      for (SSLHostConfigCertificate sslHostConfigCertificate : certificates) {
69        certificateInfos.add(toCertificateInfo(sslHostConfigCertificate));
70      }
71      sslHostConfigInfo.setCertificateInfos(certificateInfos);
72  
73      return sslHostConfigInfo;
74    }
75  
76    /**
77     * To certificate info.
78     *
79     * @param sslHostConfigCertificate the SslHostConfigCertificate
80     *
81     * @return the certificate info
82     *
83     * @throws IllegalAccessException the illegal access exception
84     * @throws InvocationTargetException the invocation target exception
85     */
86    private CertificateInfo toCertificateInfo(SSLHostConfigCertificate sslHostConfigCertificate)
87        throws IllegalAccessException, InvocationTargetException {
88      CertificateInfo certificateInfo = new CertificateInfo();
89      BeanUtils.copyProperties(certificateInfo, sslHostConfigCertificate);
90      return certificateInfo;
91    }
92  }