BasicValidator.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.monitor.engine.dynamic;

  21. import org.openspcoop2.monitor.sdk.condition.Context;
  22. import org.openspcoop2.monitor.sdk.exceptions.ValidationException;
  23. import org.openspcoop2.monitor.sdk.parameters.Parameter;
  24. import org.openspcoop2.monitor.sdk.plugins.ISearchArguments;

  25. import java.util.Iterator;
  26. import java.util.Map;

  27. import org.openspcoop2.utils.LoggerWrapperFactory;
  28. import org.slf4j.Logger;

  29. /**
  30.  * BasicValidator
  31.  *
  32.  * @author Poli Andrea (apoli@link.it)
  33.  * @author $Author$
  34.  * @version $Rev$, $Date$
  35.  */
  36. public class BasicValidator implements IDynamicValidator{
  37.    
  38.     private static Logger log = LoggerWrapperFactory.getLogger(BasicValidator.class);
  39.    
  40.     private String tipoPlugin;
  41.     private String tipo;
  42.     private String className;
  43.    
  44.     protected BasicValidator(String tipoPlugin, String tipo, String className) {
  45.         this.tipoPlugin = tipoPlugin;
  46.         this.tipo = tipo;
  47.         this.className = className;
  48.     }
  49.    
  50.     @Override
  51.     public void validate(Context context) throws ValidationException {
  52.        
  53.         try{
  54.            
  55.             IDynamicLoader bl = DynamicFactory.getInstance().newDynamicLoader(this.tipoPlugin, this.tipo, this.className, BasicValidator.log);
  56.            
  57.             Object obj = bl.newInstance();
  58.            
  59.             if(obj instanceof ISearchArguments){
  60.                 ((ISearchArguments) obj).validate(context);
  61.             }else{
  62.                 String iface = ISearchArguments.class.getName();
  63.                 throw new Exception("La classe ["+this.className+"] non implementa l'interfaccia ["+iface+"]");
  64.             }
  65.            
  66.             // Se non sono stati sollevati errori, verifico che i parametri dichiarati come required possiedano un valore.
  67.             // Il controllo è fatto appositamente dopo il validate sdk, in modo da permettere di personalizzare i controlli dentro il metodo validate
  68.             Map<String, Parameter<?>> mapParams = context.getParameters();
  69.             if(mapParams!=null && mapParams.size()>0){
  70.                 Iterator<String> keys = mapParams.keySet().iterator();
  71.                 while (keys.hasNext()) {
  72.                     String idParameter = (String) keys.next();
  73.                     Parameter<?> p = mapParams.get(idParameter);
  74.                     if(p.getRendering().isRequired()){
  75.                         Object o = p.getValue();
  76.                         if(o==null){
  77.                             throw new ValidationException("Non è stato fornito un valore per il parametro '"+idParameter+"'");
  78.                         }
  79.                     }
  80.                 }
  81.             }
  82.            
  83.         }catch(ClassNotFoundException cnfe){
  84.             BasicValidator.log.error("Impossibile caricare il plugin. La classe indicata ["+this.className+"] non esiste.",cnfe);
  85.             throw new ValidationException("Impossibile caricare il plugin. La classe indicata ["+this.className+"] non esiste.");
  86.         }catch (ValidationException ve) {
  87.             throw ve;
  88.         }catch (Exception e) {
  89.             BasicValidator.log.error(e.getMessage(),e);
  90.             throw new ValidationException(e.getMessage());
  91.         }
  92.     }
  93. }