AbstractCredenzialeList.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.core.transazioni.utils.credenziali;

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

  23. import org.apache.commons.lang.StringUtils;
  24. import org.openspcoop2.core.transazioni.utils.TipoCredenzialeMittente;
  25. import org.openspcoop2.utils.UtilsException;

  26. /**    
  27.  * AbstractCredenziale
  28.  *
  29.  * @author Poli Andrea (poli@link.it)
  30.  * @author $Author$
  31.  * @version $Rev$, $Date$
  32.  */
  33. public abstract class AbstractCredenzialeList extends AbstractCredenziale {

  34.     private List<String> values;
  35.    
  36.     protected AbstractCredenzialeList(TipoCredenzialeMittente tipo, List<String> values) {
  37.         super(tipo);
  38.         this.values = values;
  39.     }
  40.    
  41.     @Override
  42.     public String getCredenziale() throws UtilsException{
  43.         StringBuilder bf = new StringBuilder();
  44.         bf.append(PREFIX);
  45.         for (String value : this.values) {
  46.             bf.append(value);
  47.             bf.append(PREFIX);
  48.         }
  49.         return bf.toString();
  50.     }
  51.    
  52.     public static final String PREFIX = "##";
  53.    
  54.     public static String getDBValue(String address) {
  55.         return getDBValue(address, true);
  56.     }
  57.     public static String getDBValue(String address, boolean ricercaEsatta) {
  58.         if(ricercaEsatta) {
  59.             return PREFIX + address + PREFIX;
  60.         }
  61.         else {
  62.             return PREFIX + "%" + address + "%" + PREFIX;
  63.         }
  64.     }
  65.    
  66.     public static List<String> normalizeToList(String dbValue){
  67.         List<String> lReturn = null;
  68.         if(dbValue.contains(PREFIX)) {
  69.             List<String> l = new ArrayList<>();
  70.             String [] tmp = dbValue.split(PREFIX);
  71.             normalizeToList(tmp, l);
  72.             if(!l.isEmpty()) {
  73.                 return l;
  74.             }
  75.         }
  76.         return lReturn;
  77.     }
  78.     private static void normalizeToList(String [] tmp, List<String> l){
  79.         if(tmp!=null && tmp.length>0) {
  80.             for (String t : tmp) {
  81.                 if(t!=null) {
  82.                     t = t.trim();
  83.                     if(!StringUtils.isEmpty(t)) {
  84.                         l.add(t);
  85.                     }
  86.                 }
  87.             }
  88.         }
  89.     }
  90.     public static String normalize(String dbValue) {
  91.         List<String> l = normalizeToList(dbValue);
  92.         if(l!=null && !l.isEmpty()) {
  93.             StringBuilder bf = new StringBuilder();
  94.             for (String v : l) {
  95.                 if(bf.length()>0) {
  96.                     bf.append(",");
  97.                 }
  98.                 bf.append(v);
  99.             }
  100.             return bf.toString();
  101.         }
  102.         return null;
  103.     }
  104.    
  105.     @Override
  106.     public void updateCredenziale(String newCredential) throws UtilsException{
  107.         throw new UtilsException("Aggiornamento non supportato dal tipo di credenziale");
  108.     }
  109. }