ValidationUtils.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.json.validation;

  21. import java.io.ByteArrayOutputStream;
  22. import java.util.HashMap;
  23. import java.util.Iterator;
  24. import java.util.List;
  25. import java.util.Map;

  26. import org.openspcoop2.utils.Utilities;
  27. import org.openspcoop2.utils.json.JSONUtils;
  28. import org.slf4j.Logger;

  29. import com.fasterxml.jackson.databind.JsonNode;
  30. import com.fasterxml.jackson.databind.ObjectMapper;
  31. import com.fasterxml.jackson.databind.node.ArrayNode;
  32. import com.fasterxml.jackson.databind.node.BooleanNode;
  33. import com.fasterxml.jackson.databind.node.ObjectNode;

  34. /**
  35.  * ValidationUtils
  36.  *
  37.  * @author Giovanni Bussu (bussu@link.it)
  38.  * @author $Author$
  39.  * @version $Rev$, $Date$
  40.  */
  41. public class ValidationUtils {


  42.     public static void logError(Logger log, String msg, byte[] rawObject, byte[] schemaBytes, JsonNode jsonSchema) {
  43.        
  44.         if(log==null) {
  45.             return;
  46.         }
  47.        
  48.         int max100KB = 1024*100;
  49.        
  50.         JSONUtils jsonUtils = JSONUtils.getInstance(true);
  51.        
  52.         StringBuilder sb = new StringBuilder();
  53.         sb.append("Validation error: ").append(msg);
  54.         if(rawObject!=null) {
  55.             sb.append("\nMessage: ");
  56.             if(rawObject.length<max100KB) {
  57.                 try {
  58.                     ByteArrayOutputStream bout = new ByteArrayOutputStream();
  59.                     jsonUtils.writeTo(jsonUtils.getAsNode(rawObject), bout);
  60.                     bout.flush();
  61.                     bout.close();
  62.                     sb.append("\n").append(bout.toString());
  63.                 }catch(Exception e) {
  64.                     sb.append("\n").append(new String(rawObject));
  65.                 }
  66.             }
  67.             else {
  68.                 sb.append("too large for debug ("+Utilities.convertBytesToFormatString(rawObject.length)+")");
  69.             }
  70.         }
  71.         if(schemaBytes!=null) {
  72.             sb.append("\nSchema: ");
  73.             if(schemaBytes.length<max100KB) {
  74.                 try {
  75.                     ByteArrayOutputStream bout = new ByteArrayOutputStream();
  76.                     if(jsonSchema!=null) {
  77.                         jsonUtils.writeTo(jsonSchema, bout);
  78.                     }
  79.                     else {
  80.                         jsonUtils.writeTo(jsonUtils.getAsNode(schemaBytes), bout);
  81.                     }
  82.                     bout.flush();
  83.                     bout.close();
  84.                     sb.append("\n").append(bout.toString());
  85.                 }catch(Exception e) {
  86.                     sb.append("\n").append(new String(schemaBytes));
  87.                 }
  88.             }
  89.             else {
  90.                 sb.append("too large for debug ("+Utilities.convertBytesToFormatString(schemaBytes.length)+")");
  91.             }
  92.         }
  93.         log.error(sb.toString());
  94.     }
  95.    
  96.     public static void disableAdditionalProperties(ObjectMapper mapper, JsonNode jsonSchemaObject, boolean booleanType, boolean force) {
  97.         JsonNode definitions = jsonSchemaObject.get("definitions");
  98.        
  99.         BooleanNode booleanFalseNode = null;
  100.         BooleanNode booleanTrueNode = null;
  101.         ObjectNode jsonStringTypeNode = null;
  102.         if(booleanType) {
  103.             booleanFalseNode = mapper.getNodeFactory().booleanNode(false);
  104.             booleanTrueNode = mapper.getNodeFactory().booleanNode(true);
  105.         }
  106.         else {
  107.             Map<String, String> map = new HashMap<>();
  108.             map.put("type", "string");
  109.             jsonStringTypeNode = mapper.getNodeFactory().objectNode();
  110.             jsonStringTypeNode.set("type", mapper.getNodeFactory().textNode("string"));
  111.         }
  112.        
  113.         /*
  114.          * ANDREBBE EFFETTUATA LA RISCRITTURA DEL JSON SCHEMA DOVE SONO PRESENTI I ALLOF RISOLVENDO I REF E GESTENDO NELL'OGGETTO L'ADDIOTIONAL PROPERTIES
  115.         Iterator<String> fieldNames = definitions.fieldNames();
  116.         HashMap<String, JsonNode> mapNode = new HashMap<>();
  117.         while (fieldNames.hasNext()) {
  118.            
  119.             String fieldName = (String) fieldNames.next();
  120.             JsonNode definition = definitions.get(fieldName);
  121.            
  122.            
  123.         }
  124.         */
  125.        
  126.         Iterator<JsonNode> iterator = definitions.iterator();
  127.         while(iterator.hasNext()) {
  128.             ObjectNode definition = (ObjectNode) iterator.next();
  129.                        
  130.             boolean allOf = definition.has("allOf");
  131.             //boolean oneOf = definition.has("oneOf");
  132.             boolean hasAdditionalProperties = definition.has("additionalProperties");
  133.            
  134.             //System.out.println("LOG ["+allOf+"]["+oneOf+"]["+hasAdditionalProperties+"] ["+definition.getClass().getName()+"]");
  135.            
  136.             //if(!allOf && !oneOf) {
  137.             if(force || !hasAdditionalProperties) {
  138.                 definition.set("additionalProperties", booleanType ? booleanFalseNode : jsonStringTypeNode);
  139.                
  140.                 if(allOf) {
  141.                     JsonNode allOfNode = definition.get("allOf");
  142.                     if(allOfNode!=null && allOfNode instanceof ArrayNode) {
  143.                        
  144.                         ArrayNode allOfArrayNode = (ArrayNode) allOfNode;
  145.                         for (int i = 0; i < allOfArrayNode.size(); i++) {
  146.                             JsonNode child = allOfArrayNode.get(i);
  147.                             //System.out.println("I["+i+"] ["+child.getClass().getName()+"]");
  148.                             if(child instanceof ObjectNode) {
  149.                                 ObjectNode oChild = (ObjectNode) child;
  150.                                 boolean ref = oChild.has("$ref");
  151.                                 if(!ref) {
  152.                                     boolean hasAdditionalPropertiesChild = oChild.has("additionalProperties");
  153.                                     if(force || !hasAdditionalPropertiesChild) {
  154.                                         oChild.set("additionalProperties", booleanTrueNode);
  155.                                     }
  156.                                 }
  157.                             }
  158.                         }
  159.                     }
  160.                 }
  161.             }
  162.             //}
  163.         }
  164.     }
  165.    
  166.     public static void addTypes(ObjectMapper mapper, JsonNode jsonSchemaObject, List<String> nomi, boolean all) {
  167.        
  168.         String allAny = all ? "allOf" : "anyOf";
  169.         ArrayNode array = mapper.getNodeFactory().arrayNode();
  170.         for(String nome: nomi) {
  171.             array.add(mapper.getNodeFactory().objectNode().put("$ref", nome));
  172.         }
  173.         ((ObjectNode)jsonSchemaObject).remove("allOf");
  174.         ((ObjectNode)jsonSchemaObject).remove("anyOf");
  175.         ((ObjectNode)jsonSchemaObject).set(allAny, array);
  176.     }
  177.    
  178. }