ValidazioneSintattica.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.basic.validator;

  21. import java.util.Date;

  22. import org.openspcoop2.core.constants.TipoPdD;
  23. import org.openspcoop2.core.id.IDSoggetto;
  24. import org.openspcoop2.message.OpenSPCoop2Message;
  25. import org.openspcoop2.message.constants.ServiceBinding;
  26. import org.openspcoop2.protocol.basic.BasicStateComponentFactory;
  27. import org.openspcoop2.protocol.sdk.Busta;
  28. import org.openspcoop2.protocol.sdk.BustaRawContent;
  29. import org.openspcoop2.protocol.sdk.Context;
  30. import org.openspcoop2.protocol.sdk.IProtocolFactory;
  31. import org.openspcoop2.protocol.sdk.ProtocolException;
  32. import org.openspcoop2.protocol.sdk.Trasmissione;
  33. import org.openspcoop2.protocol.sdk.builder.IBustaBuilder;
  34. import org.openspcoop2.protocol.sdk.builder.ProprietaManifestAttachments;
  35. import org.openspcoop2.protocol.sdk.constants.ErroriCooperazione;
  36. import org.openspcoop2.protocol.sdk.constants.ProfiloDiCollaborazione;
  37. import org.openspcoop2.protocol.sdk.constants.RuoloMessaggio;
  38. import org.openspcoop2.protocol.sdk.constants.TipoOraRegistrazione;
  39. import org.openspcoop2.protocol.sdk.state.IState;
  40. import org.openspcoop2.protocol.sdk.validator.ProprietaValidazioneErrori;
  41. import org.openspcoop2.protocol.sdk.validator.ValidazioneSintatticaResult;
  42. import org.openspcoop2.utils.date.DateManager;

  43. /**
  44.  * ValidazioneSintattica
  45.  *
  46.  * @author Nardi Lorenzo (apoli@link.it)
  47.  * @author $Author$
  48.  * @version $Rev$, $Date$
  49.  *
  50.  */
  51. public class ValidazioneSintattica<BustaRawType> extends BasicStateComponentFactory implements
  52.         org.openspcoop2.protocol.sdk.validator.IValidazioneSintattica<BustaRawType> {

  53.     private IBustaBuilder<BustaRawType> bustaBuilder = null;
  54.     protected Context context;
  55.        
  56.     public ValidazioneSintattica(IProtocolFactory<BustaRawType> factory,IState state) throws ProtocolException{
  57.         super(factory,state);
  58.         this.bustaBuilder = factory.createBustaBuilder(state);
  59.     }

  60.     @Override
  61.     public void setContext(Context context) {
  62.         this.context = context;
  63.     }
  64.    
  65.     @Override
  66.     public ValidazioneSintatticaResult<BustaRawType> validaRichiesta(OpenSPCoop2Message msg, Busta datiBustaLettiURLMappingProperties, ProprietaValidazioneErrori proprietaValidazioneErrori) throws ProtocolException{
  67.        
  68.         Busta busta = null;
  69.         if(datiBustaLettiURLMappingProperties!=null)
  70.             busta = datiBustaLettiURLMappingProperties;
  71.         else
  72.             busta = new Busta(this.protocolFactory.getProtocol());
  73.        
  74.         Date oraRegistrazione = DateManager.getDate();
  75.         busta.setOraRegistrazione(oraRegistrazione);
  76.         if(busta.getTipoOraRegistrazione()==null){
  77.             busta.setTipoOraRegistrazione(TipoOraRegistrazione.LOCALE,this.protocolFactory.createTraduttore().toString(busta.getTipoOraRegistrazione()));
  78.         }
  79.         if(busta.sizeListaTrasmissioni()>0){
  80.             for (Trasmissione trasmissione : busta.getListaTrasmissioni()) {
  81.                 if(trasmissione.getOraRegistrazione()==null){
  82.                     trasmissione.setOraRegistrazione(oraRegistrazione);
  83.                 }
  84.                 if(trasmissione.getTempo()==null){
  85.                     trasmissione.setTempo(TipoOraRegistrazione.LOCALE,this.protocolFactory.createTraduttore().toString(busta.getTipoOraRegistrazione()));
  86.                 }
  87.             }
  88.         }
  89.        
  90.         return new ValidazioneSintatticaResult<BustaRawType>(null, null, null, busta, null, null, null, true);
  91.     }
  92.    
  93.     @Override
  94.     public ValidazioneSintatticaResult<BustaRawType> validaRisposta(OpenSPCoop2Message msg, Busta bustaRichiesta,
  95.             ProprietaValidazioneErrori proprietaValidazioneErrori) throws ProtocolException{
  96.        
  97.         Busta bustaRisposta = bustaRichiesta.invertiBusta(bustaRichiesta.getTipoOraRegistrazione(), bustaRichiesta.getTipoOraRegistrazioneValue());
  98.         bustaRisposta.setRiferimentoMessaggio(bustaRichiesta.getID());
  99.         bustaRisposta.setProfiloDiCollaborazione(bustaRichiesta.getProfiloDiCollaborazione(), bustaRichiesta.getProfiloDiCollaborazioneValue());
  100.         bustaRisposta.setTipoServizio(bustaRichiesta.getTipoServizio());
  101.         bustaRisposta.setServizio(bustaRichiesta.getServizio());
  102.         bustaRisposta.setVersioneServizio(bustaRichiesta.getVersioneServizio());
  103.         bustaRisposta.setAzione(bustaRichiesta.getAzione());
  104.         // estraggo l'identificativo di transazione dalla richiesta, eliminandone la data iniziale se il protocollo la supporta
  105.         String idSenzaData = null;
  106.         try{
  107.             Date d = this.bustaBuilder.extractDateFromID(bustaRichiesta.getID());
  108.             if(d!=null){
  109.                 String [] split = bustaRichiesta.getID().split("-");
  110.                 idSenzaData = bustaRichiesta.getID().substring((split[0].length()+1));
  111.             }
  112.             else{
  113.                 idSenzaData = bustaRichiesta.getID();
  114.             }
  115.         }catch(Exception e){
  116.             throw new ProtocolException("Identificativo di richiesta in un formato errato, estrazione della data non riuscita: "+e.getMessage(),e);
  117.         }
  118.         bustaRisposta.setID(this.bustaBuilder.newID(new IDSoggetto(bustaRisposta.getTipoMittente(), bustaRisposta.getMittente(), bustaRisposta.getIdentificativoPortaMittente()),
  119.                 idSenzaData, RuoloMessaggio.RISPOSTA));
  120.         bustaRisposta.setInoltro(bustaRichiesta.getInoltro(), bustaRichiesta.getInoltroValue());
  121.         bustaRisposta.setCollaborazione(bustaRichiesta.getCollaborazione());
  122.        
  123.         // Se e' presente UNA lista trasmissione la inverto
  124.         if(bustaRichiesta.sizeListaTrasmissioni()==1){
  125.             bustaRisposta.addTrasmissione(bustaRichiesta.getTrasmissione(0).invertiTrasmissione(bustaRichiesta.getTipoOraRegistrazione(), bustaRichiesta.getTipoOraRegistrazioneValue()));
  126.         }
  127.        
  128.         boolean hasFault = false;
  129.         try {
  130.             if(msg!=null && ServiceBinding.SOAP.equals(msg.getServiceBinding())){
  131.                 hasFault = msg.castAsSoap().hasSOAPFault();
  132.             }
  133.         } catch (Exception e) {
  134.             throw new ProtocolException(e);
  135.         }
  136.        
  137.         /*
  138.          * ONEWAY:
  139.          * Se e' un errore di cooperazione, ovvero isBustaErrore > e' una busta
  140.          * Se non e' un errore di cooperazione, non e' imbustato
  141.          *
  142.          * SINCRONO:
  143.          * E' sempre imbustato
  144.          */
  145.        
  146.         if(hasFault) {
  147.             ValidatoreErrori validatoreErrori = new ValidatoreErrori(this.protocolFactory,this.state);
  148.             if(bustaRisposta.getProfiloDiCollaborazione().equals(ProfiloDiCollaborazione.ONEWAY))
  149.                 if(validatoreErrori.isBustaErrore(bustaRichiesta, msg, proprietaValidazioneErrori)) {
  150.                     return new ValidazioneSintatticaResult<BustaRawType>(null, null, null, bustaRisposta, null, null, null, true);
  151.                 }
  152.                 else {
  153.                     return new ValidazioneSintatticaResult<BustaRawType>(null, null, null, null, ErroriCooperazione.ERRORE_GENERICO_PROCESSAMENTO_MESSAGGIO.
  154.                             getErroreProcessamento("Analizzato un messaggio fault"), bustaRisposta, null, false);
  155.                 }
  156.                
  157.             if(bustaRisposta.getProfiloDiCollaborazione().equals(ProfiloDiCollaborazione.SINCRONO))
  158.                 if(validatoreErrori.isBustaErrore(bustaRichiesta, msg, proprietaValidazioneErrori)) {
  159.                     return new ValidazioneSintatticaResult<BustaRawType>(null, null, null, bustaRisposta, ErroriCooperazione.ERRORE_GENERICO_PROCESSAMENTO_MESSAGGIO.
  160.                             getErroreProcessamento("Analizzato un messaggio fault"), bustaRisposta, null, true);
  161.                 }
  162.                 else {
  163.                     return new ValidazioneSintatticaResult<BustaRawType>(null, null, null, bustaRisposta, null, null, null, true);
  164.                 }
  165.         }
  166.         return new ValidazioneSintatticaResult<BustaRawType>(null, null, null, bustaRisposta, null, null, null, true);
  167.        
  168.     }
  169.    
  170.     @Override
  171.     public boolean verifyProtocolPresence(TipoPdD tipoPdD, ProfiloDiCollaborazione profilo, RuoloMessaggio ruoloMessaggio,
  172.             OpenSPCoop2Message msg) throws ProtocolException{
  173.         if(TipoPdD.DELEGATA.equals(tipoPdD)){
  174.             if(RuoloMessaggio.RICHIESTA.equals(ruoloMessaggio)){
  175.                 return false;
  176.             }else{
  177.                 return !ProfiloDiCollaborazione.ONEWAY.equals(profilo);
  178.             }
  179.         }
  180.         else if(TipoPdD.APPLICATIVA.equals(tipoPdD)){
  181.             if(RuoloMessaggio.RICHIESTA.equals(ruoloMessaggio)){
  182.                 return true;
  183.             }else{
  184.                 return false;
  185.             }
  186.         }
  187.         else if(TipoPdD.ROUTER.equals(tipoPdD)){
  188.             if(RuoloMessaggio.RICHIESTA.equals(ruoloMessaggio)){
  189.                 return true;
  190.             }else{
  191.                 return !ProfiloDiCollaborazione.ONEWAY.equals(profilo);
  192.             }
  193.         }
  194.         else{
  195.             throw new ProtocolException("TipoPdD ["+tipoPdD+"] non gestito");
  196.         }
  197.     }

  198.     @Override
  199.     public ValidazioneSintatticaResult<BustaRawType> validazioneFault(OpenSPCoop2Message msg) {
  200.         return null;
  201.     }

  202.     @Override
  203.     public ValidazioneSintatticaResult<BustaRawType> validazioneManifestAttachments(
  204.             OpenSPCoop2Message msg,
  205.             ProprietaManifestAttachments proprietaManifestAttachments) {
  206.         return null;
  207.     }

  208.     @Override
  209.     public BustaRawContent<BustaRawType> getBustaRawContent_senzaControlli(
  210.             OpenSPCoop2Message msg) throws ProtocolException {
  211.         return null;
  212.     }

  213.     @Override
  214.     public Busta getBusta_senzaControlli(OpenSPCoop2Message msg) throws ProtocolException{
  215.         return null;
  216.     }

  217. }