ErroreIntegrazione.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.sdk.constants;

  21. import java.io.Serializable;
  22. import java.util.ArrayList;
  23. import java.util.List;

  24. import org.openspcoop2.message.soap.SOAPFaultCode;
  25. import org.openspcoop2.protocol.sdk.IProtocolFactory;
  26. import org.openspcoop2.protocol.sdk.ProtocolException;

  27. /**
  28. *
  29. * @author Poli Andrea (apoli@link.it)
  30. * @author $Author$
  31. * @version $Rev$, $Date$
  32. */
  33. public class ErroreIntegrazione implements Serializable  {

  34.     /**
  35.      *
  36.      */
  37.     private static final long serialVersionUID = 1L;
  38.    
  39.     private String descrizione;
  40.     private CodiceErroreIntegrazione codiceErrore;
  41.     private List<KeyValueObject> keyValueObjects = new ArrayList<KeyValueObject>();
  42.     private final SOAPFaultCode soapFaultCode;
  43.    
  44.     private String codiceCustom;
  45.     public String getCodiceCustom() {
  46.         return this.codiceCustom;
  47.     }
  48.     public void setCodiceCustom(String codiceCustom) {
  49.         this.codiceCustom = codiceCustom;
  50.     }
  51.    
  52.     public ErroreIntegrazione(String descrizione, CodiceErroreIntegrazione codiceErrore, KeyValueObject ... keyValueObjects){
  53.         this.descrizione = descrizione;
  54.         this.codiceErrore = codiceErrore;
  55.         if(keyValueObjects!=null){
  56.             for (int i = 0; i < keyValueObjects.length; i++) {
  57.                 this.keyValueObjects.add(keyValueObjects[i]);
  58.             }
  59.         }
  60.         this.soapFaultCode = null;
  61.     }
  62.    
  63.     public ErroreIntegrazione(String descrizione, CodiceErroreIntegrazione codiceErrore, SOAPFaultCode soapFaultCode, KeyValueObject ... keyValueObjects){
  64.         this.descrizione = descrizione;
  65.         this.codiceErrore = codiceErrore;
  66.         if(keyValueObjects!=null){
  67.             for (int i = 0; i < keyValueObjects.length; i++) {
  68.                 this.keyValueObjects.add(keyValueObjects[i]);
  69.             }
  70.         }
  71.         this.soapFaultCode = soapFaultCode;
  72.     }
  73.    
  74.     // metodo senza protocol factory utilizzato dai traduttori
  75.     public String getDescrizioneRawValue() {
  76.         return replaceAll(this.descrizione);
  77.     }
  78.     public String getDescrizione(IProtocolFactory<?> protocolFactory) throws ProtocolException {
  79.         return replaceAll(protocolFactory.createTraduttore().toString(this));
  80.     }
  81.     protected void setDescrizione(String descrizione) {
  82.         this.descrizione = descrizione;
  83.     }

  84.     public CodiceErroreIntegrazione getCodiceErrore() {
  85.         return this.codiceErrore;
  86.     }
  87.     protected void setCodiceErrore(CodiceErroreIntegrazione codiceErrore) {
  88.         this.codiceErrore = codiceErrore;
  89.     }

  90.     public SOAPFaultCode getSoapFaultCode() {
  91.         return this.soapFaultCode;
  92.     }

  93.     public List<KeyValueObject> getKeyValueObjects() {
  94.         return this.keyValueObjects;
  95.     }
  96.     protected void addKeyValueObject(KeyValueObject o){
  97.         this.keyValueObjects.add(o);
  98.     }
  99.    
  100.     private String replaceAll(String v){
  101.         if(v==null){
  102.             return null;
  103.         }
  104.         String tmp = new String(v);
  105.         for (int i = 0; i < this.keyValueObjects.size(); i++) {
  106.             KeyValueObject keyValue = this.keyValueObjects.get(i);
  107.             String key = keyValue.getKey();
  108.             int limite = 100;
  109.             int index = 0;
  110.             while(tmp.contains(key) && index<limite){
  111.                 tmp = tmp.replace(key, keyValue.getValue());
  112.                 index++;
  113.             }
  114.         }
  115.         return tmp;
  116.     }
  117.    
  118.     @Override
  119.     public String toString(){
  120.         try{
  121.             return toString(null);
  122.         }catch(Exception e){
  123.             throw new RuntimeException(e.getMessage(),e);
  124.         }
  125.     }
  126.     public String toString(IProtocolFactory<?> protocolFactory) throws ProtocolException{
  127.         StringBuilder bf = new StringBuilder();
  128.        
  129.         if(this.descrizione!=null){
  130.             bf.append(" descrizione errore: ");
  131.             if(protocolFactory==null){
  132.                 bf.append(this.getDescrizioneRawValue());
  133.             }else{
  134.                 bf.append(this.getDescrizione(protocolFactory));
  135.             }
  136.         }
  137.        
  138.         if(this.codiceErrore!=null){
  139.             bf.append(" codice errore: ");
  140.             if(protocolFactory==null)
  141.                 bf.append(this.codiceErrore.toString());
  142.             else
  143.                 bf.append(protocolFactory.createTraduttore().toString(this.codiceErrore,null,false));
  144.         }
  145.        
  146.         if(bf.length()>0)
  147.             return bf.toString().substring(1);
  148.         else
  149.             return null;
  150.     }
  151. }