InformazioniUtilizzoOggettoRegistro.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.utils;

  21. import java.io.ByteArrayOutputStream;
  22. import java.io.IOException;
  23. import java.util.ArrayList;
  24. import java.util.HashMap;
  25. import java.util.List;
  26. import java.util.Map;

  27. import javax.servlet.ServletException;
  28. import javax.servlet.ServletOutputStream;
  29. import javax.servlet.http.HttpServlet;
  30. import javax.servlet.http.HttpServletRequest;
  31. import javax.servlet.http.HttpServletResponse;
  32. import javax.servlet.http.HttpSession;

  33. import org.apache.commons.io.IOUtils;
  34. import org.apache.commons.lang.StringUtils;
  35. import org.openspcoop2.core.commons.CoreException;
  36. import org.openspcoop2.core.config.CanaleConfigurazione;
  37. import org.openspcoop2.core.controllo_traffico.IdPolicy;
  38. import org.openspcoop2.core.id.IDAccordo;
  39. import org.openspcoop2.core.id.IDGenericProperties;
  40. import org.openspcoop2.core.id.IDGruppo;
  41. import org.openspcoop2.core.id.IDPortType;
  42. import org.openspcoop2.core.id.IDPortTypeAzione;
  43. import org.openspcoop2.core.id.IDResource;
  44. import org.openspcoop2.core.id.IDRuolo;
  45. import org.openspcoop2.core.id.IDScope;
  46. import org.openspcoop2.core.id.IDServizio;
  47. import org.openspcoop2.core.id.IDServizioApplicativo;
  48. import org.openspcoop2.core.id.IDSoggetto;
  49. import org.openspcoop2.core.registry.Soggetto;
  50. import org.openspcoop2.core.registry.driver.IDServizioFactory;
  51. import org.openspcoop2.core.plugins.IdPlugin;
  52. import org.openspcoop2.utils.json.JSONUtils;
  53. import org.openspcoop2.utils.transport.http.HttpConstants;
  54. import org.openspcoop2.utils.transport.http.HttpRequestMethod;
  55. import org.openspcoop2.web.ctrlstat.core.ControlStationCore;
  56. import org.openspcoop2.web.ctrlstat.costanti.InUsoType;
  57. import org.openspcoop2.web.ctrlstat.servlet.apc.AccordiServizioParteComuneCore;
  58. import org.openspcoop2.web.ctrlstat.servlet.aps.erogazioni.ErogazioniDetailsUtilities;
  59. import org.openspcoop2.web.ctrlstat.servlet.archivi.ArchiviCore;
  60. import org.openspcoop2.web.ctrlstat.servlet.archivi.ExporterUtils;
  61. import org.openspcoop2.web.ctrlstat.servlet.config.ConfigurazioneCore;
  62. import org.openspcoop2.web.ctrlstat.servlet.gruppi.GruppiCore;
  63. import org.openspcoop2.web.ctrlstat.servlet.ruoli.RuoliCore;
  64. import org.openspcoop2.web.ctrlstat.servlet.sa.ServiziApplicativiCore;
  65. import org.openspcoop2.web.ctrlstat.servlet.scope.ScopeCore;
  66. import org.openspcoop2.web.ctrlstat.servlet.soggetti.SoggettiCore;
  67. import org.openspcoop2.web.lib.mvc.PageData;

  68. /**
  69.  * InformazioniUtilizzoOggettoRegistro
  70.  *
  71.  * @author Andrea Poli (apoli@link.it)
  72.  * @author $Author$
  73.  * @version $Rev$, $Date$
  74.  *
  75.  */
  76. public class InformazioniUtilizzoOggettoRegistro extends HttpServlet{

  77.     /**
  78.      *
  79.      */
  80.     private static final long serialVersionUID = 1L;

  81.     @Override
  82.     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  83.         this.processRequest(req, resp);
  84.     }

  85.     @Override
  86.     protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

  87.         ByteArrayOutputStream baos = new ByteArrayOutputStream();
  88.         try {
  89.             IOUtils.copy(req.getInputStream(), baos);
  90.         }catch(Exception e){
  91.             ControlStationCore.logError("Errore durante la ricerca delle informazioni oggetto: "+e.getMessage(), e);
  92.             return;
  93.         }          

  94.         this.processRequest(req, resp);
  95.     }

  96.     private void processRequest(HttpServletRequest request, HttpServletResponse response) {
  97.         try(ByteArrayOutputStream baosPayload = new ByteArrayOutputStream();){
  98.             HttpRequestMethod httpRequestMethod = HttpRequestMethod.valueOf(request.getMethod().toUpperCase());

  99.             if(httpRequestMethod.equals(HttpRequestMethod.POST)) { // copia del payload
  100.                 IOUtils.copy(request.getInputStream(), baosPayload);
  101.             }

  102.             HttpSession session = request.getSession(true);
  103.             PageData pd = new PageData();
  104.             UtilsHelper registroHelper = new UtilsHelper(request, pd, session);
  105.             ArchiviCore archiviCore = new ArchiviCore();
  106.             AccordiServizioParteComuneCore apcCore = new AccordiServizioParteComuneCore(archiviCore);
  107.             SoggettiCore soggettiCore = new SoggettiCore(archiviCore);
  108.             ServiziApplicativiCore saCore = new ServiziApplicativiCore(archiviCore);
  109.             RuoliCore ruoliCore = new RuoliCore(archiviCore);
  110.             ScopeCore scopeCore = new ScopeCore(archiviCore);
  111.             ConfigurazioneCore confCore = new ConfigurazioneCore(archiviCore);
  112.             GruppiCore gruppiCore = new GruppiCore(archiviCore);

  113.             String identificativoOggetto = registroHelper.getParameter(UtilsCostanti.PARAMETRO_INFORMAZIONI_UTILIZZO_OGGETTO_ID_OGGETTO);
  114.             String tipoOggetto = registroHelper.getParameter(UtilsCostanti.PARAMETRO_INFORMAZIONI_UTILIZZO_OGGETTO_TIPO_OGGETTO);
  115.             String tipoRisposta = registroHelper.getParameter(UtilsCostanti.PARAMETRO_INFORMAZIONI_UTILIZZO_OGGETTO_TIPO_RISPOSTA);

  116.             InUsoType inUsoType = InUsoType.valueOf(tipoOggetto);
  117.            
  118.             ExporterUtils exporterUtils = new ExporterUtils(archiviCore);
  119.             List<?> identificativi = null;
  120.             List<String> risultatiRicerca = new ArrayList<>();
  121.             switch (inUsoType) {
  122.             case ACCORDO_SERVIZIO_PARTE_COMUNE:
  123.                 identificativi = exporterUtils.getIdsAccordiServizioParteComune(identificativoOggetto);
  124.                 for (Object object : identificativi) {
  125.                     IDAccordo idAccordo = (IDAccordo)object;
  126.                     risultatiRicerca.add(apcCore.getDettagliAccordoInUso(idAccordo));
  127.                 }
  128.                 break;
  129.             case RISORSA:
  130.                 identificativi = exporterUtils.getIdsAccordiServizioParteComuneRisorsa(identificativoOggetto);
  131.                 for (Object object : identificativi) {
  132.                     IDResource idRisorsa = (IDResource)object;
  133.                     risultatiRicerca.add(apcCore.getDettagliRisorsaInUso(idRisorsa));
  134.                 }
  135.                 break;
  136.             case PORT_TYPE:
  137.                 identificativi = exporterUtils.getIdsAccordiServizioParteComunePortType(identificativoOggetto);
  138.                 for (Object object : identificativi) {
  139.                     IDPortType idPT = (IDPortType)object;
  140.                     risultatiRicerca.add(apcCore.getDettagliPortTypeInUso(idPT));
  141.                 }
  142.                 break;
  143.             case OPERAZIONE:
  144.                 identificativi = exporterUtils.getIdsAccordiServizioParteComuneOperazione(identificativoOggetto);
  145.                 for (Object object : identificativi) {
  146.                     IDPortTypeAzione idOperazione = (IDPortTypeAzione)object;
  147.                     risultatiRicerca.add(apcCore.getDettagliOperazioneInUso(idOperazione));
  148.                 }
  149.                 break;
  150.             case SERVIZIO_APPLICATIVO:
  151.                 identificativi = exporterUtils.getIdsServiziApplicativi(identificativoOggetto);
  152.                 for (Object object : identificativi) {
  153.                     IDServizioApplicativo idServizioApplicativo = (IDServizioApplicativo)object;
  154.                     boolean verificaRuoli = true;
  155.                     risultatiRicerca.add(saCore.getDettagliServizioApplicativoInUso(idServizioApplicativo, verificaRuoli));
  156.                 }
  157.                 break;
  158.             case SOGGETTO:
  159.                 identificativi = exporterUtils.getIdsSoggetti(identificativoOggetto);
  160.                 for (Object object : identificativi) {
  161.                     IDSoggetto idSoggetto = (IDSoggetto)object;
  162.                     Soggetto soggetto = new Soggetto();
  163.                     soggetto.setTipo(idSoggetto.getTipo());
  164.                     soggetto.setNome(idSoggetto.getNome());
  165.                     soggetto.setIdentificativoPorta(idSoggetto.getCodicePorta());
  166.                     boolean verificaRuoli = true;
  167.                     risultatiRicerca.add(soggettiCore.getDettagliSoggettoInUso(soggetto, verificaRuoli));
  168.                 }
  169.                 break;
  170.             case RUOLO:
  171.                 identificativi = exporterUtils.getIdsRuoli(identificativoOggetto);
  172.                 for (Object object : identificativi) {
  173.                     IDRuolo idRuolo = (IDRuolo)object;
  174.                     risultatiRicerca.add(ruoliCore.getDettagliRuoloInUso(idRuolo));
  175.                 }
  176.                 break;
  177.             case SCOPE:
  178.                 identificativi = exporterUtils.getIdsScope(identificativoOggetto);
  179.                 for (Object object : identificativi) {
  180.                     IDScope idScope = (IDScope)object;
  181.                     risultatiRicerca.add(scopeCore.getDettagliScopeInUso(idScope));
  182.                 }
  183.                 break;
  184.             case CANALE:
  185.                 identificativi = exporterUtils.getIdsCanali(identificativoOggetto);
  186.                 for (Object object : identificativi) {
  187.                     CanaleConfigurazione canale = (CanaleConfigurazione)object;
  188.                     risultatiRicerca.add(confCore.getDettagliCanaleInUso(canale));
  189.                 }
  190.                 break;
  191.             case GRUPPO:
  192.                 identificativi = exporterUtils.getIdsGruppi(identificativoOggetto);
  193.                 for (Object object : identificativi) {
  194.                     IDGruppo idGruppo = (IDGruppo)object;
  195.                     risultatiRicerca.add(gruppiCore.getDettagliGruppoInUso(idGruppo));
  196.                 }
  197.                 break;
  198.             case TOKEN_POLICY:
  199.                 identificativi = exporterUtils.getIdsTokenPolicy(identificativoOggetto);
  200.                 for (Object object : identificativi) {
  201.                     IDGenericProperties idGP = (IDGenericProperties)object;
  202.                     risultatiRicerca.add(confCore.getDettagliTokenPolicyInUso(idGP));
  203.                 }
  204.                 break;
  205.             case ATTRIBUTE_AUTHORITY:
  206.                 identificativi = exporterUtils.getIdsAttributeAuthority(identificativoOggetto);
  207.                 for (Object object : identificativi) {
  208.                     IDGenericProperties idGP = (IDGenericProperties)object;
  209.                     risultatiRicerca.add(confCore.getDettagliTokenPolicyInUso(idGP));
  210.                 }
  211.                 break;
  212.             case RATE_LIMITING_POLICY:
  213.                 identificativi = exporterUtils.getIdsControlloTrafficoConfigPolicy(identificativoOggetto);
  214.                 for (Object object : identificativi) {
  215.                     IdPolicy idRP = (IdPolicy)object;
  216.                     risultatiRicerca.add(confCore.getDettagliRateLimitingPolicyInUso(idRP));
  217.                 }
  218.                 break;
  219.             case PLUGIN_CLASSE:
  220.                 identificativi = exporterUtils.getIdsPluginClassi(identificativoOggetto);
  221.                 for (Object object : identificativi) {
  222.                     IdPlugin idPlugin = (IdPlugin)object;
  223.                     risultatiRicerca.add(confCore.getDettagliPluginClasseInUso(idPlugin));
  224.                 }
  225.                 break;
  226.             case EROGAZIONE_INFO:{
  227.                 String uriAPSerogata = identificativoOggetto;
  228.                 IDServizio idServizio = IDServizioFactory.getInstance().getIDServizioFromUri(uriAPSerogata);
  229.                 risultatiRicerca.add(ErogazioniDetailsUtilities.getDetailsErogazione(idServizio, soggettiCore, registroHelper));
  230.                 break;
  231.             }
  232.             case FRUIZIONE_INFO:{
  233.                 String uriAPSfruita = identificativoOggetto;
  234.                 if(uriAPSfruita.contains("@")) {
  235.                     String tipoNomeFruitore = uriAPSfruita.split("@")[1];
  236.                     uriAPSfruita = uriAPSfruita.split("@")[0];
  237.                     IDSoggetto idSoggettoFruitore = new IDSoggetto(tipoNomeFruitore.split("/")[0], tipoNomeFruitore.split("/")[1]);
  238.                     IDServizio idServizio = IDServizioFactory.getInstance().getIDServizioFromUri(uriAPSfruita);
  239.                     risultatiRicerca.add(ErogazioniDetailsUtilities.getDetailsFruizione(idServizio, idSoggettoFruitore, soggettiCore, registroHelper));
  240.                 }
  241.                 else {
  242.                     risultatiRicerca.add("Internal Error: informazione fruitore non presente");
  243.                 }
  244.                 break;
  245.             }
  246.             case ACCORDO_COOPERAZIONE:
  247.             case ACCORDO_SERVIZIO_COMPOSTO:
  248.             case ACCORDO_SERVIZIO_PARTE_SPECIFICA:
  249.             case EROGAZIONE:
  250.             case FRUITORE:
  251.             case FRUIZIONE:
  252.             case PDD:
  253.             case PORTA_APPLICATIVA:
  254.             case PORTA_DELEGATA:
  255.                 throw new CoreException("TipoOggetto non gestito.");
  256.             }

  257.             ServletOutputStream outputStream = response.getOutputStream();
  258.            
  259.             if(tipoRisposta.equalsIgnoreCase(UtilsCostanti.VALUE_PARAMETRO_INFORMAZIONI_UTILIZZO_OGGETTO_TIPO_RISPOSTA_JSON)) {
  260.                 response.setContentType(HttpConstants.CONTENT_TYPE_JSON);
  261.                 JSONUtils jsonUtils = JSONUtils.getInstance(true);
  262.                 Map<String, Object> mapResult = new HashMap<>();
  263.                 mapResult.put(UtilsCostanti.KEY_JSON_RISPOSTA_USO, risultatiRicerca);
  264.                 jsonUtils.writeTo(mapResult, outputStream);
  265.             } else if(tipoRisposta.equalsIgnoreCase(UtilsCostanti.VALUE_PARAMETRO_INFORMAZIONI_UTILIZZO_OGGETTO_TIPO_RISPOSTA_TEXT)) {
  266.                 response.setContentType(HttpConstants.CONTENT_TYPE_PLAIN);
  267.                 outputStream.write(StringUtils.join(risultatiRicerca.toArray(new String[1])).getBytes());
  268.             } else {
  269.                 throw new CoreException("TipoRiposta non gestito.");
  270.             }
  271.            

  272.         }catch(Exception e){
  273.             ControlStationCore.logError("Errore durante la ricerca delle informazioni oggetto: "+e.getMessage(), e);
  274.         }
  275.     }
  276. }