ModIBustaBuilder.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.protocol.modipa.builder;

  21. import java.util.Date;

  22. import org.openspcoop2.core.id.IDSoggetto;
  23. import org.openspcoop2.message.OpenSPCoop2Message;
  24. import org.openspcoop2.message.config.ServiceBindingConfiguration;
  25. import org.openspcoop2.message.constants.ServiceBinding;
  26. import org.openspcoop2.protocol.basic.builder.BustaBuilder;
  27. import org.openspcoop2.protocol.modipa.AbstractModISecurityToken;
  28. import org.openspcoop2.protocol.modipa.config.ModIProperties;
  29. import org.openspcoop2.protocol.modipa.constants.ModICostanti;
  30. import org.openspcoop2.protocol.modipa.validator.ModIRESTSecurity;
  31. import org.openspcoop2.protocol.modipa.validator.ModISOAPSecurity;
  32. import org.openspcoop2.protocol.sdk.Busta;
  33. import org.openspcoop2.protocol.sdk.Context;
  34. import org.openspcoop2.protocol.sdk.IProtocolFactory;
  35. import org.openspcoop2.protocol.sdk.ProtocolException;
  36. import org.openspcoop2.protocol.sdk.ProtocolMessage;
  37. import org.openspcoop2.protocol.sdk.builder.ProprietaManifestAttachments;
  38. import org.openspcoop2.protocol.sdk.constants.FaseImbustamento;
  39. import org.openspcoop2.protocol.sdk.constants.FaseSbustamento;
  40. import org.openspcoop2.protocol.sdk.constants.RuoloMessaggio;
  41. import org.openspcoop2.protocol.sdk.state.IState;
  42. import org.openspcoop2.protocol.sdk.state.RequestInfo;

  43. /**
  44.  * Classe che implementa, in base al protocollo ModI, l'interfaccia {@link org.openspcoop2.protocol.sdk.builder.IBustaBuilder}
  45.  *
  46.  * @author Poli Andrea (apoli@link.it)
  47.  * @author $Author$
  48.  * @version $Rev$, $Date$
  49.  */
  50. public class ModIBustaBuilder extends BustaBuilder<AbstractModISecurityToken<?>> {

  51.     private ModIProperties modipaProperties;
  52.     public ModIBustaBuilder(IProtocolFactory<?> factory,IState state) throws ProtocolException {
  53.         super(factory,state);
  54.         this.modipaProperties = ModIProperties.getInstance();
  55.     }

  56.     @Override
  57.     public ProtocolMessage imbustamento(OpenSPCoop2Message msg, Context context,
  58.             Busta busta, Busta bustaRichiesta,
  59.             RuoloMessaggio ruoloMessaggio,
  60.             ProprietaManifestAttachments proprietaManifestAttachments,
  61.             FaseImbustamento faseImbustamento)
  62.             throws ProtocolException {
  63.        
  64.         if(FaseImbustamento.PRIMA_SICUREZZA_MESSAGGIO.equals(faseImbustamento)) {
  65.             ProtocolMessage protocolMessage = new ProtocolMessage();
  66.             protocolMessage.setPhaseUnsupported(true);
  67.             return protocolMessage;
  68.         }
  69.        
  70.         RequestInfo requestInfo = null;
  71.         if(context!=null && context.containsKey(org.openspcoop2.core.constants.Costanti.REQUEST_INFO)) {
  72.             requestInfo = (RequestInfo) context.getObject(org.openspcoop2.core.constants.Costanti.REQUEST_INFO);
  73.         }
  74.        
  75.         ProtocolMessage protocolMessage = super.imbustamento(msg, context,
  76.                 busta, bustaRichiesta,
  77.                 ruoloMessaggio, proprietaManifestAttachments, faseImbustamento);
  78.        
  79.         boolean imbusta = true;
  80.         if(RuoloMessaggio.RISPOSTA.equals(ruoloMessaggio) &&
  81.             busta.sizeListaEccezioni()>0) {
  82.             boolean ignoraEccezioniNonGravi = this.protocolFactory.createProtocolManager().isIgnoraEccezioniNonGravi();
  83.             if(ignoraEccezioniNonGravi){
  84.                 if(busta.containsEccezioniGravi() ){
  85.                     imbusta = false;
  86.                 }
  87.             }
  88.             else{
  89.                 imbusta = false;
  90.             }
  91.         }
  92.        
  93.         if(imbusta) {
  94.             ModIImbustamento imbustamento = new ModIImbustamento(this.getLog());
  95.             protocolMessage = imbustamento.buildMessage(msg, context, busta, bustaRichiesta,
  96.                     ruoloMessaggio,  
  97.                     this.getProtocolFactory().getCachedRegistryReader(this.state, requestInfo),
  98.                     this.getProtocolFactory().getCachedConfigIntegrationReader(this.state, requestInfo),
  99.                     this.getProtocolFactory(), this.state);
  100.         }
  101.            
  102.         if(RuoloMessaggio.RISPOSTA.equals(ruoloMessaggio) && busta.sizeListaEccezioni()>0 ){
  103.            
  104.             // le eccezioni vengono tornate anche per gli errori di processamento poiche' in ModIProtocolVersionManager
  105.             // e' stato cablato il metodo isGenerazioneListaEccezioniErroreProcessamento al valore 'true'
  106.            
  107.             // Per quanto riguarda la generazione dei codici SOAPFault personalizzati e/o la generazione dell'elemento errore-applicativo
  108.             // la scelta e' delegata a due proprieta' nel file di proprieta'.
  109.             //
  110.             // Infine la scelta della presenza o meno dell'elemento OpenSPCoop2Details lo stesso viene pilotata dalle proprieta' presenti nel file di proprieta'
  111.        
  112.             boolean ignoraEccezioniNonGravi = this.protocolFactory.createProtocolManager().isIgnoraEccezioniNonGravi();
  113.             if(ignoraEccezioniNonGravi){
  114.                 if(busta.containsEccezioniGravi() ){
  115.                     this.enrichFault(msg, busta, ignoraEccezioniNonGravi,
  116.                             this.modipaProperties.isPortaApplicativaBustaErrorePersonalizzaElementiFault(),
  117.                             this.modipaProperties.isPortaApplicativaBustaErroreAggiungiErroreApplicativo());
  118.                 }  
  119.             }
  120.             else{
  121.                 this.enrichFault(msg, busta, ignoraEccezioniNonGravi,
  122.                         this.modipaProperties.isPortaApplicativaBustaErrorePersonalizzaElementiFault(),
  123.                         this.modipaProperties.isPortaApplicativaBustaErroreAggiungiErroreApplicativo());
  124.             }

  125.         }
  126.            
  127.         return protocolMessage;
  128.     }
  129.    
  130.     @Override
  131.     public ProtocolMessage sbustamento(OpenSPCoop2Message msg, Context context,
  132.             Busta busta, RuoloMessaggio ruoloMessaggio,
  133.             ProprietaManifestAttachments proprietaManifestAttachments, FaseSbustamento faseSbustamento,
  134.             ServiceBinding integrationServiceBinding, ServiceBindingConfiguration serviceBindingConfiguration)
  135.             throws ProtocolException {
  136.        
  137.         ProtocolMessage protocolMessage = null;
  138.        
  139.         if(FaseSbustamento.PRE_CONSEGNA_RICHIESTA.equals(faseSbustamento) || FaseSbustamento.PRE_CONSEGNA_RISPOSTA.equals(faseSbustamento) ) {
  140.        
  141.             protocolMessage = new ProtocolMessage();
  142.             if(msg!=null) {
  143.                
  144.                 Object soapInfo = msg.getContextProperty(ModICostanti.MODIPA_OPENSPCOOP2_MSG_CONTEXT_SBUSTAMENTO_SOAP);
  145.                 Object restInfo = msg.getContextProperty(ModICostanti.MODIPA_OPENSPCOOP2_MSG_CONTEXT_SBUSTAMENTO_REST);
  146.                
  147.                 if(soapInfo!=null) {
  148.                    
  149.                     // sbustamento SOAP
  150.                    
  151.                     ModISOAPSecurity soapSecurity = (ModISOAPSecurity) soapInfo;
  152.                     try {
  153.                         soapSecurity.clean(msg.castAsSoap());
  154.                     }catch(Exception e) {
  155.                         throw new ProtocolException(e.getMessage(),e);
  156.                     }
  157.                    
  158.                 }
  159.                 else if(restInfo!=null) {
  160.                    
  161.                     // sbustamento REST
  162.                    
  163.                     ModIRESTSecurity restSecurity = (ModIRESTSecurity) restInfo;
  164.                     try {
  165.                         restSecurity.clean(msg);
  166.                     }catch(Exception e) {
  167.                         throw new ProtocolException(e.getMessage(),e);
  168.                     }

  169.                 }
  170.             }
  171.             protocolMessage.setMessage(msg);
  172.            
  173.         }
  174.         else {
  175.             protocolMessage = super.sbustamento(msg, context,
  176.                     busta, ruoloMessaggio, proprietaManifestAttachments, faseSbustamento,
  177.                 integrationServiceBinding, serviceBindingConfiguration);
  178.         }
  179.        
  180.         protocolMessage.setUseBustaRawContentReadByValidation(true);
  181.         return protocolMessage;
  182.     }
  183.    
  184.     @Override
  185.     public String newID(IDSoggetto idSoggetto, String idTransazione, RuoloMessaggio ruoloMessaggio) throws ProtocolException {
  186.         return super.newID( idSoggetto, idTransazione, ruoloMessaggio, this.modipaProperties.generateIDasUUID());
  187.     }
  188.    
  189.    
  190.     @Override
  191.     public Date extractDateFromID(String id) throws ProtocolException {
  192.         return extractDateFromID(id, this.modipaProperties.generateIDasUUID());
  193.        
  194.     }
  195.    
  196. }