StatusApiServiceImpl.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.monitor.rs.server.api.impl;

  21. import java.sql.Connection;
  22. import java.util.ArrayList;
  23. import java.util.List;

  24. import org.openspcoop2.core.monitor.rs.server.api.StatusApi;
  25. import org.openspcoop2.core.monitor.rs.server.config.DBManager;
  26. import org.openspcoop2.core.monitor.rs.server.config.LoggerProperties;
  27. import org.openspcoop2.core.monitor.rs.server.config.ServerProperties;
  28. import org.openspcoop2.core.monitor.rs.server.model.Problem;
  29. import org.openspcoop2.generic_project.exception.NotFoundException;
  30. import org.openspcoop2.generic_project.utils.ServiceManagerProperties;
  31. import org.openspcoop2.utils.beans.BlackListElement;
  32. import org.openspcoop2.utils.service.BaseImpl;
  33. import org.openspcoop2.utils.service.authorization.AuthorizationConfig;
  34. import org.openspcoop2.utils.service.authorization.AuthorizationManager;
  35. import org.openspcoop2.utils.service.context.IContext;
  36. import org.openspcoop2.utils.service.fault.jaxrs.FaultCode;
  37. import org.openspcoop2.web.monitor.core.utils.BeanUtils;
  38. import org.openspcoop2.web.monitor.eventi.bean.EventoBean;
  39. import org.openspcoop2.web.monitor.eventi.dao.EventiService;

  40. /**
  41.  * StatusApiServiceImpl
  42.  *
  43.  * @author $Author$
  44.  * @version $Rev$, $Date$
  45.  */
  46. public class StatusApiServiceImpl extends BaseImpl implements StatusApi {

  47.     public StatusApiServiceImpl(){
  48.         super(org.slf4j.LoggerFactory.getLogger(StatusApiServiceImpl.class));
  49.     }

  50.     private AuthorizationConfig getAuthorizationConfig() throws Exception{
  51.         return new AuthorizationConfig(ServerProperties.getInstance().getProperties());
  52.     }

  53.     /**
  54.      * Ritorna lo stato dell&#x27;applicazione
  55.      *
  56.      * Ritorna lo stato dell&#x27;applicazione in formato problem+json
  57.      *
  58.      */
  59.     @Override
  60.     public Problem getStatus() {
  61.         IContext context = this.getContext();
  62.         try {
  63.             context.getLogger().info("Invocazione in corso ...");    

  64.             AuthorizationManager.authorize(context, getAuthorizationConfig());
  65.             context.getLogger().debug("Autorizzazione completata con successo");    
  66.                        
  67.             // Cerco un evento con id negativo, in modo che sicuro non esiste, almeno verifico la connessione
  68.             DBManager dbManager = DBManager.getInstance();
  69.             Connection connection = null;
  70.             Problem p = null;
  71.             try {
  72.                 connection = dbManager.getConnectionTracce();
  73.                 ServiceManagerProperties smp = dbManager.getServiceManagerPropertiesTracce();
  74.                 EventiService eventiService = new EventiService(connection, true, smp, LoggerProperties.getLoggerDAO());
  75.                
  76.                 Long id = 1l;
  77.                 boolean ok = false;
  78.                 try {
  79.                     EventoBean eventoBean = eventiService.findById(id, true);
  80.                     // o l'evento viene ritornato o viene lanciata una NotFoundException. Gli altri casi sono errore
  81.                     if(eventoBean!=null) {
  82.                         ok = true;
  83.                     }
  84.                 }catch(NotFoundException notFound) {
  85.                     ok = true;
  86.                 }
  87.                
  88.                 if(ok) {
  89.                     p = new Problem();
  90.                     org.openspcoop2.utils.service.fault.jaxrs.ProblemRFC7807 pUtils = FaultCode.STATUS_OK.toFault();
  91.                     List<BlackListElement> metodiEsclusi = new ArrayList<BlackListElement>(0);
  92.                     BeanUtils.copy(p, pUtils, metodiEsclusi);
  93.                 }
  94.                 else {
  95.                     throw new Exception("Servizio non disponibile");
  96.                 }
  97.             }
  98.             finally {
  99.                 dbManager.releaseConnectionTracce(connection);
  100.             }
  101.        
  102.             context.getLogger().info("Invocazione completata con successo");
  103.             return p;
  104.         }
  105.         catch(javax.ws.rs.WebApplicationException e) {
  106.             context.getLogger().error("Invocazione terminata con errore '4xx': %s",e, e.getMessage());
  107.             throw e;
  108.         }
  109.         catch(Throwable e) {
  110.             context.getLogger().error("Invocazione terminata con errore: %s",e, e.getMessage());
  111.             throw FaultCode.ERRORE_INTERNO.toException(e);
  112.         }
  113.     }
  114.    
  115. }