CacheManager.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 javax.servlet.ServletException;
  24. import javax.servlet.ServletOutputStream;
  25. import javax.servlet.http.HttpServlet;
  26. import javax.servlet.http.HttpServletRequest;
  27. import javax.servlet.http.HttpServletResponse;
  28. import javax.servlet.http.HttpSession;

  29. import org.apache.commons.io.IOUtils;
  30. import org.openspcoop2.core.commons.CoreException;
  31. import org.openspcoop2.utils.transport.http.HttpConstants;
  32. import org.openspcoop2.utils.transport.http.HttpRequestMethod;
  33. import org.openspcoop2.web.ctrlstat.core.ControlStationCore;
  34. import org.openspcoop2.web.ctrlstat.costanti.InUsoType;
  35. import org.openspcoop2.web.ctrlstat.servlet.aps.erogazioni.ErogazioniHelper;
  36. import org.openspcoop2.web.lib.mvc.MessageType;
  37. import org.openspcoop2.web.lib.mvc.PageData;
  38. import org.openspcoop2.web.lib.mvc.ServletUtils;

  39. /**
  40.  * CacheManager
  41.  *
  42.  * @author Andrea Poli (apoli@link.it)
  43.  * @author Giuliano Pintori (giuliano.pintori@link.it)
  44.  * @author $Author$
  45.  * @version $Rev$, $Date$
  46.  *
  47.  */
  48. public class CacheManager extends HttpServlet {

  49.     private static final long serialVersionUID = 1L;

  50.     @Override
  51.     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  52.         this.processRequest(req, resp);
  53.     }

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

  56.         ByteArrayOutputStream baos = new ByteArrayOutputStream();
  57.         try {
  58.             IOUtils.copy(req.getInputStream(), baos);
  59.         }catch(Exception e){
  60.             ControlStationCore.logError("Errore durante la ricerca delle informazioni oggetto: "+e.getMessage(), e);
  61.             return;
  62.         }          

  63.         this.processRequest(req, resp);
  64.     }

  65.     private void processRequest(HttpServletRequest request, HttpServletResponse response) {
  66.         String risposta = "";
  67.         String messaggioEsito = null;
  68.         String messageType = null;
  69.         response.setContentType(HttpConstants.CONTENT_TYPE_JSON);


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

  72.             if(httpRequestMethod.equals(HttpRequestMethod.POST)) { // copia del payload
  73.                 IOUtils.copy(request.getInputStream(), baosPayload);
  74.             }

  75.             HttpSession session = request.getSession(true);
  76.             PageData pd = new PageData();
  77.             UtilsHelper registroHelper = new UtilsHelper(request, pd, session);

  78.             String tipoOggetto = registroHelper.getParameter(UtilsCostanti.PARAMETRO_RESET_CACHE_TIPO_OGGETTO);

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

  80.             switch (inUsoType) {
  81.             case EROGAZIONE:
  82.             case FRUIZIONE:
  83.                 ErogazioniHelper erogazioniHelper = new ErogazioniHelper(request, pd, session);
  84.                 erogazioniHelper.eseguiResetCacheAspsOFruitore();

  85.                 messaggioEsito = pd.getMessage();
  86.                 messageType = pd.getMessageType();
  87.                 break;
  88.             default:
  89.                 throw new CoreException("TipoOggetto non gestito.");
  90.             }


  91.         }catch(Exception e){
  92.             ControlStationCore.logError("Errore durante la decodifica: "+e.getMessage(), e);
  93.             messaggioEsito = UtilsCostanti.MESSAGGIO_ERRORE_ELIMINAZIONE_ELEMENTO_CACHE;
  94.             messageType = MessageType.ERROR.toString();
  95.         } finally {
  96.             risposta = ServletUtils.getJson(ServletUtils.getJsonPair(UtilsCostanti.KEY_ESITO, messageType), ServletUtils.getJsonPair(UtilsCostanti.KEY_DETTAGLIO_ESITO, messaggioEsito));
  97.             try {
  98.                 ServletOutputStream outputStream = response.getOutputStream();
  99.                 outputStream.write(risposta.getBytes());
  100.             }catch(Exception eErr){
  101.                 ControlStationCore.logError("Errore durante la serializzazione dell'errore di decodifica: "+eErr.getMessage(), eErr);
  102.             }
  103.         }
  104.     }
  105. }