PropertyFilter.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.utils.serialization;

  21. /**
  22.  * Contiene classe per effettuare un filtro JSON
  23.  *
  24.  * @author Poli Andrea (apoli@link.it)
  25.  * @author $Author$
  26.  * @version $Rev$, $Date$
  27.  */

  28. public class PropertyFilter implements net.sf.json.util.PropertyFilter{

  29.    
  30.     private PropertyFilterCore core;
  31.    
  32.     public PropertyFilter(Filter filter,IDBuilder idBuilder,ISerializer serializer){
  33.         this.core = new PropertyFilterCore(filter, idBuilder, serializer);
  34.     }
  35.     public PropertyFilter(Filter filter,ISerializer serializer){
  36.         this.core = new PropertyFilterCore(filter, serializer);
  37.     }
  38.    
  39.     @Override
  40.     public boolean apply( Object source, String name, Object value ) {  
  41.        
  42.         if(this.core.getFilter()!=null) {
  43.             // Filtri per valore
  44.             if(value!=null){
  45.                 for(int i=0; i<this.core.getFilter().sizeFiltersByValue(); i++){
  46.                     Class<?> classFilter = this.core.getFilter().getFilterByValue(i);
  47.                     if(value.getClass().isAssignableFrom(classFilter)){
  48.                         this.core.applicaFiltro(source, name, value, classFilter);  
  49.                         return true;
  50.                     }
  51.                 }
  52.             }
  53.            
  54.            
  55.             // Filtri per nome field
  56.             String nomeField = source.getClass().getName()+"."+name;
  57.             for(int i=0; i<this.core.getFilter().sizeFiltersByName(); i++){
  58.                 String filterName = this.core.getFilter().getFilterByName(i);
  59.                 if(nomeField.equals(filterName)){          
  60.                     if(value!=null){
  61.                         this.core.applicaFiltro(source, name, value, value.getClass());
  62.                         return true;
  63.                     }
  64.                 }
  65.             }
  66.         }
  67.        
  68.         return (value == null); // questa riga e' l'implementazione del NullPropertyFilter
  69.     }  
  70. }