Utilities.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.web.ctrlstat.core;

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

  23. import org.openspcoop2.core.registry.Connettore;
  24. import org.openspcoop2.web.ctrlstat.costanti.TipologiaConnettori;
  25. import org.openspcoop2.web.ctrlstat.servlet.connettori.ConnettoriCostanti;
  26. import org.openspcoop2.web.lib.mvc.DataElement;
  27. /**
  28.  *
  29.  * Metodi di utilita'
  30.  *
  31.  * @author Andrea Poli (apoli@link.it)
  32.  * @author Stefano Corallo (corallo@link.it)
  33.  * @author Sandra Giangrandi (sandra@link.it)
  34.  * @author $Author$
  35.  * @version $Rev$, $Date$
  36.  *
  37.  */
  38. public class Utilities {


  39.     public static boolean contains(String value,String [] listaValoriPossibili){
  40.         if(value==null)
  41.             return false;
  42.         if(listaValoriPossibili==null){
  43.             return false;
  44.         }
  45.         for (int i = 0; i < listaValoriPossibili.length; i++) {
  46.             if(value.equals(listaValoriPossibili[i])){
  47.                 return true;
  48.             }
  49.         }
  50.         return false;
  51.     }
  52.    
  53.     public static String toString(String [] values,String separator){
  54.         StringBuilder bf = new StringBuilder();
  55.         if(values!=null){
  56.             for (int i = 0; i < values.length; i++) {
  57.                 if(i>0){
  58.                     bf.append(separator);
  59.                     bf.append(values[i]);
  60.                 }
  61.             }
  62.         }
  63.         return bf.toString();
  64.     }
  65.    
  66.    
  67.     private static TipologiaConnettori TIPOLOGIA_CONNETTORI = null;
  68.     public static TipologiaConnettori getTipologiaConnettori(ControlStationCore core) throws Exception {

  69.         if (Utilities.TIPOLOGIA_CONNETTORI == null) {
  70.             Utilities.readTipologiaConnettori(core);
  71.         }

  72.         return Utilities.TIPOLOGIA_CONNETTORI;

  73.     }

  74.     /**
  75.      * Tipologia connettori ALL/HTTP
  76.      *
  77.      * @param tipologia
  78.      */
  79.      public static void setTipologiaConnettori(TipologiaConnettori tipologia) {
  80.          Utilities.TIPOLOGIA_CONNETTORI = tipologia == null ? TipologiaConnettori.TIPOLOGIA_CONNETTORI_ALL : tipologia;
  81.      }

  82.      /**
  83.       * Legge il valore della proprieta' impostata nel file di configurazione se
  84.       * la proprieta' e' nulla setta il valore di default (ALL)
  85.       */
  86.      public static void readTipologiaConnettori(ControlStationCore core) throws Exception {
  87.          
  88.          if (core.isShowAllConnettori()) {
  89.              Utilities.TIPOLOGIA_CONNETTORI = TipologiaConnettori.TIPOLOGIA_CONNETTORI_ALL;
  90.          } else {
  91.              Utilities.TIPOLOGIA_CONNETTORI = TipologiaConnettori.TIPOLOGIA_CONNETTORI_HTTP;
  92.          }

  93.      }
  94.      
  95.      public static void setDataElementLabelTipoConnettore(DataElement de,Connettore connettore){
  96.          de.setValue(ConnettoriCostanti.LABEL_CONNETTORE+" (" + connettore.getTipo() + ")");
  97.      }


  98.      public static ArrayList<String> parseIdsToRemove(String idsToRemove) {
  99.          ArrayList<String> toRem = new ArrayList<>();
  100.          StringTokenizer objTok = new StringTokenizer(idsToRemove, ",");

  101.          while (objTok.hasMoreElements()) {
  102.              String id2rem = (String) objTok.nextElement();
  103.              toRem.add(id2rem);
  104.          }

  105.          return toRem;
  106.      }

  107.    




  108.    


  109.      public static String getTestoVisualizzabile(byte [] b,StringBuilder stringBuffer) {
  110.          try{
  111.              // 1024 = 1K
  112.              // Visualizzo al massimo 250K
  113.              int max = 250 * 1024;
  114. //           if(b.length>max){
  115. //               return "Visualizzazione non riuscita: la dimensione supera 250K";
  116. //           }
  117. //
  118. //           for (int i = 0; i < b.length; i++) {
  119. //               if(!Utilities.isPrintableChar((char)b[i])){
  120. //
  121. //                   return "Visualizzazione non riuscita: il documento contiene caratteri non visualizzabili";
  122. //               }
  123. //           }
  124.              stringBuffer.append(org.openspcoop2.utils.Utilities.convertToPrintableText(b, max));
  125.              return null;

  126.          }catch(Exception e){
  127.              ControlStationCore.logError("getTestoVisualizzabile error", e);
  128.              return e.getMessage();
  129.          }

  130.      }

  131. //   public static boolean isPrintableChar( char c ) {
  132. //       if ( Character.isDefined(c))
  133. //       {
  134. //           return true;
  135. //       }
  136. //       else{
  137. //           return false;
  138. //       }
  139. //   }

  140. }