WizardAPIUtilities.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.protocol.engine.archive;

  21. import java.io.ByteArrayInputStream;
  22. import java.io.File;
  23. import java.io.FileOutputStream;
  24. import java.util.Arrays;
  25. import java.util.List;
  26. import java.util.Map;
  27. import java.util.Properties;

  28. import org.apache.commons.lang.StringUtils;
  29. import org.openspcoop2.core.registry.AccordoServizioParteComune;
  30. import org.openspcoop2.core.registry.GruppiAccordo;
  31. import org.openspcoop2.core.registry.GruppoAccordo;
  32. import org.openspcoop2.core.registry.IdSoggetto;
  33. import org.openspcoop2.core.registry.ProtocolProperty;
  34. import org.openspcoop2.core.registry.constants.FormatoSpecifica;
  35. import org.openspcoop2.core.registry.constants.ProfiloCollaborazione;
  36. import org.openspcoop2.core.registry.constants.ServiceBinding;
  37. import org.openspcoop2.core.registry.utils.CleanerOpenSPCoop2Extensions;
  38. import org.openspcoop2.protocol.engine.ProtocolFactoryManager;
  39. import org.openspcoop2.protocol.information_missing.constants.Costanti;
  40. import org.openspcoop2.protocol.sdk.ConfigurazionePdD;
  41. import org.openspcoop2.protocol.sdk.IProtocolFactory;
  42. import org.openspcoop2.protocol.sdk.ProtocolException;
  43. import org.openspcoop2.utils.LoggerWrapperFactory;
  44. import org.openspcoop2.utils.beans.WriteToSerializerType;
  45. import org.openspcoop2.utils.resources.FileSystemUtilities;
  46. import org.openspcoop2.utils.resources.Loader;
  47. import org.slf4j.Logger;

  48. /**
  49.  *  WizardAPIUtilities
  50.  *
  51.  * @author Poli Andrea (apoli@link.it)
  52.  * @author $Author$
  53.  * @version $Rev$, $Date$
  54.  */
  55. public class WizardAPIUtilities {

  56.     private static final String EMPTY = "Empty?";
  57.     private static final String SUFFIX_NON_ESISTENTE = " non esistente";
  58.     private static final String SUFFIX_NON_LEGGIBILE = " non leggibile";
  59.    
  60.     // Use: java WizardAPIUtilities outputFile profile pathInterface pathProperties [pathProfileProperties]
  61.     private static final String USE = "Usage: java WizardAPIUtilities pathOutputFile profile pathInterface pathProperties [pathProfileProperties]";
  62.    
  63.     private static final Logger log = LoggerWrapperFactory.getLogger(WizardAPIUtilities.class);
  64.    
  65.     private static final String PROPERTIES_NAME = "name";
  66.     private static final String PROPERTIES_VERSION = "version";
  67.     private static final String PROPERTIES_DESCRIPTION = "description";
  68.     private static final String PROPERTIES_SERVICE_BINDING = "serviceBinding";
  69.     private static final String PROPERTIES_INTERFACE_TYPE = "interfaceType";
  70.     private static final String PROPERTIES_TAG = "tag";
  71.     private static final String PROPERTIES_CANALE = "canale";
  72.    
  73.     public static void main(String[] args) throws ProtocolException {
  74.        
  75.         if(args==null || args.length<4) {
  76.             throw new ProtocolException("ERROR: argomenti non forniti\n"+USE);
  77.         }
  78.        
  79.         String pathOutputFile = readPathOutpuFile(args, 0);
  80.        
  81.         String profile = readProfile(args, 1);
  82.        
  83.         byte[] bytesInterface = readInterface(args, 2);
  84.        
  85.         Properties properties = readProperties(args, 3);
  86.        
  87.         Properties profileProperties = readProfileProperties(args, 4);
  88.        
  89.         IProtocolFactory<?> protocol = ProtocolFactoryManager.getInstance().getProtocolFactoryByName(profile);
  90.                
  91.         AccordoServizioParteComune as = buildAccordo(properties, bytesInterface, protocol);
  92.        
  93.         addProfileProperties(as, profileProperties);
  94.        
  95.         protocol.createArchive().setProtocolInfo(as);
  96.        
  97.         CleanerOpenSPCoop2Extensions cleaner = new CleanerOpenSPCoop2Extensions();
  98.         cleaner.clean(as);
  99.        
  100.         try(FileOutputStream fout = new FileOutputStream(pathOutputFile)){
  101.             as.writeTo(fout, WriteToSerializerType.XML_JAXB);
  102.         }catch(Exception e) {
  103.             throw new ProtocolException("Serializzazione in '"+pathOutputFile+"' non riuscita: "+e.getMessage(),e);
  104.         }
  105.        
  106.         log.info("Serializzazione completata");
  107.     }
  108.    

  109.     private static String readPathOutpuFile(String [] args, int position) throws ProtocolException {
  110.         String pathOutputFile = args[position];
  111.         if(pathOutputFile==null || StringUtils.isEmpty(pathOutputFile)) {
  112.             throw new ProtocolException("ERROR: argomento 'pathOutputFile' non fornito\n"+USE);
  113.         }
  114.         File fPathOutputFile = new File(pathOutputFile);
  115.         if(fPathOutputFile.exists()) {
  116.             throw new ProtocolException("ERROR: argomento 'pathOutputFile' contiene il riferimento ad un file "+fPathOutputFile.getAbsolutePath()+" già esistente");
  117.         }
  118.         return pathOutputFile;
  119.     }
  120.        
  121.     private static String readProfile(String [] args, int position) throws ProtocolException {
  122.         String profile = args[position];
  123.         if(profile==null || StringUtils.isEmpty(profile)) {
  124.             throw new ProtocolException("ERROR: argomento 'profile' non fornito\n"+USE);
  125.         }
  126.         try {
  127.             ConfigurazionePdD configPdD = new ConfigurazionePdD();
  128.             configPdD.setLoader(new Loader());
  129.             configPdD.setLog(log);
  130.             ProtocolFactoryManager.initialize(log, configPdD, profile);
  131.         }catch(Exception e) {
  132.             throw new ProtocolException("ERROR: argomento 'profile="+profile+"' non valido:"+e.getMessage(),e);
  133.         }
  134.         return profile;
  135.     }
  136.    
  137.     private static byte[] readInterface(String [] args, int position) throws ProtocolException {
  138.         String pathInterface = args[position];
  139.         if(pathInterface==null || StringUtils.isEmpty(pathInterface)) {
  140.             throw new ProtocolException("ERROR: argomento 'pathInterface' non fornito\n"+USE);
  141.         }
  142.         File fPathInterface = new File(pathInterface);
  143.         checkExists(fPathInterface, "pathInterface");
  144.         byte[] bytesInterface = null;
  145.         try {
  146.             bytesInterface = FileSystemUtilities.readBytesFromFile(fPathInterface);
  147.             if(bytesInterface==null || bytesInterface.length<=0) {
  148.                 throw new ProtocolException(EMPTY);
  149.             }
  150.         }catch(Exception e) {
  151.             throw new ProtocolException("ERROR: argomento 'pathInterface="+pathInterface+"' non valido:"+e.getMessage(),e);
  152.         }
  153.         return bytesInterface;
  154.     }
  155.    
  156.     private static Properties readProperties(String [] args, int position) throws ProtocolException {
  157.         String pathProperties = args[position];
  158.         if(pathProperties==null || StringUtils.isEmpty(pathProperties)) {
  159.             throw new ProtocolException("ERROR: argomento 'pathProperties' non fornito\n"+USE);
  160.         }
  161.         File fPathProperties = new File(pathProperties);
  162.         checkExists(fPathProperties, "pathProperties");
  163.         byte[] bytesProperties = null;
  164.         try {
  165.             bytesProperties = FileSystemUtilities.readBytesFromFile(fPathProperties);
  166.             if(bytesProperties==null || bytesProperties.length<=0) {
  167.                 throw new ProtocolException(EMPTY);
  168.             }
  169.         }catch(Exception e) {
  170.             throw new ProtocolException("ERROR: argomento 'pathProperties="+pathProperties+"' non è un file di proprietà corretto:"+e.getMessage(),e);
  171.         }
  172.         return convertToProperties("pathProperties="+pathProperties,bytesProperties);
  173.     }
  174.    
  175.     private static Properties readProfileProperties(String [] args, int position) throws ProtocolException {
  176.         Properties profileProperties = null;
  177.         if(args.length>=(position+1)) {
  178.             String pathProfileProperties = args[position];
  179.             if(pathProfileProperties!=null && !StringUtils.isEmpty(pathProfileProperties)) {
  180.                 File fPathProfileProperties = new File(pathProfileProperties);
  181.                 checkExists(fPathProfileProperties, "pathProfileProperties");
  182.                 byte[] bytesProfileProperties = null;
  183.                 try {
  184.                     bytesProfileProperties = FileSystemUtilities.readBytesFromFile(fPathProfileProperties);
  185.                     if(bytesProfileProperties==null || bytesProfileProperties.length<=0) {
  186.                         throw new ProtocolException(EMPTY);
  187.                     }
  188.                 }catch(Exception e) {
  189.                     throw new ProtocolException("ERROR: argomento 'pathProfileProperties="+pathProfileProperties+"' non è un file di proprietà corretto:"+e.getMessage(),e);
  190.                 }
  191.                 profileProperties = convertToProperties("pathProfileProperties="+pathProfileProperties,bytesProfileProperties);
  192.             }
  193.         }
  194.         return profileProperties;
  195.     }
  196.    
  197.     private static void checkExists(File f, String name) throws ProtocolException {
  198.         if(!f.exists()) {
  199.             throw new ProtocolException("ERROR: argomento '"+name+"' contiene il riferimento ad un file "+f.getAbsolutePath()+SUFFIX_NON_ESISTENTE);
  200.         }
  201.         if(!f.canRead()) {
  202.             throw new ProtocolException("ERROR: argomento '"+name+"' contiene il riferimento ad un file "+f.getAbsolutePath()+SUFFIX_NON_LEGGIBILE);
  203.         }
  204.     }
  205.    
  206.     private static Properties convertToProperties(String name,byte[]b) throws ProtocolException {
  207.         Properties tmp = new Properties();
  208.         try(ByteArrayInputStream bin = new ByteArrayInputStream(b)){
  209.             tmp.load(bin);
  210.         }catch(Exception e) {
  211.             throw new ProtocolException("ERROR: conversione non  riuscita, argomento '"+name+"' non corretto:"+e.getMessage(),e);
  212.         }
  213.         return tmp;
  214.     }
  215.    
  216.     private static AccordoServizioParteComune buildAccordo(Properties properties, byte[] bytesInterface, IProtocolFactory<?> protocol) throws ProtocolException {
  217.         AccordoServizioParteComune as = new AccordoServizioParteComune();
  218.         as.setNome(readProperty(properties, PROPERTIES_NAME, true));
  219.         as.setDescrizione(readProperty(properties, PROPERTIES_DESCRIPTION, false));
  220.        
  221.         as.setByteWsdlConcettuale(bytesInterface);
  222.        
  223.         as.setProfiloCollaborazione(ProfiloCollaborazione.SINCRONO);
  224.         /**as.setFiltroDuplicati(StatoFunzionalita.DISABILITATO);
  225.         as.setConfermaRicezione(StatoFunzionalita.DISABILITATO);
  226.         as.setConsegnaInOrdine(StatoFunzionalita.DISABILITATO);
  227.         as.setIdCollaborazione(StatoFunzionalita.DISABILITATO);
  228.         as.setIdRiferimentoRichiesta(StatoFunzionalita.DISABILITATO);
  229.         as.setUtilizzoSenzaAzione(true);// default true*/
  230.    
  231.         String serviceBinding = null;
  232.         try {
  233.             serviceBinding = readProperty(properties, PROPERTIES_SERVICE_BINDING, true);
  234.             as.setServiceBinding(ServiceBinding.toEnumConstant(serviceBinding, true));
  235.         }catch(Exception e) {
  236.             throw new ProtocolException("ERROR: 'pathProperties' contiene una proprietà '"+PROPERTIES_SERVICE_BINDING+"' valorizzata con un valore '"+serviceBinding+"' non supportato:"+e.getMessage(),e);
  237.         }
  238.         if(ServiceBinding.SOAP.equals(as.getServiceBinding())) {
  239.             as.setByteWsdlLogicoErogatore(bytesInterface);
  240.         }
  241.         String formatoSpecifica = null;
  242.         try{
  243.             formatoSpecifica = readProperty(properties, PROPERTIES_INTERFACE_TYPE, true);
  244.             as.setFormatoSpecifica(FormatoSpecifica.toEnumConstant(formatoSpecifica, true));
  245.         }catch(Exception e) {
  246.             throw new ProtocolException("ERROR: 'pathProperties' contiene una proprietà '"+PROPERTIES_INTERFACE_TYPE+"' valorizzata con un valore '"+formatoSpecifica+"' non supportato:"+e.getMessage(),e);
  247.         }
  248.        
  249.         String tipoSoggettoDefault = protocol.getManifest().getRegistry().getOrganization().getTypes().getType(0).getName();
  250.         IdSoggetto assr = new IdSoggetto();
  251.         assr.setTipo(tipoSoggettoDefault);
  252.         assr.setNome(Costanti.NOME_SOGGETTO_DEFAULT);
  253.         as.setSoggettoReferente(assr);
  254.        
  255.         String versioneApi = null;
  256.         try {
  257.             versioneApi = readProperty(properties, PROPERTIES_VERSION, true);
  258.             as.setVersione(Integer.parseInt(versioneApi));
  259.         }catch(Exception e) {
  260.             throw new ProtocolException("ERROR: argomento 'pathProperties' contiene una proprietà '"+PROPERTIES_VERSION+"' valorizzata con un numero '"+versioneApi+"' che non rappresenta un intero:"+e.getMessage(),e);
  261.         }
  262.        
  263.         String gruppi = readProperty(properties, PROPERTIES_TAG, false);
  264.         if(gruppi!=null && StringUtils.isNotEmpty(gruppi)) {
  265.             List<String> nomiGruppi = Arrays.asList(gruppi.split(","));
  266.             for (String nomeGruppo : nomiGruppi) {
  267.                 nomeGruppo = nomeGruppo.trim();
  268.                 GruppoAccordo gruppoAccordo = new GruppoAccordo();
  269.                 gruppoAccordo.setNome(nomeGruppo);
  270.                 if(as.getGruppi()==null) {
  271.                     as.setGruppi(new GruppiAccordo());
  272.                 }
  273.                 as.getGruppi().addGruppo(gruppoAccordo );
  274.             }
  275.         }
  276.        
  277.         String canale = readProperty(properties, PROPERTIES_CANALE, false);
  278.         if(canale!=null && StringUtils.isNotEmpty(canale)) {
  279.             as.setCanale(canale);
  280.         }
  281.        
  282.         return as;
  283.     }

  284.     private static String readProperty(Properties properties, String property, boolean required) throws ProtocolException {
  285.         String v = properties.getProperty(property);
  286.         if(v!=null) {
  287.             v = v.trim();
  288.         }
  289.         if(required &&
  290.                 (v==null || StringUtils.isEmpty(v))
  291.             ) {
  292.             throw new ProtocolException("Property '"+property+"' not found");
  293.         }
  294.         return v;
  295.     }
  296.    
  297.     private static final String CAST_BOOLEAN = "(boolean) ";
  298.     private static final String CAST_NUMBER = "(number) ";
  299.     private static final String CAST_INT = "(int) ";
  300.     private static final String CAST_LONG = "(long) ";
  301.     private static final String CAST_FILE = "(file) ";
  302.     private static void addProfileProperties(AccordoServizioParteComune as, Properties profileProperties) throws ProtocolException {
  303.         if(profileProperties!=null && !profileProperties.isEmpty()) {
  304.             for (Map.Entry<Object,Object> entry : profileProperties.entrySet()) {
  305.                 addProfileProperty(as, entry);
  306.             }
  307.         }
  308.     }
  309.     private static void addProfileProperty(AccordoServizioParteComune as, Map.Entry<Object,Object> entry) throws ProtocolException {
  310.         String key = (String) entry.getKey();
  311.         String value = (String) entry.getValue();
  312.         ProtocolProperty pp = new ProtocolProperty();
  313.         pp.setName(key);
  314.         if(value.startsWith(CAST_BOOLEAN)) {
  315.             pp.setBooleanValue(Boolean.parseBoolean(value.substring(CAST_BOOLEAN.length())));
  316.         }
  317.         else if(value.startsWith(CAST_NUMBER)) {
  318.             pp.setNumberValue(Long.parseLong(value.substring(CAST_NUMBER.length())));
  319.         }
  320.         else if(value.startsWith(CAST_INT)) {
  321.             pp.setNumberValue(Long.parseLong(value.substring(CAST_INT.length())));
  322.         }
  323.         else if(value.startsWith(CAST_LONG)) {
  324.             pp.setNumberValue(Long.parseLong(value.substring(CAST_LONG.length())));
  325.         }
  326.         else if(value.startsWith(CAST_FILE)) {
  327.             String file = value.substring(CAST_FILE.length());
  328.             pp.setFile(file);
  329.             try {
  330.                 pp.setByteFile(FileSystemUtilities.readBytesFromFile(file));
  331.             }catch(Exception e) {
  332.                 throw new ProtocolException("ERROR: argomento 'pathProfileProperties' contiene una proprietà '"+key+"' valorizzata con un file '"+file+"' non accessibile:"+e.getMessage(),e);
  333.             }
  334.         }
  335.         else {
  336.             pp.setValue(value);
  337.         }
  338.         as.addProtocolProperty(pp);
  339.     }
  340. }