ParametriAutenticazione.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.pdd.core.autenticazione;

  21. import java.util.HashMap;
  22. import java.util.Iterator;
  23. import java.util.List;
  24. import java.util.Set;

  25. import org.openspcoop2.core.config.Proprieta;


  26. /**
  27.  * ParametriAutenticazione
  28.  *
  29.  * @author Andrea Poli (apoli@link.it)
  30.  * @author $Author$
  31.  * @version $Rev$, $Date$
  32.  */
  33. public class ParametriAutenticazione implements java.io.Serializable {

  34.    
  35.     /**
  36.      *
  37.      */
  38.     private static final long serialVersionUID = 1L;

  39.     private static final String separator = "@@@";
  40.     private static final String separator_line = "@#@";
  41.    
  42.     public ParametriAutenticazione() {}
  43.     public ParametriAutenticazione(List<Proprieta> list) {
  44.         if(list!=null && !list.isEmpty()) {
  45.             for (Proprieta proprieta : list) {
  46.                 this.map.put(proprieta.getNome(), proprieta.getValore());
  47.             }
  48.         }
  49.     }
  50.     public ParametriAutenticazione(ParametriAutenticazione p) {
  51.         if(p!=null && p.map!=null) {
  52.             this.map = p.map;
  53.         }
  54.     }
  55.     public ParametriAutenticazione(String dbValue) throws AutenticazioneException {
  56.         if(dbValue!=null) {
  57.             if(dbValue.contains(separator)==false) {
  58.                 throw new AutenticazioneException("Formato errato");
  59.             }
  60.             if(dbValue.contains(separator_line)) {
  61.                 String [] linee = dbValue.split(separator_line);
  62.                 if(linee==null || linee.length<=0) {
  63.                     throw new AutenticazioneException("Formato errato (linea senza valori?)");
  64.                 }
  65.                 for (int i = 0; i < linee.length; i++) {
  66.                     String linea = linee[i];
  67.                     String [] tmp = linea.split(separator);
  68.                     if(tmp==null || tmp.length!=2) {
  69.                         throw new AutenticazioneException("Formato errato (coppia non presente?)");
  70.                     }
  71.                     this.map.put(tmp[0], tmp[1]);
  72.                 }
  73.             }
  74.             else {
  75.                 String [] tmp = dbValue.split(separator);
  76.                 if(tmp==null || tmp.length!=2) {
  77.                     throw new AutenticazioneException("Formato errato (coppia non presente?)");
  78.                 }
  79.                 this.map.put(tmp[0], tmp[1]);
  80.             }
  81.         }
  82.     }
  83.    
  84.     protected HashMap<String, String> map = new HashMap<>();
  85.    
  86.     public void add(String nome, String valore) {
  87.         this.map.put(nome, valore);
  88.     }
  89.    
  90.     public Set<String> keys() {
  91.         return this.map.keySet();
  92.     }
  93.    
  94.     public String get(String key) {
  95.         return this.map.get(key);
  96.     }
  97.    
  98.     public String convertToDBValue() {
  99.         if(this.map.isEmpty()) {
  100.             return null;
  101.         }
  102.         StringBuilder bf = new StringBuilder();
  103.         Iterator<String> it = this.map.keySet().iterator();
  104.         while (it.hasNext()) {
  105.             if(bf.length()>0) {
  106.                 bf.append(separator_line);
  107.             }
  108.             String key = (String) it.next();
  109.             String value = this.map.get(key);
  110.             bf.append(key).append(separator).append(value);
  111.         }
  112.         return bf.toString();
  113.     }
  114.    
  115.     @Override
  116.     public String toString(){
  117.         if(this.map.isEmpty()) {
  118.             return "";
  119.         }
  120.         StringBuilder bf = new StringBuilder();
  121.         Iterator<String> it = this.map.keySet().iterator();
  122.         while (it.hasNext()) {
  123.             if(bf.length()>0) {
  124.                 bf.append("\n");
  125.             }
  126.             String key = (String) it.next();
  127.             String value = this.map.get(key);
  128.             bf.append(key).append("=").append(value);
  129.         }
  130.         return bf.toString();
  131.     }
  132. }