AbstractDeserializer.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 org.openspcoop2.utils.UtilsException;

  22. /**
  23.  * XmlDeserializer
  24.  *
  25.  * @author Poli Andrea (apoli@link.it)
  26.  * @author $Author$
  27.  * @version $Rev$, $Date$
  28.  */
  29. public abstract class AbstractDeserializer {

  30.     protected boolean generateTypeBlank = false;
  31.    
  32.     public AbstractDeserializer() {
  33.         this(false);
  34.     }
  35.     public AbstractDeserializer(boolean generateTypeBlank) {
  36.         this.generateTypeBlank = generateTypeBlank;
  37.     }
  38.    
  39.     public boolean isGenerateTypeBlank() {
  40.         return this.generateTypeBlank;
  41.     }
  42.     public void setGenerateTypeBlank(boolean generateTypeBlank) {
  43.         this.generateTypeBlank = generateTypeBlank;
  44.     }
  45.    
  46.     protected void set(ProblemRFC7807 problem, String name, Object value, boolean throwExceptionIfUnsupportedCustomClaim) throws UtilsException {
  47.        
  48.         if(ProblemConstants.CLAIM_TYPE.equals(name)) {
  49.             String type = this.getAsString(value);
  50.             if("about:blank".equals(type)) {
  51.                 if(this.generateTypeBlank) {
  52.                     problem.setType(type);
  53.                 }
  54.             }
  55.             else {
  56.                 problem.setType(type);
  57.             }
  58.         }
  59.         else if(ProblemConstants.CLAIM_TITLE.equals(name)) {
  60.             problem.setTitle(this.getAsString(value));
  61.         }
  62.         else if(ProblemConstants.CLAIM_STATUS.equals(name)) {
  63.             problem.setStatus(this.getAsInt(value));
  64.         }
  65.         else if(ProblemConstants.CLAIM_DETAIL.equals(name)) {
  66.             problem.setDetail(this.getAsString(value));
  67.         }
  68.         else if(ProblemConstants.CLAIM_INSTANCE.equals(name)) {
  69.             problem.setInstance(this.getAsString(value));
  70.         }
  71.         else {
  72.             if(value!=null) {
  73.                 if(value instanceof String) {
  74.                     problem.getCustom().put(name, value);
  75.                 }
  76.                 else if(value instanceof com.fasterxml.jackson.databind.node.TextNode) {
  77.                     com.fasterxml.jackson.databind.node.TextNode textNode = (com.fasterxml.jackson.databind.node.TextNode) value;
  78.                     problem.getCustom().put(name, (textNode.asText()));
  79.                 }
  80.                 else if(value instanceof Integer) {
  81.                     problem.getCustom().put(name, value);
  82.                 }
  83.                 else if(value instanceof com.fasterxml.jackson.databind.node.IntNode) {
  84.                     com.fasterxml.jackson.databind.node.IntNode intNode = (com.fasterxml.jackson.databind.node.IntNode) value;
  85.                     problem.getCustom().put(name, intNode.asInt());
  86.                 }
  87.                 else if(value instanceof Long) {
  88.                     problem.getCustom().put(name, value);
  89.                 }
  90.                 else if(value instanceof com.fasterxml.jackson.databind.node.LongNode) {
  91.                     com.fasterxml.jackson.databind.node.LongNode longNode = (com.fasterxml.jackson.databind.node.LongNode) value;
  92.                     problem.getCustom().put(name, longNode.asInt());
  93.                 }
  94.                 else if(value instanceof Boolean) {
  95.                     problem.getCustom().put(name, value);
  96.                 }
  97.                 else if(value instanceof com.fasterxml.jackson.databind.node.BooleanNode) {
  98.                     com.fasterxml.jackson.databind.node.BooleanNode booleanNode = (com.fasterxml.jackson.databind.node.BooleanNode) value;
  99.                     problem.getCustom().put(name, booleanNode.asBoolean());
  100.                 }
  101.                 else {
  102.                     if(throwExceptionIfUnsupportedCustomClaim) {
  103.                         throw new UtilsException("Custom claim with type ["+value.getClass().getName()+"] unsupported");
  104.                     }
  105.                 }
  106.             }
  107.             else {
  108.                 problem.getCustom().put(name, null);
  109.             }
  110.         }

  111.     }
  112.    
  113.     private String getAsString(Object value) {
  114.         if(value!=null) {
  115.             if(value instanceof String) {
  116.                 return (String)value;
  117.             }
  118.             else if(value instanceof com.fasterxml.jackson.databind.node.TextNode) {
  119.                 com.fasterxml.jackson.databind.node.TextNode text = (com.fasterxml.jackson.databind.node.TextNode) value;
  120.                 return text.asText();
  121.             }
  122.             else {
  123.                 return value.toString();
  124.             }
  125.         }
  126.         return null;
  127.     }
  128.     private Integer getAsInt(Object value) {
  129.         if(value!=null) {
  130.             if(value instanceof Integer) {
  131.                 return (Integer)value;
  132.             }
  133.             else if(value instanceof com.fasterxml.jackson.databind.node.IntNode) {
  134.                 com.fasterxml.jackson.databind.node.IntNode intNode = (com.fasterxml.jackson.databind.node.IntNode) value;
  135.                 return intNode.asInt();
  136.             }
  137.             else if(value instanceof Long) {
  138.                 return ((Long)value).intValue();
  139.             }
  140.             else if(value instanceof com.fasterxml.jackson.databind.node.LongNode) {
  141.                 com.fasterxml.jackson.databind.node.LongNode longNode = (com.fasterxml.jackson.databind.node.LongNode) value;
  142.                 return (int) longNode.asLong();
  143.             }
  144.             else {
  145.                 return Integer.parseInt(value.toString());
  146.             }
  147.         }
  148.         return null;
  149.     }
  150. }