UtilitiesTemplate.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.core.integrazione;

  21. import java.io.ByteArrayOutputStream;
  22. import java.io.File;
  23. import java.util.List;
  24. import java.util.Map;

  25. import org.openspcoop2.core.config.Proprieta;
  26. import org.openspcoop2.core.id.IDPortaApplicativa;
  27. import org.openspcoop2.core.id.IDPortaDelegata;
  28. import org.openspcoop2.message.OpenSPCoop2Message;
  29. import org.openspcoop2.pdd.config.ConfigurazionePdDManager;
  30. import org.openspcoop2.pdd.config.OpenSPCoop2Properties;
  31. import org.openspcoop2.pdd.core.dynamic.DynamicUtils;
  32. import org.openspcoop2.pdd.core.dynamic.Template;
  33. import org.openspcoop2.pdd.core.trasformazioni.TipoTrasformazione;
  34. import org.openspcoop2.protocol.sdk.Busta;
  35. import org.openspcoop2.protocol.sdk.Context;
  36. import org.openspcoop2.protocol.sdk.state.RequestInfo;
  37. import org.slf4j.Logger;

  38. /**
  39.  * Classe contenenti utilities per le integrazioni gestite tramite template di trasformazione.
  40.  *
  41.  * @author Poli Andrea (apoli@link.it)
  42.  * @author $Author$
  43.  * @version $Rev$, $Date$
  44.  */
  45. public class UtilitiesTemplate {

  46.     private String tipoHeaderIntegrazione;
  47.     private OpenSPCoop2Message msg;
  48.     private Context context;
  49.     private Busta busta;
  50.     private Logger log;
  51.     private TipoTrasformazione tipoTrasformazione;
  52.     private Template template;
  53.    
  54.     public UtilitiesTemplate(String tipoHeaderIntegrazione, HeaderIntegrazione integrazione,
  55.             OutRequestPDMessage inRequestPDMessage, Context context, Logger log) throws HeaderIntegrazioneException {
  56.         try {
  57.             init(tipoHeaderIntegrazione, inRequestPDMessage.getMessage(), context, inRequestPDMessage.getBustaRichiesta(), log);
  58.            
  59.             IDPortaDelegata idPD = null;
  60.             if(inRequestPDMessage.getPortaDelegata()!=null) {
  61.                 idPD = new IDPortaDelegata();
  62.                 idPD.setNome(inRequestPDMessage.getPortaDelegata().getNome());
  63.             }
  64.            
  65.             List<Proprieta> proprieta = null;
  66.             if(inRequestPDMessage.getPortaDelegata()!=null && inRequestPDMessage.getPortaDelegata().getProprietaList()!=null) {
  67.                 proprieta = inRequestPDMessage.getPortaDelegata().getProprietaList();
  68.             }
  69.             init(proprieta, true, true,
  70.                     null, idPD);
  71.            
  72.         }catch(Exception e) {
  73.             throw new HeaderIntegrazioneException(e.getMessage(),e);
  74.         }
  75.     }
  76.     public UtilitiesTemplate(String tipoHeaderIntegrazione, HeaderIntegrazione integrazione,
  77.             OutResponsePDMessage inResponsePDMessage, Context context, Logger log) throws HeaderIntegrazioneException {
  78.         try {
  79.             init(tipoHeaderIntegrazione, inResponsePDMessage.getMessage(), context, inResponsePDMessage.getBustaRichiesta(), log);
  80.            
  81.             IDPortaDelegata idPD = null;
  82.             if(inResponsePDMessage.getPortaDelegata()!=null) {
  83.                 idPD = new IDPortaDelegata();
  84.                 idPD.setNome(inResponsePDMessage.getPortaDelegata().getNome());
  85.             }
  86.            
  87.             List<Proprieta> proprieta = null;
  88.             if(inResponsePDMessage.getPortaDelegata()!=null && inResponsePDMessage.getPortaDelegata().getProprietaList()!=null) {
  89.                 proprieta = inResponsePDMessage.getPortaDelegata().getProprietaList();
  90.             }
  91.             init(proprieta, true, false,
  92.                     null, idPD);
  93.            
  94.         }catch(Exception e) {
  95.             throw new HeaderIntegrazioneException(e.getMessage(),e);
  96.         }
  97.     }
  98.     public UtilitiesTemplate(String tipoHeaderIntegrazione, HeaderIntegrazione integrazione,
  99.             OutRequestPAMessage inRequestPAMessage, Context context, Logger log) throws HeaderIntegrazioneException {
  100.         try {
  101.             init(tipoHeaderIntegrazione, inRequestPAMessage.getMessage(), context, inRequestPAMessage.getBustaRichiesta(), log);
  102.            
  103.             IDPortaApplicativa idPA = null;
  104.             if(inRequestPAMessage.getPortaApplicativa()!=null) {
  105.                 idPA = new IDPortaApplicativa();
  106.                 idPA.setNome(inRequestPAMessage.getPortaApplicativa().getNome());
  107.             }
  108.            
  109.             List<Proprieta> proprieta = null;
  110.             if(inRequestPAMessage.getPortaApplicativa()!=null && inRequestPAMessage.getPortaApplicativa().getProprietaList()!=null) {
  111.                 proprieta = inRequestPAMessage.getPortaApplicativa().getProprietaList();
  112.             }
  113.             init(proprieta, false, true,
  114.                     idPA, null);
  115.            
  116.         }catch(Exception e) {
  117.             throw new HeaderIntegrazioneException(e.getMessage(),e);
  118.         }
  119.     }
  120.     public UtilitiesTemplate(String tipoHeaderIntegrazione, HeaderIntegrazione integrazione,
  121.             OutResponsePAMessage inResponsePAMessage, Context context, Logger log) throws HeaderIntegrazioneException {
  122.         try {
  123.             init(tipoHeaderIntegrazione, inResponsePAMessage.getMessage(), context, inResponsePAMessage.getBustaRichiesta(), log);
  124.            
  125.             IDPortaApplicativa idPA = null;
  126.             if(inResponsePAMessage.getPortaApplicativa()!=null) {
  127.                 idPA = new IDPortaApplicativa();
  128.                 idPA.setNome(inResponsePAMessage.getPortaApplicativa().getNome());
  129.             }
  130.            
  131.             List<Proprieta> proprieta = null;
  132.             if(inResponsePAMessage.getPortaApplicativa()!=null && inResponsePAMessage.getPortaApplicativa().getProprietaList()!=null) {
  133.                 proprieta = inResponsePAMessage.getPortaApplicativa().getProprietaList();
  134.             }
  135.             init(proprieta, false, false,
  136.                     idPA, null);
  137.            
  138.         }catch(Exception e) {
  139.             throw new HeaderIntegrazioneException(e.getMessage(),e);
  140.         }
  141.     }
  142.    
  143.     private void init(String tipoHeaderIntegrazione, OpenSPCoop2Message msg, Context context, Busta busta, Logger log){
  144.         this.tipoHeaderIntegrazione = tipoHeaderIntegrazione;
  145.         this.msg = msg;
  146.         this.context = context;
  147.         this.busta = busta;
  148.         this.log = log;
  149.     }
  150.     private void init(List<Proprieta> proprieta, boolean portaDelegata, boolean request,
  151.             IDPortaApplicativa idPA, IDPortaDelegata idPD) throws HeaderIntegrazioneException {
  152.         try {
  153.             OpenSPCoop2Properties properties = OpenSPCoop2Properties.getInstance();
  154.             TipoTrasformazione tipoTrasformazione = null;
  155.             String file = null;
  156.             if(portaDelegata) {
  157.                 if(request) {
  158.                     tipoTrasformazione = properties.getIntegrazioneTemplatePortaDelegataRequestTipo();
  159.                     file = properties.getIntegrazioneTemplatePortaDelegataRequestFile();
  160.                 }
  161.                 else {
  162.                     tipoTrasformazione = properties.getIntegrazioneTemplatePortaDelegataResponseTipo();
  163.                     file = properties.getIntegrazioneTemplatePortaDelegataResponseFile();
  164.                 }
  165.             }
  166.             else {
  167.                 if(request) {
  168.                     tipoTrasformazione = properties.getIntegrazioneTemplatePortaApplicativaRequestTipo();
  169.                     file = properties.getIntegrazioneTemplatePortaApplicativaRequestFile();
  170.                 }
  171.                 else {
  172.                     tipoTrasformazione = properties.getIntegrazioneTemplatePortaApplicativaResponseTipo();
  173.                     file = properties.getIntegrazioneTemplatePortaApplicativaResponseFile();
  174.                 }
  175.             }
  176.            
  177.             boolean fileDefinedInPorta = false;
  178.            
  179.             if(proprieta!=null && !proprieta.isEmpty()) {
  180.                
  181.                 String tipoTrasformazionePropertyName = request ? properties.getIntegrazioneTemplateRequestPropertyTipo() : properties.getIntegrazioneTemplateResponsePropertyTipo();
  182.                 String fileTrasformazionePropertyName = request ? properties.getIntegrazioneTemplateRequestPropertyFile() : properties.getIntegrazioneTemplateResponsePropertyFile();
  183.                
  184.                 for (Proprieta p : proprieta) {
  185.                     if(tipoTrasformazionePropertyName.equalsIgnoreCase(p.getNome())) {
  186.                         tipoTrasformazione = convert(p.getValore());
  187.                     }
  188.                     if(fileTrasformazionePropertyName.equalsIgnoreCase(p.getNome())) {
  189.                         file = p.getValore();
  190.                         fileDefinedInPorta = true;
  191.                     }
  192.                 }
  193.             }

  194.             this.tipoTrasformazione = tipoTrasformazione;
  195.             if(this.tipoTrasformazione==null) {
  196.                 throw new Exception("Tipo del template non definito");
  197.             }
  198.            
  199.             if(file==null) {
  200.                 throw new Exception("Template file non definito");
  201.             }
  202.             File fFile = new File(file);
  203.             /*
  204.             ContentFile cFile = ConfigurazionePdDManager.getInstance().getContentFile(fFile);
  205.             if(!cFile.isExists()) {
  206.                 throw new Exception("Template file '"+fFile.getAbsolutePath()+"' not exists");
  207.             }
  208.             byte [] template = cFile.getContent();
  209.             if(template==null || template.length<=0) {
  210.                 throw new Exception("Template '"+fFile.getAbsolutePath()+"' is empty");
  211.             }
  212.             */
  213.            
  214.             RequestInfo requestInfo = null;
  215.             if(this.context!=null && this.context.containsKey(org.openspcoop2.core.constants.Costanti.REQUEST_INFO)) {
  216.                 requestInfo = (RequestInfo) this.context.getObject(org.openspcoop2.core.constants.Costanti.REQUEST_INFO);
  217.             }
  218.            
  219.             ConfigurazionePdDManager configurazionePdDManager = ConfigurazionePdDManager.getInstance();
  220.             if(fileDefinedInPorta) {
  221.                 if(portaDelegata) {
  222.                     this.template = configurazionePdDManager.getTemplateIntegrazione(idPD, fFile, requestInfo);
  223.                 }
  224.                 else {
  225.                     this.template = configurazionePdDManager.getTemplateIntegrazione(idPA, fFile, requestInfo);
  226.                 }
  227.             }
  228.             else {
  229.                 this.template = configurazionePdDManager.getTemplateIntegrazione(fFile, requestInfo);
  230.             }
  231.            
  232.         }catch(Exception e) {
  233.             throw new HeaderIntegrazioneException(e.getMessage(),e);
  234.         }
  235.     }

  236.     public static TipoTrasformazione convert(String tipo) throws HeaderIntegrazioneException {
  237.         try {
  238.             TipoTrasformazione tipoT = TipoTrasformazione.toEnumConstant(tipo, true);
  239.             if(!tipoT.isTemplateFreemarker() &&
  240.                     !tipoT.isTemplateVelocity()) {
  241.                 throw new Exception("Tipo '"+tipo+"' non supportato");
  242.             }
  243.             return tipoT;
  244.         }catch(Exception e) {
  245.             throw new HeaderIntegrazioneException(e.getMessage(),e);
  246.         }
  247.     }

  248.        
  249.     public void process(boolean request) throws HeaderIntegrazioneException {
  250.         try {
  251.             if(this.msg==null) {
  252.                 return;
  253.             }
  254.            
  255.             boolean bufferMessage_readOnly =  OpenSPCoop2Properties.getInstance().isReadByPathBufferEnabled();
  256.             Map<String, Object> dynamicMapRequest = DynamicUtils.buildDynamicMap(this.msg, this.context, this.busta, this.log,
  257.                     bufferMessage_readOnly);
  258.             Map<String, Object> dynamicMapResponse = null;
  259.             if(!request) {
  260.                 dynamicMapResponse = DynamicUtils.buildDynamicMapResponse(this.msg, this.context, this.busta, this.log,
  261.                         bufferMessage_readOnly,
  262.                         dynamicMapRequest);
  263.             }
  264.            
  265.             Map<String, Object> dynamicMap = request ? dynamicMapRequest : dynamicMapResponse;
  266.            
  267.             if(this.tipoTrasformazione==null) {
  268.                 throw new Exception("Tipo di trasformazione non definita");
  269.             }
  270.             if(this.template==null) {
  271.                 throw new Exception("Template non definito");
  272.             }
  273.            
  274.             switch (this.tipoTrasformazione) {
  275.             case FREEMARKER_TEMPLATE:
  276.             case CONTEXT_FREEMARKER_TEMPLATE:
  277.             case FREEMARKER_TEMPLATE_ZIP:
  278.            
  279.                 this.log.debug("trasformazione '"+this.tipoHeaderIntegrazione+"' ["+this.tipoTrasformazione+"], risoluzione template ...");
  280.                 ByteArrayOutputStream bout = new ByteArrayOutputStream();
  281.                 if(TipoTrasformazione.FREEMARKER_TEMPLATE.equals(this.tipoTrasformazione) || TipoTrasformazione.CONTEXT_FREEMARKER_TEMPLATE.equals(this.tipoTrasformazione)) {
  282.                     DynamicUtils.convertFreeMarkerTemplate(this.template, dynamicMap, bout);
  283.                 }
  284.                 else {
  285.                     DynamicUtils.convertZipFreeMarkerTemplate(this.template.getZipTemplate(), dynamicMap, bout);
  286.                 }
  287.                 bout.flush();
  288.                 bout.close();
  289.                 this.log.debug("trasformazione '"+this.tipoHeaderIntegrazione+"' ["+this.tipoTrasformazione+"], risoluzione template completata");
  290.                 // ignoro il contenuto della trasformazione. Questo handler serve solamente a modificare il messaggio in transito
  291.                
  292.                 break;
  293.                
  294.             case VELOCITY_TEMPLATE:
  295.             case CONTEXT_VELOCITY_TEMPLATE:
  296.             case VELOCITY_TEMPLATE_ZIP:

  297.                            
  298.                 this.log.debug("trasformazione '"+this.tipoHeaderIntegrazione+"' ["+this.tipoTrasformazione+"], risoluzione template ...");
  299.                 bout = new ByteArrayOutputStream();
  300.                 if(TipoTrasformazione.VELOCITY_TEMPLATE.equals(this.tipoTrasformazione) || TipoTrasformazione.CONTEXT_VELOCITY_TEMPLATE.equals(this.tipoTrasformazione)) {
  301.                     DynamicUtils.convertVelocityTemplate(this.template, dynamicMap, bout);
  302.                 }
  303.                 else {
  304.                     DynamicUtils.convertZipVelocityTemplate(this.template.getZipTemplate(), dynamicMap, bout);  
  305.                 }
  306.                 bout.flush();
  307.                 bout.close();
  308.                 this.log.debug("trasformazione '"+this.tipoHeaderIntegrazione+"' ["+this.tipoTrasformazione+"], risoluzione template completata");
  309.                 // ignoro il contenuto della trasformazione. Questo handler serve solamente a modificare il messaggio in transito
  310.                
  311.                 break;

  312.             default:
  313.                 throw new Exception("Trasformazione '"+this.tipoHeaderIntegrazione+"' definita con un tipo '"+this.tipoTrasformazione+"' non supportato");
  314.             }
  315.            
  316.         }catch(Exception e) {
  317.             throw new HeaderIntegrazioneException(e.getMessage(),e);
  318.         }
  319.     }
  320. }