Serializer.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.pdd.logger.traccia;

  21. import java.io.ByteArrayOutputStream;
  22. import java.io.OutputStream;
  23. import java.lang.reflect.Field;
  24. import java.util.Properties;

  25. import org.openspcoop2.message.xml.MessageXMLUtils;
  26. import org.openspcoop2.utils.json.JacksonJsonUtils;
  27. import org.openspcoop2.utils.json.JacksonXmlUtils;
  28. import org.openspcoop2.utils.service.beans.TransazioneBase;
  29. import org.w3c.dom.Document;
  30. import org.w3c.dom.Element;
  31. import org.w3c.dom.Node;
  32. import org.w3c.dom.NodeList;

  33. /**
  34.  * Serializer
  35.  *
  36.  * @author Poli Andrea (apoli@link.it)
  37.  * @author $Author$
  38.  * @version $Rev$, $Date$
  39.  */
  40. public class Serializer {

  41.     private boolean prettyPrint = false;
  42.     private String xml_namespace = "http://govway.org/traccia";
  43.     private String xml_localName = "traccia";
  44.    
  45.     public Serializer() {      
  46.     }
  47.     public Serializer(Properties pConf) throws TracciaException {
  48.         try {
  49.             Field [] fields = Serializer.class.getDeclaredFields();
  50.             for (Field field : fields) {
  51.                 String fieldName = field.getName();
  52.                 fieldName = fieldName.replace("_", ".");
  53.                 if(pConf.containsKey(fieldName)) {
  54.                     String value = pConf.getProperty(fieldName);
  55.                     String bCN = boolean.class.getName()+"";
  56.                     if(bCN.equals(field.getType().getName())) {
  57.                         field.set(this, "true".equalsIgnoreCase(value));
  58.                     }else {
  59.                         field.set(this, value);
  60.                     }
  61.                 }
  62.             }
  63.         }catch(Exception e) {
  64.             throw new TracciaException(e.getMessage(),e);
  65.         }
  66.        
  67.     }
  68.    
  69.    
  70.     public byte[] toJsonByteArray(TransazioneBase transazione) throws TracciaException {
  71.         try {
  72.             ByteArrayOutputStream bout = new ByteArrayOutputStream();
  73.             this.toJson(bout, transazione);
  74.             bout.flush();
  75.             bout.close();
  76.             return bout.toByteArray();
  77.         }
  78.         catch(Exception e) {
  79.             throw new TracciaException(e.getMessage(),e);
  80.         }
  81.     }
  82.     public String toJson(TransazioneBase transazione) throws TracciaException {
  83.         try {
  84.             ByteArrayOutputStream bout = new ByteArrayOutputStream();
  85.             this.toJson(bout, transazione);
  86.             bout.flush();
  87.             bout.close();
  88.             return bout.toString();
  89.         }
  90.         catch(Exception e) {
  91.             throw new TracciaException(e.getMessage(),e);
  92.         }
  93.     }
  94.     public void toJson(OutputStream os , TransazioneBase transazione) throws TracciaException {
  95.         try {
  96.             JacksonJsonUtils jsonUtils = JacksonJsonUtils.getInstance(this.prettyPrint);
  97.             jsonUtils.writeTo(transazione, os);
  98.         }
  99.         catch(Exception e) {
  100.             throw new TracciaException(e.getMessage(),e);
  101.         }
  102.     }
  103.    
  104.     public byte[] toXmlByteArray(TransazioneBase transazione) throws TracciaException {
  105.         try {
  106.             ByteArrayOutputStream bout = new ByteArrayOutputStream();
  107.             this.toXml(bout, transazione);
  108.             bout.flush();
  109.             bout.close();
  110.             return bout.toByteArray();
  111.         }
  112.         catch(Exception e) {
  113.             throw new TracciaException(e.getMessage(),e);
  114.         }
  115.     }
  116.     public String toXml(TransazioneBase transazione) throws TracciaException {
  117.         try {
  118.             ByteArrayOutputStream bout = new ByteArrayOutputStream();
  119.             this.toXml(bout, transazione);
  120.             bout.flush();
  121.             bout.close();
  122.             return bout.toString();
  123.         }
  124.         catch(Exception e) {
  125.             throw new TracciaException(e.getMessage(),e);
  126.         }
  127.     }
  128.     public void toXml(OutputStream os , TransazioneBase transazione) throws TracciaException {
  129.         try {
  130.             JacksonXmlUtils jacksonXmlUtils = JacksonXmlUtils.getInstance(this.prettyPrint);
  131.            
  132.             ByteArrayOutputStream bout = new ByteArrayOutputStream();
  133.             jacksonXmlUtils.writeTo(transazione, bout);
  134.             bout.flush();
  135.             bout.close();
  136.            
  137.             MessageXMLUtils xmlUtils = MessageXMLUtils.DEFAULT;
  138.             Element element = xmlUtils.newElement(bout.toByteArray());
  139.             Document dom = xmlUtils.newDocument();
  140.             Element elementNew = dom.createElementNS(this.xml_namespace, this.xml_localName);
  141.             NodeList list = element.getChildNodes();
  142.             for (int i = 0; i < list.getLength(); i++) {
  143.                 Node n = list.item(i);
  144.                 Node nAdopt = dom.adoptNode(n.cloneNode(true));
  145.                 elementNew.appendChild(nAdopt);
  146.             }
  147.             xmlUtils.writeTo(elementNew, os);
  148.            
  149.         }
  150.         catch(Exception e) {
  151.             throw new TracciaException(e.getMessage(),e);
  152.         }
  153.     }
  154.    
  155.     public void setPrettyPrint(boolean prettyPrint) {
  156.         this.prettyPrint = prettyPrint;
  157.     }
  158.     public void setXml_namespace(String xml_namespace) {
  159.         this.xml_namespace = xml_namespace;
  160.     }
  161.     public void setXml_localName(String xml_localName) {
  162.         this.xml_localName = xml_localName;
  163.     }
  164. }