CertificateCheck.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */
  20. package org.openspcoop2.protocol.registry;

  21. import java.util.ArrayList;
  22. import java.util.List;

  23. import org.apache.commons.lang3.StringUtils;
  24. import org.openspcoop2.core.constants.StatoCheck;

  25. /**
  26.  * CertificateStatoCheck
  27.  *
  28.  * @author Poli Andrea (apoli@link.it)
  29.  * @author $Author$
  30.  * @version $Rev$, $Date$
  31.  */
  32. public class CertificateCheck {

  33.     private StatoCheck statoCheck;
  34.     private String configurationId;
  35.     private List<String> errorCertificateIdentity = new ArrayList<>();
  36.     private List<String> errorDetails = new ArrayList<>();
  37.     private List<String> errorCertificateDetails = new ArrayList<>();
  38.     private List<String> warningCertificateIdentity = new ArrayList<>();
  39.     private List<String> warningDetails = new ArrayList<>();
  40.     private List<String> warningCertificateDetails = new ArrayList<>();
  41.    
  42.     public void addError(String identity, String details, String certificateDetails) {
  43.         this.errorCertificateIdentity.add(identity);
  44.         this.errorDetails.add(details);
  45.         this.errorCertificateDetails.add(certificateDetails!=null ? certificateDetails : "");
  46.     }
  47.     public void addWarning(String identity, String details, String certificateDetails) {
  48.         this.warningCertificateIdentity.add(identity);
  49.         this.warningDetails.add(details);
  50.         this.warningCertificateDetails.add(certificateDetails!=null ? certificateDetails : "");
  51.     }
  52.     public void setConfigurationId(String configurationId) {
  53.         this.configurationId = configurationId;
  54.     }
  55.    
  56.     public StatoCheck getStatoCheck() {
  57.         return this.statoCheck;
  58.     }
  59.     public void setStatoCheck(StatoCheck statoCheck) {
  60.         this.statoCheck = statoCheck;
  61.     }

  62.    
  63.     public String toString(String newLine) {
  64.         StringBuilder sbEsito = new StringBuilder();
  65.         sbEsito.append(this.statoCheck.toString());
  66.         switch (this.statoCheck) {
  67.         case ERROR:
  68.             printDetails(sbEsito, newLine,
  69.                     this.errorCertificateIdentity, this.errorDetails, this.errorCertificateDetails);            
  70.             break;
  71.         case WARN:
  72.             if(!this.errorCertificateIdentity.isEmpty()) {
  73.                 this.warningCertificateIdentity.addAll(this.errorCertificateIdentity);
  74.                 this.warningDetails.addAll(this.errorDetails);
  75.                 this.warningCertificateDetails.addAll(this.errorCertificateDetails);
  76.             }
  77.            
  78.             printDetails(sbEsito, newLine,
  79.                     this.warningCertificateIdentity, this.warningDetails, this.warningCertificateDetails);          
  80.             break;
  81.         default:
  82.             break;
  83.         }
  84.         return sbEsito.toString();
  85.     }
  86.     private void printDetails(StringBuilder sbEsito, String newLine,
  87.             List<String> certificateIdentities, List<String> detailsList, List<String> certificateDetailsList) {
  88.         if(!detailsList.isEmpty()) {
  89.             if(this.configurationId!=null) {
  90.                 sbEsito.append(newLine);
  91.                 sbEsito.append(this.configurationId);
  92.             }
  93.             for (int i = 0; i < detailsList.size(); i++) {
  94.                 String details = detailsList.get(i);
  95.                 String certificateDetails = certificateDetailsList.get(i);
  96.                 if(certificateIdentities.size()>1) {
  97.                     String identity = certificateIdentities.get(i);
  98.                     sbEsito.append(newLine);
  99.                     sbEsito.append("- ");
  100.                     sbEsito.append(identity);
  101.                 }
  102.                 if(StringUtils.isNotEmpty(certificateDetails)) {
  103.                     sbEsito.append(newLine);
  104.                     sbEsito.append(certificateDetails);
  105.                 }
  106.                 sbEsito.append(newLine);
  107.                 sbEsito.append(details);
  108.             }
  109.         }  
  110.     }
  111. }