XmlSerializer.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.io.ByteArrayOutputStream;
  22. import java.util.Iterator;

  23. import org.openspcoop2.utils.UtilsException;
  24. import org.openspcoop2.utils.xml.PrettyPrintXMLUtils;
  25. import org.openspcoop2.utils.xml.XMLUtils;
  26. import org.w3c.dom.Document;
  27. import org.w3c.dom.Element;

  28. /**
  29.  * XmlSerializer
  30.  *
  31.  * @author Poli Andrea (apoli@link.it)
  32.  * @author $Author$
  33.  * @version $Rev$, $Date$
  34.  */
  35. public class XmlSerializer {
  36.    
  37.     private boolean generateTypeBlank = false;
  38.     private XMLUtils xmlUtils;
  39.     private boolean prettyPrint;
  40.    
  41.     public XmlSerializer() {
  42.         this(false, false);
  43.     }
  44.     public XmlSerializer(boolean prettyPrint) {
  45.         this(prettyPrint, false);
  46.     }
  47.     public XmlSerializer(boolean prettyPrint, boolean generateTypeBlank) {
  48.         this.xmlUtils = XMLUtils.getInstance();
  49.         this.generateTypeBlank = generateTypeBlank;
  50.         this.prettyPrint = prettyPrint;
  51.     }
  52.    
  53.     public boolean isGenerateTypeBlank() {
  54.         return this.generateTypeBlank;
  55.     }
  56.     public void setGenerateTypeBlank(boolean generateTypeBlank) {
  57.         this.generateTypeBlank = generateTypeBlank;
  58.     }
  59.    
  60.     public String toString(ProblemRFC7807 problem) throws UtilsException {
  61.         return this.toString(problem, false);
  62.     }
  63.     public String toString(ProblemRFC7807 problem, boolean omitXMLDeclaration) throws UtilsException {
  64.         Element xmlProblem = this.toNode(problem);
  65.         if(this.prettyPrint) {
  66.             try {
  67.                 return PrettyPrintXMLUtils.prettyPrintWithTrAX(xmlProblem, omitXMLDeclaration);
  68.             }catch(Exception e) {
  69.                 throw new UtilsException(e.getMessage(),e);
  70.             }
  71.         }
  72.         else {
  73.             try {
  74.                 return this.xmlUtils.toString(xmlProblem, omitXMLDeclaration);
  75.             }catch(Exception e) {
  76.                 throw new UtilsException(e.getMessage(),e);
  77.             }
  78.         }
  79.     }
  80.    
  81.     public byte[] toByteArray(ProblemRFC7807 problem) throws UtilsException {
  82.         return this.toByteArray(problem, false);
  83.     }
  84.     public byte[] toByteArray(ProblemRFC7807 problem, boolean omitXMLDeclaration) throws UtilsException {
  85.         Element xmlProblem = toNode(problem);
  86.         if(this.prettyPrint) {
  87.             try {
  88.                 ByteArrayOutputStream bout = new ByteArrayOutputStream();
  89.                 PrettyPrintXMLUtils.prettyPrintWithTrAX(xmlProblem, bout, omitXMLDeclaration);
  90.                 bout.flush();
  91.                 bout.close();
  92.                 return bout.toByteArray();
  93.             }catch(Exception e) {
  94.                 throw new UtilsException(e.getMessage(),e);
  95.             }
  96.         }
  97.         else {
  98.             try {
  99.                 return this.xmlUtils.toByteArray(xmlProblem, omitXMLDeclaration);
  100.             }catch(Exception e) {
  101.                 throw new UtilsException(e.getMessage(),e);
  102.             }
  103.         }
  104.     }
  105.     public Element toNode(ProblemRFC7807 problem) throws UtilsException {
  106.         Document document = null;
  107.         try {
  108.             document = this.xmlUtils.newDocument();
  109.         }catch(Exception e) {
  110.             throw new UtilsException(e.getMessage(),e);
  111.         }
  112.         Element xmlProblem = document.createElementNS(ProblemConstants.XML_PROBLEM_DETAILS_RFC_7807_NAMESPACE,
  113.                 ProblemConstants.XML_PROBLEM_DETAILS_RFC_7807_LOCAL_NAME);
  114.        
  115.         if(problem.getType()!=null) {
  116.             Element child = document.createElement(ProblemConstants.CLAIM_TYPE);
  117.             child.setTextContent(problem.getType());
  118.             xmlProblem.appendChild(child);
  119.         }
  120.         else if(this.generateTypeBlank) {
  121.             Element child = document.createElement(ProblemConstants.CLAIM_TYPE);
  122.             child.setTextContent(ProblemConstants.CLAIM_TYPE_BLANK_VALUE);
  123.             xmlProblem.appendChild(child);
  124.         }
  125.        
  126.         if(problem.getTitle()!=null) {
  127.             Element child = document.createElement(ProblemConstants.CLAIM_TITLE);
  128.             child.setTextContent(problem.getTitle());
  129.             xmlProblem.appendChild(child);
  130.         }  
  131.        
  132.         if(problem.getStatus()!=null) {
  133.             Element child = document.createElement(ProblemConstants.CLAIM_STATUS);
  134.             child.setTextContent(problem.getStatus()+"");
  135.             xmlProblem.appendChild(child);
  136.         }  
  137.        
  138.         if(problem.getDetail()!=null) {
  139.             Element child = document.createElement(ProblemConstants.CLAIM_DETAIL);
  140.             child.setTextContent(problem.getDetail());
  141.             xmlProblem.appendChild(child);
  142.         }  
  143.        
  144.         if(problem.getInstance()!=null) {
  145.             Element child = document.createElement(ProblemConstants.CLAIM_INSTANCE);
  146.             child.setTextContent(problem.getInstance());
  147.             xmlProblem.appendChild(child);
  148.         }  
  149.        
  150.         processCustom(problem, document, xmlProblem);
  151.        
  152.         return xmlProblem;
  153.     }
  154.     private void processCustom(ProblemRFC7807 problem, Document document, Element xmlProblem) throws UtilsException {
  155.         if(problem.getCustom()!=null && !problem.getCustom().isEmpty()) {
  156.             Iterator<String> it = problem.getCustom().keySet().iterator();
  157.             while (it.hasNext()) {
  158.                 String claimName = it.next();
  159.                 Object o = problem.getCustom().get(claimName);
  160.                 if(o!=null) {
  161.                     Element child = document.createElement(claimName);
  162.                     if(o instanceof String) {
  163.                         child.setTextContent( (String) o);
  164.                     }
  165.                     else if(o instanceof Integer) {
  166.                         child.setTextContent(((Integer) o).toString());
  167.                     }
  168.                     else if(o instanceof Long) {
  169.                         child.setTextContent(((Long) o).toString());
  170.                     }
  171.                     else if(o instanceof Boolean) {
  172.                         child.setTextContent(((Boolean) o).toString());
  173.                     }
  174.                     else {
  175.                         throw new UtilsException("Custom claim with type ["+o.getClass().getName()+"] unsupported");
  176.                     }
  177.                     xmlProblem.appendChild(child);
  178.                 }
  179.             }
  180.         }
  181.     }
  182. }