PropertiesSerializator.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;

  21. import java.util.HashMap;
  22. import java.util.List;
  23. import java.util.Map;

  24. import org.openspcoop2.core.commons.CoreException;
  25. import org.openspcoop2.utils.transport.TransportUtils;

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

  34.    
  35.     public static final String TEMPLATE_RITORNO_A_CAPO = "#@$#@$#@$#";
  36.    
  37.     protected Map<String, List<String>> properties;

  38.     public Map<String, List<String>> getProperties() {
  39.         return this.properties;
  40.     }
  41.    
  42.     public PropertiesSerializator(Map<String, List<String>> properties){
  43.         this.properties = properties;
  44.     }
  45.    
  46.     public String convertToDBColumnValue() throws CoreException{
  47.        
  48.         StringBuilder bf = new StringBuilder();
  49.        
  50.         if(this.properties!=null && !this.properties.isEmpty()) {
  51.             for (Map.Entry<String,List<String>> entry : this.properties.entrySet()) {
  52.                 String key = entry.getKey();
  53.                 if(key.contains(" ")){
  54.                     throw new CoreException("Chiave ["+key+"] contiene il carattere ' ' non ammesso");
  55.                 }
  56.                 List<String> values = this.properties.get(key);
  57.                 addDBColumnValue(values, bf, key);
  58.             }
  59.         }
  60.        
  61.         return bf.toString();
  62.        
  63.     }
  64.     private void addDBColumnValue(List<String> values, StringBuilder bf, String key) {
  65.         if(values!=null && !values.isEmpty()) {
  66.             for (String value : values) {
  67.                 /**if(value.contains("\n")){
  68.                     throw new Exception("Valore ["+value+"] della chiave ["+key+"] contiene il carattere '\\n' non ammesso");
  69.                 }*/
  70.                 while(value.contains("\n")){
  71.                     value = value.replace("\n", TEMPLATE_RITORNO_A_CAPO);
  72.                 }
  73.                 if(bf.length()>0){
  74.                     bf.append("\n");
  75.                 }
  76.                 bf.append(key).append("=").append(value);      
  77.             }
  78.         }
  79.     }
  80.    
  81.     public static Map<String, List<String>> convertoFromDBColumnValue(String dbValue) throws CoreException{
  82.        
  83.         Map<String, List<String>> map = new HashMap<>();
  84.        
  85.         if(dbValue!=null && !"".equals(dbValue)){
  86.            
  87.             String [] split = dbValue.split("\n");
  88.             for (int i = 0; i < split.length; i++) {
  89.                 String keyValueTmp = split[i].trim();
  90.                 String [] keyValue = keyValueTmp.split("=");
  91.                 if(keyValue.length==1 && keyValueTmp.endsWith("=")) {
  92.                     String key = keyValue[0];
  93.                     String value = "";
  94.                     TransportUtils.addHeader(map, key, value);
  95.                     continue;
  96.                 }
  97.                 if(keyValue.length<2){
  98.                     throw new CoreException("Valore ["+keyValueTmp+"] non contiene una coppia key=value");
  99.                 }
  100.                 String key = keyValue[0];
  101.                 String value = keyValueTmp.substring((key+"=").length());
  102.                 while(value.contains(TEMPLATE_RITORNO_A_CAPO)){
  103.                     value = value.replace(TEMPLATE_RITORNO_A_CAPO, "\n");
  104.                 }
  105.                 TransportUtils.addHeader(map, key, value);
  106.             }
  107.            
  108.         }
  109.        
  110.         return map;
  111.        
  112.     }
  113. }