JSONUtils.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;

  21. import java.io.Serializable;
  22. import java.util.HashMap;
  23. import java.util.List;
  24. import java.util.Map;
  25. import java.util.TimeZone;
  26. import java.util.regex.Matcher;
  27. import java.util.regex.Pattern;

  28. import org.openspcoop2.utils.SemaphoreLock;
  29. import org.slf4j.Logger;

  30. import com.fasterxml.jackson.annotation.JsonInclude.Include;
  31. import com.fasterxml.jackson.databind.ObjectMapper;
  32. import com.fasterxml.jackson.databind.ObjectWriter;
  33. import com.fasterxml.jackson.databind.SerializationFeature;
  34. import com.fasterxml.jackson.datatype.joda.JodaModule;
  35. import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;

  36. /**
  37.  * JSONUtils
  38.  *
  39.  * @author Poli Andrea (apoli@link.it)
  40.  * @author $Author$
  41.  * @version $Rev$, $Date$
  42.  */
  43. public class JSONUtils extends AbstractUtils {
  44.    
  45.     private static JSONUtils jsonUtils = null;
  46.     private static JSONUtils jsonUtilsPretty = null;
  47.     private static synchronized void init(boolean prettyPrint){
  48.         if(prettyPrint) {
  49.             if(JSONUtils.jsonUtilsPretty==null){
  50.                 JSONUtils.jsonUtilsPretty = new JSONUtils(true);
  51.             }
  52.         }
  53.         else {
  54.             if(JSONUtils.jsonUtils==null){
  55.                 JSONUtils.jsonUtils = new JSONUtils(false);
  56.             }
  57.         }
  58.     }
  59.     public static JSONUtils getInstance(){
  60.         return getInstance(false);
  61.     }
  62.     public static JSONUtils getInstance(boolean prettyPrint){
  63.         if(prettyPrint) {
  64.             if(JSONUtils.jsonUtilsPretty==null){
  65.                 // spotbugs warning 'SING_SINGLETON_GETTER_NOT_SYNCHRONIZED'
  66.                 synchronized (JSONUtils.class) {
  67.                     JSONUtils.init(true);
  68.                 }
  69.             }
  70.             return JSONUtils.jsonUtilsPretty;
  71.         }
  72.         else {
  73.             if(JSONUtils.jsonUtils==null){
  74.                 // spotbugs warning 'SING_SINGLETON_GETTER_NOT_SYNCHRONIZED'
  75.                 synchronized (JSONUtils.class) {
  76.                     JSONUtils.init(false);
  77.                 }
  78.             }
  79.             return JSONUtils.jsonUtils;
  80.         }
  81.     }
  82.    

  83.     private static org.openspcoop2.utils.Semaphore semaphore = new org.openspcoop2.utils.Semaphore("JSONUtils");
  84.     private static ObjectMapper internalMapper;
  85.     private static synchronized void initSyncMapper()  {
  86.         if(internalMapper==null){
  87.             internalMapper = new ObjectMapper();
  88.             internalMapper.setTimeZone(TimeZone.getDefault());
  89.             internalMapper.setSerializationInclusion(Include.NON_NULL);
  90.             internalMapper.enable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING);
  91.             internalMapper.configure(com.fasterxml.jackson.databind.SerializationFeature.
  92.                     WRITE_DATES_AS_TIMESTAMPS , false);
  93.             // Since 2.1.4, the field exampleSetFlag appears in json produced with ObjectMapper#writeValue(File, Object) when serializing an object of type OpenApi.
  94.             // "exampleSetFlag" : false
  95.             // Con il codice sottostante, nelle classi Mixin l'attributo 'getExampleSetFlag' viene ignorato
  96.             internalMapper.addMixIn(io.swagger.v3.oas.models.media.Schema.class, io.swagger.v3.core.jackson.mixin.SchemaMixin.class);
  97.             internalMapper.addMixIn(io.swagger.v3.oas.models.media.MediaType.class, io.swagger.v3.core.jackson.mixin.MediaTypeMixin.class);
  98.         }
  99.     }
  100.     private static void initMapper()  {
  101.         SemaphoreLock lock = semaphore.acquireThrowRuntime("initMapper");
  102.         try {
  103.             if(internalMapper==null){
  104.                 initSyncMapper();
  105.             }
  106.         }finally {
  107.             semaphore.release(lock, "initMapper");
  108.         }
  109.     }
  110.     public static void setMapperTimeZone(TimeZone timeZone) {
  111.         if(internalMapper==null){
  112.             initMapper();
  113.         }
  114.         SemaphoreLock lock = semaphore.acquireThrowRuntime("setMapperTimeZone");
  115.         try {
  116.             internalMapper.setTimeZone(timeZone);
  117.         }finally {
  118.             semaphore.release(lock, "setMapperTimeZone");
  119.         }
  120.     }
  121.     public static void registerJodaModule() {
  122.         if(internalMapper==null){
  123.             initMapper();
  124.         }
  125.         SemaphoreLock lock = semaphore.acquireThrowRuntime("registerJodaModule");
  126.         try {
  127.             internalMapper.registerModule(new JodaModule());
  128.         }finally {
  129.             semaphore.release(lock, "registerJodaModule");
  130.         }
  131.     }
  132.     public static void registerJavaTimeModule() {
  133.         if(internalMapper==null){
  134.             initMapper();
  135.         }
  136.         SemaphoreLock lock = semaphore.acquireThrowRuntime("registerJavaTimeModule");
  137.         try {
  138.             internalMapper.registerModule(new JavaTimeModule());
  139.         }finally {
  140.             semaphore.release(lock, "registerJavaTimeModule");
  141.         }
  142.     }
  143.    
  144.     public static ObjectMapper getObjectMapper() {
  145.         if(internalMapper==null){
  146.             initMapper();
  147.         }
  148.         return internalMapper;
  149.     }
  150.    
  151.     private static ObjectWriter writer;
  152.     private static synchronized void initWriter()  {
  153.         if(internalMapper==null){
  154.             initMapper();
  155.         }
  156.         if(writer==null){
  157.             writer = internalMapper.writer();
  158.         }
  159.     }
  160.     public static ObjectWriter getObjectWriter() {
  161.         if(writer==null){
  162.             initWriter();
  163.         }
  164.         return writer;
  165.     }
  166.    
  167.     private static ObjectWriter writerPrettyPrint;
  168.     private static synchronized void initWriterPrettyPrint()  {
  169.         if(internalMapper==null){
  170.             initMapper();
  171.         }
  172.         if(writerPrettyPrint==null){
  173.             writerPrettyPrint = internalMapper.writer().withDefaultPrettyPrinter();
  174.         }
  175.     }
  176.     public static ObjectWriter getObjectWriterPrettyPrint() {
  177.         if(writerPrettyPrint==null){
  178.             initWriterPrettyPrint();
  179.         }
  180.         return writerPrettyPrint;
  181.     }
  182.    
  183.    
  184.    
  185.     private JSONUtils(boolean prettyPrint) {
  186.         super(prettyPrint);
  187.     }
  188.    
  189.     @Override
  190.     protected void _initMapper() {
  191.         initMapper();
  192.     }
  193.     @Override
  194.     protected void _initWriter(boolean prettyPrint) {
  195.         if(prettyPrint) {
  196.             initWriterPrettyPrint();
  197.         }
  198.         else {
  199.             initWriter();
  200.         }
  201.     }
  202.    
  203.     @Override
  204.     protected ObjectMapper _getObjectMapper() {
  205.         return getObjectMapper();
  206.     }
  207.     @Override
  208.     protected ObjectWriter _getObjectWriter(boolean prettyPrint) {
  209.         if(prettyPrint) {
  210.             return getObjectWriterPrettyPrint();
  211.         }
  212.         else {
  213.             return getObjectWriter();
  214.         }
  215.     }
  216.    
  217.    
  218.     // IS
  219.    
  220.     public boolean isJson(byte[]jsonBytes){
  221.         return this.isValid(jsonBytes);
  222.     }
  223.    
  224.     public boolean isJson(String jsonString){
  225.         return this.isValid(jsonString);
  226.     }
  227.    
  228.    
  229.     /** UTILS per tipi vuoi schema: {} */
  230.    
  231.     // Fix: 1620
  232.     public static boolean containsEmptySchema(String json) {
  233.         String pattern = "(?m)\"schema\"\\s*:\\s*\\{\\s*\\}";
  234.         try {
  235.             Pattern p = Pattern.compile(pattern);
  236.             Matcher matcher = p.matcher(json);
  237.             return matcher.find();
  238.         }catch(Exception e) {
  239.             return false;
  240.         }
  241.     }
  242.     public static String resolveEmptySchema(String json) {
  243.         return json.replaceAll("(?m)\"schema\"\\s*:\\s*\\{\\s*\\}", "\"schema\": {\n  \"type\": \"string\"\n}");
  244.     }
  245.    
  246.    
  247.     // CONVERT TO MAP
  248.    
  249.     public Map<String, Serializable> convertToMap(Logger log, String source, String raw) {
  250.         return this.convertToMap(log, source, raw, null);
  251.     }
  252.     public Map<String, Serializable> convertToMap(Logger log, String source, String raw, List<String> claimsToConvert) {
  253.         if(this.isJson(raw)) {
  254.             return super.convertToMapEngine(log, source, raw, claimsToConvert);
  255.         }
  256.         else {
  257.             return new HashMap<>(); // empty return
  258.         }  
  259.     }
  260.    
  261.     public Map<String, Serializable> convertToMap(Logger log, String source, byte[]raw) {
  262.         return this.convertToMap(log, source, raw, null);
  263.     }
  264.     public Map<String, Serializable> convertToMap(Logger log, String source, byte[]raw, List<String> claimsToConvert) {
  265.         if(this.isJson(raw)) {
  266.             return super.convertToMapEngine(log, source, raw, claimsToConvert);
  267.         }
  268.         else {
  269.             return new HashMap<>(); // empty return
  270.         }  
  271.     }
  272.    
  273. }