StatusUtilities.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.monitor.engine.status;

  21. import java.util.List;

  22. import org.apache.commons.lang.StringUtils;
  23. import org.openspcoop2.monitor.engine.constants.SondaStatus;
  24. import org.openspcoop2.utils.transport.http.HttpRequest;
  25. import org.openspcoop2.utils.transport.http.HttpRequestMethod;
  26. import org.openspcoop2.utils.transport.http.HttpUtilities;
  27. import org.openspcoop2.utils.transport.http.HttpUtilsException;
  28. import org.slf4j.Logger;

  29. /**
  30.  * StatusUtilities
  31.  *
  32.  * @author Pintori Giuliano (pintori@link.it)
  33.  * @author $Author$
  34.  * @version $Rev$, $Date$
  35.  *
  36.  */
  37. public class StatusUtilities {

  38.     public static List<IStatus> updateStato( List<IStatus> listaStatus, Logger log ) throws Exception {

  39.         for (IStatus status : listaStatus) {
  40.             updateStato(status, log);
  41.         }

  42.         return listaStatus;
  43.     }
  44.    
  45.     public static void updateStato( IStatus status, Logger log ) throws Exception {

  46.         try{
  47.             _check(((GatewayStatus) status));
  48.             status.setStato(SondaStatus.OK);
  49.             status.setDescrizione(null);
  50.         }catch(Exception e){
  51.             if(e instanceof HttpUtilsException) {
  52.                 HttpUtilsException http = (HttpUtilsException) e;
  53.                 if(http.getReturnCode()>=200 && http.getReturnCode()<=299) {
  54.                     status.setStato(SondaStatus.WARNING);
  55.                 }
  56.                 else {
  57.                     status.setStato(SondaStatus.ERROR);
  58.                 }
  59.             }
  60.             else {
  61.                 status.setStato(SondaStatus.ERROR);
  62.             }
  63.             status.setDescrizione(e.getMessage());
  64.             log.error("Verifica '"+status.getNome()+"' fallita: "+e.getMessage(),e);
  65.         }

  66.     }
  67.    
  68.     private static void _check(GatewayStatus status) throws Exception {
  69.         boolean https = status.isHttps();
  70.         boolean https_verificaHostName = true;
  71.         boolean https_autenticazioneServer = true;
  72.         String https_truststorePath = null;
  73.         String https_truststoreType = null;
  74.         String https_truststorePassword = null;
  75.         if(https) {
  76.             https_verificaHostName = status.isHttps_verificaHostName();
  77.             https_autenticazioneServer = status.isHttps_autenticazioneServer();
  78.             if(https_autenticazioneServer) {
  79.                 https_truststorePath = status.getHttps_autenticazioneServer_truststorePath();
  80.                 if(StringUtils.isEmpty(https_truststorePath)) {
  81.                     throw new Exception("[alias:"+status.getNome()+"] TLS Truststore path non fornito");
  82.                 }
  83.                 https_truststoreType = status.getHttps_autenticazioneServer_truststoreType();
  84.                 if(StringUtils.isEmpty(https_truststoreType)) {
  85.                     throw new Exception("[alias:"+status.getNome()+"] TLS Truststore type non fornito");
  86.                 }
  87.                 https_truststorePassword = status.getHttps_autenticazioneServer_truststorePassword();
  88.                 if(StringUtils.isEmpty(https_truststorePassword)) {
  89.                     throw new Exception("[alias:"+status.getNome()+"] TLS Truststore password non fornito");
  90.                 }
  91.             }
  92.         }
  93.        
  94.         if(https) {
  95.             HttpRequest httpRequest = new HttpRequest();
  96.             httpRequest.setUrl(status.getUrl());
  97.             httpRequest.setReadTimeout(status.getReadConnectionTimeout());
  98.             httpRequest.setConnectTimeout(status.getConnectionTimeout());
  99.             httpRequest.setMethod(HttpRequestMethod.GET);
  100.             httpRequest.setHostnameVerifier(https_verificaHostName);
  101.             if(https_autenticazioneServer) {
  102.                 httpRequest.setTrustStorePath(https_truststorePath);
  103.                 httpRequest.setTrustStoreType(https_truststoreType);
  104.                 httpRequest.setTrustStorePassword(https_truststorePassword);
  105.             }
  106.             else {
  107.                 httpRequest.setTrustAllCerts(true);
  108.             }
  109.             HttpUtilities.check(httpRequest);
  110.         }
  111.         else {
  112.             HttpUtilities.check(status.getUrl(), status.getReadConnectionTimeout(), status.getConnectionTimeout());
  113.         }
  114.     }
  115.    
  116.     public static int getTotOk(List<IStatus> listaStatus) throws Exception {
  117.         int totOk = 0;
  118.         try{
  119.             for (IStatus pddBean : listaStatus) {
  120.                 if(pddBean.getStato().equals(SondaStatus.OK))
  121.                     totOk ++;
  122.             }
  123.         }catch(Exception e){

  124.         }
  125.         return totOk;
  126.     }
  127.    
  128.     public static SondaStatus statesProcess(List<IStatus> listaStatus) throws Exception {
  129.         int tot_ok = getTotOk(listaStatus);
  130.         // Tutti in errore
  131.         if(tot_ok == 0){
  132.             boolean findWarning = false;
  133.             try{
  134.                 for (IStatus pddBean : listaStatus) {
  135.                     if(pddBean.getStato().equals(SondaStatus.WARNING)) {
  136.                         findWarning = true;
  137.                         break;
  138.                     }
  139.                 }
  140.                 if(findWarning) {
  141.                     return SondaStatus.WARNING;
  142.                 }
  143.                 else {
  144.                     return SondaStatus.ERROR;
  145.                 }
  146.             }catch(Exception e){
  147.                 return SondaStatus.ERROR;
  148.             }
  149.         }else
  150.             // parzialmente in errore
  151.             if(tot_ok< listaStatus.size()){
  152.                 return  SondaStatus.WARNING;
  153.             }else {
  154.                 return SondaStatus.OK;
  155.             }
  156.     }
  157.    
  158.     public static String getDetailStatesProcess(List<IStatus> listaStatus) throws Exception {
  159.         int tot_ok = getTotOk(listaStatus);
  160.         // Tutti in errore
  161.         if(tot_ok == 0){
  162.             if(listaStatus.size()==1)
  163.                 return "Il Gateway non è funzionante";
  164.             else
  165.                 return "Nessuno dei "+listaStatus.size()+" nodi del Gateway è funzionante";
  166.         }else {
  167.             // parzialmente in errore
  168.             if(tot_ok< listaStatus.size()){
  169.                 return  (listaStatus.size() - tot_ok) + " su " +listaStatus.size()+ " nodi del Gateway non sono funzionanti";
  170.             }else {
  171.                 // tutti ok
  172.                 if(listaStatus.size()==1)
  173.                     return "Il Gateway è funzionante";
  174.                 else
  175.                     return "Tutti i "+listaStatus.size()+" nodi del Gateway sono funzionanti";
  176.             }
  177.         }
  178.     }
  179. }