JacksonSimpleBeanPropertyFilter.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. import java.util.ArrayList;
  22. import java.util.HashSet;
  23. import java.util.List;
  24. import java.util.Set;

  25. import com.fasterxml.jackson.core.JsonGenerator;
  26. import com.fasterxml.jackson.databind.SerializerProvider;
  27. import com.fasterxml.jackson.databind.ser.PropertyWriter;
  28. import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter.SerializeExceptFilter;

  29. /**
  30.  * @author Bussu Giovanni (bussu@link.it)
  31.  * @author  $Author$
  32.  * @version $Rev$, $Date$
  33.  *
  34.  */
  35. public class JacksonSimpleBeanPropertyFilter extends SerializeExceptFilter {

  36.    
  37.     private static final long serialVersionUID = 1L;
  38.     private PropertyFilterCore core;
  39.     private List<String> excludes;
  40.     private List<Class<?>> excludesByClass;
  41.    
  42.     public JacksonSimpleBeanPropertyFilter(SerializationConfig config,ISerializer serializer){
  43.         super(getHashSet(config.getFilter(), config.getExcludes()));
  44.         this.core = new PropertyFilterCore(config.getFilter(), config.getIdBuilder(), serializer);
  45.         this.excludes = config.getExcludes();
  46.         if(config.getFilter()!=null && config.getFilter().getFilterByValue()!=null) {
  47.             this.excludesByClass = config.getFilter().getFilterByValue();
  48.         } else {
  49.             this.excludesByClass = new ArrayList<>();
  50.         }
  51.     }

  52.     private static Set<String> getHashSet(Filter filter, List<String> excludes) {
  53.         Set<String> set = new HashSet<String>();
  54.         if(filter!= null && filter.sizeFiltersByName()>0)
  55.             set.addAll(filter.getFilterByName());
  56.         if(excludes !=null && excludes.size() > 0) {
  57.             set.addAll(excludes);
  58.         }

  59.         return set;
  60.     }

  61.     @Override
  62.     public void serializeAsField(Object pojo, JsonGenerator gen, SerializerProvider provider, PropertyWriter writer)
  63.             throws Exception {
  64.        
  65.         if (include(writer) && !this.excludesByClass.contains(writer.getType().getRawClass())) {
  66.             writer.serializeAsField(pojo, gen, provider);
  67.         } else {
  68.             if (gen.canOmitFields()) { // since 2.3
  69.                 writer.serializeAsOmittedField(pojo, gen, provider);
  70.             }
  71.             if(this.excludes == null || !this.excludes.contains(writer.getName()))
  72.                 this.core.applicaFiltro(pojo, writer.getName(), pojo, writer.getType().getRawClass());
  73.         }
  74.     }

  75. }