PorteApplicativeSoggettoAdd.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 javax.servlet.http.HttpServletRequest;
  24. import javax.servlet.http.HttpServletResponse;
  25. import javax.servlet.http.HttpSession;

  26. import org.apache.struts.action.Action;
  27. import org.apache.struts.action.ActionForm;
  28. import org.apache.struts.action.ActionForward;
  29. import org.apache.struts.action.ActionMapping;
  30. import org.openspcoop2.core.commons.Liste;
  31. import org.openspcoop2.core.config.PortaApplicativa;
  32. import org.openspcoop2.core.config.PortaApplicativaAutorizzazioneSoggetti;
  33. import org.openspcoop2.core.config.PortaApplicativaAutorizzazioneSoggetto;
  34. import org.openspcoop2.core.registry.constants.CredenzialeTipo;
  35. import org.openspcoop2.core.registry.constants.PddTipologia;
  36. import org.openspcoop2.core.registry.driver.db.IDSoggettoDB;
  37. import org.openspcoop2.web.ctrlstat.core.ControlStationCore;
  38. import org.openspcoop2.web.ctrlstat.core.ConsoleSearch;
  39. import org.openspcoop2.web.ctrlstat.costanti.CostantiControlStation;
  40. import org.openspcoop2.web.ctrlstat.servlet.ApiKeyState;
  41. import org.openspcoop2.web.ctrlstat.servlet.GeneralHelper;
  42. import org.openspcoop2.web.ctrlstat.servlet.aps.AccordiServizioParteSpecificaCore;
  43. import org.openspcoop2.web.ctrlstat.servlet.aps.AccordiServizioParteSpecificaCostanti;
  44. import org.openspcoop2.web.ctrlstat.servlet.soggetti.SoggettiCore;
  45. import org.openspcoop2.web.lib.mvc.DataElement;
  46. import org.openspcoop2.web.lib.mvc.ForwardParams;
  47. import org.openspcoop2.web.lib.mvc.GeneralData;
  48. import org.openspcoop2.web.lib.mvc.PageData;
  49. import org.openspcoop2.web.lib.mvc.Parameter;
  50. import org.openspcoop2.web.lib.mvc.ServletUtils;
  51. import org.openspcoop2.web.lib.mvc.TipoOperazione;

  52. /**
  53.  * porteAppServizioApplicativoAdd
  54.  *
  55.  * @author Andrea Poli (apoli@link.it)
  56.  * @author Stefano Corallo (corallo@link.it)
  57.  * @author Sandra Giangrandi (sandra@link.it)
  58.  * @author $Author$
  59.  * @version $Rev$, $Date$
  60.  *
  61.  */
  62. public final class PorteApplicativeSoggettoAdd extends Action {

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

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

  66.         // Inizializzo PageData
  67.         PageData pd = new PageData();

  68.         GeneralHelper generalHelper = new GeneralHelper(session);

  69.         String userLogin = ServletUtils.getUserLoginFromSession(session);  
  70.         // Inizializzo GeneralData
  71.         GeneralData gd = generalHelper.initGeneralData(request);

  72.         try {
  73.             PorteApplicativeHelper porteApplicativeHelper = new PorteApplicativeHelper(request, pd, session);
  74.             // prelevo il flag che mi dice da quale pagina ho acceduto la sezione delle porte applicative
  75.             Integer parentPA = ServletUtils.getIntegerAttributeFromSession(PorteApplicativeCostanti.ATTRIBUTO_PORTE_APPLICATIVE_PARENT, session, request);
  76.             if(parentPA == null) parentPA = PorteApplicativeCostanti.ATTRIBUTO_PORTE_APPLICATIVE_PARENT_NONE;
  77.             String idPorta = porteApplicativeHelper.getParameter(PorteApplicativeCostanti.PARAMETRO_PORTE_APPLICATIVE_ID);
  78.             int idInt = Integer.parseInt(idPorta);
  79.             String idsogg = porteApplicativeHelper.getParameter(PorteApplicativeCostanti.PARAMETRO_PORTE_APPLICATIVE_ID_SOGGETTO);
  80.             int soggInt = Integer.parseInt(idsogg);
  81.            
  82.             String idSoggettoToAdd = porteApplicativeHelper.getParameter(PorteApplicativeCostanti.PARAMETRO_PORTE_APPLICATIVE_SOGGETTO);
  83.             String idAsps = porteApplicativeHelper.getParameter(PorteApplicativeCostanti.PARAMETRO_PORTE_APPLICATIVE_ID_ASPS);
  84.             if(idAsps == null)
  85.                 idAsps = "";

  86.             PorteApplicativeCore porteApplicativeCore = new PorteApplicativeCore();
  87.             SoggettiCore soggettiCore = new SoggettiCore(porteApplicativeCore);
  88.             AccordiServizioParteSpecificaCore apsCore = new AccordiServizioParteSpecificaCore(porteApplicativeCore);

  89.             String tipologia = ServletUtils.getObjectFromSession(request, session, String.class, AccordiServizioParteSpecificaCostanti.PARAMETRO_APS_TIPO_EROGAZIONE);
  90.             boolean gestioneErogatori = false;
  91.             if(tipologia!=null &&
  92.                 AccordiServizioParteSpecificaCostanti.PARAMETRO_APS_TIPO_EROGAZIONE_VALUE_EROGAZIONE.equals(tipologia)) {
  93.                 gestioneErogatori = true;
  94.             }
  95.            
  96.             PddTipologia pddTipologiaSoggettoAutenticati = null;
  97.             boolean gestioneErogatoriSoggettiAutenticatiEscludiSoggettoErogatore = false;
  98.             if(gestioneErogatori &&
  99.                 apsCore.isMultitenant() && apsCore.getMultitenantSoggettiErogazioni()!=null) {
  100.                 switch (apsCore.getMultitenantSoggettiErogazioni()) {
  101.                 case SOLO_SOGGETTI_ESTERNI:
  102.                     pddTipologiaSoggettoAutenticati = PddTipologia.ESTERNO;
  103.                     break;
  104.                 case ESCLUDI_SOGGETTO_EROGATORE:
  105.                     gestioneErogatoriSoggettiAutenticatiEscludiSoggettoErogatore = true;
  106.                     break;
  107.                 case TUTTI:
  108.                     break;
  109.                 }
  110.             }
  111.            
  112.             // Preparo il menu
  113.             porteApplicativeHelper.makeMenu();

  114.             // Prendo nome, tipo e pdd del soggetto
  115.             String protocollo = null;
  116.             if(porteApplicativeCore.isRegistroServiziLocale()){
  117.                 org.openspcoop2.core.registry.Soggetto soggetto = soggettiCore.getSoggettoRegistro(soggInt);
  118.                 protocollo = soggettiCore.getProtocolloAssociatoTipoSoggetto(soggetto.getTipo());
  119.             }
  120.             else{
  121.                 org.openspcoop2.core.config.Soggetto soggetto = soggettiCore.getSoggetto(soggInt);
  122.                 protocollo = soggettiCore.getProtocolloAssociatoTipoSoggetto(soggetto.getTipo());
  123.             }
  124.             boolean modipa = porteApplicativeCore.isProfiloModIPA(protocollo);

  125.             //decodifica soggetto scelto
  126.             String tipoSoggettoScelto = null;
  127.             String nomeSoggettoScelto = null;
  128.             if(idSoggettoToAdd != null) {
  129.                 int soggettoToAddInt = Integer.parseInt(idSoggettoToAdd);
  130.                
  131.                 if(porteApplicativeCore.isRegistroServiziLocale()){
  132.                     org.openspcoop2.core.registry.Soggetto soggetto = soggettiCore.getSoggettoRegistro(soggettoToAddInt);
  133.                     tipoSoggettoScelto = soggetto.getTipo();
  134.                     nomeSoggettoScelto = soggetto.getNome();
  135.                 }
  136.                 else{
  137.                     org.openspcoop2.core.config.Soggetto soggetto = soggettiCore.getSoggetto(soggettoToAddInt);
  138.                     tipoSoggettoScelto = soggetto.getTipo();
  139.                     nomeSoggettoScelto = soggetto.getNome();
  140.                 }
  141.             }
  142.            
  143.             // Prendo nome della porta applicativa
  144.             PortaApplicativa pa = porteApplicativeCore.getPortaApplicativa(idInt);
  145.             String nomePorta = pa.getNome();
  146.             CredenzialeTipo tipoAutenticazione = CredenzialeTipo.toEnumConstant(pa.getAutenticazione());
  147.             Boolean appId = null;
  148.             if(CredenzialeTipo.APIKEY.equals(tipoAutenticazione)) {
  149.                 ApiKeyState apiKeyState =  new ApiKeyState(porteApplicativeCore.getParametroAutenticazione(pa.getAutenticazione(), pa.getProprietaAutenticazioneList()));
  150.                 appId = apiKeyState.appIdSelected;
  151.             }
  152.                        
  153.             List<String> tipiSoggettiGestitiProtocollo = soggettiCore.getTipiSoggettiGestitiProtocollo(protocollo);
  154.            
  155.             // lista soggetti disponibili
  156.             String[] soggettiList = null;
  157.             String[] soggettiListLabel = null;
  158.            
  159.             // calcolo soggetti compatibili con tipi protocollo supportati dalla pa e credenziali indicate
  160.             List<IDSoggettoDB> list = null;
  161.             if(apsCore.isVisioneOggettiGlobale(userLogin)){
  162.                 list = soggettiCore.getSoggettiFromTipoAutenticazione(tipiSoggettiGestitiProtocollo, null, tipoAutenticazione, appId, pddTipologiaSoggettoAutenticati);
  163.             }else{
  164.                 list = soggettiCore.getSoggettiFromTipoAutenticazione(tipiSoggettiGestitiProtocollo, userLogin, tipoAutenticazione, appId, pddTipologiaSoggettoAutenticati);
  165.             }
  166.             if(list!=null && !list.isEmpty() && gestioneErogatoriSoggettiAutenticatiEscludiSoggettoErogatore) {
  167.                 for (int i = 0; i < list.size(); i++) {
  168.                     IDSoggettoDB soggettoCheck = list.get(i);
  169.                     if(soggettoCheck.getTipo().equals(pa.getTipoSoggettoProprietario()) && soggettoCheck.getNome().equals(pa.getNomeSoggettoProprietario())) {
  170.                         list.remove(i);
  171.                         break;
  172.                     }
  173.                 }
  174.             }

  175.             boolean multiTenant = porteApplicativeCore.isMultitenant();
  176.            
  177.             PortaApplicativaAutorizzazioneSoggetti soggetti = pa.getSoggetti();
  178.             List<PortaApplicativaAutorizzazioneSoggetto> soggettoList = soggetti != null ? soggetti.getSoggettoList() : new ArrayList<>();
  179.             if (list!=null && !list.isEmpty()) {
  180.                 List<String> soggettiListTmp = new ArrayList<>();
  181.                 List<String> soggettiListLabelTmp = new ArrayList<>();
  182.                 for (IDSoggettoDB soggetto : list) {
  183.                     // scartare i soggetti gia associati
  184.                     boolean found = false;
  185.                    
  186.                     for (PortaApplicativaAutorizzazioneSoggetto soggettoAssociatoPa : soggettoList) {
  187.                         if(soggettoAssociatoPa.getTipo().equals(soggetto.getTipo()) && soggettoAssociatoPa.getNome().equals(soggetto.getNome())) {
  188.                             found = true;
  189.                             break;
  190.                         }
  191.                     }
  192.                    
  193.                     boolean soggettoErogatoreServizio = false;
  194.                     if(pa.getTipoSoggettoProprietario().equals(soggetto.getTipo()) &&
  195.                             pa.getNomeSoggettoProprietario().equals(soggetto.getNome())) {
  196.                         soggettoErogatoreServizio = true;
  197.                     }
  198.                                        
  199.                     if(!found && (!soggettoErogatoreServizio || multiTenant)){
  200.                         soggettiListTmp.add(soggetto.getId().toString());
  201.                         soggettiListLabelTmp.add(porteApplicativeHelper.getLabelNomeSoggetto(protocollo, soggetto.getTipo() , soggetto.getNome()));
  202.                     }
  203.                 }
  204.                 if(soggettiListTmp!=null && !soggettiListTmp.isEmpty()) {
  205.                     soggettiList = soggettiListTmp.toArray(new String[1]);
  206.                     soggettiListLabel = soggettiListLabelTmp.toArray(new String[1]);
  207.                 }
  208.             }
  209.            
  210.             List<Parameter> lstParam = porteApplicativeHelper.getTitoloPA(parentPA, idsogg, idAsps);
  211.            
  212.             String labelPerPorta = null;
  213.             if(parentPA!=null && (parentPA.intValue() == PorteApplicativeCostanti.ATTRIBUTO_PORTE_APPLICATIVE_PARENT_CONFIGURAZIONE)) {
  214.                 labelPerPorta = porteApplicativeCore.getLabelRegolaMappingErogazionePortaApplicativa(
  215.                         PorteApplicativeCostanti.LABEL_PARAMETRO_PORTE_APPLICATIVE_CONTROLLO_ACCESSI_CONFIG_DI,
  216.                         PorteApplicativeCostanti.LABEL_PARAMETRO_PORTE_APPLICATIVE_CONTROLLO_ACCESSI,
  217.                         pa);
  218.             }
  219.             else {
  220.                 labelPerPorta = PorteApplicativeCostanti.LABEL_PARAMETRO_PORTE_APPLICATIVE_CONTROLLO_ACCESSI_CONFIG_DI+nomePorta;
  221.             }
  222.            
  223.             lstParam.add(new Parameter(labelPerPorta, PorteApplicativeCostanti.SERVLET_NAME_PORTE_APPLICATIVE_CONTROLLO_ACCESSI,
  224.                     new Parameter( PorteApplicativeCostanti.PARAMETRO_PORTE_APPLICATIVE_ID, idPorta),
  225.                     new Parameter( PorteApplicativeCostanti.PARAMETRO_PORTE_APPLICATIVE_ID_SOGGETTO, idsogg),
  226.                     new Parameter(PorteApplicativeCostanti.PARAMETRO_PORTE_APPLICATIVE_ID_ASPS, idAsps)));
  227.            
  228.             String labelPagLista =
  229.                     (
  230.                             modipa ?
  231.                             CostantiControlStation.LABEL_PARAMETRO_PORTE_CONTROLLO_ACCESSI_AUTORIZZAZIONE_CANALE
  232.                             :
  233.                             CostantiControlStation.LABEL_PARAMETRO_PORTE_AUTORIZZAZIONE_TRASPORTO
  234.                     )
  235.                     + " - " +
  236.                     PorteApplicativeCostanti.LABEL_PARAMETRO_PORTE_APPLICATIVE_SOGGETTO_CONFIG;
  237.            
  238.             lstParam.add(new Parameter(labelPagLista,
  239.                     PorteApplicativeCostanti.SERVLET_NAME_PORTE_APPLICATIVE_SOGGETTO_LIST,
  240.                     new Parameter( PorteApplicativeCostanti.PARAMETRO_PORTE_APPLICATIVE_ID, idPorta),
  241.                     new Parameter( PorteApplicativeCostanti.PARAMETRO_PORTE_APPLICATIVE_ID_SOGGETTO, idsogg),
  242.                     new Parameter( PorteApplicativeCostanti.PARAMETRO_PORTE_APPLICATIVE_ID_ASPS, idAsps)
  243.                     ));
  244.             lstParam.add(ServletUtils.getParameterAggiungi());
  245.            
  246.             // Se servizioApplicativohid = null, devo visualizzare la pagina per
  247.             // l'inserimento dati
  248.             if (porteApplicativeHelper.isEditModeInProgress()) {
  249.                
  250.                 // setto la barra del titolo
  251.                 ServletUtils.setPageDataTitle(pd, lstParam);

  252.                 // preparo i campi
  253.                 List<DataElement> dati = new ArrayList<>();
  254.                 dati.add(ServletUtils.getDataElementForEditModeFinished());
  255.                
  256.                 dati = porteApplicativeHelper.addPorteSoggettoToDati(TipoOperazione.ADD, dati, soggettiListLabel, soggettiList, idSoggettoToAdd, soggettoList.size(), true, true);

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

  258.                 pd.setDati(dati);

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

  260.                 return ServletUtils.getStrutsForwardEditModeInProgress(mapping, PorteApplicativeCostanti.OBJECT_NAME_PORTE_APPLICATIVE_SOGGETTO,
  261.                         ForwardParams.ADD());
  262.             }

  263.             // Controlli sui campi immessi
  264.             boolean isOk = porteApplicativeHelper.porteAppSoggettoCheckData(TipoOperazione.ADD);
  265.             if (!isOk) {
  266.                 // setto la barra del titolo
  267.                 ServletUtils.setPageDataTitle(pd, lstParam);

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

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

  271.                 dati = porteApplicativeHelper.addPorteSoggettoToDati(TipoOperazione.ADD, dati, soggettiListLabel, soggettiList, idSoggettoToAdd, soggettoList.size(), true, true);

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

  273.                 pd.setDati(dati);

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

  275.                 return ServletUtils.getStrutsForwardEditModeCheckError(mapping, PorteApplicativeCostanti.OBJECT_NAME_PORTE_APPLICATIVE_SOGGETTO,
  276.                         ForwardParams.ADD());
  277.             }

  278.             // Inserisco il servizioApplicativo nel db
  279.             PortaApplicativaAutorizzazioneSoggetto paSoggetto = new PortaApplicativaAutorizzazioneSoggetto();
  280.             paSoggetto.setTipo(tipoSoggettoScelto);
  281.             paSoggetto.setNome(nomeSoggettoScelto);
  282.                
  283.             if(soggetti != null)
  284.                 soggetti.addSoggetto(paSoggetto);
  285.             else {
  286.                 soggetti = new PortaApplicativaAutorizzazioneSoggetti();
  287.                 soggetti.addSoggetto(paSoggetto);
  288.                 pa.setSoggetti(soggetti);
  289.             }

  290.             porteApplicativeCore.performUpdateOperation(userLogin, porteApplicativeHelper.smista(), pa);

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

  293.             int idLista = Liste.PORTE_APPLICATIVE_SOGGETTO;

  294.             ricerca = porteApplicativeHelper.checkSearchParameters(idLista, ricerca);

  295.             List<PortaApplicativaAutorizzazioneSoggetto> lista = porteApplicativeCore.porteAppSoggettoList(Integer.parseInt(idPorta), ricerca);

  296.             porteApplicativeHelper.preparePorteAppSoggettoList(nomePorta, ricerca, lista);

  297.             ServletUtils.setGeneralAndPageDataIntoSession(request, session, gd, pd);
  298.             // Forward control to the specified success URI
  299.             return ServletUtils.getStrutsForwardEditModeFinished(mapping, PorteApplicativeCostanti.OBJECT_NAME_PORTE_APPLICATIVE_SOGGETTO,
  300.                     ForwardParams.ADD());
  301.         } catch (Exception e) {
  302.             return ServletUtils.getStrutsForwardError(ControlStationCore.getLog(), e, pd, request, session, gd, mapping,
  303.                     PorteApplicativeCostanti.OBJECT_NAME_PORTE_APPLICATIVE_SOGGETTO,
  304.                     ForwardParams.ADD());
  305.         }
  306.     }
  307. }