AbstractErrorGenerator.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 javax.xml.namespace.QName;

  22. import org.apache.commons.lang.StringUtils;
  23. import org.openspcoop2.core.constants.Costanti;
  24. import org.openspcoop2.core.constants.TipoPdD;
  25. import org.openspcoop2.core.id.IDServizio;
  26. import org.openspcoop2.core.id.IDSoggetto;
  27. import org.openspcoop2.message.OpenSPCoop2Message;
  28. import org.openspcoop2.message.OpenSPCoop2MessageFactory;
  29. import org.openspcoop2.message.config.ConfigurationRFC7807;
  30. import org.openspcoop2.message.config.FaultBuilderConfig;
  31. import org.openspcoop2.message.config.IntegrationErrorCollection;
  32. import org.openspcoop2.message.config.IntegrationErrorConfiguration;
  33. import org.openspcoop2.message.config.IntegrationErrorReturnConfiguration;
  34. import org.openspcoop2.message.constants.IntegrationError;
  35. import org.openspcoop2.message.constants.MessageType;
  36. import org.openspcoop2.message.constants.ServiceBinding;
  37. import org.openspcoop2.pdd.config.OpenSPCoop2Properties;
  38. import org.openspcoop2.pdd.logger.OpenSPCoop2Logger;
  39. import org.openspcoop2.protocol.sdk.Context;
  40. import org.openspcoop2.protocol.sdk.IProtocolFactory;
  41. import org.openspcoop2.protocol.sdk.ProtocolException;
  42. import org.openspcoop2.protocol.sdk.constants.IntegrationFunctionError;
  43. import org.openspcoop2.protocol.sdk.state.RequestInfo;
  44. import org.openspcoop2.protocol.utils.ErroriProperties;
  45. import org.slf4j.Logger;


  46. /**
  47.  * RequestInfo
  48.  *
  49.  * @author Poli Andrea (apoli@link.it)
  50.  * @author $Author$
  51.  * @version $Rev$, $Date$
  52.  */
  53. public abstract class AbstractErrorGenerator {

  54.     protected boolean internalErrorConfiguration;
  55.    
  56.     protected Logger log;
  57.     protected IProtocolFactory<?> protocolFactory;
  58.     public IProtocolFactory<?> getProtocolFactory() {
  59.         return this.protocolFactory;
  60.     }
  61.     protected String idModulo;
  62.     protected IDSoggetto identitaPdD;
  63.    
  64.     protected IDSoggetto mittente;
  65.     protected IDServizio idServizio;
  66.     protected String servizioApplicativo;
  67.    
  68.     protected OpenSPCoop2Properties openspcoopProperties;
  69.     protected RequestInfo requestInfo;
  70.     protected MessageType requestMessageType;
  71.     protected ServiceBinding serviceBinding;
  72.    
  73.     protected TipoPdD tipoPdD;
  74.     public void updateTipoPdD(TipoPdD tipoPdD) {
  75.         this.tipoPdD = tipoPdD;
  76.     }
  77.    
  78.     protected MessageType forceMessageTypeResponse;
  79.     public void setForceMessageTypeResponse(MessageType forceMessageTypeResponse) {
  80.         this.forceMessageTypeResponse = forceMessageTypeResponse;
  81.     }
  82.    
  83.     protected AbstractErrorGenerator(Logger log, String idModulo, RequestInfo requestInfo,
  84.             TipoPdD tipoPdD, boolean internalErrorConfiguration) throws ProtocolException{
  85.         this.log = log;
  86.         this.protocolFactory = requestInfo.getProtocolFactory();
  87.         this.idModulo = idModulo;
  88.         this.identitaPdD = requestInfo.getIdentitaPdD();
  89.        
  90.         this.openspcoopProperties = OpenSPCoop2Properties.getInstance();
  91.        
  92.         this.requestInfo = requestInfo;
  93.        
  94.         this.tipoPdD = tipoPdD;
  95.        
  96.         this.internalErrorConfiguration = internalErrorConfiguration;
  97.        
  98.         if(TipoPdD.APPLICATIVA.equals(tipoPdD)){
  99.             this.requestMessageType = requestInfo.getProtocolRequestMessageType();
  100.             this.serviceBinding = requestInfo.getProtocolServiceBinding();
  101.         }
  102.         else{
  103.             this.requestMessageType = requestInfo.getIntegrationRequestMessageType();
  104.             this.serviceBinding = requestInfo.getIntegrationServiceBinding();
  105.         }
  106.     }
  107.    
  108.     protected String getInterfaceName() {
  109.         String nomePorta = null;
  110.         if(this.requestInfo!=null && this.requestInfo.getProtocolContext()!=null) {
  111.             nomePorta = this.requestInfo.getProtocolContext().getInterfaceName();
  112.         }
  113.         return nomePorta;
  114.     }
  115.    
  116.     public void updateRequestMessageType(MessageType requestMessageType){
  117.         this.requestMessageType = requestMessageType;
  118.     }
  119.    
  120.     public void updateServiceBinding(ServiceBinding serviceBinding){
  121.         this.serviceBinding = serviceBinding;
  122.     }
  123.    
  124.     public void updateDominio(IDSoggetto identitaPdD){
  125.         this.identitaPdD = identitaPdD;
  126.     }
  127.    
  128.     public void updateInformazioniCooperazione(IDSoggetto mittente, IDServizio idServizio){
  129.         this.mittente = mittente;
  130.         this.idServizio = idServizio;
  131.     }
  132.     public void updateInformazioniCooperazione(String servizioApplicativo){
  133.         this.servizioApplicativo = servizioApplicativo;
  134.     }
  135.    
  136.     protected IntegrationErrorConfiguration getConfigurationForError(IntegrationError integrationError){
  137.         IntegrationErrorCollection errorCollection = null;
  138.         if(this.internalErrorConfiguration){
  139.             errorCollection = this.requestInfo.getBindingConfig().getInternalIntegrationErrorConfiguration(this.serviceBinding);
  140.         }else{
  141.             errorCollection = this.requestInfo.getBindingConfig().getExternalIntegrationErrorConfiguration(this.serviceBinding);
  142.         }
  143.         IntegrationErrorConfiguration config = errorCollection.getIntegrationError(integrationError);
  144.         if(config==null){
  145.             config = errorCollection.getIntegrationError(IntegrationError.DEFAULT);
  146.         }
  147.         return config;
  148.     }
  149.     protected MessageType getMessageTypeForError(IntegrationError integrationError){
  150.         IntegrationErrorConfiguration config = this.getConfigurationForError(integrationError);
  151.         return config.getMessageType(this.requestInfo.getProtocolContext(), this.serviceBinding, this.requestMessageType);
  152.     }

  153.     protected IntegrationErrorReturnConfiguration getReturnConfigForError(IntegrationError integrationError){
  154.         IntegrationErrorConfiguration config = this.getConfigurationForError(integrationError);
  155.         IntegrationErrorReturnConfiguration configR = config.getErrorReturnConfig();
  156.         boolean portaDelegata = (this instanceof RicezioneContenutiApplicativiInternalErrorGenerator);
  157.         if(portaDelegata) {
  158.             configR.setRetryAfterSeconds(this.openspcoopProperties.getServiceUnavailableRetryAfterSecondsPD());
  159.             configR.setRetryRandomBackoffSeconds(this.openspcoopProperties.getServiceUnavailableRetryAfterSecondsRandomBackoffPD());
  160.         }
  161.         else {
  162.             configR.setRetryAfterSeconds(this.openspcoopProperties.getServiceUnavailableRetryAfterSecondsPA());
  163.             configR.setRetryRandomBackoffSeconds(this.openspcoopProperties.getServiceUnavailableRetryAfterSecondsRandomBackoffPA());
  164.         }
  165.         return configR;
  166.     }
  167.    
  168.     protected boolean isUseInternalFault(IntegrationError integrationError){
  169.         IntegrationErrorConfiguration config = this.getConfigurationForError(integrationError);
  170.         return config.isUseInternalFault();
  171.     }
  172.    
  173.     protected ConfigurationRFC7807 getRfc7807ForError(IntegrationError integrationError){
  174.         IntegrationErrorConfiguration config = this.getConfigurationForError(integrationError);
  175.         return config.getRfc7807();
  176.     }

  177.     protected MessageType getMessageTypeForErrorSafeMode(IntegrationError integrationError){
  178.         MessageType msgTypeErrorResponse = null;
  179.         try{
  180.             msgTypeErrorResponse = this.requestMessageType;
  181.             if(this.forceMessageTypeResponse!=null){
  182.                 msgTypeErrorResponse = this.forceMessageTypeResponse;
  183.             }
  184.             else{
  185.                 msgTypeErrorResponse = getMessageTypeForError(integrationError);
  186.             }
  187.         }catch(Exception eError){
  188.             // non dovrebbero avvenire errori, altrimenti si utilizza il message type della richiesta
  189.         }
  190.         return msgTypeErrorResponse;
  191.     }
  192.    
  193.     protected ConfigurationRFC7807 getRfc7807ForErrorSafeMode(IntegrationError integrationError){
  194.         ConfigurationRFC7807 rfc7807 = null;
  195.         try{
  196.             rfc7807 = getRfc7807ForError(integrationError);
  197.         }catch(Exception eError){
  198.             // non dovrebbero avvenire errori, altrimenti si utilizza il message type della richiesta
  199.         }
  200.         return rfc7807;
  201.     }
  202.    
  203.     public static IntegrationFunctionError getIntegrationInternalError(Context context) {
  204.         IntegrationFunctionError integrationError = IntegrationFunctionError.INTERNAL_REQUEST_ERROR;
  205.         if(context!=null && context.containsKey(Costanti.RICHIESTA_INOLTRATA_BACKEND)) {
  206.             Object o = context.getObject(Costanti.RICHIESTA_INOLTRATA_BACKEND);
  207.             if(o!=null && o instanceof String) {
  208.                 String s = (String) o;
  209.                 if(Costanti.RICHIESTA_INOLTRATA_BACKEND_VALORE.equals(s)) {
  210.                     integrationError = IntegrationFunctionError.INTERNAL_RESPONSE_ERROR;
  211.                 }
  212.             }
  213.         }
  214.         return integrationError;
  215.     }
  216.    
  217.     private FaultBuilderConfig getFaultBuilderConfig(ErroriProperties erroriProperties, IntegrationFunctionError integrationFunctionError,
  218.             MessageType msgTypeErrorResponse, boolean useProblemRFC7807, String errore) {
  219.         try {
  220.             FaultBuilderConfig config = new FaultBuilderConfig();
  221.             IntegrationError integrationError = erroriProperties.getIntegrationError(integrationFunctionError);
  222.             IntegrationErrorReturnConfiguration returnConfig = this.getReturnConfigForError(integrationError);
  223.            
  224.             config.setHttpReturnCode(returnConfig.getHttpReturnCode());
  225.             config.setGovwayReturnCode(returnConfig.getGovwayReturnCode());
  226.            
  227.             config.setRfc7807Type(erroriProperties.isTypeEnabled());
  228.             config.setRfc7807WebSite(erroriProperties.getWebSite(integrationFunctionError));
  229.            
  230.             if(MessageType.SOAP_11.equals(msgTypeErrorResponse) || MessageType.SOAP_12.equals(msgTypeErrorResponse) || !useProblemRFC7807) {
  231.                 config.setActor(this.openspcoopProperties.getProprietaGestioneErrorePD(this.protocolFactory.createProtocolManager()).getFaultActor());
  232.             }
  233.            
  234.             String codiceEccezioneGW = org.openspcoop2.protocol.basic.Costanti.getTransactionSoapFaultCode(returnConfig.getGovwayReturnCode(),erroriProperties.getErrorType(integrationFunctionError));
  235.             if(MessageType.SOAP_11.equals(msgTypeErrorResponse) || MessageType.SOAP_12.equals(msgTypeErrorResponse)) {
  236.                 if(MessageType.SOAP_11.equals(msgTypeErrorResponse)) {
  237.                     String code11 = org.openspcoop2.message.constants.Costanti.SOAP11_FAULT_CODE_SERVER;
  238.                     if(returnConfig.getGovwayReturnCode()<=499) {
  239.                         code11 = org.openspcoop2.message.constants.Costanti.SOAP11_FAULT_CODE_CLIENT;
  240.                     }
  241.                     codiceEccezioneGW = code11 +
  242.                             org.openspcoop2.message.constants.Costanti.SOAP11_FAULT_CODE_SEPARATOR+codiceEccezioneGW;
  243.                 }
  244.                 QName eccezioneNameGovway = null;
  245.                 if(MessageType.SOAP_11.equals(msgTypeErrorResponse)) {
  246.                     String prefix = "SOAP-ENV";
  247.                     eccezioneNameGovway = new QName(org.openspcoop2.message.constants.Costanti.SOAP_ENVELOPE_NAMESPACE, codiceEccezioneGW, prefix);
  248.                     config.setPrefixSoap(prefix);
  249.                 }
  250.                 else {
  251.                     eccezioneNameGovway = this.getProtocolFactory().createErroreApplicativoBuilder().getQNameEccezioneIntegrazione(this.openspcoopProperties.getProprietaGestioneErrorePD(this.protocolFactory.createProtocolManager()).getDefaultFaultCodeIntegrationNamespace(),
  252.                             codiceEccezioneGW);
  253.                 }
  254.                 config.setErrorCode(eccezioneNameGovway);
  255.             }
  256.            
  257.             boolean genericDetails = returnConfig.isGenericDetails();
  258.             if(!genericDetails && erroriProperties.isForceGenericDetails(integrationFunctionError)) {
  259.                 genericDetails = true;
  260.             }
  261.             if (org.openspcoop2.protocol.basic.Costanti.isTRANSACTION_FORCE_SPECIFIC_ERROR_DETAILS()) {
  262.                 genericDetails = false;
  263.             }
  264.             if(errore!=null && !"null".equals(errore) && !genericDetails) {
  265.                 config.setDetails(errore);
  266.             }
  267.             else {
  268.                 config.setDetails(erroriProperties.getGenericDetails(integrationFunctionError));
  269.             }
  270.            
  271.             String govwayType = erroriProperties.getErrorType(integrationFunctionError);
  272.             config.setHeaderErrorTypeName(this.openspcoopProperties.getErroriHttpHeaderGovWayType());
  273.             config.setHeaderErrorTypeValue(govwayType);
  274.            
  275.             if(org.openspcoop2.protocol.basic.Costanti.isPROBLEM_RFC7807_ENRICH_TITLE_AS_GOVWAY_TYPE()) {
  276.                 if(org.openspcoop2.protocol.basic.Costanti.isPROBLEM_RFC7807_ENRICH_TITLE_AS_GOVWAY_TYPE_CAMEL_CASE_DECODE()) {
  277.                     config.setRfc7807Title(StringUtils.join(
  278.                              StringUtils.splitByCharacterTypeCamelCase(govwayType),
  279.                              ' '));
  280.                 }
  281.                 else {
  282.                     config.setRfc7807Title(govwayType);
  283.                 }
  284.                
  285.                 if(org.openspcoop2.protocol.basic.Costanti.isPROBLEM_RFC7807_ENRICH_TITLE_AS_GOVWAY_TYPE_CUSTOM_CLAIM()) {
  286.                     config.setRfc7807GovWayTypeHeaderErrorTypeName(org.openspcoop2.protocol.basic.Costanti.getPROBLEM_RFC7807_GOVWAY_TYPE());
  287.                     config.setRfc7807GovWayTypeHeaderErrorTypeValue(govwayType);
  288.                 }
  289.             }
  290.             else {
  291.                 config.setRfc7807GovWayTypeHeaderErrorTypeName(org.openspcoop2.protocol.basic.Costanti.getPROBLEM_RFC7807_GOVWAY_TYPE());
  292.                 config.setRfc7807GovWayTypeHeaderErrorTypeValue(govwayType);
  293.             }
  294.            
  295.             return config;
  296.         }catch(Throwable t) {
  297.             OpenSPCoop2Logger.getLoggerOpenSPCoopCore().error("FaultBuilderConfig non costruibile: "+t.getMessage(),t);
  298.             return null;
  299.         }
  300.     }
  301.    
  302.     protected IntegrationError convertToIntegrationError(IntegrationFunctionError integrationFunctionError) {
  303.         ErroriProperties erroriProperties = null;
  304.         IntegrationError integrationError = null;
  305.         try {
  306.             erroriProperties = ErroriProperties.getInstance(this.log);
  307.             integrationError = erroriProperties.getIntegrationError(integrationFunctionError);
  308.         }catch(Throwable t) {
  309.             integrationError = IntegrationError.INTERNAL_REQUEST_ERROR;
  310.         }
  311.         return integrationError;
  312.     }
  313.    
  314.     public OpenSPCoop2Message buildFault(Throwable e, Context context){
  315.         IntegrationFunctionError integrationFunctionError = getIntegrationInternalError(context);
  316.         return _buildFault(e, context, integrationFunctionError);
  317.     }
  318.     public OpenSPCoop2Message buildFault(Throwable e, Context context, IntegrationFunctionError integrationFunctionError){
  319.         return _buildFault(e, context, integrationFunctionError);
  320.     }
  321.     public OpenSPCoop2Message _buildFault(Throwable e, Context context, IntegrationFunctionError integrationFunctionError){
  322.         //this.log.error(e.getMessage(),e);
  323.         ErroriProperties erroriProperties = null;
  324.         IntegrationError integrationError = null;
  325.         try {
  326.             erroriProperties = ErroriProperties.getInstance(this.log);
  327.             integrationError = erroriProperties.getIntegrationError(integrationFunctionError);
  328.         }catch(Throwable t) {
  329.             integrationError = IntegrationError.INTERNAL_REQUEST_ERROR;
  330.         }
  331.         boolean useProblemRFC7807 = this.getRfc7807ForErrorSafeMode(integrationError)!=null;
  332.         MessageType msgTypeErrorResponse = this.getMessageTypeForErrorSafeMode(integrationError);
  333.         FaultBuilderConfig config = null;
  334.         if(erroriProperties!=null) {
  335.             config = getFaultBuilderConfig(erroriProperties, integrationFunctionError, msgTypeErrorResponse, useProblemRFC7807, e!=null ? e.getMessage() : null);
  336.         }
  337.         OpenSPCoop2Message msgFault = OpenSPCoop2MessageFactory.getDefaultMessageFactory().createFaultMessage(msgTypeErrorResponse, useProblemRFC7807, config, e);
  338.         return msgFault;
  339.     }
  340.    

  341.     public OpenSPCoop2Message buildFault(String errore, Context context){
  342.         IntegrationFunctionError integrationFunctionError = getIntegrationInternalError(context);
  343.         return _buildFault(errore, context, integrationFunctionError);
  344.     }
  345.     public OpenSPCoop2Message buildFault(String errore, Context context, IntegrationFunctionError integrationFunctionError){
  346.         return _buildFault(errore, context, integrationFunctionError);
  347.     }
  348.    
  349.     private OpenSPCoop2Message _buildFault(String errore, Context context, IntegrationFunctionError integrationFunctionError){
  350.         //this.log.error(errore);
  351.         ErroriProperties erroriProperties = null;
  352.         IntegrationError integrationError = null;
  353.         try {
  354.             erroriProperties = ErroriProperties.getInstance(this.log);
  355.             integrationError = erroriProperties.getIntegrationError(integrationFunctionError);
  356.         }catch(Throwable t) {
  357.             integrationError = IntegrationError.INTERNAL_REQUEST_ERROR;
  358.             integrationFunctionError = IntegrationFunctionError.INTERNAL_REQUEST_ERROR;
  359.         }
  360.         boolean useProblemRFC7807 = this.getRfc7807ForErrorSafeMode(integrationError)!=null;
  361.         MessageType msgTypeErrorResponse = this.getMessageTypeForErrorSafeMode(integrationError);
  362.         FaultBuilderConfig config = null;
  363.         if(erroriProperties!=null) {
  364.             config = getFaultBuilderConfig(erroriProperties, integrationFunctionError, msgTypeErrorResponse, useProblemRFC7807, errore);
  365.         }
  366.         OpenSPCoop2Message msgFault = OpenSPCoop2MessageFactory.getDefaultMessageFactory().createFaultMessage(msgTypeErrorResponse, useProblemRFC7807, config, errore);
  367.         return msgFault;
  368.     }
  369. }