RicezioneContenutiApplicativiInternalErrorGenerator.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.services.error;

  21. import java.util.List;

  22. import org.openspcoop2.core.constants.TipoPdD;
  23. import org.openspcoop2.core.eccezione.details.DettaglioEccezione;
  24. import org.openspcoop2.core.id.IDSoggetto;
  25. import org.openspcoop2.message.OpenSPCoop2Message;
  26. import org.openspcoop2.message.OpenSPCoop2MessageFactory;
  27. import org.openspcoop2.message.config.ConfigurationRFC7807;
  28. import org.openspcoop2.message.config.IntegrationErrorReturnConfiguration;
  29. import org.openspcoop2.message.constants.IntegrationError;
  30. import org.openspcoop2.message.constants.MessageType;
  31. import org.openspcoop2.message.exception.ParseException;
  32. import org.openspcoop2.protocol.engine.builder.ErroreApplicativoBuilder;
  33. import org.openspcoop2.protocol.sdk.Context;
  34. import org.openspcoop2.protocol.sdk.Eccezione;
  35. import org.openspcoop2.protocol.sdk.ProtocolException;
  36. import org.openspcoop2.protocol.sdk.builder.ProprietaErroreApplicativo;
  37. import org.openspcoop2.protocol.sdk.constants.ErroreIntegrazione;
  38. import org.openspcoop2.protocol.sdk.constants.IntegrationFunctionError;
  39. import org.openspcoop2.protocol.sdk.state.RequestInfo;
  40. import org.slf4j.Logger;

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

  49.     private ProprietaErroreApplicativo proprietaErroreAppl = null;

  50.     public RicezioneContenutiApplicativiInternalErrorGenerator(Logger log, String idModulo, RequestInfo requestInfo) throws ProtocolException{
  51.        
  52.         super(log, idModulo, requestInfo, TipoPdD.DELEGATA, true);
  53.        
  54.         this.proprietaErroreAppl = this.openspcoopProperties.getProprietaGestioneErrorePD(this.protocolFactory.createProtocolManager());
  55.         this.proprietaErroreAppl.setDominio(this.identitaPdD.getCodicePorta());
  56.         this.proprietaErroreAppl.setIdModulo(this.idModulo);
  57.        
  58.     }
  59.    
  60.     public ProprietaErroreApplicativo getProprietaErroreAppl() {
  61.         return this.proprietaErroreAppl;
  62.     }
  63.    
  64.     public void updateProprietaErroreApplicativo(ProprietaErroreApplicativo proprietaErroreApplicativo){
  65.         this.proprietaErroreAppl = proprietaErroreApplicativo;
  66.     }
  67.        
  68.     @Override
  69.     public void updateDominio(IDSoggetto identitaPdD){
  70.         super.updateDominio(identitaPdD);
  71.         this.proprietaErroreAppl.setDominio(this.identitaPdD.getCodicePorta());
  72.     }
  73.    
  74.     public ErroreApplicativoBuilder getErroreApplicativoBuilderForAddDetailInSoapFault(Context context, MessageType messageTypeError, IntegrationFunctionError integrationFunctionError) throws ProtocolException{
  75.         IntegrationError integrationError = convertToIntegrationError(integrationFunctionError);
  76.         ConfigurationRFC7807 rfc7807 = this.getRfc7807ForErrorSafeMode(integrationError);
  77.         IntegrationErrorReturnConfiguration returnConfig = this.getReturnConfigForError(integrationError);
  78.         return this.getErroreApplicativoBuilder(context, messageTypeError, rfc7807, returnConfig, integrationFunctionError);
  79.     }
  80.     public ErroreApplicativoBuilder getErroreApplicativoBuilder(Context context, MessageType messageTypeError,
  81.             ConfigurationRFC7807 rfc7807, IntegrationErrorReturnConfiguration returnConfig, IntegrationFunctionError integrationFunctionError) throws ProtocolException{
  82.         ErroreApplicativoBuilder erroreApplicativoBuilder = new ErroreApplicativoBuilder(this.log, this.protocolFactory,
  83.                 this.identitaPdD, this.mittente, this.idServizio,
  84.                 this.idModulo,
  85.                 this.proprietaErroreAppl,
  86.                 messageTypeError, rfc7807, returnConfig, integrationFunctionError, this.getInterfaceName(),
  87.                 this.tipoPdD, this.servizioApplicativo,
  88.                 this.requestInfo.getIdTransazione(), context);
  89.         return erroreApplicativoBuilder;
  90.     }
  91.    
  92.    
  93.    
  94.     public OpenSPCoop2Message build(Context context, IntegrationFunctionError integrationFunctionError, Eccezione ecc, IDSoggetto idSoggettoProduceEccezione, ParseException parseException) {
  95.         return build(context, integrationFunctionError, ecc, idSoggettoProduceEccezione, null, parseException);
  96.     }
  97.     public OpenSPCoop2Message build(Context context, IntegrationFunctionError integrationFunctionError, Eccezione ecc, IDSoggetto idSoggettoProduceEccezione, DettaglioEccezione dettaglioEccezione, ParseException parseException) {
  98.        
  99.         IntegrationError integrationError = convertToIntegrationError(integrationFunctionError);
  100.         MessageType msgTypeErrorResponse = this.getMessageTypeForErrorSafeMode(integrationError);
  101.         ConfigurationRFC7807 rfc7807 = this.getRfc7807ForErrorSafeMode(integrationError);
  102.         boolean useProblemRFC7807 = rfc7807!=null;
  103.         try{
  104.             IntegrationErrorReturnConfiguration returnConfig = this.getReturnConfigForError(integrationError);
  105.             ErroreApplicativoBuilder erroreApplicativoBuilder = getErroreApplicativoBuilder(context, msgTypeErrorResponse,rfc7807, returnConfig, integrationFunctionError);
  106.             OpenSPCoop2Message msg = erroreApplicativoBuilder.toMessage(ecc,idSoggettoProduceEccezione,dettaglioEccezione,parseException);  
  107.             return msg;
  108.         }catch(Exception e){
  109.             this.log.error("Errore durante la costruzione del messaggio di eccezione integrazione",e);
  110.             return OpenSPCoop2MessageFactory.getDefaultMessageFactory().createFaultMessage(msgTypeErrorResponse,useProblemRFC7807, e);
  111.         }
  112.     }
  113.    
  114.     public OpenSPCoop2Message build(Context context, IntegrationFunctionError integrationFunctionError, ErroreIntegrazione erroreIntegrazione, Throwable eProcessamento, ParseException parseException) {
  115.        
  116.         IntegrationError integrationError = convertToIntegrationError(integrationFunctionError);
  117.         MessageType msgTypeErrorResponse = this.getMessageTypeForErrorSafeMode(integrationError);
  118.         ConfigurationRFC7807 rfc7807 = this.getRfc7807ForErrorSafeMode(integrationError);
  119.         boolean useProblemRFC7807 = rfc7807!=null;
  120.         try{
  121.             IntegrationErrorReturnConfiguration returnConfig = this.getReturnConfigForError(integrationError);
  122.             ErroreApplicativoBuilder erroreApplicativoBuilder = getErroreApplicativoBuilder(context, msgTypeErrorResponse,rfc7807, returnConfig, integrationFunctionError);
  123.             OpenSPCoop2Message msg = erroreApplicativoBuilder.toMessage(erroreIntegrazione,eProcessamento,parseException);
  124.             return msg;
  125.         }catch(Exception e){
  126.             this.log.error("Errore durante la costruzione del messaggio di eccezione integrazione",e);
  127.             return OpenSPCoop2MessageFactory.getDefaultMessageFactory().createFaultMessage(msgTypeErrorResponse,useProblemRFC7807, e);
  128.         }
  129.     }
  130.    
  131.     public byte[] buildAsByteArray(Context context, IntegrationFunctionError integrationFunctionError, ErroreIntegrazione erroreIntegrazione, List<Integer> returnCode) {
  132.        
  133.         IntegrationError integrationError = convertToIntegrationError(integrationFunctionError);
  134.         MessageType msgTypeErrorResponse = this.getMessageTypeForErrorSafeMode(integrationError);
  135.         ConfigurationRFC7807 rfc7807 = this.getRfc7807ForErrorSafeMode(integrationError);
  136.         boolean useProblemRFC7807 = rfc7807!=null;
  137.         try{
  138.             IntegrationErrorReturnConfiguration returnConfig = this.getReturnConfigForError(integrationError);
  139.             ErroreApplicativoBuilder erroreApplicativoBuilder = getErroreApplicativoBuilder(context, msgTypeErrorResponse,rfc7807, returnConfig, integrationFunctionError);
  140.             byte[] bytes = erroreApplicativoBuilder.toByteArray(erroreIntegrazione);
  141.             returnCode.add(returnConfig.getHttpReturnCode());
  142.             return bytes;
  143.         }catch(Exception e){
  144.             this.log.error("Errore durante la costruzione del messaggio di eccezione integrazione",e);
  145.             try{
  146.                 OpenSPCoop2Message msgError = OpenSPCoop2MessageFactory.getDefaultMessageFactory().createFaultMessage(msgTypeErrorResponse,useProblemRFC7807, e);
  147.                 java.io.ByteArrayOutputStream bout = new java.io.ByteArrayOutputStream();
  148.                 msgError.writeTo(bout,true);
  149.                 bout.flush();
  150.                 bout.close();
  151.                 return  bout.toByteArray();
  152.             }catch(Exception eInternal){
  153.                 throw new RuntimeException(eInternal.getMessage(),eInternal); // non dovrebbe mai accadere
  154.             }
  155.         }
  156.     }
  157.    
  158. }