WSAddressingUtilities.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.message.soap.wsaddressing;

  21. import java.util.HashMap;

  22. import javax.xml.namespace.QName;
  23. import javax.xml.soap.SOAPHeader;
  24. import javax.xml.soap.SOAPHeaderElement;

  25. import org.apache.commons.lang.StringUtils;
  26. import org.openspcoop2.message.OpenSPCoop2MessageFactory;
  27. import org.openspcoop2.message.OpenSPCoop2SoapMessage;
  28. import org.openspcoop2.message.exception.MessageException;
  29. import org.openspcoop2.message.soap.SoapUtils;
  30. import org.openspcoop2.message.xml.ValidatoreXSD;
  31. import org.slf4j.Logger;


  32. /**
  33.  * Classe contenenti utilities per le integrazioni.
  34.  *
  35.  * @author Poli Andrea (apoli@link.it)
  36.  * @author $Author$
  37.  * @version $Rev$, $Date$
  38.  */
  39. public class WSAddressingUtilities {

  40.     private Logger log;
  41.     public WSAddressingUtilities(Logger log) {
  42.         this.log = log;
  43.     }
  44.    
  45.    
  46.    
  47.    
  48.     // ***** BUILD *****
  49.    
  50.     public WSAddressingHeader build(OpenSPCoop2SoapMessage msg,String actor,  
  51.             WSAddressingValue values) throws MessageException {
  52.         return this.build(msg, actor, false, values);
  53.     }
  54.     public WSAddressingHeader build(OpenSPCoop2SoapMessage msg,String actor, boolean mustUnderstand,
  55.             WSAddressingValue values) throws MessageException {
  56.         WSAddressingHeader hdr = new WSAddressingHeader();
  57.         if(!StringUtils.isEmpty(values.getTo())) {
  58.             hdr.setTo(buildWSATo(msg, actor, mustUnderstand, values.getTo()));
  59.         }
  60.         if(!StringUtils.isEmpty(values.getFrom())) {
  61.             hdr.setFrom(buildWSAFrom(msg, actor, mustUnderstand, values.getFrom()));
  62.         }
  63.         if(!StringUtils.isEmpty(values.getAction())) {
  64.             hdr.setAction(buildWSAAction(msg, actor, mustUnderstand, values.getAction()));
  65.         }
  66.         if(!StringUtils.isEmpty(values.getId())) {
  67.             hdr.setId(buildWSAID(msg, actor, mustUnderstand, values.getId()));
  68.         }
  69.         if(!StringUtils.isEmpty(values.getRelatesTo())) {
  70.             hdr.setRelatesTo(buildWSARelatesTo(msg, actor, mustUnderstand, values.getRelatesTo()));
  71.         }
  72.         if(!StringUtils.isEmpty(values.getReplyTo())) {
  73.             hdr.setReplyTo(buildWSAReplyTo(msg, actor, mustUnderstand, values.getReplyTo()));
  74.         }
  75.         if(!StringUtils.isEmpty(values.getFaultTo())) {
  76.             hdr.setFaultTo(buildWSAFaultTo(msg, actor, mustUnderstand, values.getFaultTo()));
  77.         }
  78.         return hdr;
  79.     }
  80.    
  81.     public void addHeader(WSAddressingHeader headerWSAddressing, OpenSPCoop2SoapMessage msg) throws MessageException{
  82.        
  83.         try {
  84.        
  85.             if(headerWSAddressing!=null && msg!=null) {
  86.                
  87.                 SOAPHeader header = msg.getSOAPHeader();
  88.                 if(header==null){
  89.                     header = msg.getSOAPPart().getEnvelope().addHeader();
  90.                 }
  91.                
  92.                 if(headerWSAddressing.getTo()!=null) {
  93.                     msg.addHeaderElement(header, headerWSAddressing.getTo());
  94.                 }
  95.                 if(headerWSAddressing.getFrom()!=null) {
  96.                     msg.addHeaderElement(header, headerWSAddressing.getFrom());
  97.                 }
  98.                 if(headerWSAddressing.getAction()!=null) {
  99.                     msg.addHeaderElement(header, headerWSAddressing.getAction());
  100.                 }
  101.                 if(headerWSAddressing.getId()!=null) {
  102.                     msg.addHeaderElement(header, headerWSAddressing.getId());
  103.                 }
  104.                 if(headerWSAddressing.getRelatesTo()!=null) {
  105.                     msg.addHeaderElement(header, headerWSAddressing.getRelatesTo());
  106.                 }
  107.                 if(headerWSAddressing.getReplyTo()!=null) {
  108.                     msg.addHeaderElement(header, headerWSAddressing.getReplyTo());
  109.                 }
  110.                 if(headerWSAddressing.getFaultTo()!=null) {
  111.                     msg.addHeaderElement(header, headerWSAddressing.getFaultTo());
  112.                 }
  113.             }
  114.            
  115.         }catch(Exception e) {
  116.             throw new MessageException(e.getMessage(),e);
  117.         }
  118.        
  119.     }
  120.    
  121.    
  122.    
  123.     // ***** BUILD STATIC UTILS *****
  124.    
  125.     private static final boolean BUILD_VALUE_AS_EPR = true;
  126.     private static final boolean BUILD_VALUE_RAW = false;
  127.    
  128.     public static SOAPHeaderElement buildWSATo(OpenSPCoop2SoapMessage msg,String actor,boolean mustUnderstand, String value) throws MessageException{
  129.         QName name =  new QName(Costanti.WSA_NAMESPACE,Costanti.WSA_SOAP_HEADER_TO,Costanti.WSA_PREFIX);
  130.         SOAPHeaderElement header = WSAddressingUtilities.buildHeaderElement(msg,name,
  131.                 value,actor,mustUnderstand, BUILD_VALUE_RAW);
  132.         return header;
  133.     }
  134.    
  135.     public static SOAPHeaderElement buildWSAFrom(OpenSPCoop2SoapMessage msg,String actor,boolean mustUnderstand, String value) throws MessageException{
  136.         QName name =  new QName(Costanti.WSA_NAMESPACE,Costanti.WSA_SOAP_HEADER_FROM,Costanti.WSA_PREFIX);
  137.         SOAPHeaderElement header = WSAddressingUtilities.buildHeaderElement(msg,name,
  138.                 value,actor,mustUnderstand, BUILD_VALUE_AS_EPR);
  139.         return header;
  140.     }
  141.    
  142.     public static SOAPHeaderElement buildWSAAction(OpenSPCoop2SoapMessage msg,String actor,boolean mustUnderstand, String value) throws MessageException{
  143.         QName name =  new QName(Costanti.WSA_NAMESPACE,Costanti.WSA_SOAP_HEADER_ACTION,Costanti.WSA_PREFIX);
  144.         SOAPHeaderElement header = WSAddressingUtilities.buildHeaderElement(msg,name,
  145.                 value,actor,mustUnderstand, BUILD_VALUE_RAW);
  146.         return header;
  147.     }
  148.    
  149.     public static SOAPHeaderElement buildWSAID(OpenSPCoop2SoapMessage msg,String actor,boolean mustUnderstand, String value) throws MessageException{
  150.         QName name =  new QName(Costanti.WSA_NAMESPACE,Costanti.WSA_SOAP_HEADER_ID,Costanti.WSA_PREFIX);
  151.         SOAPHeaderElement header = WSAddressingUtilities.buildHeaderElement(msg,name,
  152.                 value,actor,mustUnderstand, BUILD_VALUE_RAW);
  153.         return header;
  154.     }
  155.    
  156.     public static SOAPHeaderElement buildWSARelatesTo(OpenSPCoop2SoapMessage msg,String actor, boolean mustUnderstand, String value) throws MessageException{
  157.         QName name =  new QName(Costanti.WSA_NAMESPACE,Costanti.WSA_SOAP_HEADER_RELATES_TO,Costanti.WSA_PREFIX);
  158.         SOAPHeaderElement header = WSAddressingUtilities.buildHeaderElement(msg,name,
  159.                 value,actor,mustUnderstand, BUILD_VALUE_RAW);
  160.         return header;
  161.     }
  162.    
  163.     public static SOAPHeaderElement buildWSAReplyTo(OpenSPCoop2SoapMessage msg,String actor,boolean mustUnderstand, String value) throws MessageException{
  164.         QName name =  new QName(Costanti.WSA_NAMESPACE,Costanti.WSA_SOAP_HEADER_REPLY_TO,Costanti.WSA_PREFIX);
  165.         SOAPHeaderElement header = WSAddressingUtilities.buildHeaderElement(msg,name,
  166.                 value,actor,mustUnderstand, BUILD_VALUE_AS_EPR);
  167.         return header;
  168.     }
  169.    
  170.     public static SOAPHeaderElement buildWSAFaultTo(OpenSPCoop2SoapMessage msg,String actor,boolean mustUnderstand, String value) throws MessageException{
  171.         QName name =  new QName(Costanti.WSA_NAMESPACE,Costanti.WSA_SOAP_HEADER_FAULT_TO,Costanti.WSA_PREFIX);
  172.         SOAPHeaderElement header = WSAddressingUtilities.buildHeaderElement(msg,name,
  173.                 value,actor,mustUnderstand, BUILD_VALUE_AS_EPR);
  174.         return header;
  175.     }
  176.        
  177.     private static SOAPHeaderElement buildHeaderElement(OpenSPCoop2SoapMessage msg,QName name,String value,String actor,boolean mustUnderstand, boolean epr) throws MessageException{
  178.        
  179.         try {
  180.        
  181.             SOAPHeader hdr = msg.getSOAPHeader();
  182.             if(hdr==null){
  183.                 hdr = msg.getSOAPPart().getEnvelope().addHeader();
  184.             }
  185.             SOAPHeaderElement element = msg.newSOAPHeaderElement(hdr, name);
  186.             if(actor!=null) {
  187.                 element.setActor(actor);
  188.             }
  189.             element.setMustUnderstand(mustUnderstand);
  190.            
  191.             if(epr==false){
  192.                 element.setValue(value);
  193.             }
  194.             else{
  195.                 QName nameAddressEPR =  new QName(Costanti.WSA_NAMESPACE,Costanti.WSA_SOAP_HEADER_EPR_ADDRESS,Costanti.WSA_PREFIX);
  196.                 element.addChildElement(nameAddressEPR).setValue(value);
  197.             }
  198.             return element;
  199.            
  200.         }catch(Exception e) {
  201.             throw new MessageException(e.getMessage(),e);
  202.         }
  203.        
  204.     }
  205.    

  206.    
  207.    
  208.    
  209.    
  210.     // ***** VALIDATE *****
  211.    
  212.     private static HashMap<String, ValidatoreXSD> _validatoreXSDMap = new HashMap<>();
  213.     private static synchronized void _initValidatore(OpenSPCoop2MessageFactory messageFactory, Logger log){
  214.         String key = messageFactory.getClass().getName();
  215.         if(!_validatoreXSDMap.containsKey(key)){
  216.             try{
  217.                 ValidatoreXSD val = new ValidatoreXSD(messageFactory, log,WSAddressingUtilities.class.getResourceAsStream("/ws-addr.xsd"));
  218.                 _validatoreXSDMap.put(key, val);
  219.             }catch(Exception e){
  220.                 log.error("ws-addr.xsd, errore durante la costruzione del validatore xsd: "+e.getMessage(),e);
  221.             }
  222.         }
  223.     }
  224.     public ValidatoreXSD getValidatoreXSD(OpenSPCoop2MessageFactory messageFactory){
  225.         String key = messageFactory.getClass().getName();
  226.         if(!_validatoreXSDMap.containsKey(key)){
  227.             _initValidatore(messageFactory, this.log);
  228.         }
  229.         return _validatoreXSDMap.get(key);
  230.     }
  231.    
  232.    
  233.     public void validate(OpenSPCoop2SoapMessage message, WSAddressingHeader header) throws MessageException {
  234.         this.log.debug("Validazione XSD...");
  235.         _validaElementoWSA(getValidatoreXSD(message.getFactory()),header.getTo(),message);
  236.         _validaElementoWSA(getValidatoreXSD(message.getFactory()),header.getFrom(),message);
  237.         _validaElementoWSA(getValidatoreXSD(message.getFactory()),header.getAction(),message);
  238.         _validaElementoWSA(getValidatoreXSD(message.getFactory()),header.getId(),message);
  239.         _validaElementoWSA(getValidatoreXSD(message.getFactory()),header.getRelatesTo(),message);
  240.         _validaElementoWSA(getValidatoreXSD(message.getFactory()),header.getReplyTo(),message);
  241.         _validaElementoWSA(getValidatoreXSD(message.getFactory()),header.getFaultTo(),message);
  242.         this.log.debug("Validazione XSD effettuate");
  243.     }
  244.    
  245.     private void _validaElementoWSA(ValidatoreXSD validatoreXSD,SOAPHeaderElement headerElement,OpenSPCoop2SoapMessage msg) throws MessageException{
  246.         if(headerElement!=null){
  247.             try {
  248.                 this.log.debug("Validazione XSD ["+headerElement.getLocalName()+"]...");
  249.                 // validazione XSD
  250.                 if(validatoreXSD==null)
  251.                     throw new Exception("Validatore XSD non istanziato");
  252.                 validatoreXSD.valida(new java.io.ByteArrayInputStream(msg.getAsByte(headerElement, false)));
  253.             }catch(Exception e) {
  254.                 throw new MessageException(e.getMessage(),e);
  255.             }
  256.         }
  257.     }
  258.    
  259.    
  260.    
  261.    
  262.    
  263.    
  264.     // ***** READ *****
  265.    
  266.     public WSAddressingHeader read(OpenSPCoop2SoapMessage message, String actor) throws MessageException{
  267.         return this.read(message, actor, false);
  268.     }
  269.     public WSAddressingHeader read(OpenSPCoop2SoapMessage message, String actor, boolean validate) throws MessageException{
  270.         SOAPHeader header = null;
  271.         try{
  272.             header = message.getSOAPHeader();
  273.         }catch(Exception e){
  274.             throw new MessageException(e.getMessage(),e);
  275.         }
  276.         return this.read(message, header, actor, validate);
  277.     }
  278.     public WSAddressingHeader read(OpenSPCoop2SoapMessage message, SOAPHeader header, String actor) throws MessageException{
  279.         return this.read(message, header, actor, false);
  280.     }
  281.     public WSAddressingHeader read(OpenSPCoop2SoapMessage message, SOAPHeader header, String actor, boolean validate) throws MessageException{
  282.         try{

  283.             if(header==null){
  284.                 return null;
  285.             }
  286.            
  287.             SOAPHeaderElement wsaTO = null;
  288.             SOAPHeaderElement wsaFROM = null;
  289.             SOAPHeaderElement wsaAction = null;
  290.             SOAPHeaderElement wsaID = null;
  291.             SOAPHeaderElement wsaRelatesTo = null;
  292.             SOAPHeaderElement wsaReplyTo = null;
  293.             SOAPHeaderElement wsaFaultTo = null;
  294.            
  295.             java.util.Iterator<?> it = header.examineAllHeaderElements();
  296.             while( it.hasNext()  ){
  297.                 // Test Header Element
  298.                 SOAPHeaderElement headerElement = (SOAPHeaderElement) it.next();
  299.                
  300.                 //Controllo Namespace
  301.                 String namespace = headerElement.getNamespaceURI();
  302.                 String actorCheck = SoapUtils.getSoapActor(headerElement, message.getMessageType());
  303.                
  304.                 boolean actorCompatible = false;
  305.                 if(actor==null) {
  306.                     actorCompatible = (actorCheck==null);
  307.                 }
  308.                 else {
  309.                     actorCompatible = actor.equals(actorCheck);
  310.                 }
  311.                
  312.                 if(actorCompatible && Costanti.WSA_NAMESPACE.equals(namespace)){
  313.                    
  314.                     if(Costanti.WSA_SOAP_HEADER_TO.equals(headerElement.getLocalName())){
  315.                         wsaTO = headerElement;
  316.                     }
  317.                     else if(Costanti.WSA_SOAP_HEADER_FROM.equals(headerElement.getLocalName())){
  318.                         wsaFROM = headerElement;
  319.                     }
  320.                     else if(Costanti.WSA_SOAP_HEADER_ACTION.equals(headerElement.getLocalName())){
  321.                         wsaAction = headerElement;
  322.                     }
  323.                     else if(Costanti.WSA_SOAP_HEADER_ID.equals(headerElement.getLocalName())){
  324.                         wsaID = headerElement;
  325.                     }
  326.                     else if(Costanti.WSA_SOAP_HEADER_RELATES_TO.equals(headerElement.getLocalName())){
  327.                         wsaRelatesTo = headerElement;
  328.                     }
  329.                     else if(Costanti.WSA_SOAP_HEADER_REPLY_TO.equals(headerElement.getLocalName())){
  330.                         wsaReplyTo = headerElement;
  331.                     }
  332.                     else if(Costanti.WSA_SOAP_HEADER_FAULT_TO.equals(headerElement.getLocalName())){
  333.                         wsaFaultTo = headerElement;
  334.                     }
  335.                 }
  336.                
  337.             }
  338.            
  339.             WSAddressingHeader wsaHeader = new WSAddressingHeader();
  340.             wsaHeader.setTo(wsaTO);
  341.             wsaHeader.setFrom(wsaFROM);
  342.             wsaHeader.setAction(wsaAction);
  343.             wsaHeader.setId(wsaID);
  344.             wsaHeader.setRelatesTo(wsaRelatesTo);
  345.             wsaHeader.setReplyTo(wsaReplyTo);
  346.             wsaHeader.setFaultTo(wsaFaultTo);
  347.            
  348.             if(validate) {
  349.                 this.validate(message, wsaHeader);
  350.             }
  351.            
  352.             return wsaHeader;
  353.            
  354.         }catch(Exception e){
  355.             throw new MessageException(e.getMessage(),e);
  356.         }
  357.     }
  358.        
  359.    
  360.    
  361.    
  362.     // ***** DELETE *****
  363.    
  364.     public void delete(OpenSPCoop2SoapMessage message,String actor) throws MessageException{

  365.         try{

  366.             SOAPHeader header = message.getSOAPHeader();
  367.             if(header==null){
  368.                 return;
  369.             }
  370.            
  371.             SOAPHeaderElement wsaTO = null;
  372.             SOAPHeaderElement wsaFROM = null;
  373.             SOAPHeaderElement wsaAction = null;
  374.             SOAPHeaderElement wsaID = null;
  375.             SOAPHeaderElement wsaRelatesTo = null;
  376.             SOAPHeaderElement wsaReplyTo = null;
  377.             SOAPHeaderElement wsaFaultTo = null;
  378.            
  379.             java.util.Iterator<?> it = header.examineAllHeaderElements();
  380.             while( it.hasNext()  ){
  381.                 // Test Header Element
  382.                 SOAPHeaderElement headerElement = (SOAPHeaderElement) it.next();
  383.                
  384.                 //Controllo Namespace
  385.                 String namespace = headerElement.getNamespaceURI();
  386.                 String actorCheck = SoapUtils.getSoapActor(headerElement, message.getMessageType());
  387.                
  388.                 boolean actorCompatible = false;
  389.                 if(actor==null) {
  390.                     actorCompatible = (actorCheck==null);
  391.                 }
  392.                 else {
  393.                     actorCompatible = actor.equals(actorCheck);
  394.                 }
  395.                
  396.                 if(actorCompatible && Costanti.WSA_NAMESPACE.equals(namespace)){
  397.                     if(Costanti.WSA_SOAP_HEADER_TO.equals(headerElement.getLocalName())){
  398.                         wsaTO = headerElement;
  399.                     }
  400.                     else if(Costanti.WSA_SOAP_HEADER_FROM.equals(headerElement.getLocalName())){
  401.                         wsaFROM = headerElement;
  402.                     }
  403.                     else if(Costanti.WSA_SOAP_HEADER_ACTION.equals(headerElement.getLocalName())){
  404.                         wsaAction = headerElement;
  405.                     }
  406.                     else if(Costanti.WSA_SOAP_HEADER_ID.equals(headerElement.getLocalName())){
  407.                         wsaID = headerElement;
  408.                     }
  409.                     else if(Costanti.WSA_SOAP_HEADER_RELATES_TO.equals(headerElement.getLocalName())){
  410.                         wsaRelatesTo = headerElement;
  411.                     }
  412.                     else if(Costanti.WSA_SOAP_HEADER_REPLY_TO.equals(headerElement.getLocalName())){
  413.                         wsaReplyTo = headerElement;
  414.                     }
  415.                     else if(Costanti.WSA_SOAP_HEADER_FAULT_TO.equals(headerElement.getLocalName())){
  416.                         wsaFaultTo = headerElement;
  417.                     }
  418.                 }
  419.                
  420.                
  421.             }
  422.             if(wsaTO==null && wsaFROM==null  &&  wsaAction==null && wsaID==null  &&  wsaRelatesTo==null &&  wsaReplyTo==null &&  wsaFaultTo==null){
  423.                 return;
  424.             }
  425.            
  426.             // delete
  427.             if(wsaTO!=null){
  428.                 header.removeChild(wsaTO);
  429.             }
  430.             if(wsaFROM!=null){
  431.                 header.removeChild(wsaFROM);
  432.             }
  433.             if(wsaAction!=null){
  434.                 header.removeChild(wsaAction);
  435.             }
  436.             if(wsaID!=null){
  437.                 header.removeChild(wsaID);
  438.             }  
  439.             if(wsaRelatesTo!=null){
  440.                 header.removeChild(wsaRelatesTo);
  441.             }
  442.             if(wsaReplyTo!=null){
  443.                 header.removeChild(wsaReplyTo);
  444.             }
  445.             if(wsaFaultTo!=null){
  446.                 header.removeChild(wsaFaultTo);
  447.             }
  448.        
  449.         }catch(Exception e){
  450.             throw new MessageException(e.getMessage(),e);
  451.         }
  452.     }
  453. }