VerificaCertificati.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.List;

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

  31. import org.apache.commons.io.IOUtils;
  32. import org.openspcoop2.core.commons.CoreException;
  33. import org.openspcoop2.core.id.IDServizio;
  34. import org.openspcoop2.core.id.IDSoggetto;
  35. import org.openspcoop2.core.registry.AccordoServizioParteSpecifica;
  36. import org.openspcoop2.core.registry.Fruitore;
  37. import org.openspcoop2.core.registry.driver.IDServizioFactory;
  38. import org.openspcoop2.utils.transport.http.HttpConstants;
  39. import org.openspcoop2.utils.transport.http.HttpRequestMethod;
  40. import org.openspcoop2.web.ctrlstat.core.ControlStationCore;
  41. import org.openspcoop2.web.ctrlstat.costanti.CostantiControlStation;
  42. import org.openspcoop2.web.ctrlstat.costanti.InUsoType;
  43. import org.openspcoop2.web.ctrlstat.servlet.aps.AccordiServizioParteSpecificaCore;
  44. import org.openspcoop2.web.ctrlstat.servlet.aps.AccordiServizioParteSpecificaCostanti;
  45. import org.openspcoop2.web.ctrlstat.servlet.aps.erogazioni.ErogazioniHelper;
  46. import org.openspcoop2.web.ctrlstat.servlet.config.ConfigurazioneCostanti;
  47. import org.openspcoop2.web.ctrlstat.servlet.soggetti.SoggettiCore;
  48. import org.openspcoop2.web.lib.mvc.DataElement;
  49. import org.openspcoop2.web.lib.mvc.MessageType;
  50. import org.openspcoop2.web.lib.mvc.PageData;
  51. import org.openspcoop2.web.lib.mvc.ServletUtils;

  52. /**
  53.  * VerificaCertificati
  54.  *
  55.  * @author Andrea Poli (apoli@link.it)
  56.  * @author Giuliano Pintori (giuliano.pintori@link.it)
  57.  * @author $Author$
  58.  * @version $Rev$, $Date$
  59.  *
  60.  */
  61. public class VerificaCertificati extends HttpServlet {

  62.     private static final long serialVersionUID = 1L;

  63.     @Override
  64.     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  65.         this.processRequest(req, resp);
  66.     }

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

  69.         ByteArrayOutputStream baos = new ByteArrayOutputStream();
  70.         try {
  71.             IOUtils.copy(req.getInputStream(), baos);
  72.         }catch(Exception e){
  73.             ControlStationCore.logError("Errore durante la ricerca delle informazioni oggetto: "+e.getMessage(), e);
  74.             return;
  75.         }          

  76.         this.processRequest(req, resp);
  77.     }

  78.     private void processRequest(HttpServletRequest request, HttpServletResponse response) {
  79.         String risposta = "";
  80.         String messaggioEsito = null;
  81.         String messageType = null;
  82.         response.setContentType(HttpConstants.CONTENT_TYPE_JSON);


  83.         try(ByteArrayOutputStream baosPayload = new ByteArrayOutputStream();){
  84.             HttpRequestMethod httpRequestMethod = HttpRequestMethod.valueOf(request.getMethod().toUpperCase());

  85.             if(httpRequestMethod.equals(HttpRequestMethod.POST)) { // copia del payload
  86.                 IOUtils.copy(request.getInputStream(), baosPayload);
  87.             }

  88.             HttpSession session = request.getSession(true);
  89.             PageData pd = new PageData();
  90.             UtilsHelper registroHelper = new UtilsHelper(request, pd, session);

  91.             String tipoOggetto = registroHelper.getParameter(UtilsCostanti.PARAMETRO_VERIFICA_CERTIFICATI_TIPO_OGGETTO);

  92.             InUsoType inUsoType = InUsoType.valueOf(tipoOggetto);

  93.             switch (inUsoType) {
  94.             case EROGAZIONE:
  95.             case FRUIZIONE:
  96.                 ErogazioniHelper apsHelper = new ErogazioniHelper(request, pd, session);
  97.                
  98.                 AccordiServizioParteSpecificaCore apsCore = new AccordiServizioParteSpecificaCore();
  99.                 SoggettiCore soggettiCore = new SoggettiCore(apsCore);
  100.                
  101.                 String verificaCertificatiFromLista = apsHelper.getParameter(CostantiControlStation.PARAMETRO_VERIFICA_CERTIFICATI_FROM_LISTA);
  102.                 boolean arrivoDaLista = "true".equalsIgnoreCase(verificaCertificatiFromLista);
  103.                
  104.                 boolean soloModI = false;
  105.                 if(!arrivoDaLista) {
  106.                     String par = apsHelper.getParameter(AccordiServizioParteSpecificaCostanti.PARAMETRO_APS_MODIFICA_PROFILO);
  107.                     soloModI = "true".equalsIgnoreCase(par);
  108.                 }
  109.                            
  110.                 String id = apsHelper.getParameter(AccordiServizioParteSpecificaCostanti.PARAMETRO_APS_ID);
  111.                 long idInt  = Long.parseLong(id);
  112.                 AccordoServizioParteSpecifica asps = apsCore.getAccordoServizioParteSpecifica(idInt);
  113.                
  114.                 String tipoSoggettoFruitore = apsHelper.getParameter(AccordiServizioParteSpecificaCostanti.PARAMETRO_APS_TIPO_SOGGETTO_FRUITORE);
  115.                 String nomeSoggettoFruitore = apsHelper.getParameter(AccordiServizioParteSpecificaCostanti.PARAMETRO_APS_NOME_SOGGETTO_FRUITORE);
  116.                 IDSoggetto idSoggettoFruitore = null;
  117.                 if(tipoSoggettoFruitore!=null && !"".equals(tipoSoggettoFruitore) &&
  118.                         nomeSoggettoFruitore!=null && !"".equals(nomeSoggettoFruitore)) {
  119.                     idSoggettoFruitore = new IDSoggetto(tipoSoggettoFruitore, nomeSoggettoFruitore);
  120.                 }
  121.                
  122.                 String alias = apsHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_SISTEMA_NODO_CLUSTER);
  123.                
  124.                 String tipologia = ServletUtils.getObjectFromSession(request, session, String.class, AccordiServizioParteSpecificaCostanti.PARAMETRO_APS_TIPO_EROGAZIONE);
  125.                 boolean gestioneFruitori = false;
  126.                 boolean gestioneErogatori = false;
  127.                 if(tipologia!=null) {
  128.                     if(AccordiServizioParteSpecificaCostanti.PARAMETRO_APS_TIPO_EROGAZIONE_VALUE_FRUIZIONE.equals(tipologia)) {
  129.                         gestioneFruitori = true;
  130.                     }
  131.                     else if(AccordiServizioParteSpecificaCostanti.PARAMETRO_APS_TIPO_EROGAZIONE_VALUE_EROGAZIONE.equals(tipologia)) {
  132.                         gestioneErogatori = true;
  133.                     }
  134.                 }
  135.                
  136.                 Fruitore fruitore = null;
  137.                 if(gestioneFruitori) {
  138.                     // In questa modalità ci deve essere un fruitore indirizzato
  139.                     for (Fruitore check : asps.getFruitoreList()) {
  140.                         if(check.getTipo().equals(idSoggettoFruitore.getTipo()) && check.getNome().equals(idSoggettoFruitore.getNome())) {
  141.                             fruitore = check;
  142.                             break;
  143.                         }
  144.                     }
  145.                 }
  146.                
  147.                 IDServizio idServizio = IDServizioFactory.getInstance().getIDServizioFromAccordo(asps);
  148.                 String tipoProtocollo = soggettiCore.getProtocolloAssociatoTipoSoggetto(asps.getTipoSoggettoErogatore());
  149.                 List<DataElement> dati = new ArrayList<>();
  150.                
  151.                 // Esecuzione delle operazioni di verifica
  152.                 boolean verificaCertificatiEffettuata = apsHelper.eseguiVerificaCertificati(soloModI, asps, idSoggettoFruitore, alias, gestioneFruitori,
  153.                         gestioneErogatori, fruitore, idServizio, tipoProtocollo, dati);
  154.                
  155.                 if(verificaCertificatiEffettuata) {
  156.                     ControlStationCore.logInfo("Verifica dei certificati completata.");
  157.                     messaggioEsito = pd.getMessage();
  158.                     messageType = pd.getMessageType();
  159.                 } else {
  160.                     ControlStationCore.logInfo("Verifica dei certificati non completata.");
  161.                     messaggioEsito = UtilsCostanti.MESSAGGIO_INFORMATIVO_VERIFICA_CERTIFICATI_NON_ESEGUITA;
  162.                     messageType = MessageType.INFO.toString();
  163.                 }
  164.                
  165.                 break;
  166.             default:
  167.                 throw new CoreException("TipoOggetto non gestito.");
  168.             }

  169.         }catch(Exception e){
  170.             ControlStationCore.logError("Errore durante la decodifica: "+e.getMessage(), e);
  171.             messaggioEsito = UtilsCostanti.MESSAGGIO_ERRORE_VERIFICA_CERTIFICATI;
  172.             messageType = MessageType.ERROR.toString();
  173.         } finally {
  174.             risposta = ServletUtils.getJson(ServletUtils.getJsonPair(UtilsCostanti.KEY_ESITO, messageType), ServletUtils.getJsonPair(UtilsCostanti.KEY_DETTAGLIO_ESITO, messaggioEsito));
  175.             try {
  176.                 ServletOutputStream outputStream = response.getOutputStream();
  177.                 outputStream.write(risposta.getBytes());
  178.             }catch(Exception eErr){
  179.                 ControlStationCore.logError("Errore durante la serializzazione dell'errore di decodifica: "+eErr.getMessage(), eErr);
  180.             }
  181.         }
  182.    
  183.     }
  184. }