ExtendedConnettoreConverter.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.plugins;

  21. import java.util.ArrayList;
  22. import java.util.Enumeration;
  23. import java.util.List;
  24. import java.util.Properties;

  25. import org.openspcoop2.core.constants.CostantiConnettori;

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

  34.     public static void readExtendedInfoFromConnettore(List<ExtendedConnettore> list,org.openspcoop2.core.config.Connettore connettore) throws ExtendedException{
  35.         Properties properties = new Properties();
  36.         if(connettore!=null){
  37.             for (org.openspcoop2.core.config.Property property : connettore.getPropertyList()) {
  38.                 if(property.getNome().startsWith(CostantiConnettori.CONNETTORE_EXTENDED_PREFIX)){
  39.                     properties.put(property.getNome().substring(CostantiConnettori.CONNETTORE_EXTENDED_PREFIX.length()), property.getValore());
  40.                 }
  41.             }
  42.         }
  43.         if(properties.size()>0){
  44.             readExtendedInfoFromConnettore(list,properties);
  45.         }
  46.     }
  47.     public static void fillExtendedInfoIntoConnettore(List<ExtendedConnettore> list, org.openspcoop2.core.config.Connettore connettore) throws ExtendedException{
  48.         if(list!=null && list.size()>0){
  49.            
  50.             List<String> pDaEliminare = new ArrayList<>();
  51.             for (org.openspcoop2.core.config.Property property : connettore.getPropertyList()) {
  52.                 if(property.getNome().startsWith(CostantiConnettori.CONNETTORE_EXTENDED_PREFIX)){
  53.                     pDaEliminare.add(property.getNome());
  54.                 }
  55.             }
  56.             while (pDaEliminare.size()>0) {
  57.                 String pNameDaEliminare = pDaEliminare.remove(0);
  58.                 int i = 0;
  59.                 boolean found = false;
  60.                 for (; i < connettore.sizePropertyList(); i++) {
  61.                     if(connettore.getProperty(i).getNome().equals(pNameDaEliminare)){
  62.                         found = true;
  63.                         break;
  64.                     }
  65.                 }
  66.                 if(found){
  67.                     connettore.removeProperty(i);  
  68.                 }
  69.             }
  70.            
  71.             Properties properties = toProperties(list);
  72.             Enumeration<?> keys = properties.keys();
  73.             while (keys.hasMoreElements()) {
  74.                 String key = (String) keys.nextElement();
  75.                 String value = properties.getProperty(key);
  76.                
  77.                 if((CostantiConnettori.CONNETTORE_EXTENDED_PREFIX+key).length()>255){
  78.                     throw new ExtendedException("Property ["+key+"] troppo lunga (max-length:"+(255-CostantiConnettori.CONNETTORE_EXTENDED_PREFIX.length())+")");
  79.                 }
  80.                
  81.                 org.openspcoop2.core.config.Property property = new org.openspcoop2.core.config.Property();
  82.                 property.setNome(CostantiConnettori.CONNETTORE_EXTENDED_PREFIX+key);
  83.                 property.setValore(value);
  84.                 connettore.addProperty(property);
  85.             }
  86.         }
  87.        
  88.     }
  89.    
  90.     public static void readExtendedInfoFromConnettore(List<ExtendedConnettore> list,org.openspcoop2.core.registry.Connettore connettore) throws ExtendedException{
  91.         Properties properties = new Properties();
  92.         if(connettore!=null){
  93.             for (org.openspcoop2.core.registry.Property property : connettore.getPropertyList()) {
  94.                 if(property.getNome().startsWith(CostantiConnettori.CONNETTORE_EXTENDED_PREFIX)){
  95.                     properties.put(property.getNome().substring(CostantiConnettori.CONNETTORE_EXTENDED_PREFIX.length()), property.getValore());
  96.                 }
  97.             }
  98.         }
  99.         if(properties.size()>0){
  100.             readExtendedInfoFromConnettore(list,properties);
  101.         }
  102.     }
  103.    
  104.     public static void fillExtendedInfoIntoConnettore(List<ExtendedConnettore> list, org.openspcoop2.core.registry.Connettore connettore) throws ExtendedException{
  105.         if(list!=null && list.size()>0){
  106.            
  107.             List<String> pDaEliminare = new ArrayList<>();
  108.             for (org.openspcoop2.core.registry.Property property : connettore.getPropertyList()) {
  109.                 if(property.getNome().startsWith(CostantiConnettori.CONNETTORE_EXTENDED_PREFIX)){
  110.                     pDaEliminare.add(property.getNome());
  111.                 }
  112.             }
  113.             while (pDaEliminare.size()>0) {
  114.                 String pNameDaEliminare = pDaEliminare.remove(0);
  115.                 int i = 0;
  116.                 boolean found = false;
  117.                 for (; i < connettore.sizePropertyList(); i++) {
  118.                     if(connettore.getProperty(i).getNome().equals(pNameDaEliminare)){
  119.                         found = true;
  120.                         break;
  121.                     }
  122.                 }
  123.                 if(found){
  124.                     connettore.removeProperty(i);  
  125.                 }
  126.             }
  127.            
  128.             Properties properties = toProperties(list);
  129.             Enumeration<?> keys = properties.keys();
  130.             while (keys.hasMoreElements()) {
  131.                 String key = (String) keys.nextElement();
  132.                 String value = properties.getProperty(key);
  133.                
  134.                 if((CostantiConnettori.CONNETTORE_EXTENDED_PREFIX+key).length()>255){
  135.                     throw new ExtendedException("Property ["+key+"] troppo lunga (max-length:"+(255-CostantiConnettori.CONNETTORE_EXTENDED_PREFIX.length())+")");
  136.                 }
  137.                
  138.                 org.openspcoop2.core.registry.Property property = new org.openspcoop2.core.registry.Property();
  139.                 property.setNome(CostantiConnettori.CONNETTORE_EXTENDED_PREFIX+key);
  140.                 property.setValore(value);
  141.                 connettore.addProperty(property);
  142.             }
  143.         }
  144.     }
  145.    
  146.    
  147.     // Commons
  148.    
  149.     public static String buildId(String idExtended,String idExtendedItem){
  150.         return idExtended+"_"+idExtendedItem;
  151.     }
  152.    
  153.     private static void readExtendedInfoFromConnettore(List<ExtendedConnettore> list,Properties properties) throws ExtendedException{
  154.        
  155.         try{
  156.            
  157.             if(list!=null){
  158.                 for (ExtendedConnettore extendedConnettore : list) {
  159.                
  160.                     if(properties.containsKey(extendedConnettore.getId())){
  161.                        
  162.                         String v = properties.getProperty(extendedConnettore.getId());
  163.                         extendedConnettore.setEnabled("true".equals(v));
  164.                        
  165.                         if(extendedConnettore.isEnabled()){
  166.                            
  167.                             if(extendedConnettore.getListItem()!=null && extendedConnettore.getListItem().size()>0){
  168.                                 for (ExtendedConnettoreItem extendedConnettoreItem : extendedConnettore.getListItem()) {
  169.                                    
  170.                                     String id = buildId(extendedConnettore.getId(),extendedConnettoreItem.getId());
  171.                                     if(properties.containsKey(id)){
  172.                                        
  173.                                         String vProperty = properties.getProperty(id);
  174.                                         extendedConnettoreItem.setValue(vProperty);
  175.                                        
  176.                                     }
  177.                                    
  178.                                 }
  179.                             }
  180.                            
  181.                         }
  182.                        
  183.                     }
  184.                    
  185.                 }
  186.             }      
  187.            
  188.         }catch(Exception e){
  189.             throw new ExtendedException(e.getMessage(),e);
  190.         }
  191.     }
  192.    
  193.     private static Properties toProperties(List<ExtendedConnettore> list) throws ExtendedException{
  194.    
  195.         try{
  196.        
  197.             Properties properties = new Properties();
  198.            
  199.             if(list!=null){
  200.                 for (ExtendedConnettore extendedConnettore : list) {
  201.                    
  202.                     properties.put(extendedConnettore.getId(), extendedConnettore.isEnabled()+"");
  203.                    
  204.                     if(extendedConnettore.isEnabled()){
  205.                        
  206.                         if(extendedConnettore.getListItem()!=null && extendedConnettore.getListItem().size()>0){
  207.                             for (ExtendedConnettoreItem extendedConnettoreItem : extendedConnettore.getListItem()) {
  208.                                
  209.                                 String v = extendedConnettoreItem.getValue();
  210.                                 if(v!=null && !"".equals(v)){
  211.                                     // altrimenti la stringa vuota equivale ad non avere un valore per l'item
  212.                                     properties.put(buildId(extendedConnettore.getId(),extendedConnettoreItem.getId()),
  213.                                             extendedConnettoreItem.getValue());
  214.                                 }
  215.                                
  216.                             }
  217.                         }
  218.                        
  219.                     }
  220.                 }
  221.             }
  222.            
  223.             return properties;
  224.        
  225.         }catch(Exception e){
  226.             throw new ExtendedException(e.getMessage(),e);
  227.         }
  228.     }
  229.    
  230. }