PorteDelegateServizioApplicativoAdd.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.pd;

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

  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.PortaDelegata;
  32. import org.openspcoop2.core.config.PortaDelegataAutorizzazioneServiziApplicativi;
  33. import org.openspcoop2.core.config.PortaDelegataServizioApplicativo;
  34. import org.openspcoop2.core.config.ServizioApplicativo;
  35. import org.openspcoop2.core.config.constants.CredenzialeTipo;
  36. import org.openspcoop2.core.config.driver.db.IDServizioApplicativoDB;
  37. import org.openspcoop2.core.id.IDSoggetto;
  38. import org.openspcoop2.web.ctrlstat.core.ControlStationCore;
  39. import org.openspcoop2.web.ctrlstat.core.ConsoleSearch;
  40. import org.openspcoop2.web.ctrlstat.costanti.CostantiControlStation;
  41. import org.openspcoop2.web.ctrlstat.servlet.ApiKeyState;
  42. import org.openspcoop2.web.ctrlstat.servlet.GeneralHelper;
  43. import org.openspcoop2.web.ctrlstat.servlet.sa.ServiziApplicativiCore;
  44. import org.openspcoop2.web.ctrlstat.servlet.sa.ServiziApplicativiCostanti;
  45. import org.openspcoop2.web.ctrlstat.servlet.soggetti.SoggettiCore;
  46. import org.openspcoop2.web.lib.mvc.DataElement;
  47. import org.openspcoop2.web.lib.mvc.ForwardParams;
  48. import org.openspcoop2.web.lib.mvc.GeneralData;
  49. import org.openspcoop2.web.lib.mvc.PageData;
  50. import org.openspcoop2.web.lib.mvc.Parameter;
  51. import org.openspcoop2.web.lib.mvc.ServletUtils;
  52. import org.openspcoop2.web.lib.mvc.TipoOperazione;

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

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

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

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

  69.         GeneralHelper generalHelper = new GeneralHelper(session);

  70.         // Inizializzo GeneralData
  71.         GeneralData gd = generalHelper.initGeneralData(request);

  72.         try {
  73.             PorteDelegateHelper porteDelegateHelper = new PorteDelegateHelper(request, pd, session);
  74.             String id = porteDelegateHelper.getParameter(PorteDelegateCostanti.PARAMETRO_PORTE_DELEGATE_ID);
  75.             int idInt = Integer.parseInt(id);
  76.             String idsogg = porteDelegateHelper.getParameter(PorteDelegateCostanti.PARAMETRO_PORTE_DELEGATE_ID_SOGGETTO);
  77.             int soggInt = Integer.parseInt(idsogg);
  78.             String servizioApplicativo = porteDelegateHelper.getParameter(PorteDelegateCostanti.PARAMETRO_PORTE_DELEGATE_SERVIZIO_APPLICATIVO);

  79.             String idAsps = porteDelegateHelper.getParameter(PorteDelegateCostanti.PARAMETRO_PORTE_DELEGATE_ID_ASPS);
  80.             if(idAsps == null)
  81.                 idAsps = "";
  82.            
  83.             String idFruizione = porteDelegateHelper.getParameter(PorteDelegateCostanti.PARAMETRO_PORTE_DELEGATE_ID_FRUIZIONE);
  84.             if(idFruizione == null)
  85.                 idFruizione = "";
  86.            
  87.             String tokenList = porteDelegateHelper.getParameter(PorteDelegateCostanti.PARAMETRO_PORTE_DELEGATE_TOKEN_AUTHORIZATION);
  88.             boolean isToken = tokenList!=null && !"".equals(tokenList) && Boolean.valueOf(tokenList);
  89.            
  90.             // prelevo il flag che mi dice da quale pagina ho acceduto la sezione delle porte delegate
  91.             Integer parentPD = ServletUtils.getIntegerAttributeFromSession(PorteDelegateCostanti.ATTRIBUTO_PORTE_DELEGATE_PARENT, session, request);
  92.             if(parentPD == null) parentPD = PorteDelegateCostanti.ATTRIBUTO_PORTE_DELEGATE_PARENT_NONE;
  93.            
  94.             // Preparo il menu
  95.             porteDelegateHelper.makeMenu();

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

  97.            
  98.             // Prendo nome, tipo e pdd del soggetto
  99.             PorteDelegateCore porteDelegateCore = new PorteDelegateCore();
  100.             SoggettiCore soggettiCore = new SoggettiCore(porteDelegateCore);
  101.             ServiziApplicativiCore saCore = new ServiziApplicativiCore(porteDelegateCore);
  102.            
  103.             boolean escludiSAServer = saCore.isApplicativiServerEnabled(porteDelegateHelper);
  104.             String filtroTipoSA = escludiSAServer ? ServiziApplicativiCostanti.VALUE_SERVIZI_APPLICATIVI_TIPO_CLIENT : null;
  105.            
  106.             IDSoggetto idSoggetto = null;
  107.             if(porteDelegateCore.isRegistroServiziLocale()){
  108.                 org.openspcoop2.core.registry.Soggetto soggetto = soggettiCore.getSoggettoRegistro(soggInt);
  109.                 idSoggetto = new IDSoggetto(soggetto.getTipo(),soggetto.getNome());
  110.             }
  111.             else{
  112.                 org.openspcoop2.core.config.Soggetto soggetto = soggettiCore.getSoggetto(soggInt);
  113.                 idSoggetto = new IDSoggetto(soggetto.getTipo(),soggetto.getNome());
  114.             }

  115.             // Prendo nome della porta delegata
  116.             PortaDelegata pde = porteDelegateCore.getPortaDelegata(idInt);
  117.             CredenzialeTipo tipoAutenticazione = null;
  118.             Boolean appId = null;
  119.             String tokenPolicy = null;
  120.             if(isToken) {
  121.                 tipoAutenticazione = CredenzialeTipo.TOKEN;
  122.                 if(pde.getGestioneToken()!=null && pde.getGestioneToken().getPolicy()!=null) {
  123.                     tokenPolicy = pde.getGestioneToken().getPolicy();
  124.                 }
  125.             }
  126.             else {
  127.                 tipoAutenticazione = CredenzialeTipo.toEnumConstant(pde.getAutenticazione());
  128.                 if(CredenzialeTipo.APIKEY.equals(tipoAutenticazione)) {
  129.                     ApiKeyState apiKeyState =  new ApiKeyState(porteDelegateCore.getParametroAutenticazione(pde.getAutenticazione(), pde.getProprietaAutenticazioneList()));
  130.                     appId = apiKeyState.appIdSelected;
  131.                 }
  132.             }
  133.             String idporta = pde.getNome();

  134.             Parameter pId = new Parameter(PorteDelegateCostanti.PARAMETRO_PORTE_DELEGATE_ID, id);
  135.             Parameter pIdSoggetto = new Parameter(PorteDelegateCostanti.PARAMETRO_PORTE_DELEGATE_ID_SOGGETTO, idsogg);
  136.             Parameter pIdAsps = new Parameter(PorteDelegateCostanti.PARAMETRO_PORTE_DELEGATE_ID_ASPS, idAsps);
  137.             Parameter pIdFrizione = new Parameter(PorteDelegateCostanti.PARAMETRO_PORTE_DELEGATE_ID_FRUIZIONE, idFruizione);
  138.             Parameter[] urlParms = { pId,pIdSoggetto,pIdAsps,pIdFrizione,
  139.                     new Parameter(PorteDelegateCostanti.PARAMETRO_PORTE_DELEGATE_TOKEN_AUTHORIZATION, isToken+"") };
  140.            
  141.             List<Parameter> lstParam = porteDelegateHelper.getTitoloPD(parentPD, idsogg, idAsps, idFruizione);
  142.            
  143.             String labelPerPorta = null;
  144.             if(parentPD!=null && (parentPD.intValue() == PorteDelegateCostanti.ATTRIBUTO_PORTE_DELEGATE_PARENT_CONFIGURAZIONE)) {
  145.                 labelPerPorta = porteDelegateCore.getLabelRegolaMappingFruizionePortaDelegata(
  146.                         PorteDelegateCostanti.LABEL_PARAMETRO_PORTE_DELEGATE_CONTROLLO_ACCESSI_CONFIG_DI,
  147.                         PorteDelegateCostanti.LABEL_PARAMETRO_PORTE_DELEGATE_CONTROLLO_ACCESSI,
  148.                         pde);
  149.             }
  150.             else {
  151.                 labelPerPorta = PorteDelegateCostanti.LABEL_PARAMETRO_PORTE_DELEGATE_CONTROLLO_ACCESSI_CONFIG_DI+idporta;
  152.             }
  153.            
  154.             lstParam.add(new Parameter(labelPerPorta, PorteDelegateCostanti.SERVLET_NAME_PORTE_DELEGATE_CONTROLLO_ACCESSI,
  155.                     new Parameter(PorteDelegateCostanti.PARAMETRO_PORTE_DELEGATE_ID, "" + pde.getId()),
  156.                     new Parameter(PorteDelegateCostanti.PARAMETRO_PORTE_DELEGATE_NOME_PORTA, pde.getNome()),
  157.                     new Parameter(PorteDelegateCostanti.PARAMETRO_PORTE_DELEGATE_ID_SOGGETTO, pde.getIdSoggetto() + ""),
  158.                     pIdAsps, new Parameter(PorteDelegateCostanti.PARAMETRO_PORTE_DELEGATE_ID_FRUIZIONE, idFruizione+ "")));
  159.            
  160.             String labelApp = PorteDelegateCostanti.LABEL_PARAMETRO_PORTE_DELEGATE_SERVIZIO_APPLICATIVO_CONFIG;
  161.             if(!porteDelegateHelper.isModalitaCompleta() || isToken) {
  162.                 labelApp = PorteDelegateCostanti.LABEL_PARAMETRO_PORTE_DELEGATE_APPLICATIVO_CONFIG;
  163.             }
  164.             String labelPagLista =
  165.                     (
  166.                             isToken ?
  167.                             CostantiControlStation.LABEL_PARAMETRO_PORTE_AUTORIZZAZIONE_TOKEN
  168.                             :
  169.                             CostantiControlStation.LABEL_PARAMETRO_PORTE_AUTORIZZAZIONE_TRASPORTO
  170.                     )
  171.                     + " - " +
  172.                     labelApp;
  173.            
  174.             lstParam.add(new Parameter(labelPagLista,
  175.                     PorteDelegateCostanti.SERVLET_NAME_PORTE_DELEGATE_SERVIZIO_APPLICATIVO_LIST,urlParms));
  176.             lstParam.add(ServletUtils.getParameterAggiungi());
  177.            
  178.             // Se servizioApplicativohid = null, devo visualizzare la pagina per
  179.             // l'inserimento dati
  180.             if( porteDelegateHelper.isEditModeInProgress()){
  181.                 // setto la barra del titolo
  182.                 ServletUtils.setPageDataTitle(pd, lstParam);

  183.                 // preparo i campi
  184.                 List<DataElement> dati = new ArrayList<>();
  185.                
  186.                 dati.add(ServletUtils.getDataElementForEditModeFinished());
  187.                
  188.                

  189.                 // Prendo la lista di servizioApplicativo associati al soggetto
  190.                 // e la metto in un array
  191.                 List<String> silV = new ArrayList<>();
  192.                 boolean bothSslAndToken = false;
  193.                 List<IDServizioApplicativoDB> oldSilList = saCore.soggettiServizioApplicativoList(idSoggetto,userLogin,tipoAutenticazione, appId, filtroTipoSA,
  194.                         bothSslAndToken, tokenPolicy);
  195.                 for (int i = 0; i < oldSilList.size(); i++) {
  196.                     IDServizioApplicativoDB singleSA = oldSilList.get(i);
  197.                     String tmpNome = singleSA.getNome();
  198.                     boolean trovatoSA = false;
  199.                     if(isToken) {
  200.                         if(pde.getAutorizzazioneToken()!=null && pde.getAutorizzazioneToken().getServiziApplicativi()!=null) {
  201.                             for (int j = 0; j < pde.getAutorizzazioneToken().getServiziApplicativi().sizeServizioApplicativoList(); j++) {
  202.                                 PortaDelegataServizioApplicativo tmpSA = pde.getAutorizzazioneToken().getServiziApplicativi().getServizioApplicativo(j);
  203.                                 if (tmpNome.equals(tmpSA.getNome())) {
  204.                                     trovatoSA = true;
  205.                                     break;
  206.                                 }
  207.                             }
  208.                         }
  209.                     }
  210.                     else {
  211.                         for (int j = 0; j < pde.sizeServizioApplicativoList(); j++) {
  212.                             PortaDelegataServizioApplicativo tmpSA = pde.getServizioApplicativo(j);
  213.                             if (tmpNome.equals(tmpSA.getNome())) {
  214.                                 trovatoSA = true;
  215.                                 break;
  216.                             }
  217.                         }
  218.                     }
  219.                     if (!trovatoSA)
  220.                         silV.add(tmpNome);
  221.                 }
  222.                 String[] servizioApplicativoList = null;
  223.                 if (!silV.isEmpty()) {
  224.                     servizioApplicativoList = new String[silV.size()];
  225.                     for (int j = 0; j < silV.size(); j++)
  226.                         servizioApplicativoList[j] = silV.get(j);
  227.                 }

  228.                 dati = porteDelegateHelper.addPorteServizioApplicativoToDati(TipoOperazione.ADD,dati, "", servizioApplicativoList, oldSilList.size(),true, true, isToken);

  229.                 dati = porteDelegateHelper.addHiddenFieldsToDati(TipoOperazione.ADD, id, idsogg, null, idAsps,
  230.                         idFruizione, pde.getTipoSoggettoProprietario(), pde.getNomeSoggettoProprietario(), dati);

  231.                 pd.setDati(dati);

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

  233.                 return ServletUtils.getStrutsForwardEditModeInProgress(mapping,
  234.                         PorteDelegateCostanti.OBJECT_NAME_PORTE_DELEGATE_SERVIZIO_APPLICATIVO, ForwardParams.ADD());

  235.             }

  236.             // Controlli sui campi immessi
  237.             boolean isOk = porteDelegateHelper.porteDelegateServizioApplicativoCheckData(TipoOperazione.ADD);
  238.             if (!isOk) {
  239.                 // setto la barra del titolo
  240.                 ServletUtils.setPageDataTitle(pd, lstParam);

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

  243.                 dati.add(ServletUtils.getDataElementForEditModeFinished());
  244.                
  245.                 // Prendo la lista di servizioApplicativo (tranne quelli giĆ 
  246.                 // usati) e la metto in un array
  247.                 List<String> silV = new ArrayList<>();
  248.                 boolean bothSslAndToken = false;
  249.                 List<IDServizioApplicativoDB> oldSilList = saCore.soggettiServizioApplicativoList(idSoggetto,userLogin,tipoAutenticazione, appId, filtroTipoSA,
  250.                         bothSslAndToken, tokenPolicy);
  251.                 for (int i = 0; i < oldSilList.size(); i++) {
  252.                     IDServizioApplicativoDB singleSA = oldSilList.get(i);
  253.                     String tmpNome = singleSA.getNome();
  254.                     boolean trovatoSA = false;
  255.                     for (int j = 0; j < pde.sizeServizioApplicativoList(); j++) {
  256.                         PortaDelegataServizioApplicativo tmpSA = pde.getServizioApplicativo(j);
  257.                         if (tmpNome.equals(tmpSA.getNome())) {
  258.                             trovatoSA = true;
  259.                             break;
  260.                         }
  261.                     }
  262.                     if (!trovatoSA)
  263.                         silV.add(tmpNome);
  264.                 }
  265.                 String[] servizioApplicativoList = null;
  266.                 if (!silV.isEmpty()) {
  267.                     servizioApplicativoList = new String[silV.size()];
  268.                     for (int j = 0; j < silV.size(); j++)
  269.                         servizioApplicativoList[j] = silV.get(j);
  270.                 }

  271.                 dati = porteDelegateHelper.addPorteServizioApplicativoToDati(TipoOperazione.ADD, dati, servizioApplicativo, servizioApplicativoList, oldSilList.size(),true, true, isToken);

  272.                 dati = porteDelegateHelper.addHiddenFieldsToDati(TipoOperazione.ADD, id, idsogg, null, idAsps,
  273.                         idFruizione, pde.getTipoSoggettoProprietario(), pde.getNomeSoggettoProprietario(), dati);
  274.  
  275.                 pd.setDati(dati);

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

  277.                 return ServletUtils.getStrutsForwardEditModeCheckError(mapping,
  278.                         PorteDelegateCostanti.OBJECT_NAME_PORTE_DELEGATE_SERVIZIO_APPLICATIVO,
  279.                         ForwardParams.ADD());
  280.             }

  281.             // Inserisco il servizioApplicativo nel db
  282.             PortaDelegataServizioApplicativo sa = new PortaDelegataServizioApplicativo();
  283.             sa.setNome(servizioApplicativo);
  284.             if(isToken) {
  285.                 if(pde.getAutorizzazioneToken().getServiziApplicativi()==null) {
  286.                     pde.getAutorizzazioneToken().setServiziApplicativi(new PortaDelegataAutorizzazioneServiziApplicativi());
  287.                 }
  288.                 pde.getAutorizzazioneToken().getServiziApplicativi().addServizioApplicativo(sa);
  289.             }
  290.             else {
  291.                 pde.addServizioApplicativo(sa);
  292.             }

  293.             porteDelegateCore.performUpdateOperation(userLogin, porteDelegateHelper.smista(), pde);

  294.             // Preparo la lista
  295.             ConsoleSearch ricerca = (ConsoleSearch) ServletUtils.getSearchObjectFromSession(request, session, ConsoleSearch.class);
  296.            
  297.             int idLista = Liste.PORTE_DELEGATE_SERVIZIO_APPLICATIVO;
  298.             if(isToken) {
  299.                 idLista = Liste.PORTE_DELEGATE_TOKEN_SERVIZIO_APPLICATIVO;
  300.             }

  301.             ricerca = porteDelegateHelper.checkSearchParameters(idLista, ricerca);

  302.             List<ServizioApplicativo> lista = isToken ?
  303.                     porteDelegateCore.porteDelegateServizioApplicativoTokenList(Integer.parseInt(id), ricerca)
  304.                     :
  305.                     porteDelegateCore.porteDelegateServizioApplicativoList(Integer.parseInt(id), ricerca) ;

  306.             porteDelegateHelper.preparePorteDelegateServizioApplicativoList(idporta, ricerca, lista);

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

  308.             // Forward control to the specified success URI
  309.             return ServletUtils.getStrutsForwardEditModeFinished(mapping, PorteDelegateCostanti.OBJECT_NAME_PORTE_DELEGATE_SERVIZIO_APPLICATIVO,
  310.                     ForwardParams.ADD());
  311.         } catch (Exception e) {
  312.             return ServletUtils.getStrutsForwardError(ControlStationCore.getLog(), e, pd, request, session, gd, mapping,
  313.                     PorteDelegateCostanti.OBJECT_NAME_PORTE_DELEGATE_SERVIZIO_APPLICATIVO,
  314.                     ForwardParams.ADD());
  315.         }  
  316.     }


  317. }