Utilities.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.Date;
  23. import java.util.List;

  24. import org.openspcoop2.utils.UtilsException;

  25. import net.sf.ezmorph.MorpherRegistry;
  26. import net.sf.ezmorph.bean.BeanMorpher;
  27. import net.sf.json.util.EnumMorpher;

  28. /**
  29.  * Contiene delle utilities per questo package
  30.  *
  31.  * @author Poli Andrea (apoli@link.it)
  32.  * @author $Author$
  33.  * @version $Rev$, $Date$
  34.  */
  35. public class Utilities {

  36.     public static void normalizeDateObjects(Object o) throws IOException{
  37.         try{
  38.             if(o!=null){
  39.                 //System.out.println("Inizio analisi per  ["+o.getClass().getName()+"]...");
  40.                 java.lang.reflect.Method[] methods = o.getClass().getMethods();
  41.                 for(int i=0; i<methods.length;i++){
  42.                     String nomeMetodo = methods[i].getName();
  43.                     //System.out.println("Esamino ["+nomeMetodo+"]...");
  44.                     if(nomeMetodo.length()>3 &&
  45.                             nomeMetodo.startsWith("get") &&
  46.                             methods[i].getParameterTypes()!=null && methods[i].getParameterTypes().length==0 &&
  47.                             methods[i].getReturnType()!=null
  48.                             //&& !("void".equals(methods[i].getReturnType().getName()))
  49.                             ){
  50.                        
  51.                         String voidS = "void";
  52.                         String returnType = methods[i].getReturnType().getName()+"";
  53.                         boolean isVoid = returnType.equals(voidS);
  54.                        
  55.                         if(!isVoid) {
  56.                             Class<?> tipoReturn = methods[i].getReturnType();
  57.                             //System.out.println("ANALIZZO TIPO RETURN ["+tipoReturn.getName()+"]...");
  58.                                                    
  59.                             if(tipoReturn.isAssignableFrom(Date.class)){
  60.                                                                        
  61.                                 //System.out.println("DATA creo oggetto time ["+methods[i].getName()+"]? check tipo per evitare object generici che sono assegnabili a Date");
  62.                                 Object oDate = methods[i].invoke(o);
  63.                                 if(oDate!=null){
  64.                                     if(oDate instanceof Date){
  65.                                         Date tmp = (Date) oDate;
  66.                                         String nomeMetodoSet = "s"+nomeMetodo.subSequence(1, nomeMetodo.length());
  67.                                         //System.out.println("DATA ("+o.getClass().getName()+") set con metodo ["+nomeMetodoSet+"]");
  68.                                         java.lang.reflect.Method methodSet =  o.getClass().getMethod(nomeMetodoSet,Date.class);
  69.                                         methodSet.invoke(o,new Date(tmp.getTime()));
  70.                                         //System.out.println("DATA set effettuato");
  71.                                     }
  72.                                 }
  73.                             }
  74.                             else if(o.getClass().getPackage()!=null && tipoReturn.getName().startsWith(o.getClass().getPackage().getName())){
  75.                                 if(tipoReturn.isEnum()==false){
  76.                                     //System.out.println("Normalize per ["+o.getClass().getPackage().getName()+"]...");
  77.                                     Utilities.normalizeDateObjects(methods[i].invoke(o));
  78.                                     //System.out.println("Normalize per ["+o.getClass().getPackage().getName()+"] fatto");
  79.                                 }/*else{
  80.                                     System.out.println("ENUM!");
  81.                                 }*/
  82.                             }
  83.                         }
  84.                     }
  85.                     else if(nomeMetodo.length()>3 &&
  86.                             nomeMetodo.startsWith("size") &&  nomeMetodo.endsWith("List") &&  
  87.                             methods[i].getParameterTypes()!=null && methods[i].getParameterTypes().length==0 &&
  88.                             methods[i].getReturnType()!=null
  89.                             //&& "int".equals(methods[i].getReturnType().getName())
  90.                             ){
  91.                         String intS = "int";
  92.                         String returnType = methods[i].getReturnType().getName()+"";
  93.                         boolean isInt = returnType.equals(intS);
  94.                        
  95.                         if(isInt) {
  96.                             Object oLista = methods[i].invoke(o);
  97.                             //System.out.println("LISTA["+oLista.getClass().getName()+"]");
  98.                             int sizeLista = (Integer) oLista;
  99.                             for(int j=0; j<sizeLista; j++){
  100.                                 String nomeMetodoGet = "get"+nomeMetodo.subSequence("size".length(), (nomeMetodo.length()-"List".length()));
  101.                                 //System.out.println("NOME METODO["+nomeMetodoGet+"]");
  102.                                 java.lang.reflect.Method methodGet =  null;
  103.                                 try{
  104.                                     methodGet = o.getClass().getMethod(nomeMetodoGet,int.class);
  105.                                 }catch(Exception e){
  106.                                     //System.out.println("NON ESISTE  IL METODO: "+e.getMessage());
  107.                                 }
  108.                                 if(methodGet!=null){
  109.                                     //System.out.println("ANALIZZO TIPO RETURN METODO GET ["+methodGet.getReturnType().getName()+"]...");
  110.                                     Class<?> tipoReturn = methodGet.getReturnType();
  111.                                     if(tipoReturn.getName().startsWith(o.getClass().getPackage().getName())){
  112.                                         //System.out.println("RINORMALIZZO IN RICORSIONE..... ");
  113.                                         try{
  114.                                             Utilities.normalizeDateObjects(methodGet.invoke(o,j));
  115.                                         }catch(Exception e){
  116.                                             //System.out.println("RINORMALIZZAZIONE NON RIUSCITA: "+e.getMessage());
  117.                                         }  
  118.                                         //System.out.println("RINORMALIZZO IN RICORSIONE FINE -------------------- ");
  119.                                     }
  120.                                 }/*else{
  121.                                     System.out.println("NON ESISTE  IL METODO");
  122.                                 }*/
  123.                             }
  124.                         }
  125.                     }
  126.                 }
  127.                 //System.out.println("Fine analisi per  ["+o.getClass().getName()+"].");
  128.             }
  129.         }catch(Exception e){
  130.             throw new IOException("Normalizzazione date non riuscita: "+e.getMessage(),e);
  131.         }
  132.     }
  133.    
  134.    
  135.    
  136.    
  137.    
  138.    
  139.    
  140.     public static void registerMorpher(Class<?> oClass,MorpherRegistry morpherRegistry, List<String> classRegistered,List<String> morpherPackage) throws UtilsException{
  141.         try{
  142.             if(oClass!=null){
  143.                 java.lang.reflect.Method[] methods = oClass.getMethods();
  144.                 for(int i=0; i<methods.length;i++){
  145.                     String nomeMetodo = methods[i].getName();
  146.                
  147.                     if(nomeMetodo.length()>3 &&
  148.                             nomeMetodo.startsWith("get") &&
  149.                             methods[i].getParameterTypes()!=null && methods[i].getParameterTypes().length==0 &&
  150.                             methods[i].getReturnType()!=null
  151.                             //&& !("void".equals(methods[i].getReturnType().getName()))
  152.                             ){
  153.                        
  154.                         String voidS = "void";
  155.                         String returnType = methods[i].getReturnType().getName()+"";
  156.                         boolean isVoid = returnType.equals(voidS);
  157.                        
  158.                         if(!isVoid) {
  159.                        
  160.                             Class<?> tipoReturn = methods[i].getReturnType();
  161.                             //System.out.println("AAAAAA ["+nomeMetodo+"] ["+tipoReturn+"] primitive["+tipoReturn.isPrimitive()+"] enum["+tipoReturn.isEnum()+"] ["+isMorpherPackage(tipoReturn)+"]");
  162.                            
  163.                             if(isMorpherPackage(tipoReturn,morpherPackage)){
  164.                                 if(tipoReturn.isEnum()){
  165.                                     if(classRegistered.contains(tipoReturn.getName())==false){
  166.                                         classRegistered.add(tipoReturn.getName());
  167.                                         //System.out.println("REGISTRATO ENUM ["+tipoReturn.getName()+"]");
  168.                                         morpherRegistry.registerMorpher( new EnumMorpher( tipoReturn ) );
  169.                                     }
  170.                                 }
  171.                                 else{
  172.                                     if(classRegistered.contains(tipoReturn.getName())==false){
  173.                                         classRegistered.add(tipoReturn.getName());
  174.                                         //System.out.println("REGISTRATO BEAN ["+tipoReturn.getName()+"]");
  175.                                         morpherRegistry.registerMorpher( new BeanMorpher( tipoReturn, morpherRegistry ) );
  176.                                     }
  177.                                     //System.out.println("RICORSIONE ["+tipoReturn.getName()+"]");
  178.                                     registerMorpher(tipoReturn, morpherRegistry, classRegistered,morpherPackage);
  179.                                 }
  180.                             }
  181.                            
  182.                             else if(tipoReturn.isAssignableFrom(java.util.List.class)){
  183.                                
  184.                                 // ParameterizedType stringListType = (ParameterizedType) stringListField.getGenericType();
  185.                                
  186.                                 Class<?> classGenericType = null;
  187.                                 try{
  188.                                     String genericType = methods[i].getGenericReturnType().toString().substring("java.util.List<".length(), methods[i].getGenericReturnType().toString().length()-1);
  189.                                     //System.out.println("IS LIST ["+tipoReturn.getName()+"] ["+genericType+"]");
  190.                                     classGenericType = Class.forName(genericType);
  191.                                 }catch(Exception e){
  192.                                     throw new UtilsException(e.getMessage(),e);
  193.                                 }
  194.                                 if(classGenericType!=null){
  195.                                    
  196.                                     if(isMorpherPackage(classGenericType,morpherPackage)){
  197.                                    
  198.                                         if(classRegistered.contains(classGenericType.getName())==false){
  199.                                             classRegistered.add(classGenericType.getName());
  200.                                             //System.out.println("REGISTRATO BEAN ["+classGenericType.getName()+"]");
  201.                                             morpherRegistry.registerMorpher( new BeanMorpher( classGenericType, morpherRegistry) );
  202.                                         }
  203.                                        
  204.                                         //System.out.println("RICORSIONE-LIST ["+classGenericType.getName()+"]");
  205.                                         registerMorpher(classGenericType, morpherRegistry, classRegistered,morpherPackage);
  206.                                        
  207.                                     }
  208.                                 }
  209.                             }
  210.                         }
  211.                     }
  212.                    
  213.                 }
  214.             }
  215.         }catch(Exception e){
  216.             throw new UtilsException("registerMorpher failed: "+e.getMessage(),e);
  217.         }
  218.     }
  219.    
  220.    
  221.     public static void morpher(Object o,MorpherRegistry morpherRegistry,List<String> morpherPackage, boolean throwExceptionMorpherFailed) throws UtilsException{
  222.        
  223.         try{
  224.             if(o!=null){
  225.                 //System.out.println("Inizio analisi per  ["+o.getClass().getName()+"]...");
  226.                 java.lang.reflect.Method[] methods = o.getClass().getMethods();
  227.                
  228.                 // Prima registro i tipi
  229.                 for(int i=0; i<methods.length;i++){
  230.                     String nomeMetodo = methods[i].getName();
  231.                
  232.                     if(nomeMetodo.length()>3 &&
  233.                             nomeMetodo.startsWith("get") &&
  234.                             methods[i].getParameterTypes()!=null && methods[i].getParameterTypes().length==0 &&
  235.                             methods[i].getReturnType()!=null
  236.                             //&&  !("void".equals(methods[i].getReturnType().getName()))
  237.                             ){
  238.                        
  239.                         String voidS = "void";
  240.                         String returnType = methods[i].getReturnType().getName()+"";
  241.                         boolean isVoid = returnType.equals(voidS);
  242.                        
  243.                         if(!isVoid) {
  244.                             Class<?> tipoReturn = methods[i].getReturnType();
  245.                             //System.out.println("AAAAAA ["+nomeMetodo+"] ["+tipoReturn+"] primitive["+tipoReturn.isPrimitive()+"] enum["+tipoReturn.isEnum()+"] ["+isMorpherPackage(tipoReturn)+"]");
  246.                            
  247.                             if(isMorpherPackage(tipoReturn,morpherPackage)){
  248.                                 Object oInternal = methods[i].invoke(o);
  249.                                 if(oInternal!=null){
  250.                                     if(oInternal instanceof net.sf.ezmorph.bean.MorphDynaBean){
  251.                                         Object oDeserialized = null;
  252.                                         try{
  253.                                             net.sf.ezmorph.bean.MorphDynaBean mdb = (net.sf.ezmorph.bean.MorphDynaBean)oInternal;
  254.                                             normalizeByteArray(tipoReturn, mdb);
  255.                                             oDeserialized = morpherRegistry.morph( tipoReturn, mdb );
  256.                                         }catch(Exception e){
  257.                                             if(throwExceptionMorpherFailed){
  258.                                                 throw e;
  259.                                             }
  260.                                         }
  261.                                        
  262.                                         if(oDeserialized!=null){
  263.                                             // cerco metodo Set
  264.                                             java.lang.reflect.Method setMethodRef = null;
  265.                                             for(int k=0; k<methods.length;k++){
  266.                                                 String setMethod = "set"+nomeMetodo.substring("get".length(), nomeMetodo.length());
  267.                                                 if(methods[k].getName().equals(setMethod) &&
  268.                                                         methods[k].getParameterTypes()!=null && methods[k].getParameterTypes().length==1){
  269.                                                     setMethodRef = methods[k];
  270.                                                     break;
  271.                                                 }
  272.                                             }
  273.                                             if(setMethodRef!=null) {
  274.                                                 setMethodRef.invoke(o, oDeserialized);
  275.                                             }
  276.                                            
  277.                                             //System.out.println("RICORSIONE MorphDynaBean ["+oDeserialized.getClass().getName()+"]");
  278.                                             morpher(oDeserialized, morpherRegistry,morpherPackage,throwExceptionMorpherFailed);
  279.                                         }
  280.                                     }
  281.                                     else{
  282.                                         //System.out.println("RICORSIONE ["+oInternal.getClass().getName()+"]");
  283.                                         morpher(oInternal, morpherRegistry,morpherPackage,throwExceptionMorpherFailed);
  284.                                     }
  285.                                 }
  286.                             }
  287.                            
  288.                             else if(tipoReturn.isAssignableFrom(java.util.List.class)){
  289.                                
  290.                                 // ParameterizedType stringListType = (ParameterizedType) stringListField.getGenericType();
  291.                                
  292.                                 Class<?> classGenericType = null;
  293.                                 try{
  294.                                     //System.out.println("AAAAA ["+nomeMetodo+"] ["+tipoReturn+"] ["+methods[i].getGenericReturnType().toString()+"]");
  295.                                     String m = methods[i].getGenericReturnType().toString();
  296.                                     if(m.startsWith("java.util.List<")){
  297.                                         String genericType = m.substring("java.util.List<".length(), methods[i].getGenericReturnType().toString().length()-1);
  298.                                         //System.out.println("IS LIST ["+tipoReturn.getName()+"] ["+genericType+"]");
  299.                                         classGenericType = Class.forName(genericType);
  300.                                     }
  301.                                 }catch(Exception e){
  302.                                     throw new UtilsException(e.getMessage(),e);
  303.                                 }
  304.                                 if(classGenericType!=null){
  305.                                    
  306.                                     if(isMorpherPackage(classGenericType,morpherPackage)){
  307.                                    
  308.                                         Object oInternal = methods[i].invoke(o);
  309.                                         if(oInternal!=null && (oInternal instanceof java.util.List)){
  310.                                             java.util.List<?> l = (java.util.List<?>) oInternal;
  311.                                             if(l.size()>0){
  312.                                                
  313.                                                 // cerco metodo Set
  314.                                                 java.lang.reflect.Method setMethodList = null;
  315.                                                 for(int k=0; k<methods.length;k++){
  316.                                                     String setMethod = "set"+nomeMetodo.substring("get".length(), nomeMetodo.length());
  317.                                                     if(methods[k].getName().equals(setMethod) &&
  318.                                                             methods[k].getParameterTypes()!=null && methods[k].getParameterTypes().length==1){
  319.                                                         setMethodList = methods[k];
  320.                                                         break;
  321.                                                     }
  322.                                                 }
  323.                                                
  324.                                                 if(setMethodList!=null){
  325.                                                     List<Object> newList = new ArrayList<>();
  326.                                                    
  327.                                                     for (Object oInternalList : l) {
  328.                                                         if(oInternalList!=null){
  329.                    
  330.                                                             if(oInternalList instanceof net.sf.ezmorph.bean.MorphDynaBean){
  331.                                                                 Object oDeserialized = null;
  332.                                                                 try{
  333.                                                                     net.sf.ezmorph.bean.MorphDynaBean mdb = (net.sf.ezmorph.bean.MorphDynaBean)oInternalList;
  334.                                                                     normalizeByteArray(classGenericType, mdb);
  335.                                                                     oDeserialized = morpherRegistry.morph( classGenericType, mdb );
  336.                                                                 }catch(Exception e){
  337.                                                                     if(throwExceptionMorpherFailed){
  338.                                                                         throw e;
  339.                                                                     }
  340.                                                                 }
  341.                                                                
  342.                                                                 if(oDeserialized!=null){
  343.                                                                    
  344.                                                                     newList.add(oDeserialized);
  345.                                                                    
  346.                                                                     //System.out.println("RICORSIONE-LIST MorphDynaBean ["+oDeserialized.getClass().getName()+"]");
  347.                                                                     morpher(oDeserialized, morpherRegistry,morpherPackage,throwExceptionMorpherFailed);
  348.                                                                 }
  349.                                                             }
  350.                                                             else{
  351.                                                                 //System.out.println("RICORSIONE-LIST ["+oInternalList.getClass().getName()+"]");
  352.                                                                 morpher(oInternalList, morpherRegistry,morpherPackage,throwExceptionMorpherFailed);
  353.                                                                
  354.                                                                 newList.add(oInternalList);
  355.                                                                
  356.                                                             }
  357.                    
  358.                                                         }
  359.                                                     }
  360.                                                    
  361.                                                     setMethodList.invoke(o, newList);
  362.                                                 }
  363.                                                
  364.                                             }
  365.                                         }
  366.                                     }
  367.                                 }
  368.                             }
  369.                         }
  370.                     }
  371.                 }
  372.                
  373.             }
  374.         }catch(Exception e){
  375.             throw new UtilsException("morpher failed: "+e.getMessage(),e);
  376.         }
  377.     }
  378.    
  379.    
  380.     private static boolean isMorpherPackage(Class<?> c,List<String> morpherPackage){
  381.         if(morpherPackage!=null && morpherPackage.size()>0){
  382.             if(c.getPackage()!=null){
  383.                 for (int i = 0; i < morpherPackage.size(); i++) {
  384.                     String p = morpherPackage.get(i);
  385.                     if(c.getPackage().getName().equals(p) ||
  386.                             c.getPackage().getName().startsWith(p+".")){
  387.                         return true;
  388.                     }
  389.                 }
  390.             }
  391.         }
  392.         return false;
  393.     }
  394.    
  395.     private static void normalizeByteArray(Class<?> classGenericType, net.sf.ezmorph.bean.MorphDynaBean mdb){
  396.         java.lang.reflect.Method[] methodsMDB = classGenericType.getMethods();
  397.         for(int j=0; j<methodsMDB.length;j++){
  398.             String nomeMetodoMDB = methodsMDB[j].getName();
  399.             if(nomeMetodoMDB.length()>3 &&
  400.                     nomeMetodoMDB.startsWith("get") &&
  401.                     methodsMDB[j].getParameterTypes()!=null && methodsMDB[j].getParameterTypes().length==0 &&
  402.                             methodsMDB[j].getReturnType()!=null
  403.                             //&& !("void".equals(methodsMDB[j].getReturnType().getName()))
  404.                             ){
  405.                 String voidS = "void";
  406.                 String returnType = methodsMDB[j].getReturnType().getName()+"";
  407.                 boolean isVoid = returnType.equals(voidS);
  408.                
  409.                 if(!isVoid) {
  410.                     Class<?> tipoReturnMDB = methodsMDB[j].getReturnType();
  411.                     String variableName = (nomeMetodoMDB.charAt(3)+"").toLowerCase() + nomeMetodoMDB.substring(4);
  412.                     if(tipoReturnMDB.isAssignableFrom(byte[].class)){
  413.                         try{
  414.                             Object oF = mdb.get(variableName);
  415.                             if(oF!=null){
  416.                                 if(oF.getClass().isAssignableFrom(java.util.ArrayList.class)){
  417.                                     java.util.List<?> lf = (java.util.List<?>) oF;
  418.                                     mdb.set(variableName, lf.toArray());
  419.                                 }
  420.                             }
  421.                         }catch(Exception e){
  422.                             //System.out.println("not exists: "+e.getMessage());
  423.                         }
  424.                     }
  425.                 }
  426.             }
  427.         }
  428.     }
  429. }