ArchiviCore.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.servlet.archivi;

  21. import java.io.ByteArrayOutputStream;
  22. import java.io.OutputStream;
  23. import java.sql.Connection;
  24. import java.util.List;

  25. import org.openspcoop2.core.registry.Documento;
  26. import org.openspcoop2.core.registry.constants.ProprietariDocumento;
  27. import org.openspcoop2.core.registry.driver.DriverRegistroServiziException;
  28. import org.openspcoop2.core.registry.driver.DriverRegistroServiziNotFound;
  29. import org.openspcoop2.protocol.basic.registry.ConfigIntegrationReader;
  30. import org.openspcoop2.protocol.basic.registry.RegistryReader;
  31. import org.openspcoop2.protocol.engine.ProtocolFactoryManager;
  32. import org.openspcoop2.protocol.engine.archive.ArchiveValidator;
  33. import org.openspcoop2.protocol.engine.archive.DeleterArchiveUtils;
  34. import org.openspcoop2.protocol.engine.archive.ExporterArchiveUtils;
  35. import org.openspcoop2.protocol.engine.archive.ImportInformationMissingCollection;
  36. import org.openspcoop2.protocol.engine.archive.ImportInformationMissingException;
  37. import org.openspcoop2.protocol.engine.archive.ImporterArchiveUtils;
  38. import org.openspcoop2.protocol.sdk.IProtocolFactory;
  39. import org.openspcoop2.protocol.sdk.archive.Archive;
  40. import org.openspcoop2.protocol.sdk.archive.ArchiveCascadeConfiguration;
  41. import org.openspcoop2.protocol.sdk.archive.ArchiveEsitoDelete;
  42. import org.openspcoop2.protocol.sdk.archive.ArchiveEsitoImport;
  43. import org.openspcoop2.protocol.sdk.archive.ArchiveMode;
  44. import org.openspcoop2.protocol.sdk.archive.ArchiveModeType;
  45. import org.openspcoop2.protocol.sdk.archive.ExportMode;
  46. import org.openspcoop2.protocol.sdk.archive.IArchive;
  47. import org.openspcoop2.protocol.sdk.archive.MapPlaceholder;
  48. import org.openspcoop2.protocol.sdk.constants.ArchiveType;
  49. import org.openspcoop2.web.ctrlstat.core.ControlStationCore;
  50. import org.openspcoop2.web.ctrlstat.driver.DriverControlStationDB;
  51. import org.openspcoop2.web.ctrlstat.driver.DriverControlStationException;
  52. import org.openspcoop2.web.ctrlstat.servlet.ConsoleHelper;

  53. /**
  54.  * ArchiviCore
  55.  *
  56.  * @author Andrea Poli (apoli@link.it)
  57.  * @author $Author$
  58.  * @version $Rev$, $Date$
  59.  */
  60. public class ArchiviCore extends ControlStationCore {

  61.     public ArchiviCore() throws Exception {
  62.         super();
  63.     }
  64.     public ArchiviCore(ControlStationCore core) throws Exception {
  65.         super(core);
  66.     }

  67.     public boolean isCascadeEnabled(List<ExportMode> exportModes,String exportMode){
  68.         boolean cascadeEnabled = false;
  69.         for (ExportMode exp : exportModes) {
  70.             if(exp.equals(exportMode) &&
  71.                 exp.getCascade()!=null &&
  72.                         (exp.getCascade().isCascadePdd() ||
  73.                                 exp.getCascade().isCascadeRuoli() ||
  74.                                 exp.getCascade().isCascadeScope() ||
  75.                                 exp.getCascade().isCascadeSoggetti() ||
  76.                                 exp.getCascade().isCascadeServiziApplicativi() ||
  77.                                 exp.getCascade().isCascadePorteDelegate() ||
  78.                                 exp.getCascade().isCascadePorteApplicative() ||
  79.                                 exp.getCascade().isCascadeAccordoCooperazione() ||
  80.                                 exp.getCascade().isCascadeAccordoServizioComposto() ||
  81.                                 exp.getCascade().isCascadeAccordoServizioParteComune() ||
  82.                                 exp.getCascade().isCascadeAccordoServizioParteSpecifica() ||
  83.                                 exp.getCascade().isCascadeFruizioni()
  84.                         )
  85.             ){
  86.                 cascadeEnabled = true;
  87.                 break;
  88.             }
  89.         }
  90.         return cascadeEnabled;
  91.     }
  92.     public ArchiveCascadeConfiguration getCascadeConfig(List<ExportMode> exportModes,String exportMode){
  93.         for (ExportMode exp : exportModes) {
  94.             if(exp.equals(exportMode)){
  95.                 return exp.getCascade();
  96.             }
  97.         }
  98.         return null;
  99.     }
  100.    
  101.     public byte[] export(String userLogin, boolean smista,
  102.             String protocol,Archive archive,ArchiveMode archiveMode) throws DriverControlStationException, ImportInformationMissingException{
  103.        
  104.         Connection con = null;
  105.         DriverControlStationDB driver = null;

  106.         ByteArrayOutputStream bout = null;
  107.         try {
  108.             // prendo una connessione
  109.             con = ControlStationCore.dbM.getConnection();
  110.             // istanzio il driver
  111.             driver = new DriverControlStationDB(con, null, this.tipoDB);

  112.             ArchiveEngine importerEngine = new ArchiveEngine(driver.getDriverRegistroServiziDB(),
  113.                     driver.getDriverConfigurazioneDB(),
  114.                     driver.getJdbcServiceManagerPlugins(),
  115.                     driver.getJdbcServiceManagerControlloTraffico(),
  116.                     driver.getJdbcServiceManagerAllarmi(),
  117.                     this, smista, userLogin);
  118.            
  119.             ExporterArchiveUtils exportUtils = new ExporterArchiveUtils(importerEngine, log);
  120.            
  121.             bout = new ByteArrayOutputStream();
  122.             exportUtils.export(protocol, archive, bout, archiveMode);
  123.            
  124.             bout.flush();
  125.             bout.close();
  126.             return bout.toByteArray();
  127.            
  128.         }
  129.         catch(ImportInformationMissingException iime) {
  130.             throw iime;
  131.         }
  132.         catch(Exception e) {
  133.             throw new DriverControlStationException(getPrefixError("export",  e),e);
  134.         }
  135.         finally {
  136.             ControlStationCore.dbM.releaseConnection(con);
  137.         }
  138.     }
  139.    
  140.     public void export(String userLogin, boolean smista,
  141.             String protocol,Archive archive,OutputStream out, ArchiveMode archiveMode) throws DriverControlStationException, ImportInformationMissingException{
  142.        
  143.         Connection con = null;
  144.         DriverControlStationDB driver = null;

  145.         try {
  146.             // prendo una connessione
  147.             con = ControlStationCore.dbM.getConnection();
  148.             // istanzio il driver
  149.             driver = new DriverControlStationDB(con, null, this.tipoDB);

  150.             ArchiveEngine importerEngine = new ArchiveEngine(driver.getDriverRegistroServiziDB(),
  151.                     driver.getDriverConfigurazioneDB(),
  152.                     driver.getJdbcServiceManagerPlugins(),
  153.                     driver.getJdbcServiceManagerControlloTraffico(),
  154.                     driver.getJdbcServiceManagerAllarmi(),
  155.                     this, smista, userLogin);
  156.            
  157.             ExporterArchiveUtils exportUtils = new ExporterArchiveUtils(importerEngine, log);
  158.            
  159.             exportUtils.export(protocol, archive, out, archiveMode);
  160.            
  161.         }
  162.         catch(ImportInformationMissingException iime) {
  163.             throw iime;
  164.         }
  165.         catch(Exception e) {
  166.             throw new DriverControlStationException(getPrefixError("export",  e),e);
  167.         }
  168.         finally {
  169.             ControlStationCore.dbM.releaseConnection(con);
  170.         }
  171.     }
  172.    
  173.     public Archive readArchiveForExport(String userLogin, boolean smista,ArchiveType tipoEsportazione,List<?> listObject, ArchiveCascadeConfiguration cascadeConfig) throws DriverControlStationException, ImportInformationMissingException{
  174.        
  175.         Connection con = null;
  176.         DriverControlStationDB driver = null;

  177.         try {
  178.             // prendo una connessione
  179.             con = ControlStationCore.dbM.getConnection();
  180.             // istanzio il driver
  181.             driver = new DriverControlStationDB(con, null, this.tipoDB);

  182.             ArchiveEngine importerEngine = new ArchiveEngine(driver.getDriverRegistroServiziDB(),
  183.                     driver.getDriverConfigurazioneDB(),
  184.                     driver.getJdbcServiceManagerPlugins(),
  185.                     driver.getJdbcServiceManagerControlloTraffico(),
  186.                     driver.getJdbcServiceManagerAllarmi(),
  187.                     this, smista, userLogin);
  188.            
  189.             ExporterArchiveUtils exportUtils = new ExporterArchiveUtils(importerEngine, log);
  190.             Archive archive = new Archive();
  191.             exportUtils.fillArchive(archive, tipoEsportazione, listObject, cascadeConfig);
  192.            
  193.             return archive;
  194.            
  195.         }
  196.         catch(ImportInformationMissingException iime) {
  197.             throw iime;
  198.         }
  199.         catch(Exception e) {
  200.             throw new DriverControlStationException(getPrefixError("readArchiveForExport",  e),e);
  201.         }
  202.         finally {
  203.             ControlStationCore.dbM.releaseConnection(con);
  204.         }
  205.     }
  206.    
  207.     public String importArchive(Archive archive,ArchiveMode archiveMode,String protocol, String userLogin, boolean smista,
  208.             boolean updateAbilitato,
  209.             boolean importPolicyConfig,
  210.             boolean importPluginConfig, boolean checkExistsPluginConfig,
  211.             boolean importConfig, String nomePddOperativa,
  212.             ConsoleHelper consoleHelper) throws DriverControlStationException, ImportInformationMissingException{
  213.        
  214.         Connection con = null;
  215.         DriverControlStationDB driver = null;

  216.         try {
  217.             // prendo una connessione
  218.             con = ControlStationCore.dbM.getConnection();
  219.             // istanzio il driver
  220.             driver = new DriverControlStationDB(con, null, this.tipoDB);

  221.             ArchiveEngine importerEngine = new ArchiveEngine(driver.getDriverRegistroServiziDB(),
  222.                     driver.getDriverConfigurazioneDB(),
  223.                     driver.getJdbcServiceManagerPlugins(),
  224.                     driver.getJdbcServiceManagerControlloTraffico(),
  225.                     driver.getJdbcServiceManagerAllarmi(),
  226.                     this, smista, userLogin);
  227.            
  228.             ImporterArchiveUtils importerArchiveUtils =
  229.                     new ImporterArchiveUtils(importerEngine, log, userLogin, nomePddOperativa, this.getImportArchivi_tipoPdD(),
  230.                             consoleHelper.isShowGestioneWorkflowStatoDocumenti(), updateAbilitato,
  231.                             importPolicyConfig,
  232.                             importPluginConfig, checkExistsPluginConfig,
  233.                             importConfig);
  234.            
  235.             ArchiveEsitoImport esito = importerArchiveUtils.importArchive(archive, userLogin,
  236.                     this.isShowAccordiColonnaAzioni(),
  237.                     this.isAbilitatoControlloUnicitaImplementazioneAccordoPerSoggetto(),
  238.                     this.isAbilitatoControlloUnicitaImplementazionePortTypePerSoggetto(),
  239.                     this.isSoggettiApplicativiCredenzialiBasicPermitSameCredentials(),
  240.                     this.isSoggettiApplicativiCredenzialiSslPermitSameCredentials(),
  241.                     this.isSoggettiApplicativiCredenzialiPrincipalPermitSameCredentials());
  242.            
  243.             IProtocolFactory<?> pf = ProtocolFactoryManager.getInstance().getProtocolFactoryByName(protocol);
  244.             IArchive archiveEngine = pf.createArchive();
  245.             return archiveEngine.toString(esito, archiveMode);
  246.                        
  247.         }
  248.         catch(ImportInformationMissingException iime) {
  249.             throw iime;
  250.         }
  251.         catch(Exception e) {
  252.             throw new DriverControlStationException(getPrefixError("importArchive",  e),e);
  253.         }
  254.         finally {
  255.             ControlStationCore.dbM.releaseConnection(con);
  256.         }
  257.     }
  258.    
  259.    
  260.     public String deleteArchive(Archive archive,ArchiveMode archiveMode,String protocol, String userLogin, boolean smista,
  261.              boolean deletePolicyConfig, boolean deletePluginConfig) throws DriverControlStationException, ImportInformationMissingException{
  262.        
  263.         Connection con = null;
  264.         DriverControlStationDB driver = null;

  265.         try {
  266.             // prendo una connessione
  267.             con = ControlStationCore.dbM.getConnection();
  268.             // istanzio il driver
  269.             driver = new DriverControlStationDB(con, null, this.tipoDB);

  270.             ArchiveEngine importerEngine = new ArchiveEngine(driver.getDriverRegistroServiziDB(),
  271.                     driver.getDriverConfigurazioneDB(),
  272.                     driver.getJdbcServiceManagerPlugins(),
  273.                     driver.getJdbcServiceManagerControlloTraffico(),
  274.                     driver.getJdbcServiceManagerAllarmi(),
  275.                     this, smista, userLogin);
  276.            
  277.             DeleterArchiveUtils deleterArchiveUtils =
  278.                     new DeleterArchiveUtils(importerEngine, log, userLogin,
  279.                             deletePolicyConfig, deletePluginConfig);
  280.            
  281.             ArchiveEsitoDelete esito = deleterArchiveUtils.deleteArchive(archive, userLogin);
  282.            
  283.             IProtocolFactory<?> pf = ProtocolFactoryManager.getInstance().getProtocolFactoryByName(protocol);
  284.             IArchive archiveEngine = pf.createArchive();
  285.             return archiveEngine.toString(esito, archiveMode);
  286.                        
  287.         }
  288.         catch(ImportInformationMissingException iime) {
  289.             throw iime;
  290.         }
  291.         catch(Exception e) {
  292.             throw new DriverControlStationException(getPrefixError("deleteArchive",  e),e);
  293.         }
  294.         finally {
  295.             ControlStationCore.dbM.releaseConnection(con);
  296.         }
  297.     }
  298.            
  299.    
  300.     public void validateArchive(Archive archive, String protocolloEffettivo,
  301.             boolean validazioneDocumenti, ImportInformationMissingCollection importInformationMissingCollection,
  302.             String userLogin, boolean delete) throws ImportInformationMissingException, DriverControlStationException{
  303.        
  304.         Connection con = null;
  305.         DriverControlStationDB driver = null;

  306.         try {
  307.             // prendo una connessione
  308.             con = ControlStationCore.dbM.getConnection();
  309.             // istanzio il driver
  310.             driver = new DriverControlStationDB(con, null, this.tipoDB);

  311.             RegistryReader reader = new RegistryReader(driver.getDriverRegistroServiziDB(),ControlStationCore.getLog());
  312.            
  313.             ArchiveValidator validator = new ArchiveValidator(reader);
  314.             validator.validateArchive(archive, protocolloEffettivo, validazioneDocumenti, importInformationMissingCollection, userLogin,
  315.                     this.isShowCorrelazioneAsincronaInAccordi(),delete);
  316.            
  317.         }
  318.         catch(ImportInformationMissingException iime) {
  319.             throw iime;
  320.         }
  321.         catch(Exception e) {
  322.             throw new DriverControlStationException(getPrefixError("validateArchive",  e),e);
  323.         }
  324.         finally {
  325.             ControlStationCore.dbM.releaseConnection(con);
  326.         }
  327.     }
  328.    
  329.    
  330.     public Archive convert(byte[] file,ArchiveModeType type,ArchiveMode mode,String protocol,boolean validateDocuments,
  331.             MapPlaceholder importInformationMissingGlobalPlaceholder) throws DriverControlStationException, ImportInformationMissingException {
  332.         Connection con = null;
  333.         DriverControlStationDB driver = null;

  334.         try {
  335.             // prendo una connessione
  336.             con = ControlStationCore.dbM.getConnection();
  337.             // istanzio il driver
  338.             driver = new DriverControlStationDB(con, null, this.tipoDB);

  339.             RegistryReader registryReader = new RegistryReader(driver.getDriverRegistroServiziDB(),ControlStationCore.getLog());
  340.             ConfigIntegrationReader configReader = new ConfigIntegrationReader(driver.getDriverConfigurazioneDB(),ControlStationCore.getLog());
  341.            
  342.             IProtocolFactory<?> pf = ProtocolFactoryManager.getInstance().getProtocolFactoryByName(protocol);
  343.             IArchive archiveEngine = pf.createArchive();
  344.             return archiveEngine.importArchive(file, mode, type, registryReader, configReader,
  345.                     validateDocuments, importInformationMissingGlobalPlaceholder);
  346.            
  347.         }
  348.         catch(Exception e) {
  349.             throw new DriverControlStationException(getPrefixError("convert",  e),e);
  350.         }
  351.         finally {
  352.             ControlStationCore.dbM.releaseConnection(con);
  353.         }
  354.     }
  355.    
  356.     public void finalizeArchive(Archive archive,ArchiveModeType type,ArchiveMode mode,String protocol,boolean validateDocuments,
  357.             MapPlaceholder importInformationMissingGlobalPlaceholder) throws DriverControlStationException, ImportInformationMissingException {
  358.         Connection con = null;
  359.         DriverControlStationDB driver = null;

  360.         try {
  361.             // prendo una connessione
  362.             con = ControlStationCore.dbM.getConnection();
  363.             // istanzio il driver
  364.             driver = new DriverControlStationDB(con, null, this.tipoDB);

  365.             RegistryReader registryReader = new RegistryReader(driver.getDriverRegistroServiziDB(),ControlStationCore.getLog());
  366.             ConfigIntegrationReader configReader = new ConfigIntegrationReader(driver.getDriverConfigurazioneDB(),ControlStationCore.getLog());
  367.            
  368.             IProtocolFactory<?> pf = ProtocolFactoryManager.getInstance().getProtocolFactoryByName(protocol);
  369.             IArchive archiveEngine = pf.createArchive();
  370.             archiveEngine.finalizeImportArchive(archive, mode, type, registryReader, configReader,
  371.                     validateDocuments, importInformationMissingGlobalPlaceholder);
  372.            
  373.         }
  374.         catch(Exception e) {
  375.             throw new DriverControlStationException(getPrefixError("finalizeArchive",  e),e);
  376.         }
  377.         finally {
  378.             ControlStationCore.dbM.releaseConnection(con);
  379.         }
  380.     }
  381.    
  382.    
  383.     public Documento getDocumento(long idDocumento,boolean readBytes) throws DriverRegistroServiziException {
  384.         Connection con = null;
  385.         String nomeMetodo = "getDocumento";
  386.         DriverControlStationDB driver = null;

  387.         try {
  388.             // prendo una connessione
  389.             con = ControlStationCore.dbM.getConnection();
  390.             // istanzio il driver
  391.             driver = new DriverControlStationDB(con, null, this.tipoDB);

  392.             return driver.getDriverRegistroServiziDB().getDocumento(idDocumento,readBytes);
  393.            
  394.         } catch (DriverRegistroServiziException e) {
  395.             ControlStationCore.logError(getPrefixError(nomeMetodo,  e), e);
  396.             throw e;
  397.         } catch (Exception e) {
  398.             ControlStationCore.logError(getPrefixError(nomeMetodo,  e), e);
  399.             throw new DriverRegistroServiziException(getPrefixError(nomeMetodo,  e),e);
  400.         } finally {
  401.             ControlStationCore.dbM.releaseConnection(con);
  402.         }
  403.     }
  404.    
  405.     public Documento getDocumento(String nome, String tipo, String ruolo, long idProprietario,boolean readBytes,ProprietariDocumento tipoProprietario) throws DriverRegistroServiziException, DriverRegistroServiziNotFound {
  406.         Connection con = null;
  407.         String nomeMetodo = "getDocumento";
  408.         DriverControlStationDB driver = null;

  409.         try {
  410.             // prendo una connessione
  411.             con = ControlStationCore.dbM.getConnection();
  412.             // istanzio il driver
  413.             driver = new DriverControlStationDB(con, null, this.tipoDB);

  414.             return driver.getDriverRegistroServiziDB().getDocumento(nome,tipo,ruolo,idProprietario,readBytes,tipoProprietario);
  415.            
  416.         } catch (DriverRegistroServiziNotFound e) {
  417.             // Lasciare DEBUG, usato anche in servizio API RS
  418.             ControlStationCore.logDebug("[ControlStationCore::" + nomeMetodo + "] NotFound :" + e.getMessage(), e);
  419.             throw e;
  420.         } catch (DriverRegistroServiziException e) {
  421.             ControlStationCore.logError(getPrefixError(nomeMetodo,  e), e);
  422.             throw e;
  423.         } catch (Exception e) {
  424.             ControlStationCore.logError(getPrefixError(nomeMetodo,  e), e);
  425.             throw new DriverRegistroServiziException(getPrefixError(nomeMetodo,  e),e);
  426.         } finally {
  427.             ControlStationCore.dbM.releaseConnection(con);
  428.         }
  429.     }

  430.     public boolean existsDocumento(Documento documento,ProprietariDocumento proprietarioDocumento, boolean documentoUnivocoIndipendentementeTipo) throws DriverRegistroServiziException {
  431.         Connection con = null;
  432.         String nomeMetodo = "existsDocumento";
  433.         DriverControlStationDB driver = null;

  434.         try {
  435.             // prendo una connessione
  436.             con = ControlStationCore.dbM.getConnection();
  437.             // istanzio il driver
  438.             driver = new DriverControlStationDB(con, null, this.tipoDB);
  439.            
  440.             String tipo = documento.getTipo();
  441.             String ruoloDoc = documento.getRuolo();
  442.             if(documentoUnivocoIndipendentementeTipo) {
  443.                 tipo = null;
  444.                 ruoloDoc = null;
  445.             }
  446.            
  447.             return driver.getDriverRegistroServiziDB().existsDocumento(documento.getFile(),tipo,ruoloDoc,documento.getIdProprietarioDocumento(),proprietarioDocumento);
  448.         } catch (Exception e) {
  449.             ControlStationCore.logError(getPrefixError(nomeMetodo,  e), e);
  450.             throw new DriverRegistroServiziException(getPrefixError(nomeMetodo,  e),e);
  451.         } finally {
  452.             ControlStationCore.dbM.releaseConnection(con);
  453.         }
  454.     }
  455. }