JsonSerializer.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.rest.problem;

  21. import java.util.Iterator;

  22. import org.openspcoop2.utils.UtilsException;
  23. import org.openspcoop2.utils.json.JSONUtils;

  24. import com.fasterxml.jackson.databind.node.ObjectNode;

  25. /**
  26.  * JsonSerializer
  27.  *
  28.  * @author Poli Andrea (apoli@link.it)
  29.  * @author $Author$
  30.  * @version $Rev$, $Date$
  31.  */
  32. public class JsonSerializer {

  33.     private boolean generateTypeBlank = false;
  34.     private JSONUtils jsonUtils;
  35.    
  36.     public JsonSerializer() {
  37.         this(false, false);
  38.     }
  39.     public JsonSerializer(boolean prettyPrint) {
  40.         this(prettyPrint, false);
  41.     }
  42.     public JsonSerializer(boolean prettyPrint, boolean generateTypeBlank) {
  43.         this.jsonUtils = JSONUtils.getInstance(prettyPrint);
  44.         this.generateTypeBlank = generateTypeBlank;
  45.     }
  46.    
  47.     public boolean isGenerateTypeBlank() {
  48.         return this.generateTypeBlank;
  49.     }
  50.     public void setGenerateTypeBlank(boolean generateTypeBlank) {
  51.         this.generateTypeBlank = generateTypeBlank;
  52.     }
  53.    
  54.     public String toString(ProblemRFC7807 problem) throws UtilsException {
  55.         ObjectNode jsonProblem = this.toNode(problem);
  56.         return this.jsonUtils.toString(jsonProblem);
  57.     }
  58.     public byte[] toByteArray(ProblemRFC7807 problem) throws UtilsException {
  59.         ObjectNode jsonProblem = toNode(problem);
  60.         return this.jsonUtils.toByteArray(jsonProblem);
  61.     }
  62.     public ObjectNode toNode(ProblemRFC7807 problem) throws UtilsException {
  63.         ObjectNode jsonProblem = this.jsonUtils.newObjectNode();
  64.        
  65.         if(problem.getType()!=null) {
  66.             jsonProblem.put(ProblemConstants.CLAIM_TYPE, problem.getType());
  67.         }
  68.         else if(this.generateTypeBlank) {
  69.             jsonProblem.put(ProblemConstants.CLAIM_TYPE, ProblemConstants.CLAIM_TYPE_BLANK_VALUE);
  70.         }
  71.        
  72.         if(problem.getTitle()!=null) {
  73.             jsonProblem.put(ProblemConstants.CLAIM_TITLE, problem.getTitle());
  74.         }  
  75.        
  76.         if(problem.getStatus()!=null) {
  77.             jsonProblem.put(ProblemConstants.CLAIM_STATUS, problem.getStatus());
  78.         }  
  79.        
  80.         if(problem.getDetail()!=null) {
  81.             jsonProblem.put(ProblemConstants.CLAIM_DETAIL, problem.getDetail());
  82.         }  
  83.        
  84.         if(problem.getInstance()!=null) {
  85.             jsonProblem.put(ProblemConstants.CLAIM_INSTANCE, problem.getInstance());
  86.         }  
  87.        
  88.         processCustom(problem, jsonProblem);
  89.        
  90.         return jsonProblem;
  91.     }
  92.     private void processCustom(ProblemRFC7807 problem, ObjectNode jsonProblem) throws UtilsException {
  93.         if(problem.getCustom()!=null && !problem.getCustom().isEmpty()) {
  94.             Iterator<String> it = problem.getCustom().keySet().iterator();
  95.             while (it.hasNext()) {
  96.                 String claimName = it.next();
  97.                 Object o = problem.getCustom().get(claimName);
  98.                 if(o!=null) {
  99.                     if(o instanceof String) {
  100.                         jsonProblem.put(claimName, (String) o);
  101.                     }
  102.                     else if(o instanceof Integer) {
  103.                         jsonProblem.put(claimName, (Integer) o);
  104.                     }
  105.                     else if(o instanceof Long) {
  106.                         jsonProblem.put(claimName, (Long) o);
  107.                     }
  108.                     else if(o instanceof Boolean) {
  109.                         jsonProblem.put(claimName, (Boolean) o);
  110.                     }
  111.                     else {
  112.                         throw new UtilsException("Custom claim with type ["+o.getClass().getName()+"] unsupported");
  113.                     }
  114.                 }
  115.             }
  116.         }
  117.     }
  118. }