SDIValidazioneConSchema.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.sdi.validator;

  21. import java.util.List;

  22. import javax.xml.soap.SOAPElement;

  23. import org.openspcoop2.message.OpenSPCoop2Message;
  24. import org.openspcoop2.message.OpenSPCoop2MessageFactory;
  25. import org.openspcoop2.message.OpenSPCoop2SoapMessage;
  26. import org.openspcoop2.message.soap.SoapUtils;
  27. import org.openspcoop2.message.soap.mtom.MtomXomReference;
  28. import org.openspcoop2.protocol.basic.validator.ValidazioneConSchema;
  29. import org.openspcoop2.protocol.sdi.constants.SDICostantiServizioRiceviFile;
  30. import org.openspcoop2.protocol.sdi.constants.SDICostantiServizioRiceviNotifica;
  31. import org.openspcoop2.protocol.sdi.constants.SDICostantiServizioRicezioneFatture;
  32. import org.openspcoop2.protocol.sdi.constants.SDICostantiServizioTrasmissioneFatture;
  33. import org.openspcoop2.protocol.sdk.Eccezione;
  34. import org.openspcoop2.protocol.sdk.IProtocolFactory;
  35. import org.openspcoop2.protocol.sdk.ProtocolException;
  36. import org.openspcoop2.protocol.sdk.constants.CodiceErroreCooperazione;
  37. import org.openspcoop2.protocol.sdk.constants.ContestoCodificaEccezione;
  38. import org.openspcoop2.protocol.sdk.constants.LivelloRilevanza;
  39. import org.openspcoop2.protocol.sdk.state.IState;
  40. import org.openspcoop2.utils.xml.AbstractValidatoreXSD;

  41. /**
  42.  * Classe che implementa, in base al protocollo SdI, l'interfaccia {@link org.openspcoop2.protocol.sdk.validator.IValidazioneConSchema}

  43.  *
  44.  * @author Poli Andrea (apoli@link.it)
  45.  * @author $Author$
  46.  * @version $Rev$, $Date$
  47.  */
  48. public class SDIValidazioneConSchema extends ValidazioneConSchema {

  49.     /** Errori di validazione riscontrati sulla busta */
  50.     private java.util.List<Eccezione> erroriValidazione;
  51.     /** Errori di processamento riscontrati sulla busta */
  52.     private java.util.List<Eccezione> erroriProcessamento;

  53.     public SDIValidazioneConSchema(IProtocolFactory<?> factory, IState state) throws ProtocolException {
  54.         super(factory, state);
  55.     }



  56.     @Override
  57.     public boolean initialize(OpenSPCoop2MessageFactory messageFactory) {
  58.         return true;
  59.     }

  60.     @Override
  61.     public List<Eccezione> getEccezioniValidazione() {
  62.         return this.erroriValidazione;
  63.     }

  64.     @Override
  65.     public List<Eccezione> getEccezioniProcessamento() {
  66.         return this.erroriProcessamento;
  67.     }

  68.     @Override
  69.     public void valida(OpenSPCoop2Message messageParam,
  70.             boolean isErroreProcessamento,
  71.             boolean isErroreIntestazione,
  72.             boolean isMessaggioConAttachments, boolean validazioneManifestAttachments) throws ProtocolException {

  73.         this.erroriValidazione = new java.util.ArrayList<Eccezione>();
  74.         this.erroriProcessamento = new java.util.ArrayList<Eccezione>();

  75.         List<MtomXomReference> references = null;
  76.        
  77.         try{
  78.             OpenSPCoop2SoapMessage message = messageParam.castAsSoap();
  79.            
  80.             references = message.mtomFastUnpackagingForXSDConformance();
  81.            
  82.             SOAPElement child = SoapUtils.getNotEmptyFirstChildSOAPElement(message.getSOAPBody());
  83.             String namespace = child.getNamespaceURI();

  84.             if(SDICostantiServizioRiceviFile.SDI_SERVIZIO_RICEVI_FILE_NAMESPACE.equals(namespace) ||
  85.                     SDICostantiServizioTrasmissioneFatture.TRASMISSIONE_SERVIZIO_TRASMISSIONE_FATTURE_NAMESPACE.equals(namespace)){
  86.                 AbstractValidatoreXSD validator = null;
  87.                 if(SDICostantiServizioRiceviFile.RICEVI_FILE_RICHIESTA_ROOT_ELEMENT.equals(child.getLocalName())){
  88.                     validator = it.gov.fatturapa.sdi.ws.trasmissione.v1_0.types.utils.SdIXSDValidator.getXSDValidatorTrasmissione_1_0(org.openspcoop2.message.xml.MessageXMLUtils.class,this.log);
  89.                 }else{
  90.                     validator = it.gov.fatturapa.sdi.ws.trasmissione.v1_0.types.utils.SdIXSDValidator.getXSDValidatorTrasmissione_1_1(org.openspcoop2.message.xml.MessageXMLUtils.class,this.log);
  91.                 }
  92.                 try{
  93.                     validator.valida(child);
  94.                 }catch(Exception e){
  95.                     this.log.error("Validazione con schema xsd fallita",e);
  96.                     Eccezione ecc = new Eccezione();
  97.                     ecc.setContestoCodifica(ContestoCodificaEccezione.INTESTAZIONE);
  98.                     ecc.setCodiceEccezione(CodiceErroreCooperazione.FORMATO_INTESTAZIONE_NON_CORRETTO);
  99.                     ecc.setRilevanza(LivelloRilevanza.ERROR);
  100.                     ecc.setDescrizione(e.getMessage());
  101.                     this.erroriValidazione.add(ecc);
  102.                 }
  103.             }
  104.             else if(SDICostantiServizioRiceviNotifica.SDI_SERVIZIO_RICEVI_NOTIFICA_NAMESPACE.equals(namespace) ||
  105.                     SDICostantiServizioRicezioneFatture.RICEZIONE_SERVIZIO_RICEZIONE_FATTURE_NAMESPACE.equals(namespace)){
  106.                 AbstractValidatoreXSD validator = it.gov.fatturapa.sdi.ws.ricezione.v1_0.types.utils.SdIXSDValidator.getXSDValidator(org.openspcoop2.message.xml.MessageXMLUtils.class,this.log);
  107.                 try{
  108.                     validator.valida(child);
  109.                 }catch(Exception e){
  110.                     this.log.error("Validazione con schema xsd fallita",e);
  111.                     Eccezione ecc = new Eccezione();
  112.                     ecc.setContestoCodifica(ContestoCodificaEccezione.INTESTAZIONE);
  113.                     ecc.setCodiceEccezione(CodiceErroreCooperazione.FORMATO_INTESTAZIONE_NON_CORRETTO);
  114.                     ecc.setRilevanza(LivelloRilevanza.ERROR);
  115.                     ecc.setDescrizione(e.getMessage());
  116.                     this.erroriValidazione.add(ecc);
  117.                 }
  118.             }
  119.         }catch(Exception e){
  120.             this.log.error("Validazione con schema xsd non riuscita non riuscita",e);
  121.             Eccezione ecc = new Eccezione();
  122.             ecc.setContestoCodifica(ContestoCodificaEccezione.PROCESSAMENTO);
  123.             ecc.setCodiceEccezione(CodiceErroreCooperazione.ERRORE_GENERICO_PROCESSAMENTO_MESSAGGIO);
  124.             ecc.setRilevanza(LivelloRilevanza.ERROR);
  125.             ecc.setDescrizione("Validazione con schema xsd non riuscita: errore di processamento");
  126.             this.erroriProcessamento.add(ecc);
  127.         }
  128.         finally{
  129.             try{
  130.                 OpenSPCoop2SoapMessage message = messageParam.castAsSoap();
  131.                 message.mtomRestoreAfterXSDConformance(references);
  132.             }catch(Exception e){
  133.                 this.log.error("Validazione con schema xsd non riuscita non riuscita",e);
  134.                 Eccezione ecc = new Eccezione();
  135.                 ecc.setContestoCodifica(ContestoCodificaEccezione.PROCESSAMENTO);
  136.                 ecc.setCodiceEccezione(CodiceErroreCooperazione.ERRORE_GENERICO_PROCESSAMENTO_MESSAGGIO);
  137.                 ecc.setRilevanza(LivelloRilevanza.ERROR);
  138.                 ecc.setDescrizione("Validazione con schema xsd non riuscita: errore di processamento");
  139.                 this.erroriProcessamento.add(ecc);
  140.             }
  141.         }

  142.         return;
  143.     }

  144. }