Environment.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.core.config.rs.server.api.impl;

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

  23. import javax.servlet.http.HttpServletRequest;

  24. import org.openspcoop2.core.config.CanaleConfigurazione;
  25. import org.openspcoop2.core.config.CanaliConfigurazione;
  26. import org.openspcoop2.core.config.constants.StatoFunzionalita;
  27. import org.openspcoop2.core.config.rs.server.config.ServerProperties;
  28. import org.openspcoop2.core.id.IDSoggetto;
  29. import org.openspcoop2.utils.service.beans.ProfiloEnum;
  30. import org.openspcoop2.utils.service.beans.utils.BaseHelper;
  31. import org.openspcoop2.core.registry.IdSoggetto;
  32. import org.openspcoop2.core.registry.Soggetto;
  33. import org.openspcoop2.protocol.engine.ProtocolFactoryManager;
  34. import org.openspcoop2.protocol.sdk.IProtocolFactory;
  35. import org.openspcoop2.utils.service.context.IContext;
  36. import org.openspcoop2.utils.service.fault.jaxrs.FaultCode;
  37. import org.openspcoop2.web.ctrlstat.core.ControlStationCore;
  38. import org.openspcoop2.web.ctrlstat.servlet.config.ConfigurazioneCore;
  39. import org.openspcoop2.web.ctrlstat.servlet.pdd.PddCore;
  40. import org.openspcoop2.web.ctrlstat.servlet.soggetti.SoggettiCore;
  41. import org.openspcoop2.web.lib.mvc.PageData;

  42. /**
  43.  * Environment
  44.  *
  45.  * @author $Author$
  46.  * @version $Rev$, $Date$
  47.  *
  48.  */
  49. public class Environment {
  50.    
  51.     public final String tipo_protocollo;
  52.    
  53.     public final String tipo_soggetto;
  54.    
  55.     public final ProfiloEnum profilo;
  56.    
  57.     public final String userLogin;
  58.    
  59.     public final IdSoggetto idSoggetto;
  60.    
  61.     public final HttpRequestWrapper requestWrapper;
  62.    
  63.     public final PageData pd = new PageData();
  64.    
  65.     public final ControlStationCore stationCore;
  66.    
  67.     public final IProtocolFactory<?> protocolFactory;
  68.    
  69.     public final SoggettiCore soggettiCore;
  70.    
  71.     public final PddCore pddCore;
  72.    
  73.     public final ProtocolFactoryManager protocolFactoryMgr;
  74.    
  75.     public final boolean delete_404;
  76.    
  77.     public final boolean findall_404;

  78.     public final boolean multitenant;

  79.     public final boolean gestioneCanali;
  80.     public String canaleDefault;
  81.     public List<String> canali = new ArrayList<>();
  82.    
  83.     public Environment(HttpServletRequest req, ProfiloEnum profilo, String soggetto, IContext ctx) throws Exception {
  84.        
  85.         if (profilo == null)
  86.             profilo = Helper.getProfiloDefault();

  87.         soggetto = Helper.getSoggettoOrDefault(soggetto, profilo);
  88.        
  89.         this.userLogin = ctx.getAuthentication().getName();
  90.         this.profilo = profilo;
  91.         this.tipo_protocollo = BaseHelper.tipoProtocolloFromProfilo.get(profilo);
  92.         this.requestWrapper = new HttpRequestWrapper(req);
  93.        
  94.         this.stationCore = new ControlStationCore(true, ServerProperties.getInstance().getConfDirectory() ,this.tipo_protocollo);
  95.         this.pddCore = new PddCore(this.stationCore);
  96.         this.soggettiCore = new SoggettiCore(this.stationCore);
  97.         this.protocolFactoryMgr = ProtocolFactoryManager.getInstance();
  98.        
  99.         this.protocolFactory = this.protocolFactoryMgr.getProtocolFactoryByName(this.tipo_protocollo);
  100.         this.tipo_soggetto = this.protocolFactoryMgr.getDefaultOrganizationTypes().get(this.tipo_protocollo);
  101.        
  102.         this.idSoggetto = new IdSoggetto();
  103.         this.idSoggetto.setNome(soggetto);
  104.         this.idSoggetto.setTipo(this.tipo_soggetto);
  105.         this.idSoggetto.setId(-1L);
  106.        
  107.         ConfigurazioneCore confCore = new ConfigurazioneCore(this.stationCore);
  108.        
  109.         this.multitenant = confCore.getConfigurazioneGenerale().getMultitenant().getStato() == StatoFunzionalita.ABILITATO;
  110.        
  111.         try {
  112.             this.idSoggetto.setId(this.soggettiCore.getIdSoggetto(soggetto,this.tipo_soggetto));
  113.         } catch (Exception e) {}
  114.        
  115.         if (this.idSoggetto.getId() == -1) {
  116.             throw FaultCode.RICHIESTA_NON_VALIDA.toException("Il soggetto con nome " + soggetto + " e tipo " + this.tipo_soggetto + " non esiste.");
  117.         }
  118.        
  119.         this.delete_404 = ServerProperties.getInstance().isDelete404();
  120.         this.findall_404 = ServerProperties.getInstance().isFindall404();
  121.        
  122.         CanaliConfigurazione canali = confCore.getCanaliConfigurazione(false);
  123.         this.gestioneCanali = canali!=null && StatoFunzionalita.ABILITATO.equals(canali.getStato());
  124.         if(this.gestioneCanali && canali.getCanaleList()!=null && !canali.getCanaleList().isEmpty()) {
  125.             for (CanaleConfigurazione canale : canali.getCanaleList()) {
  126.                 if(canale.isCanaleDefault()) {
  127.                     this.canaleDefault = canale.getNome();
  128.                 }
  129.                 this.canali.add(canale.getNome());
  130.             }
  131.         }
  132.     }
  133.    
  134.     public boolean isDominioInterno(IDSoggetto idSoggetto) {
  135.         try {
  136.             Soggetto soggetto = this.soggettiCore.getSoggettoRegistro(idSoggetto);
  137.             return !this.pddCore.isPddEsterna(soggetto.getPortaDominio());
  138.         }catch(Exception e) {
  139.             return false;
  140.         }
  141.     }
  142.    
  143.     public boolean isProfiloModi() {
  144.         return this.isProfiloModi(this.profilo);
  145.     }
  146.     public boolean isProfiloModi(ProfiloEnum profilo) {
  147.         return profilo!=null && (ProfiloEnum.MODI.equals(profilo) || ProfiloEnum.MODIPA.equals(profilo));
  148.     }
  149.    
  150.     public boolean isProfiloSPCoop() {
  151.         return this.isProfiloSPCoop(this.profilo);
  152.     }
  153.     public boolean isProfiloSPCoop(ProfiloEnum profilo) {
  154.         return profilo!=null && ProfiloEnum.SPCOOP.equals(profilo);
  155.     }

  156. }