ErroreApplicativoMessageUtils.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.builder;

  21. import java.util.Iterator;

  22. import javax.xml.namespace.QName;
  23. import javax.xml.soap.Detail;
  24. import javax.xml.soap.Name;
  25. import javax.xml.soap.SOAPBody;
  26. import javax.xml.soap.SOAPElement;
  27. import javax.xml.soap.SOAPFactory;
  28. import javax.xml.soap.SOAPFault;

  29. import org.openspcoop2.core.eccezione.router_details.Dettaglio;
  30. import org.openspcoop2.core.eccezione.router_details.DettaglioRouting;
  31. import org.openspcoop2.core.id.IDSoggetto;
  32. import org.openspcoop2.message.OpenSPCoop2Message;
  33. import org.openspcoop2.message.constants.MessageType;
  34. import org.openspcoop2.message.soap.SoapUtils;
  35. import org.openspcoop2.protocol.sdk.ProtocolException;
  36. import org.openspcoop2.utils.date.DateManager;
  37. import org.openspcoop2.utils.xml.AbstractXMLUtils;
  38. import org.slf4j.Logger;
  39. import org.w3c.dom.Element;

  40. /**
  41.  * ErroreApplicativoMessageUtils
  42.  *
  43.  * @author Poli Andrea (apoli@link.it)
  44.  * @author $Author$
  45.  * @version $Rev$, $Date$
  46.  */

  47. public class ErroreApplicativoMessageUtils {

  48.     public static void addPrefixToElement(Element elementErroreApplicativo,String prefix){
  49.         //workAround per ovviare poi al problema axiom: NAMESPACE_ERR: An attempt is made to create or change an object in a way which is incorrect with regard to namespaces.
  50.         // che si verifica in detailFaultPulito.addChildElement(eccezioneDetailApplicativo) del metodo insertErroreApplicativoIntoSOAPFault_engine di questa classe
  51.         //elementErroreApplicativo.setPrefix(prefix);
  52.     }
  53.    

  54.    
  55.     public static void addErroreApplicativoIntoSOAPFaultDetail(SOAPElement erroreApplicativo,
  56.             OpenSPCoop2Message msg, Logger log) throws ProtocolException{
  57.        
  58.         try{
  59.             if(msg==null)
  60.                 throw new ProtocolException("Messaggio non presente");
  61.             SOAPBody soapBody = msg.castAsSoap().getSOAPBody();
  62.             if(soapBody==null)
  63.                 throw new ProtocolException("SOAPBody non presente");
  64.             SOAPFault fault = null;
  65.             if(soapBody.hasFault()==false)
  66.                 throw new ProtocolException("SOAPFault non presente");
  67.             else
  68.                 fault = soapBody.getFault();
  69.             if(fault==null)
  70.                 throw new ProtocolException("SOAPFault is null");
  71.            
  72.             Detail detail = fault.getDetail();
  73.             if(detail==null){
  74.                 detail = fault.addDetail();
  75.                 detail = fault.getDetail();
  76.             }
  77.             if(detail!=null){
  78.                 detail.addChildElement(erroreApplicativo);
  79.             }
  80.            
  81.             msg.saveChanges();
  82.            
  83.         }catch(Exception e){
  84.             log.error("Errore durante la costruzione del messaggio di errore applicativo (InsertDetail)",e);
  85.             throw new ProtocolException("Errore durante la costruzione del messaggio di errore (InsertDetail)",e);
  86.         }
  87.     }
  88.    
  89.     @Deprecated
  90.     public static void insertErroreApplicativoIntoSOAPFault(SOAPElement erroreApplicativo,
  91.             OpenSPCoop2Message msg, Logger log) throws ProtocolException{
  92.         try{
  93.             if(msg==null)
  94.                 throw new ProtocolException("Messaggio non presente");
  95.             SOAPBody soapBody = msg.castAsSoap().getSOAPBody();
  96.             if(soapBody==null)
  97.                 throw new ProtocolException("SOAPBody non presente");
  98.             SOAPFault faultOriginale = null;
  99.             if(soapBody.hasFault()==false)
  100.                 throw new ProtocolException("SOAPFault non presente");
  101.             else
  102.                 faultOriginale = soapBody.getFault();
  103.             if(faultOriginale==null)
  104.                 throw new ProtocolException("SOAPFault is null");
  105.                        
  106.             QName nameDetail = null;
  107.             if(MessageType.SOAP_12.equals(msg.getMessageType())){
  108.                 nameDetail = new QName(org.openspcoop2.message.constants.Costanti.SOAP12_ENVELOPE_NAMESPACE,"Detail");
  109.             }
  110.             else{
  111.                 nameDetail = new QName("detail");
  112.             }
  113.             SOAPElement detailsFaultOriginale = null;
  114.             Iterator<?> itDetailsOriginali = faultOriginale.getChildElements(nameDetail);
  115.             if(itDetailsOriginali!=null && itDetailsOriginali.hasNext()){
  116.                 detailsFaultOriginale = (SOAPElement) itDetailsOriginali.next();
  117.             }
  118.                    
  119.             String faultActor = faultOriginale.getFaultActor(); // in soap1.2 e' il role
  120.             Name faultCode = faultOriginale.getFaultCodeAsName();
  121.             Iterator<?> faultSubCode = null;
  122.             String faultNode = null;
  123.             if(MessageType.SOAP_12.equals(msg.getMessageType())){
  124.                 faultSubCode = faultOriginale.getFaultSubcodes();
  125.                 faultNode = faultOriginale.getFaultNode();
  126.             }
  127.             String faultString = faultOriginale.getFaultString();
  128.            
  129.            
  130.             soapBody.removeChild(soapBody.getFault());
  131.            
  132.             //msg.saveChanges();
  133.            
  134.             SOAPFault faultPulito = soapBody.addFault();
  135.             if(faultActor != null)
  136.                 faultPulito.setFaultActor(faultActor);
  137.             if(faultCode!=null)
  138.                 faultPulito.setFaultCode(faultCode);
  139.             if(faultSubCode!=null){
  140.                 while (faultSubCode.hasNext()) {
  141.                     QName faultSubCodeQname = (QName) faultSubCode.next();
  142.                     faultPulito.appendFaultSubcode(faultSubCodeQname);
  143.                 }
  144.             }
  145.             if(faultNode!=null){
  146.                 faultPulito.setFaultNode(faultNode);
  147.             }
  148.             if(faultString!=null)
  149.                 SoapUtils.setFaultString(faultPulito , faultString);
  150.             Detail detailFaultPulito = faultPulito.addDetail();
  151.             detailFaultPulito = faultPulito.getDetail();
  152.        
  153.             if(detailsFaultOriginale!=null){
  154.                 Iterator<?> it = detailsFaultOriginale.getChildElements();
  155.                 while (it.hasNext()) {
  156.                     Object o = it.next();
  157.                     if(o instanceof SOAPElement){
  158.                         SOAPElement elem = (SOAPElement) o;
  159.                         detailFaultPulito.addChildElement(elem);
  160.                     }
  161.                 }
  162.             }
  163.             detailFaultPulito.addChildElement(erroreApplicativo);
  164.        
  165.             msg.saveChanges();
  166.            
  167.         }catch(Exception e){
  168.             log.error("Errore durante la costruzione del messaggio di errore applicativo (InsertDetail)",e);
  169.             throw new ProtocolException("Errore durante la costruzione del messaggio di errore (InsertDetail)",e);
  170.         }
  171.     }
  172.    
  173.    
  174.     public static void insertRoutingErrorInSOAPFault(IDSoggetto identitaRouter,String idFunzione,String msgErrore,OpenSPCoop2Message msg,
  175.             Logger log, AbstractXMLUtils xmlUtils) throws ProtocolException{
  176.        
  177.         try{
  178.             if(msg==null)
  179.                 throw new ProtocolException("Messaggio non presente");
  180.            
  181.             DettaglioRouting dettaglioRouting = new DettaglioRouting();
  182.            
  183.            
  184.             // dominio
  185.             org.openspcoop2.core.eccezione.router_details.Dominio dominio = new org.openspcoop2.core.eccezione.router_details.Dominio();
  186.             org.openspcoop2.core.eccezione.router_details.DominioSoggetto dominioSoggetto = new org.openspcoop2.core.eccezione.router_details.DominioSoggetto();
  187.             dominioSoggetto.setType(identitaRouter.getTipo());
  188.             dominioSoggetto.setBase(identitaRouter.getNome());
  189.             dominio.setOrganization(dominioSoggetto);
  190.             dominio.setId(identitaRouter.getCodicePorta());
  191.             dominio.setModule(idFunzione);
  192.             dettaglioRouting.setDomain(dominio);
  193.            
  194.             // oraRegistrazione
  195.             dettaglioRouting.setTimestamp(DateManager.getDate());
  196.            
  197.             // dettaglio
  198.             Dettaglio dettaglio = new Dettaglio();
  199.             dettaglio.setDescription(msgErrore);
  200.             dettaglio.setState(org.openspcoop2.core.eccezione.router_details.constants.Costanti.ESITO_ERRORE);
  201.             dettaglioRouting.setDetail(dettaglio);
  202.            
  203.             byte[]xmlDettaglioRouting = org.openspcoop2.core.eccezione.router_details.utils.XMLUtils.generateDettaglioRouting(dettaglioRouting);
  204.             Element elementDettaglioRouting = xmlUtils.newElement(xmlDettaglioRouting);
  205.             addPrefixToElement(elementDettaglioRouting,"op2RoutingDetail");
  206.            
  207.             SOAPFactory sf = SoapUtils.getSoapFactory(msg.getFactory(), msg.getMessageType());
  208.             SOAPElement dettaglioRoutingElementSOAP =  sf.createElement(elementDettaglioRouting);
  209.            
  210.             addErroreApplicativoIntoSOAPFaultDetail(dettaglioRoutingElementSOAP, msg, log);
  211.         }catch(Exception e){
  212.             log.error("Errore durante la costruzione del messaggio di errore",e);
  213.             throw new ProtocolException("Errore durante la costruzione del messaggio di errore",e);
  214.         }
  215.     }
  216.    
  217. }