XmlDeserializer.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.List;

  22. import org.openspcoop2.utils.UtilsException;
  23. import org.openspcoop2.utils.xml.XMLUtils;
  24. import org.w3c.dom.Element;
  25. import org.w3c.dom.Node;

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

  34.     private XMLUtils xmlUtils;
  35.    
  36.     public XmlDeserializer() {
  37.         this(false);
  38.     }
  39.     public XmlDeserializer(boolean generateTypeBlank) {
  40.         super(generateTypeBlank);
  41.         this.xmlUtils = XMLUtils.getInstance();
  42.     }
  43.    
  44.     public boolean isProblemRFC7807(String problemString) {
  45.         Element problemNode = null;
  46.         try {
  47.             problemNode = this.xmlUtils.newElement(problemString.getBytes());
  48.         }catch(Exception e) {
  49.             return false;
  50.         }
  51.         return this.isProblemRFC7807(problemNode);
  52.     }
  53.     public boolean isProblemRFC7807(byte[] problemByteArray) {
  54.         Element problemNode = null;
  55.         try {
  56.             problemNode = this.xmlUtils.newElement(problemByteArray);
  57.         }catch(Exception e) {
  58.             return false;
  59.         }
  60.         return this.isProblemRFC7807(problemNode);
  61.     }
  62.     public boolean isProblemRFC7807(Node problemNode) {
  63.         return problemNode!=null &&
  64.                 ProblemConstants.XML_PROBLEM_DETAILS_RFC_7807_LOCAL_NAME.equals(problemNode.getLocalName()) &&
  65.                 ProblemConstants.XML_PROBLEM_DETAILS_RFC_7807_NAMESPACE.equals(problemNode.getNamespaceURI());
  66.     }
  67.    
  68.     public ProblemRFC7807 fromString(String problemString) throws UtilsException {
  69.         return fromString(problemString, false);
  70.     }
  71.     public ProblemRFC7807 fromString(String problemString, boolean throwExceptionIfUnsupportedCustomClaim) throws UtilsException {
  72.         Element problemNode = null;
  73.         try {
  74.             problemNode = this.xmlUtils.newElement(problemString.getBytes());
  75.         }catch(Exception e) {
  76.             throw new UtilsException(e.getMessage(),e);
  77.         }
  78.         ProblemRFC7807 p = new ProblemRFC7807();
  79.         p.setRaw(problemString);
  80.         return this._fromNode(p, problemNode, true, throwExceptionIfUnsupportedCustomClaim);
  81.     }
  82.     public ProblemRFC7807 fromByteArray(byte[] problemByteArray) throws UtilsException {
  83.         return fromByteArray(problemByteArray, false);
  84.     }
  85.     public ProblemRFC7807 fromByteArray(byte[] problemByteArray, boolean throwExceptionIfUnsupportedCustomClaim) throws UtilsException {
  86.         Element problemNode = null;
  87.         try {
  88.             problemNode = this.xmlUtils.newElement(problemByteArray);
  89.         }catch(Exception e) {
  90.             throw new UtilsException(e.getMessage(),e);
  91.         }
  92.         ProblemRFC7807 p = new ProblemRFC7807();
  93.         p.setRaw(new String(problemByteArray));
  94.         return this._fromNode(p, problemNode, true, throwExceptionIfUnsupportedCustomClaim);
  95.     }
  96.     public ProblemRFC7807 fromNode(Node problemNode) throws UtilsException {
  97.         return this._fromNode(null, problemNode, true, false);
  98.     }
  99.     public ProblemRFC7807 fromNode(Node problemNode, boolean throwExceptionIfUnsupportedCustomClaim) throws UtilsException {
  100.         return this._fromNode(null, problemNode, true, throwExceptionIfUnsupportedCustomClaim);
  101.     }
  102.     public ProblemRFC7807 fromNode(Node problemNode, boolean setRaw, boolean throwExceptionIfUnsupportedCustomClaim) throws UtilsException {
  103.         return this._fromNode(null, problemNode, setRaw, throwExceptionIfUnsupportedCustomClaim);
  104.     }
  105.     private ProblemRFC7807 _fromNode(ProblemRFC7807 problemParam, Node problemNode, boolean setRaw,  boolean throwExceptionIfUnsupportedCustomClaim) throws UtilsException {
  106.        
  107.         ProblemRFC7807 problem = null;
  108.         if(problemParam!=null) {
  109.             problem = problemParam;
  110.         }
  111.         else {
  112.             problem = new ProblemRFC7807();
  113.         }
  114.        
  115.         List<Node> list = this.xmlUtils.getNotEmptyChildNodes(problemNode);
  116.         for (Node node : list) {
  117.            
  118.             String name = node.getLocalName();
  119.             if(name==null) {
  120.                 name = node.getNodeName();
  121.             }
  122.             Object value = null;
  123.             try {
  124.                 value = node.getTextContent();
  125.             }catch(Throwable e) {
  126.                 if(e instanceof java.lang.AbstractMethodError) { // axis per test
  127.                     value = node.getNodeValue();
  128.                     if(value==null) {
  129.                         if(node.getChildNodes()!=null) {
  130.                             value = ((org.w3c.dom.Text)node.getChildNodes().item(0)).getData();
  131.                         }
  132.                     }
  133.                 }
  134.                 else {
  135.                     throw new UtilsException(e.getMessage(),e);
  136.                 }
  137.             }
  138.            
  139.             super.set(problem, name, value,  throwExceptionIfUnsupportedCustomClaim);
  140.         }

  141.         if(setRaw && problem.getRaw()==null) {
  142.             try {
  143.                 problem.setRaw(this.xmlUtils.toString(problemNode));
  144.             }catch(Exception e) {
  145.                 throw new UtilsException(e.getMessage(), e);
  146.             }
  147.         }
  148.        
  149.         return problem;
  150.     }
  151.    
  152. }