PorteApplicativeTrasformazioniServizioApplicativoAutorizzatoAdd.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.pa;

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

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

  27. import org.apache.commons.lang.StringUtils;
  28. import org.apache.struts.action.Action;
  29. import org.apache.struts.action.ActionForm;
  30. import org.apache.struts.action.ActionForward;
  31. import org.apache.struts.action.ActionMapping;
  32. import org.openspcoop2.core.commons.Liste;
  33. import org.openspcoop2.core.config.PortaApplicativa;
  34. import org.openspcoop2.core.config.ServizioApplicativo;
  35. import org.openspcoop2.core.config.TrasformazioneRegola;
  36. import org.openspcoop2.core.config.TrasformazioneRegolaApplicabilitaRichiesta;
  37. import org.openspcoop2.core.config.TrasformazioneRegolaApplicabilitaServizioApplicativo;
  38. import org.openspcoop2.core.config.Trasformazioni;
  39. import org.openspcoop2.core.config.constants.CredenzialeTipo;
  40. import org.openspcoop2.core.config.driver.db.IDServizioApplicativoDB;
  41. import org.openspcoop2.web.ctrlstat.core.ControlStationCore;
  42. import org.openspcoop2.web.ctrlstat.core.ConsoleSearch;
  43. import org.openspcoop2.web.ctrlstat.costanti.CostantiControlStation;
  44. import org.openspcoop2.web.ctrlstat.servlet.ApiKeyState;
  45. import org.openspcoop2.web.ctrlstat.servlet.GeneralHelper;
  46. import org.openspcoop2.web.ctrlstat.servlet.sa.ServiziApplicativiCore;
  47. import org.openspcoop2.web.ctrlstat.servlet.soggetti.SoggettiCore;
  48. import org.openspcoop2.web.lib.mvc.DataElement;
  49. import org.openspcoop2.web.lib.mvc.ForwardParams;
  50. import org.openspcoop2.web.lib.mvc.GeneralData;
  51. import org.openspcoop2.web.lib.mvc.PageData;
  52. import org.openspcoop2.web.lib.mvc.Parameter;
  53. import org.openspcoop2.web.lib.mvc.ServletUtils;
  54. import org.openspcoop2.web.lib.mvc.TipoOperazione;

  55. /**
  56.  * PorteApplicativeServizioApplicativoAutorizzatoAdd
  57.  *
  58.  * @author Andrea Poli (apoli@link.it)
  59.  * @author Stefano Corallo (corallo@link.it)
  60.  * @author Sandra Giangrandi (sandra@link.it)
  61.  * @author $Author$
  62.  * @version $Rev$, $Date$
  63.  *
  64.  */
  65. public final class PorteApplicativeTrasformazioniServizioApplicativoAutorizzatoAdd extends Action {

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

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

  69.         // Inizializzo PageData
  70.         PageData pd = new PageData();

  71.         GeneralHelper generalHelper = new GeneralHelper(session);

  72.         String userLogin = ServletUtils.getUserLoginFromSession(session);  
  73.         // Inizializzo GeneralData
  74.         GeneralData gd = generalHelper.initGeneralData(request);

  75.         try {
  76.             PorteApplicativeHelper porteApplicativeHelper = new PorteApplicativeHelper(request, pd, session);
  77.             // prelevo il flag che mi dice da quale pagina ho acceduto la sezione delle porte applicative
  78.             Integer parentPA = ServletUtils.getIntegerAttributeFromSession(PorteApplicativeCostanti.ATTRIBUTO_PORTE_APPLICATIVE_PARENT, session, request);
  79.             if(parentPA == null) parentPA = PorteApplicativeCostanti.ATTRIBUTO_PORTE_APPLICATIVE_PARENT_NONE;
  80.             String idPorta = porteApplicativeHelper.getParameter(PorteApplicativeCostanti.PARAMETRO_PORTE_APPLICATIVE_ID);
  81.             int idInt = Integer.parseInt(idPorta);
  82.             String idsogg = porteApplicativeHelper.getParameter(PorteApplicativeCostanti.PARAMETRO_PORTE_APPLICATIVE_ID_SOGGETTO);
  83.             int soggInt = Integer.parseInt(idsogg);
  84.            
  85.             String idSoggettoToAdd = porteApplicativeHelper.getParameter(PorteApplicativeCostanti.PARAMETRO_PORTE_APPLICATIVE_SOGGETTO);
  86.             String idAsps = porteApplicativeHelper.getParameter(PorteApplicativeCostanti.PARAMETRO_PORTE_APPLICATIVE_ID_ASPS);
  87.             if(idAsps == null)
  88.                 idAsps = "";
  89.            
  90.             String idSAToAdd = porteApplicativeHelper.getParameter(PorteApplicativeCostanti.PARAMETRO_PORTE_APPLICATIVE_SERVIZIO_APPLICATIVO_AUTORIZZATO);
  91.            
  92.             String idTrasformazioneS = porteApplicativeHelper.getParameter(PorteApplicativeCostanti.PARAMETRO_PORTE_APPLICATIVE_ID_TRASFORMAZIONE);
  93.             long idTrasformazione = Long.parseLong(idTrasformazioneS);

  94.             String listaTmp = porteApplicativeHelper.getParameter(PorteApplicativeCostanti.PARAMETRO_PORTE_APPLICATIVE_TRASFORMAZIONI_APPLICABILITA_LIST);
  95.             boolean fromList = false;
  96.             if(listaTmp != null && !"".equals(listaTmp))
  97.                 fromList = true;
  98.            
  99.             PorteApplicativeCore porteApplicativeCore = new PorteApplicativeCore();
  100.             SoggettiCore soggettiCore = new SoggettiCore(porteApplicativeCore);
  101.             ServiziApplicativiCore saCore = new ServiziApplicativiCore(porteApplicativeCore);

  102.             boolean escludiSoggettoErogatore = false;

  103.            
  104.             // Preparo il menu
  105.             porteApplicativeHelper.makeMenu();

  106.             boolean escludiSAServer = saCore.isApplicativiServerEnabled(porteApplicativeHelper);
  107.            
  108.            
  109.             // Prendo nome, tipo e pdd del soggetto
  110.             String protocollo = null;
  111.             if(porteApplicativeCore.isRegistroServiziLocale()){
  112.                 org.openspcoop2.core.registry.Soggetto soggetto = soggettiCore.getSoggettoRegistro(soggInt);
  113.                 protocollo = soggettiCore.getProtocolloAssociatoTipoSoggetto(soggetto.getTipo());
  114.             }
  115.             else{
  116.                 org.openspcoop2.core.config.Soggetto soggetto = soggettiCore.getSoggetto(soggInt);
  117.                 protocollo = soggettiCore.getProtocolloAssociatoTipoSoggetto(soggetto.getTipo());
  118.             }
  119.            
  120.             boolean isSupportatoAutenticazioneApplicativiEsterni = saCore.isSupportatoAutenticazioneApplicativiEsterniErogazione(protocollo);

  121.             //decodifica soggetto scelto
  122.             String tipoSoggettoScelto = null;
  123.             String nomeSoggettoScelto = null;
  124.             String nomeSAScelto = null;
  125.             if(idSoggettoToAdd != null) {
  126.                 long soggettoToAddInt = Long.parseLong(idSoggettoToAdd);
  127.                
  128.                 if(porteApplicativeCore.isRegistroServiziLocale()){
  129.                     org.openspcoop2.core.registry.Soggetto soggetto = soggettiCore.getSoggettoRegistro(soggettoToAddInt);
  130.                     tipoSoggettoScelto = soggetto.getTipo();
  131.                     nomeSoggettoScelto = soggetto.getNome();
  132.                 }
  133.                 else{
  134.                     org.openspcoop2.core.config.Soggetto soggetto = soggettiCore.getSoggetto(soggettoToAddInt);
  135.                     tipoSoggettoScelto = soggetto.getTipo();
  136.                     nomeSoggettoScelto = soggetto.getNome();
  137.                 }
  138.                
  139.                 if(idSAToAdd != null) {
  140.                     long idSAToAddInt = Long.parseLong(idSAToAdd);
  141.                     ServizioApplicativo servizioApplicativo = saCore.getServizioApplicativo(idSAToAddInt);
  142.                     nomeSAScelto = servizioApplicativo.getNome();
  143.                 }
  144.             }
  145.            
  146.             // Prendo nome della porta applicativa
  147.             PortaApplicativa portaApplicativa = porteApplicativeCore.getPortaApplicativa(idInt);
  148.             String nomePorta = portaApplicativa.getNome();
  149.             CredenzialeTipo tipoAutenticazione = CredenzialeTipo.toEnumConstant(portaApplicativa.getAutenticazione());
  150.             @SuppressWarnings("unused")
  151.             Boolean appId = null;
  152.             if(CredenzialeTipo.APIKEY.equals(tipoAutenticazione)) {
  153.                 ApiKeyState apiKeyState =  new ApiKeyState(porteApplicativeCore.getParametroAutenticazione(portaApplicativa.getAutenticazione(), portaApplicativa.getProprietaAutenticazioneList()));
  154.                 appId = apiKeyState.appIdSelected;
  155.             }
  156.                    
  157.             Trasformazioni trasformazioni = portaApplicativa.getTrasformazioni();
  158.             TrasformazioneRegola regola = null;
  159.             for (int j = 0; j < trasformazioni.sizeRegolaList(); j++) {
  160.                 TrasformazioneRegola regolaTmp = trasformazioni.getRegola(j);
  161.                 if (regolaTmp.getId().longValue() == idTrasformazione) {
  162.                     regola = regolaTmp;
  163.                     break;
  164.                 }
  165.             }
  166.             if(regola==null) {
  167.                 throw new Exception("TrasformazioneRegola con id '"+idTrasformazione+"' non trovata");
  168.             }
  169.            
  170.             String nomeTrasformazione = regola.getNome();
  171.             Parameter pIdTrasformazione = new Parameter(PorteApplicativeCostanti.PARAMETRO_PORTE_APPLICATIVE_ID_TRASFORMAZIONE, idTrasformazione+"");
  172.             TrasformazioneRegolaApplicabilitaRichiesta applicabilita = regola.getApplicabilita();
  173.            
  174.             List<TrasformazioneRegolaApplicabilitaServizioApplicativo> saList = applicabilita != null ? applicabilita.getServizioApplicativoList() : null;
  175.             int saSize = saList!=null ? saList.size() : 0;

  176.             // Calcolo liste
  177.             PorteApplicativeServizioApplicativoAutorizzatoUtilities utilities = new PorteApplicativeServizioApplicativoAutorizzatoUtilities();
  178.             utilities.buildListTrasformazioni(portaApplicativa, false, protocollo, escludiSoggettoErogatore,
  179.                     idSoggettoToAdd,
  180.                     porteApplicativeCore, porteApplicativeHelper, escludiSAServer,
  181.                     isSupportatoAutenticazioneApplicativiEsterni,
  182.                     regola);
  183.            
  184.             String[] soggettiList = utilities.soggettiList;
  185.             String[] soggettiListLabel = utilities.soggettiListLabel;
  186.             idSoggettoToAdd = utilities.idSoggettoToAdd;
  187.            
  188.             Map<String,List<IDServizioApplicativoDB>> listServiziApplicativi = utilities.listServiziApplicativi;
  189.            
  190.        
  191.             List<Parameter> lstParam = porteApplicativeHelper.getTitoloPA(parentPA, idsogg, idAsps);

  192.             String labelPerPorta = null;
  193.             if(parentPA!=null && (parentPA.intValue() == PorteApplicativeCostanti.ATTRIBUTO_PORTE_APPLICATIVE_PARENT_CONFIGURAZIONE)) {
  194.                 labelPerPorta = porteApplicativeCore.getLabelRegolaMappingErogazionePortaApplicativa(
  195.                         PorteApplicativeCostanti.LABEL_PARAMETRO_PORTE_APPLICATIVE_TRASFORMAZIONI_DI,
  196.                         PorteApplicativeCostanti.LABEL_PARAMETRO_PORTE_APPLICATIVE_TRASFORMAZIONI,
  197.                         portaApplicativa);
  198.             }
  199.             else {
  200.                 labelPerPorta = PorteApplicativeCostanti.LABEL_PARAMETRO_PORTE_APPLICATIVE_TRASFORMAZIONI_DI+nomePorta;
  201.             }

  202.             lstParam.add(new Parameter(labelPerPorta,
  203.                     PorteApplicativeCostanti.SERVLET_NAME_PORTE_APPLICATIVE_TRASFORMAZIONI_LIST,
  204.                     new Parameter( PorteApplicativeCostanti.PARAMETRO_PORTE_APPLICATIVE_ID, idPorta),
  205.                     new Parameter( PorteApplicativeCostanti.PARAMETRO_PORTE_APPLICATIVE_ID_SOGGETTO, idsogg),
  206.                     new Parameter( PorteApplicativeCostanti.PARAMETRO_PORTE_APPLICATIVE_ID_ASPS, idAsps)
  207.                     ));
  208.            
  209.             if(!fromList) {
  210.                 lstParam.add(new Parameter(nomeTrasformazione, PorteApplicativeCostanti.SERVLET_NAME_PORTE_APPLICATIVE_TRASFORMAZIONI_CHANGE,
  211.                         new Parameter( PorteApplicativeCostanti.PARAMETRO_PORTE_APPLICATIVE_ID, idPorta),
  212.                         new Parameter( PorteApplicativeCostanti.PARAMETRO_PORTE_APPLICATIVE_ID_SOGGETTO, idsogg),
  213.                         new Parameter(PorteApplicativeCostanti.PARAMETRO_PORTE_APPLICATIVE_ID_ASPS, idAsps), pIdTrasformazione));
  214.             }
  215.            
  216.             String labelPagLista = PorteApplicativeCostanti.LABEL_PARAMETRO_PORTE_APPLICATIVE_TRASFORMAZIONI_APPLICATIVI_CONFIG;
  217.            
  218.             List<Parameter> parametersList = new ArrayList<>();
  219.             parametersList.add(new Parameter( PorteApplicativeCostanti.PARAMETRO_PORTE_APPLICATIVE_ID, idPorta));
  220.             parametersList.add(new Parameter( PorteApplicativeCostanti.PARAMETRO_PORTE_APPLICATIVE_ID_SOGGETTO, idsogg));
  221.             parametersList.add(new Parameter( PorteApplicativeCostanti.PARAMETRO_PORTE_APPLICATIVE_ID_ASPS, idAsps));
  222.             parametersList.add(pIdTrasformazione);
  223.             if(fromList) {
  224.                 parametersList.add(new Parameter (PorteApplicativeCostanti.PARAMETRO_PORTE_APPLICATIVE_TRASFORMAZIONI_APPLICABILITA_LIST, listaTmp));
  225.             }
  226.             lstParam.add(new Parameter(labelPagLista,
  227.                     PorteApplicativeCostanti.SERVLET_NAME_PORTE_APPLICATIVE_TRASFORMAZIONI_SERVIZIO_APPLICATIVO_AUTORIZZATO_LIST,
  228.                     parametersList));
  229.             lstParam.add(ServletUtils.getParameterAggiungi());
  230.            
  231.             // Se servizioApplicativohid = null, devo visualizzare la pagina per
  232.             // l'inserimento dati
  233.             if (porteApplicativeHelper.isEditModeInProgress()) {
  234.                
  235.                 // setto la barra del titolo
  236.                 ServletUtils.setPageDataTitle(pd, lstParam);

  237.                 // preparo i campi
  238.                 List<DataElement> dati = new ArrayList<>();
  239.                 dati.add(ServletUtils.getDataElementForEditModeFinished());
  240.                
  241.                 dati = porteApplicativeHelper.addPorteTrasformazioniServizioApplicativoAutorizzatiToDati(TipoOperazione.ADD, dati, idTrasformazioneS, fromList, soggettiListLabel, soggettiList, idSoggettoToAdd, saSize,
  242.                         listServiziApplicativi, idSAToAdd, true);

  243.                 dati = porteApplicativeHelper.addHiddenFieldsToDati(TipoOperazione.ADD, idPorta, idsogg, idPorta, idAsps,dati);

  244.                 pd.setDati(dati);

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

  246.                 return ServletUtils.getStrutsForwardEditModeInProgress(mapping, PorteApplicativeCostanti.OBJECT_NAME_PORTE_APPLICATIVE_TRASFORMAZIONI_SERVIZIO_APPLICATIVO_AUTORIZZATO,
  247.                         ForwardParams.ADD());
  248.             }

  249.             // Controlli sui campi immessi
  250.             boolean isOk = porteApplicativeHelper.porteAppTrasformazioniServizioApplicativoAutorizzatiCheckData(TipoOperazione.ADD);
  251.            
  252.             TrasformazioneRegolaApplicabilitaServizioApplicativo sa = new TrasformazioneRegolaApplicabilitaServizioApplicativo();
  253.             sa.setNome(nomeSAScelto);
  254.             sa.setTipoSoggettoProprietario(tipoSoggettoScelto);
  255.             sa.setNomeSoggettoProprietario(nomeSoggettoScelto);
  256.            
  257.             if(isOk) {
  258.                
  259.                 List<TrasformazioneRegolaApplicabilitaServizioApplicativo> sacheckList = new ArrayList<>();
  260.                 sacheckList.add(sa);
  261.                
  262.                 String patternDBCheck = (regola.getApplicabilita() != null && StringUtils.isNotEmpty(regola.getApplicabilita().getPattern())) ? regola.getApplicabilita().getPattern() : null;
  263.                 String contentTypeAsString = (regola.getApplicabilita() != null &&regola.getApplicabilita().getContentTypeList() != null) ? StringUtils.join(regola.getApplicabilita().getContentTypeList(), ",") : "";
  264.                 String contentTypeDBCheck = StringUtils.isNotEmpty(contentTypeAsString) ? contentTypeAsString : null;
  265.                 String azioniAsString = (regola.getApplicabilita() != null && regola.getApplicabilita().getAzioneList() != null) ? StringUtils.join(regola.getApplicabilita().getAzioneList(), ",") : "";
  266.                 String azioniDBCheck = StringUtils.isNotEmpty(azioniAsString) ? azioniAsString : null;
  267.                 String connettoriAsString = (regola.getApplicabilita() != null && regola.getApplicabilita().getConnettoreList() != null) ? StringUtils.join(regola.getApplicabilita().getConnettoreList(), ",") : "";
  268.                 String connettoriDBCheck = StringUtils.isNotEmpty(connettoriAsString) ? connettoriAsString : null;
  269.                 TrasformazioneRegola trasformazioneDBCheckCriteri = porteApplicativeCore.getTrasformazione(Long.parseLong(idPorta), azioniDBCheck, patternDBCheck, contentTypeDBCheck, connettoriDBCheck,
  270.                         null, sacheckList, false);
  271.                 if(trasformazioneDBCheckCriteri!=null) {
  272.                     isOk = false;
  273.                     pd.setMessage(CostantiControlStation.MESSAGGIO_ERRORE_REGOLA_TRASFORMAZIONE_APPLICABILITA_DUPLICATA_APPLICATIVO);
  274.                 }
  275.                
  276.             }
  277.             if (!isOk) {
  278.                 // setto la barra del titolo
  279.                 ServletUtils.setPageDataTitle(pd, lstParam);

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

  282.                 dati.add(ServletUtils.getDataElementForEditModeFinished());

  283.                 dati = porteApplicativeHelper.addPorteTrasformazioniServizioApplicativoAutorizzatiToDati(TipoOperazione.ADD, dati, idTrasformazioneS, fromList, soggettiListLabel, soggettiList, idSoggettoToAdd, saSize,
  284.                         listServiziApplicativi, idSAToAdd, true);

  285.                 dati = porteApplicativeHelper.addHiddenFieldsToDati(TipoOperazione.ADD, idPorta, idsogg, idPorta, idAsps, dati);

  286.                 pd.setDati(dati);

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

  288.                 return ServletUtils.getStrutsForwardEditModeCheckError(mapping, PorteApplicativeCostanti.OBJECT_NAME_PORTE_APPLICATIVE_TRASFORMAZIONI_SERVIZIO_APPLICATIVO_AUTORIZZATO,
  289.                         ForwardParams.ADD());
  290.             }

  291.             // salvataggio nuova regola
  292.             for (int j = 0; j < trasformazioni.sizeRegolaList(); j++) {
  293.                 TrasformazioneRegola regolaTmp = trasformazioni.getRegola(j);
  294.                 if (regolaTmp.getId().longValue() == idTrasformazione) {
  295.                     regola = regolaTmp;
  296.                     break;
  297.                 }
  298.             }
  299.                        
  300.             // Inserisco il servizioApplicativo nel db
  301.            
  302.             if(regola.getApplicabilita() == null)
  303.                 regola.setApplicabilita(new TrasformazioneRegolaApplicabilitaRichiesta());
  304.            
  305.             regola.getApplicabilita().addServizioApplicativo(sa);
  306.            
  307.             porteApplicativeCore.performUpdateOperation(userLogin, porteApplicativeHelper.smista(), portaApplicativa);
  308.            
  309.             // ricaricare id trasformazione
  310.             portaApplicativa = porteApplicativeCore.getPortaApplicativa(Long.parseLong(idPorta));
  311.    
  312.             TrasformazioneRegola trasformazioneAggiornata = porteApplicativeCore.getTrasformazione(Long.parseLong(idPorta), regola.getNome());


  313.             // Preparo la lista
  314.             ConsoleSearch ricerca = (ConsoleSearch) ServletUtils.getSearchObjectFromSession(request, session, ConsoleSearch.class);

  315.             int idLista = Liste.PORTE_APPLICATIVE_TRASFORMAZIONI_SERVIZIO_APPLICATIVO_AUTORIZZATO;

  316.             ricerca = porteApplicativeHelper.checkSearchParameters(idLista, ricerca);
  317.            
  318.             List<TrasformazioneRegolaApplicabilitaServizioApplicativo> lista = porteApplicativeCore.porteAppTrasformazioniServiziApplicativiAutorizzatiList(Integer.parseInt(idPorta), trasformazioneAggiornata.getId(), ricerca);
  319.            
  320.             porteApplicativeHelper.preparePorteAppTrasformazioniServizioApplicativoAutorizzatoList(portaApplicativa.getNome(), trasformazioneAggiornata.getId(), ricerca, lista);

  321.             ServletUtils.setGeneralAndPageDataIntoSession(request, session, gd, pd);
  322.             // Forward control to the specified success URI
  323.             return ServletUtils.getStrutsForwardEditModeFinished(mapping, PorteApplicativeCostanti.OBJECT_NAME_PORTE_APPLICATIVE_TRASFORMAZIONI_SERVIZIO_APPLICATIVO_AUTORIZZATO,
  324.                     ForwardParams.ADD());
  325.         } catch (Exception e) {
  326.             return ServletUtils.getStrutsForwardError(ControlStationCore.getLog(), e, pd, request, session, gd, mapping,
  327.                     PorteApplicativeCostanti.OBJECT_NAME_PORTE_APPLICATIVE_TRASFORMAZIONI_SERVIZIO_APPLICATIVO_AUTORIZZATO,
  328.                     ForwardParams.ADD());
  329.         }
  330.     }
  331. }