ConfigurazionePluginsArchiviJarAdd.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.config;

  21. import java.util.ArrayList;
  22. import java.util.Date;
  23. import java.util.List;

  24. import javax.servlet.http.HttpServletRequest;
  25. import javax.servlet.http.HttpServletResponse;
  26. import javax.servlet.http.HttpSession;

  27. import org.apache.struts.action.Action;
  28. import org.apache.struts.action.ActionForm;
  29. import org.apache.struts.action.ActionForward;
  30. import org.apache.struts.action.ActionMapping;
  31. import org.openspcoop2.core.commons.Liste;
  32. import org.openspcoop2.core.config.RegistroPlugin;
  33. import org.openspcoop2.core.config.RegistroPluginArchivio;
  34. import org.openspcoop2.core.config.driver.db.DriverConfigurazioneDBLib;
  35. import org.openspcoop2.utils.id.UUIDUtilsGenerator;
  36. import org.openspcoop2.web.ctrlstat.core.ControlStationCore;
  37. import org.openspcoop2.web.ctrlstat.core.ConsoleSearch;
  38. import org.openspcoop2.web.ctrlstat.servlet.GeneralHelper;
  39. import org.openspcoop2.web.lib.mvc.BinaryParameter;
  40. import org.openspcoop2.web.lib.mvc.DataElement;
  41. import org.openspcoop2.web.lib.mvc.ForwardParams;
  42. import org.openspcoop2.web.lib.mvc.GeneralData;
  43. import org.openspcoop2.web.lib.mvc.PageData;
  44. import org.openspcoop2.web.lib.mvc.Parameter;
  45. import org.openspcoop2.web.lib.mvc.ServletUtils;
  46. import org.openspcoop2.web.lib.mvc.TipoOperazione;

  47. /**
  48.  * ConfigurazionePluginsArchiviJarAdd
  49.  *
  50.  * @author Giuliano Pintori (pintori@link.it)
  51.  * @author $Author$
  52.  * @version $Rev$, $Date$
  53.  *
  54.  */
  55. public final class ConfigurazionePluginsArchiviJarAdd extends Action {

  56.    

  57.     @Override
  58.     public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {

  59.         HttpSession session = request.getSession(true);

  60.         // Inizializzo PageData
  61.         PageData pd = new PageData();

  62.         GeneralHelper generalHelper = new GeneralHelper(session);

  63.         // Inizializzo GeneralData
  64.         GeneralData gd = generalHelper.initGeneralData(request);

  65.         String userLogin = ServletUtils.getUserLoginFromSession(session);  

  66.         try {
  67.             ConfigurazioneHelper confHelper = new ConfigurazioneHelper(request, pd, session);
  68.            
  69.             String nome = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_PLUGINS_ARCHIVI_NOME);
  70.            
  71.             String sorgente = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_PLUGINS_ARCHIVI_SORGENTE);
  72.             BinaryParameter jarArchivio = confHelper.getBinaryParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_PLUGINS_ARCHIVI_JAR_ARCHIVIO);
  73.             String urlArchivio = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_PLUGINS_ARCHIVI_URL_ARCHIVIO);
  74.             String dirArchivio = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_PLUGINS_ARCHIVI_DIR_ARCHIVIO);
  75.            
  76.             ConfigurazioneCore confCore = new ConfigurazioneCore();

  77.             // Preparo il menu
  78.             confHelper.makeMenu();
  79.             List<Parameter> lstParam = new ArrayList<>();
  80.            
  81.             // setto la barra del titolo
  82.             lstParam.add(new Parameter(ConfigurazioneCostanti.LABEL_CONFIGURAZIONE_GENERALE, ConfigurazioneCostanti.SERVLET_NAME_CONFIGURAZIONE_GENERALE));
  83.             lstParam.add(new Parameter(ConfigurazioneCostanti.LABEL_CONFIGURAZIONE_PLUGINS_REGISTRO_ARCHIVI, ConfigurazioneCostanti.SERVLET_NAME_CONFIGURAZIONE_PLUGINS_ARCHIVI_LIST));
  84.             lstParam.add(new Parameter(nome, ConfigurazioneCostanti.SERVLET_NAME_CONFIGURAZIONE_PLUGINS_ARCHIVI_CHANGE,
  85.                     new Parameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_PLUGINS_ARCHIVI_OLD_NOME, nome)));
  86.             lstParam.add(ServletUtils.getParameterAggiungi());
  87.             ServletUtils.setPageDataTitle(pd, lstParam);

  88.             // Se nomehid = null, devo visualizzare la pagina per l'inserimento
  89.             // dati
  90.             if (confHelper.isEditModeInProgress()) {
  91.                
  92.                 if(sorgente == null) {
  93.                     sorgente = ConfigurazioneCostanti.DEFAULT_VALUE_PARAMETRO_CONFIGURAZIONE_PLUGINS_ARCHIVI_SORGENTE_JAR;
  94.                     dirArchivio = "";
  95.                     urlArchivio = "";
  96.                 }
  97.                
  98.                 // preparo i campi
  99.                 List<DataElement> dati = new ArrayList<>();
  100.                 dati.add(ServletUtils.getDataElementForEditModeFinished());

  101.                 dati = confHelper.addRegistroPluginJarToDati(TipoOperazione.ADD, dati, true, nome, sorgente, jarArchivio, dirArchivio, urlArchivio);

  102.                 pd.setDati(dati);

  103.                 ServletUtils.setGeneralAndPageDataIntoSession(request, session, gd, pd);

  104.                 return ServletUtils.getStrutsForwardEditModeInProgress(mapping,
  105.                         ConfigurazioneCostanti.OBJECT_NAME_CONFIGURAZIONE_PLUGINS_ARCHIVI_JAR,
  106.                         ForwardParams.ADD());
  107.             }

  108.             // Controlli sui campi immessi
  109.             boolean isOk = confHelper.registroPluginArchivioCheckData(TipoOperazione.ADD, sorgente, nome, jarArchivio, dirArchivio, urlArchivio, true);
  110.             if (!isOk) {

  111.                 // preparo i campi
  112.                 List<DataElement> dati = new ArrayList<>();

  113.                 dati.add(ServletUtils.getDataElementForEditModeFinished());
  114.                
  115.                 dati = confHelper.addRegistroPluginJarToDati(TipoOperazione.ADD, dati, true, nome, sorgente, jarArchivio, dirArchivio, urlArchivio);
  116.                  
  117.                 pd.setDati(dati);

  118.                 ServletUtils.setGeneralAndPageDataIntoSession(request, session, gd, pd);

  119.                 return ServletUtils.getStrutsForwardEditModeCheckError(mapping,
  120.                         ConfigurazioneCostanti.OBJECT_NAME_CONFIGURAZIONE_PLUGINS_ARCHIVI_JAR,
  121.                         ForwardParams.ADD());
  122.             }

  123.             // rileggo registro
  124.             RegistroPlugin registro = confCore.getDatiRegistroPlugin(nome);
  125.             registro.setData(new Date());
  126.            
  127.             RegistroPluginArchivio registroPluginArchivio = new RegistroPluginArchivio();
  128.             registroPluginArchivio.setNomePlugin(nome);
  129.             registroPluginArchivio.setData(registro.getData());
  130.            
  131.             registroPluginArchivio.setSorgente( DriverConfigurazioneDBLib.getEnumPluginSorgenteArchivio(sorgente));
  132.            
  133.             switch (registroPluginArchivio.getSorgente()) {
  134.             case JAR:
  135.                 registroPluginArchivio.setNome(jarArchivio.getFilename());
  136.                 registroPluginArchivio.setContenuto(jarArchivio.getValue());
  137.                 break;
  138.             case URL:
  139.                 registroPluginArchivio.setUrl(urlArchivio);
  140.                 /**URL url = new URL(urlArchivio);
  141.                 //registroPluginArchivio.setNome(FilenameUtils.getName(url.getPath()));*/
  142.                 // Fix: usando l'utility, url differenti che terminano con la stessa foglia vanno in errore di duplicate key
  143.                 registroPluginArchivio.setNome(UUIDUtilsGenerator.newUUID());
  144.                 break;
  145.             case DIR:
  146.                 registroPluginArchivio.setDir(dirArchivio);
  147.                 /**registroPluginArchivio.setNome(FilenameUtils.getName(dirArchivio));*/
  148.                 // Fix: usando l'utility, path differenti che terminano con lo stesso file name vanno in errore di duplicate key
  149.                 registroPluginArchivio.setNome(UUIDUtilsGenerator.newUUID());
  150.                 break;
  151.             }
  152.            
  153.             confCore.performCreateOperation(userLogin, confHelper.smista(), registroPluginArchivio);
  154.             confCore.performUpdateOperation(userLogin, confHelper.smista(), registro);
  155.            
  156.             // Aggiorno classLoader interno
  157.             confCore.updatePluginClassLoader();
  158.            
  159.             // Preparo la lista
  160.             ConsoleSearch ricerca = (ConsoleSearch) ServletUtils.getSearchObjectFromSession(request, session, ConsoleSearch.class);

  161.             int idLista = Liste.CONFIGURAZIONE_PLUGINS_ARCHIVI_JAR;

  162.             ricerca = confHelper.checkSearchParameters(idLista, ricerca);

  163.             List<RegistroPluginArchivio> lista = confCore.pluginsArchiviJarList(nome, ricerca);  
  164.            
  165.             confHelper.preparePluginsArchiviJarList(nome, ricerca, lista);
  166.                        
  167.             ServletUtils.setGeneralAndPageDataIntoSession(request, session, gd, pd);

  168.             return ServletUtils.getStrutsForwardEditModeFinished(mapping,
  169.                     ConfigurazioneCostanti.OBJECT_NAME_CONFIGURAZIONE_PLUGINS_ARCHIVI_JAR,
  170.                     ForwardParams.ADD());
  171.         } catch (Exception e) {
  172.             return ServletUtils.getStrutsForwardError(ControlStationCore.getLog(), e, pd, request, session, gd, mapping,
  173.                     ConfigurazioneCostanti.OBJECT_NAME_CONFIGURAZIONE_PLUGINS_ARCHIVI_JAR, ForwardParams.ADD());
  174.         }
  175.     }


  176. }